Skip to content

[Data] Fix empty global aggregation and duplicate aggregation column names - #66218

Merged
goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
AarryaSaraf:data-fix-empty-global-aggregation
Sep 18, 2026
Merged

goutamvenkat-anyscale merged 3 commits into
ray-project:masterfrom
AarryaSaraf:data-fix-empty-global-aggregation

Conversation

@AarryaSaraf

Copy link
Copy Markdown
Contributor

Why

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

A global aggregation over an empty dataset returns None.

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.

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

…names

Signed-off-by: Aarrya <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@AarryaSaraf AarryaSaraf added the data Ray Data-related issues label Sep 16, 2026
@AarryaSaraf
AarryaSaraf marked this pull request as ready for review September 16, 2026 23:33
@AarryaSaraf
AarryaSaraf requested a review from a team as a code owner September 16, 2026 23:33
@AarryaSaraf AarryaSaraf added the go add ONLY when ready to merge, run all tests label Sep 16, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors and improves the aggregation name resolution logic to handle name collisions more robustly, ensuring that suffixed names do not collide with explicitly defined names. It updates _resolve_aggregated_column_names to accept string names and use a loop to resolve conflicts. Additionally, it corrects the behavior of global aggregations over empty datasets, ensuring they emit identity rows (e.g., count() = 0) rather than returning an empty table, while keyed aggregations on empty datasets continue to return nothing. New unit tests are added to verify these behaviors. As there are no review comments, no further feedback is provided.

Signed-off-by: Aarrya <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
# emit. A global aggregation still has exactly one group -- the whole
# block -- and must emit its identity row (``count()`` of an empty
# dataset is 0, not "no answer").
if self.num_rows() == 0 and keys:

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.

let's explicitly use keys is None or len(keys) == 0

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.

Done

@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit e40b340 into ray-project:master Sep 18, 2026
6 checks passed
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

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants