Skip to content

[Data] Unify aggregation column name resolution across planning and execution - #68631

Open
m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/aggregate-resolved-names
Open

m4ru1 wants to merge 1 commit into
ray-project:masterfrom
m4ru1:fix/aggregate-resolved-names

Conversation

@m4ru1

@m4ru1 m4ru1 commented Nov 28, 2026 •

Copy link
Copy Markdown

Summary

Keep ds.groupby(...).aggregate(...) output column names (_2, _3, ... suffixes and in-place group-key overwrites) consistent across schema inference, vectorized hash-shuffle finalization, and shuffle elimination, closing #68629. This extends #66218's block-level aggregation name resolution to logical planning and default hash-shuffle execution.

Before

When ds.groupby(...).aggregate(...) produces duplicate output names—such as Quantile("v", q=0.25), Quantile("v", q=0.75)—or overwrites a group key on Arrow-backed inputs, ds.schema() and ds.columns() infer unsuffixed duplicates or duplicate key fields, causing with_column() to drop suffixed columns and drop_columns() to fail when targeting those names. In default hash-shuffle execution, collision chains like ["a", "a_2", "a"] also overwrite a_2 instead of producing a_3, and overwriting a group key with a suffixed column leaves stale partitioning metadata that can skip the shuffle in a subsequent groupby().

After

Apply the shared aggregation column-name resolver across logical schema inference, vectorized hash-aggregate finalization, and output-partitioning checks, replacing overwritten group-key fields in place and clearing hash-partitioning metadata whenever any resolved output name matches a group key. Also treat non-Count whole-row AggregateFnV2(on=None) aggregations as requiring all input columns so projection pushdown preserves non-key inputs once suffixed outputs resolve statically.

Non-Arrow input schemas and aggregations without a static output type (such as Unique on nested columns, Quantile on strings, or default legacy AggregateFn) remain statically unresolved and use the existing execution fallback; Count() continues allowing input pruning to non-empty group keys, and legacy hash/sort shuffle planners remain unchanged.

flowchart LR
  subgraph Before
    B1["Logical / Physical planning & Shuffle (separate naming)"] -. desyncs suffixes & key overwrites .-> B2["Dropped _2 columns, overwritten aliases & skipped re-shuffle"]
  end
  subgraph After
    A1["Logical / Physical planning & Shuffle (shared name resolver)"] -. aligns Arrow schema, finalize & partitioning .-> A2["Preserved _2.._N columns, intact aliases & valid re-shuffle"]
  end
  classDef bad fill:#ffcdd2,stroke:#c62828,color:#000
  classDef changed fill:#fff9c4,stroke:#f9a825,color:#000
  classDef good fill:#c8e6c9,stroke:#2e7d32,color:#000
  class B1,B2 bad
  class A1 changed
  class A2 good
Loading

Subsystems

Logical planning / query optimization, Physical planning, and Shuffle / aggregation.

Testing

Local validation passed on Python 3.11.15 / Ray 3.0.0.dev0 (a872863f2b, PyArrow 26.0.1, pandas 2.3.3) with the updated ray.data sources loaded on both driver and workers. Tests check static schema inference, downstream with_column / drop_columns / select_columns pruning, whole-row AggregateFnV2(on=None) column retention, ["a", "a_2", "a"] collision chains across shuffle strategies, and partition invalidation on suffixed key overwrites.

for test in test_infer_schema.py \
  test_dataset_aggregrations.py \
  test_shuffle_elimination.py
do
  python -m pytest -q "python/ray/data/tests/$test" || exit 1
done

Pre-commit hooks (including pyrefly-data) and pytest lint checks also passed.

AI assistance was used.

…xecution

Keep resolved aggregation output column names (_2, _3, ... suffixes and
in-place group-key overwrites) consistent across static schema inference,
vectorized hash-aggregate finalization, and output-partitioning checks.
Also treat non-Count whole-row AggregateFnV2(on=None) aggregations as
requiring all input columns so projection pushdown preserves non-key
inputs once suffixed outputs resolve statically.

Closes ray-project#68629

Signed-off-by: mora <[email protected]>
@m4ru1
m4ru1 requested a review from a team as a code owner November 28, 2026 20:32

@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 standardizes and fixes aggregation column name resolution across schema inference, execution, and planning by utilizing _resolve_aggregated_column_names. This ensures that schema inference matches the actual output column names during execution, particularly when handling name collisions and key overwrites. Comprehensive unit tests have been added to verify these changes. I have no feedback to provide as there are no review comments.

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant