Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Don't reset snapshotLog in TableMetadata.removeRef method #11779

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2152,6 +2153,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