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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -158,9 +180,61 @@ object ApplyCharTypePaddingHelper {
           i.copy(value = newChildren.head, list = newChildren.tail)
         }
 
+      // Like the cases above, for a scalar subquery of a CHAR column compared 
with a string
+      // literal, a column, or another scalar subquery.
+      case b @ BinaryComparison(e @ CharScalarSubquery(metadata), lit) if 
lit.foldable =>
+        padAttrLitCmp(e, metadata, padCharCol, 
lit).map(b.withNewChildren).getOrElse(b)
+
+      case b @ BinaryComparison(lit, e @ CharScalarSubquery(metadata)) if 
lit.foldable =>
+        padAttrLitCmp(e, metadata, padCharCol, lit)
+          .map(newChildren => b.withNewChildren(newChildren.reverse))
+          .getOrElse(b)
+
+      case b @ BinaryComparison(e1 @ CharOperand(m1), e2 @ CharOperand(m2))
+          if e1.isInstanceOf[ScalarSubquery] || 
e2.isInstanceOf[ScalarSubquery] =>
+        b.withNewChildren(CharVarcharUtils.addPaddingInStringComparison(
+          Seq(e1, e2), Seq(m1, m2).map(CharVarcharUtils.getRawType), 
padCharCol))
+
+      case i: InSubquery if i.resolved => padInSubquery(i, padCharCol)
+
       case other => other
     }
 
+  /**
+   * Pads each CHAR value or string literal of an IN subquery and the CHAR 
column of the subquery
+   * it is compared with, as a comparison with a column pads them. The columns 
are padded in a
+   * Project over the subquery, without the raw type, so that they are padded 
only once.
+   */
+  private def padInSubquery(i: InSubquery, padCharCol: Boolean): InSubquery = {
+    val padded = i.values.zip(i.query.childOutputs).map {
+      case (value @ CharOperand(metadata), col) =>
+        val Seq(newValue, newCol) = 
CharVarcharUtils.addPaddingInStringComparison(

Review Comment:
   Fixed. The IN-subquery and scalar-subquery paths pad with `readSidePadding` 
(`addPaddingInStringComparison(..., truncate = false)`, and the same for the 
literal path), which keeps values longer than the target. `SPARK-61972: a union 
of char columns does not truncate over length values` now also has external 
CHAR(3) and CHAR(5) tables over 'abcdef': IN in both directions and `=` against 
a scalar subquery return 'abcdef', and `'abcde' IN (SELECT c FROM e3)` and 
`(SELECT c FROM e3) = 'abcde'` don't match. With `rpad` they would have matched 
after truncation.



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