Skip to content

PYTHON-3075 bulk_write does not apply CodecOptions to upserted_ids result #840

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

Merged
merged 7 commits into from
Feb 2, 2022

Conversation

blink1073
Copy link
Member

No description provided.

@juliusgeo
Copy link
Contributor

LGTM!

pymongo/bulk.py Outdated
_id = doc['_id']
if rep == UuidRepresentation.UNSPECIFIED:
if isinstance(_id, uuid.UUID):
_id = Binary.from_uuid(_id)
Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't figure out how to trigger this condition

Copy link
Member

Choose a reason for hiding this comment

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

This condition will only happen on pymongo 3.

pymongo/bulk.py Outdated
_id = doc['_id']
if rep == UuidRepresentation.UNSPECIFIED:
if isinstance(_id, uuid.UUID):
_id = Binary.from_uuid(_id)
Copy link
Member

Choose a reason for hiding this comment

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

This condition will only happen on pymongo 3.

pymongo/bulk.py Outdated
@@ -317,10 +329,10 @@ def _execute_command(self, generator, write_concern, session,
# Synthesize the full bulk result without modifying the
# current one because this write operation may be retried.
full = copy.deepcopy(full_result)
_merge_command(run, full, run.idx_offset, result)
_merge_command(run, full, run.idx_offset, result, codec_options)
Copy link
Member

@ShaneHarvey ShaneHarvey Jan 31, 2022

Choose a reason for hiding this comment

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

One problem with this approach is that it doesn't handle _id that are subdocuments, eg: _id={'f': Binary(), 'f2': ...}. Could you add a test for this as well?

Instead of converting the result after the fact, we should ensure that BulkWriteContext.execute() decodes the command response with the correct uuid_representation/CodecOptions to begin with. This will involve passing the _Bulk.collection or _Bulk.collection.codec_options to the BulkWriteContext to use for decoding.

Edit: I see that we already pass the _Bulk.collection.codec_options to the BulkWriteContext. The issue is that BulkWriteContext.write_command() does not utilize it when paring the command response.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool, fixed the decoding in the latest commit. I'll address the tests in the morning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the subdocument test

@@ -376,6 +379,27 @@ def test_client_generated_upsert_id(self):
{'index': 2, '_id': 2}]},
result.bulk_api_result)

def test_upsert_uuid_standard(self):
options = CodecOptions(uuid_representation=UuidRepresentation.STANDARD)
Copy link
Member

Choose a reason for hiding this comment

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

Nice. Could you add a similar test to ensure that UuidRepresentation.UNSPECIFIED results in Binary instances?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I can tell we can't do that because it raises a ValueError.

Copy link
Member

Choose a reason for hiding this comment

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

Yes the test will need to convert the uuids to Binary manually before inserting them:

uuids = [Binary.from_uuid(uuid.uuid4()) for _ in range(3)]

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@blink1073 blink1073 requested a review from ShaneHarvey February 1, 2022 22:47
Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

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

Sorry I just realized we're missing the encrypted bulk case. We should add a similar test (just one is enough) to test_encryption.py.

@@ -798,7 +798,7 @@ def write_command(self, cmd, request_id, msg, docs):
self._start(cmd, request_id, docs)
start = datetime.datetime.now()
try:
reply = self.sock_info.write_command(request_id, msg)
reply = self.sock_info.write_command(request_id, msg, self.codec)
Copy link
Member

@ShaneHarvey ShaneHarvey Feb 1, 2022

Choose a reason for hiding this comment

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

I suspect we need a similar change in _EncryptedBulkWriteContext.execute().

Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't sure how to test that locally, waiting for evergreen

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

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

LGTM!

@blink1073 blink1073 merged commit abfa0d3 into mongodb:master Feb 2, 2022
@blink1073 blink1073 deleted the PYTHON-3075 branch February 2, 2022 19:54
blink1073 added a commit to blink1073/mongo-python-driver that referenced this pull request Feb 2, 2022
blink1073 added a commit to blink1073/mongo-python-driver that referenced this pull request Feb 2, 2022
juliusgeo pushed a commit to juliusgeo/mongo-python-driver that referenced this pull request Apr 5, 2022
juliusgeo pushed a commit to juliusgeo/mongo-python-driver that referenced this pull request Apr 7, 2022
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.

3 participants