Search in sources :

Example 1 with DrdsSqlWithParams

use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.

the class PlanImpl method explain.

public List<String> explain(MycatDataContext dataContext, DrdsSqlWithParams drdsSql, boolean code) {
    ArrayList<String> list = new ArrayList<>();
    ExplainWriter explainWriter = new ExplainWriter();
    switch(this.type) {
        case PHYSICAL:
            Optional<ExplainDetail> singleViewOptional = ColocatedPlanner.executeQuery(dataContext, this, drdsSql);
            if (singleViewOptional.isPresent()) {
                ExplainDetail explainDetail = singleViewOptional.get();
                list.add("ColocatedPushDown:");
                list.add(explainDetail.toString());
            }
            list.add("\n");
            list.add("Plan:");
            String s = dumpPlan();
            list.addAll(Arrays.asList(s.split("\n")));
            List<SpecificSql> map = specificSql(drdsSql, dataContext);
            for (SpecificSql specificSql : map) {
                list.addAll(Arrays.asList(specificSql.toString().split("\n")));
            }
            if (code) {
                list.add("Code:");
                list.addAll(Arrays.asList(getCodeExecuterContext().getCodeContext().getCode().split("\n")));
            }
            break;
        case UPDATE:
            {
                Collection<VertxExecuter.EachSQL> eachSQLS = VertxUpdateExecuter.explainUpdate(drdsSql, dataContext);
                for (VertxExecuter.EachSQL eachSQL : eachSQLS) {
                    list.add(eachSQL.toString());
                }
                break;
            }
        case INSERT:
            {
                Iterable<VertxExecuter.EachSQL> eachSQLS = (VertxExecuter.explainInsert((SQLInsertStatement) drdsSql.getParameterizedStatement(), drdsSql.getParams()));
                for (VertxExecuter.EachSQL eachSQL : eachSQLS) {
                    list.add(eachSQL.toString());
                }
                break;
            }
        default:
            throw new IllegalStateException("Unexpected value: " + this.type);
    }
    for (String s1 : explainWriter.getText().split("\n")) {
        list.add(s1);
    }
    return list.stream().filter(i -> !i.isEmpty()).collect(Collectors.toList());
}
Also used : VertxExecuter(io.mycat.vertx.VertxExecuter) java.util(java.util) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) SqlNode(org.apache.calcite.sql.SqlNode) ExplainWriter(io.mycat.calcite.ExplainWriter) MycatTransientSQLTableScan(io.mycat.calcite.table.MycatTransientSQLTableScan) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) MycatRel(io.mycat.calcite.MycatRel) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ExplainDetail(io.mycat.ExplainDetail) MycatView(io.mycat.calcite.logical.MycatView) VertxUpdateExecuter(io.mycat.vertx.VertxUpdateExecuter) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) MycatCalciteSupport(io.mycat.calcite.MycatCalciteSupport) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) MycatSQLTableLookup(io.mycat.calcite.physical.MycatSQLTableLookup) MycatUpdateRel(io.mycat.calcite.physical.MycatUpdateRel) MycatInsertRel(io.mycat.calcite.physical.MycatInsertRel) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) MycatDataContext(io.mycat.MycatDataContext) SqlString(org.apache.calcite.sql.util.SqlString) Util(org.apache.calcite.util.Util) NotNull(org.jetbrains.annotations.NotNull) ColocatedPlanner(io.mycat.calcite.plan.ColocatedPlanner) VertxExecuter(io.mycat.vertx.VertxExecuter) SqlString(org.apache.calcite.sql.util.SqlString) ExplainWriter(io.mycat.calcite.ExplainWriter) ExplainDetail(io.mycat.ExplainDetail)

Example 2 with DrdsSqlWithParams

use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.

the class MycatSQLTableLookup method dispatchRightObservable.

