Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion committed Sep 30, 2024
1 parent 6645b0d commit 0703188
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
33 changes: 33 additions & 0 deletions crates/polars-plan/src/plans/conversion/dsl_to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,39 @@ pub fn to_alp_impl(lp: DslPlan, ctxt: &mut DslConversionContext) -> PolarsResult
output_schema: None,
filter: None,
},
// `select(pl.all().filter())` -> `filter()`
// `select((pl.all().<exprs>).filter())` -> `select(pl.all().<exprs>).filter()`
DslPlan::Select {
expr,
input,
options,
} if matches!(expr.as_slice(), &[Expr::Filter { .. }]) => {
let Expr::Filter {
input: filter_input,
by: predicate,
} = expr.into_iter().next().unwrap()
else {
unreachable!()
};

let input = match filter_input.as_ref() {
// If it's just a wildcard we don't need the `Select` node.
Expr::Wildcard => input,
_ => Arc::new(DslPlan::Select {
expr: vec![Arc::unwrap_or_clone(filter_input)],
input,
options,
}),
};

return to_alp_impl(
DslPlan::Filter {
input,
predicate: Arc::unwrap_or_clone(predicate),
},
ctxt,
);
},
DslPlan::Select {
expr,
input,
Expand Down
11 changes: 10 additions & 1 deletion py-polars/tests/unit/expr/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,18 @@ def test_filter_all() -> None:
)

q = df.lazy().select(pl.all().filter(~pl.col("p")))
assert "__POLARS_CSER" in q.explain()
# Ensure this is re-written to a `Filter` node during IR conversion.
assert r'SELECTION: col("p").not()' in q.explain()

assert_frame_equal(
q.collect(),
pl.DataFrame({"a": [2, 5], "b": ["q", "t"], "p": False}),
)

q = df.lazy().select((pl.all().reverse()).filter(~pl.col("p")))
assert r'FILTER col("p").not()' in q.explain()

assert_frame_equal(
q.collect(),
pl.DataFrame({"a": [5, 2], "b": ["t", "q"], "p": False}),
)

0 comments on commit 0703188

Please sign in to comment.