Java Code Examples for org.apache.calcite.plan.hep.HepProgramBuilder#build()

The following examples show how to use org.apache.calcite.plan.hep.HepProgramBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 6 votes vote down vote up
static HepPlanner createHepPlanner() {
    HepProgramBuilder builder = new HepProgramBuilder();
    // builder.addRuleInstance(FilterJoinRule.FilterIntoJoinRule.FILTER_ON_JOIN);
    // builder.addRuleInstance(FilterJoinRule.JOIN);
    builder.addRuleCollection(Programs.CALC_RULES);
    // builder.addRuleCollection(Programs.RULE_SET);
    // builder.addRuleInstance(ReduceExpressionsRule.PROJECT_INSTANCE); // 加上这个可以把100+100变成200,常量折叠
    // builder.addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE);
    // builder.addRuleInstance(FilterProjectTransposeRule.INSTANCE);

    // HepMatchOrder order = HepMatchOrder.TOP_DOWN;
    // builder.addMatchOrder(order);
    // builder.addConverters(true);

    HepPlanner hepPlanner = new HepPlanner(builder.build());

    hepPlanner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    hepPlanner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    return hepPlanner;
}
 
Example 2
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testRuleClass() throws Exception {
  // Verify that an entire class of rules can be applied.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addRuleClass(CoerceInputsRule.class);

  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());

  planner.addRule(
      new CoerceInputsRule(LogicalUnion.class, false,
          RelFactories.LOGICAL_BUILDER));
  planner.addRule(
      new CoerceInputsRule(LogicalIntersect.class, false,
          RelFactories.LOGICAL_BUILDER));

  final String sql = "(select name from dept union select ename from emp)\n"
      + "intersect (select fname from customer.contact)";
  sql(sql).with(planner).check();
}
 
Example 3
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Tests that if two relational expressions are equivalent, the planner
 * notices, and only applies the rule once. */
@Test void testCommonSubExpression() {
  // In the following,
  //   (select 1 from dept where abs(-1)=20)
  // occurs twice, but it's a common sub-expression, so the rule should only
  // apply once.
  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addRuleInstance(FilterToCalcRule.INSTANCE);

  final HepTestListener listener = new HepTestListener(0);
  HepPlanner planner = new HepPlanner(programBuilder.build());
  planner.addListener(listener);

  final String sql = "(select 1 from dept where abs(-1)=20)\n"
      + "union all\n"
      + "(select 1 from dept where abs(-1)=20)";
  planner.setRoot(tester.convertSqlToRel(sql).rel);
  RelNode bestRel = planner.findBestExp();

  assertThat(bestRel.getInput(0).equals(bestRel.getInput(1)), is(true));
  assertThat(listener.getApplyTimes() == 1, is(true));
}
 
Example 4
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testGC() throws Exception {
  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addMatchOrder(HepMatchOrder.TOP_DOWN);
  programBuilder.addRuleInstance(CalcMergeRule.INSTANCE);
  programBuilder.addRuleInstance(ProjectToCalcRule.INSTANCE);
  programBuilder.addRuleInstance(FilterToCalcRule.INSTANCE);

  HepPlanner planner = new HepPlanner(programBuilder.build());
  planner.setRoot(
      tester.convertSqlToRel("select upper(name) from dept where deptno=20").rel);
  planner.findBestExp();
  // Reuse of HepPlanner (should trigger GC).
  planner.setRoot(
      tester.convertSqlToRel("select upper(name) from dept where deptno=20").rel);
  planner.findBestExp();
}
 
