Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support placeholder values in BsonReader and BsonWriter #1652

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jyemin
Copy link
Collaborator

@jyemin jyemin commented Mar 19, 2025

This is an attempt to add support for placeholder values in JSON, so that our Hibernate dialect can support native queries like

{ 
   aggregate: "book",
   pipeline: [
       {
          $match: {
              title: ?
          }
       }
   ]
}

instead of the less intuitive:

{ 
   aggregate: "book",
   pipeline: [
       {
          $match: {
              title: {$undefined: true}
           }
       }
   ]
}

It could also be used for non-native queries, but it's less important there. It would be more to make the query logs look more sensible, i.e.

DEBUG o.h.p.entity.AbstractEntityPersister -  Insert (0): {"insert": "books", "documents": [{"author": ?, "publishYear": ?, "title": ?, "_id": ?}]}
DEBUG o.h.p.entity.AbstractEntityPersister -  Update (0): {"update": "books", "updates": [{"q": {"_id": {"$eq": ?}}, "u": {"$set": {"author": ?, "publishYear": ?, "title": ?}}, "multi": true}]}
DEBUG o.h.p.entity.AbstractEntityPersister -  Delete (0): {"delete": "books", "deletes": [{"q": {"_id": {"$eq": ?}}, "limit": {"$numberInt": "0"}}]}

instead of

DEBUG o.h.p.entity.AbstractEntityPersister -  Insert (0): {"insert": "books", "documents": [{"authorFirstName": {"$undefined": true}, "authorLastName": {"$undefined": true}, "publishYear": {"$undefined": true}, "title": {"$undefined": true}, "_id": {"$undefined": true}}]}
DEBUG o.h.p.entity.AbstractEntityPersister -  Update (0): {"update": "books", "updates": [{"q": {"_id": {"$eq": {"$undefined": true}}}, "u": {"$set": {"authorFirstName": {"$undefined": true}, "authorLastName": {"$undefined": true}, "publishYear": {"$undefined": true}, "title": {"$undefined": true}}}, "multi": true}]}
DEBUG o.h.p.entity.AbstractEntityPersister -  Delete (0): {"delete": "books", "deletes": [{"q": {"_id": {"$eq": {"$undefined": true}}}, "limit": {"$numberInt": "0"}}]}

The problem is that in order to fit in the BsonReader/BsonWriter framework, the change is quite invasive, including

  • Adding writePlaceholder methods to BsonWriter
  • Adding readPlaceholder methods to BsonReader
  • Adding a BsonPlaceholder subclass to BsonValue
  • Adding a PLACEHOLDER value to the BsonType enum
  • Adding a BsonPlaceholderCodec
  • Added asPlaceholder and isPlaceholder to BsonValue
  • Adding doWritePlaceholder method to AbstractBsonWriter and subclasses, including one in BsonBinaryWriter that necessarily throws UnsupportedOperationException since placeholder is not a real BSON type
  • Adding doReadPlaceholder method to AbstractBsonReader and subclasses, including one in BsonBinaryReader that necessarily throws UnsupportedOperationException since placeholder is not a real BSON type

It would be hard to justify all these changes, but I thought it would be interesting for people to look at.

@jyemin jyemin self-assigned this Mar 19, 2025
@@ -250,6 +251,9 @@ public BsonType readBsonType() {
currentValue = visitUUIDConstructor();
} else if ("new".equals(value)) {
visitNew();
} else if ("?".equals(value)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part was easy, since we already support raw strings dues to the shell syntax

@jyemin jyemin force-pushed the bson-placeholder-poc branch 3 times, most recently from 7468aeb to 1975f5b Compare March 19, 2025 19:34
@jyemin jyemin force-pushed the bson-placeholder-poc branch from 1975f5b to 1d6c270 Compare March 20, 2025 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant