From 24578a28fe69db96da460ac49eeb1a60fee7b8c7 Mon Sep 17 00:00:00 2001 From: Ajantha Bhat Date: Sun, 17 Dec 2023 00:18:55 +0530 Subject: [PATCH] Core: Fix metadata table uuid to return a consistent UUID for the same reference (#9310) --- .../java/org/apache/iceberg/BaseMetadataTable.java | 4 +++- .../org/apache/iceberg/TestMetadataTableScans.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java b/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java index 124115496b3e..4565b898c43d 100644 --- a/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java +++ b/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java @@ -44,6 +44,7 @@ public abstract class BaseMetadataTable extends BaseReadOnlyTable private final SortOrder sortOrder = SortOrder.unsorted(); private final BaseTable table; private final String name; + private final UUID uuid; protected BaseMetadataTable(Table table, String name) { super("metadata"); @@ -51,6 +52,7 @@ protected BaseMetadataTable(Table table, String name) { table instanceof BaseTable, "Cannot create metadata table for non-data table: %s", table); this.table = (BaseTable) table; this.name = name; + this.uuid = UUID.randomUUID(); } /** @@ -202,7 +204,7 @@ public Map refs() { @Override public UUID uuid() { - return UUID.randomUUID(); + return uuid; } @Override diff --git a/core/src/test/java/org/apache/iceberg/TestMetadataTableScans.java b/core/src/test/java/org/apache/iceberg/TestMetadataTableScans.java index 9da1838ad2e4..aa1da6b182dc 100644 --- a/core/src/test/java/org/apache/iceberg/TestMetadataTableScans.java +++ b/core/src/test/java/org/apache/iceberg/TestMetadataTableScans.java @@ -43,6 +43,7 @@ import org.apache.iceberg.relocated.com.google.common.collect.Streams; import org.apache.iceberg.types.Types; import org.apache.iceberg.util.StructLikeWrapper; +import org.assertj.core.api.Assertions; import org.junit.Assert; import org.junit.Assume; import org.junit.Test; @@ -138,6 +139,18 @@ public void testManifestsTableAlwaysIgnoresResiduals() throws IOException { } } + @Test + public void testMetadataTableUUID() { + Table manifestsTable = new ManifestsTable(table); + + Assertions.assertThat(manifestsTable.uuid()) + .as("UUID should be consistent on multiple calls") + .isEqualTo(manifestsTable.uuid()); + Assertions.assertThat(manifestsTable.uuid()) + .as("Metadata table UUID should be different from the base table UUID") + .isNotEqualTo(table.uuid()); + } + @Test public void testDataFilesTableWithDroppedPartition() throws IOException { table.newFastAppend().appendFile(FILE_A).appendFile(FILE_B).commit();