Skip to content

feat(firestore): add BSON cross-type query ordering support - #18405

Open
ohmayr wants to merge 1 commit into
bson-pr2-readsfrom
bson-pr3-ordering
Open

ohmayr wants to merge 1 commit into
bson-pr2-readsfrom
bson-pr3-ordering

Conversation

@ohmayr

@ohmayr ohmayr commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@ohmayr
ohmayr added this pull request to stack #18386 September 16, 2026 22:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for ordering and comparing BSON types (such as BSON min/max keys, object IDs, binaries, regexes, timestamps, and numbers) in Firestore. Feedback on these changes suggests adding defensive checks for keys in BSON regex and timestamp maps to prevent potential KeyError exceptions, as well as simplifying the number comparison logic by removing redundant float attribute checks.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/order.py Outdated
Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/order.py
Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/order.py
@ohmayr
ohmayr marked this pull request as ready for review September 16, 2026 22:51
@ohmayr
ohmayr requested a review from a team as a code owner September 16, 2026 22:51
@ohmayr
ohmayr force-pushed the bson-pr3-ordering branch 3 times, most recently from b92472b to 9e8a85b Compare September 16, 2026 23:18
return TypeOrder.BSON_REGEX
if key == "__request_timestamp__":
return TypeOrder.TIMESTAMP
if "__type__" in fields and fields["__type__"].string_value == "__vector__":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems like there should be a cleaner way to do this

You have to do a similar key->BSONType mapping in the last PR. Maybe we can do something similar, and add an extra cls._get_type_order() field to each BSONType?

Then you could just do something like BSONType._class_for_key(key)._get_type_order()

TypeOrder.BSON_MIN_KEY: 1,
TypeOrder.BOOLEAN: 2,
TypeOrder.NUMBER: 3,
TypeOrder.TIMESTAMP: 4,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

BSON_TIMESTAMP seems to be missing

left_val = left_val.value
if hasattr(right_val, "value"):
right_val = right_val.value
return Order.compare_doubles(float(left_val), float(right_val))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like BSONDecimal can hold larger values than float. maybe we should use decimal.Decimal here?

if hasattr(left_val, "value"):
left_val = left_val.value
if hasattr(right_val, "value"):
right_val = right_val.value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This wouldn't be needed if we implement __int__ and __float__ in the BSON types, so they are automatically treated as numbers (We would still need to compare decimals for BSONDecimal though)

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.

2 participants