LuciferYang commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4646029382


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -97,8 +97,15 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
 
   def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
     _.containsPattern(INNER_LIKE_JOIN), ruleId) {
+    // `ExtractFiltersAndInnerJoins` does not flatten a lower join or filter 
with a condition that
+    // may not move (see `splitMovable`), so such conditions are only in the 
top join or filter.
+    // They stay in a filter above all the joins, which takes the place of a 
top filter. It can not
+    // take the place of a top join, since `CombineFilters` can merge a filter 
above it into it,
+    // e.g. one with a subquery that would stay above the join, and `NULL AND 
x` evaluates `x`. So
+    // the joins are not reordered when the top join has such a condition.
     case p @ ExtractFiltersAndInnerJoins(input, conditions)
-        if input.size > 2 && conditions.nonEmpty =>
+        if input.size > 2 && conditions.nonEmpty &&
+          (p.isInstanceOf[Filter] || splitMovable(conditions, 
input.map(_._1))._2.isEmpty) =>

Review Comment:
   I reproduced your `CAST` counterexample: with the kept conditions in the top 
join it fails with `CAST_INVALID_INPUT`, while this commit returns no rows, and 
all the earlier forms return 2 here.
   
   The top-join refusal keeps the written order, so `SELECT * FROM ta JOIN td 
JOIN tc ON tc.k2 = ta.k AND td.j2 = tc.j AND <raise_error CASE WHEN>` (`INT` 
columns) now plans a `BroadcastNestedLoopJoin` and fails with 
`spark.sql.crossJoin.enabled=false`, while master and 4.2.0 use hash joins. 
Moving `td.j2 = tc.j` into a `WHERE` clause gives the same.
   
   Locally, dropping the refusal and having `CombineFilters` pass 
`afterBarrier` to `splitMovable` when the inner filter has a throwable 
conjunct, as SPARK-62091 does for joins, keeps the hash joins, returns 2 for 
all the forms and no rows for `CAST`, and passes `FilterPushdownSuite`, 
`PruneFiltersSuite`, `InferFiltersFromConstraintsSuite` and the SPARK-6* tests 
of `JoinSuite`. It changes `CombineFilters`, though. What do you think?



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