Fix DynamoDB Rounded error for numbers with trailing zeros#4717
Open
veeceey wants to merge 1 commit intoboto:developfrom
Open
Fix DynamoDB Rounded error for numbers with trailing zeros#4717veeceey wants to merge 1 commit intoboto:developfrom
veeceey wants to merge 1 commit intoboto:developfrom
Conversation
…zeros Numbers like 1234567895171680000000000000000000000000 are valid in DynamoDB (16 significant digits) but cause a decimal.Rounded exception because Python's Decimal treats the trailing zeros as significant, giving 40 digits which exceeds DYNAMODB_CONTEXT's 38-digit precision. The fix normalizes numbers before applying DYNAMODB_CONTEXT to strip non-significant trailing zeros. A separate _NORMALIZE_CONTEXT with wider precision (100) is used so that normalization itself never silently loses significant digits. Fixes boto#4693 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Author
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4693
1234567895171680000000000000000000000000) causedecimal.Roundedexceptions during serialization and deserialization because Python'sDecimalcounts trailing zeros as significant digits, exceedingDYNAMODB_CONTEXT's 38-digit precision limitDYNAMODB_CONTEXTto strip non-significant trailing zeros, using a separate_NORMALIZE_CONTEXTwith wider precision (100 digits) so normalization itself never silently loses significant digitsTypeSerializer._serialize_nandTypeDeserializer._deserialize_n, as well as number setsReproduction
The number
1234567895171680000000000000000000000000has only 16 significant digits but its string representation has 40 characters. DynamoDB supports up to 38 digits of precision, so this number is perfectly valid and is accepted by other AWS SDKs (Go, Java, etc.).Approach
The root cause is that
DYNAMODB_CONTEXT.create_decimal(value)treats all digits in the string representation as significant. A value like"1234567895171680000000000000000000000000"is seen as having 40 digits of precision, which exceeds the 38-digit limit and triggers theRoundedtrap.The fix introduces
_NORMALIZE_CONTEXT(adecimal.Contextwithprec=100and no traps) and calls_NORMALIZE_CONTEXT.normalize(Decimal(value))before passing toDYNAMODB_CONTEXT.create_decimal(). This converts trailing zeros into an exponent (e.g.1.23456789517168E+39) so only significant digits count toward the precision check. The wider precision of 100 ensures normalization never rounds values that fit within DynamoDB's 38-digit limit.Test plan
tests/unit/dynamodb/test_types.pytests pass unchangedtests/unit/dynamodb/)Decimalvalues with trailing zeros