peter-toth commented on code in PR #60651:
URL: https://github.com/apache/spark/pull/60651#discussion_r4620006042


##########
sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala:
##########
@@ -1276,6 +1276,16 @@ class UDFSuite extends SharedSparkSession {
     checkAnswer(df, Seq(Row(2, 3), Row(null, null)))
   }
 
+  test("SPARK-61935: ScalaUDF null handling of a column made nullable by an 
outer join") {

Review Comment:
   **Finding 3.** Issues 4 and 5 in the description are wrong results that this 
PR fixes, but no test covers them. What I ran, with `plus1` and `gs` as in the 
description, and "without the fix" meaning `ScalaUDF.nullable` set back to 
`udfNullable`:
   - Issue 4: without the fix it returns `[-1,2], [202,1]` with codegen and 
`[0,2], [202,1]` with `NO_CODEGEN`. At this head it returns `[null,2], [202,1]` 
in both modes.
   - Issue 5: without the fix it returns `[1,0,false], [2,202,false], 
[3,0,false]`. At this head it returns `[1,null,true], [2,202,false], 
[3,null,true]`.
   - `explode(array(plus1(a), plus1(b)))` over `(1, 2)` and `(NULL, 3)`: 
without the fix the NULL input gives `-1` and the column is non-nullable. At 
this head it gives NULL and the column is nullable.
   
   A `checkAnswer` for each, next to this test, would keep them fixed. The 
`explode` one would also pin the schema change from the user-facing section.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MergeIntoTableSuiteBase.scala:
##########
@@ -2425,6 +2425,60 @@ abstract class MergeIntoTableSuiteBase extends 
RowLevelOperationSuiteBase
     }
   }
 
+  test("SPARK-61935: merge checks NOT NULL for a Scala UDF with a primitive 
input") {

Review Comment:
   **Finding 1.** The description's first issue lists `UPDATE t SET salary = 
plus1(IF(pk = 1, NULL, salary)) WHERE pk = 1`, but only the MERGE form is 
tested. UPDATE is rewritten by `RewriteUpdateTable` rather than 
`RewriteMergeIntoTable`, so the MERGE test does not cover it. What I ran: that 
statement on this test's table, in probes on `GroupBasedUpdateTableSuite` and 
`DeltaBasedUpdateTableSuite`.
   - With `ScalaUDF.nullable` set back to `udfNullable`, it succeeds and the 
table reads back `(1, 0, hr)`.
   - At this head, it fails with `NOT_NULL_ASSERT_VIOLATION`.
   
   Could you add it to `UpdateTableSuiteBase`? Something like:
   
   ```scala
     test("SPARK-61935: update checks NOT NULL for a Scala UDF with a primitive 
input") {
       withUserDefinedFunction("plus1" -> true) {
         spark.udf.register("plus1", (x: Int) => x + 1)
         createAndInitTable("pk INT NOT NULL, salary INT NOT NULL, dep STRING",
           """{ "pk": 1, "salary": 100, "dep": "hr" }
             |{ "pk": 2, "salary": 200, "dep": "software" }
             |""".stripMargin)
         val e = intercept[SparkRuntimeException] {
           sql(s"UPDATE $tableNameAsString SET salary = plus1(IF(pk = 1, NULL, 
salary)) WHERE pk = 1")
         }
         assert(e.getCondition == "NOT_NULL_ASSERT_VIOLATION")
       }
     }
   ```
   



##########
docs/streaming/ss-migration-guide.md:
##########
@@ -38,6 +38,15 @@ Please refer [Migration Guide: SQL, Datasets and 
DataFrame](../sql-migration-gui
   To recover, write to a new table location with a new checkpoint location, or 
rewrite the
   committed files with the partition columns of the table.
 
+- Since Spark 4.4, a column derived from a typed Scala UDF with a primitive 
parameter is nullable
+  when the input of the parameter can be null, as the UDF returns NULL for a 
NULL input.
+  Previously, some of these columns, for example the output of a generator 
such as `explode` over
+  an array of the UDF results, were non-nullable. As a result, an existing 
query started before
+  Spark 4.3 that keeps such a column in its state, for example as a grouping 
key of a streaming
+  aggregation, fails to restart with `STATE_STORE_KEY_SCHEMA_NOT_COMPATIBLE`. 
To recover, use a new
+  checkpoint location, or set 
`spark.sql.streaming.stateStore.stateSchemaCheck` to `false`. Note
+  that the state of such a query can hold wrong keys, such as `-1` or `0`, for 
NULL inputs.

Review Comment:
   **Finding 4.** This sentence covers the queries that fail to restart. A 
query started on Spark 4.3 or later restarts, since SPARK-57003 keeps its state 
schema nullable, but its state can hold the same wrong keys. What I ran: the 
description's `explode` aggregation over a JSON file source. I wrote the 
checkpoint without the fix (`ScalaUDF.nullable` set back to `udfNullable`), 
then restarted at this head over a second file with the same rows.
   - Without the fix, the NULL input is keyed as `-1`.
   - After the restart, the output has both `[-1, 1]` and `[null, 1]`, so the 
NULL inputs are split across two groups.
   
   The description also says such queries "are not affected". Could the guide 
say that they keep these keys too? For example:
   
   ```suggestion
     that the state of such a query can hold wrong keys, such as `-1` or `0`, 
for NULL inputs. The
     same holds for a query started on Spark 4.3 or later, which restarts and 
then puts new NULL
     inputs under a separate NULL key. A new checkpoint location recomputes the 
state without them.
   ```
   



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