dongjoon-hyun commented on code in PR #60646:
URL: https://github.com/apache/spark/pull/60646#discussion_r4608501428
##########
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:
Thank you. I moved the side partitioning of `split` into `splitBySide`. The
filter-over-join case now calls `splitMovable` once and splits only the
conjuncts that may move by side, so the ones that stay up never reach the join
condition, and the `!cond.throwable` and `contains` checks are gone. `split`
still returns the ones that stay up with the common conditions, so the other
callers keep them without appending them themselves.
##########
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:
Right. I removed the reference to `CombineFilters`.
##########
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:
Thank you. I added `a + 1 = d` with an ANSI addition after `rand(0) > 0.1`.
The expected plan has it in the filter above a join without a condition.
##########
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:
Thank you. #60643 (SPARK-61921) makes `failableOutput` look through a join,
which covers this case too, so I'd like to keep that change there. I added this
case to the PR description, next to the one for `InferFiltersFromConstraints`.
--
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]