Skip to content

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

[#14825] feat(java): Expose Semantic Model dataset lookup on sources (#14826)

mchadesd1ccec59e6b0
diff --git a/api/src/main/java/org/apache/gravitino/rel/Table.java b/api/src/main/java/org/apache/gravitino/rel/Table.java
index decfa80d9b..0154811bfe 100644
--- a/api/src/main/java/org/apache/gravitino/rel/Table.java
+++ b/api/src/main/java/org/apache/gravitino/rel/Table.java
@@ -34,6 +34,7 @@ import org.apache.gravitino.rel.expressions.transforms.Transforms;
import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.rel.indexes.Indexes;
import org.apache.gravitino.secret.SupportsSecrets;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
import org.apache.gravitino.stats.SupportsPartitionStatistics;
import org.apache.gravitino.stats.SupportsStatistics;
import org.apache.gravitino.tag.SupportsTags;
@@ -202,4 +203,14 @@ public interface Table extends Auditable {
throw new UnsupportedOperationException(
"Table does not support partition statistics operations.");
}
+
+ /**
+ * Returns the Semantic Model dataset lookup operations for this source.
+ *
+ * @return The source-scoped Semantic Model dataset lookup capability.
+ * @throws UnsupportedOperationException If lookup is not supported by this implementation.
+ */
+ default SupportsSemanticModels supportsSemanticModels() {
+ throw new UnsupportedOperationException("Semantic Model dataset lookup is not supported");
+ }
}
diff --git a/api/src/main/java/org/apache/gravitino/rel/View.java b/api/src/main/java/org/apache/gravitino/rel/View.java
index 23146096b5..e45396836e 100644
--- a/api/src/main/java/org/apache/gravitino/rel/View.java
+++ b/api/src/main/java/org/apache/gravitino/rel/View.java
@@ -26,6 +26,7 @@ import org.apache.gravitino.Auditable;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Unstable;
import org.apache.gravitino.secret.SupportsSecrets;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
import org.apache.gravitino.tag.SupportsTags;
/**
@@ -141,4 +142,14 @@ public interface View extends Auditable {
default SupportsSecrets supportsSecrets() {
throw new UnsupportedOperationException("View does not support secret property operations.");
}
+
+ /**
+ * Returns the Semantic Model dataset lookup operations for this source.
+ *
+ * @return The source-scoped Semantic Model dataset lookup capability.
+ * @throws UnsupportedOperationException If lookup is not supported by this implementation.
+ */
+ default SupportsSemanticModels supportsSemanticModels() {
+ throw new UnsupportedOperationException("Semantic Model dataset lookup is not supported");
+ }
}
diff --git a/api/src/main/java/org/apache/gravitino/semantic/SupportsSemanticModels.java b/api/src/main/java/org/apache/gravitino/semantic/SupportsSemanticModels.java
new file mode 100644
index 0000000000..ad7fda0de2
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/semantic/SupportsSemanticModels.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.semantic;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.annotation.Evolving;
+import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
+
+/** Source-scoped discovery of datasets contained in Semantic Models. */
+@Evolving
+public interface SupportsSemanticModels {
+ /**
+ * Lists datasets directly associated with this source from models the caller can load.
+ *
+ * <p>Each identifier has namespace {@code [metalake, catalog, schema, semanticModel]} and the
+ * exact dataset name, which may contain literal dots. Results may span catalogs in the source's
+ * metalake. Results are sorted lexicographically by namespace components, then by dataset name,
+ * using case-sensitive {@link String#compareTo(String)} order for each component.
+ *
+ * <p>For a returned {@code dataset}, {@code NameIdentifier.of(dataset.namespace().levels())}
+ * identifies its containing model.
+ *
+ * <p>Associations record the sources resolved at the latest definition write. They do not
+ * establish whether a model can currently execute, and do not include transitive dependencies.
+ *
+ * @return Fully qualified dataset identifiers, or an empty array when no matches are visible.
+ * @throws NoSuchMetadataObjectException If the source does not exist.
+ */
+ NameIdentifier[] listSemanticModelDatasets() throws NoSuchMetadataObjectException;
+}
diff --git a/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java b/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
index 35cb952990..55fcf3c703 100644
--- a/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
+++ b/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
@@ -1532,6 +1532,9 @@ public class ErrorHandlers {
throw new IllegalArgumentException(errorMessage);
case ErrorConstants.NOT_FOUND_CODE:
+ if (NoSuchMetadataObjectException.class.getSimpleName().equals(errorResponse.getType())) {
+ throw new NoSuchMetadataObjectException(errorMessage);
+ }
if (NoSuchMetalakeException.class.getSimpleName().equals(errorResponse.getType())) {
throw new NoSuchMetalakeException(errorMessage);
} else if (NoSuchCatalogException.class.getSimpleName().equals(errorResponse.getType())) {
diff --git a/clients/client-java/src/main/java/org/apache/gravitino/client/GenericView.java b/clients/client-java/src/main/java/org/apache/gravitino/client/GenericView.java
index f4b1e9e935..c092d7b129 100644
--- a/clients/client-java/src/main/java/org/apache/gravitino/client/GenericView.java
+++ b/clients/client-java/src/main/java/org/apache/gravitino/client/GenericView.java
@@ -32,6 +32,7 @@ import org.apache.gravitino.rel.Column;
import org.apache.gravitino.rel.Representation;
import org.apache.gravitino.rel.View;
import org.apache.gravitino.secret.SupportsSecrets;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
import org.apache.gravitino.tag.SupportsTags;
import org.apache.gravitino.tag.Tag;
import org.apache.gravitino.tag.TagList;
@@ -43,6 +44,7 @@ class GenericView implements View, SupportsTags, SupportsSecrets {
private final ViewDTO viewDTO;
private final MetadataObjectTagOperations objectTagOperations;
+ private final MetadataObjectSemanticModelOperations objectSemanticModelOperations;
private final MetadataObjectSecretOperations objectSecretOperations;
GenericView(ViewDTO viewDTO, RESTClient restClient, Namespace viewNs) {
@@ -50,6 +52,8 @@ class GenericView implements View, SupportsTags, SupportsSecrets {
List<String> viewFullName =
Lists.newArrayList(viewNs.level(1), viewNs.level(2), viewDTO.name());
MetadataObject viewObject = MetadataObjects.of(viewFullName, MetadataObject.Type.VIEW);
+ this.objectSemanticModelOperations =
+ new MetadataObjectSemanticModelOperations(viewNs.level(0), viewObject, restClient);
this.objectTagOperations =
new MetadataObjectTagOperations(viewNs.level(0), viewObject, restClient);
this.objectSecretOperations =
@@ -96,6 +100,11 @@ class GenericView implements View, SupportsTags, SupportsSecrets {
return viewDTO.properties();
}
+ @Override
+ public SupportsSemanticModels supportsSemanticModels() {
+ return objectSemanticModelOperations;
+ }
+
@Override
public SupportsTags supportsTags() {
return this;
diff --git a/clients/client-java/src/main/java/org/apache/gravitino/client/MetadataObjectSemanticModelOperations.java b/clients/client-java/src/main/java/org/apache/gravitino/client/MetadataObjectSemanticModelOperations.java
new file mode 100644
index 0000000000..c79b9cb646
--- /dev/null
+++ b/clients/client-java/src/main/java/org/apache/gravitino/client/MetadataObjectSemanticModelOperations.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.client;
+
+import java.util.Collections;
+import java.util.Locale;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.dto.responses.EntityListResponse;
+import org.apache.gravitino.rest.RESTUtils;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
+
+/** Shared relationship request handling for tables and Views. */
+class MetadataObjectSemanticModelOperations implements SupportsSemanticModels {
+ private final RESTClient restClient;
+ private final String requestPath;
+
+ MetadataObjectSemanticModelOperations(
+ String metalake, MetadataObject source, RESTClient restClient) {
+ this.restClient = restClient;
+ this.requestPath =
+ String.format(
+ "api/metalakes/%s/objects/%s/%s/semantic-model-datasets",
+ RESTUtils.encodeString(metalake),
+ source.type().name().toLowerCase(Locale.ROOT),
+ RESTUtils.encodeString(source.fullName()));
+ }
+
+ @Override
+ public NameIdentifier[] listSemanticModelDatasets() {
+ EntityListResponse response =
+ restClient.get(
+ requestPath,
+ EntityListResponse.class,
+ Collections.emptyMap(),
+ ErrorHandlers.semanticModelErrorHandler());
+ response.validate();
+ return response.identifiers();
+ }
+}
diff --git a/clients/client-java/src/main/java/org/apache/gravitino/client/RelationalTable.java b/clients/client-java/src/main/java/org/apache/gravitino/client/RelationalTable.java
index b2eb7b627c..00e6eb9eb0 100644
--- a/clients/client-java/src/main/java/org/apache/gravitino/client/RelationalTable.java
+++ b/clients/client-java/src/main/java/org/apache/gravitino/client/RelationalTable.java
@@ -59,6 +59,7 @@ import org.apache.gravitino.rel.indexes.Index;
import org.apache.gravitino.rel.partitions.Partition;
import org.apache.gravitino.rest.RESTUtils;
import org.apache.gravitino.secret.SupportsSecrets;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
import org.apache.gravitino.stats.PartitionRange;
import org.apache.gravitino.stats.PartitionStatistics;
import org.apache.gravitino.stats.PartitionStatisticsDrop;
@@ -90,6 +91,7 @@ class RelationalTable
private final Namespace namespace;
private final MetadataObjectTagOperations objectTagOperations;
+ private final MetadataObjectSemanticModelOperations objectSemanticModelOperations;
private final MetadataObjectRoleOperations objectRoleOperations;
private final MetadataObjectPolicyOperations objectPolicyOperations;
private final MetadataObjectStatisticsOperations objectStatisticsOperations;
@@ -123,6 +125,8 @@ class RelationalTable
MetadataObjects.of(
Arrays.asList(namespace.level(1), namespace.level(2), tableDTO.name()),
MetadataObject.Type.TABLE);
+ this.objectSemanticModelOperations =
+ new MetadataObjectSemanticModelOperations(namespace.level(0), tableObject, restClient);
this.objectTagOperations =
new MetadataObjectTagOperations(namespace.level(0), tableObject, restClient);
this.objectRoleOperations =
@@ -353,6 +357,11 @@ class RelationalTable
return prefix + "/" + RESTUtils.encodeString(partitionName);
}
+ @Override
+ public SupportsSemanticModels supportsSemanticModels() {
+ return objectSemanticModelOperations;
+ }
+
@Override
public SupportsTags supportsTags() {
return this;
diff --git a/clients/client-java/src/test/java/org/apache/gravitino/client/TestMetadataObjectSemanticModelOperations.java b/clients/client-java/src/test/java/org/apache/gravitino/client/TestMetadataObjectSemanticModelOperations.java
new file mode 100644
index 0000000000..f81552790b
--- /dev/null
+++ b/clients/client-java/src/test/java/org/apache/gravitino/client/TestMetadataObjectSemanticModelOperations.java
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.client;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+
+import java.time.Instant;
+import java.util.List;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.dto.AuditDTO;
+import org.apache.gravitino.dto.rel.ColumnDTO;
+import org.apache.gravitino.dto.rel.RepresentationDTO;
+import org.apache.gravitino.dto.rel.SQLRepresentationDTO;
+import org.apache.gravitino.dto.rel.TableDTO;
+import org.apache.gravitino.dto.rel.ViewDTO;
+import org.apache.gravitino.dto.responses.EntityListResponse;
+import org.apache.gravitino.dto.responses.ErrorResponse;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.exceptions.NoSuchMetadataObjectException;
+import org.apache.gravitino.exceptions.NotFoundException;
+import org.apache.gravitino.rel.Table;
+import org.apache.gravitino.rel.View;
+import org.apache.gravitino.rest.RESTUtils;
+import org.apache.gravitino.semantic.SupportsSemanticModels;
+import org.apache.hc.core5.http.Method;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class TestMetadataObjectSemanticModelOperations extends TestBase {
+ private static final Namespace SOURCE_NAMESPACE =
+ Namespace.of("metalake /%? 测试", "catalog", "schema");
+ private static final String SOURCE_NAME = "orders /%? 明细";
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testSourceCapabilitiesPreserveFullIdentifiers(boolean view) throws Exception {
+ NameIdentifier[] expected = {
+ NameIdentifier.of(SOURCE_NAMESPACE.level(0), "another_catalog", "schema", "a", "Orders.raw"),
+ NameIdentifier.of(SOURCE_NAMESPACE.level(0), "another_catalog", "schema", "a", "orders.raw"),
+ NameIdentifier.of(SOURCE_NAMESPACE.level(0), "another_catalog", "schema", "a-b", "收入")
+ };
+ buildMockResource(Method.GET, path(view), null, new EntityListResponse(expected), 200);
+ SupportsSemanticModels source = source(view);
+ NameIdentifier[] actual = source.listSemanticModelDatasets();
+ assertArrayEquals(expected, actual);
+ assertEquals(4, actual[0].namespace().length());
+ assertEquals("Orders.raw", actual[0].name());
+ assertEquals(
+ NameIdentifier.of(SOURCE_NAMESPACE.level(0), "another_catalog", "schema", "a"),
+ NameIdentifier.of(actual[0].namespace().levels()));
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testEmptyResults(boolean view) throws Exception {
+ buildMockResource(
+ Method.GET, path(view), null, new EntityListResponse(new NameIdentifier[0]), 200);
+ assertArrayEquals(new NameIdentifier[0], source(view).listSemanticModelDatasets());
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testSourceErrorsAreNotEmptyResults(boolean view) throws Exception {
+ List<ErrorResponse> errors =
+ List.of(
+ ErrorResponse.notFound(
+ NoSuchMetadataObjectException.class.getSimpleName(), "source missing"),
+ ErrorResponse.notFound(NotFoundException.class.getSimpleName(), "endpoint unavailable"),
+ ErrorResponse.illegalArguments("invalid source"),
+ ErrorResponse.forbidden("source hidden", null),
+ ErrorResponse.internalError("storage unavailable", null));
+ List<Class<? extends RuntimeException>> types =
+ List.of(
+ NoSuchMetadataObjectException.class,
+ NotFoundException.class,
+ IllegalArgumentException.class,
+ ForbiddenException.class,
+ RuntimeException.class);
+ int[] statuses = {404, 404, 400, 403, 500};
+ SupportsSemanticModels source = source(view);
+ for (int i = 0; i < errors.size(); i++) {
+ ErrorResponse error = errors.get(i);
+ buildMockResource(Method.GET, path(view), null, error, statuses[i]);
+ RuntimeException failure = assertThrows(types.get(i), source::listSemanticModelDatasets);
+ assertEquals(types.get(i), failure.getClass());
+ assertTrue(failure.getMessage().contains(error.getMessage()));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void testInvalidResponseIsRejected(boolean view) throws Exception {
+ buildMockResource(Method.GET, path(view), null, new EntityListResponse(), 200);
+ IllegalArgumentException failure =
+ assertThrows(IllegalArgumentException.class, source(view)::listSemanticModelDatasets);
+ assertEquals("identifiers must not be null", failure.getMessage());
+ }
+
+ @Test
+ void testDefaultCapabilitiesRemainOptional() {
+ assertThrows(
+ UnsupportedOperationException.class,
+ mock(Table.class, CALLS_REAL_METHODS)::supportsSemanticModels);
+ assertThrows(
+ UnsupportedOperationException.class,
+ mock(View.class, CALLS_REAL_METHODS)::supportsSemanticModels);
+ }
+
+ private static String path(boolean view) {
+ return "/api/metalakes/"
+ + RESTUtils.encodeString(SOURCE_NAMESPACE.level(0))
+ + "/objects/"
+ + (view ? "view/" : "table/")
+ + RESTUtils.encodeString("catalog.schema." + SOURCE_NAME)
+ + "/semantic-model-datasets";
+ }
+
+ private static SupportsSemanticModels source(boolean view) {
+ AuditDTO audit = AuditDTO.builder().withCreator("test").withCreateTime(Instant.now()).build();
+ if (view) {
+ ViewDTO dto =
+ ViewDTO.builder()
+ .withName(SOURCE_NAME)
+ .withAudit(audit)
+ .withRepresentations(
+ new RepresentationDTO[] {
+ SQLRepresentationDTO.builder().withDialect("trino").withSql("SELECT 1").build()
+ })
+ .build();
+ return new GenericView(dto, client.restClient(), SOURCE_NAMESPACE).supportsSemanticModels();
+ }
+ TableDTO dto =
+ TableDTO.builder()
+ .withName(SOURCE_NAME)
+ .withColumns(new ColumnDTO[0])
+ .withAudit(audit)
+ .build();
+ return RelationalTable.from(SOURCE_NAMESPACE, dto, client.restClient())
+ .supportsSemanticModels();
+ }
+}