viirya commented on code in PR #60646: URL: https://github.com/apache/spark/pull/60646#discussion_r4608248806
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala: ########## @@ -3349,17 +3349,38 @@ object PushPredicateThroughNonJoin extends Rule[LogicalPlan] with PredicateHelpe * Check https://cwiki.apache.org/confluence/display/Hive/OuterJoinBehavior for more details */ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { + /** + * Splits conjuncts, in their written order, into the ones that may be evaluated ahead of the + * others, by pushing them below the join or moving them into its condition, and the ones that + * may not. A non-deterministic or throwable conjunct may not, and nor may a conjunct written + * after it that can fail, as in [[CombineFilters]]: one that + * [[ExprUtils.canEvaluateUnconditionally]] rejects or that reads a column of either side that + * can fail (see `failableOutput`). It would be evaluated on rows that the conjunct before it may + * have dropped. The conjuncts that may not move are returned in their written order. + */ + private def splitMovable( + condition: Seq[Expression], + left: LogicalPlan, + right: LogicalPlan): (Seq[Expression], Seq[Expression]) = { + val (leading, following) = condition.span(cond => cond.deterministic && !cond.throwable) + lazy val failable = failableOutput(left) ++ failableOutput(right) Review Comment: `failableOutput` returns nothing for a `Join`, so a nested join gets past this check. Take a filter `rand() > 0.1 AND q > 0` over `(tq JOIN r) LEFT JOIN s`, where `tq` projects `10L / (id - 3) AS q`. `q > 0` moves into the inner join, where no `rand()` is in front of it anymore, and it ends up as `(10.0 / (id - 3)) > 0` right above the scan. Since this rule is the first caller of the helper over a join, could the helper look through `Join` here? If not, could the description list this limitation next to the one for `InferFiltersFromConstraints`? ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala: ########## @@ -3419,18 +3443,22 @@ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { // push the where condition down into join filter case f @ Filter(filterCondition, Join(left, right, joinType, joinCondition, hint)) if canPushThrough(joinType) => + val conjuncts = splitConjunctivePredicates(filterCondition) val (leftFilterConditions, rightFilterConditions, commonFilterCondition) = - split(splitConjunctivePredicates(filterCondition), left, right) + split(conjuncts, left, right) joinType match { case _: InnerLike => // push down the single side `where` condition into respective sides val newLeft = leftFilterConditions. reduceLeftOption(And).map(Filter(_, left)).getOrElse(left) val newRight = rightFilterConditions. reduceLeftOption(And).map(Filter(_, right)).getOrElse(right) - // don't push throwable expressions into join condition - val (newJoinConditions, others) = - commonFilterCondition.partition(cond => canEvaluateWithinJoin(cond) && !cond.throwable) + // don't push throwable expressions into join condition, nor the other conjuncts that + // may not move + val stayUp = splitMovable(conjuncts, left, right)._2 Review Comment: This runs `splitMovable` a second time, after `split` already ran it, so `failableOutput` is walked again, and it depends on `==` for `contains`. Could `split` return `stayUp` instead? That would also make `!cond.throwable` on the next line unnecessary. ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala: ########## @@ -3349,17 +3349,38 @@ object PushPredicateThroughNonJoin extends Rule[LogicalPlan] with PredicateHelpe * Check https://cwiki.apache.org/confluence/display/Hive/OuterJoinBehavior for more details */ object PushPredicateThroughJoin extends Rule[LogicalPlan] with PredicateHelper { + /** + * Splits conjuncts, in their written order, into the ones that may be evaluated ahead of the + * others, by pushing them below the join or moving them into its condition, and the ones that + * may not. A non-deterministic or throwable conjunct may not, and nor may a conjunct written + * after it that can fail, as in [[CombineFilters]]: one that Review Comment: On master, `CombineFilters` doesn't check `failableOutput` yet; that check comes with #60631. Until that merges, "as in CombineFilters" holds only for the `canEvaluateUnconditionally` part. ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala: ########## @@ -1203,6 +1203,26 @@ class FilterPushdownSuite extends PlanTest { comparePlans(optimized, originalQuery) } + test("SPARK-61924: joins: do not push a condition that can fail ahead of a " + + "non-deterministic one") { + // The divisions are built in ANSI mode, where they can fail, and so can `q` and `p`, which + // they compute. After `rand(0) > 0.1`, a condition that can fail by itself or reads `q` or `p` + // stays above the join, while one over `a`, which can not fail, is pushed. So is a division + // written before `rand(0) > 0.1`. + def divide(l: Expression, r: Expression): Expression = + Divide(l, r, NumericEvalContext(EvalMode.ANSI)) + val left = testRelation.select($"a", $"b", divide($"a", $"b").as("q")) + val right = testRelation1.select($"d", divide($"d", Literal(2)).as("p")) + val originalQuery = left.join(right).where(divide($"a", $"b") > 1 && Rand(0) > 0.1 && Review Comment: Every conjunct written after `Rand(0)` here reads one side only. Could you add one that reads both sides and can fail, e.g. an ANSI `$"a" + 1 === $"d"`, and expect it in the filter above a join that has no condition? That would pin the change from equi-join to nested loop join that the description mentions. Given how much that costs, could the migration guide mention it too? -- 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]
