Skip to content

Refactor projection scalar subquery rewrites to use slot-owned state#22771

Open
kosiew wants to merge 3 commits into
apache:mainfrom
kosiew:scalar-subquery-02-22670
Open

Refactor projection scalar subquery rewrites to use slot-owned state#22771
kosiew wants to merge 3 commits into
apache:mainfrom
kosiew:scalar-subquery-02-22670

Conversation

@kosiew

@kosiew kosiew commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The projection branch of ScalarSubqueryToJoin tracked rewrite ownership through multiple parallel structures (all_subqueries, alias_to_index, and rewrite_exprs). While functionally correct, this made ownership indirect and increased the risk of applying compensation expressions to the wrong projection slot during future changes.

This refactor makes rewrite ownership explicit by associating rewritten expressions and their generated subquery aliases with a dedicated per-slot state structure, while preserving existing behavior, join order, alias generation, compensation semantics, and projection output names.

What changes are included in this PR?

  • Introduce a private ProjectionRewriteState struct containing:

    • rewritten_expr
    • subquery_aliases
  • Refactor projection expression extraction to build per-slot rewrite state instead of maintaining separate rewrite-expression and ownership containers.

  • Derive alias ownership from the slot state and map aliases back to the owning projection slot during join conversion.

  • Rewrite compensation handling for projection expressions to update only the owning slot's expression.

  • Extract compensation-expression replacement logic into a reusable helper:

    • apply_compensation_exprs
  • Update the filter branch to use the new helper for compensation expression application.

  • Preserve existing projection name restoration logic when rebuilding the final projection.

Are these changes tested?

Yes.

Added SQLLogicTests covering projection rewrite ownership and compensation behavior:

  • correlated_scalar_subquery_multiple_projection_slots
  • correlated_scalar_subquery_multiple_subqueries_one_projection_slot
  • correlated_scalar_subquery_mixed_repeated_and_non_count_projection_slots

These tests verify that:

  • Distinct projection slots receive compensation independently.
  • Multiple scalar subqueries within a single projection expression are all compensated correctly.
  • Repeated COUNT scalar subqueries are compensated while non-COUNT aggregates retain their existing NULL semantics.

Are there any user-facing changes?

No.

This is a behavior-preserving internal refactor of the optimizer rule. The included tests validate that existing SQL semantics are maintained.

LLM-generated code disclosure

This PR includes LLM-generated code and comments. All LLM-generated content has been manually reviewed and tested.

kosiew added 3 commits June 5, 2026 14:33
…iteState

- Added ProjectionRewriteState for improved management
- Updated projection branch to utilize slot-owned rewrite state
- Improved alias ownership derivation based on states
- Preserved the join order, compensation rewrite, and output aliases
- Added private `apply_compensation_exprs` helper.
- Reused helper in filter and projection branches.
- Built alias ownership during extraction.
- Renamed `all_subqueries` to `subqueries_to_join`.
- Fixed stale projection comment.
- Converted final projection expression build to use `map(...).collect()`.
…mization

- Built ProjectionRewriteState as a local variable
- Enhanced reading of aliases from state
- Implemented pushing state after map extension
- Avoided immediate indexing into rewrite_states to improve performance
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jun 5, 2026
@kosiew kosiew marked this pull request as ready for review June 5, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant