cloud-fan commented on a change in pull request #32488:
URL: https://github.com/apache/spark/pull/32488#discussion_r643950742



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparison.scala
##########
@@ -121,6 +134,91 @@ object UnwrapCastInBinaryComparison extends 
Rule[LogicalPlan] {
         if canImplicitlyCast(fromExp, toType, literalType) =>
       simplifyNumericComparison(be, fromExp, toType, value)
 
+    // As the analyzer makes sure that the list of In is already of the same 
data type, then the
+    // rule can simply check the first literal in `in.list` can implicitly 
cast to `toType` or not,
+    // and note that:
+    // 1. this rule doesn't convert in when `in.list` is empty or `in.list` 
contains only null
+    // values.
+    // 2. this rule only handles the case when both `fromExp` and value in 
`in.list` are of numeric
+    // type.
+    case in @ In(Cast(fromExp, toType: NumericType, _), list @ Seq(firstLit, 
_*))
+      if canImplicitlyCast(fromExp, toType, firstLit.dataType) =>
+
+      // There are 3 kinds of literals in the list:
+      // 1. null literals
+      // 2. The literals that can cast to fromExp.dataType
+      // 3. The literals that cannot cast to fromExp.dataType
+      // null literals is special as we can cast null literals to any data 
type.
+      val (nullList, canCastList, cannotCastList) =
+        (ArrayBuffer[Literal](), ArrayBuffer[Literal](), 
ArrayBuffer[Expression]())
+      list.foreach {
+        case lit @ Literal(null, _) => nullList += lit
+        case lit @ NonNullLiteral(_, _) =>
+          unwrapCast(EqualTo(in.value, lit)) match {
+            case EqualTo(_, unwrapLit: Literal) => canCastList += unwrapLit
+            case e @ And(IsNull(_), Literal(null, BooleanType)) => 
cannotCastList += e
+            case _ => throw new IllegalStateException("Illegal unwrap cast 
result found.")
+          }
+        case _ => throw new IllegalStateException("Illegal value found in 
in.list.")
+      }
+
+      // return original expression when in.list contains only null values.
+      if (canCastList.isEmpty && cannotCastList.isEmpty) {
+        exp
+      } else {
+        // cast null value to fromExp.dataType, to make sure the new return 
list is in the same data
+        // type.
+        val newList = nullList.map(lit => Cast(lit, fromExp.dataType)) ++ 
canCastList
+        val unwrapIn = In(fromExp, newList.toSeq)
+        cannotCastList.headOption match {
+          case None => unwrapIn
+          // since `cannotCastList` are all the same,
+          // convert to a single value `And(IsNull(_), Literal(null, 
BooleanType))`.
+          case Some(falseIfNotNull @ And(IsNull(_), Literal(null, 
BooleanType)))
+              if cannotCastList.map(_.canonicalized).distinct.length == 1 =>
+            Or(falseIfNotNull, unwrapIn)
+          case _ => exp
+        }
+      }
+
+    // The same with `In` expression, the analyzer makes sure that the hset of 
InSet is already of

Review comment:
       In -> InSet




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



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

Reply via email to