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


##########
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 {
+                  case (padded, paddedRawType) =>
+                    Alias(padded, attr.name)(explicitMetadata =

Review Comment:
   Fixed: the padding aliases keep the qualifier of the attribute they replace, 
in the set-operation children and in IN subqueries. 
`spark.table("t3").union(spark.table("t5")).select("t3.c")` is now in the 
set-operations test.



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