Skip to content

[Data][2/n] Eager StarExpr expansion + optimizer-rule narrowing - #63776

Merged
goutamvenkat-anyscale merged 6 commits into
ray-project:masterfrom
goutamvenkat-anyscale:goutam/schema_inference_phase_2
Jun 2, 2026
Merged

goutamvenkat-anyscale merged 6 commits into
ray-project:masterfrom
goutamvenkat-anyscale:goutam/schema_inference_phase_2

Conversation

@goutamvenkat-anyscale

Copy link
Copy Markdown
Contributor

Phase 2a: Eager StarExpr expansion in Project.post_init.

Project.__post_init__ calls a new _expand_star_exprs(exprs, input_schema) helper when the upstream input schema is a pa.Schema. The resulting Project.exprs contains explicit col() references rather than StarExpr, so downstream optimizer rules treat projection lists uniformly on every typed chain. When the input is opaque (UDF map upstream), StarExpr is preserved and the runtime eval_projection expands it per-block.

Phase 2b: Narrow star handling in optimizer rules to UDF-fallback.

After Phase 2a, every typed chain reaches the optimizer with no StarExpr in the projection list. The defensive StarExpr branches in projection_pushdown (_collect_referenced_columns, _push_projection_into_read_op, _try_fuse), predicate_pushdown (_can_push_filter_through_projection), and eval_projection are now only hit on the UDF-fallback path; document this and remove the resolved TODO at _collect_referenced_columns (issue #57720).

Tests: 6 unit tests for _expand_star_exprs and 3 integration tests verifying Project.exprs after with_column / rename_columns / map_batches -> with_column, plus a fixed exprlist_to_fields regression test for column overrides.

@goutamvenkat-anyscale
goutamvenkat-anyscale requested a review from a team as a code owner June 1, 2026 19:01

@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 implements eager expansion of StarExpr in Project.__post_init__ when the input schema is known, simplifying downstream optimizer rules by avoiding StarExpr special cases on typed chains. However, the current implementation of _expand_star_exprs appends renamed columns to the end of the list, which causes a critical schema mismatch and breaks column ordering. To resolve this, the star expansion should replace renamed columns in-place, and the corresponding test assertions should be updated accordingly.

Comment thread python/ray/data/expressions.py Resolved
Comment thread python/ray/data/expressions.py Resolved
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Jun 1, 2026
@goutamvenkat-anyscale

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 introduces eager expansion of StarExpr into explicit col() references within Project.__post_init__ when the input schema is known. This ensures that downstream optimizer rules do not encounter StarExpr on typed chains, while preserving the fallback behavior for opaque schemas (e.g., UDF maps). To support this, a new helper function _expand_star_exprs is added to ray.data.expressions and integrated into exprlist_to_fields to align plan-time and runtime semantics. Comprehensive unit tests and a new test utility assert_exprs_equal have also been added. There are no review comments, so no feedback is provided.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 97b9b6c27e1d793ecf3b26274fe5afc68b932152. Configure here.

@goutamvenkat-anyscale goutamvenkat-anyscale added the go add ONLY when ready to merge, run all tests label Jun 1, 2026
@goutamvenkat-anyscale
goutamvenkat-anyscale force-pushed the goutam/schema_inference_phase_2 branch from 6881818 to d82257b Compare June 1, 2026 22:36
# Check if filter references columns removed by explicit select.
# Valid if: projection includes all columns (star, UDF-fallback path)
# OR predicate columns exist in the explicit output set (typed path,
# since Phase 2a expands ``StarExpr`` into explicit ``col()`` refs

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 the reference to phase 2a exist in this comment?

# ``Project.__post_init__`` when the input schema is known. So this
# branch is hit only on the UDF-fallback path (Project on top of an
# opaque-schema input like ``MapBatches``), where we can't enumerate
# columns and have to fall back to "all columns" (``None``).
if any(isinstance(expr, StarExpr) for expr in exprs):

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.

wait to clarify, the star expression will exist here only when we encounter an unknown schema (like UDF?)

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.

Yup. If it encounters any black box udf will remain a starexpr

of its source column.

Mirrors the runtime expansion in
``ray.data._internal.planner.plan_expression.expression_evaluator.eval_projection``

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.

if u reference this, u can probably just have the name of the function, and make sure it's a developer API

goutamvenkat-anyscale and others added 6 commits June 2, 2026 14:11
Phase 2a: Eager StarExpr expansion in Project.__post_init__.

Mirrors DataFusion's ``Projection::try_new``: ``Project.__post_init__``
calls a new ``_expand_star_exprs(exprs, input_schema)`` helper when the
upstream input schema is a ``pa.Schema``. The resulting ``Project.exprs``
contains explicit ``col()`` references rather than ``StarExpr``, so
downstream optimizer rules treat projection lists uniformly on every
typed chain. When the input is opaque (UDF map upstream), ``StarExpr``
is preserved and the runtime ``eval_projection`` expands it per-block.

Phase 2b: Narrow star handling in optimizer rules to UDF-fallback.

After Phase 2a, every typed chain reaches the optimizer with no
``StarExpr`` in the projection list. The defensive ``StarExpr``
branches in ``projection_pushdown`` (``_collect_referenced_columns``,
``_push_projection_into_read_op``, ``_try_fuse``), ``predicate_pushdown``
(``_can_push_filter_through_projection``), and ``eval_projection`` are
now only hit on the UDF-fallback path; document this and remove the
resolved TODO at ``_collect_referenced_columns`` (issue ray-project#57720).

Also fixes a Phase 1 bug in ``exprlist_to_fields``: duplicate output
names (e.g., ``with_column('a', new_expr)`` on an input that already
has ``a``) now last-wins to match the runtime ``eval_projection``
upsert semantics.

Tests: 6 unit tests for ``_expand_star_exprs`` and 3 integration
tests verifying ``Project.exprs`` after ``with_column`` /
``rename_columns`` / ``map_batches -> with_column``, plus a fixed
``exprlist_to_fields`` regression test for column overrides.

Co-authored-by: Cursor <[email protected]>
Signed-off-by: Goutam <[email protected]>
Signed-off-by: Goutam <[email protected]>
Signed-off-by: Goutam <[email protected]>
Signed-off-by: Goutam <[email protected]>
Signed-off-by: Goutam <[email protected]>
@goutamvenkat-anyscale
goutamvenkat-anyscale force-pushed the goutam/schema_inference_phase_2 branch from d82257b to f8313be Compare June 2, 2026 21:17
@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) June 2, 2026 21:42
@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) June 2, 2026 21:45
@goutamvenkat-anyscale
goutamvenkat-anyscale merged commit 031afdb into ray-project:master Jun 2, 2026
5 checks passed
@goutamvenkat-anyscale
goutamvenkat-anyscale deleted the goutam/schema_inference_phase_2 branch June 2, 2026 21:57
rueian pushed a commit to rueian/ray that referenced this pull request Jun 4, 2026
…project#63776)

