Apache DataFusion SQL Query Engine
fix(substrait): consuming AggregateRel as last node (#12875)
Tornike Gurgenidze87c2081d3e3c
diff --git a/datafusion/substrait/src/logical_plan/consumer.rs b/datafusion/substrait/src/logical_plan/consumer.rsindex 3cafd64321..c727f784ee 100644--- a/datafusion/substrait/src/logical_plan/consumer.rs+++ b/datafusion/substrait/src/logical_plan/consumer.rs@@ -229,14 +229,16 @@ pub async fn from_substrait_plan(match plan {// If the last node of the plan produces expressions, bake the renames into those expressions.// This isn't necessary for correctness, but helps with roundtrip tests.- LogicalPlan::Projection(p) => Ok(LogicalPlan::Projection(Projection::try_new(rename_expressions(p.expr, p.input.schema(), &renamed_schema)?, p.input)?)),+ LogicalPlan::Projection(p) => Ok(LogicalPlan::Projection(Projection::try_new(rename_expressions(p.expr, p.input.schema(), renamed_schema.fields())?, p.input)?)),LogicalPlan::Aggregate(a) => {- let new_aggr_exprs = rename_expressions(a.aggr_expr, a.input.schema(), &renamed_schema)?;- Ok(LogicalPlan::Aggregate(Aggregate::try_new(a.input, a.group_expr, new_aggr_exprs)?))+ let (group_fields, expr_fields) = renamed_schema.fields().split_at(a.group_expr.len());+ let new_group_exprs = rename_expressions(a.group_expr, a.input.schema(), group_fields)?;+ let new_aggr_exprs = rename_expressions(a.aggr_expr, a.input.schema(), expr_fields)?;+ Ok(LogicalPlan::Aggregate(Aggregate::try_new(a.input, new_group_exprs, new_aggr_exprs)?))},// There are probably more plans where we could bake things in, can add them later as needed.// Otherwise, add a new Project to handle the renaming.- _ => Ok(LogicalPlan::Projection(Projection::try_new(rename_expressions(plan.schema().columns().iter().map(|c| col(c.to_owned())), plan.schema(), &renamed_schema)?, Arc::new(plan))?))+ _ => Ok(LogicalPlan::Projection(Projection::try_new(rename_expressions(plan.schema().columns().iter().map(|c| col(c.to_owned())), plan.schema(), renamed_schema.fields())?, Arc::new(plan))?))}}},@@ -364,11 +366,11 @@ pub fn apply_masking(fn rename_expressions(exprs: impl IntoIterator<Item = Expr>,input_schema: &DFSchema,- new_schema: &DFSchema,+ new_schema_fields: &[Arc<Field>],) -> Result<Vec<Expr>> {exprs.into_iter()- .zip(new_schema.fields())+ .zip(new_schema_fields).map(|(old_expr, new_field)| {// Check if type (i.e. nested struct field names) match, use Cast to rename if neededlet new_expr = if &old_expr.get_type(input_schema)? != new_field.data_type() {diff --git a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rsindex d60fe90388..ae87dad015 100644--- a/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs+++ b/datafusion/substrait/tests/cases/roundtrip_logical_plan.rs@@ -663,6 +663,19 @@ async fn simple_intersect() -> Result<()> {.await}+#[tokio::test]+async fn aggregate_wo_projection_consume() -> Result<()> {+ let proto_plan =+ read_json("tests/testdata/test_plans/aggregate_no_project.substrait.json");++ assert_expected_plan_substrait(+ proto_plan,+ "Aggregate: groupBy=[[data.a]], aggr=[[count(data.a) AS countA]]\+ \n TableScan: data projection=[a]",+ )+ .await+}+#[tokio::test]async fn simple_intersect_consume() -> Result<()> {let proto_plan = read_json("tests/testdata/test_plans/intersect.substrait.json");@@ -1122,12 +1135,29 @@ async fn assert_expected_plan(Ok(())}+async fn assert_expected_plan_substrait(+ substrait_plan: Plan,+ expected_plan_str: &str,+) -> Result<()> {+ let ctx = create_context().await?;++ let plan = from_substrait_plan(&ctx, &substrait_plan).await?;++ let plan = ctx.state().optimize(&plan)?;++ let planstr = format!("{plan}");+ assert_eq!(planstr, expected_plan_str);++ Ok(())+}+async fn assert_substrait_sql(substrait_plan: Plan, sql: &str) -> Result<()> {let ctx = create_context().await?;let expected = ctx.sql(sql).await?.into_optimized_plan()?;let plan = from_substrait_plan(&ctx, &substrait_plan).await?;+let plan = ctx.state().optimize(&plan)?;let planstr = format!("{plan}");diff --git a/datafusion/substrait/tests/testdata/test_plans/aggregate_no_project.substrait.json b/datafusion/substrait/tests/testdata/test_plans/aggregate_no_project.substrait.jsonnew file mode 100644index 0000000000..ed8675b968--- /dev/null+++ b/datafusion/substrait/tests/testdata/test_plans/aggregate_no_project.substrait.json@@ -0,0 +1,97 @@+{+ "extensionUris": [+ {+ "uri": "https://github.com/substrait-io/substrait/blob/main/extensions/functions_aggregate_generic.yaml"+ }+ ],+ "extensions": [+ {+ "extensionFunction": {+ "functionAnchor": 185,+ "name": "count:any"+ }+ }+ ],+ "relations": [+ {+ "root": {+ "input": {+ "aggregate": {+ "input": {+ "read": {+ "common": {+ "direct": {}+ },+ "baseSchema": {+ "names": [+ "a"+ ],+ "struct": {+ "types": [+ {+ "i64": {+ "nullability": "NULLABILITY_NULLABLE"+ }+ }+ ],+ "nullability": "NULLABILITY_NULLABLE"+ }+ },+ "namedTable": {+ "names": [+ "data"+ ]+ }+ }+ },+ "groupings": [+ {+ "groupingExpressions": [+ {+ "selection": {+ "directReference": {+ "structField": {}+ },+ "rootReference": {}+ }+ }+ ]+ }+ ],+ "measures": [+ {+ "measure": {+ "functionReference": 185,+ "phase": "AGGREGATION_PHASE_INITIAL_TO_RESULT",+ "outputType": {+ "i64": {}+ },+ "arguments": [+ {+ "value": {+ "selection": {+ "directReference": {+ "structField": {}+ },+ "rootReference": {}+ }+ }+ }+ ]+ }+ }+ ]+ }+ },+ "names": [+ "a",+ "countA"+ ]+ }+ }+ ],+ "version": {+ "minorNumber": 54,+ "producer": "subframe"+ }+}\ No newline at end of file