Skip to content

Apache DataFusion SQL Query Engine

[dbolympics] fix: cast outer projection expr to declared field type in SingleDistinctToGroupBy (#26211)

jdymitarai833f499da96a
diff --git a/datafusion/optimizer/src/single_distinct_to_groupby.rs b/datafusion/optimizer/src/single_distinct_to_groupby.rs
index aabfec83c0..28a1ed995e 100644
--- a/datafusion/optimizer/src/single_distinct_to_groupby.rs
+++ b/datafusion/optimizer/src/single_distinct_to_groupby.rs
@@ -30,7 +30,7 @@ use datafusion_expr::builder::project;
use datafusion_expr::expr::AggregateFunctionParams;
use datafusion_expr::expr_schema::ExprSchemable;
use datafusion_expr::{
- AggregateUDF, Expr, col,
+ AggregateUDF, Expr, cast, col,
expr::AggregateFunction,
lit,
logical_plan::{Aggregate, LogicalPlan},
@@ -402,6 +402,12 @@ impl OptimizerRule for SingleDistinctToGroupBy {
.map(|(expr, _)| expr.clone())
.collect();
+ let outer_aggr = LogicalPlan::Aggregate(Aggregate::try_new(
+ Arc::new(inner_agg),
+ outer_group_exprs,
+ outer_aggr_exprs,
+ )?);
+
// so the aggregates are displayed in the same way even after the rewrite
// this optimizer has two kinds of alias:
// - group_by aggr
@@ -417,15 +423,16 @@ impl OptimizerRule for SingleDistinctToGroupBy {
.chain(outer_proj_exprs.into_iter().enumerate().map(|(idx, expr)| {
let idx = idx + group_size;
let (qualifier, field) = schema.qualified_field(idx);
+ let current_type = outer_aggr.schema().field(idx).data_type();
+ let expr = if current_type != field.data_type() {
+ cast(expr, field.data_type().clone())
+ } else {
+ expr
+ };
expr.alias_qualified(qualifier.cloned(), field.name())
}))
.collect();
- let outer_aggr = LogicalPlan::Aggregate(Aggregate::try_new(
- Arc::new(inner_agg),
- outer_group_exprs,
- outer_aggr_exprs,
- )?);
Ok(Transformed::yes(project(outer_aggr, alias_expr)?))
}
_ => Ok(Transformed::no(plan)),
@@ -1310,4 +1317,30 @@ mod tests {
",
)
}
+
+ #[test]
+ fn sum_decimal_with_distinct_preserves_declared_type() -> Result<()> {
+ let schema = Schema::new(vec![
+ Field::new("d", DataType::Decimal128(5, 2), false),
+ Field::new("x", DataType::Int32, false),
+ ]);
+ let table_scan = table_scan(Some("test"), &schema, None)?.build()?;
+
+ let plan = LogicalPlanBuilder::from(table_scan)
+ .aggregate(
+ Vec::<Expr>::new(),
+ vec![sum(col("d")), count_distinct(col("x"))],
+ )?
+ .build()?;
+
+ let rule: Arc<dyn OptimizerRule + Send + Sync> =
+ Arc::new(SingleDistinctToGroupBy::new());
+ let optimizer = Optimizer::with_rules(vec![rule]);
+ let optimized_plan = optimizer
+ .optimize(plan.clone(), &RegistryOptimizerContext::new(), |_, _| {})
+ .expect("failed to optimize plan");
+
+ assert_eq!(plan.schema().as_ref(), optimized_plan.schema().as_ref());
+ Ok(())
+ }
}
diff --git a/datafusion/sqllogictest/test_files/aggregate.slt b/datafusion/sqllogictest/test_files/aggregate.slt
index bbbf8ebea9..61a6f4578f 100644
--- a/datafusion/sqllogictest/test_files/aggregate.slt
+++ b/datafusion/sqllogictest/test_files/aggregate.slt
@@ -10629,3 +10629,17 @@ DROP TABLE a;
statement ok
DROP TABLE b;
+
+
+# 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 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.000000 2 2
+b 3.000000 1 1
+
+statement ok
+DROP TABLE s_decimal_distinct;
diff --git a/datafusion/sqllogictest/test_files/single_distinct_to_groupby.slt b/datafusion/sqllogictest/test_files/single_distinct_to_groupby.slt
index fddfc661b8..def09f4a03 100644
--- a/datafusion/sqllogictest/test_files/single_distinct_to_groupby.slt
+++ b/datafusion/sqllogictest/test_files/single_distinct_to_groupby.slt
@@ -453,3 +453,33 @@ 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
+EXPLAIN SELECT sum(d) AS s, count(DISTINCT x) AS c FROM t_decimal_distinct;
+----
+logical_plan
+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(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(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]
+
+statement ok
+DROP TABLE t_decimal_distinct;