Repository navigation
[SPARK-62240][SQL] Fix query failures by filter pushdown ahead of a non-deterministic or throwable scalar subquery - #60784
Open
dongjoon-hyun wants to merge 2 commits into
Open
dongjoon-hyun wants to merge 2 commits into
dongjoon-hyun wants to merge 2 commits into
Conversation
…on-deterministic or throwable scalar subquery
spark-test
reviewed
Nov 22, 2026
spark-test
left a comment
Collaborator
There was a problem hiding this comment.
The AI review requested by @dongjoon-hyun did not finish. A committer can request a review from spark-test again to retry.
spark-test
reviewed
Nov 23, 2026
spark-test
left a comment
Collaborator
There was a problem hiding this comment.
The AI review requested by @dongjoon-hyun did not finish. A committer can request a review from spark-test again to retry.
This branch has not been deployed
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?
This PR fixes query failures where a filter condition that can fail, such as a division in ANSI mode, is evaluated ahead of a scalar subquery written before it whose plan has a throwable expression, such as a
CASE WHENwithraise_errorin the subquery'sWHEREclause, or a non-deterministic one.#60646 (SPARK-61924) keeps a condition that can fail in place when a non-deterministic or throwable conjunct is written before it (
PredicateHelper.splitMovable). A scalar subquery missed that in three ways:Expression.throwablereads only the children. UnlikePlanExpression.deterministic, which readsplan.deterministic, it did not read the plan. So a filter condition written after the subquery was pushed below a join, for example.RewriteCorrelatedScalarSubqueryrewrites a correlated scalar subquery into a left outer (or left single) join, and the conjunct then reads the result column of that join. Nothing marked that conjunct as a barrier any more, soPushPredicateThroughJoinpushed a condition over the outer query's columns below the join. That also happens afterEliminateOuterJoinmakes it an inner join, and for a non-deterministic subquery too.MergeSubplansmerges uncorrelated scalar subqueries into one plan read through aCTERelationRef, whose definition the subquery's plan no longer shows. The laterPushDownPredicatesbatch then moved the condition again.This PR makes three changes:
ScalarSubquery.throwablealso reads the expressions of its plan, asdeterministicdoes.ReorderBarriertagging expression, likeKnownNotNull, passes its child through and is throwable.RewriteCorrelatedScalarSubquerywraps the result of a non-deterministic or throwable correlated scalar subquery in it. So the conjunct that reads the result stays a barrier after the rewrite, andsplitMovablekeeps the conditions written after it that can fail in the filter above the join.MergeSubplansdoes not merge a throwable scalar subquery, as it already does not merge a non-deterministic one.10 / (t1.v - 9) < 100MergeSubplanswould mergeReorderBarrieris throwable also for a non-deterministic subquery: that keeps those conditions in place just the same, while a non-deterministic expression would also stop other rules, such as pushing a filter through a projection or eliminating an outer join. It is not null-intolerant:FilterExecturns anIsNotNullover a null-intolerant expression into a null check of its columns, which it may evaluate after the other conjuncts.EXISTSandINsubqueries are not changed here:RewritePredicateSubqueryitself places the other conjuncts below the left semi join that it rewrites them to. A condition that can fail inside the subquery, written after the throwable expression, is not covered here either, as in theoncondition of the non-sort-merge as-of join:DecorrelateInnerQuerycan pull it up into the join condition, ahead of that expression. Likedeterministic,ScalarSubquery.throwabledoes not look into a leaf that hides its plan, such as the cached plan of anInMemoryRelation.Why are the changes needed?
A query that should succeed fails. With ANSI mode, the default since Apache Spark 4.0.0, a division is enough.
In Issues 1, 2 and 4,
t1.f > t2.gis false on the only pair, since5 > 9is false, so the subquery is NULL and the only row oftc1is dropped. In Issue 3, theCASE WHENis false on the only row oftc2, since9 > 9is false, so the subquery is NULL and no row is kept. Either way, the division by zero should never run. Before this PR, in Issues 1, 2 and 4,10 / (v - 9) < 100is pushed into the filter ontc1, below the join that the subquery is rewritten to. In Issue 3,10 / (g - 9) < 100is pushed into the filter ontc2, below the join. Issue 3 with a second subquery(SELECT min(g) FROM tc2 WHERE ...) IS NOT NULLthat has the sameWHEREclause fails the same way, also with the first two changes alone, asMergeSubplansmerges the two subqueries.Does this PR introduce any user-facing change?
Yes. The queries above succeed. I confirmed that all four issues, and Issue 3 with the second subquery, fail with
DIVIDE_BY_ZEROin ANSI mode, with whole-stage codegen on and off, on the released v3.5.9 (2026-07-12), v4.0.5 (2026-10-21), v4.1.4 (2026-10-21), v4.2.1 (2026-10-21) and v4.3.0 (2026-10-27).Plans change only for a query with a scalar subquery whose plan has a throwable expression, or a correlated one whose plan has a non-deterministic expression. A filter condition written after the subquery that can fail is no longer pushed below a join or moved into a join condition, and neither is the subquery's own conjunct. The result of such a correlated subquery shows as
reorderbarrier(...)in the plan. SinceReorderBarrieris not null-intolerant,IS NOT NULLis not inferred through it, so a conjunct such as(subquery) = t1.vno longer turns the left outer join into an inner join with that equality as a join key. It stays in a filter above the left outer join.MergeSubplansno longer merges a throwable uncorrelated scalar subquery. Merging it with its reference wrapped inReorderBarrierwould keep the merge, but code after the rule expects the merged reference unwrapped, such as the type check ofBloomFilterMightContain. Besidesraise_error, throwable expressions include, for example,json_objectwith a nullable key andsequencewith a step. As before, a division written before the subquery, or a subquery without such an expression, may be evaluated first, as #60646 allows.How was this patch tested?
Pass the CIs with the newly added test cases. They fail without this PR.
FilterPushdownSuite: over an inner join, a division written after an uncorrelated scalar subquery whose plan hasraise_errorstays above the join, with the subquery. Withoutraise_error, both are pushed below it.SubquerySuite: Issues 1, 2 and 4, written overrangewith a correlated scalar subquery in theWHEREclause and in the select list of a derived table, return the rows that the subquery keeps instead of failing withDIVIDE_BY_ZERO. So does a correlated scalar subquery without an aggregate, which is rewritten to a left single join whose condition takes the throwableCASE WHEN, and a join under two throwable uncorrelated scalar subqueries thatMergeSubplanswould merge.MergeSubplansSuite: two scalar subqueries whose plans haveraise_errorare not merged.I also ran the catalyst optimizer suites,
SubquerySuite,DataFrameSuite,JoinSuite, the plan merging suites, the subquery tests ofSQLQueryTestSuiteand thePlanStabilitySuites locally. No golden files change. With a temporary suite (not included), the following also fail before this PR and succeed after it: the non-sort-merge as-of join with a throwableoncondition and the division in theWHEREclause, aCOUNTsubquery, a subquery underOR, a correlated subquery with the division over the other side of a join, and a subquery withrand(). AHAVINGclause and aGROUP BYover the subquery give the same results before and after it, and two non-throwable uncorrelated scalar subqueries are still merged.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5