jayzhan211 commented on code in PR #26211:
URL: https://github.com/apache/datafusion/pull/26211#discussion_r4373338441


##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -10295,3 +10295,16 @@ SELECT avg(DISTINCT v ORDER BY v), var(DISTINCT v 
ORDER BY v) FROM agg_order_by_
 
 statement ok
 DROP TABLE agg_order_by_t;
+
+# Repro for issue #17699 / #26661: distinct count with sum(decimal) preserves 
declared type across rewrites
+statement ok
+CREATE TABLE s_decimal_distinct (g varchar, k varchar, x decimal(18,6)) AS 
VALUES ('a', 'u1', 1.5), ('a', 'u2', 2.5), ('b', 'u1', 3.0);
+
+query T R I I

Review Comment:
   The sqllogictest parser rejects `query T R I I` (it fails with `unexpected 
token: "I"`, which is the CI failure), and the decimal column prints with its 
scale. With these two changes the file passes locally:
   
   ```diff
   -query T R I I
   +query TRII
    SELECT g, nullif(sum(x), 0) AS s, count(*) AS n, count(DISTINCT k) AS d 
FROM s_decimal_distinct GROUP BY g ORDER BY g;
    ----
   -a 4 2 2
   -b 3 1 1
   +a 4.000000 2 2
   +b 3.000000 1 1
   ```
   



##########
datafusion/sqllogictest/test_files/single_distinct_to_groupby.slt:
##########
@@ -453,3 +453,35 @@ DROP TABLE empty_t;
 
 statement ok
 DROP TABLE t;
+
+# Repro for issue #26193: single distinct with sum(decimal) preserves declared 
type
+statement ok
+CREATE TABLE t_decimal_distinct AS SELECT * FROM (VALUES (CAST(1.25 AS 
DECIMAL(5,2)), 1), (CAST(2.50 AS DECIMAL(5,2)), 2)) AS v(d, x);
+
+query TT
+SELECT arrow_typeof(s), arrow_typeof(c) FROM (SELECT sum(d) AS s, 
count(DISTINCT x) AS c FROM t_decimal_distinct);
+----
+Decimal128(15, 2) Int64
+
+query TT

Review Comment:
   This expected plan doesn't match what the rule produces, so the file fails 
in CI (the outer aggregate reads `sum(alias2)`, and the current physical plan 
has `DataSourceExec` and no `CoalesceBatchesExec`). Running `cargo test -p 
datafusion-sqllogictest --test sqllogictests -- single_distinct_to_groupby 
--complete` regenerates it; on this branch it gives:
   
   ```diff
    logical_plan
   -01)Projection: CAST(sum(t_decimal_distinct.d) AS Decimal128(15, 2)) AS s, 
count(alias1) AS c
   -02)--Aggregate: groupBy=[[]], aggr=[[sum(t_decimal_distinct.d), 
count(alias1)]]
   -03)----Aggregate: groupBy=[[t_decimal_distinct.x AS alias1]], 
aggr=[[sum(t_decimal_distinct.d)]]
   +01)Projection: CAST(sum(alias2) AS Decimal128(15, 2)) AS s, count(alias1) 
AS c
   +02)--Aggregate: groupBy=[[]], aggr=[[sum(alias2), count(alias1)]]
   +03)----Aggregate: groupBy=[[t_decimal_distinct.x AS alias1]], 
aggr=[[sum(t_decimal_distinct.d) AS alias2]]
    04)------TableScan: t_decimal_distinct projection=[d, x]
    physical_plan
   -01)ProjectionExec: expr=[CAST(sum(t_decimal_distinct.d)@0 AS Decimal128(15, 
2)) as s, count(alias1)@1 as c]
   -02)--AggregateExec: mode=Final, gby=[], aggr=[sum(t_decimal_distinct.d), 
count(alias1)]
   +01)ProjectionExec: expr=[CAST(sum(alias2)@0 AS Decimal128(15, 2)) as s, 
count(alias1)@1 as c]
   +02)--AggregateExec: mode=Final, gby=[], aggr=[sum(alias2), count(alias1)]
    03)----CoalescePartitionsExec
   -04)------AggregateExec: mode=Partial, gby=[], 
aggr=[sum(t_decimal_distinct.d), count(alias1)]
   -05)--------AggregateExec: mode=FinalPartitioned, gby=[alias1@0], 
aggr=[sum(t_decimal_distinct.d)]
   -06)----------CoalesceBatchesExec: target_batch_size=8192
   -07)------------RepartitionExec: partitioning=Hash([alias1@0], 4), 
input_partitions=4
   -08)--------------AggregateExec: mode=Partial, gby=[alias1@1], 
aggr=[sum(t_decimal_distinct.d)]
   -09)----------------RepartitionExec: partitioning=RoundRobinBatch(4), 
input_partitions=1
   -10)------------------MemoryExec: partitions=1, partition_sizes=[1]
   +04)------AggregateExec: mode=Partial, gby=[], aggr=[sum(alias2), 
count(alias1)]
   +05)--------AggregateExec: mode=FinalPartitioned, gby=[alias1@0 as alias1], 
aggr=[sum(t_decimal_distinct.d) as alias2]
   +06)----------RepartitionExec: partitioning=Hash([alias1@0], 4), 
input_partitions=1
   +07)------------AggregateExec: mode=Partial, gby=[x@1 as alias1], 
aggr=[sum(t_decimal_distinct.d) as alias2]
   +08)--------------DataSourceExec: partitions=1, partition_sizes=[1]
   ```
   



-- 
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]

Reply via email to