dongjoon-hyun commented on code in PR #60711:
URL: https://github.com/apache/spark/pull/60711#discussion_r4647741864


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -260,16 +260,36 @@ object ExtractFiltersAndInnerJoins extends 
PredicateHelper {
   def flattenJoin(plan: LogicalPlan, parentJoinType: InnerLike = Inner)
       : (Seq[(LogicalPlan, InnerLike)], Seq[Expression]) = plan match {
     case Join(left, right, joinType: InnerLike, cond, hint) if hint == 
JoinHint.NONE =>
-      val (plans, conditions) = flattenJoin(left, joinType)
+      val (plans, conditions) = flattenBelow(left, joinType)
       (plans ++ Seq((right, joinType)), conditions ++
         cond.toSeq.flatMap(splitConjunctivePredicates))
     case Filter(filterCondition, j @ Join(_, _, _: InnerLike, _, hint)) if 
hint == JoinHint.NONE =>
-      val (plans, conditions) = flattenJoin(j)
+      val (plans, conditions) = flattenBelow(j, Inner)
       (plans, conditions ++ splitConjunctivePredicates(filterCondition))
 
     case _ => (Seq((plan, parentJoinType)), Seq.empty)
   }
 
+  /**
+   * [[flattenJoin]] below the top join or filter, where a join or filter with 
a condition that
+   * may not move (see `PredicateHelper.splitMovable`) is one of the plans, 
with its condition. It
+   * removes a row on which its condition is NULL, so no condition above it is 
evaluated on the row.
+   */
+  private def flattenBelow(plan: LogicalPlan, parentJoinType: InnerLike)
+      : (Seq[(LogicalPlan, InnerLike)], Seq[Expression]) = {
+    val condition = plan match {
+      case j: Join => j.condition
+      case f: Filter => Some(f.condition)
+      case _ => None
+    }
+    if (condition.exists(c =>
+        splitMovable(splitConjunctivePredicates(c), 
plan.children)._2.nonEmpty)) {
+      (Seq((plan, parentJoinType)), Seq.empty)

Review Comment:
   Thank you for the precise repro. I confirmed it: with star schema detection, 
the query fails with `DIVIDE_BY_ZERO` with the previous commit, and returns 
`[2], [5]` on master and with `JOIN sd1`.
   
   I applied your suggestion, limited to the plans that `flattenBelow` keeps 
instead of flattening, an inner join or a filter over one, so that a plan that 
was already a leaf keeps its type. The query returns `[2], [5]` now. The 
`JoinOptimizationSuite` test checks that the kept lower join and the kept lower 
filter are `Inner` plans under a cross join, which fails with `parentJoinType`. 
The catalyst optimizer suites, including `StarJoinReorderSuite`, 
`StarJoinCostBasedReorderSuite` and `JoinReorderSuite`, pass.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -97,6 +97,10 @@ 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 the 
top join or filter.
+    // `CombineFilters` does not combine a condition that can fail from a 
filter above into it.
     case p @ ExtractFiltersAndInnerJoins(input, conditions)
         if input.size > 2 && conditions.nonEmpty =>

Review Comment:
   Confirmed: your query fails with `DIVIDE_BY_ZERO` here and returns no rows 
on master, the join of two fails on master too, and with the plain comparison 
both return no rows. SPARK-62091 keeps the `WHERE` conjuncts that can fail 
above a join only when its condition is non-deterministic or throwable, so a 
join condition that can fail does not keep them above. I'll file a ticket for 
it with both queries, and the PR description lists this one as a remaining 
failure.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2625,9 +2625,13 @@ object CombineFilters extends Rule[LogicalPlan] with 
PredicateHelper {
   val applyLocally: PartialFunction[LogicalPlan, LogicalPlan] = {
     // Combines the conjuncts of the outer condition that may be evaluated 
ahead of the others
     // (see `splitMovable`) into the inner filter, and keeps the rest in a 
filter above it. The
-    // inner condition must be deterministic.
+    // inner condition must be deterministic. When it is throwable, e.g. one 
that `ReorderJoin`
+    // keeps in a filter in place of the top join, only the outer conjuncts 
that can not fail are
+    // combined: the inner filter drops a row on which its condition is NULL, 
but `NULL AND x`
+    // evaluates `x`.
     case Filter(fc, nf @ Filter(nc, grandChild)) if nc.deterministic =>
-      val (movable, rest) = splitMovable(splitConjunctivePredicates(fc), 
Seq(grandChild))
+      val (movable, rest) = splitMovable(
+        splitConjunctivePredicates(fc), Seq(grandChild), afterBarrier = 
nc.throwable)

Review Comment:
   Confirmed: it fails with `DIVIDE_BY_ZERO` on master and here, since 
`ScanOperation` merges the two filters over the file scan again. I'll file a 
ticket for the scan path.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -260,16 +260,36 @@ object ExtractFiltersAndInnerJoins extends 
PredicateHelper {
   def flattenJoin(plan: LogicalPlan, parentJoinType: InnerLike = Inner)
       : (Seq[(LogicalPlan, InnerLike)], Seq[Expression]) = plan match {
     case Join(left, right, joinType: InnerLike, cond, hint) if hint == 
JoinHint.NONE =>
-      val (plans, conditions) = flattenJoin(left, joinType)
+      val (plans, conditions) = flattenBelow(left, joinType)
       (plans ++ Seq((right, joinType)), conditions ++
         cond.toSeq.flatMap(splitConjunctivePredicates))
     case Filter(filterCondition, j @ Join(_, _, _: InnerLike, _, hint)) if 
hint == JoinHint.NONE =>
-      val (plans, conditions) = flattenJoin(j)
+      val (plans, conditions) = flattenBelow(j, Inner)
       (plans, conditions ++ splitConjunctivePredicates(filterCondition))
 
     case _ => (Seq((plan, parentJoinType)), Seq.empty)
   }
 
+  /**
+   * [[flattenJoin]] below the top join or filter, where a join or filter with 
a condition that

Review Comment:
   Updated both: the object doc says that a filter or inner join below the top 
one with a condition that may not move is one of the plans, with its condition, 
and the `flattenJoin` scaladoc says that it does not flatten the joins below 
one that `flattenBelow` keeps.
   



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