public static Observable<Object[]> dispatchRightObservable(NewMycatDataContext context, MycatSQLTableLookup tableLookup, Observable<Object[]> leftInput) {
    MycatView rightView = (MycatView) tableLookup.getRight();
    CopyMycatRowMetaData rightRowMetaData = new CopyMycatRowMetaData(new CalciteRowMetaData(rightView.getRowType().getFieldList()));
    AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = (AsyncMycatDataContextImpl.SqlMycatDataContextImpl) context;
    Observable<@NonNull List<Object[]>> buffer;
    if (tableLookup.getType() == Type.BACK) {
        // semi 可以分解
        buffer = leftInput.buffer(300, 50);
    } else {
        // NONE 该运算不能分解
        buffer = leftInput.toList().toObservable();
    }
    Observable<Object[]> rightObservable = buffer.flatMap(argsList -> {
        if (argsList.isEmpty()) {
            return Observable.empty();
        }
        RexShuttle rexShuttle = argSolver(argsList);
        RelNode mycatInnerRelNode = rightView.getRelNode().accept(new RelShuttleImpl() {

            @Override
            public RelNode visit(RelNode other) {
                if (other instanceof Filter) {
                    Filter filter = (Filter) other;
                    return filter.accept(rexShuttle);
                } else if (other instanceof MycatTableLookupValues) {
                    MycatTableLookupValues mycatTableLookupValues = (MycatTableLookupValues) other;
                    return mycatTableLookupValues.apply(argsList);
                }
                return super.visit(other);
            }
        });
        MycatView newRightView = new MycatView(rightView.getTraitSet(), mycatInnerRelNode, rightView.getDistribution(), rightView.getCondition().map(c -> c.accept(rexShuttle)).orElse(null));
        DrdsSqlWithParams drdsSql = context.getDrdsSql();
        SqlNode sqlTemplate = newRightView.getSQLTemplate(DrdsSqlWithParams.isForUpdate(drdsSql.getParameterizedSQL()));
        ImmutableMultimap<String, SqlString> apply1 = newRightView.apply(context.getContext().getMergeUnionSize(), sqlTemplate, sqlMycatDataContext.getSqlMap(Collections.emptyMap(), newRightView, drdsSql, drdsSql.getHintDataNodeFilter()), drdsSql.getParams());
        return Observable.merge(sqlMycatDataContext.getObservables(apply1, rightRowMetaData, (sessionConnection, sql, extractParams, calciteRowMetaData) -> VertxExecuter.runQuery(sessionConnection, sql, extractParams, calciteRowMetaData)));
    });
    return rightObservable;
}
Also used : VertxExecuter(io.mycat.vertx.VertxExecuter) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) Getter(lombok.Getter) NewMycatDataContext(org.apache.calcite.runtime.NewMycatDataContext) SqlShuttle(org.apache.calcite.sql.util.SqlShuttle) RelOptCost(org.apache.calcite.plan.RelOptCost) NonNull(io.reactivex.rxjava3.annotations.NonNull) Filter(org.apache.calcite.rel.core.Filter) RelMdUtil(org.apache.calcite.rel.metadata.RelMdUtil) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) RexUtil(org.apache.calcite.rex.RexUtil) CorrelationId(org.apache.calcite.rel.core.CorrelationId) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) Observable(io.reactivex.rxjava3.core.Observable) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelOptCluster(org.apache.calcite.plan.RelOptCluster) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) NewMycatConnection(io.mycat.newquery.NewMycatConnection) io.mycat.calcite(io.mycat.calcite) org.apache.calcite.adapter.enumerable(org.apache.calcite.adapter.enumerable) MycatView(io.mycat.calcite.logical.MycatView) Future(io.vertx.core.Future) JoinInfo(org.apache.calcite.rel.core.JoinInfo) Collectors(java.util.stream.Collectors) org.apache.calcite.sql(org.apache.calcite.sql) List(java.util.List) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) MYCAT_SQL_LOOKUP_IN(io.mycat.calcite.MycatImplementor.MYCAT_SQL_LOOKUP_IN) org.apache.calcite.linq4j.tree(org.apache.calcite.linq4j.tree) JoinRelType(org.apache.calcite.rel.core.JoinRelType) org.apache.calcite.rel(org.apache.calcite.rel) SqlString(org.apache.calcite.sql.util.SqlString) RexShuttle(org.apache.calcite.rex.RexShuttle) Util(org.apache.calcite.util.Util) NotNull(org.jetbrains.annotations.NotNull) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Collections(java.util.Collections) RexCall(org.apache.calcite.rex.RexCall) RexShuttle(org.apache.calcite.rex.RexShuttle) AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) SqlString(org.apache.calcite.sql.util.SqlString) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SqlString(org.apache.calcite.sql.util.SqlString) MycatView(io.mycat.calcite.logical.MycatView) Filter(org.apache.calcite.rel.core.Filter) CalciteRowMetaData(io.mycat.calcite.resultset.CalciteRowMetaData) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with DrdsSqlWithParams

