@@ -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 ( ) ;
0 commit comments