FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[SPARK-59042][SQL] Fix PushProjectionThroughUnion failure on correlated scalar subquery over UNION ALL by zahed1994 · Pull Request #58348 · apache/spark · GitHub

/ spark Public

[SPARK-59042][SQL] Fix PushProjectionThroughUnion failure on correlated scalar subquery over UNION ALL - #58348

Open
zahed1994 wants to merge 3 commits into
apache:masterfrom
zahed1994:SPARK-59042-push-proj-union-subquery
Open

[SPARK-59042][SQL] Fix PushProjectionThroughUnion failure on correlated scalar subquery over UNION ALL#58348
zahed1994 wants to merge 3 commits into
apache:masterfrom
zahed1994:SPARK-59042-push-proj-union-subquery

Conversation

Copy link
Copy Markdown
Contributor

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:

  • 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:

  1. 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.
  2. 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?

No.

zahed1994 force-pushed the SPARK-59042-push-proj-union-subquery branch from 4cfa526 to ed0e4e1 Compare August 28, 2026 18:06
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL