vinishjail97 opened a new issue, #1034:
URL: https://github.com/apache/incubator-xtable/issues/1034

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/incubator-xtable/issues) and found no 
similar issues.
   
   ### Please describe the bug 🐞
   
   The Iceberg schema sync fails when the record key of an existing Iceberg 
target changes from required to nullable. Iceberg rejects the change with:
   
   ```
   java.lang.IllegalArgumentException: Cannot add field id as an identifier 
field: not a required field
   ```
   
   This was found with the Iceberg pluggable table format 
(`xtable-iceberg-pluggable-tf`) and the released 0.5.0 jars, on Spark 3.5 with 
Hudi 1.2.1, both locally and on S3. A Spark SQL `UPDATE` fails on a table that 
a Scala DataFrame created with a primitive (`NOT NULL`) key column. The failed 
Iceberg commit fails the Hudi commit.
   
   `DELETE`, `MERGE INTO` and DataFrame upserts on the same table work, because 
they keep the key column required. A table created with `CREATE TABLE` in Spark 
SQL has a nullable key from the start and is not affected.
   
   **Cause**
   
   - When every record key field is required, 
[`IcebergSchemaExtractor.toIceberg`](https://github.com/apache/incubator-xtable/blob/603a057c7a623747be0abcf553431dc36269c5c5/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaExtractor.java#L122-L151)
 sets the record key as the Iceberg identifier fields. When a key field is 
nullable, it returns a schema without identifier fields.
   - The SQL `UPDATE` writes with the catalog schema, in which `id` is 
nullable. 
[`IcebergSchemaSync.sync`](https://github.com/apache/incubator-xtable/blob/603a057c7a623747be0abcf553431dc36269c5c5/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaSync.java#L139)
 then calls `makeColumnOptional("id")`, but it does not update the identifier 
fields, so `id` is still an identifier field when the change is committed.
   
   #366 handled a nullable key when the table is created. This is the case 
where the nullability changes on an existing table.
   
   `IcebergSchemaSync` is used by `IcebergConversionTarget` for every Iceberg 
target, so a regular sync whose source key changes from required to nullable 
takes the same path. This was reproduced only through the pluggable table 
format so far.
   
   **Reproduce** (Spark 3.5, `hudi-spark3.5-bundle_2.12:1.2.1`, 
`iceberg-spark-runtime-3.5_2.12:1.9.2` and the 0.5.0 
`xtable-iceberg-pluggable-tf` jars, as in the 
[docs](https://xtable.apache.org/docs/how-to-iceberg-table-format)):
   
   ```scala
   val basePath = "/tmp/trips"
   Seq((1, "a", 100L, "sf"), (2, "b", 100L, "nyc")).toDF("id", "name", "ts", 
"city")
     .write.format("hudi")
     .option("hoodie.table.name", "trips")
     .option("hoodie.datasource.write.recordkey.field", "id")
     .option("hoodie.datasource.write.precombine.field", "ts")
     .option("hoodie.datasource.write.partitionpath.field", "city")
     .option("hoodie.table.format", "ICEBERG")
     .option("hoodie.metadata.enable", "false")
     .mode("append").save(basePath)
   
   spark.sql("SET hoodie.metadata.enable=false")
   spark.sql(s"CREATE TABLE trips USING hudi LOCATION '$basePath'")
   spark.sql("UPDATE trips SET name = 'u1' WHERE id = 1")
   // UpdateException: Failed to record the instant in Iceberg:
   //   Cannot add field id as an identifier field: not a required field
   ```
   
   **Possible fix**
   
   In `IcebergSchemaSync.sync`, set the identifier fields of the latest schema 
(`updateSchema.setIdentifierFields(...)`) before the nullability updates run, 
so the identifier is dropped before the column becomes optional. A case in 
`TestIcebergSchemaSync` that changes a required record key to nullable would 
cover it.
   
   The docs currently list this as a limitation of the Iceberg table format 
(#995).
   
   ### Are you willing to submit PR?
   
   - [x] I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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

Reply via email to