sarutak commented on code in PR #60337:
URL: https://github.com/apache/spark/pull/60337#discussion_r4543196718


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -195,9 +199,8 @@ object ConstantPropagation extends Rule[LogicalPlan] with 
PredicateHelper {
           traverse(a.right, replaceChildren = false, nullIsFalse)
         val equalityPredicates = equalityPredicatesLeft ++ 
equalityPredicatesRight
         val newSelf = if (equalityPredicates.nonEmpty && replaceChildren) {
-          val unreplaced = And(newLeft.getOrElse(a.left), 
newRight.getOrElse(a.right))
-          Some(moveEqualitiesFirst(
-            unreplaced, replaceConstants(unreplaced, equalityPredicates), 
equalityPredicates))
+          Some(replaceConstants(

Review Comment:
   Thank you, you are right. I missed the shapes where the conjunct that keeps 
the attribute can fail itself, which released versions rewrite and master 
guards by moving the equality. I took your suggestion: the equalities go first 
again, as on master, but only up to the preceding non-deterministic conjunct, 
and a conjunct before a non-deterministic one does not take the constant of an 
equality after it. A condition without a non-deterministic conjunct is now 
rewritten as on master, except an `EqualTo` with a NULL literal. The case of 
#59940, where the moved equality is over an alias that becomes a cast, is left 
to SPARK-61437.
   
   `n / d > 1 AND d = 5` over `(10, 0), (10, 5)` and `(a > 3 OR 10 / c > 1) AND 
a = 5` over `(2, 0), (5, 1)` now return `[10,5]` and `[5,1]` with ANSI mode on, 
as on master, and are in the `DataFrameSuite` test; with the previous head both 
failed with `DIVIDE_BY_ZERO`. The tests of SPARK-60983, SPARK-61033 and 
SPARK-60882 are as on master, two of them now also checking that the equality 
goes first. What remains is a non-deterministic conjunct in between, e.g. `n / 
d > 1 AND rand() < 0.5 AND d = 5`, which fails on the row with `d = 0` as 
written and without the rule, where master returns `[10,5]` and 4.1.4 and 4.3.0 
return no row; moving `d = 5` ahead of `rand()` would change the rows it sees. 
The description lists it as a change, and a test pins it.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -241,18 +244,105 @@ object ConstantPropagation extends Rule[LogicalPlan] 
with PredicateHelper {
    * condition cannot raise, an expression that can fail keeps its attributes, 
unless all of them
    * are replaced and the result evaluates without an error. For the same 
reason, an operand of an
    * AND, OR, COALESCE or IN in a conjunct is rewritten only if no operand 
after it can fail, as it
-   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`. The conjuncts of
-   * `condition` itself are rewritten regardless, and [[moveEqualitiesFirst]] 
orders them.
+   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`.
+   *
+   * A conjunct decides whether the ones after it run as well, but the 
conjuncts are rewritten by
+   * the rules below instead. The conjuncts keep their written order: moving 
an equality ahead of a
+   * non-deterministic conjunct changes the rows that one sees, and a later 
rule can turn an

Review Comment:
   Thanks, confirmed: with this PR, `x > a AND rand(10) < 0.5 AND a = 1` over 
your data returns 3 rows by default and 9 with `InferFiltersFromConstraints` 
excluded, the same as `where(x > a).where(rand(10) < 0.5)` filtered by `a = 1`. 
SPARK-61378, which is to add the inferred filters after the condition instead 
of in front of it, covers this; I added it to the description as still open.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -241,18 +244,105 @@ object ConstantPropagation extends Rule[LogicalPlan] 
with PredicateHelper {
    * condition cannot raise, an expression that can fail keeps its attributes, 
unless all of them
    * are replaced and the result evaluates without an error. For the same 
reason, an operand of an
    * AND, OR, COALESCE or IN in a conjunct is rewritten only if no operand 
after it can fail, as it
-   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`. The conjuncts of
-   * `condition` itself are rewritten regardless, and [[moveEqualitiesFirst]] 
orders them.
+   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`.
+   *
+   * A conjunct decides whether the ones after it run as well, but the 
conjuncts are rewritten by
+   * the rules below instead. The conjuncts keep their written order: moving 
an equality ahead of a
+   * non-deterministic conjunct changes the rows that one sees, and a later 
rule can turn an
+   * equality into an expression that can fail, e.g. by pushing it through a 
projection that defines
+   * its attribute with a cast, which would then run on rows that the 
conjuncts written before it
+   * drop. Whether a conjunct can fail is judged on it as it is when this rule 
runs, and an equality
+   * in between is not checked (SPARK-61437). A conjunct takes the constants 
of the equalities
+   * before it, which hold on the rows that reach it where they are not NULL. 
It takes the constant
+   * of an equality after it only if every conjunct in between, as rewritten, 
can be evaluated
+   * unconditionally ([[ExprUtils.canEvaluateUnconditionally]]), which rejects 
e.g. an expression
+   * that can fail or is non-deterministic: not into `y > 0` in `y > 0 AND 10 
/ x > 0 AND y = 1` or
+   * `y > 0 AND rand() < 0.5 AND y = 1`, nor into `d <> 0` in `d <> 0 AND n / 
d > 1 AND d = 5`, with
+   * the divisions in ANSI mode. Of several equalities over the same 
attribute, only the last
+   * one is propagated, together with any earlier one identical to it; the 
others are rewritten
+   * like the other conjuncts.
+   *
+   * On a row where the attribute of an `EqualTo` is NULL, the equality is 
NULL, so the row still
+   * reaches the conjuncts after it. There, a conjunct rewritten with its 
constant, before or after
+   * it, can still decide whether a later conjunct runs, e.g. `coalesce(y, 0) 
> 0` in
+   * `(y = 1 AND coalesce(y, 0) > 0 AND 10 / x > 0) OR z > 0` or in
+   * `(coalesce(y, 0) > 0 AND y = 1 AND 10 / x > 0) OR z > 0`, or `y > w` in
+   * `(y = 1 AND y > w AND rand() < 0.5) OR z > 0`, which can be FALSE instead 
of NULL there once
+   * rewritten. A condition that becomes FALSE below can be NULL there, too. 
Without whole-stage
+   * codegen, or under an OR, the conjuncts of a filter are evaluated this 
way; with it, a filter
+   * drops a row at the first conjunct that is NULL (SPARK-61438).

Review Comment:
   Thanks, confirmed: with `spark.sql.constraintPropagation.enabled` set to 
`false` and ANSI mode on, `y = 1 AND coalesce(y, 0) > 0 AND 10 / x > 0` over 
`(NULL, 0), (1, 1)` fails with `DIVIDE_BY_ZERO` on master and with this PR, 
with whole-stage codegen on or off, and returns `(1, 1)` without the rule. The 
Scaladoc now says that this also applies with whole-stage codegen where the 
generated filter goes on after a NULL conjunct when a later one can fail 
(SPARK-60315), and the description lists the case under SPARK-61438.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -241,18 +244,105 @@ object ConstantPropagation extends Rule[LogicalPlan] 
with PredicateHelper {
    * condition cannot raise, an expression that can fail keeps its attributes, 
unless all of them
    * are replaced and the result evaluates without an error. For the same 
reason, an operand of an
    * AND, OR, COALESCE or IN in a conjunct is rewritten only if no operand 
after it can fail, as it
-   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`. The conjuncts of
-   * `condition` itself are rewritten regardless, and [[moveEqualitiesFirst]] 
orders them.
+   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`.
+   *
+   * A conjunct decides whether the ones after it run as well, but the 
conjuncts are rewritten by
+   * the rules below instead. The conjuncts keep their written order: moving 
an equality ahead of a
+   * non-deterministic conjunct changes the rows that one sees, and a later 
rule can turn an
+   * equality into an expression that can fail, e.g. by pushing it through a 
projection that defines
+   * its attribute with a cast, which would then run on rows that the 
conjuncts written before it
+   * drop. Whether a conjunct can fail is judged on it as it is when this rule 
runs, and an equality
+   * in between is not checked (SPARK-61437). A conjunct takes the constants 
of the equalities
+   * before it, which hold on the rows that reach it where they are not NULL. 
It takes the constant
+   * of an equality after it only if every conjunct in between, as rewritten, 
can be evaluated
+   * unconditionally ([[ExprUtils.canEvaluateUnconditionally]]), which rejects 
e.g. an expression
+   * that can fail or is non-deterministic: not into `y > 0` in `y > 0 AND 10 
/ x > 0 AND y = 1` or
+   * `y > 0 AND rand() < 0.5 AND y = 1`, nor into `d <> 0` in `d <> 0 AND n / 
d > 1 AND d = 5`, with
+   * the divisions in ANSI mode. Of several equalities over the same 
attribute, only the last
+   * one is propagated, together with any earlier one identical to it; the 
others are rewritten
+   * like the other conjuncts.
+   *
+   * On a row where the attribute of an `EqualTo` is NULL, the equality is 
NULL, so the row still
+   * reaches the conjuncts after it. There, a conjunct rewritten with its 
constant, before or after
+   * it, can still decide whether a later conjunct runs, e.g. `coalesce(y, 0) 
> 0` in
+   * `(y = 1 AND coalesce(y, 0) > 0 AND 10 / x > 0) OR z > 0` or in
+   * `(coalesce(y, 0) > 0 AND y = 1 AND 10 / x > 0) OR z > 0`, or `y > w` in
+   * `(y = 1 AND y > w AND rand() < 0.5) OR z > 0`, which can be FALSE instead 
of NULL there once
+   * rewritten. A condition that becomes FALSE below can be NULL there, too. 
Without whole-stage
+   * codegen, or under an OR, the conjuncts of a filter are evaluated this 
way; with it, a filter
+   * drops a row at the first conjunct that is NULL (SPARK-61438).
+   *
+   * If a conjunct other than the propagated equalities is FALSE with the 
constants of all of them,
+   * e.g. `y = 1` in `y = 1 AND 10 / x > 0 AND y = 2`, where `y = 2` is 
propagated, the condition
+   * becomes FALSE, as a row that passes the equalities fails that conjunct, 
so none passes the
+   * condition. This also holds under a NOT, where the attributes are not 
nullable and an `EqualTo`
+   * with a NULL literal is not propagated, so the equalities are never NULL.
    */
   private def replaceConstants(
       condition: Expression,
       equalityPredicates: AttributeMap[(Literal, BinaryComparison)]): 
Expression = {
-    val constantsMap = AttributeMap(equalityPredicates.map { case (attr, (lit, 
_)) => attr -> lit })
     val predicates = equalityPredicates.values.map(_._2).toSet
+    val attributeOf: Map[Expression, Attribute] = equalityPredicates
+      .map { case (attr, (_, predicate)) => (predicate: Expression) -> attr 
}.toMap
+    val constants =
+      AttributeMap(equalityPredicates.map { case (attr, (lit, _)) => attr -> 
lit })
+    val conjuncts = splitConjunctivePredicates(condition)
+    // `replaceInConjunct` keeps the propagated equalities, which do not fold.
+    val isContradiction = conjuncts.exists { conjunct =>
+      fold(replaceInConjunct(conjunct, constants, _ => true, 
predicates)).contains(FalseLiteral)

Review Comment:
   Thanks. SPARK-61139 is for replacing a `FLOAT` or `DOUBLE` attribute with 
the other signed zero in `ConstantPropagation` and 
`InferFiltersFromConstraints`, so I'd leave this to it, including the check for 
a `FALSE` conjunct, which master reaches too by rewriting the conjunct with the 
same fold. The description now mentions it.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -241,18 +244,105 @@ object ConstantPropagation extends Rule[LogicalPlan] 
with PredicateHelper {
    * condition cannot raise, an expression that can fail keeps its attributes, 
unless all of them
    * are replaced and the result evaluates without an error. For the same 
reason, an operand of an
    * AND, OR, COALESCE or IN in a conjunct is rewritten only if no operand 
after it can fail, as it
-   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`. The conjuncts of
-   * `condition` itself are rewritten regardless, and [[moveEqualitiesFirst]] 
orders them.
+   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`.
+   *
+   * A conjunct decides whether the ones after it run as well, but the 
conjuncts are rewritten by
+   * the rules below instead. The conjuncts keep their written order: moving 
an equality ahead of a
+   * non-deterministic conjunct changes the rows that one sees, and a later 
rule can turn an
+   * equality into an expression that can fail, e.g. by pushing it through a 
projection that defines
+   * its attribute with a cast, which would then run on rows that the 
conjuncts written before it
+   * drop. Whether a conjunct can fail is judged on it as it is when this rule 
runs, and an equality
+   * in between is not checked (SPARK-61437). A conjunct takes the constants 
of the equalities
+   * before it, which hold on the rows that reach it where they are not NULL. 
It takes the constant
+   * of an equality after it only if every conjunct in between, as rewritten, 
can be evaluated
+   * unconditionally ([[ExprUtils.canEvaluateUnconditionally]]), which rejects 
e.g. an expression
+   * that can fail or is non-deterministic: not into `y > 0` in `y > 0 AND 10 
/ x > 0 AND y = 1` or
+   * `y > 0 AND rand() < 0.5 AND y = 1`, nor into `d <> 0` in `d <> 0 AND n / 
d > 1 AND d = 5`, with
+   * the divisions in ANSI mode. Of several equalities over the same 
attribute, only the last
+   * one is propagated, together with any earlier one identical to it; the 
others are rewritten
+   * like the other conjuncts.
+   *
+   * On a row where the attribute of an `EqualTo` is NULL, the equality is 
NULL, so the row still
+   * reaches the conjuncts after it. There, a conjunct rewritten with its 
constant, before or after
+   * it, can still decide whether a later conjunct runs, e.g. `coalesce(y, 0) 
> 0` in
+   * `(y = 1 AND coalesce(y, 0) > 0 AND 10 / x > 0) OR z > 0` or in
+   * `(coalesce(y, 0) > 0 AND y = 1 AND 10 / x > 0) OR z > 0`, or `y > w` in
+   * `(y = 1 AND y > w AND rand() < 0.5) OR z > 0`, which can be FALSE instead 
of NULL there once
+   * rewritten. A condition that becomes FALSE below can be NULL there, too. 
Without whole-stage
+   * codegen, or under an OR, the conjuncts of a filter are evaluated this 
way; with it, a filter
+   * drops a row at the first conjunct that is NULL (SPARK-61438).
+   *
+   * If a conjunct other than the propagated equalities is FALSE with the 
constants of all of them,
+   * e.g. `y = 1` in `y = 1 AND 10 / x > 0 AND y = 2`, where `y = 2` is 
propagated, the condition
+   * becomes FALSE, as a row that passes the equalities fails that conjunct, 
so none passes the
+   * condition. This also holds under a NOT, where the attributes are not 
nullable and an `EqualTo`
+   * with a NULL literal is not propagated, so the equalities are never NULL.
    */
   private def replaceConstants(
       condition: Expression,
       equalityPredicates: AttributeMap[(Literal, BinaryComparison)]): 
Expression = {
-    val constantsMap = AttributeMap(equalityPredicates.map { case (attr, (lit, 
_)) => attr -> lit })
     val predicates = equalityPredicates.values.map(_._2).toSet
+    val attributeOf: Map[Expression, Attribute] = equalityPredicates
+      .map { case (attr, (_, predicate)) => (predicate: Expression) -> attr 
}.toMap
+    val constants =
+      AttributeMap(equalityPredicates.map { case (attr, (lit, _)) => attr -> 
lit })
+    val conjuncts = splitConjunctivePredicates(condition)
+    // `replaceInConjunct` keeps the propagated equalities, which do not fold.
+    val isContradiction = conjuncts.exists { conjunct =>
+      fold(replaceInConjunct(conjunct, constants, _ => true, 
predicates)).contains(FalseLiteral)
+    }
+    if (isContradiction) {
+      FalseLiteral
+    } else {
+      val equalityAttributes = conjuncts.map(attributeOf.get)
+      // The position of the first conjunct that is the propagated equality 
over each attribute,
+      // or a copy of it. Another equality over the attribute, e.g. `1 = a` 
for `a = 1`, is not.
+      val firstEquality = equalityAttributes.zipWithIndex
+        .collect { case (Some(attr), i) => attr.exprId -> i }
+        .groupMapReduce(_._1)(_._2)(math.min)
+      // `equalitiesAfter` holds the attributes of the equalities after the 
conjunct with only
+      // conjuncts that can be evaluated unconditionally in between.
+      val (newConjuncts, _) = 
conjuncts.lazyZip(equalityAttributes).lazyZip(conjuncts.indices)
+        .foldRight((List.empty[Expression], Set.empty[ExprId])) {
+          case ((conjunct, Some(attr), _), (laterConjuncts, equalitiesAfter)) 
=>
+            (conjunct :: laterConjuncts, equalitiesAfter + attr.exprId)
+          case ((conjunct, None, i), (laterConjuncts, equalitiesAfter)) =>
+            def canTake(attr: Attribute): Boolean =
+              firstEquality.get(attr.exprId).exists(_ < i) || 
equalitiesAfter.contains(attr.exprId)
+            val newConjunct = replaceInConjunct(conjunct, constants, canTake, 
predicates)
+            val newEqualitiesAfter = if 
(ExprUtils.canEvaluateUnconditionally(newConjunct)) {
+              equalitiesAfter
+            } else {
+              Set.empty[ExprId]
+            }
+            (newConjunct :: laterConjuncts, newEqualitiesAfter)
+        }
+      val newConjunctsIterator = newConjuncts.iterator
+      def rebuild(e: Expression): Expression = e match {
+        case a @ And(left, right) =>
+          val newLeft = rebuild(left)
+          val newRight = rebuild(right)
+          if ((newLeft eq left) && (newRight eq right)) a else And(newLeft, 
newRight)
+        case _ => newConjunctsIterator.next()
+      }
+      rebuild(condition)
+    }
+  }
+
+  /**
+   * Replaces the attributes that `canTake` accepts with their values in 
`constantsMap` in the
+   * comparisons of `conjunct`, one of the conjuncts of the condition rather 
than an `And` of them,
+   * as described in [[replaceConstants]]. The equalities in `predicates` are 
kept.
+   */
+  private def replaceInConjunct(
+      conjunct: Expression,
+      constantsMap: AttributeMap[Literal],
+      canTake: Attribute => Boolean,
+      predicates: Set[BinaryComparison]): Expression = {
     def replaceInComparison(b: BinaryComparison): Expression = {
       lazy val collationSafeReplacement = isSameCollationAttrRefComparison(b)
-      def canReplace(a: Attribute): Boolean =
-        constantsMap.contains(a) && (isBinaryStable(a.dataType) || 
collationSafeReplacement)
+      def canReplace(a: Attribute): Boolean = constantsMap.contains(a) && 
canTake(a) &&
+        (isBinaryStable(a.dataType) || collationSafeReplacement)
       def replace(e: Expression): Expression = e match {
         case a: AttributeReference if canReplace(a) => constantsMap(a)

Review Comment:
   Thanks, confirmed both, on master and with this PR, with ANSI mode on: `(y = 
5 AND y > 10 / x) OR b > 0` over `(NULL, 0, 0), (5, 1, 0)` fails with 
`DIVIDE_BY_ZERO` and returns no row without the rule, and so does the top-level 
`y > 10 / x AND y = 5` over `(NULL, 0), (5, 1)` with the default settings and 
whole-stage codegen on, as you traced; with it off, it returns no row. As both 
happen on the rows where the equality is NULL, I added them under SPARK-61438 
in the Scaladoc and the description, and qualified the sentence at L243 with 
those rows.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -241,18 +244,105 @@ object ConstantPropagation extends Rule[LogicalPlan] 
with PredicateHelper {
    * condition cannot raise, an expression that can fail keeps its attributes, 
unless all of them
    * are replaced and the result evaluates without an error. For the same 
reason, an operand of an
    * AND, OR, COALESCE or IN in a conjunct is rewritten only if no operand 
after it can fail, as it
-   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`. The conjuncts of
-   * `condition` itself are rewritten regardless, and [[moveEqualitiesFirst]] 
orders them.
+   * decides whether they run, e.g. `y < 3` in `(y < 3 OR 10 / x > 1) AND y = 
5`.
+   *
+   * A conjunct decides whether the ones after it run as well, but the 
conjuncts are rewritten by
+   * the rules below instead. The conjuncts keep their written order: moving 
an equality ahead of a
+   * non-deterministic conjunct changes the rows that one sees, and a later 
rule can turn an
+   * equality into an expression that can fail, e.g. by pushing it through a 
projection that defines
+   * its attribute with a cast, which would then run on rows that the 
conjuncts written before it
+   * drop. Whether a conjunct can fail is judged on it as it is when this rule 
runs, and an equality
+   * in between is not checked (SPARK-61437). A conjunct takes the constants 
of the equalities
+   * before it, which hold on the rows that reach it where they are not NULL. 
It takes the constant
+   * of an equality after it only if every conjunct in between, as rewritten, 
can be evaluated
+   * unconditionally ([[ExprUtils.canEvaluateUnconditionally]]), which rejects 
e.g. an expression
+   * that can fail or is non-deterministic: not into `y > 0` in `y > 0 AND 10 
/ x > 0 AND y = 1` or
+   * `y > 0 AND rand() < 0.5 AND y = 1`, nor into `d <> 0` in `d <> 0 AND n / 
d > 1 AND d = 5`, with
+   * the divisions in ANSI mode. Of several equalities over the same 
attribute, only the last
+   * one is propagated, together with any earlier one identical to it; the 
others are rewritten
+   * like the other conjuncts.
+   *
+   * On a row where the attribute of an `EqualTo` is NULL, the equality is 
NULL, so the row still
+   * reaches the conjuncts after it. There, a conjunct rewritten with its 
constant, before or after
+   * it, can still decide whether a later conjunct runs, e.g. `coalesce(y, 0) 
> 0` in
+   * `(y = 1 AND coalesce(y, 0) > 0 AND 10 / x > 0) OR z > 0` or in
+   * `(coalesce(y, 0) > 0 AND y = 1 AND 10 / x > 0) OR z > 0`, or `y > w` in
+   * `(y = 1 AND y > w AND rand() < 0.5) OR z > 0`, which can be FALSE instead 
of NULL there once
+   * rewritten. A condition that becomes FALSE below can be NULL there, too. 
Without whole-stage
+   * codegen, or under an OR, the conjuncts of a filter are evaluated this 
way; with it, a filter
+   * drops a row at the first conjunct that is NULL (SPARK-61438).
+   *
+   * If a conjunct other than the propagated equalities is FALSE with the 
constants of all of them,
+   * e.g. `y = 1` in `y = 1 AND 10 / x > 0 AND y = 2`, where `y = 2` is 
propagated, the condition
+   * becomes FALSE, as a row that passes the equalities fails that conjunct, 
so none passes the
+   * condition. This also holds under a NOT, where the attributes are not 
nullable and an `EqualTo`
+   * with a NULL literal is not propagated, so the equalities are never NULL.
    */
   private def replaceConstants(
       condition: Expression,
       equalityPredicates: AttributeMap[(Literal, BinaryComparison)]): 
Expression = {
-    val constantsMap = AttributeMap(equalityPredicates.map { case (attr, (lit, 
_)) => attr -> lit })
     val predicates = equalityPredicates.values.map(_._2).toSet
+    val attributeOf: Map[Expression, Attribute] = equalityPredicates
+      .map { case (attr, (_, predicate)) => (predicate: Expression) -> attr 
}.toMap
+    val constants =
+      AttributeMap(equalityPredicates.map { case (attr, (lit, _)) => attr -> 
lit })
+    val conjuncts = splitConjunctivePredicates(condition)
+    // `replaceInConjunct` keeps the propagated equalities, which do not fold.
+    val isContradiction = conjuncts.exists { conjunct =>
+      fold(replaceInConjunct(conjunct, constants, _ => true, 
predicates)).contains(FalseLiteral)
+    }
+    if (isContradiction) {
+      FalseLiteral
+    } else {
+      val equalityAttributes = conjuncts.map(attributeOf.get)
+      // The position of the first conjunct that is the propagated equality 
over each attribute,
+      // or a copy of it. Another equality over the attribute, e.g. `1 = a` 
for `a = 1`, is not.
+      val firstEquality = equalityAttributes.zipWithIndex

Review Comment:
   Done: an `EqualTo` or `EqualNullSafe` between the attribute and its 
constant, on either side and matched by `exprId`, so also with another 
qualifier, counts as an equality. So `c > a` in `1 = a AND c > a AND rand() < 
0.5 AND a = 1` becomes `c > 1`. Such an equality is kept when the propagated 
one is in a later segment, and folds away as on master in the segment of the 
propagated one. The test covers `1 = a`, `a <=> 1`, a copy of `a = 1` and one 
with another qualifier.
   



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