Search in sources :

Example 1 with OptimizationContext

use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.

the class DrdsSqlCompiler method dispatch.

public MycatRel dispatch(OptimizationContext optimizationContext, DrdsSql drdsSql, SchemaPlus plus) {
    SQLStatement sqlStatement = drdsSql.getParameterizedStatement();
    if (sqlStatement instanceof SQLSelectStatement) {
        return compileQuery(optimizationContext, plus, drdsSql);
    }
    MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
    if (sqlStatement instanceof MySqlInsertStatement) {
        MySqlInsertStatement insertStatement = (MySqlInsertStatement) sqlStatement;
        String schemaName = SQLUtils.normalize(insertStatement.getTableSource().getSchema());
        String tableName = SQLUtils.normalize(insertStatement.getTableName().getSimpleName());
        TableHandler logicTable = Objects.requireNonNull(metadataManager.getTable(schemaName, tableName));
        switch(logicTable.getType()) {
            case SHARDING:
                MycatInsertRel mycatInsertRel = new MycatInsertRel(sqlStatement, false);
                optimizationContext.saveAlways();
                return mycatInsertRel;
            case GLOBAL:
                return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
            case NORMAL:
                return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
            case CUSTOM:
                throw new UnsupportedOperationException();
        }
    } else if (sqlStatement instanceof MySqlUpdateStatement) {
        SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlUpdateStatement) sqlStatement).getTableSource();
        String schemaName = SQLUtils.normalize(tableSource.getSchema());
        String tableName = SQLUtils.normalize(((MySqlUpdateStatement) sqlStatement).getTableName().getSimpleName());
        TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
        switch(logicTable.getType()) {
            case SHARDING:
                return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
            case GLOBAL:
                {
                    return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
                }
            case NORMAL:
                {
                    return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
                }
            case CUSTOM:
                throw new UnsupportedOperationException();
        }
    } else if (sqlStatement instanceof SQLReplaceStatement) {
        SQLExprTableSource tableSource = (SQLExprTableSource) ((SQLReplaceStatement) sqlStatement).getTableSource();
        String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
        String tableName = SQLUtils.normalize(((SQLReplaceStatement) sqlStatement).getTableName().getSimpleName());
        TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
        switch(logicTable.getType()) {
            case SHARDING:
                return compileUpdate(logicTable, optimizationContext, drdsSql, plus);
            case GLOBAL:
                {
                    return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
                }
            case NORMAL:
                {
                    return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
                }
            case CUSTOM:
                throw new UnsupportedOperationException();
        }
    } else if (sqlStatement instanceof MySqlDeleteStatement) {
        SQLExprTableSource tableSource = (SQLExprTableSource) ((MySqlDeleteStatement) sqlStatement).getTableSource();
        String schemaName = SQLUtils.normalize(Optional.ofNullable(tableSource).map(i -> i.getSchema()).orElse(null));
        String tableName = SQLUtils.normalize(((MySqlDeleteStatement) sqlStatement).getTableName().getSimpleName());
        TableHandler logicTable = metadataManager.getTable(schemaName, tableName);
        switch(logicTable.getType()) {
            case SHARDING:
                return compileDelete(logicTable, optimizationContext, drdsSql, plus);
            case GLOBAL:
                {
                    return complieGlobalUpdate(optimizationContext, drdsSql, sqlStatement, (GlobalTable) logicTable);
                }
            case NORMAL:
                {
                    return complieNormalUpdate(optimizationContext, drdsSql, sqlStatement, (NormalTable) logicTable);
                }
            case CUSTOM:
                throw new UnsupportedOperationException();
        }
    }
    return null;
}
Also used : Programs(org.apache.calcite.tools.Programs) SneakyThrows(lombok.SneakyThrows) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) LoggerFactory(org.slf4j.LoggerFactory) MycatHepJoinClustering(org.apache.calcite.rel.rules.MycatHepJoinClustering) HBTParser(io.mycat.hbt.parser.HBTParser) SqlNode(org.apache.calcite.sql.SqlNode) RelBuilder(org.apache.calcite.tools.RelBuilder) HepProgram(org.apache.calcite.plan.hep.HepProgram) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) HepMatchOrder(org.apache.calcite.plan.hep.HepMatchOrder) PlanImpl(io.mycat.calcite.spm.PlanImpl) Program(org.apache.calcite.tools.Program) ImmutableSet(com.google.common.collect.ImmutableSet) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) ImmutableMap(com.google.common.collect.ImmutableMap) MycatAggDistinctRule(io.mycat.calcite.rewriter.MycatAggDistinctRule) io.mycat.calcite.rules(io.mycat.calcite.rules) Table(org.apache.calcite.schema.Table) MycatUpdateRel(io.mycat.calcite.physical.MycatUpdateRel) ParseNode(io.mycat.hbt.parser.ParseNode) MultiJoin(org.apache.calcite.rel.rules.MultiJoin) Schema(io.mycat.hbt.ast.base.Schema) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) LocalRules(io.mycat.calcite.localrel.LocalRules) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) NotNull(org.jetbrains.annotations.NotNull) java.util(java.util) Getter(lombok.Getter) DefaultRelMetadataProvider(org.apache.calcite.rel.metadata.DefaultRelMetadataProvider) RelRoot(org.apache.calcite.rel.RelRoot) DrdsExecutorCompiler.getCodeExecuterContext(io.mycat.DrdsExecutorCompiler.getCodeExecuterContext) HBTQueryConvertor(io.mycat.hbt.HBTQueryConvertor) SQLReplaceStatement(com.alibaba.druid.sql.ast.statement.SQLReplaceStatement) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) ImmutableList(com.google.common.collect.ImmutableList) org.apache.calcite.rel.type(org.apache.calcite.rel.type) HintStrategyTable(org.apache.calcite.rel.hint.HintStrategyTable) RelDecorrelator(org.apache.calcite.sql2rel.RelDecorrelator) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) MycatTopN(io.mycat.calcite.physical.MycatTopN) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) io.mycat.calcite.table(io.mycat.calcite.table) org.apache.calcite.rel.core(org.apache.calcite.rel.core) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Logger(org.slf4j.Logger) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) io.mycat.calcite(io.mycat.calcite) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) MatierialRewriter(io.mycat.calcite.rewriter.MatierialRewriter) MycatProject(io.mycat.calcite.physical.MycatProject) SchemaConvertor(io.mycat.hbt.SchemaConvertor) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) RelNode(org.apache.calcite.rel.RelNode) AggregateExpandDistinctAggregatesRule(org.apache.calcite.rel.rules.AggregateExpandDistinctAggregatesRule) SqlDialect(org.apache.calcite.sql.SqlDialect) MycatViewIndexViewRule(io.mycat.calcite.logical.MycatViewIndexViewRule) SQLRBORewriter(io.mycat.calcite.rewriter.SQLRBORewriter) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) CoreRules(org.apache.calcite.rel.rules.CoreRules) NameMap(io.mycat.util.NameMap) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) RelMetadataProvider(org.apache.calcite.rel.metadata.RelMetadataProvider) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) org.apache.calcite.plan(org.apache.calcite.plan) Plan(io.mycat.calcite.spm.Plan) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) MySqlDeleteStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLReplaceStatement(com.alibaba.druid.sql.ast.statement.SQLReplaceStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel)

