dongjoon-hyun opened a new pull request, #60572:
URL: https://github.com/apache/spark/pull/60572
### What changes were proposed in this pull request?
This PR normalizes the `exists` attribute of `ExistenceJoin` in plan
canonicalization, in the logical `Join` and in the physical joins.
`ExistenceJoin(exists: Attribute)` is a join type, not an `Expression`.
`QueryPlan.doCanonicalize` normalizes only the expressions of a node, so a
canonicalized join keeps the exprId of `exists`. `RewritePredicateSubquery`
creates a fresh `exists` attribute for every existence join it builds, so two
identical subplans with an existence join never get the same canonicalized form.
- Add `JoinType.canonicalized`. It returns the join type itself, except that
`ExistenceJoin` normalizes `exists` to `ExprId(0)`. That is the id
`QueryPlan.doCanonicalize` gives to the first attribute a node produces, and
`NormalizePlan` already does the same when tests compare plans. The parent's
references to `exists` are already normalized by their ordinal in the join
output.
- Use it in the logical `Join.doCanonicalize`, which already drops the join
hint.
- Add `BaseJoinExec.doCanonicalize`, which does the same for the physical
joins that can plan an existence join: `BroadcastHashJoinExec`,
`ShuffledHashJoinExec`, `SortMergeJoinExec` and `BroadcastNestedLoopJoinExec`.
### Why are the changes needed?
Canonicalized plans decide what `ReuseExchangeAndSubquery`, the AQE stage
cache, `ReuseAdaptiveSubquery` and `MergeSubplans` deduplicate. An existence
join comes from an `EXISTS` or `IN` subquery under `OR`, so none of them
deduplicates a subplan with one. For example, `InlineCTE` inlines a CTE once
per reference, and the shared subplan runs twice.
```scala
spark.conf.set("spark.sql.adaptive.enabled", "false")
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")
spark.range(100).selectExpr("id % 10 AS k", "id AS
x").createOrReplaceTempView("t")
spark.range(5).selectExpr("id AS k").createOrReplaceTempView("s")
sql("""
WITH cte AS (
SELECT k, x FROM t WHERE x > 50 OR EXISTS (SELECT 1 FROM s WHERE s.k =
t.k)
)
SELECT * FROM cte c1 JOIN cte c2 ON c1.x = c2.x
""").explain()
```
**BEFORE** (Apache Spark 4.3.0): only the exchanges below the second
existence join are reused.
```
...
+- *(12) Sort [x#8L ASC NULLS FIRST], false, 0
+- Exchange hashpartitioning(x#8L, 200), ENSURE_REQUIREMENTS,
[plan_id=177]
+- *(11) Project [(id#15L % 10) AS k#7L, id#15L AS x#8L]
+- *(11) Filter ((id#15L > 50) OR exists#22)
+- *(11) SortMergeJoin [(id#15L % 10)], [k#19L],
ExistenceJoin(exists#22)
:- *(8) Sort [(id#15L % 10) ASC NULLS FIRST], false, 0
: +- ReusedExchange [id#15L], Exchange
hashpartitioning((id#0L % 10), 200), ENSURE_REQUIREMENTS, [plan_id=81]
+- *(10) Sort [k#19L ASC NULLS FIRST], false, 0
+- ReusedExchange [k#19L], Exchange hashpartitioning(k#4L,
200), ENSURE_REQUIREMENTS, [plan_id=87]
```
**AFTER**: the exchange above it is reused.
```
...
+- *(12) Sort [x#876L ASC NULLS FIRST], false, 0
+- ReusedExchange [k#875L, x#876L], Exchange hashpartitioning(x#870L,
200), ENSURE_REQUIREMENTS, [plan_id=5520]
```
AQE loses the reuse in the same way. Likewise, `MergeSubplans` does not
merge `SELECT (SELECT max(x) FROM t WHERE <cond>), (SELECT sum(x) FROM t WHERE
<cond>)` when `<cond>` is `x > 50 OR EXISTS (SELECT 1 FROM s WHERE s.k = t.k)`,
while it merges them without the `EXISTS`.
`ExistenceJoin` has kept its `exists` attribute through canonicalization
since it was added (SPARK-14781), which first shipped in
[v2.1.0](https://github.com/apache/spark/releases/tag/v2.1.0) (2016-12-15).
### Does this PR introduce _any_ user-facing change?
No. Query results do not change. Queries that repeat a subplan with an
`EXISTS` or `IN` subquery under `OR` reuse more exchanges and subqueries and
merge more scalar subqueries, as they already do without such a predicate.
### How was this patch tested?
Pass the CIs with the newly added test cases. All of them fail without this
PR.
- `SameResultSuite` in catalyst: two existence joins that differ only in the
`exists` attribute have the same result, and two with different join conditions
do not.
- `ReuseExchangeAndSubquerySuite`: the exchange above the existence join of
an inlined CTE self-join is reused, with AQE on and off.
### 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]