[SPARK-57109][SQL] Fix exchange reuse failure when a VIEW is referenced in both UNION ALL branches via a CTE#57356
Open
kush2439p wants to merge 1 commit into
Open
Conversation
Author
|
I already went through your issue and this is the pr i have made please review this. |
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?
Fix a performance regression where ReusedExchangeExec fails to match the shuffle of the underlying table when a persisted VIEW is referenced in both branches of a UNION ALL through a CTE, causing the table to be scanned and shuffled twice.
The fix is confined entirely to the canonicalization layer (QueryPlan.doCanonicalize()). Inside mapExpressions, a new case is added before the generic case a: Alias branch:
ame == attr.name AND �.metadata == attr.metadata. This is precisely the condition RemoveRedundantAliases itself uses to decide an alias is truly redundant.
No changes are made to RemoveRedundantAliases, DeduplicateRelations, or InlineCTE.
Why are the changes needed?
Root cause chain (5 steps):
VIEW construction (SPARK-34269) wraps each output column in a schema-compat Alias(UpCast(col, field.dataType), field.name)(explicitMetadata = Some(field.metadata)) — a genuine rename since #scan != #catalog.
InlineCTE: both UNION branches share identical ExprIds, making Union.duplicateResolved = false.
DeduplicateRelations resolves duplicates by wrapping the non-first branch in an outer Project(Alias(attr, attr.name)) and rewriting ExprIds via
ewInstance()/
ewriteAttrs, turning the view's inner schema-compat alias into a no-op Alias(a#X AS a#X).
RemoveRedundantAliases (SPARK-39887 Union first-child protection): protects branch 1's attribute set from alias removal, strips the no-op alias from branch 2. Result: branch 1 keeps the Alias node, branch 2 has a bare AttributeReference — structurally asymmetric though semantically identical. This protection is required for correctness and is not touched.
Canonicalization asymmetry: doCanonicalize() assigns a sequential id to the top-level Alias (branch 1) but uses ordinal-position normalization for the bare AttributeReference (branch 2). The two branches get different canonical forms, so ReuseExchangeAndSubquery (and AQE's stage cache) cannot match the upstream Exchange, and 1 is shuffled twice.
Does this PR introduce any user-facing change?
Yes — this is a performance fix. Queries matching the pattern (a persisted VIEW referenced in both branches of a UNION ALL via a CTE) will now correctly reuse the shuffle/exchange across both branches, avoiding redundant table scans and shuffles.
How was this patch tested?
Two tests were added:
Unit test in sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/QueryPlanSuite.scala:
End-to-end regression test in sql/core/src/test/scala/org/apache/spark/sql/execution/ReuseExchangeAndSubquerySuite.scala:
Closes #57109
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Google Antigravity (Gemini 2.5 Pro)