@@ -2235,15 +2235,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
22352235 normalize_ident ( function. name . 0 [ 0 ] . clone ( ) )
22362236 } ;
22372237
2238- // first, check SQL reserved words
2239- if name == "rollup" {
2240- let args = self . function_args_to_expr ( function. args , schema) ?;
2241- return Ok ( Expr :: GroupingSet ( GroupingSet :: Rollup ( args) ) ) ;
2242- } else if name == "cube" {
2243- let args = self . function_args_to_expr ( function. args , schema) ?;
2244- return Ok ( Expr :: GroupingSet ( GroupingSet :: Cube ( args) ) ) ;
2245- }
2246-
22472238 // next, scalar built-in
22482239 if let Ok ( fun) = BuiltinScalarFunction :: from_str ( & name) {
22492240 let args = self . function_args_to_expr ( function. args , schema) ?;
@@ -2347,6 +2338,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
23472338 }
23482339 }
23492340
2341+ SQLExpr :: Rollup ( exprs) => self . sql_rollup_to_expr ( exprs, schema, planner_context) ,
2342+ SQLExpr :: Cube ( exprs) => self . sql_cube_to_expr ( exprs, schema, planner_context) ,
2343+ SQLExpr :: GroupingSets ( exprs) => self . sql_grouping_sets_to_expr ( exprs, schema, planner_context) ,
2344+
23502345 SQLExpr :: Floor { expr, field : _field } => {
23512346 let fun = BuiltinScalarFunction :: Floor ;
23522347 let args = vec ! [ self . sql_expr_to_logical_expr( * expr, schema, planner_context) ?] ;
@@ -2387,6 +2382,67 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
23872382 } )
23882383 }
23892384
2385+ fn sql_rollup_to_expr (
2386+ & self ,
2387+ exprs : Vec < Vec < SQLExpr > > ,
2388+ schema : & DFSchema ,
2389+ planner_context : & mut PlannerContext ,
2390+ ) -> Result < Expr > {
2391+ let args: Result < Vec < _ > > = exprs
2392+ . into_iter ( )
2393+ . map ( |v| {
2394+ if v. len ( ) != 1 {
2395+ Err ( DataFusionError :: Internal (
2396+ "Tuple expressions are not supported for Rollup expressions"
2397+ . to_string ( ) ,
2398+ ) )
2399+ } else {
2400+ self . sql_expr_to_logical_expr ( v[ 0 ] . clone ( ) , schema, planner_context)
2401+ }
2402+ } )
2403+ . collect ( ) ;
2404+ Ok ( Expr :: GroupingSet ( GroupingSet :: Rollup ( args?) ) )
2405+ }
2406+
2407+ fn sql_cube_to_expr (
2408+ & self ,
2409+ exprs : Vec < Vec < SQLExpr > > ,
2410+ schema : & DFSchema ,
2411+ planner_context : & mut PlannerContext ,
2412+ ) -> Result < Expr > {
2413+ let args: Result < Vec < _ > > = exprs
2414+ . into_iter ( )
2415+ . map ( |v| {
2416+ if v. len ( ) != 1 {
2417+ Err ( DataFusionError :: Internal (
2418+ "Tuple expressions not are supported for Cube expressions"
2419+ . to_string ( ) ,
2420+ ) )
2421+ } else {
2422+ self . sql_expr_to_logical_expr ( v[ 0 ] . clone ( ) , schema, planner_context)
2423+ }
2424+ } )
2425+ . collect ( ) ;
2426+ Ok ( Expr :: GroupingSet ( GroupingSet :: Cube ( args?) ) )
2427+ }
2428+
2429+ fn sql_grouping_sets_to_expr (
2430+ & self ,
2431+ exprs : Vec < Vec < SQLExpr > > ,
2432+ schema : & DFSchema ,
2433+ planner_context : & mut PlannerContext ,
2434+ ) -> Result < Expr > {
2435+ let args: Result < Vec < Vec < _ > > > = exprs
2436+ . into_iter ( )
2437+ . map ( |v| {
2438+ v. into_iter ( )
2439+ . map ( |e| self . sql_expr_to_logical_expr ( e, schema, planner_context) )
2440+ . collect ( )
2441+ } )
2442+ . collect ( ) ;
2443+ Ok ( Expr :: GroupingSet ( GroupingSet :: GroupingSets ( args?) ) )
2444+ }
2445+
23902446 fn parse_exists_subquery (
23912447 & self ,
23922448 subquery : Query ,
@@ -2634,6 +2690,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
26342690 SQLExpr :: Identifier ( i) => i. to_string ( ) ,
26352691 SQLExpr :: Value ( v) => match v {
26362692 Value :: SingleQuotedString ( s) => s. to_string ( ) ,
2693+ Value :: DollarQuotedString ( s) => s. to_string ( ) ,
26372694 Value :: Number ( _, _) | Value :: Boolean ( _) => v. to_string ( ) ,
26382695 Value :: DoubleQuotedString ( _)
26392696 | Value :: UnQuotedString ( _)
@@ -5664,11 +5721,12 @@ mod tests {
56645721 quick_test ( sql, expected) ;
56655722 }
56665723
5667- #[ ignore] // see https://github.com/apache/arrow-datafusion/issues/2469
56685724 #[ test]
56695725 fn aggregate_with_grouping_sets ( ) {
56705726 let sql = "SELECT id, state, age, COUNT(*) FROM person GROUP BY id, GROUPING SETS ((state), (state, age), (id, state))" ;
5671- let expected = "TBD" ;
5727+ let expected = "Projection: person.id, person.state, person.age, COUNT(UInt8(1))\
5728+ \n Aggregate: groupBy=[[person.id, GROUPING SETS ((person.state), (person.state, person.age), (person.id, person.state))]], aggr=[[COUNT(UInt8(1))]]\
5729+ \n TableScan: person";
56725730 quick_test ( sql, expected) ;
56735731 }
56745732
0 commit comments