wangyum commented on a change in pull request #27252: [SPARK-29231][SQL] 
Constraints should be inferred from cast equality constraint
URL: https://github.com/apache/spark/pull/27252#discussion_r378248376
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/QueryPlanConstraints.scala
 ##########
 @@ -62,11 +62,17 @@ trait ConstraintHelper {
    */
   def inferAdditionalConstraints(constraints: Set[Expression]): 
Set[Expression] = {
     var inferredConstraints = Set.empty[Expression]
-    constraints.foreach {
+    // IsNotNull should be constructed by `constructIsNotNullConstraints`.
+    val predicates = constraints.filterNot(_.isInstanceOf[IsNotNull])
+    predicates.foreach {
       case eq @ EqualTo(l: Attribute, r: Attribute) =>
-        val candidateConstraints = constraints - eq
+        val candidateConstraints = predicates - eq
         inferredConstraints ++= replaceConstraints(candidateConstraints, l, r)
         inferredConstraints ++= replaceConstraints(candidateConstraints, r, l)
+      case eq @ EqualTo(l @ Cast(_: Attribute, _, _), r: Attribute) =>
+        inferredConstraints ++= replaceConstraints(predicates - eq, r, l)
 
 Review comment:
   @cloud-fan This PR support `cast(1, dt) = b` before: 
   
https://github.com/apache/spark/compare/048a0ecc65763c6feaa939938e2dec6f4040d939..7dcfe915087dbe274b470928600197745a645f5e
   
   I removed it because:
   1. It may be broken the plan. [This 
is](https://github.com/apache/spark/compare/048a0ecc65763c6feaa939938e2dec6f4040d939..7dcfe915087dbe274b470928600197745a645f5e#diff-b40fcb6ac9b2e94b410f39a94a97e822R91-R93)
 how I handled it before.
   2. For `cast(a, dt) = b`, we support inferring many predicates, for example: 
`a > 1`, `a < 1`, `a in (2, 3)`. I'm not sure if it's safe.
   
   How about only supporting `cast(a, dt) = 1` now?
   
   @peter-toth I'd like to support these cases in 
https://github.com/apache/spark/pull/27518:
   ```
   a < b && b < c infer a < c
   a < b && b <= c infer a < c
   a < b && b = c infer a < c
   ...
   ```

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