Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public interface SemanticModelMetaMapper {
@Result(property = "version", column = "version"),
@Result(property = "semanticModelName", column = "version_semantic_model_name"),
@Result(property = "semanticModelComment", column = "semantic_model_comment"),
@Result(property = "semanticModelDefinition", column = "semantic_model_definition"),
@Result(property = "datasets", column = "datasets"),
@Result(property = "relationships", column = "relationships"),
@Result(property = "metrics", column = "metrics"),
@Result(property = "aiContext", column = "ai_context"),
@Result(property = "customExtensions", column = "custom_extensions"),
@Result(property = "properties", column = "properties"),
@Result(property = "auditInfo", column = "version_audit_info"),
@Result(property = "deletedAt", column = "version_deleted_at")
Expand All @@ -80,7 +84,8 @@ public interface SemanticModelMetaMapper {
column =
"{id,version_metalake_id,version_catalog_id,version_schema_id,"
+ "version_semantic_model_id,version,version_semantic_model_name,"
+ "semantic_model_comment,semantic_model_definition,properties,"
+ "semantic_model_comment,datasets,relationships,metrics,ai_context,"
+ "custom_extensions,properties,"
+ "version_audit_info,version_deleted_at}",
one = @One(resultMap = "mapToSemanticModelVersionInfoPO"))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.storage.relational.mapper;

import java.util.List;
import org.apache.gravitino.storage.relational.po.SemanticModelMetricPO;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

/** A MyBatis mapper for identities of metrics contained in Semantic Models. */
public interface SemanticModelMetricMapper {
/** The metric identity table name. */
String TABLE_NAME = "semantic_model_metric_meta";

/** Inserts a new metric identity. */
@InsertProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "insertSemanticModelMetric")
void insertSemanticModelMetric(@Param("metric") SemanticModelMetricPO metricPO);

/** Locks and reads the live identities of a Semantic Model. */
@SelectProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "selectSemanticModelMetricsByModelIdForUpdate")
List<SemanticModelMetricPO> selectSemanticModelMetricsByModelIdForUpdate(
@Param("semanticModelId") Long semanticModelId);

/** Retires selected live identities belonging to a Semantic Model. */
@UpdateProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "softDeleteSemanticModelMetricsByIds")
Integer softDeleteSemanticModelMetricsByIds(
@Param("semanticModelId") Long semanticModelId, @Param("metricIds") List<Long> metricIds);

/** Retires all live metric identities of a Semantic Model. */
@UpdateProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "softDeleteSemanticModelMetricsBySemanticModelId")
Integer softDeleteSemanticModelMetricsBySemanticModelId(
@Param("semanticModelId") Long semanticModelId);

/** Retires live metric identities under schemas. */
@UpdateProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "softDeleteSemanticModelMetricsBySchemaIds")
Integer softDeleteSemanticModelMetricsBySchemaIds(@Param("schemaIds") List<Long> schemaIds);

/** Retires live metric identities under a catalog. */
@UpdateProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "softDeleteSemanticModelMetricsByCatalogId")
Integer softDeleteSemanticModelMetricsByCatalogId(@Param("catalogId") Long catalogId);

/** Retires live metric identities under a metalake. */
@UpdateProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "softDeleteSemanticModelMetricsByMetalakeId")
Integer softDeleteSemanticModelMetricsByMetalakeId(@Param("metalakeId") Long metalakeId);

/** Permanently deletes retired identities older than a timeline. */
@DeleteProvider(
type = SemanticModelMetricSQLProviderFactory.class,
method = "deleteSemanticModelMetricsByLegacyTimeline")
Integer deleteSemanticModelMetricsByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.storage.relational.mapper;

import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
import org.apache.gravitino.storage.relational.mapper.provider.base.SemanticModelMetricBaseSQLProvider;
import org.apache.gravitino.storage.relational.mapper.provider.postgresql.SemanticModelMetricPostgreSQLProvider;
import org.apache.gravitino.storage.relational.po.SemanticModelMetricPO;
import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
import org.apache.ibatis.annotations.Param;

