Skip to content

Commit 04313c1

Browse files
authored
fix: Raise length mismatch in multiple sort_by in group_by (#27772)
1 parent 878246b commit 04313c1

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

crates/polars-expr/src/expressions/sortby.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ impl PhysicalExpr for SortByExpr {
391391

392392
groups?
393393
} else {
394+
let groups_in = ac_in.groups();
395+
for ac in ac_sort_by.iter() {
396+
check_groups(groups_in.as_ref().as_ref(), ac.groups.as_ref().as_ref())?;
397+
}
398+
394399
let groups = ac_sort_by[0].groups();
395400

396401
let groups = RAYON.install(|| {

py-polars/tests/unit/operations/test_sort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,3 +1316,17 @@ def test_sort_maintain_order_all_nulls_27514(
13161316
.collect()
13171317
)
13181318
assert_frame_equal(result, df)
1319+
1320+
1321+
def test_sort_by_multiple_length_mismatch_27759() -> None:
1322+
df = pl.DataFrame(
1323+
{
1324+
"a": [0, 0],
1325+
"b": ["foo", "blah"],
1326+
"c": [False, True],
1327+
"d": [0, 0],
1328+
"e": [0, 0],
1329+
}
1330+
)
1331+
with pytest.raises(pl.exceptions.ShapeError):
1332+
df.group_by("a").agg(pl.col("b").filter("c").sort_by(["d", "e"]))

0 commit comments

Comments
 (0)