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: Fix filter incorrectly pushed past struct unnest when unnested column name matches upper column name #19638

Merged
merged 4 commits into from
Nov 6, 2024
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 @@ -576,6 +576,28 @@ impl<'a> PredicatePushDown<'a> {
expr_arena,
))
},
FunctionIR::Unnest { columns } => {
let exclude = columns.iter().cloned().collect::<PlHashSet<_>>();

let local_predicates =
transfer_to_local_by_name(expr_arena, &mut acc_predicates, |x| {
exclude.contains(x)
});

let lp = self.pushdown_and_continue(
lp,
acc_predicates,
lp_arena,
expr_arena,
false,
)?;
Ok(self.optional_apply_predicate(
lp,
local_predicates,
lp_arena,
expr_arena,
))
},
_ => self.pushdown_and_continue(
lp,
acc_predicates,
Expand Down
42 changes: 42 additions & 0 deletions py-polars/tests/unit/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,45 @@ def test_predicate_push_down_list_gather_17492() -> None:
.filter(pl.col("val").list.get(1, null_on_oob=True) == 1)
.explain()
)


def test_predicate_pushdown_struct_unnest_19632() -> None:
lf = pl.LazyFrame({"a": [{"a": 1, "b": 2}]}).unnest("a")

q = lf.filter(pl.col("a") == 1)
plan = q.explain()

assert "FILTER" in plan
assert plan.index("FILTER") < plan.index("UNNEST")

assert_frame_equal(
q.collect(),
pl.DataFrame({"a": 1, "b": 2}),
)

# With `pl.struct()`
lf = pl.LazyFrame({"a": 1, "b": 2}).select(pl.struct(pl.all())).unnest("a")

q = lf.filter(pl.col("a") == 1)
plan = q.explain()

assert "FILTER" in plan
assert plan.index("FILTER") < plan.index("UNNEST")

assert_frame_equal(
q.collect(),
pl.DataFrame({"a": 1, "b": 2}),
)

# With `value_counts()`
lf = pl.LazyFrame({"a": [1]}).select(pl.col("a").value_counts()).unnest("a")

q = lf.filter(pl.col("a") == 1)
plan = q.explain()

assert plan.index("FILTER") < plan.index("UNNEST")

assert_frame_equal(
q.collect(),
pl.DataFrame({"a": 1, "count": 1}, schema={"a": pl.Int64, "count": pl.UInt32}),
)