This is an automated email from the ASF dual-hosted git repository.

akurmustafa pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 33fc1104c1 Add alias check to optimize projections merge (#8438)
33fc1104c1 is described below

commit 33fc1104c199904fb0ee019546ac6587e7088316
Author: Mustafa Akur <[email protected]>
AuthorDate: Thu Dec 7 09:23:40 2023 +0300

    Add alias check to optimize projections merge (#8438)
    
    * Relax schema check for optimize projections.
    
    * Minor changes
    
    * Update datafusion/optimizer/src/optimize_projections.rs
    
    Co-authored-by: jakevin <[email protected]>
    
    ---------
    
    Co-authored-by: jakevin <[email protected]>
---
 datafusion/optimizer/src/optimize_projections.rs | 15 ++++++++++++---
 datafusion/sqllogictest/test_files/select.slt    |  9 +++++++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/datafusion/optimizer/src/optimize_projections.rs 
b/datafusion/optimizer/src/optimize_projections.rs
index bbf704a83c..440e12cc26 100644
--- a/datafusion/optimizer/src/optimize_projections.rs
+++ b/datafusion/optimizer/src/optimize_projections.rs
@@ -405,9 +405,18 @@ fn merge_consecutive_projections(proj: &Projection) -> 
Result<Option<Projection>
         .iter()
         .map(|expr| rewrite_expr(expr, prev_projection))
         .collect::<Result<Option<Vec<_>>>>()?;
-    new_exprs
-        .map(|exprs| Projection::try_new(exprs, prev_projection.input.clone()))
-        .transpose()
+    if let Some(new_exprs) = new_exprs {
+        let new_exprs = new_exprs
+            .into_iter()
+            .zip(proj.expr.iter())
+            .map(|(new_expr, old_expr)| {
+                new_expr.alias_if_changed(old_expr.name_for_alias()?)
+            })
+            .collect::<Result<Vec<_>>>()?;
+        Projection::try_new(new_exprs, prev_projection.input.clone()).map(Some)
+    } else {
+        Ok(None)
+    }
 }
 
 /// Trim Expression
diff --git a/datafusion/sqllogictest/test_files/select.slt 
b/datafusion/sqllogictest/test_files/select.slt
index 3f3befd85a..bbb05b6cff 100644
--- a/datafusion/sqllogictest/test_files/select.slt
+++ b/datafusion/sqllogictest/test_files/select.slt
@@ -1056,3 +1056,12 @@ drop table annotated_data_finite2;
 
 statement ok
 drop table t;
+
+statement ok
+create table t(x bigint, y bigint) as values (1,2), (1,3);
+
+query II
+select z+1, y from (select x+1 as z, y from t) where y > 1;
+----
+3 2
+3 3

Reply via email to