shivadarshan-devadiga commented on code in PR #60750:
URL: https://github.com/apache/spark/pull/60750#discussion_r4651092791


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -84,46 +120,41 @@ object ApplyCharTypePaddingHelper {
     expression match {
       // String literal is treated as char type when it's compared to a char 
type column.
       // We should pad the shorter one to the longer length.
-      case b @ BinaryComparison(e @ AttrOrOuterRef(attr), lit) if lit.foldable 
=>
-        padAttrLitCmp(e, attr.metadata, padCharCol, lit)
+      case b @ BinaryComparison(e @ RawTypedRef(c: CharType), lit) if 
isStringConstant(lit) =>
+        padAttrLitCmp(e, c, padCharCol, lit)
           .map { newChildren =>
             b.withNewChildren(newChildren)
           }
           .getOrElse(b)
 
-      case b @ BinaryComparison(lit, e @ AttrOrOuterRef(attr)) if lit.foldable 
=>
-        padAttrLitCmp(e, attr.metadata, padCharCol, lit)
+      case b @ BinaryComparison(lit, e @ RawTypedRef(c: CharType)) if 
isStringConstant(lit) =>
+        padAttrLitCmp(e, c, padCharCol, lit)
           .map { newChildren =>
             b.withNewChildren(newChildren.reverse)
           }
           .getOrElse(b)
 
-      case i @ In(e @ AttrOrOuterRef(attr), list)
-          if i.resolved && attr.dataType.isInstanceOf[StringType] && 
list.forall(_.foldable) =>
-        CharVarcharUtils
-          .getRawType(attr.metadata)
-          .flatMap {
-            case c: CharType =>
-              // Compute the length of every list element in place, so that 
each length stays
-              // aligned with the element it came from. NULL elements have no 
length: they can
-              // never match, so they are left untouched instead of being 
padded.
-              val literalCharLengths = list.map { lit =>
-                Option(lit.eval().asInstanceOf[UTF8String]).map(_.numChars())
-              }
-              val targetLen = (c.length +: literalCharLengths.flatten).max
-              Some(
-                i.copy(
-                  value = addPadding(e, c.length, targetLen, alwaysPad = 
padCharCol),
-                  list = list.zip(literalCharLengths).map {
-                    case (lit, Some(charLength)) =>
-                      addPadding(lit, charLength, targetLen, alwaysPad = false)
-                    case (lit, None) => lit
-                  }
-                )
-              )
-            case _ => None
-          }
-          .getOrElse(i)
+      case i @ In(e @ RawTypedRef(c: CharType), list)
+          if i.resolved && e.dataType.isInstanceOf[StringType] && 
list.forall(_.foldable) =>
+        val values = list.map(tryEvalString)
+        if (values.exists(_.isEmpty)) {

Review Comment:
   Agreed, padding per element is closer to `ConstantFolding`, so I changed it. 
The elements that evaluate are padded, the target length comes from them, and 
an element that fails is left as it is. Your `CASE WHEN id = 1 THEN s.c IN 
('a', CAST(1 / 0 AS STRING)) END` example is in the test and returns true.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -84,46 +120,41 @@ object ApplyCharTypePaddingHelper {
     expression match {
       // String literal is treated as char type when it's compared to a char 
type column.
       // We should pad the shorter one to the longer length.
-      case b @ BinaryComparison(e @ AttrOrOuterRef(attr), lit) if lit.foldable 
=>
-        padAttrLitCmp(e, attr.metadata, padCharCol, lit)
+      case b @ BinaryComparison(e @ RawTypedRef(c: CharType), lit) if 
isStringConstant(lit) =>

Review Comment:
   Done, with your restructuring for both orders. The guard is checked first, 
and the raw type is parsed only when the other side is a string constant.



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

To unsubscribe, e-mail: [email protected]

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