Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql planner support for rollup/cube/grouping sets ast nodes #4727

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 32 additions & 11 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,15 +2274,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
normalize_ident(function.name.0[0].clone())
};

// first, check SQL reserved words
if name == "rollup" {
let args = self.function_args_to_expr(function.args, schema)?;
return Ok(Expr::GroupingSet(GroupingSet::Rollup(args)));
} else if name == "cube" {
let args = self.function_args_to_expr(function.args, schema)?;
return Ok(Expr::GroupingSet(GroupingSet::Cube(args)));
}

// next, scalar built-in
if let Ok(fun) = BuiltinScalarFunction::from_str(&name) {
let args = self.function_args_to_expr(function.args, schema)?;
Expand Down Expand Up @@ -2387,6 +2378,35 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}

SQLExpr::Rollup(exprs) => {
let args: Result<Vec<_>> = exprs.into_iter().map(|v| {
if v.len() != 1 {
Err(DataFusionError::Internal("Tuple expressions are not supported for Rollup expressions".to_string()))
} else {
self.sql_expr_to_logical_expr(v[0].clone(), schema, planner_context)
}
}).collect();
Ok(Expr::GroupingSet(GroupingSet::Rollup(args?)))
}

SQLExpr::Cube(exprs) => {
let args: Result<Vec<_>> = exprs.into_iter().map(|v| {
if v.len() != 1 {
Err(DataFusionError::Internal("Tuple expressions not are supported for Cube expressions".to_string()))
} else {
self.sql_expr_to_logical_expr(v[0].clone(), schema, planner_context)
}
}).collect();
Ok(Expr::GroupingSet(GroupingSet::Cube(args?)))
}

SQLExpr::GroupingSets(exprs) => {
let args: Result<Vec<Vec<_>>> = exprs.into_iter().map(|v| {
v.into_iter().map(|e| self.sql_expr_to_logical_expr(e, schema, planner_context)).collect()
}).collect();
Ok(Expr::GroupingSet(GroupingSet::GroupingSets(args?)))
}

SQLExpr::Floor { expr, field: _field } => {
let fun = BuiltinScalarFunction::Floor;
let args = vec![self.sql_expr_to_logical_expr(*expr, schema, planner_context)?];
Expand Down Expand Up @@ -5829,11 +5849,12 @@ mod tests {
quick_test(sql, expected);
}

#[ignore] // see https://github.com/apache/arrow-datafusion/issues/2469
#[test]
fn aggregate_with_grouping_sets() {
let sql = "SELECT id, state, age, COUNT(*) FROM person GROUP BY id, GROUPING SETS ((state), (state, age), (id, state))";
let expected = "TBD";
let expected = "Projection: person.id, person.state, person.age, COUNT(UInt8(1))\
\n Aggregate: groupBy=[[person.id, GROUPING SETS ((person.state), (person.state, person.age), (person.id, person.state))]], aggr=[[COUNT(UInt8(1))]]\
\n TableScan: person";
quick_test(sql, expected);
}

Expand Down