Skip to content

Commit e36da75

Browse files
cetra3claude
andcommitted
fix: preserve grouping set member names during type coercion
Type coercion can rename a grouping set member, for example when a literal argument such as `date_bin('5 minutes', ts)` is coerced to an interval. The original name was then restored by aliasing the whole grouping set, which is no longer recognized as a grouping set, so the aggregate lost `__grouping_id` and its member columns. Coerce each grouping set member individually instead, as `SimplifyExpressions` already does. Closes apache#25890 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 76adbad commit e36da75

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

‎datafusion/optimizer/src/analyzer/type_coercion.rs‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,21 @@ fn analyze_internal(
152152
let mut expr_rewrite = TypeCoercionRewriter::new(&schema);
153153

154154
let name_preserver = NamePreserver::new(&plan);
155-
// apply coercion rewrite all expressions in the plan individually
156-
plan.map_expressions(|expr| {
155+
let mut coerce_expr = |expr: Expr| {
157156
let original_name = name_preserver.save(&expr);
158157
expr.rewrite(&mut expr_rewrite)
159158
.map(|transformed| transformed.update_data(|e| original_name.restore(e)))
159+
};
160+
161+
// apply coercion rewrite all expressions in the plan individually
162+
plan.map_expressions(|expr| {
163+
// Preserve the names of grouping set members, not of the grouping set itself.
164+
// An aliased grouping set is no longer recognized as a grouping set.
165+
if let Expr::GroupingSet(_) = &expr {
166+
expr.map_children(&mut coerce_expr)
167+
} else {
168+
coerce_expr(expr)
169+
}
160170
})?
161171
// some plans need extra coercion after their expressions are coerced
162172
.map_data(|plan| expr_rewrite.coerce_plan(plan))?
@@ -1621,24 +1631,24 @@ mod test {
16211631
use arrow::datatypes::{DataType, Field, Schema, SchemaBuilder, TimeUnit};
16221632
use insta::assert_snapshot;
16231633

1624-
use crate::analyzer::Analyzer;
16251634
use crate::analyzer::type_coercion::{
16261635
TypeCoercion, TypeCoercionRewriter, coerce_case_expression,
16271636
};
1637+
use crate::analyzer::{Analyzer, AnalyzerRule};
16281638
use crate::assert_analyzed_plan_with_config_eq_snapshot;
16291639
use datafusion_common::config::ConfigOptions;
16301640
use datafusion_common::tree_node::{TransformedResult, TreeNode};
16311641
use datafusion_common::{
16321642
DFSchema, DFSchemaRef, Result, ScalarValue, Spans, TableReference,
16331643
};
16341644
use datafusion_expr::expr::{self, InSubquery, Like, ScalarFunction};
1635-
use datafusion_expr::logical_plan::{EmptyRelation, Projection, Sort};
1645+
use datafusion_expr::logical_plan::{EmptyRelation, Projection, Sort, table_scan};
16361646
use datafusion_expr::test::function_stub::avg_udaf;
16371647
use datafusion_expr::{
16381648
AccumulatorFactoryFunction, AggregateUDF, BinaryExpr, Case, ColumnarValue, Expr,
16391649
ExprSchemable, Filter, LogicalPlan, Operator, ScalarFunctionArgs, ScalarUDF,
16401650
ScalarUDFImpl, Signature, SimpleAggregateUDF, Subquery, Union, Volatility, cast,
1641-
col, create_udaf, is_true, lit,
1651+
col, create_udaf, grouping_set, is_true, lit,
16421652
};
16431653
use datafusion_functions_aggregate::average::AvgAccumulator;
16441654

@@ -2217,6 +2227,41 @@ mod test {
22172227
)
22182228
}
22192229

2230+
#[test]
2231+
fn scalar_udf_in_grouping_set() -> Result<()> {
2232+
let udf = ScalarUDF::from(TestScalarUDF {
2233+
signature: Signature::uniform(1, vec![DataType::Float32], Volatility::Stable),
2234+
})
2235+
.call(vec![lit(123_i32)]);
2236+
let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
2237+
let plan = table_scan(Some("t"), &schema, None)?
2238+
.aggregate(
2239+
vec![grouping_set(vec![
2240+
vec![col("t.a"), udf.clone()],
2241+
vec![udf],
2242+
vec![],
2243+
])],
2244+
Vec::<Expr>::new(),
2245+
)?
2246+
.build()?;
2247+
2248+
// Each renamed member is aliased to its original name, not the grouping set
2249+
let analyzed = TypeCoercion::new().analyze(plan, &ConfigOptions::default())?;
2250+
assert_snapshot!(
2251+
analyzed,
2252+
@r#"
2253+
Aggregate: groupBy=[[GROUPING SETS ((t.a, TestScalarUDF(Float32(123)) AS TestScalarUDF(Int32(123))), (TestScalarUDF(Float32(123)) AS TestScalarUDF(Int32(123))), ())]], aggr=[[]]
2254+
TableScan: t
2255+
"#
2256+
);
2257+
2258+
// A second pass leaves the plan unchanged
2259+
let reanalyzed =
2260+
TypeCoercion::new().analyze(analyzed.clone(), &ConfigOptions::default())?;
2261+
assert_eq!(analyzed, reanalyzed);
2262+
Ok(())
2263+
}
2264+
22202265
#[test]
22212266
fn scalar_udf_invalid_input() -> Result<()> {
22222267
let empty = empty();

‎datafusion/sqllogictest/test_files/grouping.slt‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,38 @@ query TT
439439
SELECT MIN(p), MAX(p) FROM (SELECT p FROM hive_partitioned_empty GROUP BY p)
440440
----
441441
NULL NULL
442+
443+
# Grouping-set keys keep their names when type coercion rewrites a member.
444+
# `date_bin('5 minutes', ts)` has its Utf8 stride literal coerced to an interval,
445+
# which renames the member expression.
446+
statement ok
447+
CREATE TABLE grouping_coerced (ts TIMESTAMP, k VARCHAR, calls BIGINT) AS VALUES
448+
('1970-01-01T00:00:00'::timestamp, 'a', 1),
449+
('1970-01-01T00:01:00'::timestamp, 'b', 2),
450+
('1970-01-01T00:05:00'::timestamp, 'a', 4);
451+
452+
query PII
453+
SELECT date_bin('5 minutes', ts) AS bucket, grouping(date_bin('5 minutes', ts)), SUM(calls)
454+
FROM grouping_coerced GROUP BY ROLLUP(bucket) ORDER BY bucket NULLS FIRST
455+
----
456+
NULL 1 7
457+
1970-01-01T00:00:00 0 3
458+
1970-01-01T00:05:00 0 4
459+
460+
# A qualified column next to the coerced member, with the coerced member repeated
461+
# across sets
462+
query TPI
463+
SELECT grouping_coerced.k, date_bin('5 minutes', grouping_coerced.ts) AS bucket, SUM(calls)
464+
FROM grouping_coerced
465+
GROUP BY GROUPING SETS ((grouping_coerced.k, bucket), (bucket), ())
466+
ORDER BY grouping_coerced.k NULLS FIRST, bucket NULLS FIRST
467+
----
468+
NULL NULL 7
469+
NULL 1970-01-01T00:00:00 3
470+
NULL 1970-01-01T00:05:00 4
471+
a 1970-01-01T00:00:00 1
472+
a 1970-01-01T00:05:00 4
473+
b 1970-01-01T00:00:00 2
474+
475+
statement ok
476+
DROP TABLE grouping_coerced;

0 commit comments

Comments
 (0)