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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -158,9 +180,62 @@ 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 of an IN subquery and the CHAR column of the 
subquery it is compared
+   * with, as a comparison of two columns pads them. The columns are padded in 
a Project over the
+   * subquery, which gives them the raw type of the padded length.
+   */
+  private def padInSubquery(i: InSubquery, padCharCol: Boolean): InSubquery = {
+    val padded = i.values.zip(i.query.childOutputs).map {
+      case (value @ CharOperand(metadata), col) =>
+        val valueType = CharVarcharUtils.getRawType(metadata)
+        val colType = CharVarcharUtils.getRawType(col.metadata)
+        val Seq(newValue, newCol) = 
CharVarcharUtils.addPaddingInStringComparison(
+          Seq(value, col), Seq(valueType, colType), padCharCol)
+        val target = for {
+          v <- valueType
+          c <- colType
+          wider <- CharVarcharUtils.widerCharType(c, v)
+        } yield wider
+        (newValue, Some(newCol).filterNot(_ eq col).map((_, target)))
+      case (value, _) => (value, None)

Review Comment:
   Fixed. `padInSubquery` now pads a string literal value and the subquery's 
CHAR column it is compared with the way `padAttrLitCmp` pads a literal compared 
with a column: the literal to the column's length, or the column to the 
literal's length. As with the other subquery cases, this gap predates the PR: 
`SELECT 1 WHERE 'a' IN (SELECT c FROM t5)` returns nothing on master. The 
padded subquery columns no longer carry a raw type, so they are padded only 
once, also when predicates pad CHAR columns. New checks: `'a' IN (<union>)`, 
`'a' IN (SELECT c FROM t5)`, `'abcdef' IN (SELECT c FROM t5)` (no match), and 
`'a' IN (SELECT c FROM t5)` without read-side padding.



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