Skip to content

Commit

Permalink
fix: Sum after filter in aggregation context sometimes returned NULL (#…
Browse files Browse the repository at this point in the history
…14752)

Co-authored-by: Ritchie Vink <ritchie46@gmail.com>
  • Loading branch information
nameexhaustion and ritchie46 committed Feb 29, 2024
1 parent fe42166 commit a2f5cf2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/polars-core/src/frame/group_by/aggregations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ pub fn _use_rolling_kernels(groups: &GroupsSlice, chunks: &[ArrayRef]) -> bool {
let [first_offset, first_len] = groups[0];
let second_offset = groups[1][0];

second_offset < (first_offset + first_len) && chunks.len() == 1
second_offset >= first_offset // Prevent false positive from regular group-by that has out of order slices.
// Rolling group-by is expected to have monotonically increasing slices.
&& second_offset < (first_offset + first_len)
&& chunks.len() == 1
},
}
}
Expand Down
25 changes: 25 additions & 0 deletions py-polars/tests/unit/operations/test_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,28 @@ def test_agg_filter_over_empty_df_13610() -> None:
out = df.group_by("a").agg(pl.col("b").filter(pl.col("b").shift()))
expected = pl.DataFrame(schema={"a": pl.Int64, "b": pl.List(pl.Boolean)})
assert_frame_equal(out, expected)


@pytest.mark.slow()
def test_agg_empty_sum_after_filter_14734() -> None:
f = (
pl.DataFrame({"a": [1, 2], "b": [1, 2]})
.lazy()
.group_by("a")
.agg(pl.col("b").filter(pl.lit(False)).sum())
.collect
)

last = f()

# We need both possible output orders, which should happen within
# 1000 iterations (during testing it usually happens within 10).
limit = 1000
i = 0
while (curr := f()).equals(last):
i += 1
assert i != limit

expect = pl.Series("b", [0, 0]).to_frame()
assert_frame_equal(expect, last.select("b"))
assert_frame_equal(expect, curr.select("b"))

0 comments on commit a2f5cf2

Please sign in to comment.