You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[dbolympics] sum of a decimal returns a wider type than the plan schema declares when the query has a DISTINCT aggregate #26193
For sum(d) next to a DISTINCT aggregate, the declared type of the result and the type of the returned batches differ. The plan schema says Decimal128(15, 2). The returned column is Decimal128(25, 2).
SingleDistinctToGroupBy rewrites the query to a sum of sums. The outer sum widens the precision a second time, and nothing casts the result back to the declared type.
To Reproduce
In datafusion-cli 54.0.0:
CREATETABLEtASSELECT*FROM (VALUES (CAST(1.25ASDECIMAL(5,2)), 1), (CAST(2.50ASDECIMAL(5,2)), 2)) AS v(d, x);
-- Declared type: Decimal128(15, 2)
DESCRIBE SELECTsum(d) AS s, count(DISTINCT x) AS c FROM t;
-- Returned type: Decimal128(25, 2)SELECT arrow_typeof(s) FROM (SELECTsum(d) AS s, count(DISTINCT x) AS c FROM t);
-- Without the DISTINCT aggregate: Decimal128(15, 2)SELECT arrow_typeof(sum(d)) FROM t;
EXPLAIN shows the rewrite:
Projection: sum(alias2) AS s, count(alias1) AS c
Aggregate: groupBy=[[]], aggr=[[sum(alias2), count(alias1)]]
Aggregate: groupBy=[[t.x AS alias1]], aggr=[[sum(t.d) AS alias2]]
TableScan: t projection=[d, x]
Expected behavior
The returned batches have the declared type, Decimal128(15, 2). The rewrite casts the outer sum to the type of the original aggregate.
Additional context
The optimizer accepts the rewritten plan because the schema check treats two Decimal128 types as equal when only their precision differs. A client that reads the declared schema and then the batches sees two different types.
changed the title [-]`sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/-][+][dbolympics] `sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/+]on Oct 24, 2026
Activity
[-]`sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/-][+][dbolympics] `sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/+]alamb commentedon Oct 24, 2026
Affects DB Olympics, see: #26661
[dbolympics] fix: cast outer projection expr to declared field type i…