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