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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -426,6 +426,55 @@ object CharVarcharUtils extends Logging with 
SparkCharVarcharUtils {
     }
   }
 
+  /**
+   * Pads the given attributes, the outputs of the children of a set operation 
at one position, so
+   * that CHAR columns and fields of different lengths at the same place all 
get the widest length.
+   * Returns the raw type with the widest lengths, which keeps the names of 
the first attribute's
+   * fields, and an expression for each attribute: the padded value, or the 
attribute if it already
+   * has the widest lengths. Returns None if the raw types are already the 
same, or if they are not
+   * all CHAR types, or structs and arrays of them, at the same places.
+   */
+  def padToWiderCharLength(attrs: Seq[Attribute]): Option[(DataType, 
Seq[Expression])] = {
+    val rawTypes = attrs.map(attr => getRawType(attr.metadata))
+    if (rawTypes.exists(_.isEmpty) || rawTypes.distinct.length == 1) {
+      None
+    } else {
+      rawTypes.map(_.get).map(Option(_)).reduce { (left, right) =>
+        for (l <- left; r <- right; wider <- widerCharType(l, r)) yield wider
+      }.map { target =>
+        val padded = attrs.zip(rawTypes.map(_.get)).map { case (attr, rawType) 
=>
+          padCharToTargetLength(attr, rawType, target, alwaysPad = 
false).getOrElse(attr)

Review Comment:
   Fixed: the union padding now uses `readSidePadding` through 
`processStringForCharVarchar`, as the scan does, instead of `StringRPad`. New 
test `SPARK-61972: a union of char columns does not truncate over length 
values` in `FileSourceCharVarcharTestSuite` (an external CHAR(2) table over 
'123456' unioned with CHAR(5)).



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