edubraqd opened a new issue, #24898: URL: https://github.com/apache/datafusion/issues/24898
### Describe the bug `DecimalAverager::try_new` (`datafusion/functions-aggregate-common/src/utils.rs`) computes `10^sum_scale` and `10^target_scale` with `pow_wrapping(scale as u32)`. For a negative scale the exponent wraps to a huge value and the wrapped power is 0. `DecimalAverager::avg` then evaluates `target_mul.div_wrapping(sum_mul)` with `sum_mul == 0` and panics. All four decimal widths are affected (`Decimal32`, `Decimal64`, `Decimal128`, `Decimal256`); the plain aggregate, the grouped aggregate and `avg(DISTINCT ...)` all go through the same helper. ### To Reproduce ```sql SELECT avg(x) FROM (VALUES (arrow_cast(1, 'Decimal128(10, -2)'))) t(x); ``` ```text thread 'main' panicked at library/core/src/num/mod.rs:475:5: attempt to divide by zero ``` (`arrow_array::arithmetic::div_wrapping` called from `DecimalAverager::avg`, `datafusion/functions-aggregate-common/src/utils.rs`.) ### Expected behavior `avg` of `100` and `200` at scale -2 (i.e. 10 000 and 20 000) returns `15000.00` as `Decimal128(14, 2)`, the type `Avg::return_type` already declares for that input. ### Additional context The two factors are only ever used as the ratio `10^(target_scale - sum_scale)`, which is a plain non-negative exponent whenever the target scale is not smaller than the input scale (already enforced). Found while running a corpus of extreme-value literals against a debug build of `datafusion-cli`. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
