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 Expr.over with order_by did not take effect if group keys were sorted #18947

Merged
merged 5 commits into from
Sep 27, 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
12 changes: 3 additions & 9 deletions crates/polars-expr/src/expressions/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ impl WindowExpr {
fn determine_map_strategy(
&self,
agg_state: &AggState,
sorted_keys: bool,
gb: &GroupBy,
) -> PolarsResult<MapStrategy> {
match (self.mapping, agg_state) {
Expand All @@ -334,13 +333,8 @@ impl WindowExpr {
// no explicit aggregations, map over the groups
//`(col("x").sum() * col("y")).over("groups")`
(WindowMapping::GroupsToRows, AggState::AggregatedList(_)) => {
if sorted_keys {
if let GroupsProxy::Idx(g) = gb.get_groups() {
debug_assert!(g.is_sorted_flag())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GroupsProxy::Idx must be explicitly mapped back to the row positions regardless of whether it was sorted

}
// GroupsProxy::Slice is always sorted

// Note that group columns must be sorted for this to make sense!!!
if let GroupsProxy::Slice { .. } = gb.get_groups() {
// Result can be directly exploded if the input was sorted.
Ok(MapStrategy::Explode)
} else {
Ok(MapStrategy::Map)
Expand Down Expand Up @@ -516,7 +510,7 @@ impl PhysicalExpr for WindowExpr {
let mut ac = self.run_aggregation(df, state, &gb)?;

use MapStrategy::*;
match self.determine_map_strategy(ac.agg_state(), sorted_keys, &gb)? {
match self.determine_map_strategy(ac.agg_state(), &gb)? {
Nothing => {
let mut out = ac.flat_naive().into_owned();

Expand Down
2 changes: 0 additions & 2 deletions crates/polars/tests/it/lazy/expressions/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ fn test_sort_by_in_groups() -> PolarsResult<()> {
col("cars"),
col("A")
.sort_by([col("B")], SortMultipleOptions::default())
.implode()
.over([col("cars")])
.explode()
Copy link
Collaborator Author

@nameexhaustion nameexhaustion Sep 27, 2024

Choose a reason for hiding this comment

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

This query internally created a DataFrame of height 5 containing a list[_] type column of length 2 (the imploded aggregation result), which it then exploded 🤯 out to the correct height, where the correctness of the result was guaranteed because the DataFrame was sorted beforehand 😂

.alias("sorted_A_by_B"),
])
.collect()?;
Expand Down
18 changes: 18 additions & 0 deletions py-polars/tests/unit/operations/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,21 @@ def test_lit_window_broadcast() -> None:
assert pl.DataFrame({"a": [1, 1, 2]}).select(pl.lit(0).over("a").alias("a"))[
"a"
].to_list() == [0, 0, 0]


def test_order_by_sorted_keys_18943() -> None:
df = pl.DataFrame(
{
"g": [1, 1, 1, 1],
"t": [4, 3, 2, 1],
"x": [10, 20, 30, 40],
}
)

expect = pl.DataFrame({"x": [100, 90, 70, 40]})

out = df.select(pl.col("x").cum_sum().over("g", order_by="t"))
assert_frame_equal(out, expect)

out = df.set_sorted("g").select(pl.col("x").cum_sum().over("g", order_by="t"))
assert_frame_equal(out, expect)