use of io.mycat.calcite.rewriter.MatierialRewriter in project Mycat2 by MyCATApache.
the class DrdsSqlCompiler method optimizeWithCBO.
// private MycatRel planUpdate(LogicalTableModify tableModify,
// DrdsSql drdsSql, OptimizationContext optimizationContext) {
// MycatLogicTable mycatTable = (MycatLogicTable) tableModify.getTable().unwrap(AbstractMycatTable.class);
// RelNode input = tableModify.getInput();
// if (input instanceof LogicalProject) {
// input = ((LogicalProject) input).getInput();
// }
// RexNode condition;
// if (input instanceof Filter && ((Filter) input).getInput() instanceof LogicalTableScan) {
// RelDataType rowType = input.getRowType();
// condition = ((Filter) input).getCondition();
// }else {
// condition = MycatCalciteSupport.RexBuilder.makeLiteral(true);
// }
// MycatUpdateRel mycatUpdateRel = new MycatUpdateRel(
// drdsSql.getParameterizedStatement(),
// mycatTable.getTable().getSchemaName(),
// mycatTable.getTable().getTableName(),
// condition);
// optimizationContext.saveAlways();
// return mycatUpdateRel;
// }
public MycatRel optimizeWithCBO(RelNode logPlan, Collection<RelOptRule> relOptRules) {
if (logPlan instanceof MycatRel) {
return (MycatRel) logPlan;
} else {
RelOptCluster cluster = logPlan.getCluster();
RelOptPlanner planner = cluster.getPlanner();
planner.clear();
MycatConvention.INSTANCE.register(planner);
ImmutableList.Builder<RelOptRule> listBuilder = ImmutableList.builder();
listBuilder.addAll(MycatExtraSortRule.RULES);
listBuilder.addAll(LocalRules.CBO_RULES);
// 算子交换
// Filter/Join, TopN/Join, Agg/Join, Filter/Agg, Sort/Project, Join/TableLookup
listBuilder.add(CoreRules.JOIN_PUSH_EXPRESSIONS);
listBuilder.add(CoreRules.FILTER_INTO_JOIN);
// TopN/Join
listBuilder.add(CoreRules.SORT_JOIN_TRANSPOSE.config.withOperandFor(MycatTopN.class, Join.class).toRule());
listBuilder.add(CoreRules.FILTER_SET_OP_TRANSPOSE.config.toRule());
listBuilder.add(CoreRules.AGGREGATE_JOIN_TRANSPOSE.config.withOperandFor(Aggregate.class, Join.class, false).toRule());
// Sort/Project
listBuilder.add(CoreRules.SORT_PROJECT_TRANSPOSE.config.withOperandFor(Sort.class, Project.class).toRule());
// index
listBuilder.add(MycatViewIndexViewRule.DEFAULT_CONFIG.toRule());
if (DrdsSqlCompiler.RBO_BKA_JOIN) {
// TABLELOOKUP
listBuilder.add(MycatTableLookupSemiJoinRule.INSTANCE);
listBuilder.add(MycatTableLookupCombineRule.INSTANCE);
listBuilder.add(MycatJoinTableLookupTransposeRule.LEFT_INSTANCE);
listBuilder.add(MycatJoinTableLookupTransposeRule.RIGHT_INSTANCE);
listBuilder.add(MycatValuesJoinRule.INSTANCE);
}
listBuilder.build().forEach(c -> planner.addRule(c));
MycatConvention.INSTANCE.register(planner);
if (relOptRules != null) {
for (RelOptRule relOptRule : relOptRules) {
planner.addRule(relOptRule);
}
}
if (DEBUG) {
MycatRelOptListener mycatRelOptListener = new MycatRelOptListener();
planner.addListener(mycatRelOptListener);
log.debug(mycatRelOptListener.dump());
}
logPlan = planner.changeTraits(logPlan, cluster.traitSetOf(MycatConvention.INSTANCE));
planner.setRoot(logPlan);
RelNode bestExp = planner.findBestExp();
RelNode accept = bestExp.accept(new MatierialRewriter());
return (MycatRel) accept;
}
}
Aggregations