Example 2 with OptimizationContext

use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.

the class MemPlanCache method add.

public synchronized PlanResultSet add(boolean fix, DrdsSql drdsSql) {
    Long baselineId = null;
    Baseline baseline = this.getBaseline(drdsSql);
    DrdsSqlCompiler drdsSqlCompiler = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
    OptimizationContext optimizationContext = new OptimizationContext();
    MycatRel mycatRel = drdsSqlCompiler.dispatch(optimizationContext, drdsSql);
    RelJsonWriter relJsonWriter = new RelJsonWriter();
    mycatRel.explain(relJsonWriter);
    long hash = planIds.nextPlanId();
    BaselinePlan newBaselinePlan = new BaselinePlan(drdsSql.getParameterizedSQL(), relJsonWriter.asString(), hash, baselineId = baseline.getBaselineId(), null);
    getCodeExecuterContext(baseline, newBaselinePlan, optimizationContext, mycatRel);
    return saveBaselinePlan(fix, false, baseline, newBaselinePlan);
}
Also used : MycatRel(io.mycat.calcite.MycatRel) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter)

Example 3 with OptimizationContext

use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.

the class PartitionKeyJoinTest method parse.

public static Explain parse(DrdsConst drdsConst, String sql) {
    DrdsSqlCompiler drds = getDrds(drdsConst);
    DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
    OptimizationContext optimizationContext = new OptimizationContext();
    MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
    Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
    return new Explain(plan, drdsSqlWithParams);
}
Also used : MycatRel(io.mycat.calcite.MycatRel) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) Plan(io.mycat.calcite.spm.Plan) PlanImpl(io.mycat.calcite.spm.PlanImpl)

