[SPARK-59042][SQL] Fix PushProjectionThroughUnion failure on correlated scalar subquery over UNION ALL - #58348
Open
zahed1994 wants to merge 3 commits into
Open
Conversation
…ed scalar subquery over UNION ALL
…ThroughUnion for subqueries
zahed1994
force-pushed
the
SPARK-59042-push-proj-union-subquery
branch
from
August 28, 2026 18:06
4cfa526 to
ed0e4e1
Compare
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.
What changes were proposed in this pull request?
This PR fixes a
java.util.NoSuchElementExceptionwhenPushProjectionThroughUnionoptimizes aProjectover aUnionwhere project expressions contain subqueries or outer references.Specifically:
PushProjectionThroughUnion.pushToRightto userewrites.getOrElse(a, a)instead ofrewrites(a)when looking up attribute references. This preventsNoSuchElementExceptionwhen encountering subquery-internal or unmapped attributes.updateOuterReferencesInSubqueryhelper inPushProjectionThroughUnionto recurse into subquery plans (PlanExpression.plan) and rewrite innerOuterReference(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,PushProjectionThroughUnionattempts to push project expressions to the children of theUnion.Prior to this fix:
pushToRightcalledrewrites(a)directly on all attributes. Subquery-internal attributes (e.g.r.x#7inWHERE r.x = u.a) were absent fromrewrites(which mapsleft.output -> right.output), throwingjava.util.NoSuchElementException: key not found: x#7.OuterReference(a)) were skipped bye transformbecauseOuterReferenceextendsLeafExpressionandPlanExpression.planis aLogicalPlanrather than anExpressionchild.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?
PushProjectThroughUnionSuite.SubquerySuite.Was this patch authored or co-authored using generative AI tooling?
No.