cloud-fan commented on a change in pull request #27212: [SPARK-30515][SQL] 
Refactor SimplifyBinaryComparison to reduce the time complexity
URL: https://github.com/apache/spark/pull/27212#discussion_r366713813
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
 ##########
 @@ -410,37 +410,41 @@ object SimplifyBinaryComparison
   extends Rule[LogicalPlan] with PredicateHelper with ConstraintHelper {
 
   private def canSimplifyComparison(
-      plan: LogicalPlan, left: Expression, right: Expression): Boolean = {
+      left: Expression,
+      right: Expression,
+      getNotNullExpressions: () => ExpressionSet): Boolean = {
     if (left.semanticEquals(right)) {
-      if (!left.nullable && !right.nullable) {
-        true
-      } else {
-        // We do more checks for non-nullable cases
-        plan match {
-          case Filter(fc, _) =>
-            splitConjunctivePredicates(fc).exists { condition =>
-              condition.semanticEquals(IsNotNull(left))
-            }
-          case _ => false
-        }
-      }
+      (!left.nullable && !right.nullable) || 
getNotNullExpressions().contains(left)
     } else {
       false
     }
   }
 
   def apply(plan: LogicalPlan): LogicalPlan = plan transform {
-    case l: LogicalPlan => l transformExpressionsUp {
-      // True with equality
-      case a EqualNullSafe b if a.semanticEquals(b) => TrueLiteral
-      case a EqualTo b if canSimplifyComparison(l, a, b) => TrueLiteral
-      case a GreaterThanOrEqual b if canSimplifyComparison(l, a, b) => 
TrueLiteral
-      case a LessThanOrEqual b if canSimplifyComparison(l, a, b) => TrueLiteral
-
-      // False with inequality
-      case a GreaterThan b if canSimplifyComparison(l, a, b) => FalseLiteral
-      case a LessThan b if canSimplifyComparison(l, a, b) => FalseLiteral
-    }
+    case l: LogicalPlan =>
+      lazy val notNullExpressions = ExpressionSet(l match {
+        case Filter(fc, _) =>
+          splitConjunctivePredicates(fc).collect {
+            case i: IsNotNull => i.child
 
 Review comment:
   nit: `i @ IsNotNull(_: Attribute)`, to reduce the scope.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to