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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -85,30 +111,31 @@ object ReorderJoin extends Rule[LogicalPlan] with 
PredicateHelper {
       val (right, innerJoinType) = conditionalJoin.getOrElse(rest.head)
 
       val joinedRefs = left.outputSet ++ right.outputSet
-      val (joinConditions, others) = conditions.partition(
-        e => e.references.subsetOf(joinedRefs) && canEvaluateWithinJoin(e))
+      val (joinConditions, others) = conditions.partition(e => 
e.references.subsetOf(joinedRefs) &&
+        canEvaluateWithinJoin(e) && !keptAbove.contains(e))
       val joined = Join(left, right, innerJoinType,
         joinConditions.reduceLeftOption(And), JoinHint.NONE)
 
       // should not have reference to same logical plan
-      createOrderedJoin(Seq((joined, Inner)) ++ rest.filterNot(_._1 eq right), 
others)
+      createOrderedJoin(Seq((joined, Inner)) ++ rest.filterNot(_._1 eq right), 
others, keptAbove)
     }
   }
 
   def apply(plan: LogicalPlan): LogicalPlan = plan.transformWithPruning(
     _.containsPattern(INNER_LIKE_JOIN), ruleId) {
     case p @ ExtractFiltersAndInnerJoins(input, conditions)
         if input.size > 2 && conditions.nonEmpty =>
+      val keptAbove = conditionsKeptAbove(conditions, input.map(_._1))

Review Comment:
   Thank you. Done. `ReorderJoin` now passes only the movable conditions to 
`createOrderedJoin`, which is the same as on master, and puts the kept ones in 
a `Filter` above its result. I removed the direct `createOrderedJoin` test. The 
two plan tests give the same plans and the same top-filter order.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/joins.scala:
##########
@@ -43,21 +43,46 @@ import org.apache.spark.util.Utils
  * If star schema detection is enabled, reorder the star join plans based on 
heuristics.
  */
 object ReorderJoin extends Rule[LogicalPlan] with PredicateHelper {
+  /**
+   * Returns the conditions that stay in the [[Filter]] above all the joins, 
instead of going into
+   * the condition of a join. The first join that covers the references of a 
condition can be
+   * below the filter or the join that the condition is written in, where it 
would be evaluated
+   * on rows that a join above removes. So a non-deterministic or throwable 
condition stays above,
+   * and so does a condition written after it that can fail: one that
+   * [[ExprUtils.canEvaluateUnconditionally]] rejects or that reads a column 
of `plans` that can
+   * fail (see `failableOutput`). It would be evaluated on rows that the 
condition before it may
+   * have dropped. `conditions` are in their written order, as 
[[ExtractFiltersAndInnerJoins]]
+   * returns them: the ones of a lower join or filter before the ones above it.
+   */
+  private def conditionsKeptAbove(

Review Comment:
   Done. I moved `splitMovable` to `PredicateHelper`, and both 
`PushPredicateThroughJoin` and `ReorderJoin` use it.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/JoinSuite.scala:
##########
@@ -2030,6 +2030,46 @@ class JoinSuite extends SharedSparkSession with 
AdaptiveSparkPlanHelper
       cached.unpersist()
     }
   }
+
+  test("SPARK-61930: reordering inner joins does not move a throwable filter 
condition into " +
+    "a lower join") {
+    withTable("s") {
+      Seq((-1, 10), (1, 20)).toDF("id", "value").write.saveAsTable("s")
+      // The join with w removes the row whose id is -1, so raise_error is not 
evaluated on it.
+      val df = sql(
+        """SELECT s.id FROM s
+          |JOIN VALUES (10), (20) AS v(value) ON s.value = v.value
+          |JOIN VALUES (20) AS w(value) ON s.value = w.value
+          |WHERE CASE WHEN s.id > 0 THEN true ELSE raise_error('boom') END
+          |""".stripMargin)
+      checkAnswer(df, Row(1))

Review Comment:
   Done. The test checks the `CASE WHEN` in the `ON` clause of the join with 
`w` as well.
   



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