Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#268] (bugfix) Unable to get explicit catalog identifier in the NameIdentifier (#273)

Xun Liu0f0cf4d375ba
diff --git a/api/src/main/java/com/datastrato/graviton/NameIdentifier.java b/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
index bd5abfbb29..0bbe08fd87 100644
--- a/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
+++ b/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
@@ -197,7 +197,7 @@ public class NameIdentifier {
}
}
- private static void check(boolean condition, String message, Object... args) {
+ public static void check(boolean condition, String message, Object... args) {
if (!condition) {
throw new IllegalNameIdentifierException(String.format(message, args));
}
diff --git a/api/src/main/java/com/datastrato/graviton/Namespace.java b/api/src/main/java/com/datastrato/graviton/Namespace.java
index 2881cd87fb..53241a8316 100644
--- a/api/src/main/java/com/datastrato/graviton/Namespace.java
+++ b/api/src/main/java/com/datastrato/graviton/Namespace.java
@@ -183,7 +183,7 @@ public class Namespace {
return DOT.join(levels);
}
- private static void check(boolean expression, String message, Object... args) {
+ public static void check(boolean expression, String message, Object... args) {
if (!expression) {
throw new IllegalNamespaceException(String.format(message, args));
}
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
index 30a488ec06..efbfb1657c 100644
--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java
@@ -6,6 +6,7 @@ package com.datastrato.graviton.catalog;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
import com.datastrato.graviton.exceptions.NoSuchSchemaException;
import com.datastrato.graviton.exceptions.NoSuchTableException;
@@ -20,7 +21,12 @@ import com.datastrato.graviton.rel.Table;
import com.datastrato.graviton.rel.TableCatalog;
import com.datastrato.graviton.rel.TableChange;
import com.datastrato.graviton.util.ThrowableFunction;
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Arrays;
+import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
* A catalog operation dispatcher that dispatches the catalog operations to the underlying catalog
@@ -251,7 +257,9 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
Class<E2> ex2)
throws E1, E2 {
try {
- CatalogManager.CatalogWrapper c = catalogManager.loadCatalogAndWrap(ident);
+ NameIdentifier catalogIdent = getCatalogIdentifier(ident);
+
+ CatalogManager.CatalogWrapper c = catalogManager.loadCatalogAndWrap(catalogIdent);
return fn.apply(c);
} catch (Throwable throwable) {
if (ex1.isInstance(throwable)) {
@@ -263,4 +271,26 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemas
throw new RuntimeException(throwable);
}
}
+
+ @VisibleForTesting
+ // TODO(xun): Remove this method when we implement a better way to get the catalog identifier
+ // [#257] Add an explicit get catalog functions in NameIdentifier
+ NameIdentifier getCatalogIdentifier(NameIdentifier ident) {
+ NameIdentifier.check(
+ ident.name() != null, "The name variable in the NameIdentifier must have value.");
+ Namespace.check(
+ ident.namespace() != null && ident.namespace().length() > 0,
+ String.format(
+ "Catalog namespace must be non-null and have 1 level, the input namespace is %s",
+ ident.namespace()));
+
+ List<String> allElems =
+ Stream.concat(Arrays.stream(ident.namespace().levels()), Stream.of(ident.name()))
+ .collect(Collectors.toList());
+ if (allElems.size() < 2) {
+ throw new IllegalNameIdentifierException(
+ "Cannot create a catalog NameIdentifier less than two elements.");
+ }
+ return NameIdentifier.of(allElems.get(0), allElems.get(1));
+ }
}
diff --git a/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java b/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java
new file mode 100644
index 0000000000..fb7213ee2b
--- /dev/null
+++ b/core/src/test/java/com/datastrato/graviton/catalog/TestCatalogOperationDispatcher.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.catalog;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import com.datastrato.graviton.NameIdentifier;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
+import org.junit.jupiter.api.Test;
+
+public class TestCatalogOperationDispatcher {
+ @Test
+ public void testGetCatalogIdentifier() {
+ CatalogOperationDispatcher dispatcher = new CatalogOperationDispatcher(null);
+
+ NameIdentifier id1 = NameIdentifier.of("a");
+ assertThrows(IllegalNamespaceException.class, () -> dispatcher.getCatalogIdentifier(id1));
+
+ NameIdentifier id2 = NameIdentifier.of("a", "b");
+ assertEquals(dispatcher.getCatalogIdentifier(id2), NameIdentifier.of("a", "b"));
+
+ NameIdentifier id3 = NameIdentifier.of("a", "b", "c");
+ assertEquals(dispatcher.getCatalogIdentifier(id3), NameIdentifier.of("a", "b"));
+
+ NameIdentifier id4 = NameIdentifier.of("a", "b", "c", "d");
+ assertEquals(dispatcher.getCatalogIdentifier(id4), NameIdentifier.of("a", "b"));
+
+ NameIdentifier id5 = NameIdentifier.of("a", "b", "c", "d", "e");
+ assertEquals(dispatcher.getCatalogIdentifier(id5), NameIdentifier.of("a", "b"));
+ }
+}