Repository navigation
[Data] Fix empty global aggregation and duplicate aggregation column names - #66218
Conversation
…names Signed-off-by: Aarrya <[email protected]> Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
let's explicitly use keys is None or len(keys) == 0
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Aarrya <[email protected]>
…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]>
Why
Two bugs in
TableBlockAccessor's aggregation path, both visible from the public API.A global aggregation over an empty dataset returns
None.#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.
_resolve_aggregated_column_namessuffixes 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_2again. Because_aggregatebuilds rows withrow[name] = accumulator, two aggregations sharing a name means one result is silently dropped.What changes
_aggregateis gated on there being group keys, and thenum_rows() > 0check aroundaccumulate_blockis restored, so an empty group leaves the accumulators at their identity values._resolve_aggregated_column_namescounts 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.GPUAggregationPlancarried 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.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 blamepoints there.🤖 Generated with Claude Code