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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,18 +95,38 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
     }
   }
 
+  /**
+   * Returns the condition of the filter above all the joins from the 
conditions kept there, each
+   * with the index of the join or filter it is written in. That join or 
filter removes a row on
+   * which its condition is NULL, so the conditions written above it are not 
evaluated on the row,
+   * but `NULL AND x` evaluates `x`. So the kept conditions of each join or 
filter but the last are
+   * wrapped as `coalesce(<their AND>, false)`, whose FALSE skips the 
conditions after them. The
+   * kept conditions of one join or filter stay in a plain AND, as they are 
written.
+   */
+  private def keptCondition(kept: Seq[(Expression, Int)]): Option[Expression] 
= {
+    val groups = kept.groupBy(_._2).toSeq.sortBy(_._1).map(_._2.map(_._1))
+    groups.lastOption.map { last =>
+      val lower = groups.init.map(g => Coalesce(Seq(g.reduceLeft(And), 
Literal.FalseLiteral)))

Review Comment:
   Confirmed on master: it returns no rows there, and with the wrapper it fails 
with `DIVIDE_BY_ZERO` with whole-stage codegen on and off, while interpreted 
evaluation returns no rows. One correction: v4.3.0 fails this query too, in 
every mode, since its `ReorderJoin` puts the division ahead of the `CASE WHEN` 
in the lower join's condition, which SPARK-61930 fixed.
   
   With the joins left as written, the two divisions are in different joins, 
and the query returns no rows. The variant with `10 / (t1.v - t2.g) < 100` 
added to the upper `ON` clause still fails, as on v4.3.0: 
`PushPredicateThroughJoin` puts that condition, which reads only `t1` and `t2`, 
ahead of the `CASE WHEN` in the lower join's condition. A join of two with that 
condition in a `WHERE` clause above it fails the same way on v4.3.0, so I'll 
handle it separately.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,18 +95,38 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
     }
   }
 
+  /**
+   * Returns the condition of the filter above all the joins from the 
conditions kept there, each
+   * with the index of the join or filter it is written in. That join or 
filter removes a row on
+   * which its condition is NULL, so the conditions written above it are not 
evaluated on the row,
+   * but `NULL AND x` evaluates `x`. So the kept conditions of each join or 
filter but the last are
+   * wrapped as `coalesce(<their AND>, false)`, whose FALSE skips the 
conditions after them. The
+   * kept conditions of one join or filter stay in a plain AND, as they are 
written.
+   */
+  private def keptCondition(kept: Seq[(Expression, Int)]): Option[Expression] 
= {
+    val groups = kept.groupBy(_._2).toSeq.sortBy(_._1).map(_._2.map(_._1))
+    groups.lastOption.map { last =>
+      val lower = groups.init.map(g => Coalesce(Seq(g.reduceLeft(And), 
Literal.FalseLiteral)))

Review Comment:
   Confirmed: with the wrapper the query raises the `CASE WHEN`'s error, while 
master, v4.3.0 and a join of two return no rows. With the joins left as 
written, `t1.b / t2.g > 0` stays in the lower join's condition, so 
`isnotnull(b)` is inferred and pushed into the `tb1` scan again, and the query 
returns no rows. I didn't run the streaming case, but the inputs of the joins 
get the same inferred filters as on v4.3.0 for this shape. I also removed the 
sentence about the lost inferred filters from the PR description.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -95,18 +95,38 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
     }
   }
 
+  /**
+   * Returns the condition of the filter above all the joins from the 
conditions kept there, each
+   * with the index of the join or filter it is written in. That join or 
filter removes a row on
+   * which its condition is NULL, so the conditions written above it are not 
evaluated on the row,
+   * but `NULL AND x` evaluates `x`. So the kept conditions of each join or 
filter but the last are
+   * wrapped as `coalesce(<their AND>, false)`, whose FALSE skips the 
conditions after them. The
+   * kept conditions of one join or filter stay in a plain AND, as they are 
written.
+   */
+  private def keptCondition(kept: Seq[(Expression, Int)]): Option[Expression] 
= {
+    val groups = kept.groupBy(_._2).toSeq.sortBy(_._1).map(_._2.map(_._1))
+    groups.lastOption.map { last =>
+      val lower = groups.init.map(g => Coalesce(Seq(g.reduceLeft(And), 
Literal.FalseLiteral)))

Review Comment:
   With the wrapper gone, the groups only decide whether the joins are 
reordered. The extractor test now asserts the groups for a plan with join and 
filter conditions, and the new `ReorderJoin` test checks that the plan is left 
as written when both `ON` clauses keep a condition. Always reordering, merging 
a filter's conditions into the group of the join below it, and putting a 
filter's group first each fail a test. The PR description lists the forms I ran 
with a temporary suite.
   



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