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());
}
Aggregations