This is an automated email from the ASF dual-hosted git repository.

jerryshao pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/branch-1.3 by this push:
     new aa308efea5 [Cherry-pick to branch-1.3] [#14429] fix(iceberg-rest): 
refuse optional-to-required column changes on tables with data (#14748) (#14759)
aa308efea5 is described below

commit aa308efea58c41e3f8127877fe562729a880c318
Author: MaSai <[email protected]>
AuthorDate: Mon Nov 9 14:04:00 2026 +0800

    [Cherry-pick to branch-1.3] [#14429] fix(iceberg-rest): refuse 
optional-to-required column changes on tables with data (#14748) (#14759)
    
    **Cherry-pick Information:**
    - Original commit: 426f8908ef69175e54f14aea04259381436c66c0
    - Target branch: `branch-1.3`
    - Status: ✅ Conflicts resolved
    
    Co-authored-by: Cursor <[email protected]>
---
 .../iceberg/service/IcebergCommitValidation.java   | 20 ++++++--
 .../service/TestIcebergCommitValidation.java       | 55 ++++++++++++++++++++++
 2 files changed, 71 insertions(+), 4 deletions(-)

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 6d72c96d20..ba0c71b755 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);
+          checkNullabilityChange(existing, field);
         }
       }
     }
@@ -441,6 +442,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 b53c0b12bc..f0ee647e71 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 testReusingADroppedFieldIdIsRefused() {
     // Schema 1 adds b (id 2); schema 2 drops it; a later schema may not give 
id 2 to another field.

Reply via email to