spark-test commented on code in PR #60572:
URL: https://github.com/apache/spark/pull/60572#discussion_r4594559502


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala:
##########
@@ -848,9 +848,10 @@ case class Join(
     patterns
   }
 
-  // Ignore hint for canonicalization
+  // Ignore hint for canonicalization, and normalize the join type, see 
`JoinType.canonicalized`.
   protected override def doCanonicalize(): LogicalPlan =
-    super.doCanonicalize().asInstanceOf[Join].copy(hint = JoinHint.NONE)
+    super.doCanonicalize().asInstanceOf[Join].copy(
+      joinType = joinType.canonicalized, hint = JoinHint.NONE)

Review Comment:
   **Non-blocking (P2):** This normalization reaches MergeSubplans only through 
`PlanMerger.checkIdenticalPlans`, which reuses a subplan when its whole 
canonical form matches. When two subplans differ, the Join case of 
`PlanMerger.tryMergePlans` decides instead (PlanMerger.scala:672):
   
   ```scala
   case (np: Join, cp: Join) if np.joinType == cp.joinType && np.hint == 
cp.hint =>
   ```
   
   It compares the raw join types, so two existence joins with fresh `exists` 
attributes never match, and the mapping it passes up (line 693) covers only the 
children's attributes.
   
   From reading the code, the usual MergeSubplans case therefore still does not 
merge when the shared filter has an `EXISTS` or `IN` subquery under `OR`: 
subqueries that read different columns of the same table. For example, with a 
column `y` added to the PR's `t`:
   
   ```sql
   SELECT (SELECT max(x) FROM t WHERE x > 50 OR EXISTS (SELECT 1 FROM s WHERE 
s.k = t.k)),
          (SELECT sum(y) FROM t WHERE x > 50 OR EXISTS (SELECT 1 FROM s WHERE 
s.k = t.k))
   ```
   
   ColumnPruning runs before RewritePredicateSubquery, so the left sides of the 
two existence joins read (k, x) and (k, x, y), and `checkIdenticalPlans` 
rejects them even with this change. Without the `EXISTS`, the same pair merges. 
The same applies to any pair whose existence joins differ only below the join, 
including the stacked joins of a filter with several `EXISTS` or `IN` 
subqueries under `OR`. Results stay correct, but each subquery still scans `t` 
and runs its own existence join. Exchange reuse, subquery reuse and the AQE 
stage cache compare canonical forms, so this change already covers them.
   
   To close the gap, that case could compare `np.joinType.canonicalized == 
cp.joinType.canonicalized`. For two existence joins, it would then add the new 
plan's `exists` attribute, mapped to the cached plan's, to the mapping it 
returns, because the merged join keeps the cached join type. 
`filterSafeForJoin` can stay as it is.
   
   **Verification:**
   - **Regression:** Pairs of scalar subqueries over the same table whose 
filters contain the same EXISTS or IN subqueries under OR, and whose existence 
joins differ only below the join in the columns their children read, are merged 
into one subquery, both when the filter has one such subquery and when it has 
two that stack two existence joins; the merged plan reads the cached plan's 
`exists` columns, and the query returns the same values as when the subqueries 
run separately.
   - **Compatibility:** Subqueries whose existence joins differ in the join 
condition, and a subquery with an existence join next to one with a different 
join type, are still not merged, and with filter propagation through joins 
enabled, subqueries whose existence joins differ by a filter on either side of 
the join are still not merged.



-- 
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