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 nullable filter mask in group_by #11207

Merged
merged 1 commit into from
Sep 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PhysicalExpr for FilterExpr {
let predicate = predicate_s.bool()?;

// All values true - don't do anything.
if predicate.all() {
if let Some(true) = predicate.all_kleene() {
Copy link
Collaborator Author

@reswqa reswqa Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If predicate only has null and true, all will return true and return the non-filtered series directly, this looks a bit strange. IIUC, value masked by None should also be filtered out. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do the same as in the select context. I see that we only return the true values there. So yes this seems correct.

return Ok(ac_s);
}
// All values false - create empty groups.
Expand Down
16 changes: 16 additions & 0 deletions py-polars/tests/unit/operations/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ def test_melt_values_predicate_pushdown() -> None:
).to_dict(False) == {"id": [1], "variable": ["asset_key_1"], "value": ["123"]}


def test_group_by_filter_all_true() -> None:
df = pl.DataFrame(
{
"name": ["a", "a", "b", "b"],
"type": [None, 1, 1, None],
"order": [1, 2, 3, 4],
}
)
out = (
df.group_by("name")
.agg([pl.col("order").filter(pl.col("type") == 1).n_unique().alias("n_unique")])
.select("n_unique")
)
assert out.to_dict(False) == {"n_unique": [1, 1]}


def test_filter_is_in_4572() -> None:
df = pl.DataFrame({"id": [1, 2, 1, 2], "k": ["a"] * 2 + ["b"] * 2})
expected = (
Expand Down