waterWang opened a new pull request, #39765:
URL: https://github.com/apache/beam/pull/39765

   ## Problem
   
   [#39751](https://github.com/apache/beam/issues/39751) — SQL `AVG` rounds 
every result to 10 significant digits via the fixed `MathContext(10, 
RoundingMode.HALF_UP)` at `BeamBuiltinAggregations.java:93`. All AVG subtypes 
(`INT32`, `INT64`, `INT16`, `INT8`, `FLOAT`, `DOUBLE`, `DECIMAL`) go through 
`prepareOutput` which divides the accumulator with this precision.
   
   Consequences:
   - `AVG` over a single `BIGINT` value `9223372036854775807` returns 
`-9223372036709551616` — **sign flipped** due to 10-digit rounding then 
`.longValue()` truncation
   - Values above 10 significant digits (ids, epoch-millis, cent-denominated 
amounts) are silently rounded (e.g. `AVG(1786500000123)` → `1786500000000`)
   - `DECIMAL(18,2)` values lose precision (`123456789.99` → `123456790.0`)
   
   ## Fix
   
   Increase the `MathContext` precision from 10 to 34 significant digits 
(`MathContext.DECIMAL128` precision, with the same `HALF_UP` rounding mode). 
This keeps all values representable in any of the supported input types exact 
through the division:
   - `INT64`/`BIGINT` values need up to 19 significant digits — 34 is 
sufficient, so the sign flip and truncation disappear
   - `DECIMAL(38,10)` can need up to 49 digits, but 34 covers the 
overwhelmingly common `DECIMAL(p,s)` ranges and matches 
`MathContext.DECIMAL128` semantics (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 output 
`RelDataType` precision/scale from `AggregateCall` is a larger design change 
touching `VAR_*`, `STDDEV_*`, `COVAR_*` coders — a follow-up for the component 
owners. This fix addresses the reported correctness bugs with a minimal, 
targeted change.
   
   Fixes #39751


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to