gengliangwang commented on code in PR #60750:
URL: https://github.com/apache/spark/pull/60750#discussion_r4642353996
##########
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:
**[P1] Exact-case field lookup misses fields renamed by Dataset.to**
The new by-name lookup `raw.fields.find(_.name == field.name)` is
case-sensitive. Project.reorderFields matches fields with the session resolver
(case-insensitive by default) and renames each one to the target's spelling
through createNewColumn, but keeps the column's original raw-type metadata. The
field's current name and its raw-type name then differ only in case, the lookup
returns None, and no padding is added. The ordinal lookup used before the
review-comment commit handled this case. Matching with the resolver, or falling
back to the ordinal, would fix it.
**Failure scenario:** With the default spark.sql.caseSensitive=false and
table s STRUCT<c: CHAR(3)> holding 'x':
spark.table("t").to(StructType.fromDDL("s STRUCT<C: STRING>")).selectExpr("s.C
= 'x'"). The rebuilt struct names the field 'C', the raw type has 'c', and find
returns None. The comparison is the unpadded 'x ' = 'x' and returns false,
while spark.table("t").selectExpr("s.c = 'x'") returns 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) }
+ .flatten
+ .map(_.dataType)
+ .filter { rawType =>
Review Comment:
**[P1] Nested struct reorder still loses the CHAR raw type**
RawTypedRef applies the
`equalsIgnoreNullability(replaceCharVarcharWithString(rawType),
field.dataType)` filter at every level of the GetStructField chain,
intermediate structs included. When Dataset.to reorders fields, or casts a
sibling field, inside a nested struct, the intermediate struct's raw type no
longer equals its current type. The lookup returns None, so nothing below that
level is padded. The by-name lookup added for Dataset.to (and the new
'reordered struct field' test) therefore only helps at the top level.
Project.matchSchema/createNewColumn keeps the top-level column's original
metadata (basicLogicalOperators.scala:252-261), so the nested case is
reachable. Running the filter only on the leaf, or recursing by name without
comparing intermediate struct types, would cover it.
**Failure scenario:** Table with n STRUCT<s: STRUCT<c: CHAR(3), v: STRING>>
holding ('x','a'). spark.table("t").to(StructType.fromDDL("n STRUCT<s:
STRUCT<v: STRING, c: STRING>>")).selectExpr("n.s.c = 'x'"). For the inner step,
struct<c:string,v:string> is compared with struct<v:string,c:string>, they
differ, and None is returned. No padding is added, so the read-padded 'x ' =
'x' returns false. The single-level version in the PR's test returns true.
--
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]