Example 4 with OptimizationContext

use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.

the class ShardingJoinRBOMegreTest method parse.

public static Explain parse(String sql) {
    DrdsSqlCompiler drds = getDrds();
    DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sql, null);
    OptimizationContext optimizationContext = new OptimizationContext();
    MycatRel dispatch = drds.dispatch(optimizationContext, drdsSqlWithParams);
    Plan plan = new PlanImpl(dispatch, DrdsExecutorCompiler.getCodeExecuterContext(optimizationContext.relNodeContext.getConstantMap(), dispatch, false), drdsSqlWithParams.getAliasList());
    return new Explain(plan, drdsSqlWithParams);
}
Also used : DrdsSqlCompiler(io.mycat.DrdsSqlCompiler) MycatRel(io.mycat.calcite.MycatRel) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) Plan(io.mycat.calcite.spm.Plan) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) PlanImpl(io.mycat.calcite.spm.PlanImpl)

Example 5 with OptimizationContext

use of io.mycat.calcite.rewriter.OptimizationContext in project Mycat2 by MyCATApache.

the class UpdateSQLHandler method getPlan.

@Nullable
public static Plan getPlan(DrdsSqlWithParams drdsSqlWithParams) {
    UpdatePlanCache updatePlanCache = MetaClusterCurrent.wrapper(UpdatePlanCache.class);
    Plan plan = updatePlanCache.computeIfAbsent(drdsSqlWithParams.getParameterizedSQL(), s -> {
        DrdsSqlCompiler drdsRunner = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
        OptimizationContext optimizationContext = new OptimizationContext();
        MycatRel dispatch = drdsRunner.dispatch(optimizationContext, drdsSqlWithParams);
        return DrdsExecutorCompiler.convertToExecuter(dispatch);
    });
    return plan;
}
Also used : MycatRel(io.mycat.calcite.MycatRel) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) Plan(io.mycat.calcite.spm.Plan) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

OptimizationContext (io.mycat.calcite.rewriter.OptimizationContext)10 MycatRel (io.mycat.calcite.MycatRel)9 Plan (io.mycat.calcite.spm.Plan)9 PlanImpl (io.mycat.calcite.spm.PlanImpl)8 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)4 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)4 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLReplaceStatement (com.alibaba.druid.sql.ast.statement.SQLReplaceStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 MySqlDeleteStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 MySqlUpdateStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 DrdsExecutorCompiler.getCodeExecuterContext (io.mycat.DrdsExecutorCompiler.getCodeExecuterContext)1 io.mycat.calcite (io.mycat.calcite)1 LocalRules (io.mycat.calcite.localrel.LocalRules)1