kazuyukitanimura commented on a change in pull request #33930:
URL: https://github.com/apache/spark/pull/33930#discussion_r719870007



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
##########
@@ -441,6 +457,27 @@ object BooleanSimplification extends Rule[LogicalPlan] 
with PredicateHelper {
 
       case Not(IsNull(e)) => IsNotNull(e)
       case Not(IsNotNull(e)) => IsNull(e)
+
+      // Move `Not` from one side of `EqualTo`/`EqualNullSafe` to the other 
side if it's beneficial.
+      // E.g. `EqualTo(Not(a), b)` where `b = Not(c)`, it will become
+      // `EqualTo(a, Not(b))` => `EqualTo(a, Not(Not(c)))` => `EqualTo(a, c)`
+      // In addition, `if canSimplifyNot(b)` checks if the optimization can 
converge
+      // that avoids the situation two conditions are returning to each other.
+      case EqualTo(Not(a), b) if !canSimplifyNot(a) && canSimplifyNot(b) => 
EqualTo(a, Not(b))

Review comment:
       I addressed this by using `q.transformExpressionsDownWithPruning()` that 
optimizes from top to bottom. That is helpful for the case like in the test `'e 
=!= ('f === ('g === Not('h)))` It will be optimized as
   ```
   'e =!= ('f === ('g === Not('h))) => 'e === ('f =!= ('g === Not('h))) => 'e 
=== ('f === ('g =!= Not('h))) => 'e === ('f === ('g === Not(Not('h)))) => 'e 
=== ('f === ('g === 'h)) 
   ```
   Only the last `Not(Not('h))` will need to re-visit `BooleanSimplification` 
one more time. Otherwise it will stay the fist `Not` propagation is done within 
`NotPropagation`




-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to