MaxGekk commented on code in PR #59985:
URL: https://github.com/apache/spark/pull/59985#discussion_r4535020502
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2305,6 +2305,31 @@ object InferFiltersFromConstraints extends
Rule[LogicalPlan]
}
}
+ /**
+ * Adds the predicates inferred from the conjuncts of `condition` to it: at
the front, or, with
+ * `spark.sql.optimizer.evaluateNotNullChecksInOrder`, each just before the
first conjunct that
+ * references one of its columns, from which it is inferred, so that
evaluating the condition
Review Comment:
Good point. Done in 85af45e80f6: with the config, `withInferredPredicates`
computes the constraints of each prefix of the condition with the child
(`Filter(prefix, child).constraints`) until every inferred predicate is placed,
and puts each one just before the first conjunct at which the prefix implies
it. The whole condition implies them all, so none is left over. In `coalesce(x,
1) + y > 0 AND x > 0`, `isnotnull(x)` now goes just before `x > 0`, and in `a =
b AND y + 1 > 0 AND b = c`, `a = c` goes just before `b = c`. Both are in the
`InferFiltersFromConstraintsSuite` test, which fails with the previous
placement. The extra constraint computation happens only with the config. The
Scaladocs and the PR description now describe it this way.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1833,6 +1833,33 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val EVALUATE_NOT_NULL_CHECKS_IN_ORDER =
+ buildConf("spark.sql.optimizer.evaluateNotNullChecksInOrder")
+ .internal()
+ .doc("When true, an IS NOT NULL check in a filter condition, including
the FILTER clause " +
+ "of an aggregate function, is evaluated in the order of the condition,
as the " +
+ "interpreted evaluation does, so that it drops a row before an error
that a later " +
+ "conjunct raises on the row. With whole-stage codegen, a check that
cannot raise an " +
+ "error is evaluated at its position before a conjunct that can raise
one, is " +
+ "nondeterministic or calls a user-defined function, instead of just
before the first " +
+ "conjunct that references its column; a check whose column the same
stage has not " +
+ "computed yet, by an expression that can raise, keeps the latter
placement, except " +
+ "after a conjunct that can be NULL, as when false. A predicate that
the optimizer " +
+ "infers from a conjunct is placed just before the first conjunct that
references its " +
+ "columns, or at the end if there is none, instead of at the front of
the condition, so " +
+ "with whole-stage codegen off it is evaluated after the
nondeterministic or " +
+ "user-defined function conjuncts that precede that one. A predicate
pushed down to a " +
+ "data source is still evaluated before the others, by the source or
after the scan. " +
Review Comment:
Right. Done in 85af45e80f6: the doc keeps the sentence for DSv2 sources, and
says that after the scan of a DSv1 source, a pushed predicate that the source
doesn't handle is evaluated after the ones that cannot be pushed down. The PR
description says the same, with `DataSourceStrategy.selectFilters`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1833,6 +1833,33 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val EVALUATE_NOT_NULL_CHECKS_IN_ORDER =
+ buildConf("spark.sql.optimizer.evaluateNotNullChecksInOrder")
+ .internal()
+ .doc("When true, an IS NOT NULL check in a filter condition, including
the FILTER clause " +
+ "of an aggregate function, is evaluated in the order of the condition,
as the " +
+ "interpreted evaluation does, so that it drops a row before an error
that a later " +
+ "conjunct raises on the row. With whole-stage codegen, a check that
cannot raise an " +
+ "error is evaluated at its position before a conjunct that can raise
one, is " +
Review Comment:
Done in 85af45e80f6: the doc now lists a conjunct that evaluates a column
that the same stage computes on demand by an expression that can raise.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -253,25 +349,27 @@ trait GeneratePredicateHelper extends PredicateHelper {
protected def precedingNotNullChecks(
notNullPreds: Seq[Expression],
precedingNotNulls: Seq[Int],
+ placement: Option[NotNullCheckPlacement],
k: Int,
conjunct: Expression,
afterNullable: Boolean,
generated: Array[Boolean]): Seq[Expression] = {
- if (afterNullable) {
- val pending =
notNullPreds.take(precedingNotNulls(k)).zipWithIndex.filterNot {
- case (_, idx) => generated(idx)
- }
- if (pending.nonEmpty && (mayRaise(conjunct) || pending.exists(p =>
mayRaise(p._1)))) {
- pending.map { case (p, idx) =>
- generated(idx) = true
- p
- }
- } else {
- Nil
- }
+ val pending =
notNullPreds.take(precedingNotNulls(k)).zipWithIndex.filterNot {
Review Comment:
Done in 85af45e80f6: `pending` is a lazy val, and the method returns `Nil`
first unless `afterNullable` or a placement is given, as on master.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -581,8 +697,13 @@ case class FilterExec(condition: Expression, child:
SparkPlan)
case ((orig, bound), idx) =>
val afterNullable = nullFlag.isDefined && nullableSeen
precedingNotNullChecks(
- notNullPreds, precedingNotNulls, idx, orig, afterNullable,
- generatedIsNotNullChecks).foreach { p =>
+ notNullPreds, precedingNotNulls, placement, idx, orig,
+ afterNullable, generatedIsNotNullChecks).foreach { p =>
+ // The column of a check generated here needs no synthetic
check below.
+ p match {
+ case IsNotNull(a: Attribute) => extraIsNotNullAttrs += a
Review Comment:
Done in 85af45e80f6: both paths use `extraIsNotNullAttrs ++=
preceding.collect { case IsNotNull(a: Attribute) => a }`.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -510,11 +617,18 @@ case class FilterExec(condition: Expression, child:
SparkPlan)
//
// `subexpressionElimination.filterExec.enabled` additionally gates this
path so it can be
// turned off independently of subexpression elimination elsewhere.
+ //
+ // With `spark.sql.optimizer.evaluateNotNullChecksInOrder`, the prologue
would evaluate a
+ // column that this stage computes on demand by an expression that can
raise before any
+ // check, so a filter over such a column falls back to
`generatePredicateCode` too
+ // (SPARK-60329).
val (prologueCode, predicateCode) =
if (conf.subexpressionEliminationEnabled &&
conf.subexpressionEliminationFilterExecEnabled &&
otherPreds.nonEmpty &&
otherPredsEquivalentExpressions.getCommonSubexpressions
- .exists(!CollapseProject.isCheap(_))) {
+ .exists(!CollapseProject.isCheap(_)) &&
+ !computesRaisingInput(
+ AttributeSet(otherPreds.flatMap(_.references)), child.output,
input)) {
Review Comment:
Done in 85af45e80f6. `raisingInputTest` holds the rule and its per-column
cache, and `notNullCheckPlacement` and `computesRaisingInput` both use it.
`otherPredInputAttrs` is computed once, above the `if`.
--
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]