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 3 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
34 changes: 31 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,37 @@ 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:
wence- marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't have to do this if keys_are_sorted was true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point,
skipped this block for the sorted case.

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],
)
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
_, indices = plc.join.left_join(left, right, plc.types.NullEquality.EQUAL)
ordered_tbl = plc.copying.gather(
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
plc.Table([col.obj for col in result_keys + results]),
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
indices,
plc.copying.OutOfBoundsPolicy.DONT_CHECK,
)
result_keys = ordered_tbl.columns()[: len(keys)]
results = ordered_tbl.columns()[len(keys) :]
result_keys = [NamedColumn(gk, k.name) for gk, k in zip(result_keys, keys)]
results = [
NamedColumn(res, req.name)
for res, req in zip(results, self.agg_requests)
]
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
return DataFrame(result_keys + results).slice(self.options.slice)
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved


@dataclasses.dataclass
Expand Down
25 changes: 16 additions & 9 deletions python/cudf_polars/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,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 +119,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