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

huaxingao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new 233d924fec Core: Reject manifest-reading metadata tables for tables 
with root manifest (#19008)
233d924fec is described below

commit 233d924fec0c3c72dbd4f1636dec8072da309425
Author: Anoop Johnson <[email protected]>
AuthorDate: Tue Nov 17 17:26:08 2026 -0800

    Core: Reject manifest-reading metadata tables for tables with root manifest 
(#19008)
    
    Core: Reject manifest-reading metadata tables for snapshots with root 
manifests
    
    Metadata tables that read manifests (files, entries, manifests, all_*, ...)
    decode them through the v1-v3 metadata schema. Block the metadata tables
    that are incompatible.
    
    Metadata tables that are independent of the v4 schema (e.g. history,
    snapshots, refs, metadata_log_entries) are compatible and hence not blocked.
---
 .../org/apache/iceberg/MetadataTableUtils.java     |  22 ++++
 .../org/apache/iceberg/TestMetadataTableUtils.java | 118 +++++++++++++++++++++
 2 files changed, 140 insertions(+)

diff --git a/core/src/main/java/org/apache/iceberg/MetadataTableUtils.java 
b/core/src/main/java/org/apache/iceberg/MetadataTableUtils.java
index adb0f18ba1..025a4b3d96 100644
--- a/core/src/main/java/org/apache/iceberg/MetadataTableUtils.java
+++ b/core/src/main/java/org/apache/iceberg/MetadataTableUtils.java
@@ -19,10 +19,20 @@
 package org.apache.iceberg;
 
 import java.util.Locale;
+import java.util.Set;
 import org.apache.iceberg.catalog.TableIdentifier;
 import org.apache.iceberg.exceptions.NoSuchTableException;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
 
 public class MetadataTableUtils {
+  private static final int MIN_FORMAT_VERSION_WITH_ROOT_MANIFESTS = 4;
+  private static final Set<MetadataTableType> MANIFEST_INDEPENDENT_TABLES =
+      Sets.immutableEnumSet(
+          MetadataTableType.HISTORY,
+          MetadataTableType.SNAPSHOTS,
+          MetadataTableType.METADATA_LOG_ENTRIES,
+          MetadataTableType.REFS);
+
   private MetadataTableUtils() {}
 
   public static boolean hasMetadataTableName(TableIdentifier identifier) {
@@ -55,6 +65,18 @@ public class MetadataTableUtils {
 
   private static Table createMetadataTableInstance(
       Table baseTable, String metadataTableName, MetadataTableType type) {
+    if (!MANIFEST_INDEPENDENT_TABLES.contains(type)) {
+      TableMetadata metadata = ((BaseTable) baseTable).operations().current();
+      if (metadata.formatVersion() >= MIN_FORMAT_VERSION_WITH_ROOT_MANIFESTS
+          && metadata.snapshots().stream()
+              .anyMatch(snapshot -> snapshot.rootManifestLocation() != null)) {
+        throw new UnsupportedOperationException(
+            String.format(
+                "Metadata tables are not supported for tables with v4 root 
manifests: %s",
+                metadataTableName));
+      }
+    }
+
     switch (type) {
       case ENTRIES:
         return new ManifestEntriesTable(baseTable, metadataTableName);
diff --git a/core/src/test/java/org/apache/iceberg/TestMetadataTableUtils.java 
b/core/src/test/java/org/apache/iceberg/TestMetadataTableUtils.java
new file mode 100644
index 0000000000..4b40ff81ba
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/TestMetadataTableUtils.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.io.File;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+class TestMetadataTableUtils {
+  private static final long ROOT_MANIFEST_SNAPSHOT_ID = 42L;
+
+  @TempDir private File tableDir;
+
+  @AfterEach
+  void clearTables() {
+    TestTables.clearTables();
+  }
+
+  @Test
+  void rejectsMetadataTableForRootManifestSnapshot() {
+    Table table = withRootManifestSnapshot(upgradedV4Table(), 
SnapshotRef.MAIN_BRANCH);
+
+    assertThatThrownBy(
+            () -> MetadataTableUtils.createMetadataTableInstance(table, 
MetadataTableType.FILES))
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessageContaining(
+            "Metadata tables are not supported for tables with v4 root 
manifests");
+  }
+
+  @Test
+  void rejectsNoncurrentRootManifestSnapshot() {
+    Table table = withRootManifestSnapshot(upgradedV4Table(), "b1");
+
+    assertThatThrownBy(
+            () ->
+                MetadataTableUtils.createMetadataTableInstance(table, 
MetadataTableType.ALL_FILES))
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessageContaining(
+            "Metadata tables are not supported for tables with v4 root 
manifests");
+  }
+
+  @Test
+  void allowsMetadataTableWithoutRootManifestSnapshot() {
+    Table filesTable =
+        MetadataTableUtils.createMetadataTableInstance(upgradedV4Table(), 
MetadataTableType.FILES);
+
+    assertThat(filesTable).isInstanceOf(FilesTable.class);
+  }
+
+  @Test
+  void allowsManifestIndependentTableForRootManifestSnapshot() {
+    Table table = withRootManifestSnapshot(upgradedV4Table(), 
SnapshotRef.MAIN_BRANCH);
+
+    Table snapshotsTable =
+        MetadataTableUtils.createMetadataTableInstance(table, 
MetadataTableType.SNAPSHOTS);
+
+    assertThat(snapshotsTable).isInstanceOf(SnapshotsTable.class);
+  }
+
+  private Table upgradedV4Table() {
+    Table table =
+        TestTables.create(
+            tableDir, "test", TestBase.SCHEMA, TestBase.SPEC, 
SortOrder.unsorted(), 3);
+    table.newAppend().appendFile(TestBase.FILE_A).commit();
+
+    TableOperations ops = ((HasTableOperations) table).operations();
+    TableMetadata current = ops.current();
+    ops.commit(current, 
TableMetadata.buildFrom(current).upgradeFormatVersion(4).build());
+    table.refresh();
+    return table;
+  }
+
+  private static Table withRootManifestSnapshot(Table table, String branch) {
+    TableOperations ops = ((HasTableOperations) table).operations();
+    TableMetadata base = ops.current();
+    Snapshot parent = base.currentSnapshot();
+    Snapshot rootManifestSnapshot =
+        new BaseSnapshot(
+            base.lastSequenceNumber() + 1,
+            ROOT_MANIFEST_SNAPSHOT_ID,
+            parent.snapshotId(),
+            parent.timestampMillis() + 1,
+            DataOperations.APPEND,
+            ImmutableMap.of(),
+            base.currentSchemaId(),
+            null,
+            "file:/nonexistent/root-manifest.parquet",
+            base.nextRowId(),
+            0L,
+            null);
+    ops.commit(
+        base,
+        TableMetadata.buildFrom(base).setBranchSnapshot(rootManifestSnapshot, 
branch).build());
+    table.refresh();
+    return table;
+  }
+}

Reply via email to