[SPARK-58365][SQL] Build the NOT-IN-in-disjunction join condition from the deduplicated subquery output - #57558
Open
LuciferYang wants to merge 1 commit into
Open
Conversation
…m the deduplicated subquery output `RewritePredicateSubquery.rewriteExistentialExprWithAttrs`'s `Not(InSubquery(...))` branch calls `dedupSubqueryOnSelfJoin` but then zips the IN equality conditions against the pre-dedup `sub.output` instead of `newSub.output`. When dedup fires the condition references stale exprIds absent from the join's right child. Use `newSub.output`, matching the three sibling branches.
uros-b
approved these changes
Jul 27, 2026
Member
|
Thank you @LuciferYang! |
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?
In
RewritePredicateSubquery.rewriteExistentialExprWithAttrs, theNot(InSubquery(...))branch (the one handling a NOT IN nested inside a disjunction, e.g.v > 0 OR x NOT IN (...)) callsdedupSubqueryOnSelfJointo alias the subquery's attributes when they conflict with the outer plan, but then builds the IN equality conditions from the pre-dedupsub.outputinstead of the deduplicatednewSub.output. The join's right child usesnewSub, so the condition can reference attributes that are no longer on the right side. This changessub.outputtonewSub.output, matching the three sibling branches that already do this (the plainInSubquerybranch in the same method, and both the top-level IN and NOT IN branches inapply).Why are the changes needed?
When
dedupSubqueryOnSelfJoinfires, it rebinds the conflicting subquery attributes to fresh exprIds. Building the condition fromsub.outputthen uses the stale ids, which only exist on the outer side, so the null-aware anti-join condition collapses to trivially-true self-equalities likeid#2 = id#2and no longer references the join's right child. That is exactly the SPARK-26078 defect thededupSubqueryOnSelfJoincall is there to prevent, so today that call is dead weight on this branch. Analysis-timeDeduplicateRelationscurrently renews subquery exprIds before the optimizer runs, so this is not reachable from user SQL on currentmasterand produces no wrong results today. It is a latent correctness hole: any future change that lets an outer/subquery exprId conflict reach this rule would silently return wrong NOT IN results, and the branch is the odd one out among four otherwise-consistent sites.Does this PR introduce any user-facing change?
No. When dedup does not fire,
newSubis the same object assub, so the change is a no-op on every plan reachable from user SQL today.How was this patch tested?
Added a
RewriteSubquerySuitecase that builds the colliding-attribute plan directly (bypassing the analyzer'sDeduplicateRelations, which would otherwise renew the ids) and asserts the rewritten join condition references the deduplicated right-side output. It fails on the unfixed tree (the condition is(a#0 = a#0) OR isnull((a#0 = a#0)), referencing nothing on the right) and passes with the fix.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)