datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

sum() over DECIMAL silently wraps on overflow instead of erroring

Open Shahzeb-DataEngineer opened this issue 1 month ago • 5 comments

Describe the bug

sum() over a DECIMAL column silently wraps around on overflow and returns a wrong result, instead of raising an arithmetic overflow error.

Summing two positive DECIMAL(38, 0) values near the type maximum returns a negative number. The returned value also has 39 digits, which violates the declared result type of the aggregate (Decimal128(38, 0)).

This is inconsistent with the rest of the engine for the same overflow:

  • scalar decimal + and * raise Arrow error: Arithmetic overflow
  • avg() over the same Decimal256 data raises Arithmetic Overflow in AvgAccumulator

Only sum() wraps, and it does so silently.

To Reproduce

SELECT arrow_typeof(sum(v)), sum(v)
FROM (VALUES (CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0))),
             (CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0)))) t(v);
-- Decimal128(38, 0) | -140282366920938463463374607431768211458     <-- wrapped, 39 digits

SELECT k, sum(v) FROM (VALUES (1, CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0))),
                              (1, CAST('99999999999999999999999999999999999999' AS DECIMAL(38,0))),
                              (2, CAST('5' AS DECIMAL(38,0)))) t(k,v) GROUP BY k ORDER BY k;
-- 1 | -140282366920938463463374607431768211458
-- 2 | 5

The same wrapping reproduces for DECIMAL(76, 0) / Decimal256 (seven rows of 76 nines):

SELECT sum(v) FROM ...;
-- -45792089237316195423570985008687907853269984665640564039457584007913129639943  (wrapped)
SELECT avg(v) FROM ...;
-- Execution error: Arithmetic Overflow in AvgAccumulator

Expected behavior

sum() over a decimal column should raise an arithmetic overflow error when the true sum is not representable in the result type, consistent with scalar decimal arithmetic and with avg().

Note that this report is specifically about decimal sums. Integer sum() wrapping matches DataFusion's documented lenient non-ANSI integer arithmetic behavior, and I am not proposing to change integer behavior.

Additional context

The decimal paths of the SUM accumulator in datafusion/functions-aggregate/src/sum.rs use wrapping addition (add_wrapping) in the groups accumulator, update_batch, the sliding accumulator, and retract_batch. Batch partials also come from arrow::compute::sum, which wraps as well.

Willingness to contribute: I plan to contribute a fix for this bug independently, and will open a PR shortly.

Shahzeb-DataEngineer avatar Oct 23 '26 08:10 Shahzeb-DataEngineer

We can expand sum the way we do internally in avg, but the current this seems generally consistent with integers to me.

AdamGS avatar Oct 23 '26 09:10 AdamGS

Thanks Adam. The PR (#26652) already takes the avg route for decimals: checked accumulation that errors with Arithmetic Overflow in SumAccumulator, plus the precision bound. Integers are intentionally untouched since their wrapping is documented. The inconsistency I was aiming at is within decimal itself: scalar add and multiply error on overflow, avg errors, only sum wraps, and it can return a value outside its declared precision (39 digits for a Decimal(38,0) in the repro). Happy to adjust the approach if you see a better shape for it.

Shahzeb-DataEngineer avatar Oct 23 '26 10:10 Shahzeb-DataEngineer

I'd like to take this as the first slice of the wrong-results EPIC #26672. The previous PR was closed for being too broad and for disabling the grouped accumulator without calling it out. I'll keep this focused on the decimal accumulator paths, make the grouped-accumulator behavior explicit, and add reproducer tests for Decimal128 and Decimal256 before changing the implementation.

vinycalves avatar Nov 12 '26 14:11 vinycalves

@vinycalves you can tag me when you have a PR

AdamGS avatar Nov 19 '26 19:11 AdamGS

@AdamGS the branch fix/decimal-sum-overflow is pushed with checked decimal accumulation in the row, sliding and distinct sum paths, an explicit groups-accumulator fallback, and Decimal128/Decimal256 tests. The unit tests and decimal.slt pass. I'll open the PR and tag you as soon as one of my current three open PRs is merged.

vinycalves avatar Nov 19 '26 20:11 vinycalves