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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/ApplyCharTypePadding.scala:
##########
@@ -88,13 +88,47 @@ object ApplyCharTypePadding extends Rule[LogicalPlan] {
           })
       }, skipCond = p => p.analyzed || p.isInstanceOf[DeleteFromTable], 
canGetOutput = _.resolved)
       padRowLevelConditions(padDeleteConditions(
-        ApplyCharTypePaddingHelper.paddingForStringComparison(newPlan, 
padCharCol = false)))
+        ApplyCharTypePaddingHelper.paddingForStringComparison(
+          padUnionChildren(newPlan), padCharCol = false)))
     } else {
       ApplyCharTypePaddingHelper.paddingForStringComparison(
-        plan, padCharCol = 
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
+        padUnionChildren(plan),
+        padCharCol = 
!conf.getConf(SQLConf.LEGACY_NO_CHAR_PADDING_IN_PREDICATE))
     }
   }
 
+  /**
+   * When the children of a Union have CHAR columns or fields of different 
lengths at the same
+   * position, the output of the Union has the widest length, see 
[[Union.mergeChildOutputs]], as
+   * the SQL standard does for the union of CHAR(n) and CHAR(m). This pads the 
values of the
+   * shorter children to that length, so that comparisons padded to it match 
them.
+   */
+  private def padUnionChildren(plan: LogicalPlan): LogicalPlan = {
+    plan.transformUpWithNewOutput({
+      // An unresolved Union, or one with children of different arity, fails 
in CheckAnalysis.
+      case u: Union if u.resolved =>
+        val rawTypes = 
u.children.map(_.output).transpose.map(CharVarcharUtils.widerCharRawType)
+        if (rawTypes.forall(_.isEmpty)) {
+          u -> Nil
+        } else {
+          val newChildren = u.children.map { child =>
+            val projectList = child.output.zip(rawTypes).map {
+              case (attr, Some(rawType)) =>
+                CharVarcharUtils.padCharToWiderLength(attr, rawType).map {

Review Comment:
   Fixed. The cause was older than this PR. Spark never padded CHAR in an IN 
subquery or in INTERSECT/EXCEPT, so `SELECT id FROM t3 WHERE c IN (SELECT c 
FROM t5)` and `SELECT c FROM t3 INTERSECT SELECT c FROM t5` already returned 
nothing on master. The union only changed which rows were missed. Now:
   - `paddingForStringComparison` pads the shorter of each IN value and the 
matching subquery column. The column is padded in a `Project` over the 
subquery, without truncating.
   - `Intersect`/`Except` outputs take the widest CHAR length, and 
`ApplyCharTypePadding` pads their shorter child as it does for `Union`.
   
   Your examples are in `SPARK-61972: set operations and IN subqueries of char 
columns of different lengths`: IN against the union returns 1, the INTERSECT 
returns `'a    '` as CHAR(5), and the EXCEPT is empty. The test also covers the 
plain t3/t5 cases, NOT IN, and a multi-column IN.



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