Skip to content

Commit

Permalink
REST: Don't reset snapshotLog in TableMetadata.removeRef
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Dec 23, 2024
1 parent a3dcfd1 commit 9c2ca14
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/src/main/java/org/apache/iceberg/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,6 @@ public Builder setRef(String name, SnapshotRef ref) {
public Builder removeRef(String name) {
if (SnapshotRef.MAIN_BRANCH.equals(name)) {
this.currentSnapshotId = -1;
snapshotLog.clear();
}

SnapshotRef ref = refs.remove(name);
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestTableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,24 @@ public void buildReplacementKeepsSnapshotLog() throws Exception {
.containsExactlyElementsOf(metadata.snapshotLog());
}

@Test
public void removeRefKeepsSnapshotLog() throws Exception {
TableMetadata metadata =
TableMetadataParser.fromJson(readTableMetadataInputFile("TableMetadataV2Valid.json"));
assertThat(metadata.currentSnapshot()).isNotNull();
assertThat(metadata.snapshots()).hasSize(2);
assertThat(metadata.snapshotLog()).hasSize(2);

TableMetadata removeRef =
TableMetadata.buildFrom(metadata).removeRef(SnapshotRef.MAIN_BRANCH).build();

assertThat(removeRef.currentSnapshot()).isNull();
assertThat(removeRef.snapshots()).hasSize(2).containsExactlyElementsOf(metadata.snapshots());
assertThat(removeRef.snapshotLog())
.hasSize(2)
.containsExactlyElementsOf(metadata.snapshotLog());
}

@Test
public void testConstructV3Metadata() {
TableMetadata.newTableMetadata(
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/org/apache/iceberg/rest/TestRESTCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.apache.hadoop.conf.Configuration;
import org.apache.iceberg.BaseTable;
import org.apache.iceberg.BaseTransaction;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DataFiles;
import org.apache.iceberg.HistoryEntry;
import org.apache.iceberg.MetadataUpdate;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
Expand Down Expand Up @@ -2418,6 +2421,34 @@ public void testPaginationForListTables(int numberOfItems) {
eq(ListTablesResponse.class));
}

@Test
public void testReplaceTableKeepsSnapshotLog() {
RESTCatalogAdapter adapter = Mockito.spy(new RESTCatalogAdapter(backendCatalog));
RESTCatalog catalog = catalog(adapter);

if (requiresNamespaceCreate()) {
catalog.createNamespace(TABLE.namespace());
}

catalog.createTable(TABLE, SCHEMA);

Table table = catalog.loadTable(TABLE);
table.newAppend().appendFile(FILE_A).commit();

List<HistoryEntry> snapshotLogBeforeReplace =
((BaseTable) table).operations().current().snapshotLog();
assertThat(snapshotLogBeforeReplace).hasSize(1);

Transaction replaceTableTransaction = catalog.newReplaceTableTransaction(TABLE, SCHEMA, false);
replaceTableTransaction.newAppend().appendFile(FILE_A).commit();
replaceTableTransaction.commitTransaction();

table.refresh();
assertThat(((BaseTable) table).operations().current().snapshotLog())
.hasSize(2)
.containsAll(snapshotLogBeforeReplace);
}

@Test
public void testCleanupUncommitedFilesForCleanableFailures() {
RESTCatalogAdapter adapter = Mockito.spy(new RESTCatalogAdapter(backendCatalog));
Expand Down

0 comments on commit 9c2ca14

Please sign in to comment.