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 56b0034acf Spark 4.2: Reject ExpireSnapshots action only for tables
with root manifests (#19012)
56b0034acf is described below
commit 56b0034acf15176298651e4796678cf9f408b4a2
Author: Anoop Johnson <[email protected]>
AuthorDate: Tue Nov 17 11:26:38 2026 -0800
Spark 4.2: Reject ExpireSnapshots action only for tables with root
manifests (#19012)
Loosen the guard added in #18999 so that the Spark ExpireSnapshots action
rejects only tables that have a snapshot with a root manifest, instead of
all v4 tables. This matches the check used by core RemoveSnapshots
(#19006) and the manifest-reading metadata tables (#19008).
v4 tables without root manifests are handled like v3 tables again, so
TestExpireSnapshotsAction and TestRewriteTablePathsAction go back to
V2_AND_ABOVE.
Addresses review feedback:
https://github.com/apache/iceberg/pull/19006#discussion_r4594288653
---
.../spark/actions/ExpireSnapshotsSparkAction.java | 9 ++--
.../spark/actions/TestExpireSnapshotsAction.java | 2 +-
.../spark/actions/TestExpireSnapshotsActionV4.java | 63 +++++++++++++++-------
.../spark/actions/TestRewriteTablePathsAction.java | 2 +-
4 files changed, 50 insertions(+), 26 deletions(-)
diff --git
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
index 9770b3caf0..c42fe08485 100644
---
a/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
+++
b/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/actions/ExpireSnapshotsSparkAction.java
@@ -74,8 +74,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;
@@ -156,11 +154,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.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
index b766cff29f..61857cdf35 100644
---
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
+++
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsAction.java
@@ -134,7 +134,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.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
index bc08ae66ce..9252fd560b 100644
---
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestExpireSnapshotsActionV4.java
+++
b/spark/v4.2/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.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
b/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
index ef246c7a61..d520f350d2 100644
---
a/spark/v4.2/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java
+++
b/spark/v4.2/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;