MaxGekk commented on code in PR #59985:
URL: https://github.com/apache/spark/pull/59985#discussion_r4507920202
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -173,8 +174,59 @@ trait GeneratePredicateHelper extends PredicateHelper {
* Whether evaluating `e` on a row can be observed other than through its
value: it can raise
* an error, is nondeterministic or may have side effects, such as a UDF.
Such an expression
* has to be evaluated on the rows, and in the order, that `And.eval`
evaluates it on.
+ *
+ * On top of `ExprUtils.canEvaluateUnconditionally`, a total expression over
such expressions
+ * is known not to raise: the string functions of `isTotal`, a hash, the
Bloom filter predicate
+ * of a runtime filter, and the arithmetic of numbers in the legacy
evaluation mode, which
+ * returns NULL instead of raising. A column that an operator of the same
stage computes
+ * lazily, such as a projection alias or a result of an aggregate, is judged
by the expression
+ * that computes it, see `producedBy`, since a check on the column evaluates
that expression.
+ */
+ protected def mayRaise(e: Expression): Boolean = {
Review Comment:
Fixed in 94228e373fa. The NULL-conjunct flag and the `afterNullable` branch
keep master's classification and ignore the new config, and with the config off
the placement and the generated code are as on master. The bot's `NOT (m > 0
AND mw > 0) AND r IS NOT NULL AND mz > 0` case is a test with the config on and
off.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2260,6 +2260,23 @@ object InferFiltersFromGenerate extends
Rule[LogicalPlan] {
object InferFiltersFromConstraints extends Rule[LogicalPlan]
with PredicateHelper with ConstraintHelper {
+ /**
+ * Marks a predicate that this rule infers, as opposed to one written in the
query. An inferred
+ * `IsNotNull` check is implied by the conjunct it was inferred from, so the
whole-stage codegen
+ * of a filter keeps generating it lazily, before the first conjunct that
references its column,
+ * where it generates a check written in the query at its position: before
an earlier conjunct
+ * that can raise an error, when the check cannot raise one itself, see
+ * `GeneratePredicateHelper.precedingNotNullChecks`.
+ */
+ val INFERRED = TreeNodeTag[Unit]("inferred")
+
+ private def markInferred(predicates: Iterable[Expression]): Seq[Expression]
= {
Review Comment:
Gone with the tag in 94228e373fa: nothing mutates the shared `IsNotNull`
instances anymore.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -256,22 +319,25 @@ trait GeneratePredicateHelper extends PredicateHelper {
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
+ generated: Array[Boolean]): Seq[Int] = {
+ val pending =
notNullPreds.take(precedingNotNulls(k)).zipWithIndex.filterNot {
+ case (_, idx) => generated(idx)
+ }
+ val conjunctMayRaise = mayRaise(conjunct)
Review Comment:
Done in 94228e373fa. `notNullCheckPlacement` computes, once per operator,
which checks can move and which conjuncts move them, and resolves each deferred
column once. `precedingNotNullChecks` returns right away when nothing is
pending.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -173,8 +174,59 @@ trait GeneratePredicateHelper extends PredicateHelper {
* Whether evaluating `e` on a row can be observed other than through its
value: it can raise
* an error, is nondeterministic or may have side effects, such as a UDF.
Such an expression
* has to be evaluated on the rows, and in the order, that `And.eval`
evaluates it on.
+ *
+ * On top of `ExprUtils.canEvaluateUnconditionally`, a total expression over
such expressions
+ * is known not to raise: the string functions of `isTotal`, a hash, the
Bloom filter predicate
+ * of a runtime filter, and the arithmetic of numbers in the legacy
evaluation mode, which
+ * returns NULL instead of raising. A column that an operator of the same
stage computes
+ * lazily, such as a projection alias or a result of an aggregate, is judged
by the expression
+ * that computes it, see `producedBy`, since a check on the column evaluates
that expression.
+ */
+ protected def mayRaise(e: Expression): Boolean = {
+ val resolved = e.transform { case a: Attribute => producedBy(a) }
+ !(ExprUtils.canEvaluateUnconditionally(resolved) || isTotal(resolved))
+ }
+
+ private def isTotal(e: Expression): Boolean = e.deterministic &&
isTotalInternal(e)
+
+ // The structural cases of `ExprUtils.canEvaluateUnconditionally`, plus the
total functions.
+ private def isTotalInternal(e: Expression): Boolean = e match {
Review Comment:
Done in 94228e373fa. The PR's list is gone and only
`ExprUtils.canEvaluateUnconditionally` is used. SPARK-60882 has since extended
it on master with `StartsWith`, the casts that cannot fail, date fields and
LEGACY arithmetic. `upper` and the hashes are not on it, so a check moves
before them with the config on. That cost goes away with SPARK-60872 too.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -173,8 +174,59 @@ trait GeneratePredicateHelper extends PredicateHelper {
* Whether evaluating `e` on a row can be observed other than through its
value: it can raise
* an error, is nondeterministic or may have side effects, such as a UDF.
Such an expression
* has to be evaluated on the rows, and in the order, that `And.eval`
evaluates it on.
+ *
+ * On top of `ExprUtils.canEvaluateUnconditionally`, a total expression over
such expressions
+ * is known not to raise: the string functions of `isTotal`, a hash, the
Bloom filter predicate
+ * of a runtime filter, and the arithmetic of numbers in the legacy
evaluation mode, which
+ * returns NULL instead of raising. A column that an operator of the same
stage computes
+ * lazily, such as a projection alias or a result of an aggregate, is judged
by the expression
+ * that computes it, see `producedBy`, since a check on the column evaluates
that expression.
+ */
+ protected def mayRaise(e: Expression): Boolean = {
+ val resolved = e.transform { case a: Attribute => producedBy(a) }
+ !(ExprUtils.canEvaluateUnconditionally(resolved) || isTotal(resolved))
+ }
+
+ private def isTotal(e: Expression): Boolean = e.deterministic &&
isTotalInternal(e)
+
+ // The structural cases of `ExprUtils.canEvaluateUnconditionally`, plus the
total functions.
+ private def isTotalInternal(e: Expression): Boolean = e match {
+ case _: AttributeReference | _: Literal => true
+ case _: And | _: Or | _: Not | _: BinaryComparison | _: IsNull | _:
IsNotNull | _: IsNaN |
+ _: NullIf | _: Coalesce | _: In | _: InSet |
+ _: GetStructField | _: GetArrayStructFields | _: GetMapValue |
+ _: Upper | _: Lower | _: Length | _: StringTrim | _: StringTrimLeft |
+ _: StringTrimRight | _: Substring | _: Concat | _: XxHash64 | _:
Murmur3Hash =>
+ e.children.forall(isTotalInternal)
+ // The Bloom filter is a scalar subquery already evaluated, or a literal.
+ case b: BloomFilterMightContain => isTotalInternal(b.valueExpression)
+ case b: BinaryArithmetic
+ if b.evalMode == EvalMode.LEGACY &&
b.dataType.isInstanceOf[NumericType] =>
+ e.children.forall(isTotalInternal)
+ case _ => false
+ }
+
+ /**
+ * The expression that computes `attr` lazily in this stage, when an
operator between this one
+ * and the input of the stage does: a projection alias, or a result
expression of an aggregate,
+ * whose own inputs, the aggregation buffers, are not computed lazily. The
attribute itself
+ * otherwise: an input of the stage, read from a scan, a columnar batch or
an exchange, or an
+ * output that its operator computes before this one runs.
*/
- protected def mayRaise(e: Expression): Boolean =
!ExprUtils.canEvaluateUnconditionally(e)
+ protected def producedBy(attr: Attribute): Expression = {
+ def substitute(e: Expression, named: Seq[NamedExpression]): Expression = {
Review Comment:
Done in 94228e373fa: `getAliasMap` and `replaceAlias`.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -256,22 +319,25 @@ trait GeneratePredicateHelper extends PredicateHelper {
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
+ generated: Array[Boolean]): Seq[Int] = {
Review Comment:
Reverted in 94228e373fa; it returns the checks again.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2274,7 +2291,7 @@ object InferFiltersFromConstraints extends
Rule[LogicalPlan]
val newFilters = filter.constraints --
(child.constraints ++ splitConjunctivePredicates(condition))
if (newFilters.nonEmpty) {
- Filter(And(newFilters.reduce(And), condition), child)
+ Filter(And(markInferred(newFilters).reduce(And), condition), child)
Review Comment:
Yes, it works, and it's done in 94228e373fa; please see the top-level
comment. With the config, an inferred predicate goes just before the first
conjunct that references its columns, and the codegen places every check by its
position, without a tag. The one difference from your sketch is that the
predicates inferred for a side of a join are left in a filter of their own. The
codegen treats them by position too, so they are correct but can cost a decode
before a conjunct that can raise.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1808,6 +1808,21 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val LEGACY_FILTER_LAZY_NOT_NULL_CHECK_PLACEMENT =
+ buildConf("spark.sql.legacy.filterLazyNotNullCheckPlacement")
Review Comment:
Done in 94228e373fa: `spark.sql.optimizer.evaluateNotNullChecksInOrder`,
which says what `true` does. It is no longer a legacy config, since the new
placement is now opt-in.
--
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]