Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14429] fix(iceberg-rest): Apply schema evolution rules without snapshots (#14796)

MaSai8a4608379736
diff --git a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCommitValidation.java b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCommitValidation.java
index 51a4dcacff..b5f6900a1e 100644
--- a/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCommitValidation.java
+++ b/iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergCommitValidation.java
@@ -357,11 +357,24 @@ public final class IcebergCommitValidation implements UpdateRequirement {
}
/**
- * An added schema must keep the data already written readable: a field id that exists in the
- * current schema keeps its kind, may only change type by an allowed promotion and cannot go from
- * optional to required, a field id not in the current schema must be new (above last-column-id)
- * or identical to the field that had the id before, and a new required field needs a default. A
- * replace (which removes main) starts from no data, so only the id rule applies to it.
+ * An added schema must follow the Iceberg Table Spec schema-evolution rules
+ * (https://iceberg.apache.org/spec/#schema-evolution): a field id that exists in the current
+ * schema keeps its kind, may only change type by an allowed promotion, and cannot go from
+ * optional to required; a field id not in the current schema must be new (above last-column-id)
+ * or identical to the field that had the id before; and a new required field needs a default. The
+ * same rules are what Iceberg's {@code UpdateSchema} / {@code SchemaUpdate} enforce, and what the
+ * Gravitino API path applies.
+ *
+ * <p>Per that Table Spec section (and {@code SchemaUpdate}), these rules do <em>not</em> depend
+ * on whether the table has snapshots: incompatible evolution is rejected on empty tables unless
+ * {@code allowIncompatibleChanges()} is set. The Iceberg REST commit API likewise defines no
+ * {@code hasData} exemption. Dropping IRC's old {@code hasData} gate is therefore the
+ * Table-Spec-compatible behavior; empty-table clients that previously got 200 for illegal
+ * evolutions may now get 4xx.
+ *
+ * <p>A replace that removes {@code main} is not schema evolution under the Table Spec: it starts
+ * from no retained data, so only the field-id reuse rule applies and create-then-replace / staged
+ * create still succeed.
*/
private void checkSchemas(TableMetadata base, TableMetadata updated) {
boolean replacesTable =
@@ -373,7 +386,6 @@ public final class IcebergCommitValidation implements UpdateRequirement {
((MetadataUpdate.RemoveSnapshotRef) u).name()))
&& (updated.currentSnapshot() == null
|| base.snapshot(updated.currentSnapshot().snapshotId()) == null);
- boolean hasData = !base.snapshots().isEmpty();
Map<Integer, Types.NestedField> current = TypeUtil.indexById(base.schema().asStruct());
for (MetadataUpdate update : updates) {
if (!(update instanceof MetadataUpdate.AddSchema)) {
@@ -382,9 +394,10 @@ public final class IcebergCommitValidation implements UpdateRequirement {
Schema schema = ((MetadataUpdate.AddSchema) update).schema();
for (Types.NestedField field : TypeUtil.indexById(schema.asStruct()).values()) {
Types.NestedField existing = current.get(field.fieldId());
+ // Table Spec schema evolution (no hasData exemption); same as UpdateSchema / Gravitino API.
if (existing == null) {
- checkNewField(base, field, hasData && !replacesTable);
- } else if (hasData && !replacesTable) {
+ checkNewField(base, field, !replacesTable);
+ } else if (!replacesTable) {
checkTypeChange(existing, field, updated.formatVersion());
checkNullabilityChange(existing, field);
}
@@ -409,9 +422,7 @@ public final class IcebergCommitValidation implements UpdateRequirement {
}
if (checkRequired && field.isRequired() && field.initialDefault() == null) {
throw new ValidationException(
- "Invalid schema: required field %s has no initial default, so existing rows cannot be"
- + " read",
- field.name());
+ "Invalid schema: required field %s has no initial default", field.name());
}
}
@@ -456,8 +467,9 @@ public final class IcebergCommitValidation implements UpdateRequirement {
}
/**
- * An optional field cannot become required once data exists: files already written may hold nulls
- * for it, or lack it entirely, and readers then fail on them.
+ * An optional field cannot become required: the same rule as Iceberg's {@code
+ * UpdateSchema#requireColumn}, which refuses the change unless incompatible changes are
+ * explicitly allowed.
*/
private static void checkNullabilityChange(Types.NestedField existing, Types.NestedField field) {
if (existing.isOptional() && field.isRequired()) {
diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergCommitValidation.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergCommitValidation.java
index abc84fcae0..2167b55cc8 100644
--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergCommitValidation.java
+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/TestIcebergCommitValidation.java
@@ -317,6 +317,176 @@ public class TestIcebergCommitValidation {
Assertions.assertEquals(2, catalog.loadTable(IDENT).schemas().size());
}
+ /**
+ * Same evolution rules as Iceberg {@code UpdateSchema} / the Gravitino API: illegal changes are
+ * refused even when the table has no snapshots. Covers the #14429 OSS suite cases that still
+ * returned IRC 200 on empty tables.
+ */
+ @Test
+ public void testSchemaEvolutionRulesApplyWithoutSnapshots() {
+ TableIdentifier empty = TableIdentifier.of(Namespace.of("db"), "empty_evo");
+ Schema start =
+ new Schema(
+ Types.NestedField.optional(1, "l", Types.LongType.get()),
+ Types.NestedField.optional(2, "s", Types.StringType.get()),
+ Types.NestedField.optional(3, "i", Types.IntegerType.get()),
+ Types.NestedField.optional(4, "f", Types.FloatType.get()),
+ Types.NestedField.optional(5, "db", Types.DoubleType.get()),
+ Types.NestedField.optional(6, "d", Types.DecimalType.of(9, 2)));
+ catalog.createTable(
+ empty, start, PartitionSpec.unpartitioned(), ImmutableMap.of("format-version", "2"));
+ Assertions.assertNull(catalog.loadTable(empty).currentSnapshot());
+
+ // long -> int
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // string -> int
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // int -> string
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // float -> int
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // double -> float
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // decimal(9,2) -> decimal(9,3) and decimal(8,2)
+ for (String decimal : new String[] {"decimal(9, 3)", "decimal(8, 2)"}) {
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\""
+ + decimal
+ + "\"}");
+ }
+ // int -> required
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":true,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"}");
+ // add_required without default
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"l\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"},"
+ + "{\"id\":3,\"name\":\"i\",\"required\":false,\"type\":\"int\"},"
+ + "{\"id\":4,\"name\":\"f\",\"required\":false,\"type\":\"float\"},"
+ + "{\"id\":5,\"name\":\"db\",\"required\":false,\"type\":\"double\"},"
+ + "{\"id\":6,\"name\":\"d\",\"required\":false,\"type\":\"decimal(9, 2)\"},"
+ + "{\"id\":7,\"name\":\"nr\",\"required\":true,\"type\":\"string\"}");
+
+ Assertions.assertEquals(1, catalog.loadTable(empty).schemas().size());
+ Assertions.assertEquals(start.asStruct(), catalog.loadTable(empty).schema().asStruct());
+ }
+
+ /**
+ * Per Iceberg Table Spec schema evolution (no empty-table exemption): ordinary evolution on an
+ * empty table is refused, but replace / create-then-replace may redefine the schema because
+ * removing {@code main} is not evolution — type and nullability checks stay exempt.
+ */
+ @Test
+ public void testEmptyTableReplaceMayRedefineSchema() {
+ TableIdentifier empty = TableIdentifier.of(Namespace.of("db"), "empty_replace");
+ catalog.createTable(
+ empty,
+ new Schema(
+ Types.NestedField.optional(1, "id", Types.LongType.get()),
+ Types.NestedField.optional(2, "s", Types.StringType.get())),
+ PartitionSpec.unpartitioned(),
+ ImmutableMap.of("format-version", "2"));
+ Assertions.assertNull(catalog.loadTable(empty).currentSnapshot());
+
+ // Without RemoveSnapshotRef(main) these evolutions are refused on an empty table.
+ assertEmptyEvolutionRefused(
+ empty,
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"string\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":true,\"type\":\"string\"}");
+
+ // create-then-replace: drop main and install an incompatible schema in one commit.
+ CatalogHandlers.updateTable(
+ catalog,
+ empty,
+ request(
+ new MetadataUpdate.RemoveSnapshotRef(SnapshotRef.MAIN_BRANCH),
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"string\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":true,\"type\":\"string\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1)));
+
+ Table replaced = catalog.loadTable(empty);
+ Assertions.assertNull(replaced.currentSnapshot());
+ Assertions.assertEquals(Types.StringType.get(), replaced.schema().findType("id"));
+ Assertions.assertTrue(replaced.schema().findField("id").isRequired());
+ Assertions.assertTrue(replaced.schema().findField("s").isRequired());
+ }
+
+ /**
+ * Staged create-transaction (AssertTableDoesNotExist) committing a required column is correct per
+ * Iceberg create (not Table Spec schema evolution), so removing {@code hasData} must not reject
+ * it.
+ */
+ @Test
+ public void testStagedCreateTransactionStillSucceeds() {
+ TableIdentifier staged = TableIdentifier.of(Namespace.of("db"), "staged_create_ok");
+ UpdateTableRequest create = stagedCreateRequest(staged);
+ CatalogHandlers.updateTable(catalog, staged, IcebergCommitValidation.withValidation(create));
+ Assertions.assertTrue(catalog.tableExists(staged));
+ Assertions.assertNull(catalog.loadTable(staged).currentSnapshot());
+ Assertions.assertEquals(
+ Types.LongType.get(), catalog.loadTable(staged).schema().findType("id"));
+ Assertions.assertTrue(catalog.loadTable(staged).schema().findField("id").isRequired());
+ }
+
+ private void assertEmptyEvolutionRefused(TableIdentifier ident, String fields) {
+ Assertions.assertThrows(
+ ValidationException.class,
+ () ->
+ CatalogHandlers.updateTable(
+ catalog,
+ ident,
+ request(addSchema(fields), new MetadataUpdate.SetCurrentSchema(-1))));
+ }
+
@Test
public void testOptionalFieldCannotBecomeRequired() {
// Add optional s (id 2) and make it current; the table already has data without it.
diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java
index 348d085ad8..6fe62062bd 100644
--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java
+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/service/rest/TestIcebergTableOperations.java
@@ -128,8 +128,10 @@ public class TestIcebergTableOperations extends IcebergNamespaceTestBase {
private static final Schema tableSchema =
new Schema(NestedField.of(1, false, "foo_string", StringType.get()));
+ // Optional: IRC schema checks reject a new required field without an initial default even when
+ // the table has no snapshots (#14429).
private static final Schema newTableSchema =
- new Schema(NestedField.of(2, false, "foo_string1", StringType.get()));
+ new Schema(NestedField.of(2, true, "foo_string1", StringType.get()));
private DummyEventListener dummyEventListener;