dongjoon-hyun opened a new pull request, #60764:
URL: https://github.com/apache/spark/pull/60764
### What changes were proposed in this pull request?
This PR aims to fix query failures by `DecorrelateInnerQuery`, which
evaluates the `WHERE` conditions of a correlated subquery out of their written
order around a throwable or non-deterministic condition, such as a guard with
`raise_error`.
When the conditions of a filter in a correlated subquery have a
non-deterministic or throwable conjunct, the conjuncts written up to the last
one that may not be evaluated ahead of the others, as
`PredicateHelper.splitMovable` decides (SPARK-61924), now stay first in the
filter in their written order, with their outer references replaced by domain
attributes, as a correlated non-equality predicate under an `Aggregate` already
is. `splitMovable` takes an outer reference as a column here, which can not
fail. The conjuncts written after them can not fail, so they are still used as
join conditions with the outer query. Without such a conjunct, nothing changes.
```scala
val (keptInPlace, rest) = {
val stayUp = splitMovable(conditions.map(stripOuterReference),
Seq(child))._2.toSet
conditions.splitAt(
conditions.lastIndexWhere(cond =>
stayUp.contains(stripOuterReference(cond))) + 1)
}
```
### Why are the changes needed?
`DecorrelateInnerQuery` moves the correlated conjuncts of a filter into the
join condition with the outer query: under an `Aggregate`, the equalities and
the conjuncts with only outer columns, otherwise all of them. The join
condition is evaluated after the filter, or ahead of it once pushed into the
outer query, and the conjuncts left in the filter are reordered. So, like
SPARK-61924 and SPARK-62091 for joins, it breaks the written order around a
throwable condition in two ways.
1. A conjunct that can fail, written after the throwable condition, is
evaluated ahead of it, e.g. after it is pushed into the outer query.
2. The throwable condition is evaluated on the rows that a correlated
equality written before it would remove, because the equality is evaluated in
the join afterwards.
```sql
SET spark.sql.ansi.enabled=true;
CREATE TABLE t1 (k INT, v INT) USING parquet;
INSERT INTO t1 VALUES (1, 0);
CREATE TABLE t2 (k INT, g INT) USING parquet;
INSERT INTO t2 VALUES (1, 0), (2, 1);
-- Issue 1: It should return no row, since the guard removes the only
matching row of t2
-- before `1 / t1.v`, but it fails with DIVIDE_BY_ZERO.
SELECT * FROM t1 WHERE (SELECT max(g) FROM t2 WHERE t2.k = t1.k AND
CASE WHEN g < 0 THEN raise_error('negative g') ELSE g > 0 END AND 1 / t1.v
> 0) IS NOT NULL;
SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.k = t1.k AND
CASE WHEN g < 0 THEN raise_error('negative g') ELSE g > 0 END AND 1 / t1.v
> 0);
-- Issue 2: It should return the row (1, 0), since the guard is evaluated
only on the row (1, 0)
-- of t2, but it fails with USER_RAISED_EXCEPTION.
INSERT INTO t2 VALUES (3, -1);
SELECT * FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.k = t1.k AND
CASE WHEN g < 0 THEN raise_error('negative g') ELSE true END);
```
The first query has the shape of a non-sort-merge as-of join with a
throwable `on` condition, e.g. PySpark `_joinAsOf(..., on=...)`, whose rewrite
builds such a scalar subquery.
The conditions in different operators of a subquery, e.g. a throwable
condition in a filter above another filter with a correlated equality, and a
throwable correlated subquery in the `WHERE` clause of the outer query, are not
covered by this PR.
### Does this PR introduce _any_ user-facing change?
Yes. The queries above return their correct results instead of failing. The
issue exists since `DecorrelateInnerQuery` was added in Apache Spark 3.2.0 by
SPARK-34974, and I verified it with [v3.5.9
(2026-07-12)](https://github.com/apache/spark/releases/tag/v3.5.9), [v4.0.5
(2026-10-21)](https://github.com/apache/spark/releases/tag/v4.0.5), [v4.1.4
(2026-10-21)](https://github.com/apache/spark/releases/tag/v4.1.4), [v4.2.1
(2026-10-21)](https://github.com/apache/spark/releases/tag/v4.2.1) and [v4.3.0
(2026-10-27)](https://github.com/apache/spark/releases/tag/v4.3.0). Such a
subquery now joins with a domain of the outer columns that its kept conditions
read, instead of using them as join conditions.
### How was this patch tested?
Pass the CIs with the newly added test cases in `DecorrelateInnerQuerySuite`
and `SubquerySuite`.
### 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]