spark-test commented on code in PR #60420:
URL: https://github.com/apache/spark/pull/60420#discussion_r4558271924


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -309,14 +309,22 @@ object CharVarcharUtils extends Logging with 
SparkCharVarcharUtils {
           returnNullable = false)
 
       case StructType(fields) =>
-        val struct = CreateNamedStruct(fields.zipWithIndex.flatMap { case (f, 
i) =>
-          Seq(Literal(f.name), processStringForCharVarchar(
-            GetStructField(expr, i, Some(f.name)), f.dataType, charFuncName, 
varcharFuncName))
-        }.toImmutableArraySeq)
-        if (struct.valExprs.forall(_.isInstanceOf[GetStructField])) {
+        val fieldExprs = fields.zipWithIndex.map { case (f, i) =>
+          f -> processStringForCharVarchar(
+            GetStructField(expr, i, Some(f.name)), f.dataType, charFuncName, 
varcharFuncName)
+        }
+        if (fieldExprs.forall(_._2.isInstanceOf[GetStructField])) {
           // No field needs char/varchar processing, just return the original 
expression.
           expr
         } else {
+          val struct = CreateNamedStruct(fieldExprs.flatMap { case (f, 
fieldExpr) =>
+            // `GetStructField` of a nullable struct is nullable, so 
rebuilding the struct would
+            // drop a declared nested NOT NULL. Preserve it; 
`nullSafeRebuiltStruct` handles the
+            // whole-struct-null case.
+            val typed =
+              if (!f.nullable && fieldExpr.nullable) KnownNotNull(fieldExpr) 
else fieldExpr

Review Comment:
   **Non-blocking (P2):** `f` here is a field of the passed type `dt`, not of 
`expr.dataType`. On every table path, `dt` is parsed from the column's 
`__CHAR_VARCHAR_TYPE_STRING` metadata: `addPaddingForScan` does this for 
read-side padding and for DELETE, UPDATE, and MERGE target references, and so 
do the write-side checks in `TableOutputResolver`, `AssignmentUtils`, and 
`RowLevelAssignmentResolution`. That string comes from 
`StructType.catalogString`, which renders each field as `name:type` with no NOT 
NULL, and the parser reads every such field back as nullable. So `!f.nullable` 
is false on all of those paths, and this branch never fires there.
   
   For a V2 table `t (s STRUCT<c: CHAR(2), z: STRING NOT NULL>)` read with the 
default `spark.sql.readSideCharPadding=true`, the relation attribute keeps `z` 
NOT NULL, but the padded `s` still reports `z` as nullable. That is the same 
widened type that the description says breaks a Delta MERGE type check. Only 
the TRANSFORM assignment Project and direct `stringLengthCheck` calls with a 
declared type, like the new test's, keep the NOT NULL.
   
   See **Shared repair plan 1** in the review body.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/CharVarcharUtils.scala:
##########
@@ -309,14 +309,22 @@ object CharVarcharUtils extends Logging with 
SparkCharVarcharUtils {
           returnNullable = false)
 
       case StructType(fields) =>
-        val struct = CreateNamedStruct(fields.zipWithIndex.flatMap { case (f, 
i) =>
-          Seq(Literal(f.name), processStringForCharVarchar(
-            GetStructField(expr, i, Some(f.name)), f.dataType, charFuncName, 
varcharFuncName))
-        }.toImmutableArraySeq)
-        if (struct.valExprs.forall(_.isInstanceOf[GetStructField])) {
+        val fieldExprs = fields.zipWithIndex.map { case (f, i) =>
+          f -> processStringForCharVarchar(
+            GetStructField(expr, i, Some(f.name)), f.dataType, charFuncName, 
varcharFuncName)
+        }
+        if (fieldExprs.forall(_._2.isInstanceOf[GetStructField])) {
           // No field needs char/varchar processing, just return the original 
expression.
           expr
         } else {
+          val struct = CreateNamedStruct(fieldExprs.flatMap { case (f, 
fieldExpr) =>
+            // `GetStructField` of a nullable struct is nullable, so 
rebuilding the struct would
+            // drop a declared nested NOT NULL. Preserve it; 
`nullSafeRebuiltStruct` handles the
+            // whole-struct-null case.
+            val typed =
+              if (!f.nullable && fieldExpr.nullable) KnownNotNull(fieldExpr) 
else fieldExpr
+            Seq(Literal(f.name), typed)

Review Comment:
   **Blocking (P1):** On the TRANSFORM path under first-class CHAR/VARCHAR 
types, `KnownNotNull(fieldExpr)` sits between an extract and the CASE WHEN or 
`named_struct` it would otherwise fold into. `PushFoldableIntoBranches` pushes 
an extract only into a CASE WHEN or If that is its direct child and does not 
treat `KnownNotNull` as pushable, and `SimplifyExtractValueOps` folds only a 
`GetStructField` directly over a `CreateNamedStruct`.
   
   For `AS (s STRUCT<x: STRING, inner: STRUCT<d: CHAR(2), e: STRING> NOT 
NULL>)`, a query that reads `s.inner.e` therefore keeps `knownnotnull(CASE WHEN 
isnull(s.inner) THEN null ELSE named_struct('d', 
charTypeWriteSideCheck(s.inner.d, 2), 'e', s.inner.e) END)` in the plan. Every 
row builds the inner struct and runs the check on `d`, and a row whose `d` is 
`'abc'` fails with `EXCEED_LIMIT_LENGTH` even though the query never reads `d`. 
The base branch reduces the read to `s.inner.e` and returns the value, which is 
the pruning that the test `SPARK-60090: unreferenced CHAR overflow is pruned 
like scans` relies on.
   
   The same wrapper also drops nested field metadata: `CreateNamedStruct` 
copies metadata only from a `NamedExpression` or a bare `GetStructField`, so a 
wrapped passthrough field such as `z STRING NOT NULL COMMENT 'zip'` loses its 
comment from the analyzed schema. This review reached these conclusions by 
reading the optimizer rules at this commit, not by running them. If the kept 
NOT NULL is extended to table reads, the same wrapper would also block nested 
schema pruning and V2 predicate translation on default-on scans.
   
   See **Shared repair plan 1** in the review body.



##########
sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala:
##########
@@ -105,6 +105,19 @@ trait CharVarcharTestSuite extends QueryTest {
     }
   }
 
+  test("SPARK-61579: char/varchar struct processing preserves a nested NOT 
NULL field") {
+    val structType = new StructType()
+      .add("country", CharType(2))
+      .add("zip", StringType, nullable = false)
+    // A nullable struct whose fields carry the raw (string) types, like a 
read/write-side input.

Review Comment:
   **Nit (P3):** In this codebase, "raw type" means the original CHAR/VARCHAR 
type that `getRawType` restores from metadata, which is also what 
`checkColType` in this suite asserts. The next line builds the input with 
`replaceCharVarcharWithStringForCast(structType)`, which gives the 
string-replaced types, so this comment describes the opposite of what the input 
carries.
   
   ```suggestion
       // A nullable struct whose fields carry string-replaced types, like a 
read/write-side input.
   ```
   
   See **Shared repair plan 2** in the review body.



##########
sql/core/src/test/scala/org/apache/spark/sql/CharVarcharTestSuite.scala:
##########
@@ -105,6 +105,19 @@ trait CharVarcharTestSuite extends QueryTest {
     }
   }
 
+  test("SPARK-61579: char/varchar struct processing preserves a nested NOT 
NULL field") {
+    val structType = new StructType()
+      .add("country", CharType(2))
+      .add("zip", StringType, nullable = false)
+    // A nullable struct whose fields carry the raw (string) types, like a 
read/write-side input.
+    val input = Literal(null, 
CharVarcharUtils.replaceCharVarcharWithStringForCast(structType))
+    val processed = CharVarcharUtils.stringLengthCheck(input, structType)

Review Comment:
   **Non-blocking (P2):** This calls `stringLengthCheck` with a hand-built 
`structType` and a NULL literal, then checks only `processed.dataType`. No test 
runs the TRANSFORM assignment Project, the production caller whose output type 
this PR changes, and nothing evaluates the expression. The assertion would 
still hold if the rebuild were skipped, because the input type already declares 
`zip` NOT NULL, and skipping the rebuild would also drop the CHAR check on 
`country`. It would also hold if the tagged field were evaluated outside the 
`nullSafeRebuiltStruct` CASE WHEN, where `KnownNotNull` forces `isNull` to 
false in generated code. And because the test lives in the shared trait, the 
FileSource, DSV2, and Hive suites each run the same check without using a table.
   
   See **Shared repair plan 2** in the review body.



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