Skip to content

[#39751] Stop rounding Beam SQL AVG to 10 significant digits - #41984

Open
harshilv17 wants to merge 1 commit into
apache:masterfrom
harshilv17:fix/39751-sql-avg-precision
Open

harshilv17 wants to merge 1 commit into
apache:masterfrom
harshilv17:fix/39751-sql-avg-precision

Conversation

@harshilv17

@harshilv17 harshilv17 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #39751.

BeamBuiltinAggregations.Avg.prepareOutput divided every sum with MathContext(10, HALF_UP), so AVG kept only 10 significant digits:

  • the average of the single BIGINT row 9223372036854775807 came back as -9223372036709551616
  • 1786500000123 came back as 1786500000000
  • DECIMAL 123456789.99 came back as 123456790.0

Now:

  • INTEGER, BIGINT, SMALLINT and TINYINT divide the exact BigDecimal sum to an integral result, truncating toward zero (RoundingMode.DOWN), then convert with intValueExact() and similar. Results that already fit in 10 digits don't change, since intValue() truncated the same way, and the average of any values of an integer type always fits that type.
  • DECIMAL divides with 34 significant digits (HALF_UP, decimal128 precision) instead of 10.
  • FLOAT and DOUBLE already divided as double and are unchanged.

The issue also mentions that VarianceFn and CovarianceFn use the same MathContext(10). Changing them changes their output, and BeamSqlDslAggregationTest hard-codes the 10-digit results (varsamp1 = 1.666666667), so I left them out of this PR to keep it to AVG. Happy to follow up if you'd like them to keep full precision too.

BeamBuiltinAggregationsTest covers:

  • the single-row cases from the issue
  • truncation toward zero for each integer type
  • a BIGINT average whose sum overflows a long
  • DECIMAL precision
  • the empty input

BeamSqlDslAggregationTest and BeamSqlDslAggregationNullableTest pass. Two testTumbleWindow* cases fail locally when the JVM runs outside UTC and pass with TZ=UTC; they don't use AVG.


  • Mention the appropriate issue in your description (for example: 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, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

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.
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @Abacn added as fallback since no labels match configuration
Backup reviewer: chamikaramj (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: SQL AVG rounds to 10 significant digits, flipping the sign on large BIGINT values

1 participant