dongjoon-hyun opened a new pull request, #60486:
URL: https://github.com/apache/spark/pull/60486

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue of DSv2 `UPDATE` on a table that accepts 
any schema (`TableCapability.ACCEPT_ANY_SCHEMA`) when its assignments include 
the metadata columns of the table. `RewriteUpdateTable` now writes only the 
data columns of the target table and handles the assignments to metadata 
columns as follows:
   
   - An assignment of a metadata column to itself, such as `index = index`, is 
removed. The metadata values passed to the connector follow `MetadataColumn` 
(e.g. `PRESERVE_ON_UPDATE`), as for any other `UPDATE`.
   - Any other assignment to a metadata column, such as `index = 5`, fails with 
`DATATYPE_MISMATCH.INVALID_ROW_LEVEL_OPERATION_ASSIGNMENTS`, as it does on 
other tables since SPARK-60721 (#59930), unless 
`spark.sql.legacy.ignoreMetadataColumnAssignments` is `true`. With that config, 
it is ignored.
   
   Spark does not align the assignments of an `UPDATE` on such a table, and 
leaves them to the rules injected for that table. `RewriteUpdateTable` rewrites 
the command once its assignments are aligned with the output of its target 
table. When the command references a metadata column, `AddMetadataColumns` 
still adds every metadata column to that output, which SPARK-60667 (#59895) 
kept on purpose for such tables. So the aligned assignments include one for 
each metadata column. Without any injected rule, this happens when the `SET` 
clause lists every column of the table, data columns first and then the 
metadata columns, in order. A rule that aligns the assignments with the output 
of the target table, as `ResolveRowLevelCommandAssignments` does for other 
tables, would add the assignments of the metadata columns to themselves in the 
same way.
   
   `RewriteUpdateTable` handled these assignments as assignments to data 
columns. As a result:
   
   - Group-based `UPDATE` and delta-based `UPDATE` pass the old value of a 
metadata column with `PRESERVE_ON_UPDATE = false` to the connector, instead of 
`null`.
   - They also pass the metadata columns in every row as data columns, and 
report them in `LogicalWriteInfo.schema()`.
   - An `UPDATE` represented as a delete and an insert 
(`SupportsDelta.representUpdateAsDeleteAndInsert`) builds a reinsert projection 
that is wider than the delete projection and the output of `Expand`. With 
whole-stage codegen, it fails in `ExpandExec` with 
`ArrayIndexOutOfBoundsException`. Without whole-stage codegen, it passes the 
old value of a metadata column that is not preserved on reinsert to the 
connector.
   
   For example, take an in-memory table `pk INT NOT NULL, id INT, dep STRING` 
partitioned by `dep` that accepts any schema, with the metadata columns `index` 
(`PRESERVE_ON_UPDATE = false`) and `_partition`, and the row `(1, 1, 'hr')`, 
and run:
   
   ```sql
   UPDATE t SET pk = pk, id = -1, dep = dep, index = index, _partition = 
_partition WHERE id IN (1, 100)
   -- Issue 1: The connector should receive the metadata (hr, null) and the 
write schema
   --          struct<pk:int,id:int,dep:string>, but it receives the metadata 
(hr, 0) and
   --          the write schema 
struct<pk:int,id:int,dep:string,index:int,_partition:string>.
   -- Issue 2: If the table represents an update as a delete and an insert, the 
statement should
   --          succeed, but it fails with
   --          java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds 
for length 6.
   
   UPDATE t SET pk = pk, id = -1, dep = dep, index = 5, _partition = _partition 
WHERE id IN (1, 100)
   -- Issue 3: It should fail as on a table that does not accept any schema,
   --          but the connector receives the metadata (hr, 5).
   ```
   
   **AFTER**
   
   The first statement writes what `UPDATE t SET id = -1 WHERE id IN (1, 100)` 
writes on a table that does not accept any schema: the connector receives the 
metadata `(hr, null)` and the write schema `struct<pk:int,id:int,dep:string>`, 
also when the update is represented as a delete and an insert. The second 
statement fails:
   
   ```
   [DATATYPE_MISMATCH.INVALID_ROW_LEVEL_OPERATION_ASSIGNMENTS] Cannot resolve 
"pk = pk", "id = -1", "dep = dep", "index = 5", "_partition = _partition" due 
to data type mismatch:
   - Cannot assign to metadata column 'index' SQLSTATE: 42K09
   ```
   
   ### Why are the changes needed?
   
   This is a correctness issue. `MetadataColumn.PRESERVE_ON_UPDATE` says that 
the metadata value is replaced with `null` if it is false. However, such an 
`UPDATE` passes the old value to the connector without any error. Row lineage 
in Iceberg and Delta Lake relies on this contract to reset the row version on 
update (SPARK-50820), so a connector that accepts such a write can record a 
stale row version. The same `UPDATE` also passes metadata columns to the 
connector as data columns, and an `UPDATE` represented as a delete and an 
insert fails instead.
   
   SPARK-60667 (#59895) fixed the same problems for tables that do not accept 
any schema.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. This is a bug fix for DSv2 `UPDATE` statements on tables that accept 
any schema whose assignments include metadata columns:
   
   - An assignment of a metadata column to itself no longer changes how the 
metadata columns are passed to the connector. Metadata columns with 
`PRESERVE_ON_UPDATE = false` are replaced with `null`, as documented, and 
connectors no longer receive metadata columns as data columns.
   - An `UPDATE` represented as a delete and an insert no longer fails.
   - Any other assignment to a metadata column fails, as on other tables. To 
ignore such assignments instead, set 
`spark.sql.legacy.ignoreMetadataColumnAssignments` to `true`.
   
   By code reading, this has happened since Spark started to rewrite `UPDATE` 
on DSv2 tables that support row-level operations in Apache Spark 
[3.5.0](https://github.com/apache/spark/releases/tag/v3.5.0) (2023-09-09).
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test cases:
   
   - `GroupBasedUpdateTableSuite`, `DeltaBasedUpdateTableSuite`, 
`DeltaBasedUpdateAsDeleteAndInsertTableSuite`: `SPARK-61683: update of a table 
accepting any schema handles metadata columns correctly`. Each runs the first 
statement above and checks the result, the write schema and the metadata that 
the connector receives. The group-based test runs it also with a subquery in 
the condition.
   - `UpdateTableSuiteBase`, for all group-based and delta-based `UPDATE` 
suites: `SPARK-61683: update of a table accepting any schema rejects metadata 
column assignments`. It runs the second statement above, and runs it again with 
the legacy config.
   
   All of them fail without this fix.
   
   ```
   build/sbt "sql/testOnly *UpdateTableSuite *UpdateAsDeleteAndInsertTableSuite 
-- -z SPARK-61683"
   ```
   
   ### 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]

Reply via email to