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): refuse optional-to-required column changes on tables with data (#14748)

Mark Hoerth426f8908ef69
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 dd5eaf5c71..51a4dcacff 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
@@ -358,10 +358,10 @@ 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 and may only change type by an allowed promotion, 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.
+ * 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.
*/
private void checkSchemas(TableMetadata base, TableMetadata updated) {
boolean replacesTable =
@@ -386,6 +386,7 @@ public final class IcebergCommitValidation implements UpdateRequirement {
checkNewField(base, field, hasData && !replacesTable);
} else if (hasData && !replacesTable) {
checkTypeChange(existing, field, updated.formatVersion());
+ checkNullabilityChange(existing, field);
}
}
}
@@ -454,6 +455,17 @@ 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.
+ */
+ private static void checkNullabilityChange(Types.NestedField existing, Types.NestedField field) {
+ if (existing.isOptional() && field.isRequired()) {
+ throw new ValidationException(
+ "Invalid schema: field %s cannot change from optional to required", field.name());
+ }
+ }
+
/**
* Partition field ids identify partition data across specs (v2+): a field id another spec uses
* for a different field, or a new field id at or below last-partition-id, would mix up partition
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 398e9ce679..abc84fcae0 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,61 @@ public class TestIcebergCommitValidation {
Assertions.assertEquals(2, catalog.loadTable(IDENT).schemas().size());
}
+ @Test
+ public void testOptionalFieldCannotBecomeRequired() {
+ // Add optional s (id 2) and make it current; the table already has data without it.
+ commit(
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+
+ commitFails(
+ ValidationException.class,
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":true,\"type\":\"string\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+ Assertions.assertTrue(catalog.loadTable(IDENT).schema().findField(2).isOptional());
+
+ // Allowed: required to optional.
+ commit(
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":false,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"s\",\"required\":false,\"type\":\"string\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+ Assertions.assertTrue(catalog.loadTable(IDENT).schema().findField(1).isOptional());
+ }
+
+ @Test
+ public void testDecimalChangesFollowPromotionRules() {
+ commit(
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"d\",\"required\":false,\"type\":\"decimal(10, 2)\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+
+ for (String type : new String[] {"decimal(10, 3)", "decimal(9, 2)"}) {
+ commitFails(
+ ValidationException.class,
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"d\",\"required\":false,\"type\":\""
+ + type
+ + "\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+ }
+
+ // Allowed: widening precision at the same scale.
+ commit(
+ addSchema(
+ "{\"id\":1,\"name\":\"id\",\"required\":true,\"type\":\"long\"},"
+ + "{\"id\":2,\"name\":\"d\",\"required\":false,\"type\":\"decimal(12, 2)\"}"),
+ new MetadataUpdate.SetCurrentSchema(-1));
+ Assertions.assertEquals(
+ Types.DecimalType.of(12, 2), catalog.loadTable(IDENT).schema().findType("d"));
+ }
+
@Test
public void testDatePromotionsAreAllowedOnlyFromV3() {
// The v3 table spec allows date to timestamp and timestamp_ns; v2 does not.