shivadarshan-devadiga commented on code in PR #60750:
URL: https://github.com/apache/spark/pull/60750#discussion_r4642481473
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -48,6 +52,34 @@ 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 {
+ def unapply(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 =>
+ val field = current(ordinal)
+ // Look the field up by name: its position in the raw type can
differ, for example
+ // after Dataset.to reorders the fields of a struct and keeps the
column's metadata.
+ // A field whose type no longer matches its raw type has none.
+ unapply(child).collect { case raw: StructType =>
raw.fields.find(_.name == field.name) }
+ .flatten
+ .map(_.dataType)
+ .filter { rawType =>
Review Comment:
Good catch, thanks. `RawTypedRef` now follows the `GetStructField` chain by
name without comparing the types of the intermediate structs, and checks the
type only on the leaf: it keeps the raw type if it still matches the value's
current type, ignoring nullability. Your example is now in `SPARK-61970: a
reordered struct field keeps its own raw type`: after `to(n STRUCT<s: STRUCT<v:
STRING, c: STRING>>)`, `n.s.c = 'x'` and `n.s.v = 'a'` are both true.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ApplyCharTypePaddingHelper.scala:
##########
@@ -48,6 +52,34 @@ 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 {
+ def unapply(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 =>
+ val field = current(ordinal)
+ // Look the field up by name: its position in the raw type can
differ, for example
+ // after Dataset.to reorders the fields of a struct and keeps the
column's metadata.
+ // A field whose type no longer matches its raw type has none.
+ unapply(child).collect { case raw: StructType =>
raw.fields.find(_.name == field.name) }
Review Comment:
Fixed. The field is now matched with the session resolver, as
`Project.reorderFields` matches it. The same test covers your case: after `to(s
STRUCT<C: STRING, V: STRING>)`, `s.C = 'x'` and `s.V = 'a'` are true. Against
the previous commit it returned `false` for `s.C = 'x'`.
--
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]