Skip to content

Commit

Permalink
Add alias check to optimize projections merge (#8438)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mustafasrepo and jackwener authored Dec 7, 2023
1 parent c8e1c84 commit 33fc110
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 12 additions & 3 deletions datafusion/optimizer/src/optimize_projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions datafusion/sqllogictest/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 33fc110

Please sign in to comment.