dongjoon-hyun opened a new pull request, #60648: URL: https://github.com/apache/spark/pull/60648
### What changes were proposed in this pull request? This PR aims to fix a correctness issue where DSv2 `MERGE` writes the default value of a primitive type (e.g. `-1` for `INT`) instead of `NULL` when whole-stage codegen is enabled and an action assigns a Scala UDF with a primitive parameter whose argument is `NULL`. `RewriteMergeIntoTable` creates the output attributes of `MergeRows` with new expression IDs and derives their nullability from the instruction outputs once, in the `Resolution` batch. After that, `HandleNullInputsForUDF` in the `ScalaUDF Null Handling` batch wraps such a UDF as `If(IsNull(x), null, udf(x))`, which makes the instruction output nullable. `UpdateAttributeNullability` runs in the same batch, but it only updates the attribute references whose expression IDs come from the children. So, the output attributes of `MergeRows` stay non-nullable, and the whole-stage codegen of `MergeRowsExec` skips the null bits when it builds the output rows. This PR makes `UpdateAttributeNullability` widen the nullability of the output attributes of `MergeRows` from its instruction outputs after updating the attribute references, keeping their expression IDs. ### Why are the changes needed? `MergeRowsExec` supports whole-stage codegen since Apache Spark 4.2.0 ([v4.2.0](https://github.com/apache/spark/releases/tag/v4.2.0), 2026-07-11) via SPARK-53652. Its interpreted path uses the nullability of the instruction outputs and writes `NULL` correctly, but its codegen path uses the stale nullability of the output attributes. ```scala spark.udf.register("plus1", (x: Int) => x + 1) ``` ```sql -- t(pk INT NOT NULL, salary INT, dep STRING): (1, 100, 'hr'), (2, 200, 'software') -- s(pk INT, salary INT): (1, NULL), (2, 201), (3, NULL) -- Issue 1: It should update `salary` of `pk = 1` to NULL, but writes -1 on delta-based tables. MERGE INTO t USING s ON t.pk = s.pk WHEN MATCHED THEN UPDATE SET salary = plus1(s.salary); -- Issue 2: It should insert (3, NULL, 'new'), but inserts (3, -1, 'new'). MERGE INTO t USING s ON t.pk = s.pk WHEN NOT MATCHED AND s.salary < 0 THEN INSERT (pk, salary, dep) VALUES (s.pk, 0, 'invalid') WHEN NOT MATCHED THEN INSERT (pk, salary, dep) VALUES (s.pk, plus1(s.salary), 'new'); ``` ### Does this PR introduce _any_ user-facing change? Yes. `MERGE` writes `NULL` in these cases, as it does when whole-stage codegen is disabled. This fixes a regression introduced in Apache Spark 4.2.0. When the target column is `NOT NULL`, an insert-only `MERGE` with multiple `NOT MATCHED` clauses now fails at analysis with the same `INTERNAL_ERROR` that an insert-only `MERGE` with a single `NOT MATCHED` clause and `INSERT INTO` already raise in the same case, instead of writing `-1` for `NULL` inputs. ### How was this patch tested? Pass the CIs with the newly added test case. Without this fix, it fails in the suites with whole-stage codegen enabled. ``` [info] - SPARK-61934: merge writes NULL for a Scala UDF with a primitive input on NULL *** FAILED *** [info] !== Correct Answer - 3 == == Spark Answer - 3 == [info] !struct<> struct<pk:int,salary:int,dep:string> [info] [1,null,hr] [1,null,hr] [info] [2,202,software] [2,202,software] [info] ![3,null,new] [3,-1,new] (CheckAnswerHelper.scala:61) ``` ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 -- 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]
