dongjoon-hyun opened a new pull request, #60731:
URL: https://github.com/apache/spark/pull/60731
### What changes were proposed in this pull request?
This PR fixes the `INTERNAL_ERROR` that `DataFrameWriterV2.append()`,
`overwrite()`, and `overwritePartitions()` raise when a column, which is
non-nullable in its source Dataset and is made nullable by an outer join, is
written into a `NOT NULL` column of a DSv2 table.
`DataFrameWriterV2` builds its write command with the unanalyzed plan
(`df.queryExecution.logical`), so the analyzer analyzes the query again inside
the command. For the Dataset API, `right("b")` refers to the non-nullable
attribute of `right`, and only `UpdateAttributeNullability` makes the reference
nullable after the outer join. During the re-analysis:
1. `ResolveOutputRelation` in the `Resolution` batch sees the non-nullable
reference, finds it compatible with the `NOT NULL` column, and adds no
`AssertNotNull`.
2. `UpdateAttributeNullability` in a later batch makes the reference
nullable, so the output of the write command is not resolved anymore and
`CheckAnalysis` reports it as an unresolved operator.
`DataFrameWriter` builds the same commands with the analyzed plan
(`df.logicalPlan`), where the reference is already nullable. So,
`ResolveOutputRelation` adds `AssertNotNull`, and `insertInto` and
`saveAsTable` fail with `NOT_NULL_ASSERT_VIOLATION` as expected.
This PR makes `DataFrameWriterV2` use the analyzed plan like
`DataFrameWriter`. `create()`, `replace()`, and `createOrReplace()` use it too,
like `DataFrameWriter.saveAsTable` does for `CreateTableAsSelect`.
### Why are the changes needed?
To fix the `INTERNAL_ERROR`. This is a long-standing issue since Apache
Spark 3.0.0, which added `DataFrameWriterV2`, and is reproduced with Apache
Spark 3.5.9 and 4.3.0.
```scala
sql("CREATE TABLE testcat.t (a INT, e INT NOT NULL)")
val left = Seq(1, 2).toDF("a")
val right = Seq(2).toDF("b")
val df = left.join(right, left("a") === right("b"), "left_outer")
.select(left("a"), right("b").as("e"))
// NOT_NULL_ASSERT_VIOLATION
df.write.insertInto("testcat.t")
// INTERNAL_ERROR: Found the unresolved operator: 'AppendData
RelationV2[a#13, e#14] testcat.t
df.writeTo("testcat.t").append()
```
### Does this PR introduce _any_ user-facing change?
Yes.
- `DataFrameWriterV2.append()`, `overwrite()`, and `overwritePartitions()`
fail with `NOT_NULL_ASSERT_VIOLATION` instead of `INTERNAL_ERROR` when such a
column has a `NULL` value. When it has no `NULL` value, the write succeeds
instead of failing with `INTERNAL_ERROR`.
- When the source is a table DataFrame without any transformation, e.g.,
`spark.table(t)`, and a column of the table is dropped and re-added after the
DataFrame is created, `DataFrameWriterV2` fails with
`INCOMPATIBLE_TABLE_CHANGE_AFTER_ANALYSIS.COLUMNS_MISMATCH` like
`DataFrameWriter.insertInto`, instead of resolving the table again by name. A
source with any transformation, e.g., `spark.table(t).filter(...)`, already
fails in the same way.
### How was this patch tested?
Pass the CIs with the newly added test case in `DataFrameWriterV2Suite` and
the updated test case in `DataSourceV2DataFrameSuite`.
### 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]