/** Selects database-specific SQL providers for Semantic Model metric identities. */
public class SemanticModelMetricSQLProviderFactory {
private static final Map<JDBCBackendType, SemanticModelMetricBaseSQLProvider> PROVIDERS =
ImmutableMap.of(
JDBCBackendType.MYSQL, new SemanticModelMetricBaseSQLProvider(),
JDBCBackendType.H2, new SemanticModelMetricBaseSQLProvider(),
JDBCBackendType.POSTGRESQL, new SemanticModelMetricPostgreSQLProvider());

/** Returns the SQL provider for the configured relational backend. */
public static SemanticModelMetricBaseSQLProvider getProvider() {
String databaseId =
SqlSessionFactoryHelper.getInstance()
.getSqlSessionFactory()
.getConfiguration()
.getDatabaseId();
return PROVIDERS.get(JDBCBackendType.fromString(databaseId));
}

/** Inserts a new metric identity. */
public static String insertSemanticModelMetric(@Param("metric") SemanticModelMetricPO metricPO) {
return getProvider().insertSemanticModelMetric(metricPO);
}

/** Locks and reads the live identities of a Semantic Model. */
public static String selectSemanticModelMetricsByModelIdForUpdate(
@Param("semanticModelId") Long semanticModelId) {
return getProvider().selectSemanticModelMetricsByModelIdForUpdate(semanticModelId);
}

/** Retires selected live identities belonging to a Semantic Model. */
public static String softDeleteSemanticModelMetricsByIds(
@Param("semanticModelId") Long semanticModelId, @Param("metricIds") List<Long> metricIds) {
return getProvider().softDeleteSemanticModelMetricsByIds(semanticModelId, metricIds);
}

/** Retires all live metric identities of a Semantic Model. */
public static String softDeleteSemanticModelMetricsBySemanticModelId(
@Param("semanticModelId") Long semanticModelId) {
return getProvider().softDeleteSemanticModelMetricsBySemanticModelId(semanticModelId);
}

/** Retires live metric identities under schemas. */
public static String softDeleteSemanticModelMetricsBySchemaIds(
@Param("schemaIds") List<Long> schemaIds) {
return getProvider().softDeleteSemanticModelMetricsBySchemaIds(schemaIds);
}

/** Retires live metric identities under a catalog. */
public static String softDeleteSemanticModelMetricsByCatalogId(
@Param("catalogId") Long catalogId) {
return getProvider().softDeleteSemanticModelMetricsByCatalogId(catalogId);
}

/** Retires live metric identities under a metalake. */
public static String softDeleteSemanticModelMetricsByMetalakeId(
@Param("metalakeId") Long metalakeId) {
return getProvider().softDeleteSemanticModelMetricsByMetalakeId(metalakeId);
}

/** Permanently deletes retired identities older than a timeline. */
public static String deleteSemanticModelMetricsByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
return getProvider().deleteSemanticModelMetricsByLegacyTimeline(legacyTimeline, limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.gravitino.storage.relational.mapper.SchemaMetaMapper;
import org.apache.gravitino.storage.relational.mapper.SecurableObjectMapper;
import org.apache.gravitino.storage.relational.mapper.SemanticModelMetaMapper;
import org.apache.gravitino.storage.relational.mapper.SemanticModelMetricMapper;
import org.apache.gravitino.storage.relational.mapper.SemanticModelVersionInfoMapper;
import org.apache.gravitino.storage.relational.mapper.StatisticMetaMapper;
import org.apache.gravitino.storage.relational.mapper.TableColumnMapper;
Expand Down Expand Up @@ -87,6 +88,7 @@ public List<Class<?>> getMapperClasses() {
SchemaMetaMapper.class,
SecurableObjectMapper.class,
SemanticModelMetaMapper.class,
SemanticModelMetricMapper.class,
SemanticModelVersionInfoMapper.class,
StatisticMetaMapper.class,
TableColumnMapper.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class SemanticModelMetaBaseSQLProvider {
+ " smvi.schema_id as version_schema_id,"
+ " smvi.semantic_model_id as version_semantic_model_id, smvi.version,"
+ " smvi.semantic_model_name as version_semantic_model_name,"
+ " smvi.semantic_model_comment, smvi.semantic_model_definition, smvi.properties,"
+ " smvi.semantic_model_comment, smvi.datasets, smvi.relationships, smvi.metrics,"
+ " smvi.ai_context, smvi.custom_extensions, smvi.properties,"
+ " smvi.audit_info as version_audit_info,"
+ " smvi.deleted_at as version_deleted_at";

Expand Down Expand Up @@ -238,7 +239,11 @@ private String qualifiedNameSelect(boolean filterBySemanticModelName) {
smvi.version,
smvi.semantic_model_name as version_semantic_model_name,
smvi.semantic_model_comment,
smvi.semantic_model_definition,
smvi.datasets,
smvi.relationships,
smvi.metrics,
smvi.ai_context,
smvi.custom_extensions,
smvi.properties,
smvi.audit_info as version_audit_info,
smvi.deleted_at as version_deleted_at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.storage.relational.mapper.provider.base;

import static org.apache.gravitino.storage.relational.mapper.SemanticModelMetricMapper.TABLE_NAME;

import java.util.List;
import org.apache.gravitino.storage.relational.po.SemanticModelMetricPO;
import org.apache.ibatis.annotations.Param;

/** Provides MySQL-compatible SQL for Semantic Model metric identities. */
public class SemanticModelMetricBaseSQLProvider {

/** Returns SQL for inserting a new identity. */
public String insertSemanticModelMetric(@Param("metric") SemanticModelMetricPO metricPO) {
return "INSERT INTO "
+ TABLE_NAME
+ " (metric_id, metalake_id, catalog_id, schema_id, semantic_model_id, metric_name, deleted_at)"
+ " VALUES (#{metric.metricId}, #{metric.metalakeId}, #{metric.catalogId},"
+ " #{metric.schemaId}, #{metric.semanticModelId}, #{metric.metricName}, #{metric.deletedAt})";
}

/** Returns a locking read of live identities after the containing model's concurrency check. */
public String selectSemanticModelMetricsByModelIdForUpdate(
@Param("semanticModelId") Long semanticModelId) {
return "SELECT metric_id as metricId, metalake_id as metalakeId, catalog_id as catalogId,"
+ " schema_id as schemaId, semantic_model_id as semanticModelId, metric_name as metricName,"
+ " deleted_at as deletedAt FROM "
+ TABLE_NAME
+ " WHERE semantic_model_id = #{semanticModelId} AND deleted_at = 0 FOR UPDATE";
}

/** Returns SQL for retiring selected live identities of a model. */
public String softDeleteSemanticModelMetricsByIds(
@Param("semanticModelId") Long semanticModelId, @Param("metricIds") List<Long> metricIds) {
return "<script>"
+ softDeleteBy(
"semantic_model_id = #{semanticModelId} AND metric_id IN ("
+ "<foreach collection='metricIds' item='metricId' separator=','>"
+ "#{metricId}</foreach>)")
+ "</script>";
}

/** Returns SQL for retiring all live identities of a model. */
public String softDeleteSemanticModelMetricsBySemanticModelId(
@Param("semanticModelId") Long semanticModelId) {
return softDeleteBy("semantic_model_id = #{semanticModelId}");
}

/** Returns SQL for retiring live identities under schemas. */
public String softDeleteSemanticModelMetricsBySchemaIds(
@Param("schemaIds") List<Long> schemaIds) {
return "<script>"
+ softDeleteBy(
"schema_id IN ("
+ "<foreach collection='schemaIds' item='schemaId' separator=','>"
+ "#{schemaId}</foreach>)")
+ "</script>";
}

/** Returns SQL for retiring live identities under a catalog. */
public String softDeleteSemanticModelMetricsByCatalogId(@Param("catalogId") Long catalogId) {
return softDeleteBy("catalog_id = #{catalogId}");
}

/** Returns SQL for retiring live identities under a metalake. */
public String softDeleteSemanticModelMetricsByMetalakeId(@Param("metalakeId") Long metalakeId) {
return softDeleteBy("metalake_id = #{metalakeId}");
}

/** Returns SQL for permanently deleting old retired identities. */
public String deleteSemanticModelMetricsByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
return "DELETE FROM "
+ TABLE_NAME
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}";
}

/** Returns the database expression for the current time in epoch milliseconds. */
protected String currentTimestampMillis() {
return "(UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000";
}

private String softDeleteBy(String condition) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = "
+ currentTimestampMillis()
+ " WHERE "
+ condition
+ " AND deleted_at = 0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ public String insertSemanticModelVersionInfo(
return "INSERT INTO "
+ SemanticModelVersionInfoMapper.TABLE_NAME
+ " (metalake_id, catalog_id, schema_id, semantic_model_id, version,"
+ " semantic_model_name, semantic_model_comment, semantic_model_definition,"
+ " semantic_model_name, semantic_model_comment, datasets, relationships, metrics,"
+ " ai_context, custom_extensions,"
+ " properties, audit_info, deleted_at)"
+ " VALUES (#{semanticModelVersionInfo.metalakeId},"
+ " #{semanticModelVersionInfo.catalogId}, #{semanticModelVersionInfo.schemaId},"
+ " #{semanticModelVersionInfo.semanticModelId}, #{semanticModelVersionInfo.version},"
+ " #{semanticModelVersionInfo.semanticModelName},"
+ " #{semanticModelVersionInfo.semanticModelComment},"
+ " #{semanticModelVersionInfo.semanticModelDefinition},"
+ " #{semanticModelVersionInfo.datasets}, #{semanticModelVersionInfo.relationships},"
+ " #{semanticModelVersionInfo.metrics}, #{semanticModelVersionInfo.aiContext},"
+ " #{semanticModelVersionInfo.customExtensions},"
+ " #{semanticModelVersionInfo.properties}, #{semanticModelVersionInfo.auditInfo},"
+ " #{semanticModelVersionInfo.deletedAt})";
}
Expand All @@ -51,7 +54,8 @@ public String selectSemanticModelVersionInfoBySemanticModelIdAndVersion(
+ " schema_id as schemaId, semantic_model_id as semanticModelId, version as version,"
+ " semantic_model_name as semanticModelName,"
+ " semantic_model_comment as semanticModelComment,"
+ " semantic_model_definition as semanticModelDefinition, properties as properties,"
+ " datasets as datasets, relationships as relationships, metrics as metrics,"
+ " ai_context as aiContext, custom_extensions as customExtensions, properties as properties,"
+ " audit_info as auditInfo, deleted_at as deletedAt FROM "
+ SemanticModelVersionInfoMapper.TABLE_NAME
+ " WHERE semantic_model_id = #{semanticModelId}"
Expand Down
Loading
Loading