Apache DataFusion SQL Query Engine
fix: `common_subexpr_eliminate` w/ aggregates and relations (#6199)
Marco Neumann2787e7a36a6b
diff --git a/datafusion/optimizer/src/common_subexpr_eliminate.rs b/datafusion/optimizer/src/common_subexpr_eliminate.rsindex be7481f38d..ae8bb3293d 100644--- a/datafusion/optimizer/src/common_subexpr_eliminate.rs+++ b/datafusion/optimizer/src/common_subexpr_eliminate.rs@@ -267,8 +267,9 @@ impl OptimizerRule for CommonSubexprEliminate {let mut proj_exprs = vec![];for expr in &new_group_expr {- let out_name = expr.to_field(&new_input_schema)?.qualified_name();- proj_exprs.push(Expr::Column(Column::from_name(out_name)));+ let out_col: Column =+ expr.to_field(&new_input_schema)?.qualified_column();+ proj_exprs.push(Expr::Column(out_col));}for (expr_rewritten, expr_orig) inrewritten.into_iter().zip(new_aggr_expr)@@ -986,6 +987,33 @@ mod test {Ok(())}+ #[test]+ fn aggregate_with_releations_and_dots() -> Result<()> {+ let schema = Schema::new(vec![Field::new("col.a", DataType::UInt32, false)]);+ let table_scan = table_scan(Some("table.test"), &schema, None)?.build()?;++ let col_a = Expr::Column(Column::new(Some("table.test"), "col.a"));++ let plan = LogicalPlanBuilder::from(table_scan)+ .aggregate(+ vec![col_a.clone()],+ vec![+ (lit(1u32) + avg(lit(1u32) + col_a.clone())),+ avg(lit(1u32) + col_a),+ ],+ )?+ .build()?;++ let expected = "Projection: table.test.col.a, UInt32(1) + AVG(UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a)UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a AS AVG(UInt32(1) + table.test.col.a), AVG(UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a)UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a AS AVG(UInt32(1) + table.test.col.a)\+ \n Aggregate: groupBy=[[table.test.col.a]], aggr=[[AVG(UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a) AS AVG(UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a)UInt32(1) + table.test.col.atable.test.col.aUInt32(1) AS UInt32(1) + table.test.col.a]]\+ \n Projection: UInt32(1) + table.test.col.a AS UInt32(1) + table.test.col.atable.test.col.aUInt32(1), table.test.col.a\+ \n TableScan: table.test";++ assert_optimized_plan_eq(expected, &plan);++ Ok(())+ }+#[test]fn subexpr_in_same_order() -> Result<()> {let table_scan = test_table_scan()?;