kazuyukitanimura commented on a change in pull request #33930:
URL: https://github.com/apache/spark/pull/33930#discussion_r705598219
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -290,6 +290,21 @@ object OptimizeIn extends Rule[LogicalPlan] {
* 4. Removes `Not` operator.
*/
object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
+ // Given argument x, return true if expression Not(x) can be simplified
+ // E.g. when x == Not(y), Not(x) == Not(Not(y)) == y
+ // For the case of x = EqualTo(a, b), recursively check each child expression
+ // Extra nullable check is required for EqualNullSafe because
+ // Not(EqualNullSafe(x, null)) is different from EqualNullSafe(x, Not(null))
+ private def canSimplifyNot(x: Expression): Boolean = x match {
+ case Literal(_, BooleanType) | Literal(_, NullType) => true
+ case _: Not | _: IsNull | _: IsNotNull | _: And | _: Or => true
+ case _: GreaterThan | _: GreaterThanOrEqual | _: LessThan | _:
LessThanOrEqual => true
+ case EqualTo(a, b) if canSimplifyNot(a) || canSimplifyNot(b) => true
Review comment:
Actually the code does not work in the way you described.
It works like this.
`Not(EqualTo(a, EqualTo(b, EqualTo(c, d))))` where `d` is optimizable, first
`canSimplifyNot(a)` or `canSimplifyNot(EqualTo(b, EqualTo(c, d)))` are called
depending on the case matches here
https://github.com/apache/spark/blob/f5d3984c04029bdb7f4f00ef4de25a9e9fbc75cb/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala#L472-L473
If `a` happens to be optimizable, `canSimplifyNot(a)` returns `true` and it
propagates `Not` to `a`. That is the second case in the above two case match.
That is fine as `canSimplifyNot(a)=true` promises that `Not(a)` will be
optimized for sure.
Otherwise, `canSimplifyNot(EqualTo(b, EqualTo(c, d)))` will call
`canSimplifyNot(b)` and `canSimplifyNot(EqualTo(c, d))`, then that will call
`canSimplifyNot(c)` and `canSimplifyNot(d)`.
Since `d` is optimizable in this example, `canSimplifyNot(d)` returns `true`
that makes `canSimplifyNot(EqualTo(c, d))` return `true`. Eventually
`canSimplifyNot(EqualTo(b, EqualTo(c, d)))` returns `true`
In conclusion, that will match the first one out of the above two cases (not
the second one), i.e.
https://github.com/apache/spark/blob/f5d3984c04029bdb7f4f00ef4de25a9e9fbc75cb/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala#L472
After all it will optimize `Not(EqualTo(a, EqualTo(b, EqualTo(c, d))))` =>
`EqualTo(a, Not(EqualTo(b, EqualTo(c, d))))` => `EqualTo(a, EqualTo(b,
Not(EqualTo(c, d))))` => `EqualTo(a, EqualTo(b, EqualTo(c, Not(d))))` =>
`EqualTo(a, EqualTo(b, EqualTo(c, e)))` where `e` is optimized `Not(d)`
--
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]