datafusion
datafusion copied to clipboard
array_sum loses precision on integer and decimal arrays
Describe the bug
array_sum casts every element to Float64 and returns Float64, so sums of
integer or decimal arrays are rounded once they pass 2^53. The sum aggregate
over the same values is exact.
To Reproduce
SELECT array_sum([9007199254740993, 1]);
-- 9007199254740992 (Float64)
SELECT sum(v) FROM (VALUES (9007199254740993), (1)) AS t(v);
-- 9007199254740994 (Int64)
Casting the input to List(Decimal128(38, 0)) first doesn't help: the result
is still Float64.
Expected behavior
array_sum follows the sum aggregate's types: Int64 for signed integers, UInt64 for unsigned, a widened decimal for decimals, Float64 for floats. Integer overflow wraps, as sum does.
Additional context
Found while mapping narwhals' list.sum onto DataFusion 55. That backend now refuses integer lists because the result can be silently wrong.
Willingness to contribute
Yes