Skip to content

Apache DataFusion SQL Query Engine

Fix "Projection references non-aggregate values" by updating `rebase_expr` to use `transform_down` (#8890)

Zhong Xud14f766b5d3c
diff --git a/datafusion/sql/src/utils.rs b/datafusion/sql/src/utils.rs
index 616a2fc749..0dc1258eba 100644
--- a/datafusion/sql/src/utils.rs
+++ b/datafusion/sql/src/utils.rs
@@ -66,7 +66,7 @@ pub(crate) fn rebase_expr(
base_exprs: &[Expr],
plan: &LogicalPlan,
) -> Result<Expr> {
- expr.clone().transform_up(&|nested_expr| {
+ expr.clone().transform_down(&|nested_expr| {
if base_exprs.contains(&nested_expr) {
Ok(Transformed::Yes(expr_as_column_expr(&nested_expr, plan)?))
} else {
diff --git a/datafusion/sqllogictest/test_files/group_by.slt b/datafusion/sqllogictest/test_files/group_by.slt
index 79e6a9357b..c75929049c 100644
--- a/datafusion/sqllogictest/test_files/group_by.slt
+++ b/datafusion/sqllogictest/test_files/group_by.slt
@@ -4853,4 +4853,9 @@ statement error DataFusion error: This feature is not implemented: Conflicting o
SELECT a, b, NTH_VALUE(c, 2 ORDER BY c ASC), NTH_VALUE(c, 3 ORDER BY d ASC)
FROM multiple_ordered_table
GROUP BY a, b
-ORDER BY a, b;
\ No newline at end of file
+ORDER BY a, b;
+
+query II
+SELECT a + 1 AS d, a + 1 + b AS c FROM (SELECT 1 AS a, 2 AS b) GROUP BY a + 1, a + 1 + b;
+----
+2 4