use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.

the class MycatPreparedStatementUtil method outputToParameterizedProxySql.

public static DrdsSqlWithParams outputToParameterizedProxySql(MySqlInsertStatement sqlStatement) {
    StringBuilder sb = new StringBuilder();
    List<Object> outputParameters = new ArrayList<>();
    MySqlExportParameterVisitor parameterVisitor = new MySqlExportParameterVisitor(outputParameters, sb, true) {
    };
    parameterVisitor.setShardingSupport(false);
    parameterVisitor.setFeatures(VisitorFeature.OutputParameterizedQuesUnMergeInList.mask | VisitorFeature.OutputParameterizedQuesUnMergeAnd.mask | VisitorFeature.OutputParameterizedUnMergeShardingTable.mask | VisitorFeature.OutputParameterizedQuesUnMergeOr.mask | VisitorFeature.OutputParameterizedQuesUnMergeValuesList.mask | VisitorFeature.OutputParameterized.mask);
    parameterVisitor.setInputParameters(Collections.emptyList());
    sqlStatement.accept(parameterVisitor);
    return new DrdsSqlWithParams(sb.toString(), outputParameters, false, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
}
Also used : ArrayList(java.util.ArrayList) MySqlExportParameterVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams)

Example 4 with DrdsSqlWithParams

use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.

the class ExecutorProviderImpl method runAsObjectArray.

@Override
public RowBaseIterator runAsObjectArray(MycatDataContext context, String sqlStatement) {
    DrdsSqlWithParams drdsSql = DrdsRunnerHelper.preParse(sqlStatement, context.getDefaultSchema());
    Plan plan = DrdsRunnerHelper.getPlan(drdsSql);
    AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, plan.getCodeExecuterContext(), drdsSql);
    PrepareExecutor prepareExecutor = prepare(plan);
    RowBaseIterator baseIterator = prepareExecutor.asRowBaseIterator(sqlMycatDataContext, plan.getMetaData());
    return baseIterator;
}
Also used : AsyncMycatDataContextImpl(io.mycat.AsyncMycatDataContextImpl) RowBaseIterator(io.mycat.api.collector.RowBaseIterator) PhysicalPlan(io.ordinate.engine.physicalplan.PhysicalPlan) Plan(io.mycat.calcite.spm.Plan) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams)

Example 5 with DrdsSqlWithParams

use of io.mycat.DrdsSqlWithParams in project Mycat2 by MyCATApache.

the class ExplainSQLHandler method onExecute.

