adriangb opened a new issue, #26193:
URL: https://github.com/apache/datafusion/issues/26193
### Describe the bug
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:
```sql
CREATE TABLE t AS
SELECT * FROM (VALUES (CAST(1.25 AS DECIMAL(5,2)), 1), (CAST(2.50 AS
DECIMAL(5,2)), 2)) AS v(d, x);
-- Declared type: Decimal128(15, 2)
DESCRIBE SELECT sum(d) AS s, count(DISTINCT x) AS c FROM t;
-- Returned type: Decimal128(25, 2)
SELECT arrow_typeof(s) FROM (SELECT sum(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.
--
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]