[SPARK-59042][SQL] Fix PushProjectionThroughUnion failure on correlated scalar subquery over UNION ALL by zahed1994 · Pull Request #58348 · apache/spark · GitHub
This PR fixes a java.util.NoSuchElementException when PushProjectionThroughUnion optimizes a Project over a Union where project expressions contain subqueries or outer references.
Specifically:
Updated PushProjectionThroughUnion.pushToRight to use rewrites.getOrElse(a, a) instead of rewrites(a) when looking up attribute references. This prevents NoSuchElementException when encountering subquery-internal or unmapped attributes.
Added updateOuterReferencesInSubquery helper in PushProjectionThroughUnion to recurse into subquery plans (PlanExpression.plan) and rewrite inner OuterReference(a) references to match the right child's corresponding attributes.
Why are the changes needed?
When a query contains a correlated scalar subquery (or any expression referencing subquery-internal attributes) over a UNION ALL, PushProjectionThroughUnion attempts to push project expressions to the children of the Union.
Prior to this fix:
pushToRight called rewrites(a) directly on all attributes. Subquery-internal attributes (e.g. r.x#7 in WHERE r.x = u.a) were absent from rewrites (which maps left.output -> right.output), throwing java.util.NoSuchElementException: key not found: x#7.
Outer references inside subquery plans (OuterReference(a)) were skipped by e transform because OuterReference extends LeafExpression and PlanExpression.plan is a LogicalPlan rather than an Expression child.
Does this PR introduce any user-facing change?
No. Fixes an optimization failure exception for correlated subqueries over UNION ALL.
How was this patch tested?
Added catalyst optimizer unit test in PushProjectThroughUnionSuite.
Added end-to-end SQL query test in SubquerySuite.
Was this patch authored or co-authored using generative AI tooling?
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
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.
What changes were proposed in this pull request?
This PR fixes a java.util.NoSuchElementException when PushProjectionThroughUnion optimizes a Project over a Union where project expressions contain subqueries or outer references.
Specifically:
Why are the changes needed?
When a query contains a correlated scalar subquery (or any expression referencing subquery-internal attributes) over a UNION ALL, PushProjectionThroughUnion attempts to push project expressions to the children of the Union.
Prior to this fix:
Does this PR introduce any user-facing change?
No. Fixes an optimization failure exception for correlated subqueries over UNION ALL.
How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?
No.