Search in sources :

Example 11 with Plan

use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.

the class UpdateSQLHandler method updateHandler.

@SneakyThrows
public static Future<Void> updateHandler(SQLStatement sqlStatement, MycatDataContext dataContext, SQLTableSource tableSourceArg, Response receiver) {
    boolean insert = sqlStatement instanceof SQLInsertStatement;
    if (tableSourceArg instanceof SQLExprTableSource) {
        SQLExprTableSource tableSource = (SQLExprTableSource) tableSourceArg;
        String schemaName = Optional.ofNullable(tableSource.getSchema() == null ? dataContext.getDefaultSchema() : tableSource.getSchema()).map(i -> SQLUtils.normalize(i)).orElse(null);
        String tableName = SQLUtils.normalize(tableSource.getTableName());
        if (schemaName == null) {
            return receiver.sendError("unknown schema", MySQLErrorCode.ER_UNKNOWN_ERROR);
        }
        tableSource.setSchema(schemaName);
        SchemaHandler schemaHandler;
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        Optional<NameMap<SchemaHandler>> handlerMapOptional = Optional.ofNullable(metadataManager.getSchemaMap());
        Optional<String> targetNameOptional = Optional.ofNullable(metadataManager.getPrototype());
        if (!handlerMapOptional.isPresent()) {
            if (targetNameOptional.isPresent()) {
                if (insert) {
                    DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                    return receiver.proxyInsert(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
                } else {
                    DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
                    return receiver.proxyUpdate(Collections.singletonList(targetNameOptional.get()), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
                }
            } else {
                return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
            }
        } else {
            NameMap<SchemaHandler> handlerMap = handlerMapOptional.get();
            schemaHandler = Optional.ofNullable(handlerMap.get(schemaName)).orElseGet(() -> {
                if (dataContext.getDefaultSchema() == null) {
                    // 可能schemaName有值,但是值名不是配置的名字
                    throw new MycatException("unknown schema:" + schemaName);
                }
                return handlerMap.get(dataContext.getDefaultSchema());
            });
            if (schemaHandler == null) {
                return receiver.sendError(new MycatException("Unable to route:" + sqlStatement));
            }
        }
        String defaultTargetName = schemaHandler.defaultTargetName();
        NameMap<TableHandler> tableMap = schemaHandler.logicTables();
        TableHandler tableHandler = tableMap.get(tableName);
        // /////////////////////////////common///////////////////////////////
        if (tableHandler == null) {
            if (insert) {
                DrdsSqlWithParams drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                return receiver.proxyInsert(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
            } else {
                DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, null);
                return receiver.proxyUpdate(Collections.singletonList(Objects.requireNonNull(defaultTargetName, "can not route :" + drdsSqlWithParams)), drdsSqlWithParams.getParameterizedSQL(), drdsSqlWithParams.getParams());
            }
        }
        switch(tableHandler.getType()) {
            case NORMAL:
                DrdsSqlWithParams drdsSqlWithParams;
                if (insert) {
                    drdsSqlWithParams = MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement);
                } else {
                    drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
                }
                HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
                if (hackRouter.analyse()) {
                    Pair<String, String> plan = hackRouter.getPlan();
                    if (insert) {
                        return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
                    } else {
                        return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
                    }
                }
                break;
            default:
                break;
        }
        DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
        return executeUpdate(drdsSqlWithParams, dataContext, receiver, schemaName);
    }
    DrdsSqlWithParams drdsSqlWithParams = insert ? MycatPreparedStatementUtil.outputToParameterizedProxySql((MySqlInsertStatement) sqlStatement) : DrdsRunnerHelper.preParse(sqlStatement, dataContext.getDefaultSchema());
    HackRouter hackRouter = new HackRouter(drdsSqlWithParams.getParameterizedStatement(), dataContext);
    if (hackRouter.analyse()) {
        Pair<String, String> plan = hackRouter.getPlan();
        if (insert) {
            return receiver.proxyInsert(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
        } else {
            return receiver.proxyUpdate(Collections.singletonList(plan.getKey()), plan.getValue(), drdsSqlWithParams.getParams());
        }
    }
    return receiver.sendError(new MycatException("can not route " + sqlStatement));
}
Also used : io.mycat(io.mycat) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) SneakyThrows(lombok.SneakyThrows) SQLRequest(io.mycat.sqlhandler.SQLRequest) UpdatePlanCache(io.mycat.calcite.spm.UpdatePlanCache) SchemaHandler(io.mycat.calcite.table.SchemaHandler) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlUpdateStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement) PlanImplementor(io.mycat.calcite.plan.PlanImplementor) MycatRel(io.mycat.calcite.MycatRel) MySQLErrorCode(io.mycat.beans.mysql.MySQLErrorCode) OptimizationContext(io.mycat.calcite.rewriter.OptimizationContext) Pair(io.mycat.util.Pair) SQLUtils(com.alibaba.druid.sql.SQLUtils) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) Future(io.vertx.core.Future) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) NameMap(io.mycat.util.NameMap) MycatPreparedStatementUtil(io.mycat.calcite.executor.MycatPreparedStatementUtil) Optional(java.util.Optional) DrdsRunnerHelper(io.mycat.calcite.DrdsRunnerHelper) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) Collections(java.util.Collections) Plan(io.mycat.calcite.spm.Plan) SchemaHandler(io.mycat.calcite.table.SchemaHandler) NameMap(io.mycat.util.NameMap) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SneakyThrows(lombok.SneakyThrows)

Example 12 with Plan

use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.

the class ShardingJoinTest 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 13 with Plan

use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.

the class UnionAllTest 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 14 with Plan

use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.

the class AllShardingJoinTest 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 15 with Plan

use of io.mycat.calcite.spm.Plan in project Mycat2 by MyCATApache.

the class BkaJoinTest method parse.

public static Explain parse(String sql) {
    DrdsSqlCompiler drds = getDrds();
    DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse("/*+ mycat:use_bka_join() */" + 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)

Aggregations

Plan (io.mycat.calcite.spm.Plan)16 MycatRel (io.mycat.calcite.MycatRel)9 OptimizationContext (io.mycat.calcite.rewriter.OptimizationContext)9 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)7 PlanImpl (io.mycat.calcite.spm.PlanImpl)7 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)4 MysqlPayloadObject (io.mycat.api.collector.MysqlPayloadObject)4 SneakyThrows (lombok.SneakyThrows)4 XaSqlConnection (cn.mycat.vertx.xa.XaSqlConnection)3 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)3 io.mycat (io.mycat)3 ExecutorProvider (io.mycat.calcite.ExecutorProvider)3 PrepareExecutor (io.mycat.calcite.PrepareExecutor)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)2 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)2 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)2 MycatInsertRel (io.mycat.calcite.physical.MycatInsertRel)2 MycatUpdateRel (io.mycat.calcite.physical.MycatUpdateRel)2 PlanImplementor (io.mycat.calcite.plan.PlanImplementor)2