dongjoon-hyun opened a new pull request, #60651:
URL: https://github.com/apache/spark/pull/60651
### What changes were proposed in this pull request?
This PR fixes DSv2 writes into a NOT NULL column whose value is a typed
Scala UDF with a primitive parameter over a nullable input, e.g.
`spark.udf.register("plus1", (x: Int) => x + 1)`. This is a correctness issue:
UPDATE and MERGE silently write NULL (or `-1` with whole-stage codegen) into
the NOT NULL column, while INSERT INTO and insert-only MERGE fail with
`INTERNAL_ERROR` even when no NULL is produced.
The analyzer makes such a UDF return NULL for a NULL input by wrapping it as
`If(IsNull(x), null, udf(KnownNotNull(x)))` in `HandleNullInputsForUDF`, which
runs only after the `Resolution` batch. Until then, `ScalaUDF.nullable` is the
declared nullability of the function result, which is `false` for `Int => Int`.
So the rules in the `Resolution` batch decide on stale nullability:
- `TableOutputResolver` (INSERT) and `AssignmentUtils` (UPDATE and MERGE
assignments) add no `AssertNotNull`.
- For INSERT and insert-only MERGE, `UpdateAttributeNullability` later makes
the query output nullable, so `V2WriteCommand.outputResolved` becomes false and
`CheckAnalysis` reports `Found the unresolved operator: 'AppendData` (or
`'InsertOnlyMerge`).
- For UPDATE and MERGE with other clauses, nothing checks the value and NULL
is written. The row projections of `ReplaceData` and `WriteDelta` also keep the
stale nullability, so their output stays resolved.
This PR makes `ScalaUDF.nullable` also true when the input of a primitive
parameter is nullable, in the same way as `deterministic` combines
`udfDeterministic` with the children:
- The constructor parameter `nullable` is renamed to `udfNullable`. It keeps
the declared nullability, which the code generation still uses to decide
whether the result must be unwrapped from an `Option`, so the generated code
does not change.
- `nullable` is `udfNullable || <a primitive parameter has a nullable
input>`.
Once `HandleNullInputsForUDF` wraps the UDF, its primitive inputs are
`KnownNotNull`, so `nullable` equals `udfNullable` again. The wrapping then no
longer changes the nullability that the rules in the `Resolution` batch saw.
### Why are the changes needed?
With a DSv2 table `t (pk INT NOT NULL, salary INT NOT NULL, dep STRING)`
that keeps NOT NULL and supports row-level operations, like the test
`InMemoryRowLevelOperationTableCatalog`:
```scala
spark.udf.register("plus1", (x: Int) => x + 1)
Seq((1, None), (2, Some(201)), (3, Some(301))).toDF("pk",
"salary").createOrReplaceTempView("source")
```
```sql
-- t has (1, 100, 'hr') and (2, 200, 'software')
-- Issue 1: It should fail with NOT_NULL_ASSERT_VIOLATION, but it writes
NULL (or `-1` with
-- whole-stage codegen in MERGE) into the NOT NULL column `salary` of the
row 1.
MERGE INTO t USING source s ON t.pk = s.pk WHEN MATCHED THEN UPDATE SET
salary = plus1(s.salary);
UPDATE t SET salary = plus1(IF(pk = 1, NULL, salary)) WHERE pk = 1;
-- Issue 2: It should insert (3, 302, 'new'), but it fails with
-- [INTERNAL_ERROR] Found the unresolved operator: 'InsertOnlyMerge
MERGE INTO t USING source s ON t.pk = s.pk
WHEN NOT MATCHED THEN INSERT (pk, salary, dep) VALUES (s.pk,
plus1(s.salary), 'new');
-- Issue 3: It should insert (3, 302, 'new'), but it fails with
-- [INTERNAL_ERROR] Found the unresolved operator: 'AppendData
INSERT INTO t SELECT pk, plus1(salary), 'new' FROM source WHERE pk = 3;
```
The same stale nullability is behind the wrong results fixed by SPARK-61934
(DSv2 MERGE into a nullable column writes `-1` with whole-stage codegen), and
this PR also fixes them at the source. Other wrong results that this PR fixes,
reproduced on Apache Spark 4.3.0 and 3.5.9:
```sql
-- Issue 4: It should return [NULL, 2], [202, 1], but it returns [-1, 2],
[202, 1] (`0` without codegen).
SELECT plus1(salary) AS k, count(*) FROM gs GROUP BY GROUPING SETS
((plus1(salary))) ORDER BY 1;
-- Issue 5: For a CTE that is not inlined, it should return [1, NULL, true]
and [3, NULL, true],
-- but it returns [1, 0, false] and [3, 0, false].
WITH c AS (SELECT pk, plus1(salary) AS k, rand() AS r FROM gs)
SELECT a.pk, a.k, a.k IS NULL FROM c a JOIN c b ON a.pk = b.pk ORDER BY a.pk;
```
where `gs` has the rows `(1, NULL)`, `(2, 201)` and `(3, NULL)`.
### Does this PR introduce _any_ user-facing change?
Yes. DSv2 writes of such a UDF into a NOT NULL column fail with
`NOT_NULL_ASSERT_VIOLATION` only for the rows that produce NULL and succeed
otherwise, instead of writing NULL or failing with `INTERNAL_ERROR`. The
queries above return NULL instead of `-1`, `0` or `false`.
### How was this patch tested?
Pass the CIs with the newly added test cases. The new tests fail without
this fix.
- `DataSourceV2SQLSuite`: INSERT INTO a NOT NULL column (it fails with
`INTERNAL_ERROR` without this fix).
- `MergeIntoTableSuiteBase` (7 suites): a NOT MATCHED INSERT succeeds and a
MATCHED UPDATE fails with `NOT_NULL_ASSERT_VIOLATION` (it fails with
`INTERNAL_ERROR` without this fix).
- `ScalaUDFSuite`: the nullability of `ScalaUDF` with primitive and
non-primitive parameters.
### 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]