viirya commented on a change in pull request #27008: [SPARK-30353][SQL] Use 
constraints in SimplifyBinaryComparison optimization
URL: https://github.com/apache/spark/pull/27008#discussion_r361519277
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala
 ##########
 @@ -384,22 +384,49 @@ 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 in constraints.
+ * 3) Replace '<' and '>' with 'false' literal if both operands are 
non-nullable or in constraints.
  */
-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 = {
+    def canSimplify(left: Expression, right: Expression): Boolean = {
+      !left.nullable && !right.nullable && left.semanticEquals(right)
+    }
+
+    if (canSimplify(left, right)) {
+      return true
+    }
+
+    if (SQLConf.get.constraintPropagationEnabled) {
+      plan match {
+        case f @ Filter(_, _) =>
+          f.constraints.exists {
+            case IsNotNull(e) => e.semanticEquals(left) && 
e.semanticEquals(right)
+            case _ => false
+          }
+
+        case _ => false
+      }
+    } else {
+      false
+    }
+  }
+
   def apply(plan: LogicalPlan): LogicalPlan = plan transform {
-    case q: LogicalPlan => q transformExpressionsUp {
+    case l: LogicalPlan => l transformExpressionsUp {
       // True with equality
       case a EqualNullSafe b if a.semanticEquals(b) => TrueLiteral
-      case a EqualTo b if !a.nullable && !b.nullable && a.semanticEquals(b) => 
TrueLiteral
-      case a GreaterThanOrEqual b if !a.nullable && !b.nullable && 
a.semanticEquals(b) =>
-        TrueLiteral
-      case a LessThanOrEqual b if !a.nullable && !b.nullable && 
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 !a.nullable && !b.nullable && 
a.semanticEquals(b) => FalseLiteral
-      case a LessThan b if !a.nullable && !b.nullable && a.semanticEquals(b) 
=> FalseLiteral
+      case a GreaterThan b if canSimplifyComparison(l, a, b) => FalseLiteral
 
 Review comment:
   `IsNotNull(e) && e > e` => `IsNotNull(e) && false` => 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