viirya commented on code in PR #60677:
URL: https://github.com/apache/spark/pull/60677#discussion_r4618897128
##########
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:
Could this split `conditions` into movable and kept ones here, call
`createOrderedJoin(input, movable)` unchanged, and wrap the result in
`Filter(kept)`? Then `createOrderedJoin` keeps its signature, the three
`!keptAbove.contains(e)` checks go away (and the `==` lookup with them), the
next-plan search never sees kept conditions, and the direct `createOrderedJoin`
test is not needed.
The order still holds. A movable condition written after the first kept one
is accepted by `canEvaluateUnconditionally`, so it can always go into a join.
So anything left in the filter that `createOrderedJoin` builds was written
before the kept conditions. On the plan in the second `JoinOptimizationSuite`
test, this gives the same plan and the same top-filter order.
##########
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:
Could this also check the `CASE WHEN` in the `ON` clause of the join with
`w`? That is the case where the condition leaves the join it is written in, and
right now only the `WHERE` form is covered.
##########
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:
This repeats `PushPredicateThroughJoin.splitMovable`: the same `span`,
`canEvaluateUnconditionally` and `failableOutput` checks. Could the two share
one helper in `PredicateHelper`? The two placements depend on agreeing, and
`PushPredicateThroughNonJoin.splitPushable` already shows how such copies drift
apart.
--
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]