diff --git a/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaSync.java b/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaSync.java index 29f7805ba..61f5775c6 100644 --- a/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaSync.java +++ b/xtable-core/src/main/java/org/apache/xtable/iceberg/IcebergSchemaSync.java @@ -57,6 +57,10 @@ public void sync(Schema current, Schema latest, Transaction transaction) { .sorted(Map.Entry.comparingByKey()) // execute updates in order .forEach(entry -> entry.getValue().get()); + // a field that becomes optional can no longer be an identifier field, so keep them in sync + if (!current.identifierFieldNames().equals(latest.identifierFieldNames())) { + updateSchema.setIdentifierFields(latest.identifierFieldNames()); + } updateSchema.commit(); } } diff --git a/xtable-core/src/test/java/org/apache/xtable/iceberg/TestIcebergSchemaSync.java b/xtable-core/src/test/java/org/apache/xtable/iceberg/TestIcebergSchemaSync.java index 4b0aca64d..289ad7fb0 100644 --- a/xtable-core/src/test/java/org/apache/xtable/iceberg/TestIcebergSchemaSync.java +++ b/xtable-core/src/test/java/org/apache/xtable/iceberg/TestIcebergSchemaSync.java @@ -24,6 +24,8 @@ import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -35,9 +37,13 @@ import org.apache.iceberg.BaseTable; import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; import org.apache.iceberg.TableMetadata; import org.apache.iceberg.Transaction; import org.apache.iceberg.UpdateSchema; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.inmemory.InMemoryCatalog; import org.apache.iceberg.types.Type; import org.apache.iceberg.types.Types; @@ -212,6 +218,44 @@ public void testUpdateMapKey() { verify(mockUpdateSchema, never()).commit(); } + @Test + public void testIdentifierFieldsRemovedWhenKeyBecomesOptional() { + Schema current = + new Schema( + Arrays.asList( + Types.NestedField.required(1, "id", Types.IntegerType.get()), + Types.NestedField.optional(2, "name", Types.StringType.get())), + Collections.singleton(1)); + Schema latest = + new Schema( + Arrays.asList( + Types.NestedField.optional(1, "id", Types.IntegerType.get()), + Types.NestedField.optional(2, "name", Types.StringType.get()))); + InMemoryCatalog catalog = new InMemoryCatalog(); + catalog.initialize("test", Collections.emptyMap()); + Namespace namespace = Namespace.of("db"); + catalog.createNamespace(namespace); + Table table = catalog.createTable(TableIdentifier.of(namespace, "tbl"), current); + + Transaction transaction = table.newTransaction(); + schemaSync.sync(table.schema(), latest, transaction); + transaction.commitTransaction(); + + Schema result = catalog.loadTable(TableIdentifier.of(namespace, "tbl")).schema(); + Assertions.assertTrue(result.identifierFieldNames().isEmpty()); + Assertions.assertTrue(result.findField("id").isOptional()); + } + + @Test + public void testIdentifierFieldsNotTouchedWhenUnchanged() { + UpdateSchema mockUpdateSchema = Mockito.mock(UpdateSchema.class); + when(mockTransaction.updateSchema()).thenReturn(mockUpdateSchema); + + schemaSync.sync(SCHEMA, updateFieldRequired(1), mockTransaction); + + verify(mockUpdateSchema, never()).setIdentifierFields(Mockito.anyCollection()); + } + @Test public void testMakeExistingColumnOptional() { UpdateSchema mockUpdateSchema = Mockito.mock(UpdateSchema.class);