From fdac95359f44f0fc87c64e052a9daade704c0a86 Mon Sep 17 00:00:00 2001 From: amogh-jahagirdar Date: Wed, 11 Oct 2023 03:40:58 -0700 Subject: [PATCH] API, Core: Add uuid API to Table --- api/src/main/java/org/apache/iceberg/Table.java | 10 ++++++++++ .../java/org/apache/iceberg/BaseMetadataTable.java | 6 ++++++ core/src/main/java/org/apache/iceberg/BaseTable.java | 6 ++++++ .../main/java/org/apache/iceberg/BaseTransaction.java | 6 ++++++ .../java/org/apache/iceberg/SerializableTable.java | 8 ++++++++ .../java/org/apache/iceberg/catalog/CatalogTests.java | 3 +++ 6 files changed, 39 insertions(+) diff --git a/api/src/main/java/org/apache/iceberg/Table.java b/api/src/main/java/org/apache/iceberg/Table.java index 02db808417a4..5ab1def51ca0 100644 --- a/api/src/main/java/org/apache/iceberg/Table.java +++ b/api/src/main/java/org/apache/iceberg/Table.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; +import java.util.UUID; import org.apache.iceberg.encryption.EncryptionManager; import org.apache.iceberg.io.FileIO; import org.apache.iceberg.io.LocationProvider; @@ -333,6 +334,15 @@ default UpdateStatistics updateStatistics() { */ Map refs(); + /** + * Returns the UUID of the table + * + * @return the UUID of the table + */ + default UUID uuid() { + throw new UnsupportedOperationException(this.getClass().getName() + " doesn't implement uuid"); + } + /** * Returns the snapshot referenced by the given name or null if no such reference exists. * diff --git a/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java b/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java index 5f7c48e95867..124115496b3e 100644 --- a/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java +++ b/core/src/main/java/org/apache/iceberg/BaseMetadataTable.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; import org.apache.iceberg.encryption.EncryptionManager; import org.apache.iceberg.io.FileIO; @@ -199,6 +200,11 @@ public Map refs() { return table().refs(); } + @Override + public UUID uuid() { + return UUID.randomUUID(); + } + @Override public String toString() { return name(); diff --git a/core/src/main/java/org/apache/iceberg/BaseTable.java b/core/src/main/java/org/apache/iceberg/BaseTable.java index 2093753bf755..d4cf1f4b76f4 100644 --- a/core/src/main/java/org/apache/iceberg/BaseTable.java +++ b/core/src/main/java/org/apache/iceberg/BaseTable.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.util.List; import java.util.Map; +import java.util.UUID; import org.apache.iceberg.encryption.EncryptionManager; import org.apache.iceberg.io.FileIO; import org.apache.iceberg.io.LocationProvider; @@ -259,6 +260,11 @@ public Map refs() { return ops.current().refs(); } + @Override + public UUID uuid() { + return UUID.fromString(ops.current().uuid()); + } + @Override public String toString() { return name(); diff --git a/core/src/main/java/org/apache/iceberg/BaseTransaction.java b/core/src/main/java/org/apache/iceberg/BaseTransaction.java index c1ebd554befa..32cf695c8b5a 100644 --- a/core/src/main/java/org/apache/iceberg/BaseTransaction.java +++ b/core/src/main/java/org/apache/iceberg/BaseTransaction.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.function.Consumer; import java.util.stream.Collectors; import org.apache.iceberg.encryption.EncryptionManager; @@ -770,6 +771,11 @@ public Map refs() { return current.refs(); } + @Override + public UUID uuid() { + return UUID.fromString(current.uuid()); + } + @Override public String toString() { return name(); diff --git a/core/src/main/java/org/apache/iceberg/SerializableTable.java b/core/src/main/java/org/apache/iceberg/SerializableTable.java index cf8e1b3fbaa7..c5085b1313c9 100644 --- a/core/src/main/java/org/apache/iceberg/SerializableTable.java +++ b/core/src/main/java/org/apache/iceberg/SerializableTable.java @@ -21,6 +21,7 @@ import java.io.Serializable; import java.util.List; import java.util.Map; +import java.util.UUID; import org.apache.hadoop.conf.Configuration; import org.apache.iceberg.encryption.EncryptionManager; import org.apache.iceberg.hadoop.HadoopConfigurable; @@ -67,6 +68,7 @@ public class SerializableTable implements Table, Serializable { private transient volatile Schema lazySchema = null; private transient volatile Map lazySpecs = null; private transient volatile SortOrder lazySortOrder = null; + private final UUID uuid; protected SerializableTable(Table table) { this.name = table.name(); @@ -83,6 +85,7 @@ protected SerializableTable(Table table) { this.encryption = table.encryption(); this.locationProvider = table.locationProvider(); this.refs = SerializableMap.copyOf(table.refs()); + this.uuid = table.uuid(); } /** @@ -247,6 +250,11 @@ public Map refs() { return refs; } + @Override + public UUID uuid() { + return uuid; + } + @Override public void refresh() { throw new UnsupportedOperationException(errorMsg("refresh")); diff --git a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java index 835115ad4d9c..e01deebf5bc5 100644 --- a/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java +++ b/core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.stream.Collectors; import org.apache.iceberg.AppendFiles; import org.apache.iceberg.BaseTable; @@ -634,6 +635,8 @@ public void testCompleteCreateTable() { Assertions.assertThat(table.properties().entrySet()) .as("Table properties should be a superset of the requested properties") .containsAll(properties.entrySet()); + Assertions.assertThat(table.uuid()) + .isEqualTo(UUID.fromString(((BaseTable) table).operations().current().uuid())); } @Test