Skip to content

[Python] Read MongoDB collections with mixed _id types - #41995

Open
raman118 wants to merge 1 commit into
apache:masterfrom
raman118:fix-mongodb-mixed-id-types
Open

raman118 wants to merge 1 commit into
apache:masterfrom
raman118:fix-mongodb-mixed-id-types

Conversation

@raman118

@raman118 raman118 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #30472

ReadFromMongoDB splits a collection into ranges of _id. If a collection has _id values of different types, for example strings and ObjectIds, the first and last ids are of different types. Splitting then fails while comparing them:

TypeError: '>=' not supported between instances of 'str' and 'ObjectId'

Splitting by range can't work for these collections anyway. MongoDB applies range operators such as $gte/$lt only to values of the same BSON type, so a range like {"$gte": "abc", "$lt": ObjectId(...)} would silently skip documents.

Change

When the ends of the _id range are not both of the same supported type (ObjectId, int or str):

  • split() returns a single bundle, and logs at INFO why.
  • get_range_tracker() returns an unsplittable tracker.
  • read() reads the whole collection, with only the user's filter and projection and no _id range filter, still sorted by _id.

Collections whose _id values are all of one supported type are split exactly as before. Collections with _id types that were never supported, such as floats or embedded documents, used to fail with NotImplementedError in get_range_tracker(). They now also go through the single-bundle path.

Testing

  • New MixedIdTypesTest, run with and without bucket_auto, covers split, read with and without a filter, and an end-to-end ReadFromMongoDB pipeline. It fails without this change and passes with it.
  • The mock collection now sorts and compares values in BSON type order, as MongoDB does.
  • pytest apache_beam/io/mongodbio_test.py: 137 passed.
  • yapf is clean.

Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

🤖 Generated with Claude Code

_BoundedMongoSource splits a collection into ranges of _id. When the
collection has _id values of different types, for example strings and
ObjectIds, the smallest and largest ids are of different types and
splitting fails with a TypeError comparing them. Range queries could not
work anyway, as MongoDB only matches range operators against values of
the same BSON type.

When the ends of the _id range are not of a single supported type
(ObjectId, int or str), read the collection in a single, unsplittable
bundle without an _id range filter. This also covers collections whose
_id values are of other types, which used to fail with
NotImplementedError in get_range_tracker.

Fixes apache#30472

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm added as fallback since no labels match configuration
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Mixed data types in _id field causing pipeline failures (SDK 2.53.0)

1 participant