shivadarshan-devadiga commented on code in PR #60756:
URL: https://github.com/apache/spark/pull/60756#discussion_r4643935348
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -426,6 +426,65 @@ object CharVarcharUtils extends Logging with
SparkCharVarcharUtils {
}
}
+ /**
+ * The raw type of the output of a UNION at one position, given the outputs
of its children
+ * there, if they are CHAR types, or structs and arrays of them, with
different lengths at the
+ * same places: the widest lengths, as the SQL standard gives the union of
CHAR(n) and CHAR(m)
+ * the length max(n, m). Keeps the names of the first attribute's fields.
Returns None if the
+ * raw types are the same, or not all CHAR types of the same shape.
+ */
+ def widerCharRawType(attrs: Seq[Attribute]): Option[DataType] = {
+ val rawTypeStrings = attrs.map(attr => getRawTypeString(attr.metadata))
+ if (rawTypeStrings.exists(_.isEmpty) || rawTypeStrings.distinct.length ==
1) {
+ None
+ } else {
+ rawTypeStrings.map(s =>
Option(CatalystSqlParser.parseDataType(s.get))).reduce {
+ (left, right) => for (l <- left; r <- right; wider <- widerCharType(l,
r)) yield wider
+ }
+ }
+ }
+
+ /**
+ * Pads the CHAR columns and fields of the given attribute that are shorter
than those at the
+ * same places of the given raw type, see [[widerCharRawType]]. Like
read-side padding, it does
+ * not truncate longer values. Returns the padded value and its raw type,
which keeps the names
+ * of the attribute's fields, or None if the attribute is already as long.
+ */
+ def padCharToWiderLength(attr: Attribute, rawType: DataType):
Option[(Expression, DataType)] = {
+ getRawType(attr.metadata).flatMap { attrRawType =>
+ widerCharType(attrRawType, rawType).filter(_ != attrRawType).map { wider
=>
+ val padded = processStringForCharVarchar(
+ attr, wider, charFuncName = Some(READ_SIDE_PADDING), varcharFuncName
= None)
+ (padded, wider)
+ }
+ }
+ }
+
+ /** The metadata with the given raw type, for a value padded to the CHAR
lengths of that type. */
+ def withRawType(metadata: Metadata, rawType: DataType): Metadata = {
+ new MetadataBuilder()
+ .withMetadata(metadata)
+ .putString(CHAR_VARCHAR_TYPE_STRING_METADATA_KEY, rawType.catalogString)
+ .build()
+ }
+
+ // Like typeWithWiderCharLength, but keeps the field names of type1, and
None for types that are
+ // not both CHAR types, or structs and arrays of them, with the same shape.
+ private def widerCharType(type1: DataType, type2: DataType):
Option[DataType] = {
+ (type1, type2) match {
+ case (c1: CharType, c2: CharType) if c1.collation == c2.collation =>
Review Comment:
Fixed: `widerCharType` compares `collationId`, so CHAR(3) and CHAR(5)
COLLATE UTF8_BINARY are compatible. Covered by the `tc` case 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]