diff --git a/core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java b/core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java index 1ecf40cc40d6..8c9b1b3f4eaa 100644 --- a/core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java +++ b/core/src/main/java/org/apache/iceberg/MetadataUpdateParser.java @@ -397,7 +397,12 @@ private static MetadataUpdate readUpgradeFormatVersion(JsonNode node) { private static MetadataUpdate readAddSchema(JsonNode node) { JsonNode schemaNode = JsonUtil.get(SCHEMA, node); Schema schema = SchemaParser.fromJson(schemaNode); - int lastColumnId = JsonUtil.getInt(LAST_COLUMN_ID, node); + int lastColumnId; + if (node.has(LAST_COLUMN_ID)) { + lastColumnId = JsonUtil.getInt(LAST_COLUMN_ID, node); + } else { + lastColumnId = schema.highestFieldId(); + } return new MetadataUpdate.AddSchema(schema, lastColumnId); } diff --git a/core/src/test/java/org/apache/iceberg/TestMetadataUpdateParser.java b/core/src/test/java/org/apache/iceberg/TestMetadataUpdateParser.java index c6baead8a747..aa7c120aebe8 100644 --- a/core/src/test/java/org/apache/iceberg/TestMetadataUpdateParser.java +++ b/core/src/test/java/org/apache/iceberg/TestMetadataUpdateParser.java @@ -123,6 +123,17 @@ public void testAddSchemaFromJson() { assertEquals(action, actualUpdate, MetadataUpdateParser.fromJson(json)); } + @Test + public void testAddSchemaFromJsonWithoutLastColumnId() { + String action = MetadataUpdateParser.ADD_SCHEMA; + Schema schema = ID_DATA_SCHEMA; + int lastColumnId = schema.highestFieldId(); + String json = + String.format("{\"action\":\"add-schema\",\"schema\":%s}", SchemaParser.toJson(schema)); + MetadataUpdate actualUpdate = new MetadataUpdate.AddSchema(schema, lastColumnId); + assertEquals(action, actualUpdate, MetadataUpdateParser.fromJson(json)); + } + @Test public void testAddSchemaToJson() { Schema schema = ID_DATA_SCHEMA; diff --git a/open-api/rest-catalog-open-api.yaml b/open-api/rest-catalog-open-api.yaml index a2473488e9f4..dc29f66fb890 100644 --- a/open-api/rest-catalog-open-api.yaml +++ b/open-api/rest-catalog-open-api.yaml @@ -1500,6 +1500,9 @@ components: properties: schema: $ref: '#/components/schemas/Schema' + last-column-id: + type: integer + description: The highest assigned column ID for the table. This is used to ensure columns are always assigned an unused ID when evolving schemas. When omitted, it will be computed on the server side. SetCurrentSchemaUpdate: allOf: