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

fix(rust): fix bug in unneeded projection pruning #5071

Merged
merged 1 commit into from
Oct 2, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,11 @@ impl ProjectionPushDown {
// So we:
// - add the root of the projections to accumulation,
// - also do the complete projection locally to keep the schema (column order) and the alias.

// set this flag outside the loop as we modify within the loop
let has_pushed_down = !acc_projections.is_empty();
for e in &expr {
if !acc_projections.is_empty() {
if has_pushed_down {
// remove projections that are not used upstream
if projections_seen > 0 {
let input_schema = lp_arena.get(input).schema(lp_arena);
Expand All @@ -313,7 +316,7 @@ impl ProjectionPushDown {
// In this query, bar cannot pass this projection, as it would not exist in DF.
// THE ORDER IS IMPORTANT HERE!
// this removes projection names, so any checks ot upstream names should
// be done befor this branch.
// be done before this branch.
for (_, ae) in (&*expr_arena).iter(*e) {
if let AExpr::Alias(_, name) = ae {
if projected_names.remove(name) {
Expand Down
26 changes: 26 additions & 0 deletions polars/tests/it/lazy/projection_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,29 @@ fn test_err_no_found() {
Err(PolarsError::NotFound(_))
));
}

#[test]
fn test_many_aliasing_projections_5070() -> PolarsResult<()> {
let df = df! {
"date" => [1, 2, 3],
"val" => [1, 2, 3],
}?;

let out = df
.lazy()
.filter(col("date").gt(lit(1)))
.select([col("*")])
.with_columns([col("val").max().alias("max")])
.with_column(col("max").alias("diff"))
.with_column((col("val") / col("diff")).alias("output"))
.select([all().exclude(&["max", "diff"])])
.collect()?;
let expected = df![
"date" => [2, 3],
"val" => [2, 3],
"output" => [0, 1],
]?;
assert!(out.frame_equal(&expected));

Ok(())
}