Skip to content

Commit

Permalink
fix(rust): fix bug in unneeded projection pruning (#5071)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 2, 2022
1 parent 74fb89e commit ab6fb25
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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(())
}

0 comments on commit ab6fb25

Please sign in to comment.