Skip to content

Commit 0566b55

Browse files
cetra3claude
andauthored
fix: preserve grouping set member names during type coercion (#25891)
## Which issue does this PR close? - Closes #25890. ## Rationale for this change `GROUP BY ROLLUP`, `CUBE`, or `GROUPING SETS` fails during analysis when a member is a scalar function with an implicitly coerced literal argument, such as `date_bin('5 minutes', ts)`: ``` type_coercion caused by Schema error: No field named "date_bin(Utf8(""5 minutes""),t.ts)". Did you mean '"ROLLUP (date_bin(Utf8(""5 minutes""),t.ts))"'? ``` Type coercion now turns the literal into an interval value, which renames the member. `TypeCoercion` then restored the original name by aliasing the whole `Expr::GroupingSet`. An aliased grouping set is no longer recognized as a grouping set: it becomes one ordinary group key named `ROLLUP (...)`, with no `__grouping_id`, and the projection above can no longer find the member. ## What changes are included in this PR? `TypeCoercion` now coerces each grouping set member individually through `map_children`, saving and restoring each member's name, and leaves the grouping set itself unaliased. This is the same approach `SimplifyExpressions` has used since #14888. ## What is the testing strategy for this PR? - New sqllogictest cases in `grouping.slt`: `ROLLUP` over the coerced member with `grouping()` in the select list, and `GROUPING SETS` with a qualified column next to the coerced member, which is repeated across sets. Both fail without this change. - New unit test `scalar_udf_in_grouping_set` in `type_coercion.rs`, checking the analyzed plan shape and that a second coercion pass leaves the plan unchanged. ## Are there any user-facing changes? Queries of this shape now plan and run. No public API changes. ## AI assistance This change was prepared with AI assistance. Assumptions called out for reviewers: - The fix is scoped to `TypeCoercion`. `SimplifyExpressions` already carries the same grouping set workaround, and other rules that use `NamePreserver` on an `Aggregate` could in principle hit the same whole-grouping-set alias. Moving this into `NamePreserver` would fix it in one place, but changes the public `SavedName` API, so I left that for a possible follow-up. - Skipping `TypeCoercionRewriter` on the `Expr::GroupingSet` node itself relies on the rewriter passing grouping sets through unchanged, which it does today. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 1814c6c commit 0566b55

2 files changed

Lines changed: 84 additions & 4 deletions

File tree

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ fn analyze_internal(
177177
TypeCoercionRewriter::new(&schema).with_session_time_zone(session_time_zone);
178178

179179
let name_preserver = NamePreserver::new(&plan);
180-
// apply coercion rewrite all expressions in the plan individually
181-
plan.map_expressions(|expr| {
180+
let mut coerce_expr = |expr: Expr| {
182181
let original_name = name_preserver.save(&expr);
183182

184183
// A lambda variable carries the field recorded when the plan was built, which
@@ -192,6 +191,17 @@ fn analyze_internal(
192191

193192
expr.rewrite(&mut expr_rewrite)
194193
.map(|transformed| transformed.update_data(|e| original_name.restore(e)))
194+
};
195+
196+
// apply coercion rewrite all expressions in the plan individually
197+
plan.map_expressions(|expr| {
198+
// Preserve the names of grouping set members, not of the grouping set itself.
199+
// An aliased grouping set is no longer recognized as a grouping set.
200+
if let Expr::GroupingSet(_) = &expr {
201+
expr.map_children(&mut coerce_expr)
202+
} else {
203+
coerce_expr(expr)
204+
}
195205
})?
196206
// some plans need extra coercion after their expressions are coerced
197207
.map_data(|plan| expr_rewrite.coerce_plan(plan))?
@@ -1775,13 +1785,13 @@ mod test {
17751785
use datafusion_expr::expr::{
17761786
self, InSubquery, Like, ScalarFunction, SetComparison, SetQuantifier,
17771787
};
1778-
use datafusion_expr::logical_plan::{EmptyRelation, Projection, Sort};
1788+
use datafusion_expr::logical_plan::{EmptyRelation, Projection, Sort, table_scan};
17791789
use datafusion_expr::test::function_stub::avg_udaf;
17801790
use datafusion_expr::{
17811791
AccumulatorFactoryFunction, AggregateUDF, BinaryExpr, Case, ColumnarValue, Expr,
17821792
ExprSchemable, Filter, Limit, LogicalPlan, Operator, ScalarFunctionArgs,
17831793
ScalarUDF, ScalarUDFImpl, Signature, SimpleAggregateUDF, Subquery, Union,
1784-
Volatility, cast, col, create_udaf, is_true, lit, placeholder,
1794+
Volatility, cast, col, create_udaf, grouping_set, is_true, lit, placeholder,
17851795
};
17861796
use datafusion_functions_aggregate::average::AvgAccumulator;
17871797

@@ -2373,6 +2383,41 @@ mod test {
23732383
)
23742384
}
23752385

2386+
#[test]
2387+
fn scalar_udf_in_grouping_set() -> Result<()> {
2388+
let udf = ScalarUDF::from(TestScalarUDF {
2389+
signature: Signature::uniform(1, vec![DataType::Float32], Volatility::Stable),
2390+
})
2391+
.call(vec![lit(123_i32)]);
2392+
let schema = Schema::new(vec![Field::new("a", Utf8, false)]);
2393+
let plan = table_scan(Some("t"), &schema, None)?
2394+
.aggregate(
2395+
vec![grouping_set(vec![
2396+
vec![col("t.a"), udf.clone()],
2397+
vec![udf],
2398+
vec![],
2399+
])],
2400+
Vec::<Expr>::new(),
2401+
)?
2402+
.build()?;
2403+
2404+
// Each renamed member is aliased to its original name, not the grouping set
2405+
let analyzed = TypeCoercion::new().analyze(plan, &ConfigOptions::default())?;
2406+
assert_snapshot!(
2407+
analyzed,
2408+
@r#"
2409+
Aggregate: groupBy=[[GROUPING SETS ((t.a, TestScalarUDF(Float32(123)) AS TestScalarUDF(Int32(123))), (TestScalarUDF(Float32(123)) AS TestScalarUDF(Int32(123))), ())]], aggr=[[]]
2410+
TableScan: t
2411+
"#
2412+
);
2413+
2414+
// A second pass leaves the plan unchanged
2415+
let reanalyzed =
2416+
TypeCoercion::new().analyze(analyzed.clone(), &ConfigOptions::default())?;
2417+
assert_eq!(analyzed, reanalyzed);
2418+
Ok(())
2419+
}
2420+
23762421
#[test]
23772422
fn scalar_udf_invalid_input() -> Result<()> {
23782423
let empty = empty();

‎datafusion/sqllogictest/test_files/grouping.slt‎

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

0 commit comments

Comments
 (0)