Refactor projection scalar subquery rewrites to use slot-owned state#22771
Open
kosiew wants to merge 3 commits into
Open
Refactor projection scalar subquery rewrites to use slot-owned state#22771kosiew wants to merge 3 commits into
kosiew wants to merge 3 commits into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
ScalarSubqueryToJoinProjection Rewrite State and Coverage #22670Rationale for this change
The projection branch of
ScalarSubqueryToJointracked rewrite ownership through multiple parallel structures (all_subqueries,alias_to_index, andrewrite_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
ProjectionRewriteStatestruct containing:rewritten_exprsubquery_aliasesRefactor 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_exprsUpdate 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_slotscorrelated_scalar_subquery_multiple_subqueries_one_projection_slotcorrelated_scalar_subquery_mixed_repeated_and_non_count_projection_slotsThese tests verify that:
COUNTscalar subqueries are compensated while non-COUNTaggregates retain their existingNULLsemantics.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.