Skip to content

Commit

Permalink
Core: Don't clear snapshotLog in TableMetadata.removeRef (#11779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr authored Dec 24, 2024
1 parent d6d3cf5 commit 1b5886d
Show file tree
Hide file tree
Showing 3 changed files with 52 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
34 changes: 34 additions & 0 deletions core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.iceberg.FileScanTask;
import org.apache.iceberg.FilesTable;
import org.apache.iceberg.HasTableOperations;
import org.apache.iceberg.HistoryEntry;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.ReachableFileUtil;
import org.apache.iceberg.ReplaceSortOrder;
Expand Down Expand Up @@ -2151,6 +2152,39 @@ public void testReplaceTransactionRequiresTableExists() {
.hasMessageStartingWith("Table does not exist: newdb.table");
}

@Test
public void testReplaceTableKeepsSnapshotLog() {
C catalog = catalog();

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);
HistoryEntry snapshotBeforeReplace = snapshotLogBeforeReplace.get(0);

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

List<HistoryEntry> snapshotLogAfterReplace =
((BaseTable) table).operations().current().snapshotLog();
HistoryEntry snapshotAfterReplace = snapshotLogAfterReplace.get(1);

assertThat(snapshotAfterReplace).isNotEqualTo(snapshotBeforeReplace);
assertThat(snapshotLogAfterReplace)
.hasSize(2)
.containsExactly(snapshotBeforeReplace, snapshotAfterReplace);
}

@Test
public void testConcurrentReplaceTransactions() {
C catalog = catalog();
Expand Down

0 comments on commit 1b5886d

Please sign in to comment.