Example 5
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Pair<RelNode, RelNode> pushFilterToOriginalViewPlan(RelBuilder builder, RelNode topViewProject,
        RelNode viewNode, RexNode cond) {
    // We add (and push) the filter to the view plan before triggering the rewriting.
    // This is useful in case some of the columns can be folded to same value after
    // filter is added.
    HepProgramBuilder pushFiltersProgram = new HepProgramBuilder();
    if (topViewProject != null) {
        pushFiltersProgram.addRuleInstance(filterProjectTransposeRule);
    }
    pushFiltersProgram.addRuleInstance(this.filterAggregateTransposeRule)
            .addRuleInstance(this.aggregateProjectPullUpConstantsRule).addRuleInstance(this.projectMergeRule);
    final HepPlanner tmpPlanner = new HepPlanner(pushFiltersProgram.build());
    // Now that the planner is created, push the node
    RelNode topNode = builder.push(topViewProject != null ? topViewProject : viewNode).filter(cond).build();
    tmpPlanner.setRoot(topNode);
    topNode = tmpPlanner.findBestExp();
    RelNode resultTopViewProject = null;
    RelNode resultViewNode = null;
    while (topNode != null) {
        if (topNode instanceof Project) {
            if (resultTopViewProject != null) {
                // Both projects could not be merged, we will bail out
                return Pair.of(topViewProject, viewNode);
            }
            resultTopViewProject = topNode;
            topNode = topNode.getInput(0);
        } else if (topNode instanceof Aggregate) {
            resultViewNode = topNode;
            topNode = null;
        } else {
            // We move to the child
            topNode = topNode.getInput(0);
        }
    }
    return Pair.of(resultTopViewProject, resultViewNode);
}
 
Example 6
Source File: MycatCalcitePlanner.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public RelNode pullUpUnion(RelNode relNode1) {
    HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
    hepProgramBuilder.addMatchLimit(PULL_RULES.size());
    hepProgramBuilder.addRuleCollection(PULL_RULES);
    final HepPlanner planner2 = new HepPlanner(hepProgramBuilder.build());
    planner2.setRoot(relNode1);
    return planner2.findBestExp();
}
 
Example 7
Source File: MaterializationExpander.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static RelNode applyRule(RelNode node, RelOptRule rule) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addMatchOrder(HepMatchOrder.ARBITRARY);
  builder.addRuleCollection(ImmutableList.of(rule));
  final HepProgram program = builder.build();

  final HepPlanner planner = new HepPlanner(program);
  planner.setRoot(node);
  return planner.findBestExp();
}
 
Example 8
Source File: MaterializedViewAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Pair<RelNode, RelNode> pushFilterToOriginalViewPlan(RelBuilder builder,
    RelNode topViewProject, RelNode viewNode, RexNode cond) {
  // We add (and push) the filter to the view plan before triggering the rewriting.
  // This is useful in case some of the columns can be folded to same value after
  // filter is added.
  HepProgramBuilder pushFiltersProgram = new HepProgramBuilder();
  if (topViewProject != null) {
    pushFiltersProgram.addRuleInstance(filterProjectTransposeRule);
  }
  pushFiltersProgram
      .addRuleInstance(this.filterAggregateTransposeRule)
      .addRuleInstance(this.aggregateProjectPullUpConstantsRule)
      .addRuleInstance(this.projectMergeRule);
  final HepPlanner tmpPlanner = new HepPlanner(pushFiltersProgram.build());
  // Now that the planner is created, push the node
  RelNode topNode = builder
      .push(topViewProject != null ? topViewProject : viewNode)
      .filter(cond).build();
  tmpPlanner.setRoot(topNode);
  topNode = tmpPlanner.findBestExp();
  RelNode resultTopViewProject = null;
  RelNode resultViewNode = null;
  while (topNode != null) {
    if (topNode instanceof Project) {
      if (resultTopViewProject != null) {
        // Both projects could not be merged, we will bail out
        return Pair.of(topViewProject, viewNode);
      }
      resultTopViewProject = topNode;
      topNode = topNode.getInput(0);
    } else if (topNode instanceof Aggregate) {
      resultViewNode = topNode;
      topNode = null;
    } else {
      // We move to the child
      topNode = topNode.getInput(0);
    }
  }
  return Pair.of(resultTopViewProject, resultViewNode);
}
 
Example 9
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testRuleDescription() throws Exception {
  // Verify that a rule can be applied via its description.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addRuleByDescription("FilterToCalcRule");

  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());

  planner.addRule(FilterToCalcRule.INSTANCE);

  final String sql = "select name from sales.dept where deptno=12";
  sql(sql).with(planner).check();
}
 
Example 10
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures {@link org.apache.calcite.rel.AbstractRelNode} digest does not include
 * full digest tree.
 */
