Skip to content

[Datasets] Multi-aggregations [1/3]: Add basic support for groupby multi-aggregations. - #20044

Merged
ericl merged 4 commits into
ray-project:masterfrom
clarkzinzow:datasets/feat/groupby-multi-aggregation
Nov 5, 2021
Merged

ericl merged 4 commits into
ray-project:masterfrom
clarkzinzow:datasets/feat/groupby-multi-aggregation

Conversation

@clarkzinzow

@clarkzinzow clarkzinzow commented Nov 4, 2021 •

Copy link
Copy Markdown
Contributor

This PR adds basic support for groupby multi-aggregations.

TODOs (for future PRs)

  1. (In review, PR) Translate ds.mean(["col1", "col2"]) to ds.aggregate(Mean("col1"), Mean("col2")) (and likewise for groupby aggregations). I've implemented a proof-of-concept for this locally, but I'm going to add it as a follow-up PR.
  2. (In review, PR) Expose a nicer multi-aggregation API, similar to the Pandas API:
ds.agg(["mean", "min", "max"])
ds.agg({"col1": ["mean", "max"], "col2": ["std", "min"]})

Checks

@clarkzinzow clarkzinzow changed the title [Datasets] Add multi-aggregations. [Datasets] Add groupby multi-aggregations. Nov 4, 2021
Comment thread python/ray/data/grouped_dataset.py Resolved
Comment thread python/ray/data/grouped_dataset.py Resolved

@jjyao jjyao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎉

Comment thread python/ray/data/impl/simple_block.py Resolved
Comment thread python/ray/data/tests/test_dataset.py Resolved
Comment thread python/ray/data/grouped_dataset.py Resolved
Comment thread python/ray/data/impl/arrow_block.py Resolved
>>> ray.data.range(100).aggregate(Max())
>>> ray.data.range_arrow(100).aggregate(Max("value"))
>>> ray.data.range_arrow(100).aggregate(
Max("value"), Mean("value"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we mark our built-in agg as part of the PublicAPI as well? (add to docs, etc.?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Definitely! I'm planning on adding that in the third stacked PR, since we'll have the full API at that point (nicest UX).

@clarkzinzow clarkzinzow changed the title [Datasets] Add groupby multi-aggregations. [Datasets] Multi-aggregations [1/3]: Add basic support for groupby multi-aggregations. Nov 5, 2021
@clarkzinzow

Copy link
Copy Markdown
Contributor Author

Datasets tests are passing, looks good to merge

@ericl
ericl merged commit 6ade6f0 into ray-project:master Nov 5, 2021
goutamvenkat-anyscale pushed a commit that referenced this pull request Sep 18, 2026
…names (#66218)

## Why

Two bugs in `TableBlockAccessor`'s aggregation path, both visible from
the public API.

**A global aggregation over an empty dataset returns `None`.**

```python
ray.data.range(10).filter(lambda row: False).aggregate(Count())
# before: None
# after:  {'count()': 0}
```

#66194 added an early return for zero-row blocks. That is right for a
keyed aggregation — no rows means no groups — but a global aggregation
always has exactly one group, and still owes its identity row. Counting
an empty dataset is `0`, not "no answer".

**Repeated aggregations overwrite each other's results.**

```python
ray.data.range(4).aggregate(Sum("id"), Sum("id"), Sum("id"))
# before: {'sum(id)': 6, 'sum(id)_2': 6}
# after:  {'sum(id)': 6, 'sum(id)_2': 6, 'sum(id)_3': 6}
```

`_resolve_aggregated_column_names` suffixes duplicate names, but
incremented the counter for the *suffixed* name rather than the
original. The original's count never advanced past one, so every
duplicate after the first resolved to `_2` again. Because `_aggregate`
builds rows with `row[name] = accumulator`, two aggregations sharing a
name means one result is silently dropped.

## What changes

- The empty-block early return in `_aggregate` is gated on there being
group keys, and the `num_rows() > 0` check around `accumulate_block` is
restored, so an empty group leaves the accumulators at their identity
values.
- `_resolve_aggregated_column_names` counts the original name and
suffixes until the name is free. Looping rather than suffixing once also
covers a generated suffix colliding with an aggregation explicitly named
that, as in `["a", "a_2", "a"]`. It now takes the names rather than the
aggregations, since that is all it reads.
- `GPUAggregationPlan` carried its own copy of that loop, which resolved
names differently from the CPU path — the same query returned different
column names depending on the shuffle strategy. It now calls the shared
helper.
- Tests for both bugs in `test_dataset_aggregrations.py`.

The name-collision bug predates #66194 — it dates to #20044 and survived
#46668 and #50498; #66194 only lifted the loop into a helper, which is
why `git blame` points there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Aarrya <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…names (ray-project#66218)

## Why

Two bugs in `TableBlockAccessor`'s aggregation path, both visible from
the public API.

**A global aggregation over an empty dataset returns `None`.**

```python
ray.data.range(10).filter(lambda row: False).aggregate(Count())
# before: None
# after:  {'count()': 0}
```

ray-project#66194 added an early return for zero-row blocks. That is right for a
keyed aggregation — no rows means no groups — but a global aggregation
always has exactly one group, and still owes its identity row. Counting
an empty dataset is `0`, not "no answer".

**Repeated aggregations overwrite each other's results.**

```python
ray.data.range(4).aggregate(Sum("id"), Sum("id"), Sum("id"))
# before: {'sum(id)': 6, 'sum(id)_2': 6}
# after:  {'sum(id)': 6, 'sum(id)_2': 6, 'sum(id)_3': 6}
```

`_resolve_aggregated_column_names` suffixes duplicate names, but
incremented the counter for the *suffixed* name rather than the
original. The original's count never advanced past one, so every
duplicate after the first resolved to `_2` again. Because `_aggregate`
builds rows with `row[name] = accumulator`, two aggregations sharing a
name means one result is silently dropped.

## What changes

- The empty-block early return in `_aggregate` is gated on there being
group keys, and the `num_rows() > 0` check around `accumulate_block` is
restored, so an empty group leaves the accumulators at their identity
values.
- `_resolve_aggregated_column_names` counts the original name and
suffixes until the name is free. Looping rather than suffixing once also
covers a generated suffix colliding with an aggregation explicitly named
that, as in `["a", "a_2", "a"]`. It now takes the names rather than the
aggregations, since that is all it reads.
- `GPUAggregationPlan` carried its own copy of that loop, which resolved
names differently from the CPU path — the same query returned different
column names depending on the shuffle strategy. It now calls the shared
helper.
- Tests for both bugs in `test_dataset_aggregrations.py`.

The name-collision bug predates ray-project#66194 — it dates to ray-project#20044 and survived
ray-project#46668 and ray-project#50498; ray-project#66194 only lifted the loop into a helper, which is
why `git blame` points there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Aarrya <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants