maropu commented on a change in pull request #27008: [SPARK-30353][SQL] Add 
IsNotNull check in SimplifyBinaryComparison optimization
URL: https://github.com/apache/spark/pull/27008#discussion_r361783206
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
 ##########
 @@ -384,22 +384,40 @@ object BooleanSimplification extends Rule[LogicalPlan] 
with PredicateHelper {
 /**
  * Simplifies binary comparisons with semantically-equal expressions:
  * 1) Replace '<=>' with 'true' literal.
- * 2) Replace '=', '<=', and '>=' with 'true' literal if both operands are 
non-nullable.
- * 3) Replace '<' and '>' with 'false' literal if both operands are 
non-nullable.
+ * 2) Replace '=', '<=', and '>=' with 'true' literal
+ *    if both operands are non-nullable or exist IsNotNull.
+ * 3) Replace '<' and '>' with 'false' literal if both operands are 
non-nullable or exist IsNotNull.
  */
-object SimplifyBinaryComparison extends Rule[LogicalPlan] with PredicateHelper 
{
+object SimplifyBinaryComparison
+  extends Rule[LogicalPlan] with PredicateHelper with ConstraintHelper {
+
+  private def canSimplifyComparison(
+      plan: LogicalPlan, left: Expression, right: Expression): Boolean = {
+    if (!left.nullable && !right.nullable && left.semanticEquals(right)) {
+      return true
+    }
+
+    plan match {
+      case Filter(fc, _) =>
+        splitConjunctivePredicates(fc).exists { condition =>
+          condition.semanticEquals(IsNotNull(left)) && 
condition.semanticEquals(IsNotNull(right))
+        }
+
+      case _ => false
+    }
 
 Review comment:
   How about this?
   ```
     private def canSimplifyComparison(
         plan: LogicalPlan, left: Expression, right: Expression): Boolean = {
       if (!left.nullable && !right.nullable && left.semanticEquals(right)) {
         true
       } else {
         // We do more checks for non-nullable cases
         plan match {
           case Filter(fc, _) =>
             splitConjunctivePredicates(fc).exists { condition =>
               condition.semanticEquals(IsNotNull(left)) && 
condition.semanticEquals(IsNotNull(right))
             }
           case _ =>
             false
         }
       }
     }
   ```

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