adam-christian-software commented on code in PR #4961:
URL: https://github.com/apache/polaris/pull/4961#discussion_r3529599012
##########
polaris-core/src/main/java/org/apache/polaris/core/auth/RbacOperationSemantics.java:
##########
@@ -407,6 +413,16 @@ private static void register(
register(UPDATE_POLICY, POLICY_WRITE);
register(LIST_POLICY, POLICY_LIST);
+ // Semantic-model operations.
+ // Interim (Phase 3): gated behind the coarse CATALOG_MANAGE_CONTENT
privilege, matching the
Review Comment:
Nit: I would make sure to put TODO because this feels like we are
comfortable with releasing this which I don't think we are.
##########
runtime/service/src/main/java/org/apache/polaris/service/exception/PolarisExceptionMapper.java:
##########
Review Comment:
This is not on you, but IMO, this should be inverted. We are having this
PolarisExceptionMapper depend on every exception rather than every exception
defining what status it should return.
Basically, we should have someone do a refactor where the PolarisException
has a required method which returns a Response.Status and update it, so that
every PolarisException returns the right item.
##########
polaris-core/src/main/java/org/apache/polaris/core/semantic/exceptions/SemanticModelVersionMismatchException.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.polaris.core.semantic.exceptions;
+
+import org.apache.polaris.core.exceptions.PolarisException;
+
+/**
+ * Thrown when a semantic-model update supplies an {@code entity-version} that
does not match the
+ * version currently stored in the catalog (optimistic-concurrency conflict).
Maps to HTTP 409.
Review Comment:
So, you have a bit of a terminology overload here. You have "version" which
could mean something akin to an HTTP ETag used for optimistic concurrency
control or you could have the OSI Spec Version. Personally, I would prefer if
you use "tag" or ETag for things in OCC and version for the OSI Spec Version.
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.ListEntitiesResult;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.semantic.SemanticModelEntity;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import
org.apache.polaris.core.semantic.exceptions.SemanticModelVersionMismatchException;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Core create/list/load/update/drop logic for OSI semantic models. Mirrors
{@link
+ * org.apache.polaris.service.catalog.policy.PolicyCatalog}: the OSI document
body is stored inside
+ * the entity {@code properties} map, writes resolve every {@code
dataset.source} to a {@code
+ * TABLE_LIKE} entity in the current catalog, and updates use optimistic
concurrency on the entity
+ * version. Document schema validation is a separate concern (see {@link
+ * SemanticDocumentValidator}).
+ */
+public class SemanticModelCatalog {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(SemanticModelCatalog.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final CallContext callContext;
+ private final PolarisResolutionManifestCatalogView resolvedEntityView;
+ private final CatalogEntity catalogEntity;
+ private final long catalogId;
+ private final PolarisMetaStoreManager metaStoreManager;
+
+ public SemanticModelCatalog(
+ PolarisMetaStoreManager metaStoreManager,
+ CallContext callContext,
+ PolarisResolutionManifestCatalogView resolvedEntityView) {
+ this.callContext = callContext;
+ this.resolvedEntityView = resolvedEntityView;
+ this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity();
+ this.catalogId = catalogEntity.getId();
+ this.metaStoreManager = metaStoreManager;
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolvedParent =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedParent == null) {
+ // Illegal state because the namespace should've already been in the
static resolution set.
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved parent for semantic model
'%s'", identifier));
+ }
+ List<PolarisEntity> catalogPath = resolvedParent.getRawFullPath();
+
+ PolarisResolvedPathWrapper existing =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (existing != null && existing.getRawLeafEntity() != null) {
+ throw new AlreadyExistsException("Semantic model already exists: %s",
identifier.getName());
+ }
+
+ // Parse the document and resolve its source tables before persisting.
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity entity =
+ new SemanticModelEntity.Builder(namespace, identifier.getName())
+ .setCatalogId(catalogId)
+ .setParentId(resolvedParent.getRawLeafEntity().getId())
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .setId(
+
metaStoreManager.generateNewEntityId(callContext.getPolarisCallContext()).getId())
+ .setCreateTimestamp(System.currentTimeMillis())
+ .build();
+
+ EntityResult res =
+ metaStoreManager.createEntityIfNotExists(
+ callContext.getPolarisCallContext(),
PolarisEntity.toCoreList(catalogPath), entity);
+ if (!res.isSuccess()) {
+ switch (res.getReturnStatus()) {
+ case ENTITY_ALREADY_EXISTS ->
+ throw new AlreadyExistsException(
+ "Semantic model already exists: %s", identifier.getName());
+ default ->
+ throw new IllegalStateException(
+ String.format(
+ "Unknown error status for identifier %s: %s with
extraInfo: %s",
+ identifier, res.getReturnStatus(),
res.getExtraInformation()));
+ }
+ }
+
+ SemanticModelEntity result = SemanticModelEntity.of(res.getEntity());
+ LOGGER.debug("Created semantic model entity {} with identifier {}",
result, identifier);
+ return toLoadResponse(result);
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(Namespace namespace,
PageToken pageToken) {
+ PolarisResolvedPathWrapper resolvedEntities =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedEntities == null) {
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved namespace '%s'", namespace));
+ }
+ List<PolarisEntity> catalogPath = resolvedEntities.getRawFullPath();
+
+ ListEntitiesResult result =
+ metaStoreManager.listEntities(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ PolarisEntityType.SEMANTIC_MODEL,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ pageToken);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException("Failed to list semantic models in
namespace: " + namespace);
+ }
+
+ // LinkedHashSet keeps the page order stable in the serialized response.
+ Set<SemanticModelIdentifier> identifiers =
+ result.getEntities().stream()
+ .map(
+ record ->
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(record.getName())
+ .build())
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return ListSemanticModelsResponse.builder()
+ .setIdentifiers(identifiers)
+ .setNextPageToken(result.getPage().encodedResponseToken())
+ .build();
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ return toLoadResponse(resolveModelOrThrow(identifier));
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document,
String expectedVersion) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ SemanticModelEntity current =
SemanticModelEntity.of(resolvedPath.getRawLeafEntity());
+
+ String currentVersion = Integer.toString(current.getEntityVersion());
+ if (!currentVersion.equals(expectedVersion)) {
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model version mismatch. Given version is %s, current
version is %s",
+ expectedVersion, currentVersion));
+ }
+
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity newEntity =
+ new SemanticModelEntity.Builder(current)
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .build();
+
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ SemanticModelEntity updated =
+ Optional.ofNullable(
+ metaStoreManager
+ .updateEntityPropertiesIfNotChanged(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ newEntity)
+ .getEntity())
+ .map(SemanticModelEntity::of)
+ .orElse(null);
+ if (updated == null) {
+ // Lost the optimistic-concurrency race with a concurrent writer.
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model %s was modified concurrently; retry after
reloading",
+ identifier.getName()));
+ }
+
+ return toLoadResponse(updated);
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ PolarisBaseEntity entity = resolvedPath.getRawLeafEntity();
+
+ var result =
+ metaStoreManager.dropEntityIfExists(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ entity,
+ java.util.Map.of(),
+ false);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException(
+ String.format(
+ "Failed to drop semantic model %s error status: %s with
extraInfo: %s",
+ identifier, result.getReturnStatus(),
result.getExtraInformation()));
Review Comment:
Just for my understanding, is all of the extra information returned by this
exception guaranteed to not cause any additional information disclosure? Like,
we aren't leaking out the DB IP or anything like that?
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.ListEntitiesResult;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.semantic.SemanticModelEntity;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import
org.apache.polaris.core.semantic.exceptions.SemanticModelVersionMismatchException;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Core create/list/load/update/drop logic for OSI semantic models. Mirrors
{@link
+ * org.apache.polaris.service.catalog.policy.PolicyCatalog}: the OSI document
body is stored inside
+ * the entity {@code properties} map, writes resolve every {@code
dataset.source} to a {@code
+ * TABLE_LIKE} entity in the current catalog, and updates use optimistic
concurrency on the entity
+ * version. Document schema validation is a separate concern (see {@link
+ * SemanticDocumentValidator}).
+ */
+public class SemanticModelCatalog {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(SemanticModelCatalog.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final CallContext callContext;
+ private final PolarisResolutionManifestCatalogView resolvedEntityView;
+ private final CatalogEntity catalogEntity;
+ private final long catalogId;
+ private final PolarisMetaStoreManager metaStoreManager;
+
+ public SemanticModelCatalog(
+ PolarisMetaStoreManager metaStoreManager,
+ CallContext callContext,
+ PolarisResolutionManifestCatalogView resolvedEntityView) {
+ this.callContext = callContext;
+ this.resolvedEntityView = resolvedEntityView;
+ this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity();
+ this.catalogId = catalogEntity.getId();
+ this.metaStoreManager = metaStoreManager;
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolvedParent =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedParent == null) {
+ // Illegal state because the namespace should've already been in the
static resolution set.
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved parent for semantic model
'%s'", identifier));
+ }
+ List<PolarisEntity> catalogPath = resolvedParent.getRawFullPath();
+
+ PolarisResolvedPathWrapper existing =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (existing != null && existing.getRawLeafEntity() != null) {
+ throw new AlreadyExistsException("Semantic model already exists: %s",
identifier.getName());
+ }
+
+ // Parse the document and resolve its source tables before persisting.
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity entity =
+ new SemanticModelEntity.Builder(namespace, identifier.getName())
+ .setCatalogId(catalogId)
+ .setParentId(resolvedParent.getRawLeafEntity().getId())
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .setId(
+
metaStoreManager.generateNewEntityId(callContext.getPolarisCallContext()).getId())
+ .setCreateTimestamp(System.currentTimeMillis())
+ .build();
+
+ EntityResult res =
+ metaStoreManager.createEntityIfNotExists(
+ callContext.getPolarisCallContext(),
PolarisEntity.toCoreList(catalogPath), entity);
+ if (!res.isSuccess()) {
+ switch (res.getReturnStatus()) {
+ case ENTITY_ALREADY_EXISTS ->
+ throw new AlreadyExistsException(
+ "Semantic model already exists: %s", identifier.getName());
+ default ->
+ throw new IllegalStateException(
+ String.format(
+ "Unknown error status for identifier %s: %s with
extraInfo: %s",
+ identifier, res.getReturnStatus(),
res.getExtraInformation()));
+ }
+ }
+
+ SemanticModelEntity result = SemanticModelEntity.of(res.getEntity());
+ LOGGER.debug("Created semantic model entity {} with identifier {}",
result, identifier);
+ return toLoadResponse(result);
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(Namespace namespace,
PageToken pageToken) {
+ PolarisResolvedPathWrapper resolvedEntities =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedEntities == null) {
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved namespace '%s'", namespace));
+ }
+ List<PolarisEntity> catalogPath = resolvedEntities.getRawFullPath();
+
+ ListEntitiesResult result =
+ metaStoreManager.listEntities(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ PolarisEntityType.SEMANTIC_MODEL,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ pageToken);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException("Failed to list semantic models in
namespace: " + namespace);
+ }
+
+ // LinkedHashSet keeps the page order stable in the serialized response.
+ Set<SemanticModelIdentifier> identifiers =
+ result.getEntities().stream()
+ .map(
+ record ->
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(record.getName())
+ .build())
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return ListSemanticModelsResponse.builder()
+ .setIdentifiers(identifiers)
+ .setNextPageToken(result.getPage().encodedResponseToken())
+ .build();
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ return toLoadResponse(resolveModelOrThrow(identifier));
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document,
String expectedVersion) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ SemanticModelEntity current =
SemanticModelEntity.of(resolvedPath.getRawLeafEntity());
+
+ String currentVersion = Integer.toString(current.getEntityVersion());
+ if (!currentVersion.equals(expectedVersion)) {
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model version mismatch. Given version is %s, current
version is %s",
+ expectedVersion, currentVersion));
+ }
+
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity newEntity =
+ new SemanticModelEntity.Builder(current)
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .build();
+
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ SemanticModelEntity updated =
+ Optional.ofNullable(
+ metaStoreManager
+ .updateEntityPropertiesIfNotChanged(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ newEntity)
+ .getEntity())
+ .map(SemanticModelEntity::of)
+ .orElse(null);
+ if (updated == null) {
+ // Lost the optimistic-concurrency race with a concurrent writer.
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model %s was modified concurrently; retry after
reloading",
+ identifier.getName()));
+ }
+
+ return toLoadResponse(updated);
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ PolarisBaseEntity entity = resolvedPath.getRawLeafEntity();
+
+ var result =
+ metaStoreManager.dropEntityIfExists(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ entity,
+ java.util.Map.of(),
+ false);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException(
+ String.format(
+ "Failed to drop semantic model %s error status: %s with
extraInfo: %s",
+ identifier, result.getReturnStatus(),
result.getExtraInformation()));
+ }
+ }
+
+ // ---- helpers ----
+
+ private PolarisResolvedPathWrapper
resolveModelPathOrThrow(SemanticModelIdentifier identifier) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (resolved == null || resolved.getRawLeafEntity() == null) {
+ throw new NoSuchSemanticModelException(
+ String.format("Semantic model does not exist: %s",
identifier.getName()));
+ }
+ return resolved;
+ }
+
+ private SemanticModelEntity resolveModelOrThrow(SemanticModelIdentifier
identifier) {
+ return
SemanticModelEntity.of(resolveModelPathOrThrow(identifier).getRawLeafEntity());
+ }
+
+ /**
+ * Parses the OSI document body and resolves every {@code dataset.source} to
a {@code TABLE_LIKE}
+ * entity. Shared by create and update. Schema/size validation is a separate
concern handled by a
+ * {@link SemanticDocumentValidator} implementation (not yet wired in this
phase).
+ */
+ private void validateDocumentAndSources(SemanticModelDocument document) {
+ String body = document.getSemanticModel();
+ if (body == null || body.isBlank()) {
+ return;
+ }
+ JsonNode semanticModel;
+ try {
+ semanticModel = MAPPER.readTree(body);
+ } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
+ throw new BadRequestException(
+ "Field 'semantic_model' is not valid JSON: %s",
e.getOriginalMessage());
+ }
+ resolveSourcesOrThrow(semanticModel);
+ // TODO call the SemanticDocumentValidator when it's ready
+ }
+
+ /**
+ * Resolves every {@code dataset.source} in the OSI document to a {@code
TABLE_LIKE} entity in the
+ * current catalog. Fails with 400 and a JSON-Pointer to the offending
dataset if any source does
+ * not resolve. Column-level checks are deferred (F5).
+ */
+ private void resolveSourcesOrThrow(JsonNode semanticModel) {
+ if (!semanticModel.isArray()) {
+ return;
+ }
+ for (int m = 0; m < semanticModel.size(); m++) {
+ JsonNode datasets = semanticModel.get(m).get("datasets");
+ if (datasets == null || !datasets.isArray()) {
+ continue;
+ }
+ for (int d = 0; d < datasets.size(); d++) {
+ JsonNode source = datasets.get(d).get("source");
+ if (source != null && source.isTextual()) {
+ String pointer =
String.format("/semantic_model/%d/datasets/%d/source", m, d);
+ resolveSourceOrThrow(source.asText(), pointer);
+ }
+ }
+ }
+ }
+
+ private void resolveSourceOrThrow(String source, String pointer) {
+ TableIdentifier tableIdentifier = parseSource(source, pointer);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofTableLike(tableIdentifier),
PolarisEntitySubType.ANY_SUBTYPE);
+ if (resolved == null
+ || resolved.getRawLeafEntity() == null
+ || resolved.getRawLeafEntity().getType() !=
PolarisEntityType.TABLE_LIKE) {
+ throw new BadRequestException(
+ "Semantic model source '%s' at %s does not resolve to a table or
view in catalog '%s'",
+ source, pointer, catalogEntity.getName());
+ }
+ }
+
+ private TableIdentifier parseSource(String source, String pointer) {
+ List<String> parts = Splitter.on('.').splitToList(source);
Review Comment:
Nit: Make '.' defined as a static variable, so folks know that we split on
dots in this spec.
##########
persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/EntityObjMappings.java:
##########
@@ -90,6 +90,9 @@ public final class EntityObjMappings {
static {
var missingEntityTypes = new HashSet<>(Set.of(PolarisEntityType.values()));
missingEntityTypes.remove(PolarisEntityType.NULL_TYPE);
+ // SEMANTIC_MODEL is not yet supported by the NoSQL backend; its mapping
lands in a follow-up.
Review Comment:
I would put a TODO since we aren't going to try to release without NoSQL.
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.ListEntitiesResult;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.semantic.SemanticModelEntity;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import
org.apache.polaris.core.semantic.exceptions.SemanticModelVersionMismatchException;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Core create/list/load/update/drop logic for OSI semantic models. Mirrors
{@link
+ * org.apache.polaris.service.catalog.policy.PolicyCatalog}: the OSI document
body is stored inside
+ * the entity {@code properties} map, writes resolve every {@code
dataset.source} to a {@code
+ * TABLE_LIKE} entity in the current catalog, and updates use optimistic
concurrency on the entity
+ * version. Document schema validation is a separate concern (see {@link
+ * SemanticDocumentValidator}).
+ */
+public class SemanticModelCatalog {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(SemanticModelCatalog.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final CallContext callContext;
+ private final PolarisResolutionManifestCatalogView resolvedEntityView;
+ private final CatalogEntity catalogEntity;
+ private final long catalogId;
+ private final PolarisMetaStoreManager metaStoreManager;
+
+ public SemanticModelCatalog(
+ PolarisMetaStoreManager metaStoreManager,
+ CallContext callContext,
+ PolarisResolutionManifestCatalogView resolvedEntityView) {
+ this.callContext = callContext;
+ this.resolvedEntityView = resolvedEntityView;
+ this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity();
+ this.catalogId = catalogEntity.getId();
+ this.metaStoreManager = metaStoreManager;
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolvedParent =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedParent == null) {
+ // Illegal state because the namespace should've already been in the
static resolution set.
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved parent for semantic model
'%s'", identifier));
+ }
+ List<PolarisEntity> catalogPath = resolvedParent.getRawFullPath();
+
+ PolarisResolvedPathWrapper existing =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (existing != null && existing.getRawLeafEntity() != null) {
+ throw new AlreadyExistsException("Semantic model already exists: %s",
identifier.getName());
+ }
+
+ // Parse the document and resolve its source tables before persisting.
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity entity =
+ new SemanticModelEntity.Builder(namespace, identifier.getName())
+ .setCatalogId(catalogId)
+ .setParentId(resolvedParent.getRawLeafEntity().getId())
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .setId(
+
metaStoreManager.generateNewEntityId(callContext.getPolarisCallContext()).getId())
+ .setCreateTimestamp(System.currentTimeMillis())
+ .build();
+
+ EntityResult res =
+ metaStoreManager.createEntityIfNotExists(
+ callContext.getPolarisCallContext(),
PolarisEntity.toCoreList(catalogPath), entity);
+ if (!res.isSuccess()) {
+ switch (res.getReturnStatus()) {
+ case ENTITY_ALREADY_EXISTS ->
+ throw new AlreadyExistsException(
+ "Semantic model already exists: %s", identifier.getName());
+ default ->
+ throw new IllegalStateException(
+ String.format(
+ "Unknown error status for identifier %s: %s with
extraInfo: %s",
+ identifier, res.getReturnStatus(),
res.getExtraInformation()));
+ }
+ }
+
+ SemanticModelEntity result = SemanticModelEntity.of(res.getEntity());
+ LOGGER.debug("Created semantic model entity {} with identifier {}",
result, identifier);
+ return toLoadResponse(result);
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(Namespace namespace,
PageToken pageToken) {
+ PolarisResolvedPathWrapper resolvedEntities =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedEntities == null) {
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved namespace '%s'", namespace));
+ }
+ List<PolarisEntity> catalogPath = resolvedEntities.getRawFullPath();
+
+ ListEntitiesResult result =
+ metaStoreManager.listEntities(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ PolarisEntityType.SEMANTIC_MODEL,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ pageToken);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException("Failed to list semantic models in
namespace: " + namespace);
+ }
+
+ // LinkedHashSet keeps the page order stable in the serialized response.
+ Set<SemanticModelIdentifier> identifiers =
+ result.getEntities().stream()
+ .map(
+ record ->
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(record.getName())
+ .build())
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return ListSemanticModelsResponse.builder()
+ .setIdentifiers(identifiers)
+ .setNextPageToken(result.getPage().encodedResponseToken())
+ .build();
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ return toLoadResponse(resolveModelOrThrow(identifier));
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document,
String expectedVersion) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ SemanticModelEntity current =
SemanticModelEntity.of(resolvedPath.getRawLeafEntity());
+
+ String currentVersion = Integer.toString(current.getEntityVersion());
+ if (!currentVersion.equals(expectedVersion)) {
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model version mismatch. Given version is %s, current
version is %s",
+ expectedVersion, currentVersion));
+ }
+
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity newEntity =
+ new SemanticModelEntity.Builder(current)
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .build();
+
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ SemanticModelEntity updated =
+ Optional.ofNullable(
+ metaStoreManager
+ .updateEntityPropertiesIfNotChanged(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ newEntity)
+ .getEntity())
+ .map(SemanticModelEntity::of)
+ .orElse(null);
+ if (updated == null) {
+ // Lost the optimistic-concurrency race with a concurrent writer.
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model %s was modified concurrently; retry after
reloading",
+ identifier.getName()));
+ }
+
+ return toLoadResponse(updated);
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ PolarisBaseEntity entity = resolvedPath.getRawLeafEntity();
+
+ var result =
+ metaStoreManager.dropEntityIfExists(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ entity,
+ java.util.Map.of(),
+ false);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException(
+ String.format(
+ "Failed to drop semantic model %s error status: %s with
extraInfo: %s",
+ identifier, result.getReturnStatus(),
result.getExtraInformation()));
+ }
+ }
+
+ // ---- helpers ----
+
+ private PolarisResolvedPathWrapper
resolveModelPathOrThrow(SemanticModelIdentifier identifier) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (resolved == null || resolved.getRawLeafEntity() == null) {
+ throw new NoSuchSemanticModelException(
+ String.format("Semantic model does not exist: %s",
identifier.getName()));
+ }
+ return resolved;
+ }
+
+ private SemanticModelEntity resolveModelOrThrow(SemanticModelIdentifier
identifier) {
+ return
SemanticModelEntity.of(resolveModelPathOrThrow(identifier).getRawLeafEntity());
+ }
+
+ /**
+ * Parses the OSI document body and resolves every {@code dataset.source} to
a {@code TABLE_LIKE}
+ * entity. Shared by create and update. Schema/size validation is a separate
concern handled by a
+ * {@link SemanticDocumentValidator} implementation (not yet wired in this
phase).
+ */
+ private void validateDocumentAndSources(SemanticModelDocument document) {
+ String body = document.getSemanticModel();
+ if (body == null || body.isBlank()) {
+ return;
+ }
+ JsonNode semanticModel;
+ try {
+ semanticModel = MAPPER.readTree(body);
+ } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
+ throw new BadRequestException(
+ "Field 'semantic_model' is not valid JSON: %s",
e.getOriginalMessage());
+ }
+ resolveSourcesOrThrow(semanticModel);
+ // TODO call the SemanticDocumentValidator when it's ready
+ }
+
+ /**
+ * Resolves every {@code dataset.source} in the OSI document to a {@code
TABLE_LIKE} entity in the
+ * current catalog. Fails with 400 and a JSON-Pointer to the offending
dataset if any source does
+ * not resolve. Column-level checks are deferred (F5).
+ */
+ private void resolveSourcesOrThrow(JsonNode semanticModel) {
+ if (!semanticModel.isArray()) {
+ return;
+ }
+ for (int m = 0; m < semanticModel.size(); m++) {
Review Comment:
Nit: IMO, we should have descriptive names for variables rather than just
"m".
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.ListEntitiesResult;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.semantic.SemanticModelEntity;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import
org.apache.polaris.core.semantic.exceptions.SemanticModelVersionMismatchException;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Core create/list/load/update/drop logic for OSI semantic models. Mirrors
{@link
+ * org.apache.polaris.service.catalog.policy.PolicyCatalog}: the OSI document
body is stored inside
+ * the entity {@code properties} map, writes resolve every {@code
dataset.source} to a {@code
+ * TABLE_LIKE} entity in the current catalog, and updates use optimistic
concurrency on the entity
+ * version. Document schema validation is a separate concern (see {@link
+ * SemanticDocumentValidator}).
+ */
+public class SemanticModelCatalog {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(SemanticModelCatalog.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final CallContext callContext;
+ private final PolarisResolutionManifestCatalogView resolvedEntityView;
+ private final CatalogEntity catalogEntity;
+ private final long catalogId;
+ private final PolarisMetaStoreManager metaStoreManager;
+
+ public SemanticModelCatalog(
+ PolarisMetaStoreManager metaStoreManager,
+ CallContext callContext,
+ PolarisResolutionManifestCatalogView resolvedEntityView) {
+ this.callContext = callContext;
+ this.resolvedEntityView = resolvedEntityView;
+ this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity();
+ this.catalogId = catalogEntity.getId();
+ this.metaStoreManager = metaStoreManager;
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolvedParent =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedParent == null) {
+ // Illegal state because the namespace should've already been in the
static resolution set.
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved parent for semantic model
'%s'", identifier));
+ }
+ List<PolarisEntity> catalogPath = resolvedParent.getRawFullPath();
+
+ PolarisResolvedPathWrapper existing =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (existing != null && existing.getRawLeafEntity() != null) {
+ throw new AlreadyExistsException("Semantic model already exists: %s",
identifier.getName());
+ }
+
+ // Parse the document and resolve its source tables before persisting.
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity entity =
+ new SemanticModelEntity.Builder(namespace, identifier.getName())
+ .setCatalogId(catalogId)
+ .setParentId(resolvedParent.getRawLeafEntity().getId())
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .setId(
+
metaStoreManager.generateNewEntityId(callContext.getPolarisCallContext()).getId())
+ .setCreateTimestamp(System.currentTimeMillis())
+ .build();
+
+ EntityResult res =
+ metaStoreManager.createEntityIfNotExists(
+ callContext.getPolarisCallContext(),
PolarisEntity.toCoreList(catalogPath), entity);
+ if (!res.isSuccess()) {
+ switch (res.getReturnStatus()) {
+ case ENTITY_ALREADY_EXISTS ->
+ throw new AlreadyExistsException(
+ "Semantic model already exists: %s", identifier.getName());
+ default ->
+ throw new IllegalStateException(
+ String.format(
+ "Unknown error status for identifier %s: %s with
extraInfo: %s",
+ identifier, res.getReturnStatus(),
res.getExtraInformation()));
+ }
+ }
+
+ SemanticModelEntity result = SemanticModelEntity.of(res.getEntity());
+ LOGGER.debug("Created semantic model entity {} with identifier {}",
result, identifier);
+ return toLoadResponse(result);
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(Namespace namespace,
PageToken pageToken) {
+ PolarisResolvedPathWrapper resolvedEntities =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedEntities == null) {
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved namespace '%s'", namespace));
+ }
+ List<PolarisEntity> catalogPath = resolvedEntities.getRawFullPath();
+
+ ListEntitiesResult result =
+ metaStoreManager.listEntities(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ PolarisEntityType.SEMANTIC_MODEL,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ pageToken);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException("Failed to list semantic models in
namespace: " + namespace);
+ }
+
+ // LinkedHashSet keeps the page order stable in the serialized response.
+ Set<SemanticModelIdentifier> identifiers =
+ result.getEntities().stream()
+ .map(
+ record ->
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(record.getName())
+ .build())
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return ListSemanticModelsResponse.builder()
+ .setIdentifiers(identifiers)
+ .setNextPageToken(result.getPage().encodedResponseToken())
+ .build();
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ return toLoadResponse(resolveModelOrThrow(identifier));
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document,
String expectedVersion) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ SemanticModelEntity current =
SemanticModelEntity.of(resolvedPath.getRawLeafEntity());
+
+ String currentVersion = Integer.toString(current.getEntityVersion());
+ if (!currentVersion.equals(expectedVersion)) {
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model version mismatch. Given version is %s, current
version is %s",
+ expectedVersion, currentVersion));
+ }
+
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity newEntity =
+ new SemanticModelEntity.Builder(current)
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .build();
+
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ SemanticModelEntity updated =
+ Optional.ofNullable(
+ metaStoreManager
+ .updateEntityPropertiesIfNotChanged(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ newEntity)
+ .getEntity())
+ .map(SemanticModelEntity::of)
+ .orElse(null);
+ if (updated == null) {
+ // Lost the optimistic-concurrency race with a concurrent writer.
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model %s was modified concurrently; retry after
reloading",
+ identifier.getName()));
+ }
+
+ return toLoadResponse(updated);
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ PolarisBaseEntity entity = resolvedPath.getRawLeafEntity();
+
+ var result =
+ metaStoreManager.dropEntityIfExists(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ entity,
+ java.util.Map.of(),
+ false);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException(
+ String.format(
+ "Failed to drop semantic model %s error status: %s with
extraInfo: %s",
+ identifier, result.getReturnStatus(),
result.getExtraInformation()));
+ }
+ }
+
+ // ---- helpers ----
+
+ private PolarisResolvedPathWrapper
resolveModelPathOrThrow(SemanticModelIdentifier identifier) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (resolved == null || resolved.getRawLeafEntity() == null) {
+ throw new NoSuchSemanticModelException(
+ String.format("Semantic model does not exist: %s",
identifier.getName()));
+ }
+ return resolved;
+ }
+
+ private SemanticModelEntity resolveModelOrThrow(SemanticModelIdentifier
identifier) {
+ return
SemanticModelEntity.of(resolveModelPathOrThrow(identifier).getRawLeafEntity());
+ }
+
+ /**
+ * Parses the OSI document body and resolves every {@code dataset.source} to
a {@code TABLE_LIKE}
+ * entity. Shared by create and update. Schema/size validation is a separate
concern handled by a
+ * {@link SemanticDocumentValidator} implementation (not yet wired in this
phase).
+ */
+ private void validateDocumentAndSources(SemanticModelDocument document) {
+ String body = document.getSemanticModel();
+ if (body == null || body.isBlank()) {
+ return;
+ }
+ JsonNode semanticModel;
+ try {
+ semanticModel = MAPPER.readTree(body);
+ } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
+ throw new BadRequestException(
+ "Field 'semantic_model' is not valid JSON: %s",
e.getOriginalMessage());
+ }
+ resolveSourcesOrThrow(semanticModel);
+ // TODO call the SemanticDocumentValidator when it's ready
+ }
+
+ /**
+ * Resolves every {@code dataset.source} in the OSI document to a {@code
TABLE_LIKE} entity in the
+ * current catalog. Fails with 400 and a JSON-Pointer to the offending
dataset if any source does
+ * not resolve. Column-level checks are deferred (F5).
+ */
+ private void resolveSourcesOrThrow(JsonNode semanticModel) {
+ if (!semanticModel.isArray()) {
+ return;
+ }
+ for (int m = 0; m < semanticModel.size(); m++) {
+ JsonNode datasets = semanticModel.get(m).get("datasets");
+ if (datasets == null || !datasets.isArray()) {
+ continue;
+ }
+ for (int d = 0; d < datasets.size(); d++) {
+ JsonNode source = datasets.get(d).get("source");
+ if (source != null && source.isTextual()) {
+ String pointer =
String.format("/semantic_model/%d/datasets/%d/source", m, d);
+ resolveSourceOrThrow(source.asText(), pointer);
+ }
+ }
+ }
+ }
+
+ private void resolveSourceOrThrow(String source, String pointer) {
+ TableIdentifier tableIdentifier = parseSource(source, pointer);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofTableLike(tableIdentifier),
PolarisEntitySubType.ANY_SUBTYPE);
+ if (resolved == null
+ || resolved.getRawLeafEntity() == null
+ || resolved.getRawLeafEntity().getType() !=
PolarisEntityType.TABLE_LIKE) {
+ throw new BadRequestException(
+ "Semantic model source '%s' at %s does not resolve to a table or
view in catalog '%s'",
+ source, pointer, catalogEntity.getName());
+ }
+ }
+
+ private TableIdentifier parseSource(String source, String pointer) {
+ List<String> parts = Splitter.on('.').splitToList(source);
Review Comment:
Nit: Make '.' defined as a static variable, so folks know that we split on
dots in this spec.
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Splitter;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.AlreadyExistsException;
+import org.apache.iceberg.exceptions.BadRequestException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.entity.CatalogEntity;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.dao.entity.EntityResult;
+import org.apache.polaris.core.persistence.dao.entity.ListEntitiesResult;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import
org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.semantic.SemanticModelEntity;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import
org.apache.polaris.core.semantic.exceptions.SemanticModelVersionMismatchException;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Core create/list/load/update/drop logic for OSI semantic models. Mirrors
{@link
+ * org.apache.polaris.service.catalog.policy.PolicyCatalog}: the OSI document
body is stored inside
+ * the entity {@code properties} map, writes resolve every {@code
dataset.source} to a {@code
+ * TABLE_LIKE} entity in the current catalog, and updates use optimistic
concurrency on the entity
+ * version. Document schema validation is a separate concern (see {@link
+ * SemanticDocumentValidator}).
+ */
+public class SemanticModelCatalog {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(SemanticModelCatalog.class);
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private final CallContext callContext;
+ private final PolarisResolutionManifestCatalogView resolvedEntityView;
+ private final CatalogEntity catalogEntity;
+ private final long catalogId;
+ private final PolarisMetaStoreManager metaStoreManager;
+
+ public SemanticModelCatalog(
+ PolarisMetaStoreManager metaStoreManager,
+ CallContext callContext,
+ PolarisResolutionManifestCatalogView resolvedEntityView) {
+ this.callContext = callContext;
+ this.resolvedEntityView = resolvedEntityView;
+ this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity();
+ this.catalogId = catalogEntity.getId();
+ this.metaStoreManager = metaStoreManager;
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolvedParent =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedParent == null) {
+ // Illegal state because the namespace should've already been in the
static resolution set.
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved parent for semantic model
'%s'", identifier));
+ }
+ List<PolarisEntity> catalogPath = resolvedParent.getRawFullPath();
+
+ PolarisResolvedPathWrapper existing =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (existing != null && existing.getRawLeafEntity() != null) {
+ throw new AlreadyExistsException("Semantic model already exists: %s",
identifier.getName());
+ }
+
+ // Parse the document and resolve its source tables before persisting.
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity entity =
+ new SemanticModelEntity.Builder(namespace, identifier.getName())
+ .setCatalogId(catalogId)
+ .setParentId(resolvedParent.getRawLeafEntity().getId())
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .setId(
+
metaStoreManager.generateNewEntityId(callContext.getPolarisCallContext()).getId())
+ .setCreateTimestamp(System.currentTimeMillis())
+ .build();
+
+ EntityResult res =
+ metaStoreManager.createEntityIfNotExists(
+ callContext.getPolarisCallContext(),
PolarisEntity.toCoreList(catalogPath), entity);
+ if (!res.isSuccess()) {
+ switch (res.getReturnStatus()) {
+ case ENTITY_ALREADY_EXISTS ->
+ throw new AlreadyExistsException(
+ "Semantic model already exists: %s", identifier.getName());
+ default ->
+ throw new IllegalStateException(
+ String.format(
+ "Unknown error status for identifier %s: %s with
extraInfo: %s",
+ identifier, res.getReturnStatus(),
res.getExtraInformation()));
+ }
+ }
+
+ SemanticModelEntity result = SemanticModelEntity.of(res.getEntity());
+ LOGGER.debug("Created semantic model entity {} with identifier {}",
result, identifier);
+ return toLoadResponse(result);
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(Namespace namespace,
PageToken pageToken) {
+ PolarisResolvedPathWrapper resolvedEntities =
+
resolvedEntityView.getResolvedPath(ResolvedPathKey.ofNamespace(namespace));
+ if (resolvedEntities == null) {
+ throw new IllegalStateException(
+ String.format("Failed to fetch resolved namespace '%s'", namespace));
+ }
+ List<PolarisEntity> catalogPath = resolvedEntities.getRawFullPath();
+
+ ListEntitiesResult result =
+ metaStoreManager.listEntities(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ PolarisEntityType.SEMANTIC_MODEL,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ pageToken);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException("Failed to list semantic models in
namespace: " + namespace);
+ }
+
+ // LinkedHashSet keeps the page order stable in the serialized response.
+ Set<SemanticModelIdentifier> identifiers =
+ result.getEntities().stream()
+ .map(
+ record ->
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(record.getName())
+ .build())
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+ return ListSemanticModelsResponse.builder()
+ .setIdentifiers(identifiers)
+ .setNextPageToken(result.getPage().encodedResponseToken())
+ .build();
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ return toLoadResponse(resolveModelOrThrow(identifier));
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, SemanticModelDocument document,
String expectedVersion) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ SemanticModelEntity current =
SemanticModelEntity.of(resolvedPath.getRawLeafEntity());
+
+ String currentVersion = Integer.toString(current.getEntityVersion());
+ if (!currentVersion.equals(expectedVersion)) {
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model version mismatch. Given version is %s, current
version is %s",
+ expectedVersion, currentVersion));
+ }
+
+ validateDocumentAndSources(document);
+
+ SemanticModelEntity newEntity =
+ new SemanticModelEntity.Builder(current)
+ .setSpecVersion(document.getVersion())
+ .setContent(document.getSemanticModel())
+ .build();
+
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ SemanticModelEntity updated =
+ Optional.ofNullable(
+ metaStoreManager
+ .updateEntityPropertiesIfNotChanged(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ newEntity)
+ .getEntity())
+ .map(SemanticModelEntity::of)
+ .orElse(null);
+ if (updated == null) {
+ // Lost the optimistic-concurrency race with a concurrent writer.
+ throw new SemanticModelVersionMismatchException(
+ String.format(
+ "Semantic model %s was modified concurrently; retry after
reloading",
+ identifier.getName()));
+ }
+
+ return toLoadResponse(updated);
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisResolvedPathWrapper resolvedPath =
resolveModelPathOrThrow(identifier);
+ List<PolarisEntity> catalogPath = resolvedPath.getRawParentPath();
+ PolarisBaseEntity entity = resolvedPath.getRawLeafEntity();
+
+ var result =
+ metaStoreManager.dropEntityIfExists(
+ callContext.getPolarisCallContext(),
+ PolarisEntity.toCoreList(catalogPath),
+ entity,
+ java.util.Map.of(),
+ false);
+ if (!result.isSuccess()) {
+ throw new IllegalStateException(
+ String.format(
+ "Failed to drop semantic model %s error status: %s with
extraInfo: %s",
+ identifier, result.getReturnStatus(),
result.getExtraInformation()));
+ }
+ }
+
+ // ---- helpers ----
+
+ private PolarisResolvedPathWrapper
resolveModelPathOrThrow(SemanticModelIdentifier identifier) {
+ Namespace namespace = toNamespace(identifier);
+ PolarisResolvedPathWrapper resolved =
+ resolvedEntityView.getPassthroughResolvedPath(
+ ResolvedPathKey.ofSemanticModel(namespace, identifier.getName()),
+ PolarisEntitySubType.NULL_SUBTYPE);
+ if (resolved == null || resolved.getRawLeafEntity() == null) {
+ throw new NoSuchSemanticModelException(
+ String.format("Semantic model does not exist: %s",
identifier.getName()));
+ }
+ return resolved;
+ }
+
+ private SemanticModelEntity resolveModelOrThrow(SemanticModelIdentifier
identifier) {
+ return
SemanticModelEntity.of(resolveModelPathOrThrow(identifier).getRawLeafEntity());
+ }
+
+ /**
+ * Parses the OSI document body and resolves every {@code dataset.source} to
a {@code TABLE_LIKE}
+ * entity. Shared by create and update. Schema/size validation is a separate
concern handled by a
+ * {@link SemanticDocumentValidator} implementation (not yet wired in this
phase).
+ */
+ private void validateDocumentAndSources(SemanticModelDocument document) {
+ String body = document.getSemanticModel();
+ if (body == null || body.isBlank()) {
+ return;
+ }
+ JsonNode semanticModel;
+ try {
+ semanticModel = MAPPER.readTree(body);
+ } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
+ throw new BadRequestException(
+ "Field 'semantic_model' is not valid JSON: %s",
e.getOriginalMessage());
+ }
+ resolveSourcesOrThrow(semanticModel);
+ // TODO call the SemanticDocumentValidator when it's ready
+ }
+
+ /**
+ * Resolves every {@code dataset.source} in the OSI document to a {@code
TABLE_LIKE} entity in the
+ * current catalog. Fails with 400 and a JSON-Pointer to the offending
dataset if any source does
+ * not resolve. Column-level checks are deferred (F5).
+ */
+ private void resolveSourcesOrThrow(JsonNode semanticModel) {
Review Comment:
Is this method this class's responsibility? I thought that the
SemanticDocumentValidator would have been the one who would have validated the
syntax and the semantics of the document.
##########
extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalogHandler.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.polaris.service.catalog.semanticmodel;
+
+import java.util.List;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.polaris.core.auth.PolarisAuthorizableOperation;
+import org.apache.polaris.core.catalog.PolarisCatalogHelpers;
+import org.apache.polaris.core.config.FeatureConfiguration;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper;
+import org.apache.polaris.core.persistence.pagination.PageToken;
+import org.apache.polaris.core.persistence.resolver.ResolvedPathKey;
+import org.apache.polaris.core.persistence.resolver.ResolverPath;
+import
org.apache.polaris.core.semantic.exceptions.NoSuchSemanticModelException;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.service.catalog.common.CatalogHandler;
+import
org.apache.polaris.service.catalog.semanticmodel.types.CreateSemanticModelRequest;
+import
org.apache.polaris.service.catalog.semanticmodel.types.ListSemanticModelsResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.LoadSemanticModelResponse;
+import
org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelIdentifier;
+import
org.apache.polaris.service.catalog.semanticmodel.types.UpdateSemanticModelRequest;
+
+/**
+ * Authorizes and delegates OSI semantic-model operations to {@link
SemanticModelCatalog}. Mirrors
+ * {@link org.apache.polaris.service.catalog.policy.PolicyCatalogHandler}.
+ *
+ * <p>Authorization is intentionally minimal in this phase: operations are
gated by the coarse
+ * {@code CATALOG_MANAGE_CONTENT} privilege (see {@code
RbacOperationSemantics}). The dedicated
+ * {@code SEMANTIC_MODEL_*} privilege matrix, the write-time source-access
check, and the
+ * independent/propagated read-time enforcement modes land in the
authorization phase.
+ */
+@PolarisImmutable
+@SuppressWarnings("immutables:incompat")
+public abstract class SemanticModelCatalogHandler extends CatalogHandler {
+
+ private SemanticModelCatalog semanticModelCatalog;
+
+ @Override
+ protected void initializeCatalog() {
+ this.semanticModelCatalog =
+ new SemanticModelCatalog(metaStoreManager(), callContext(),
this.resolutionManifest);
+ }
+
+ public LoadSemanticModelResponse createSemanticModel(
+ Namespace namespace, CreateSemanticModelRequest request) {
+ PolarisAuthorizableOperation op =
PolarisAuthorizableOperation.CREATE_SEMANTIC_MODEL;
+ authorizeBasicNamespaceOperationOrThrow(op, namespace);
+
+ SemanticModelIdentifier identifier =
+ SemanticModelIdentifier.builder()
+ .setNamespace(List.of(namespace.levels()))
+ .setName(request.getName())
+ .build();
+ return semanticModelCatalog.createSemanticModel(identifier,
request.getDocument());
+ }
+
+ public ListSemanticModelsResponse listSemanticModels(
+ Namespace namespace, String pageToken, Integer pageSize) {
+ PolarisAuthorizableOperation op =
PolarisAuthorizableOperation.LIST_SEMANTIC_MODEL;
+ authorizeBasicNamespaceOperationOrThrow(op, namespace);
+
+ PageToken pageRequest = PageToken.build(pageToken, pageSize,
this::shouldDecodeToken);
+ return semanticModelCatalog.listSemanticModels(namespace, pageRequest);
+ }
+
+ public LoadSemanticModelResponse loadSemanticModel(SemanticModelIdentifier
identifier) {
+ PolarisAuthorizableOperation op =
PolarisAuthorizableOperation.LOAD_SEMANTIC_MODEL;
+ authorizeBasicSemanticModelOperationOrThrow(op, identifier);
+ return semanticModelCatalog.loadSemanticModel(identifier);
+ }
+
+ public LoadSemanticModelResponse updateSemanticModel(
+ SemanticModelIdentifier identifier, UpdateSemanticModelRequest request) {
+ PolarisAuthorizableOperation op =
PolarisAuthorizableOperation.UPDATE_SEMANTIC_MODEL;
+ authorizeBasicSemanticModelOperationOrThrow(op, identifier);
+ return semanticModelCatalog.updateSemanticModel(
+ identifier, request.getDocument(), request.getEntityVersion());
+ }
+
+ public void dropSemanticModel(SemanticModelIdentifier identifier) {
+ PolarisAuthorizableOperation op =
PolarisAuthorizableOperation.DROP_SEMANTIC_MODEL;
+ authorizeBasicSemanticModelOperationOrThrow(op, identifier);
+ semanticModelCatalog.dropSemanticModel(identifier);
+ }
+
+ private boolean shouldDecodeToken() {
Review Comment:
Given that we are directly duplicating IcebergCatalogHandler, should we put
this into the CatalogHandler?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]