[SPARK-57005][SQL] Fix None.get in RewritePredicateSubquery when subquery predicates are eliminated#56077
Open
yadavay-amzn wants to merge 1 commit into
Open
Conversation
eejbyfeldt
reviewed
May 23, 2026
Contributor
eejbyfeldt
left a comment
There was a problem hiding this comment.
Would it be possible to add the test in RewriteSubquerySuite instead? So that it more localized and faster to run since it does not have to bring in the full planning machinery.
…uery predicates are eliminated Guard joinCond.get with isDefined check in rewriteDomainJoinsIfPresent. When constant folding eliminates all correlated predicates (e.g., WHERE FALSE AND correlated_pred), rewriteExistentialExpr returns None for joinCond. Without the guard, this crashes with NoSuchElementException. Closes #SPARK-57005
e3525a7 to
a808317
Compare
Contributor
Author
|
Done -- moved the test to |
eejbyfeldt
reviewed
May 26, 2026
Comment on lines
+101
to
+126
| // When BooleanSimplification in PullupCorrelatedPredicates eliminates all correlated | ||
| // predicates (e.g., FALSE AND correlated_pred -> FALSE), the Exists node ends up with | ||
| // outerAttrs non-empty but joinCond empty. RewritePredicateSubquery must handle this. | ||
| object OptimizeWithPullup extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("Pullup Correlated Expressions", Once, | ||
| PullupCorrelatedPredicates) :: | ||
| Batch("Rewrite Subquery", FixedPoint(1), | ||
| RewritePredicateSubquery, | ||
| ColumnPruning, | ||
| CollapseProject, | ||
| RemoveNoopOperators) :: Nil | ||
| } | ||
|
|
||
| val outer = LocalRelation($"a".int, $"b".int) | ||
| val inner = LocalRelation($"x".int, $"y".int) | ||
|
|
||
| // NOT EXISTS with FALSE AND correlated_pred - should not throw None.get | ||
| val notExistsQuery = outer.where( | ||
| Not(Exists(inner.where(Literal.FalseLiteral && $"a" === $"x")))).select($"a") | ||
| OptimizeWithPullup.execute(notExistsQuery.analyze) | ||
|
|
||
| // EXISTS with FALSE AND correlated_pred - should not throw None.get | ||
| val existsQuery = outer.where( | ||
| Exists(inner.where(Literal.FalseLiteral && $"a" === $"x"))).select($"a") | ||
| OptimizeWithPullup.execute(existsQuery.analyze) |
Contributor
There was a problem hiding this comment.
The pullup rule is not needed here to reproduce the crash? What the motivation for not just using the simpler Optimize already defined?
Suggested change
| // When BooleanSimplification in PullupCorrelatedPredicates eliminates all correlated | |
| // predicates (e.g., FALSE AND correlated_pred -> FALSE), the Exists node ends up with | |
| // outerAttrs non-empty but joinCond empty. RewritePredicateSubquery must handle this. | |
| object OptimizeWithPullup extends RuleExecutor[LogicalPlan] { | |
| val batches = | |
| Batch("Pullup Correlated Expressions", Once, | |
| PullupCorrelatedPredicates) :: | |
| Batch("Rewrite Subquery", FixedPoint(1), | |
| RewritePredicateSubquery, | |
| ColumnPruning, | |
| CollapseProject, | |
| RemoveNoopOperators) :: Nil | |
| } | |
| val outer = LocalRelation($"a".int, $"b".int) | |
| val inner = LocalRelation($"x".int, $"y".int) | |
| // NOT EXISTS with FALSE AND correlated_pred - should not throw None.get | |
| val notExistsQuery = outer.where( | |
| Not(Exists(inner.where(Literal.FalseLiteral && $"a" === $"x")))).select($"a") | |
| OptimizeWithPullup.execute(notExistsQuery.analyze) | |
| // EXISTS with FALSE AND correlated_pred - should not throw None.get | |
| val existsQuery = outer.where( | |
| Exists(inner.where(Literal.FalseLiteral && $"a" === $"x"))).select($"a") | |
| OptimizeWithPullup.execute(existsQuery.analyze) | |
| val outer = LocalRelation($"a".int, $"b".int) | |
| val inner = LocalRelation($"x".int, $"y".int) | |
| // NOT EXISTS with FALSE AND correlated_pred - should not throw None.get | |
| val notExistsQuery = outer.where( | |
| Not(Exists(inner.where(Literal.FalseLiteral && $"a" === $"x")))).select($"a") | |
| Optimize.execute(notExistsQuery.analyze) | |
| // EXISTS with FALSE AND correlated_pred - should not throw None.get | |
| val existsQuery = outer.where( | |
| Exists(inner.where(Literal.FalseLiteral && $"a" === $"x"))).select($"a") | |
| Optimize.execute(existsQuery.analyze) |
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?
Guard
joinCond.getwithisDefinedcheck inrewriteDomainJoinsIfPresent.Why are the changes needed?
When constant folding eliminates all correlated predicates in a subquery (e.g.,
WHERE FALSE AND correlated_pred),rewriteExistentialExprreturnsNoneforjoinCond. The subsequent call torewriteDomainJoinsIfPresentthen crashes withNoSuchElementException: None.get.Repro:
Does this PR introduce any user-facing change?
Yes -- queries with EXISTS/NOT EXISTS subqueries where all correlated predicates are eliminated by constant folding no longer crash.
How was this patch tested?
Added test in
SubquerySuitecovering bothNOT EXISTS(returns all rows) andEXISTS(returns no rows) withWHERE FALSE AND (correlated_pred). Test verifies correct query semantics, not just absence of crash.NoSuchElementException: None.getat subquery.scala:76Was this patch authored or co-authored using generative AI tooling?
Generated by: Claude Opus 4.7