Repository navigation
[#39751] Stop rounding Beam SQL AVG to 10 significant digits - #41984
Open
harshilv17 wants to merge 1 commit into
Open
harshilv17 wants to merge 1 commit into
harshilv17 wants to merge 1 commit into
Conversation
AVG divided every sum with MathContext(10), so an average over one row did not equal that row and a BIGINT average near Long.MAX_VALUE came back with the opposite sign. Divide integer sums exactly to an integral result, truncating toward zero as integer division does, and DECIMAL sums with 34 significant digits.
Contributor
|
Assigning reviewers: R: @Abacn added as fallback since no labels match configuration 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.
Fixes #39751.
BeamBuiltinAggregations.Avg.prepareOutputdivided every sum withMathContext(10, HALF_UP), soAVGkept only 10 significant digits:BIGINTrow9223372036854775807came back as-92233720367095516161786500000123came back as1786500000000DECIMAL123456789.99came back as123456790.0Now:
INTEGER,BIGINT,SMALLINTandTINYINTdivide the exactBigDecimalsum to an integral result, truncating toward zero (RoundingMode.DOWN), then convert withintValueExact()and similar. Results that already fit in 10 digits don't change, sinceintValue()truncated the same way, and the average of any values of an integer type always fits that type.DECIMALdivides with 34 significant digits (HALF_UP, decimal128 precision) instead of 10.FLOATandDOUBLEalready divided asdoubleand are unchanged.The issue also mentions that
VarianceFnandCovarianceFnuse the sameMathContext(10). Changing them changes their output, andBeamSqlDslAggregationTesthard-codes the 10-digit results (varsamp1 = 1.666666667), so I left them out of this PR to keep it toAVG. Happy to follow up if you'd like them to keep full precision too.BeamBuiltinAggregationsTestcovers:BIGINTaverage whose sum overflows alongDECIMALprecisionBeamSqlDslAggregationTestandBeamSqlDslAggregationNullableTestpass. TwotestTumbleWindow*cases fail locally when the JVM runs outside UTC and pass withTZ=UTC; they don't useAVG.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, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.