wangyum commented on a change in pull request #27518:
URL: https://github.com/apache/spark/pull/27518#discussion_r484167230



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/QueryPlanConstraints.scala
##########
@@ -78,6 +91,72 @@ trait ConstraintHelper {
     inferredConstraints -- constraints
   }
 
+  /**
+   * Infers an additional set of constraints from a given set of inequality 
constraints.
+   * For e.g., if an operator has constraints of the form (`a > b`, `b > 5`), 
this returns an
+   * additional constraint of the form `a > 5`.
+   */
+  def inferInequalityConstraints(constraints: Set[Expression]): 
Set[Expression] = {
+    val binaryComparisons = constraints.filter {
+      case _: GreaterThan => true
+      case _: GreaterThanOrEqual => true
+      case _: LessThan => true
+      case _: LessThanOrEqual => true
+      case _: EqualTo => true
+      case _ => false
+    }
+
+    val greaterThans = binaryComparisons.map {
+      case EqualTo(l, r) if l.foldable => EqualTo(r, l)
+      case LessThan(l, r) => GreaterThan(r, l)
+      case LessThanOrEqual(l, r) => GreaterThanOrEqual(r, l)
+      case other => other
+    }
+
+    val lessThans = binaryComparisons.map {
+      case EqualTo(l, r) if l.foldable => EqualTo(r, l)
+      case GreaterThan(l, r) => LessThan(r, l)
+      case GreaterThanOrEqual(l, r) => LessThanOrEqual(r, l)
+      case other => other
+    }
+
+    var inferredConstraints = Set.empty[Expression]
+    greaterThans.foreach {
+      case op @ BinaryComparison(source: Attribute, destination: Expression)
+        if destination.foldable =>

Review comment:
       To avoid generating too many constraints. For example: `a > b > c > 1`. 
The expected inferred constraints are: `a > 1 and b > 1`. ` a > c` is useless.
   
   




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