@Override
@SneakyThrows
protected Future<Void> onExecute(SQLRequest<MySqlExplainStatement> request, MycatDataContext dataContext, Response response) {
    MySqlExplainStatement explainAst = request.getAst();
    explainAst.accept0(new MySqlASTVisitorAdapter() {

        @Override
        public boolean visit(SQLExprTableSource x) {
            if (x.getSchema() == null) {
                x.setSchema(dataContext.getDefaultSchema());
            }
            return super.visit(x);
        }
    });
    if (explainAst.isDescribe()) {
        SQLName tableName = explainAst.getTableName();
        if (tableName instanceof SQLIdentifierExpr && dataContext.getDefaultSchema() != null) {
            explainAst.setTableName(new SQLPropertyExpr(new SQLIdentifierExpr(dataContext.getDefaultSchema()), ((SQLIdentifierExpr) tableName).getName()));
        }
        return response.proxySelectToPrototype(explainAst.toUnformattedString(), Collections.emptyList());
    }
    SQLStatement statement = request.getAst().getStatement();
    boolean forUpdate = false;
    if (statement instanceof SQLSelectStatement) {
        forUpdate = ((SQLSelectStatement) explainAst.getStatement()).getSelect().getFirstQueryBlock().isForUpdate();
    }
    ResultSetBuilder builder = ResultSetBuilder.create().addColumnInfo("plan", JDBCType.VARCHAR);
    try {
        HackRouter hackRouter = new HackRouter(statement, dataContext);
        if (hackRouter.analyse()) {
            Pair<String, String> plan = hackRouter.getPlan();
            builder.addObjectRowPayload(Arrays.asList("targetName: " + plan.getKey() + "   sql: " + plan.getValue()));
        } else {
            List<SQLCommentHint> hints = explainAst.getHints();
            if (hints != null) {
                statement.setHeadHints(hints);
            }
            DrdsSqlWithParams drdsSqlWithParams = DrdsRunnerHelper.preParse(statement, dataContext.getDefaultSchema());
            Plan plan;
            if (statement instanceof SQLInsertStatement || statement instanceof SQLUpdateStatement || statement instanceof SQLDeleteStatement) {
                plan = UpdateSQLHandler.getPlan(drdsSqlWithParams);
            } else {
                plan = DrdsRunnerHelper.getPlan(drdsSqlWithParams);
            }
            List<String> explain = plan.explain(dataContext, drdsSqlWithParams, true);
            for (String s1 : explain) {
                builder.addObjectRowPayload(Arrays.asList(s1));
            }
        }
    } catch (Throwable th) {
        LOGGER.error("", th);
        builder.addObjectRowPayload(Arrays.asList(th.toString()));
    }
    return response.sendResultSet(RowIterable.create(builder.build()));
}
Also used : MySqlExplainStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExplainStatement) MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) ResultSetBuilder(io.mycat.beans.mycat.ResultSetBuilder) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) Plan(io.mycat.calcite.spm.Plan) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) HackRouter(io.mycat.prototypeserver.mysql.HackRouter) DrdsSqlWithParams(io.mycat.DrdsSqlWithParams) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SneakyThrows(lombok.SneakyThrows)

Aggregations

DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)13 Plan (io.mycat.calcite.spm.Plan)7 AsyncMycatDataContextImpl (io.mycat.AsyncMycatDataContextImpl)5 MycatRel (io.mycat.calcite.MycatRel)5 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)4 OptimizationContext (io.mycat.calcite.rewriter.OptimizationContext)4 PlanImpl (io.mycat.calcite.spm.PlanImpl)4 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 MycatView (io.mycat.calcite.logical.MycatView)3 CalciteRowMetaData (io.mycat.calcite.resultset.CalciteRowMetaData)3 VertxExecuter (io.mycat.vertx.VertxExecuter)3 MycatDataContext (io.mycat.MycatDataContext)2 RowBaseIterator (io.mycat.api.collector.RowBaseIterator)2 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)2 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)2 ResultSetBuilder (io.mycat.beans.mycat.ResultSetBuilder)2 io.mycat.calcite (io.mycat.calcite)2 NewMycatConnection (io.mycat.newquery.NewMycatConnection)2 HackRouter (io.mycat.prototypeserver.mysql.HackRouter)2 CalciteCompiler (io.ordinate.engine.builder.CalciteCompiler)2