Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alias check to optimize projections merge #8438

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1041,3 +1041,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