dongjoon-hyun commented on code in PR #60766:
URL: https://github.com/apache/spark/pull/60766#discussion_r4650981038
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3145,25 +3158,42 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
/**
* Splits `conds`, the conjuncts of a filter in their written order, into
the ones to push below
* an operator and the ones to keep above it. `canPush` tells whether the
operator lets a
- * conjunct through, and `throwable` whether it may throw (e.g.
`raise_error`). Every conjunct
- * written before the first one that cannot be pushed is pushed. After it,
only the conjuncts
- * that are not throwable are pushed, so a throwable conjunct is not
evaluated on rows the
- * conjuncts before it would have filtered out. Moving a non-throwable
conjunct below a
- * throwable one is fine, as it can only reduce the rows the throwable one
sees. The conjuncts
- * to keep above are returned in the order they were written.
+ * conjunct through, `pushedForm` gives the conjunct as it is evaluated
below the operator (e.g.
+ * with the aliases of a projection substituted), and `failable` the columns
it can read that
+ * can fail to compute (see `failableOutput`). Every conjunct written before
the first one that
+ * cannot be pushed is pushed. After it, only the conjuncts that are not
throwable (e.g.
+ * `raise_error`) are pushed, so a throwable conjunct is not evaluated on
rows the conjuncts
+ * before it would have filtered out. Moving a non-throwable conjunct below
a throwable one is
+ * fine, as it can only reduce the rows the throwable one sees. Once a
non-deterministic or
+ * throwable conjunct stays above, only the conjuncts that can not fail are
pushed after it: a
+ * conjunct that [[ExprUtils.canEvaluateUnconditionally]] rejects, or that
reads a column in
+ * `failable`, would be evaluated on rows that conjunct may have filtered
out, e.g. an ANSI
+ * division written after `rand() < 0.1`. The conjuncts to keep above are
returned in the order
+ * they were written.
*/
private def splitPushable[T](conds: Seq[T])(
- canPush: T => Boolean, throwable: T => Boolean): (Seq[T], Seq[T]) = {
+ canPush: T => Boolean,
+ pushedForm: T => Expression,
+ failable: => AttributeSet): (Seq[T], Seq[T]) = {
val (leading, rest) = conds.span(canPush)
- // The head of `rest`, if any, is the first conjunct that cannot be pushed.
- val (pushedRest, stayUp) = rest.drop(1).partition(c => canPush(c) &&
!throwable(c))
- (leading ++ pushedRest, rest.take(1) ++ stayUp)
+ // The head of `rest`, if any, is the first conjunct that cannot be
pushed, so it stays above.
+ // So does the head of `ordered`, if any, as it is non-deterministic or
throwable.
+ val (reorderable, ordered) = rest.span { c =>
+ pushedForm(c).deterministic && !pushedForm(c).throwable
Review Comment:
Thank you. You are right, this placement did not need to change. A cheap
non-deterministic conjunct that can be pushed now continues the part that is
pushed as before (`(deterministic || canPush) && !throwable`), so `rand(10) <
0.01` goes below the projection again, and the conjuncts pushed after it follow
it in the same filter. I added a plan test for this placement and corrected the
PR description.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2871,11 +2874,14 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
// split orders conditions by cost, and cost is no way to decide how
many rows a
// non-deterministic expression sees. Throwable conditions, or
conditions over throwable
// aliases, stay there too: ordering by cost could evaluate them (or
compute the aliases
- // in a lower projection) ahead of the conditions written before them.
+ // in a lower projection) ahead of the conditions written before them.
So do the cheap
+ // conditions: one is only here to keep its order (see
`splitPushable`), and splitting it
+ // off would evaluate it ahead of the conditions written before it all
the same.
val (splitChild, splitAliases, stayUp) =
if (expensiveWithUsed.nonEmpty &&
SQLConf.get.splitProjectionForExpensiveFilters) {
val (splittable, notSplittable) = expensiveWithUsed.partition {
- case (cond, _, replaced) => cond.deterministic &&
!replaced.throwable
+ case (cond, used, replaced) =>
+ cond.deterministic && !replaced.throwable && !isCheap(used)
Review Comment:
With the fix above, `rand(10) * 10 > 100` goes below the split again, so
your query returns no row, as before this PR. I confirmed it with Scala UDFs.
The remaining part, where the split filters an expensive condition that can
fail in a lower layer after an expensive or throwable condition that stays
above, also fails without this PR, and fixing it changes what the SPARK-55014
and SPARK-60602 tests expect. So I'll handle it in a separate JIRA. I narrowed
the comment of the `Project` case to the cheap conjuncts, and the PR
description mentions the remaining part.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2952,7 +2960,8 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
case filter @ Filter(condition, union: Union) =>
// Union could change the rows, so non-deterministic predicate can't be
pushed down
- val (pushDown, stayUp) =
splitPushableConjuncts(condition)(_.deterministic)
+ val (pushDown, stayUp) =
+ splitPushableConjuncts(condition,
failableOutput(union))(_.deterministic)
Review Comment:
Right, `failableOutput` has no `Union` case, and the PR description was
wrong about that example. I corrected it. As you noted,
`InferFiltersFromConstraints` reads it too, so I'll fix it in `failableOutput`
itself in a separate JIRA, together with the next comment.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -3145,25 +3158,42 @@ object PushPredicateThroughNonJoin extends
Rule[LogicalPlan] with PredicateHelpe
/**
* Splits `conds`, the conjuncts of a filter in their written order, into
the ones to push below
* an operator and the ones to keep above it. `canPush` tells whether the
operator lets a
- * conjunct through, and `throwable` whether it may throw (e.g.
`raise_error`). Every conjunct
- * written before the first one that cannot be pushed is pushed. After it,
only the conjuncts
- * that are not throwable are pushed, so a throwable conjunct is not
evaluated on rows the
- * conjuncts before it would have filtered out. Moving a non-throwable
conjunct below a
- * throwable one is fine, as it can only reduce the rows the throwable one
sees. The conjuncts
- * to keep above are returned in the order they were written.
+ * conjunct through, `pushedForm` gives the conjunct as it is evaluated
below the operator (e.g.
+ * with the aliases of a projection substituted), and `failable` the columns
it can read that
+ * can fail to compute (see `failableOutput`). Every conjunct written before
the first one that
+ * cannot be pushed is pushed. After it, only the conjuncts that are not
throwable (e.g.
+ * `raise_error`) are pushed, so a throwable conjunct is not evaluated on
rows the conjuncts
+ * before it would have filtered out. Moving a non-throwable conjunct below
a throwable one is
+ * fine, as it can only reduce the rows the throwable one sees. Once a
non-deterministic or
+ * throwable conjunct stays above, only the conjuncts that can not fail are
pushed after it: a
+ * conjunct that [[ExprUtils.canEvaluateUnconditionally]] rejects, or that
reads a column in
+ * `failable`, would be evaluated on rows that conjunct may have filtered
out, e.g. an ANSI
+ * division written after `rand() < 0.1`. The conjuncts to keep above are
returned in the order
+ * they were written.
*/
private def splitPushable[T](conds: Seq[T])(
- canPush: T => Boolean, throwable: T => Boolean): (Seq[T], Seq[T]) = {
+ canPush: T => Boolean,
+ pushedForm: T => Expression,
+ failable: => AttributeSet): (Seq[T], Seq[T]) = {
val (leading, rest) = conds.span(canPush)
- // The head of `rest`, if any, is the first conjunct that cannot be pushed.
- val (pushedRest, stayUp) = rest.drop(1).partition(c => canPush(c) &&
!throwable(c))
- (leading ++ pushedRest, rest.take(1) ++ stayUp)
+ // The head of `rest`, if any, is the first conjunct that cannot be
pushed, so it stays above.
+ // So does the head of `ordered`, if any, as it is non-deterministic or
throwable.
+ val (reorderable, ordered) = rest.span { c =>
+ pushedForm(c).deterministic && !pushedForm(c).throwable
+ }
+ lazy val failableColumns = failable
+ val (pushedReorderable, stayUpReorderable) = reorderable.partition(canPush)
+ val (pushedOrdered, stayUpOrdered) = ordered.partition { c =>
+ canPush(c) && ExprUtils.canEvaluateUnconditionally(pushedForm(c)) &&
+ !pushedForm(c).references.exists(failableColumns.contains)
Review Comment:
Confirmed. These queries also fail without this PR, and the same gap reaches
`CombineFilters` and `PushPredicateThroughJoin` through `splitMovable`. A
change to `failableOutput` changes all of its callers, so I'll handle it in a
separate JIRA. The PR description mentions it.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:
##########
@@ -1096,6 +1096,65 @@ class FilterPushdownSuite extends PlanTest {
comparePlans(Optimize.execute(originalQuery.analyze),
correctAnswer.analyze)
}
+ // Built in ANSI mode, where it can fail, whatever the session's mode is.
+ private def ansiDivide(l: Expression, r: Expression): Expression =
+ Divide(l, r, NumericEvalContext(EvalMode.ANSI))
+
+ test("SPARK-62144: a condition that can fail is not pushed through an
aggregate ahead of a " +
Review Comment:
Added a plan test over a `Window` and a `Sort` whose column comes from a
division in the projection below, which fails when either call site passes an
empty set, the placement test over a `Project`, and a division in legacy mode
that is still pushed after `rand(10) > 0.5`.
--
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]