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

Implement order preserving groupby in cudf-polars #16555

Merged
merged 5 commits into from
Aug 22, 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
29 changes: 26 additions & 3 deletions python/cudf_polars/cudf_polars/dsl/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,6 @@ def check_agg(agg: expr.Expr) -> int:

def __post_init__(self) -> None:
"""Check whether all the aggregations are implemented."""
if self.options.rolling is None and self.maintain_order:
raise NotImplementedError("Maintaining order in groupby")
if self.options.rolling:
raise NotImplementedError(
"rolling window/groupby"
Expand Down Expand Up @@ -621,7 +619,32 @@ def evaluate(self, *, cache: MutableMapping[int, DataFrame]) -> DataFrame:
results = [
req.evaluate(result_subs, mapping=mapping) for req in self.agg_requests
]
return DataFrame(broadcast(*result_keys, *results)).slice(self.options.slice)
broadcasted = broadcast(*result_keys, *results)
result_keys = broadcasted[: len(result_keys)]
results = broadcasted[len(result_keys) :]
# Handle order preservation of groups
# like cudf classic does
# https://github.com/rapidsai/cudf/blob/5780c4d8fb5afac2e04988a2ff5531f94c22d3a3/python/cudf/cudf/core/groupby/groupby.py#L723-L743
if self.maintain_order and not sorted:
left = plc.stream_compaction.stable_distinct(
plc.Table([k.obj for k in keys]),
list(range(group_keys.num_columns())),
plc.stream_compaction.DuplicateKeepOption.KEEP_FIRST,
plc.types.NullEquality.EQUAL,
plc.types.NanEquality.ALL_EQUAL,
)
right = plc.Table([key.obj for key in result_keys])
_, indices = plc.join.left_join(left, right, plc.types.NullEquality.EQUAL)
ordered_table = plc.copying.gather(
plc.Table([col.obj for col in broadcasted]),
indices,
plc.copying.OutOfBoundsPolicy.DONT_CHECK,
)
broadcasted = [
NamedColumn(reordered, b.name)
for reordered, b in zip(ordered_table.columns(), broadcasted)
]
return DataFrame(broadcasted).slice(self.options.slice)


@dataclasses.dataclass
Expand Down
26 changes: 17 additions & 9 deletions python/cudf_polars/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def df():
params=[
[pl.col("key1")],
[pl.col("key2")],
[pl.col("key1"), pl.lit(1)],
[pl.col("key1") * pl.col("key2")],
[pl.col("key1"), pl.col("key2")],
[pl.col("key1") == pl.col("key2")],
Expand Down Expand Up @@ -59,15 +60,7 @@ def exprs(request):


@pytest.fixture(
params=[
False,
pytest.param(
True,
marks=pytest.mark.xfail(
reason="Maintaining order in groupby not implemented"
),
),
],
params=[False, True],
ids=["no_maintain_order", "maintain_order"],
)
def maintain_order(request):
Expand Down Expand Up @@ -127,6 +120,21 @@ def test_groupby_unsupported(df, expr):
assert_ir_translation_raises(q, NotImplementedError)


def test_groupby_null_keys(maintain_order):
df = pl.LazyFrame(
{
"key": pl.Series([1, float("nan"), 2, None, 2, None], dtype=pl.Float64()),
"value": [-1, 2, 1, 2, 3, 4],
}
)

q = df.group_by("key", maintain_order=maintain_order).agg(pl.col("value").min())
if not maintain_order:
q = q.sort("key")

assert_gpu_result_equal(q)


@pytest.mark.xfail(reason="https://github.com/pola-rs/polars/issues/17513")
def test_groupby_minmax_with_nan():
df = pl.LazyFrame(
Expand Down
Loading