Skip to content

Commit

Permalink
fix: Don't remove sort if first/last strategy is set in unique (#20481)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Dec 27, 2024
1 parent a35c66c commit 3aaf4c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/polars-plan/src/plans/optimizer/set_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ pub(super) fn set_order_flags(
options.maintain_order = false;
continue;
}
if !options.maintain_order {
if matches!(
options.keep_strategy,
UniqueKeepStrategy::First | UniqueKeepStrategy::Last
) {
maintain_order_above = true;
} else if !options.maintain_order {
maintain_order_above = false;
}
},
Expand Down
25 changes: 25 additions & 0 deletions py-polars/tests/unit/operations/unique/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,28 @@ def test_categorical_updated_revmap_unique_20233() -> None:
)

assert_series_equal(s.unique(), pl.Series("a", ["D"], pl.Categorical))


def test_unique_check_order_20480() -> None:
df = pl.DataFrame(
[
{
"key": "some_key",
"value": "second",
"number": 2,
},
{
"key": "some_key",
"value": "first",
"number": 1,
},
]
)
assert (
df.lazy()
.sort("key", "number")
.unique(subset="key", keep="first")
.collect()["number"]
.item()
== 1
)

0 comments on commit 3aaf4c2

Please sign in to comment.