Skip to content

[Data] Abstracting common methods, removing duplication in ArrowBlockAccessor, PandasBlockAccessor - #50498

Merged
raulchen merged 21 commits into
masterfrom
ak/blk-aggr-clup
Feb 14, 2025
Merged

raulchen merged 21 commits into
masterfrom
ak/blk-aggr-clup

Conversation

@alexeykudinkin

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Abstracting common methods, removing duplication in ArrowBlockAccessor, PandasBlockAccessor.

Related issue number

Checks

…sor`, remove duplication

Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
@alexeykudinkin
alexeykudinkin requested a review from a team as a code owner February 12, 2025 23:12
@alexeykudinkin alexeykudinkin added the go add ONLY when ready to merge, run all tests label Feb 12, 2025
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Comment thread python/ray/data/block.py Outdated

return _get_group_boundaries_sorted_numpy(projected_block)

return np.array([0, block_accessor.num_rows()])

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.

why return this when key is empty? Should we just raise an error?

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.

It's semantically correct -- there's just 1 group. Otherwise, i'd need to handle that case separately at every caller

Comment thread python/ray/data/block.py Outdated
by a list of 1D numpy arrays for each column. In each column,
NaNs/None are considered to be the same group.
def _get_group_boundaries_sorted(
block: Block, key: Union[str, List[str]]

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.

key: Union[str, List[str]]

not a new issue, but I think we should stop handling key-or-keys everywhere. Ideally only the user-facing API needs to deals with single or multiple keys.
All internal function should only handle lists to simplify code

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.

On the same page. Let me fix that

Comment thread python/ray/data/block.py Outdated

# Get the list of boundaries including first start and last end indices
if key:
projected_block = block_accessor.to_numpy(key)

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.

to_numpy isn't zero-copy. we should probably push down the implementation to each BlockAccessor and use their native methods.

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.

to_numpy will be zero-copy if it's

  • Contiguous
  • Numerical
  • No nans/nulls

Since it's sorted it has quite good chances of being one. But even when it's not it's still gonna be way faster than what we do right now (deserializing the keys and iterating in Python)

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.

makes sense

Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
…-type if no target one was provided

Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Comment thread python/ray/data/block.py Resolved
Comment thread python/ray/data/block.py Resolved
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
Signed-off-by: Alexey Kudinkin <[email protected]>
@raulchen
raulchen merged commit 2cc71a8 into master Feb 14, 2025
@raulchen
raulchen deleted the ak/blk-aggr-clup branch February 14, 2025 00:10
xsuler pushed a commit to antgroup/ant-ray that referenced this pull request Mar 4, 2025
…kAccessor`, `PandasBlockAccessor` (ray-project#50498)

## Why are these changes needed?

Abstracting common methods, removing duplication in
`ArrowBlockAccessor`, `PandasBlockAccessor`.

---------

Signed-off-by: Alexey Kudinkin <[email protected]>
xsuler pushed a commit to antgroup/ant-ray that referenced this pull request Mar 4, 2025
…kAccessor`, `PandasBlockAccessor` (ray-project#50498)

## Why are these changes needed?

Abstracting common methods, removing duplication in
`ArrowBlockAccessor`, `PandasBlockAccessor`.

---------

Signed-off-by: Alexey Kudinkin <[email protected]>
park12sj pushed a commit to park12sj/ray that referenced this pull request Mar 18, 2025
…kAccessor`, `PandasBlockAccessor` (ray-project#50498)

## Why are these changes needed?

Abstracting common methods, removing duplication in
`ArrowBlockAccessor`, `PandasBlockAccessor`.

---------

Signed-off-by: Alexey Kudinkin <[email protected]>
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

community-backlog go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants