Search in sources :

Example 1 with PlanImpl

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

the class DrdsSqlCompiler method doHbt.

@SneakyThrows
public Plan doHbt(String hbtText) {
    log.debug("reveice hbt");
    log.debug(hbtText);
    HBTParser hbtParser = new HBTParser(hbtText);
    ParseNode statement = hbtParser.statement();
    SchemaConvertor schemaConvertor = new SchemaConvertor();
    Schema originSchema = schemaConvertor.transforSchema(statement);
    RelOptCluster cluster = newCluster();
    RelBuilder relBuilder = MycatCalciteSupport.relBuilderFactory.create(cluster, catalogReader);
    HBTQueryConvertor hbtQueryConvertor = new HBTQueryConvertor(Collections.emptyList(), relBuilder);
    RelNode relNode = hbtQueryConvertor.complie(originSchema);
    HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
    hepProgramBuilder.addRuleInstance(CoreRules.AGGREGATE_REDUCE_FUNCTIONS);
    hepProgramBuilder.addMatchLimit(512);
    HepProgram hepProgram = hepProgramBuilder.build();
    HepPlanner hepPlanner = new HepPlanner(hepProgram);
    hepPlanner.setRoot(relNode);
    RelNode bestExp = hepPlanner.findBestExp();
    bestExp = bestExp.accept(new RelShuttleImpl() {

        @Override
        public RelNode visit(TableScan scan) {
            AbstractMycatTable table = scan.getTable().unwrap(AbstractMycatTable.class);
            if (table != null) {
                if (table instanceof MycatPhysicalTable) {
                    Partition partition = ((MycatPhysicalTable) table).getPartition();
                    MycatPhysicalTable mycatPhysicalTable = (MycatPhysicalTable) table;
                    SqlNode sqlNode = MycatCalciteSupport.INSTANCE.convertToSqlTemplate(scan, MycatCalciteSupport.INSTANCE.getSqlDialectByTargetName(partition.getTargetName()), false);
                    SqlDialect dialect = MycatCalciteSupport.INSTANCE.getSqlDialectByTargetName(partition.getTargetName());
                    return new MycatTransientSQLTableScan(cluster, mycatPhysicalTable.getRowType(), partition.getTargetName(), sqlNode.toSqlString(dialect).getSql());
                }
            }
            return super.visit(scan);
        }
    });
    bestExp = bestExp.accept(new RelShuttleImpl() {

        @Override
        public RelNode visit(TableScan scan) {
            return SQLRBORewriter.view(scan).orElse(scan);
        }
    });
    MycatRel mycatRel = optimizeWithCBO(bestExp, Collections.emptyList());
    CodeExecuterContext codeExecuterContext = getCodeExecuterContext(ImmutableMap.of(), mycatRel, false);
    return new PlanImpl(mycatRel, codeExecuterContext, mycatRel.getRowType().getFieldNames());
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) HepProgram(org.apache.calcite.plan.hep.HepProgram) DrdsExecutorCompiler.getCodeExecuterContext(io.mycat.DrdsExecutorCompiler.getCodeExecuterContext) HBTQueryConvertor(io.mycat.hbt.HBTQueryConvertor) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) Schema(io.mycat.hbt.ast.base.Schema) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) HBTParser(io.mycat.hbt.parser.HBTParser) SchemaConvertor(io.mycat.hbt.SchemaConvertor) RelNode(org.apache.calcite.rel.RelNode) ParseNode(io.mycat.hbt.parser.ParseNode) SqlDialect(org.apache.calcite.sql.SqlDialect) SqlNode(org.apache.calcite.sql.SqlNode) PlanImpl(io.mycat.calcite.spm.PlanImpl) SneakyThrows(lombok.SneakyThrows)

Example 2 with PlanImpl

use of io.mycat.calcite.spm.PlanImpl 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 3 with PlanImpl

use of io.mycat.calcite.spm.PlanImpl 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 4 with PlanImpl

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

the class PrototypeService method getMycatRowMetaDataForPrepareStatement.

public Optional<MycatRowMetaData> getMycatRowMetaDataForPrepareStatement(String schema, String sql) {
    SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
    if (schema != null) {
        sqlStatement.accept(new MySqlASTVisitorAdapter() {

            @Override
            public boolean visit(SQLExprTableSource x) {
                if (x.getSchema() == null) {
                    x.setSchema(schema);
                }
                return super.visit(x);
            }
        });
    }
    sql = sqlStatement.toString();
    Optional<MycatRowMetaData> rowMetaData = Optional.empty();
    if (!rowMetaData.isPresent()) {
        HackRouter hackRouter = new HackRouter(sqlStatement, schema);
        if (hackRouter.analyse()) {
            Pair<String, String> plan = hackRouter.getPlan();
            rowMetaData = getMycatRowMetaDataPrepareSQLByTargetName(plan.getKey(), plan.getValue());
        }
    }
    if (!rowMetaData.isPresent()) {
        try {
            PlanImpl plan = DrdsRunnerHelper.getPlan(DrdsRunnerHelper.preParse(sqlStatement, schema));
            rowMetaData = Optional.of(plan.getMetaData());
        } catch (Exception e) {
            LOGGER.warn("can not get meta from calcite ", e);
            rowMetaData = Optional.empty();
        }
    }
    if (!rowMetaData.isPresent()) {
        rowMetaData = getMycatRowMetaDataPrepareSQLByTargetName(MetadataManager.getPrototype(), sql);
    }
    return rowMetaData;
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MycatRowMetaData(io.mycat.beans.mycat.MycatRowMetaData) CopyMycatRowMetaData(io.mycat.beans.mycat.CopyMycatRowMetaData) PlanImpl(io.mycat.calcite.spm.PlanImpl)

Example 5 with PlanImpl

use of io.mycat.calcite.spm.PlanImpl 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)

Aggregations

PlanImpl (io.mycat.calcite.spm.PlanImpl)9 MycatRel (io.mycat.calcite.MycatRel)7 OptimizationContext (io.mycat.calcite.rewriter.OptimizationContext)7 Plan (io.mycat.calcite.spm.Plan)7 DrdsSqlCompiler (io.mycat.DrdsSqlCompiler)4 DrdsSqlWithParams (io.mycat.DrdsSqlWithParams)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 DrdsExecutorCompiler.getCodeExecuterContext (io.mycat.DrdsExecutorCompiler.getCodeExecuterContext)1 CopyMycatRowMetaData (io.mycat.beans.mycat.CopyMycatRowMetaData)1 MycatRowMetaData (io.mycat.beans.mycat.MycatRowMetaData)1 HBTQueryConvertor (io.mycat.hbt.HBTQueryConvertor)1 SchemaConvertor (io.mycat.hbt.SchemaConvertor)1 Schema (io.mycat.hbt.ast.base.Schema)1 HBTParser (io.mycat.hbt.parser.HBTParser)1 ParseNode (io.mycat.hbt.parser.ParseNode)1 SneakyThrows (lombok.SneakyThrows)1 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)1 HepPlanner (org.apache.calcite.plan.hep.HepPlanner)1 HepProgram (org.apache.calcite.plan.hep.HepProgram)1