fix: increase AVG MathContext precision from 10 to 34 to prevent BIGINT overflow and silent precision loss - #39765
Open
waterWang wants to merge 1 commit into
Open
Conversation
…NT overflow and silent precision loss
Contributor
|
Assigning reviewers: R: @Abacn for label java. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
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.
Problem
#39751 — SQL
AVGrounds every result to 10 significant digits via the fixedMathContext(10, RoundingMode.HALF_UP)atBeamBuiltinAggregations.java:93. All AVG subtypes (INT32,INT64,INT16,INT8,FLOAT,DOUBLE,DECIMAL) go throughprepareOutputwhich divides the accumulator with this precision.Consequences:
AVGover a singleBIGINTvalue9223372036854775807returns-9223372036709551616— sign flipped due to 10-digit rounding then.longValue()truncationAVG(1786500000123)→1786500000000)DECIMAL(18,2)values lose precision (123456789.99→123456790.0)Fix
Increase the
MathContextprecision from 10 to 34 significant digits (MathContext.DECIMAL128precision, with the sameHALF_UProunding mode). This keeps all values representable in any of the supported input types exact through the division:INT64/BIGINTvalues need up to 19 significant digits — 34 is sufficient, so the sign flip and truncation disappearDECIMAL(38,10)can need up to 49 digits, but 34 covers the overwhelmingly commonDECIMAL(p,s)ranges and matchesMathContext.DECIMAL128semantics (sum/count where the division terminates is exact; non-terminating divisions are rounded to 34 digits instead of 10)The alternative of dropping the MathContext would turn non-terminating divisions into
ArithmeticException; threading the declared outputRelDataTypeprecision/scale fromAggregateCallis a larger design change touchingVAR_*,STDDEV_*,COVAR_*coders — a follow-up for the component owners. This fix addresses the reported correctness bugs with a minimal, targeted change.Fixes #39751