cloud-fan commented on a change in pull request #33930:
URL: https://github.com/apache/spark/pull/33930#discussion_r705130603



##########
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:
       I'm not sure we should recursive into `EqualTo`. For example:
   `Not(EqualTo(a, EqualTo(b, EqualTo(c, d))))`, if `d` is optimizable, we will 
turn it into `EqualTo(Not(a), EqualTo(Not(b), EqualTo(Not(c), optimized_d)))`. 
We need to evaluate `Not` more times which is a perf degradation.




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