sarutak commented on code in PR #60337:
URL: https://github.com/apache/spark/pull/60337#discussion_r4543198204
##########
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 =>
Review Comment:
Done, both: each conjunct is rewritten with all the constants once, which
the check uses, and a conjunct that takes the constants of all the attributes
it refers to reuses that result; a conjunct without a propagated attribute is
not rewritten.
##########
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)) {
Review Comment:
That loop is gone: the conjuncts are now grouped at the non-deterministic
ones, and `moveEqualitiesFirst` runs per group, as on master, but returns right
away for a group without an equality, so `canEvaluateUnconditionally` does not
run there.
##########
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
Review Comment:
`attributeOf` and `firstEquality` are gone with the new grouping, and
`predicates` is the set of the propagated equalities, which `replaceInConjunct`
now takes as a `Set[Expression]`.
--
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]