MaxGekk commented on code in PR #59985:
URL: https://github.com/apache/spark/pull/59985#discussion_r4522468294


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2305,6 +2305,29 @@ 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
+   * in its order does not evaluate it before the conjuncts that precede that 
one, whose errors
+   * it would hide, e.g. `isnotnull(x)` from `x > 0` in `y + 1 > 0 AND x > 0`.
+   */
+  private def withInferredPredicates(
+      inferred: Seq[Expression],
+      condition: Expression): Expression = {
+    if (conf.evaluateNotNullChecksInOrder) {

Review Comment:
   Agreed that it's acceptable, since a pushed filter drops rows before the 
scan's output anyway. The config doc and the PR description now say that a 
predicate pushed down to a data source is still evaluated before the others, by 
the source or after the scan.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala:
##########
@@ -253,25 +315,29 @@ trait GeneratePredicateHelper extends PredicateHelper {
   protected def precedingNotNullChecks(
       notNullPreds: Seq[Expression],
       precedingNotNulls: Seq[Int],
+      atPosition: Seq[Boolean],
+      movesChecks: Seq[Boolean],
       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 {
+      case (_, idx) => generated(idx)
+    }
+    val positioned = if (pending.isEmpty) {
+      Nil
+    } else if (afterNullable && (mayRaise(conjunct) || pending.exists(p => 
mayRaise(p._1)))) {
+      pending
+    } else if (movesChecks.nonEmpty && movesChecks(k) &&

Review Comment:
   Done in 6c21b2bbbe3. `notNullCheckPlacement` returns 
`Option[NotNullCheckPlacement]`, and the branch is 
`placement.exists(_.movesChecks(k))` with the filter alone.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/InferFiltersFromConstraintsSuite.scala:
##########
@@ -26,7 +26,7 @@ import org.apache.spark.sql.catalyst.rules._
 import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.types._
 
-class InferFiltersFromConstraintsSuite extends PlanTest {
+class InferFiltersFromConstraintsSuite extends PlanTest with PredicateHelper {

Review Comment:
   Done in 6c21b2bbbe3.



-- 
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]

Reply via email to