shivadarshan-devadiga commented on code in PR #60750:
URL: https://github.com/apache/spark/pull/60750#discussion_r4651090947
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -48,6 +53,37 @@ object ApplyCharTypePaddingHelper {
}
}
+ /**
+ * A column, or a field of a struct column reached through
[[GetStructField]], with the raw type
+ * of that column or field. The raw type is the one recorded in the column's
metadata, so a CHAR
+ * or VARCHAR field keeps its length although its data type is a string.
+ */
+ object RawTypedRef {
+ // A value whose type no longer matches its raw type, e.g. after a cast,
has none.
+ def unapply(e: Expression): Option[DataType] = rawType(e).filter { rawType
=>
+ DataTypeUtils.equalsIgnoreNullability(
+ CharVarcharUtils.replaceCharVarcharWithString(rawType), e.dataType)
+ }
+
+ private def rawType(e: Expression): Option[DataType] = e match {
+ case AttrOrOuterRef(attr) => CharVarcharUtils.getRawType(attr.metadata)
+ case GetStructField(child, ordinal, _) =>
+ child.dataType match {
+ case current: StructType if ordinal < current.length =>
+ // Look the field up by name, as Dataset.to matches fields: it can
reorder and
+ // rename the fields of a struct at any level, and keeps the
column's metadata.
+ val name = current(ordinal).name
+ rawType(child).flatMap {
+ case raw: StructType => raw.fields.find(f =>
SQLConf.get.resolver(f.name, name))
Review Comment:
Done as you suggested. The field at `ordinal` is used when its name matches
exactly, and the resolver lookup only when it doesn't, as after `Dataset.to`.
--
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]