Search in sources :

Example 1 with HBTQueryConvertor

use of io.mycat.hbt.HBTQueryConvertor 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)

Aggregations

DrdsExecutorCompiler.getCodeExecuterContext (io.mycat.DrdsExecutorCompiler.getCodeExecuterContext)1 PlanImpl (io.mycat.calcite.spm.PlanImpl)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 HepProgramBuilder (org.apache.calcite.plan.hep.HepProgramBuilder)1 RelNode (org.apache.calcite.rel.RelNode)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1 SqlDialect (org.apache.calcite.sql.SqlDialect)1 SqlNode (org.apache.calcite.sql.SqlNode)1 RelBuilder (org.apache.calcite.tools.RelBuilder)1