Skip to content

feat(firestore): add BSONDecimal128 support - #18395

Open
ohmayr wants to merge 1 commit into
bson-pr1f-regexfrom
bson-pr1g-decimal128
Open

ohmayr wants to merge 1 commit into
bson-pr1f-regexfrom
bson-pr1g-decimal128

Conversation

@ohmayr

@ohmayr ohmayr commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Adds the BSONDecimal128 data type to support 128-bit high-precision decimal field serialization in Google Cloud Firestore according to the official Firestore BSON specification.

  • BSONDecimal128 Container Class: Introduces google.cloud.firestore_v1.bson.BSONDecimal128 for lossless 128-bit decimal storage (e.g. "123.45").
  • Flexible Construction: Accepts str, int, float, decimal.Decimal, or existing BSONDecimal128 instances.
  • Precision Helper: Includes .to_decimal property for converting losslessly to Python standard library decimal.Decimal.
  • Wire Serialization: Implements _to_map_value() converting BSONDecimal128 into the reserved {"__decimal128__": "<string>"} wire map representation.
  • SDK Surface Export: Exported on top-level package namespaces (google.cloud.firestore_v1 and google.cloud.firestore).

Fixes b/562163604 🦕

@ohmayr
ohmayr added this pull request to stack #18386 September 16, 2026 08:51

@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 the BSONDecimal128 class to represent 128-bit BSON decimals in Firestore, along with corresponding integration and unit tests. The review feedback highlights critical issues in the eq and hash implementations of BSONDecimal128, specifically pointing out a transitivity of equality violation and a hash invariant violation when comparing with decimal.Decimal. The reviewer provides suggestions to resolve these issues by comparing the underlying decimal.Decimal values and recommends adding tests to verify equality transitivity and hash consistency.

Comment thread packages/google-cloud-firestore/google/cloud/firestore_v1/bson.py Outdated
Comment thread packages/google-cloud-firestore/tests/unit/v1/test_bson.py
Comment thread packages/google-cloud-firestore/tests/unit/v1/test_bson.py
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 1ef7892 to 9b544a8 Compare September 16, 2026 20:02
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 9b544a8 to 136d4cc Compare September 16, 2026 20:43
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch 2 times, most recently from d2e6a9b to 1ee01b3 Compare September 16, 2026 21:12
@ohmayr
ohmayr marked this pull request as ready for review September 16, 2026 21:13
@ohmayr
ohmayr requested review from a team as code owners September 16, 2026 21:13
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 1ee01b3 to 3b1421c Compare September 16, 2026 21:58
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 3b1421c to 5103b1a Compare September 16, 2026 22:11
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 5103b1a to fe17e56 Compare September 18, 2026 20:06
return self._value

@property
def to_decimal(self) -> decimal.Decimal:

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.

Is this intended to be a property? The name looks like it should be a method


def __eq__(self, other: Any) -> bool:
if isinstance(other, BSONDecimal128):
if self._value.upper() == "NAN" and other._value.upper() == "NAN":

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.

what about float("inf")?

It looks like there are some other special case strings too (e,g, "-NaN"). Maybe we should have some tests around these

except decimal.InvalidOperation:
return False
return NotImplemented

@daniel-sanche daniel-sanche Sep 18, 2026

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.

We should probably implement __float__, so this type can be treated as a number

@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from fe17e56 to 4799274 Compare September 18, 2026 22:11
@ohmayr
ohmayr force-pushed the bson-pr1g-decimal128 branch from 4799274 to 8baccaa Compare September 18, 2026 22:52
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