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 ecfdad338c Spark 3.5, 4.0, 4.1: Reject ExpireSnapshots action only for 
tables with root manifest (#19016)
ecfdad338c is described below

commit ecfdad338c7a9c60bda87765c49413f18aef0137
Author: Anoop Johnson <[email protected]>
AuthorDate: Tue Nov 17 13:46:40 2026 -0800

    Spark 3.5, 4.0, 4.1: Reject ExpireSnapshots action only for tables with 
root manifest (#19016)
    
    Spark 3.5, 4.0, 4.1: Reject ExpireSnapshots action only for tables with 
root manifests
    
    Backport of #19012 (https://github.com/apache/iceberg/pull/19012) to Spark
    3.5, 4.0, and 4.1.
---
 .../spark/actions/ExpireSnapshotsSparkAction.java  |  9 ++--
 .../spark/actions/TestExpireSnapshotsAction.java   |  2 +-
 .../spark/actions/TestExpireSnapshotsActionV4.java | 63 +++++++++++++++-------
 .../spark/actions/TestRewriteTablePathsAction.java |  2 +-
 .../spark/actions/ExpireSnapshotsSparkAction.java  |  9 ++--
 .../spark/actions/TestExpireSnapshotsAction.java   |  2 +-
 .../spark/actions/TestExpireSnapshotsActionV4.java | 63 +++++++++++++++-------
 .../spark/actions/TestRewriteTablePathsAction.java |  2 +-
 .../spark/actions/ExpireSnapshotsSparkAction.java  |  9 ++--
 .../spark/actions/TestExpireSnapshotsAction.java   |  2 +-
 .../spark/actions/TestExpireSnapshotsActionV4.java | 63 +++++++++++++++-------
 .../spark/actions/TestRewriteTablePathsAction.java |  2 +-
 12 files changed, 150 insertions(+), 78 deletions(-)

diff --git 
a/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
 
b/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
index b8b95d4faf..67542e7023 100644
--- 
a/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
+++ 
b/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
@@ -69,8 +69,6 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public static final String STREAM_RESULTS = "stream-results";
   public static final boolean STREAM_RESULTS_DEFAULT = false;
 
-  private static final int LAST_SUPPORTED_FORMAT_VERSION = 3;
-
   private static final Logger LOG = 
LoggerFactory.getLogger(ExpireSnapshotsSparkAction.class);
 
   private final Table table;
@@ -151,11 +149,10 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public Dataset<FileInfo> expireFiles() {
     if (expiredFileDS == null) {
       TableMetadata originalMetadata = ops.refresh();
-      int formatVersion = originalMetadata.formatVersion();
       ValidationException.check(
-          formatVersion <= LAST_SUPPORTED_FORMAT_VERSION,
-          "Cannot expire snapshots: this action does not support v%s tables",
-          formatVersion);
+          originalMetadata.snapshots().stream()
+              .noneMatch(snapshot -> snapshot.rootManifestLocation() != null),
+          "Cannot expire snapshots: this action does not support tables with 
root manifests");
 
       // perform expiration
       org.apache.iceberg.ExpireSnapshots expireSnapshots = 
table.expireSnapshots();
diff --git 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
index e18fc7e12a..c89e4f7852 100644
--- 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
+++ 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
@@ -131,7 +131,7 @@ public class TestExpireSnapshotsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> parameters() {
-    return org.apache.iceberg.TestHelpers.V2_AND_V3;
+    return org.apache.iceberg.TestHelpers.V2_AND_ABOVE;
   }
 
   @TempDir private File tableDir;
diff --git 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
index bc08ae66ce..9252fd560b 100644
--- 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
+++ 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
@@ -29,7 +29,11 @@ import org.apache.iceberg.DataFiles;
 import org.apache.iceberg.HasTableOperations;
 import org.apache.iceberg.PartitionSpec;
 import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.SnapshotParser;
+import org.apache.iceberg.SnapshotRef;
 import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
 import org.apache.iceberg.TableOperations;
 import org.apache.iceberg.TableProperties;
 import org.apache.iceberg.exceptions.ValidationException;
@@ -55,14 +59,17 @@ class TestExpireSnapshotsActionV4 extends TestBase {
           .withFileSizeInBytes(10)
           .withRecordCount(1)
           .build();
+  private static final String UNSUPPORTED_MESSAGE =
+      "Cannot expire snapshots: this action does not support tables with root 
manifests";
 
   @TempDir private File tableDir;
 
   @Test
-  void rejectsExpirationOnV4() {
-    Table table = createTableWithTwoSnapshots(4);
-    TableOperations ops = ((HasTableOperations) table).operations();
+  void rejectsExpirationWithRootManifestSnapshot() {
+    Table table = createTableWithTwoSnapshots();
     long expirableSnapshotId = table.currentSnapshot().parentId();
+    commitRootManifestSnapshot(table);
+    TableOperations ops = ((HasTableOperations) table).operations();
     String metadataBefore = ops.current().metadataFileLocation();
 
     assertThatThrownBy(
@@ -72,7 +79,7 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(ops.refresh().metadataFileLocation())
         .as("no new metadata file should be committed")
@@ -80,18 +87,13 @@ class TestExpireSnapshotsActionV4 extends TestBase {
   }
 
   @Test
-  void rejectsOnStaleV3HandleAfterUpgradeToV4() {
-    Table staleHandle = createTableWithTwoSnapshots(3);
-    TableOperations staleOps = ((HasTableOperations) staleHandle).operations();
+  void rejectsOnStaleHandleAfterRootManifestSnapshot() {
+    Table staleHandle = createTableWithTwoSnapshots();
     long expirableSnapshotId = staleHandle.currentSnapshot().parentId();
-    assertThat(staleOps.current().formatVersion()).isEqualTo(3);
 
-    // upgrade to v4 through a second handle; the first handle keeps its 
cached v3 metadata
-    TABLES
-        .load(tableLocation())
-        .updateProperties()
-        .set(TableProperties.FORMAT_VERSION, "4")
-        .commit();
+    // commit through a second handle; the first handle keeps its cached 
metadata
+    commitRootManifestSnapshot(TABLES.load(tableLocation()));
+    assertThat(staleHandle.snapshots()).as("stale handle should not see the 
commit").hasSize(2);
 
     assertThatThrownBy(
             () ->
@@ -100,25 +102,50 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(TABLES.load(tableLocation()).snapshots())
         .as("no snapshot should be expired")
-        .hasSize(2);
+        .hasSize(3);
   }
 
-  private Table createTableWithTwoSnapshots(int formatVersion) {
+  private Table createTableWithTwoSnapshots() {
     Table table =
         TABLES.create(
             SCHEMA,
             PartitionSpec.unpartitioned(),
-            ImmutableMap.of(TableProperties.FORMAT_VERSION, 
String.valueOf(formatVersion)),
+            ImmutableMap.of(TableProperties.FORMAT_VERSION, "4"),
             tableLocation());
     table.newAppend().appendFile(FILE_A).commit();
     table.newAppend().appendFile(FILE_B).commit();
     return table;
   }
 
+  // there is no writer that produces root manifests yet, so commit a 
hand-built snapshot
+  private static void commitRootManifestSnapshot(Table table) {
+    TableOperations ops = ((HasTableOperations) table).operations();
+    TableMetadata base = ops.current();
+    Snapshot parent = base.currentSnapshot();
+    Snapshot rootManifestSnapshot =
+        SnapshotParser.fromJson(
+            String.format(
+                "{\"sequence-number\": %d, \"snapshot-id\": %d, 
\"parent-snapshot-id\": %d, "
+                    + "\"timestamp-ms\": %d, \"summary\": {\"operation\": 
\"append\"}, "
+                    + "\"schema-id\": %d, \"first-row-id\": %d, 
\"added-rows\": 0, "
+                    + "\"root-manifest\": 
\"file:/nonexistent/root-manifest.parquet\"}",
+                base.lastSequenceNumber() + 1,
+                parent.snapshotId() + 1,
+                parent.snapshotId(),
+                parent.timestampMillis() + 1,
+                base.currentSchemaId(),
+                base.nextRowId()));
+    ops.commit(
+        base,
+        TableMetadata.buildFrom(base)
+            .setBranchSnapshot(rootManifestSnapshot, SnapshotRef.MAIN_BRANCH)
+            .build());
+  }
+
   private String tableLocation() {
     return tableDir.toURI().toString();
   }
diff --git 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
index ef246c7a61..d520f350d2 100644
--- 
a/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
+++ 
b/spark/v3.5/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
@@ -115,7 +115,7 @@ public class TestRewriteTablePathsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> formatVersions() {
-    return TestHelpers.V2_AND_V3;
+    return TestHelpers.V2_AND_ABOVE;
   }
 
   @Parameter private int formatVersion;
diff --git 
a/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
 
b/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
index b8b95d4faf..67542e7023 100644
--- 
a/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
+++ 
b/spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
@@ -69,8 +69,6 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public static final String STREAM_RESULTS = "stream-results";
   public static final boolean STREAM_RESULTS_DEFAULT = false;
 
-  private static final int LAST_SUPPORTED_FORMAT_VERSION = 3;
-
   private static final Logger LOG = 
LoggerFactory.getLogger(ExpireSnapshotsSparkAction.class);
 
   private final Table table;
@@ -151,11 +149,10 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public Dataset<FileInfo> expireFiles() {
     if (expiredFileDS == null) {
       TableMetadata originalMetadata = ops.refresh();
-      int formatVersion = originalMetadata.formatVersion();
       ValidationException.check(
-          formatVersion <= LAST_SUPPORTED_FORMAT_VERSION,
-          "Cannot expire snapshots: this action does not support v%s tables",
-          formatVersion);
+          originalMetadata.snapshots().stream()
+              .noneMatch(snapshot -> snapshot.rootManifestLocation() != null),
+          "Cannot expire snapshots: this action does not support tables with 
root manifests");
 
       // perform expiration
       org.apache.iceberg.ExpireSnapshots expireSnapshots = 
table.expireSnapshots();
diff --git 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
index e18fc7e12a..c89e4f7852 100644
--- 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
+++ 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
@@ -131,7 +131,7 @@ public class TestExpireSnapshotsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> parameters() {
-    return org.apache.iceberg.TestHelpers.V2_AND_V3;
+    return org.apache.iceberg.TestHelpers.V2_AND_ABOVE;
   }
 
   @TempDir private File tableDir;
diff --git 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
index bc08ae66ce..9252fd560b 100644
--- 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
+++ 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
@@ -29,7 +29,11 @@ import org.apache.iceberg.DataFiles;
 import org.apache.iceberg.HasTableOperations;
 import org.apache.iceberg.PartitionSpec;
 import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.SnapshotParser;
+import org.apache.iceberg.SnapshotRef;
 import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
 import org.apache.iceberg.TableOperations;
 import org.apache.iceberg.TableProperties;
 import org.apache.iceberg.exceptions.ValidationException;
@@ -55,14 +59,17 @@ class TestExpireSnapshotsActionV4 extends TestBase {
           .withFileSizeInBytes(10)
           .withRecordCount(1)
           .build();
+  private static final String UNSUPPORTED_MESSAGE =
+      "Cannot expire snapshots: this action does not support tables with root 
manifests";
 
   @TempDir private File tableDir;
 
   @Test
-  void rejectsExpirationOnV4() {
-    Table table = createTableWithTwoSnapshots(4);
-    TableOperations ops = ((HasTableOperations) table).operations();
+  void rejectsExpirationWithRootManifestSnapshot() {
+    Table table = createTableWithTwoSnapshots();
     long expirableSnapshotId = table.currentSnapshot().parentId();
+    commitRootManifestSnapshot(table);
+    TableOperations ops = ((HasTableOperations) table).operations();
     String metadataBefore = ops.current().metadataFileLocation();
 
     assertThatThrownBy(
@@ -72,7 +79,7 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(ops.refresh().metadataFileLocation())
         .as("no new metadata file should be committed")
@@ -80,18 +87,13 @@ class TestExpireSnapshotsActionV4 extends TestBase {
   }
 
   @Test
-  void rejectsOnStaleV3HandleAfterUpgradeToV4() {
-    Table staleHandle = createTableWithTwoSnapshots(3);
-    TableOperations staleOps = ((HasTableOperations) staleHandle).operations();
+  void rejectsOnStaleHandleAfterRootManifestSnapshot() {
+    Table staleHandle = createTableWithTwoSnapshots();
     long expirableSnapshotId = staleHandle.currentSnapshot().parentId();
-    assertThat(staleOps.current().formatVersion()).isEqualTo(3);
 
-    // upgrade to v4 through a second handle; the first handle keeps its 
cached v3 metadata
-    TABLES
-        .load(tableLocation())
-        .updateProperties()
-        .set(TableProperties.FORMAT_VERSION, "4")
-        .commit();
+    // commit through a second handle; the first handle keeps its cached 
metadata
+    commitRootManifestSnapshot(TABLES.load(tableLocation()));
+    assertThat(staleHandle.snapshots()).as("stale handle should not see the 
commit").hasSize(2);
 
     assertThatThrownBy(
             () ->
@@ -100,25 +102,50 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(TABLES.load(tableLocation()).snapshots())
         .as("no snapshot should be expired")
-        .hasSize(2);
+        .hasSize(3);
   }
 
-  private Table createTableWithTwoSnapshots(int formatVersion) {
+  private Table createTableWithTwoSnapshots() {
     Table table =
         TABLES.create(
             SCHEMA,
             PartitionSpec.unpartitioned(),
-            ImmutableMap.of(TableProperties.FORMAT_VERSION, 
String.valueOf(formatVersion)),
+            ImmutableMap.of(TableProperties.FORMAT_VERSION, "4"),
             tableLocation());
     table.newAppend().appendFile(FILE_A).commit();
     table.newAppend().appendFile(FILE_B).commit();
     return table;
   }
 
+  // there is no writer that produces root manifests yet, so commit a 
hand-built snapshot
+  private static void commitRootManifestSnapshot(Table table) {
+    TableOperations ops = ((HasTableOperations) table).operations();
+    TableMetadata base = ops.current();
+    Snapshot parent = base.currentSnapshot();
+    Snapshot rootManifestSnapshot =
+        SnapshotParser.fromJson(
+            String.format(
+                "{\"sequence-number\": %d, \"snapshot-id\": %d, 
\"parent-snapshot-id\": %d, "
+                    + "\"timestamp-ms\": %d, \"summary\": {\"operation\": 
\"append\"}, "
+                    + "\"schema-id\": %d, \"first-row-id\": %d, 
\"added-rows\": 0, "
+                    + "\"root-manifest\": 
\"file:/nonexistent/root-manifest.parquet\"}",
+                base.lastSequenceNumber() + 1,
+                parent.snapshotId() + 1,
+                parent.snapshotId(),
+                parent.timestampMillis() + 1,
+                base.currentSchemaId(),
+                base.nextRowId()));
+    ops.commit(
+        base,
+        TableMetadata.buildFrom(base)
+            .setBranchSnapshot(rootManifestSnapshot, SnapshotRef.MAIN_BRANCH)
+            .build());
+  }
+
   private String tableLocation() {
     return tableDir.toURI().toString();
   }
diff --git 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
index ef246c7a61..d520f350d2 100644
--- 
a/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
+++ 
b/spark/v4.0/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
@@ -115,7 +115,7 @@ public class TestRewriteTablePathsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> formatVersions() {
-    return TestHelpers.V2_AND_V3;
+    return TestHelpers.V2_AND_ABOVE;
   }
 
   @Parameter private int formatVersion;
diff --git 
a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
 
b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
index b8b95d4faf..67542e7023 100644
--- 
a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
+++ 
b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
@@ -69,8 +69,6 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public static final String STREAM_RESULTS = "stream-results";
   public static final boolean STREAM_RESULTS_DEFAULT = false;
 
-  private static final int LAST_SUPPORTED_FORMAT_VERSION = 3;
-
   private static final Logger LOG = 
LoggerFactory.getLogger(ExpireSnapshotsSparkAction.class);
 
   private final Table table;
@@ -151,11 +149,10 @@ public class ExpireSnapshotsSparkAction extends 
BaseSparkAction<ExpireSnapshotsS
   public Dataset<FileInfo> expireFiles() {
     if (expiredFileDS == null) {
       TableMetadata originalMetadata = ops.refresh();
-      int formatVersion = originalMetadata.formatVersion();
       ValidationException.check(
-          formatVersion <= LAST_SUPPORTED_FORMAT_VERSION,
-          "Cannot expire snapshots: this action does not support v%s tables",
-          formatVersion);
+          originalMetadata.snapshots().stream()
+              .noneMatch(snapshot -> snapshot.rootManifestLocation() != null),
+          "Cannot expire snapshots: this action does not support tables with 
root manifests");
 
       // perform expiration
       org.apache.iceberg.ExpireSnapshots expireSnapshots = 
table.expireSnapshots();
diff --git 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
index e18fc7e12a..c89e4f7852 100644
--- 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
+++ 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
@@ -131,7 +131,7 @@ public class TestExpireSnapshotsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> parameters() {
-    return org.apache.iceberg.TestHelpers.V2_AND_V3;
+    return org.apache.iceberg.TestHelpers.V2_AND_ABOVE;
   }
 
   @TempDir private File tableDir;
diff --git 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
index bc08ae66ce..9252fd560b 100644
--- 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
+++ 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
@@ -29,7 +29,11 @@ import org.apache.iceberg.DataFiles;
 import org.apache.iceberg.HasTableOperations;
 import org.apache.iceberg.PartitionSpec;
 import org.apache.iceberg.Schema;
+import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.SnapshotParser;
+import org.apache.iceberg.SnapshotRef;
 import org.apache.iceberg.Table;
+import org.apache.iceberg.TableMetadata;
 import org.apache.iceberg.TableOperations;
 import org.apache.iceberg.TableProperties;
 import org.apache.iceberg.exceptions.ValidationException;
@@ -55,14 +59,17 @@ class TestExpireSnapshotsActionV4 extends TestBase {
           .withFileSizeInBytes(10)
           .withRecordCount(1)
           .build();
+  private static final String UNSUPPORTED_MESSAGE =
+      "Cannot expire snapshots: this action does not support tables with root 
manifests";
 
   @TempDir private File tableDir;
 
   @Test
-  void rejectsExpirationOnV4() {
-    Table table = createTableWithTwoSnapshots(4);
-    TableOperations ops = ((HasTableOperations) table).operations();
+  void rejectsExpirationWithRootManifestSnapshot() {
+    Table table = createTableWithTwoSnapshots();
     long expirableSnapshotId = table.currentSnapshot().parentId();
+    commitRootManifestSnapshot(table);
+    TableOperations ops = ((HasTableOperations) table).operations();
     String metadataBefore = ops.current().metadataFileLocation();
 
     assertThatThrownBy(
@@ -72,7 +79,7 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(ops.refresh().metadataFileLocation())
         .as("no new metadata file should be committed")
@@ -80,18 +87,13 @@ class TestExpireSnapshotsActionV4 extends TestBase {
   }
 
   @Test
-  void rejectsOnStaleV3HandleAfterUpgradeToV4() {
-    Table staleHandle = createTableWithTwoSnapshots(3);
-    TableOperations staleOps = ((HasTableOperations) staleHandle).operations();
+  void rejectsOnStaleHandleAfterRootManifestSnapshot() {
+    Table staleHandle = createTableWithTwoSnapshots();
     long expirableSnapshotId = staleHandle.currentSnapshot().parentId();
-    assertThat(staleOps.current().formatVersion()).isEqualTo(3);
 
-    // upgrade to v4 through a second handle; the first handle keeps its 
cached v3 metadata
-    TABLES
-        .load(tableLocation())
-        .updateProperties()
-        .set(TableProperties.FORMAT_VERSION, "4")
-        .commit();
+    // commit through a second handle; the first handle keeps its cached 
metadata
+    commitRootManifestSnapshot(TABLES.load(tableLocation()));
+    assertThat(staleHandle.snapshots()).as("stale handle should not see the 
commit").hasSize(2);
 
     assertThatThrownBy(
             () ->
@@ -100,25 +102,50 @@ class TestExpireSnapshotsActionV4 extends TestBase {
                     .expireSnapshotId(expirableSnapshotId)
                     .execute())
         .isInstanceOf(ValidationException.class)
-        .hasMessage("Cannot expire snapshots: this action does not support v4 
tables");
+        .hasMessage(UNSUPPORTED_MESSAGE);
 
     assertThat(TABLES.load(tableLocation()).snapshots())
         .as("no snapshot should be expired")
-        .hasSize(2);
+        .hasSize(3);
   }
 
-  private Table createTableWithTwoSnapshots(int formatVersion) {
+  private Table createTableWithTwoSnapshots() {
     Table table =
         TABLES.create(
             SCHEMA,
             PartitionSpec.unpartitioned(),
-            ImmutableMap.of(TableProperties.FORMAT_VERSION, 
String.valueOf(formatVersion)),
+            ImmutableMap.of(TableProperties.FORMAT_VERSION, "4"),
             tableLocation());
     table.newAppend().appendFile(FILE_A).commit();
     table.newAppend().appendFile(FILE_B).commit();
     return table;
   }
 
+  // there is no writer that produces root manifests yet, so commit a 
hand-built snapshot
+  private static void commitRootManifestSnapshot(Table table) {
+    TableOperations ops = ((HasTableOperations) table).operations();
+    TableMetadata base = ops.current();
+    Snapshot parent = base.currentSnapshot();
+    Snapshot rootManifestSnapshot =
+        SnapshotParser.fromJson(
+            String.format(
+                "{\"sequence-number\": %d, \"snapshot-id\": %d, 
\"parent-snapshot-id\": %d, "
+                    + "\"timestamp-ms\": %d, \"summary\": {\"operation\": 
\"append\"}, "
+                    + "\"schema-id\": %d, \"first-row-id\": %d, 
\"added-rows\": 0, "
+                    + "\"root-manifest\": 
\"file:/nonexistent/root-manifest.parquet\"}",
+                base.lastSequenceNumber() + 1,
+                parent.snapshotId() + 1,
+                parent.snapshotId(),
+                parent.timestampMillis() + 1,
+                base.currentSchemaId(),
+                base.nextRowId()));
+    ops.commit(
+        base,
+        TableMetadata.buildFrom(base)
+            .setBranchSnapshot(rootManifestSnapshot, SnapshotRef.MAIN_BRANCH)
+            .build());
+  }
+
   private String tableLocation() {
     return tableDir.toURI().toString();
   }
diff --git 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
index ef246c7a61..d520f350d2 100644
--- 
a/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
+++ 
b/spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
@@ -115,7 +115,7 @@ public class TestRewriteTablePathsAction extends TestBase {
 
   @Parameters(name = "formatVersion = {0}")
   protected static List<Integer> formatVersions() {
-    return TestHelpers.V2_AND_V3;
+    return TestHelpers.V2_AND_ABOVE;
   }
 
   @Parameter private int formatVersion;

Reply via email to