Phase 2a: Eager StarExpr expansion in Project.__post_init__.

``Project.__post_init__`` calls a new ``_expand_star_exprs(exprs,
input_schema)`` helper when the upstream input schema is a
``pa.Schema``. The resulting ``Project.exprs`` contains explicit
``col()`` references rather than ``StarExpr``, so downstream optimizer
rules treat projection lists uniformly on every typed chain. When the
input is opaque (UDF map upstream), ``StarExpr`` is preserved and the
runtime ``eval_projection`` expands it per-block.

Phase 2b: Narrow star handling in optimizer rules to UDF-fallback.

After Phase 2a, every typed chain reaches the optimizer with no
``StarExpr`` in the projection list. The defensive ``StarExpr`` branches
in ``projection_pushdown`` (``_collect_referenced_columns``,
``_push_projection_into_read_op``, ``_try_fuse``),
``predicate_pushdown`` (``_can_push_filter_through_projection``), and
``eval_projection`` are now only hit on the UDF-fallback path; document
this and remove the resolved TODO at ``_collect_referenced_columns``
(issue ray-project#57720).

Tests: 6 unit tests for ``_expand_star_exprs`` and 3 integration tests
verifying ``Project.exprs`` after ``with_column`` / ``rename_columns`` /
``map_batches -> with_column``, plus a fixed ``exprlist_to_fields``
regression test for column overrides.

---------

Signed-off-by: Goutam <[email protected]>
Co-authored-by: Cursor <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…project#63776)

Phase 2a: Eager StarExpr expansion in Project.__post_init__.

``Project.__post_init__`` calls a new ``_expand_star_exprs(exprs,
input_schema)`` helper when the upstream input schema is a
``pa.Schema``. The resulting ``Project.exprs`` contains explicit
``col()`` references rather than ``StarExpr``, so downstream optimizer
rules treat projection lists uniformly on every typed chain. When the
input is opaque (UDF map upstream), ``StarExpr`` is preserved and the
runtime ``eval_projection`` expands it per-block.

Phase 2b: Narrow star handling in optimizer rules to UDF-fallback.

After Phase 2a, every typed chain reaches the optimizer with no
``StarExpr`` in the projection list. The defensive ``StarExpr`` branches
in ``projection_pushdown`` (``_collect_referenced_columns``,
``_push_projection_into_read_op``, ``_try_fuse``),
``predicate_pushdown`` (``_can_push_filter_through_projection``), and
``eval_projection`` are now only hit on the UDF-fallback path; document
this and remove the resolved TODO at ``_collect_referenced_columns``
(issue ray-project#57720).

Tests: 6 unit tests for ``_expand_star_exprs`` and 3 integration tests
verifying ``Project.exprs`` after ``with_column`` / ``rename_columns`` /
``map_batches -> with_column``, plus a fixed ``exprlist_to_fields``
regression test for column overrides.

---------

Signed-off-by: Goutam <[email protected]>
Co-authored-by: Cursor <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…project#63776)

Phase 2a: Eager StarExpr expansion in Project.__post_init__.

``Project.__post_init__`` calls a new ``_expand_star_exprs(exprs,
input_schema)`` helper when the upstream input schema is a
``pa.Schema``. The resulting ``Project.exprs`` contains explicit
``col()`` references rather than ``StarExpr``, so downstream optimizer
rules treat projection lists uniformly on every typed chain. When the
input is opaque (UDF map upstream), ``StarExpr`` is preserved and the
runtime ``eval_projection`` expands it per-block.

Phase 2b: Narrow star handling in optimizer rules to UDF-fallback.

After Phase 2a, every typed chain reaches the optimizer with no
``StarExpr`` in the projection list. The defensive ``StarExpr`` branches
in ``projection_pushdown`` (``_collect_referenced_columns``,
``_push_projection_into_read_op``, ``_try_fuse``),
``predicate_pushdown`` (``_can_push_filter_through_projection``), and
``eval_projection`` are now only hit on the UDF-fallback path; document
this and remove the resolved TODO at ``_collect_referenced_columns``
(issue ray-project#57720).

Tests: 6 unit tests for ``_expand_star_exprs`` and 3 integration tests
verifying ``Project.exprs`` after ``with_column`` / ``rename_columns`` /
``map_batches -> with_column``, plus a fixed ``exprlist_to_fields``
regression test for column overrides.

---------

Signed-off-by: Goutam <[email protected]>
Co-authored-by: Cursor <[email protected]>
Signed-off-by: 400Ping <[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