@Test void relDigestLength() {
  HepProgramBuilder programBuilder = HepProgram.builder();
  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());
  StringBuilder sb = new StringBuilder();
  final int n = 10;
  sb.append("select * from (");
  sb.append("select name from sales.dept");
  for (int i = 0; i < n; i++) {
    sb.append(" union all select name from sales.dept");
  }
  sb.append(")");
  RelRoot root = tester.convertSqlToRel(sb.toString());
  planner.setRoot(root.rel);
  RelNode best = planner.findBestExp();

  // Good digest should look like rel#66:LogicalProject(input=rel#64:LogicalUnion)
  // Bad digest includes full tree like rel#66:LogicalProject(input=rel#64:LogicalUnion(...))
  // So the assertion is to ensure digest includes LogicalUnion exactly once

  assertIncludesExactlyOnce("best.getDescription()",
      best.toString(), "LogicalUnion");
  assertIncludesExactlyOnce("best.getDigest()",
      best.getDigest(), "LogicalUnion");
}
 
Example 11
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testRelNodeCacheWithDigest() {
  HepProgramBuilder programBuilder = HepProgram.builder();
  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());
  String query = "(select n_nationkey from SALES.CUSTOMER) union all\n"
      + "(select n_name from CUSTOMER_MODIFIABLEVIEW)";
  sql(query).withTester(t -> createDynamicTester())
      .withDecorrelation(true)
      .with(programBuilder.build())
      .with(planner)
      .checkUnchanged();
}
 
Example 12
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private long checkRuleApplyCount(HepMatchOrder matchOrder) {
  final HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addMatchOrder(matchOrder);
  programBuilder.addRuleInstance(ReduceExpressionsRule.FILTER_INSTANCE);
  programBuilder.addRuleInstance(ReduceExpressionsRule.PROJECT_INSTANCE);

  final HepTestListener listener = new HepTestListener(0);
  HepPlanner planner = new HepPlanner(programBuilder.build());
  planner.addListener(listener);
  planner.setRoot(tester.convertSqlToRel(COMPLEX_UNION_TREE).rel);
  planner.findBestExp();
  return listener.getApplyTimes();
}
 
Example 13
Source File: CalciteRunners.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@SneakyThrows
    public static RowBaseIterator run(String sql, MycatCalciteDataContext calciteDataContext, RelNode relNode) {
        SqlRecorder recorder = SqlRecorderRuntime.INSTANCE.getCurrentRecorder();
        Map<String, List<SingeTargetSQLTable>> map = new HashMap<>();
        relNode.accept(new RelShuttleImpl() {
            @Override
            public RelNode visit(TableScan scan) {
                SingeTargetSQLTable unwrap = scan.getTable().unwrap(SingeTargetSQLTable.class);
                if (unwrap != null && !unwrap.existsEnumerable()) {
                    List<SingeTargetSQLTable> tables = map.computeIfAbsent(unwrap.getTargetName(), s -> new ArrayList<>(2));
                    tables.add(unwrap);
                }
                return super.visit(scan);
            }
        });

        HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
        hepProgramBuilder.addMatchLimit(64);

        hepProgramBuilder.addRuleInstance(StreamUnionRule.INSTANCE);
        final HepPlanner planner2 = new HepPlanner(hepProgramBuilder.build());
        planner2.setRoot(relNode);
        relNode = planner2.findBestExp();

        //check
        relNode.accept(new RelShuttleImpl() {
            @Override
            public RelNode visit(LogicalUnion union) {
                if (union.getInputs().size() > 2) {
                    throw new AssertionError("union input more 2");
                }
                return super.visit(union);
            }
        });
        long startGetConnectionTime = TimeProvider.INSTANCE.now();
        fork(sql, calciteDataContext, map);
        long cbo = TimeProvider.INSTANCE.now();
        recorder.addRecord(SqlRecorderType.GET_CONNECTION, sql, cbo - startGetConnectionTime);
        ArrayBindable bindable1 = Interpreters.bindable(relNode);
        long execution_start = TimeProvider.INSTANCE.now();
        recorder.addRecord(SqlRecorderType.CBO, sql, execution_start - cbo);
//        EnumerableInterpretable.toBindable()
        Enumerable<Object[]> bind = bindable1.bind(calciteDataContext);
        Enumerator<Object[]> enumerator = bind.enumerator();

        return new EnumeratorRowIterator(CalciteConvertors.getMycatRowMetaData(relNode.getRowType()), enumerator,
                () -> {
                    recorder.addRecord(SqlRecorderType.EXECUTION_TIME, sql, TimeProvider.INSTANCE.now()-execution_start);
                    recorder.addRecord(SqlRecorderType.AT_END, sql, TimeProvider.INSTANCE.now());
                });
    }