diff --git a/nessie/src/main/java/org/apache/iceberg/nessie/NessieTableOperations.java b/nessie/src/main/java/org/apache/iceberg/nessie/NessieTableOperations.java index ebc294b6031f..ab781b16591f 100644 --- a/nessie/src/main/java/org/apache/iceberg/nessie/NessieTableOperations.java +++ b/nessie/src/main/java/org/apache/iceberg/nessie/NessieTableOperations.java @@ -89,7 +89,8 @@ private TableMetadata loadTableMetadata(String metadataLocation) { TableMetadata.Builder builder = TableMetadata.buildFrom(TableMetadataParser.read(io(), metadataLocation)) .setCurrentSchema(table.getSchemaId()) .setDefaultSortOrder(table.getSortOrderId()) - .setDefaultPartitionSpec(table.getSpecId()); + .setDefaultPartitionSpec(table.getSpecId()) + .withMetadataLocation(metadataLocation); if (table.getSnapshotId() != -1) { builder.setBranchSnapshot(table.getSnapshotId(), SnapshotRef.MAIN_BRANCH); } diff --git a/nessie/src/test/java/org/apache/iceberg/nessie/TestBranchVisibility.java b/nessie/src/test/java/org/apache/iceberg/nessie/TestBranchVisibility.java index 64bd6660be31..3a8b40d7b2b6 100644 --- a/nessie/src/test/java/org/apache/iceberg/nessie/TestBranchVisibility.java +++ b/nessie/src/test/java/org/apache/iceberg/nessie/TestBranchVisibility.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.Map; import org.apache.avro.generic.GenericRecordBuilder; +import org.apache.iceberg.BaseTable; import org.apache.iceberg.DataFile; import org.apache.iceberg.Snapshot; import org.apache.iceberg.Table; @@ -173,6 +174,32 @@ public void testSchemaSnapshot() throws Exception { Assertions.assertThat(metadataOn2).isNotEqualTo(metadataOnTest).isNotEqualTo(metadataOnTest2); } + @Test + public void testMetadataLocation() throws Exception { + String branch1 = "test"; + String branch2 = "branch-2"; + + // commit on tableIdentifier1 on branch1 + NessieCatalog catalog = initCatalog(branch1); + String metadataLocationOfCommit1 = addRow(catalog, tableIdentifier1, "initial-data", + ImmutableMap.of("id0", 4L)); + + createBranch(branch2, catalog.currentHash(), branch1); + // commit on tableIdentifier1 on branch2 + catalog = initCatalog(branch2); + String metadataLocationOfCommit2 = addRow(catalog, tableIdentifier1, "some-more-data", + ImmutableMap.of("id0", 42L)); + Assertions.assertThat(metadataLocationOfCommit2).isNotNull().isNotEqualTo(metadataLocationOfCommit1); + + catalog = initCatalog(branch1); + // load tableIdentifier1 on branch1 + BaseTable table = (BaseTable) catalog.loadTable(tableIdentifier1); + // branch1's tableIdentifier1's metadata location + // should be the latest global state (aka commit2 from branch2) + Assertions.assertThat(table.operations().current().metadataFileLocation()) + .isNotNull().isEqualTo(metadataLocationOfCommit2); + } + /** * Complex-ish test case that verifies that both the snapshot-ID and schema-ID are properly set * and retained when working with a mixture of DDLs and DMLs across multiple branches.