singhpk234 commented on code in PR #4961: URL: https://github.com/apache/polaris/pull/4961#discussion_r3670375383
########## extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java: ########## @@ -0,0 +1,378 @@ +/* + * 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.core.JsonProcessingException; +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.auth.PolarisPrincipal; +import org.apache.polaris.core.catalog.PolarisCatalogHelpers; +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.PolarisResolutionManifest; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.apache.polaris.core.persistence.resolver.ResolutionManifestFactory; +import org.apache.polaris.core.persistence.resolver.ResolvedPathKey; +import org.apache.polaris.core.persistence.resolver.ResolverPath; +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 Review Comment: minor: do we wanna say it mirrors X ? ########## extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticDocumentValidator.java: ########## @@ -0,0 +1,39 @@ +/* + * 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 org.apache.polaris.service.catalog.semanticmodel.types.SemanticModelDocument; + +/** + * Contract for validating an OSI (Open Semantic Interchange) semantic-model document at write time. + * + * <p>Only the interface ships in this phase; a concrete implementation (schema validation against + * the bundled OSI JSON Schema, size caps, etc.) lands in a follow-up. Implementations are expected Review Comment: nit: would be nice to add a ticket (instead saying follow-up) ########## extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java: ########## @@ -0,0 +1,378 @@ +/* + * 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.core.JsonProcessingException; +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.auth.PolarisPrincipal; +import org.apache.polaris.core.catalog.PolarisCatalogHelpers; +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.PolarisResolutionManifest; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.apache.polaris.core.persistence.resolver.ResolutionManifestFactory; +import org.apache.polaris.core.persistence.resolver.ResolvedPathKey; +import org.apache.polaris.core.persistence.resolver.ResolverPath; +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(); + + /** + * Separator between the namespace path and the name in an OSI {@code dataset.source}, per the IRC + * catalog object-identifier scheme (e.g. {@code sales.store_sales}). + */ + private static final char SOURCE_SEPARATOR = '.'; + + private final CallContext callContext; + private final PolarisResolutionManifestCatalogView resolvedEntityView; + private final CatalogEntity catalogEntity; + private final long catalogId; + private final PolarisMetaStoreManager metaStoreManager; + private final ResolutionManifestFactory resolutionManifestFactory; + private final PolarisPrincipal principal; + + public SemanticModelCatalog( + PolarisMetaStoreManager metaStoreManager, + CallContext callContext, + PolarisResolutionManifestCatalogView resolvedEntityView, + ResolutionManifestFactory resolutionManifestFactory, + PolarisPrincipal principal) { + this.callContext = callContext; + this.resolvedEntityView = resolvedEntityView; + this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity(); + this.catalogId = catalogEntity.getId(); + this.metaStoreManager = metaStoreManager; + this.resolutionManifestFactory = resolutionManifestFactory; + this.principal = principal; + } + + 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(); + + // Validate the document schema, then resolve its source tables before persisting. Duplicate + // names are caught by createEntityIfNotExists below (ENTITY_ALREADY_EXISTS), so there is no + // separate existence pre-check: the semantic-model path is not registered on this request's + // resolution manifest, and probing it there would fail. + JsonNode semanticModel = validateDocument(document); + resolveAndValidateSources(semanticModel); + + 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)); + } + + JsonNode semanticModel = validateDocument(document); + resolveAndValidateSources(semanticModel); + + 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(), Review Comment: inline import ? ########## extensions/semantic-models/src/main/java/org/apache/polaris/service/catalog/semanticmodel/SemanticModelCatalog.java: ########## @@ -0,0 +1,378 @@ +/* + * 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.core.JsonProcessingException; +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.auth.PolarisPrincipal; +import org.apache.polaris.core.catalog.PolarisCatalogHelpers; +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.PolarisResolutionManifest; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.apache.polaris.core.persistence.resolver.ResolutionManifestFactory; +import org.apache.polaris.core.persistence.resolver.ResolvedPathKey; +import org.apache.polaris.core.persistence.resolver.ResolverPath; +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(); + + /** + * Separator between the namespace path and the name in an OSI {@code dataset.source}, per the IRC + * catalog object-identifier scheme (e.g. {@code sales.store_sales}). + */ + private static final char SOURCE_SEPARATOR = '.'; + + private final CallContext callContext; + private final PolarisResolutionManifestCatalogView resolvedEntityView; + private final CatalogEntity catalogEntity; + private final long catalogId; + private final PolarisMetaStoreManager metaStoreManager; + private final ResolutionManifestFactory resolutionManifestFactory; + private final PolarisPrincipal principal; + + public SemanticModelCatalog( + PolarisMetaStoreManager metaStoreManager, + CallContext callContext, + PolarisResolutionManifestCatalogView resolvedEntityView, + ResolutionManifestFactory resolutionManifestFactory, + PolarisPrincipal principal) { + this.callContext = callContext; + this.resolvedEntityView = resolvedEntityView; + this.catalogEntity = resolvedEntityView.getResolvedCatalogEntity(); + this.catalogId = catalogEntity.getId(); + this.metaStoreManager = metaStoreManager; + this.resolutionManifestFactory = resolutionManifestFactory; + this.principal = principal; + } + + 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(); + + // Validate the document schema, then resolve its source tables before persisting. Duplicate + // names are caught by createEntityIfNotExists below (ENTITY_ALREADY_EXISTS), so there is no + // separate existence pre-check: the semantic-model path is not registered on this request's + // resolution manifest, and probing it there would fail. + JsonNode semanticModel = validateDocument(document); + resolveAndValidateSources(semanticModel); + + 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)); + } + + JsonNode semanticModel = validateDocument(document); + resolveAndValidateSources(semanticModel); + + 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())); + } + } + + 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()); + } + + /** + * Validates the OSI document against the Ossie JSON schema and returns the parsed {@code + * semantic_model} body. Currently only checks that the required body is present and well-formed + * JSON; full schema validation is deferred to a {@link SemanticDocumentValidator} implementation. + * + * @return the parsed {@code semantic_model} node + * @throws BadRequestException if the body is missing/blank or not valid JSON + */ + private JsonNode validateDocument(SemanticModelDocument document) { + String body = document.getSemanticModel(); + if (body == null || body.isBlank()) { + throw new BadRequestException("Semantic model document must not be empty"); + } + try { + // TODO: delegate full Ossie JSON-schema validation to SemanticDocumentValidator. + return MAPPER.readTree(body); + } catch (JsonProcessingException e) { + throw new BadRequestException( + "Field 'semantic_model' is not valid JSON: %s", e.getOriginalMessage()); + } + } + + /** + * Resolves and validates every {@code dataset.source} in the parsed OSI document against the + * current catalog. Every dataset must define a string {@code source}; a missing or non-string + * source, or one that does not resolve to a {@code TABLE_LIKE} entity, fails with 400 and a + * JSON-Pointer to the offending dataset. Column-level checks are deferred (F5). Review Comment: is this something a catalog should validate ? or can rely on engine to have done due deligence ? like normal views ? -- 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]
