Skip to content

[SPARK-57005][SQL] Fix None.get in RewritePredicateSubquery when subquery predicates are eliminated#56077

Open
yadavay-amzn wants to merge 1 commit into
apache:masterfrom
yadavay-amzn:fix/SPARK-57005-none-get-subquery
Open

[SPARK-57005][SQL] Fix None.get in RewritePredicateSubquery when subquery predicates are eliminated#56077
yadavay-amzn wants to merge 1 commit into
apache:masterfrom
yadavay-amzn:fix/SPARK-57005-none-get-subquery

Conversation

@yadavay-amzn
Copy link
Copy Markdown
Contributor

@yadavay-amzn yadavay-amzn commented May 23, 2026

What changes were proposed in this pull request?

Guard joinCond.get with isDefined check in rewriteDomainJoinsIfPresent.

Why are the changes needed?

When constant folding eliminates all correlated predicates in a subquery (e.g., WHERE FALSE AND correlated_pred), rewriteExistentialExpr returns None for joinCond. The subsequent call to rewriteDomainJoinsIfPresent then crashes with NoSuchElementException: None.get.

Repro:

SELECT t0.value FROM VALUES (CAST(0 AS TINYINT)) AS t0(value)
WHERE NOT EXISTS (
  SELECT t1.value FROM VALUES (CAST(0 AS TINYINT)) AS t1(value)
  WHERE FALSE AND (t0.value = t1.value)
)

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 SubquerySuite covering both NOT EXISTS (returns all rows) and EXISTS (returns no rows) with WHERE FALSE AND (correlated_pred). Test verifies correct query semantics, not just absence of crash.

  • Without fix: NoSuchElementException: None.get at subquery.scala:76
  • With fix: correct results returned
  • Full SubquerySuite (142 tests) passes with no regressions.

Was this patch authored or co-authored using generative AI tooling?

Generated by: Claude Opus 4.7

Copy link
Copy Markdown
Contributor

@eejbyfeldt eejbyfeldt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@yadavay-amzn yadavay-amzn force-pushed the fix/SPARK-57005-none-get-subquery branch from e3525a7 to a808317 Compare May 23, 2026 17:48
@yadavay-amzn
Copy link
Copy Markdown
Contributor Author

Done -- moved the test to RewriteSubquerySuite as a catalyst-level test using the optimizer rules directly. Thanks @eejbyfeldt!

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.

2 participants