This is an automated email from the ASF dual-hosted git repository.
amogh-jahagirdar 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 c832d4c9a5 Core: Reject ExpireSnapshots file cleanup for snapshots
with root manifests (#19006)
c832d4c9a5 is described below
commit c832d4c9a5c3d5bd9bd98bc82ed39bc52fed9d8b
Author: Anoop Johnson <[email protected]>
AuthorDate: Wed Nov 18 05:56:15 2026 -0800
Core: Reject ExpireSnapshots file cleanup for snapshots with root manifests
(#19006)
---
.../java/org/apache/iceberg/RemoveSnapshots.java | 10 ++
.../org/apache/iceberg/TestRemoveSnapshotsV4.java | 121 +++++++++++++++++++++
2 files changed, 131 insertions(+)
diff --git a/core/src/main/java/org/apache/iceberg/RemoveSnapshots.java
b/core/src/main/java/org/apache/iceberg/RemoveSnapshots.java
index dbccdb86ed..518fccf48f 100644
--- a/core/src/main/java/org/apache/iceberg/RemoveSnapshots.java
+++ b/core/src/main/java/org/apache/iceberg/RemoveSnapshots.java
@@ -195,6 +195,11 @@ class RemoveSnapshots implements ExpireSnapshots {
private TableMetadata internalApply() {
this.base = ops.refresh();
+ ValidationException.check(
+ CleanupLevel.NONE == cleanupLevel || !hasRootManifestSnapshot(base),
+ "File cleanup for V4 tables is not supported. "
+ + "Use CleanupLevel.NONE to cleanup snapshot metadata.");
+
// attempt to clean expired metadata even if there are no snapshots to
expire
// table metadata builder takes care of the case when this should actually
be a no-op
if (base.snapshots().isEmpty() && !cleanExpiredMetadata) {
@@ -472,4 +477,9 @@ class RemoveSnapshots implements ExpireSnapshots {
return ancestors;
}
+
+ private static boolean hasRootManifestSnapshot(TableMetadata metadata) {
+ return metadata.snapshots().stream()
+ .anyMatch(snapshot -> snapshot.rootManifestLocation() != null);
+ }
}
diff --git a/core/src/test/java/org/apache/iceberg/TestRemoveSnapshotsV4.java
b/core/src/test/java/org/apache/iceberg/TestRemoveSnapshotsV4.java
new file mode 100644
index 0000000000..23ae9fa2f7
--- /dev/null
+++ b/core/src/test/java/org/apache/iceberg/TestRemoveSnapshotsV4.java
@@ -0,0 +1,121 @@
+/*
+ * 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 java.util.Set;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+class TestRemoveSnapshotsV4 {
+ private static final String CLEANUP_UNSUPPORTED_MESSAGE =
+ "File cleanup for V4 tables is not supported. "
+ + "Use CleanupLevel.NONE to cleanup snapshot metadata.";
+ private static final long ROOT_MANIFEST_SNAPSHOT_ID = 42L;
+ private static final long FAR_FUTURE_MS = System.currentTimeMillis() +
100_000;
+
+ @TempDir private File tableDir;
+
+ @AfterEach
+ void clearTables() {
+ TestTables.clearTables();
+ }
+
+ @ParameterizedTest
+ @EnumSource(
+ value = ExpireSnapshots.CleanupLevel.class,
+ mode = EnumSource.Mode.EXCLUDE,
+ names = "NONE")
+ void rejectsFileCleanupWithRootManifestSnapshot(ExpireSnapshots.CleanupLevel
cleanupLevel) {
+ Table table = tableWithRootManifestSnapshot();
+
+ assertThatThrownBy(
+ () ->
+ table
+ .expireSnapshots()
+ .expireOlderThan(FAR_FUTURE_MS)
+ .retainLast(1)
+ .cleanupLevel(cleanupLevel)
+ .commit())
+ .isInstanceOf(ValidationException.class)
+ .hasMessage(CLEANUP_UNSUPPORTED_MESSAGE);
+
+ table.refresh();
+ assertThat(table.snapshots()).as("no snapshot should be
expired").hasSize(2);
+ }
+
+ @Test
+ void allowsMetadataOnlyRemovalWithRootManifestSnapshot() {
+ Table table = tableWithRootManifestSnapshot();
+ Set<String> deletedFiles = Sets.newHashSet();
+
+ table
+ .expireSnapshots()
+ .expireOlderThan(FAR_FUTURE_MS)
+ .retainLast(1)
+ .cleanupLevel(ExpireSnapshots.CleanupLevel.NONE)
+ .deleteWith(deletedFiles::add)
+ .commit();
+
+ table.refresh();
+ assertThat(table.snapshots()).as("one snapshot should be
expired").hasSize(1);
+ assertThat(deletedFiles).as("no files should be deleted").isEmpty();
+ }
+
+ private Table tableWithRootManifestSnapshot() {
+ Table table =
+ TestTables.create(
+ tableDir, "test", TestBase.SCHEMA, TestBase.SPEC,
SortOrder.unsorted(), 4);
+ table.newAppend().appendFile(TestBase.FILE_A).commit();
+
+ 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, SnapshotRef.MAIN_BRANCH)
+ .build());
+ table.refresh();
+ return table;
+ }
+}