dongjoon-hyun opened a new pull request, #60784:
URL: https://github.com/apache/spark/pull/60784

   ### 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 WHEN` with 
`raise_error` in the subquery's `WHERE` clause, 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:
   
   - It was throwable only when its outer references or its pulled-up join 
conditions were, since `Expression.throwable` reads only the children. Unlike 
`PlanExpression.deterministic`, which reads `plan.deterministic`, it did not 
read the plan. So a filter condition written after the subquery was pushed 
below a join, for example.
   - `RewriteCorrelatedScalarSubquery` rewrites 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, so 
`PushPredicateThroughJoin` pushed a condition over the outer query's columns 
below the join. That also happens after `EliminateOuterJoin` makes it an inner 
join, and for a non-deterministic subquery too.
   - `MergeSubplans` merges uncorrelated scalar subqueries into one plan read 
through a `CTERelationRef`, whose definition the subquery's plan no longer 
shows. The later `PushDownPredicates` batch then moved the condition again.
   
   This PR makes three changes:
   
   - `ScalarSubquery.throwable` also reads the expressions of its plan, as 
`deterministic` does.
   - A new `ReorderBarrier` tagging expression, like `KnownNotNull`, passes its 
child through and is throwable. `RewriteCorrelatedScalarSubquery` wraps 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, and 
`splitMovable` keeps the conditions written after it that can fail in the 
filter above the join.
   - `MergeSubplans` does not merge a throwable scalar subquery, as it already 
does not merge a non-deterministic one.
   
   | Filter conditions | Before | After |
   |---|---|---|
   | A correlated scalar subquery with a throwable or non-deterministic 
expression, then a condition that can fail over the outer query's columns, e.g. 
`10 / (t1.v - 9) < 100` | The condition is pushed below the join that the 
subquery is rewritten to | Kept above the join |
   | The same, with the subquery in the select list of a derived table and the 
conditions in the outer query (the shape that the non-sort-merge as-of join is 
rewritten to) | The condition is pushed below the join | Kept above the join |
   | An uncorrelated scalar subquery with a throwable expression, then a 
condition that can fail, over a join, also with two such subqueries that 
`MergeSubplans` would merge | Both are pushed below the join | Kept above the 
join |
   | A scalar subquery without a non-deterministic or throwable expression | 
Pushed | Same |
   
   `ReorderBarrier` is 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: 
`FilterExec` turns an `IsNotNull` over a null-intolerant expression into a null 
check of its columns, which it may evaluate after the other conjuncts.
   
   `EXISTS` and `IN` subqueries are not changed here: 
`RewritePredicateSubquery` itself places the other conjuncts below the left 
semi join that it rewrites them to. A condition that can fail inside the 
subquery, after the throwable expression, is #60764 (SPARK-62142).
   
   ### 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.
   
   ```sql
   CREATE TABLE tc1(k INT, v INT, f INT) USING parquet;
   CREATE TABLE tc2(k INT, g INT) USING parquet;
   INSERT INTO tc1 VALUES (1, 9, 5);
   INSERT INTO tc2 VALUES (1, 9);
   
   -- Issue 1: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT t1.k FROM tc1 t1
   WHERE (SELECT max(g) FROM tc2 t2
          WHERE t2.k = t1.k AND CASE WHEN t1.f < 0 THEN raise_error('neg') ELSE 
t1.f > t2.g END)
         IS NOT NULL
     AND 10 / (t1.v - 9) < 100;
   
   -- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT k FROM (
     SELECT t1.*, (SELECT max(g) FROM tc2 t2
                   WHERE t2.k = t1.k AND CASE WHEN t1.f < 0 THEN 
raise_error('neg') ELSE t1.f > t2.g END) AS m
     FROM tc1 t1)
   WHERE m IS NOT NULL AND 10 / (v - 9) < 100;
   
   -- Issue 3: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT t1.k FROM tc1 t1 JOIN tc2 t2 ON t1.k = t2.k
   WHERE (SELECT max(g) FROM tc2 WHERE CASE WHEN g < 0 THEN raise_error('neg') 
ELSE g > 9 END) IS NOT NULL
     AND 10 / (t2.g - 9) < 100;
   
   -- Issue 4: It should return no row, but fails with DIVIDE_BY_ZERO.
   SELECT t1.k FROM tc1 t1
   WHERE (SELECT max(g) FROM tc2 t2 WHERE t2.k = t1.k AND length(uuid()) = 36 
AND t1.f > t2.g)
         IS NOT NULL
     AND 10 / (t1.v - 9) < 100;
   ```
   
   In Issues 1, 2 and 4, `t1.f > t2.g` is false on the only pair, since `5 > 9` 
is false, so the subquery is NULL and the only row of `tc1` is dropped. In 
Issue 3, the `CASE WHEN` is false on the only row of `tc2`, since `9 > 9` is 
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) < 
100` is pushed into the filter on `tc1`, below the join that the subquery is 
rewritten to. In Issue 3, `10 / (g - 9) < 100` is pushed into the filter on 
`tc2`, below the join. Issue 3 with a second subquery `(SELECT min(g) FROM tc2 
WHERE ...) IS NOT NULL` that has the same `WHERE` clause fails the same way, 
also with the first two changes alone, as `MergeSubplans` merges 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_ZERO` in ANSI mode, with 
whole-stage codegen on and off, on the released 
[v3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9) (2026-07-12), 
[v4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) (2026-10-21), 
[v4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21), 
[v4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) and 
[v4.3.0](https://github.com/apache/spark/releases/tag/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. Since `ReorderBarrier` is not 
null-intolerant, `IS NOT NULL` is not inferred through it, so a conjunct such 
as `(subquery) = t1.v` no 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. `MergeSubplans` no longer merges a throwable uncorrelated scalar 
subquery. Besides `raise_error`, throwable expressions include, for example, 
`json_object` with a nullable key and `sequence` with 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 has `raise_error` stays above the join, 
with the subquery. Without `raise_error`, both are pushed below it.
   - `SubquerySuite`: Issues 1, 2 and 4, written over `range` with a correlated 
scalar subquery in the `WHERE` clause and in the select list of a derived 
table, return the rows that the subquery keeps instead of failing with 
`DIVIDE_BY_ZERO`. So does a join under two throwable uncorrelated scalar 
subqueries that `MergeSubplans` would merge.
   
   I also ran the catalyst optimizer suites, `SubquerySuite`, `DataFrameSuite`, 
`JoinSuite`, the plan merging suites, the subquery tests of `SQLQueryTestSuite` 
and the `PlanStabilitySuite`s 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 throwable `on` condition and the 
division in the `WHERE` clause, a `COUNT` subquery, a subquery under `OR`, a 
subquery without an aggregate (a left single join), a correlated subquery with 
the division over the other side of a join, and a subquery with `rand()`. A 
`HAVING` clause and a `GROUP BY` over 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
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to