org.apache.calcite.plan.hep.HepMatchOrder Java Examples

The following examples show how to use org.apache.calcite.plan.hep.HepMatchOrder. 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: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testSubprogram() throws Exception {
  // Verify that subprogram gets re-executed until fixpoint.
  // In this case, the first time through we limit it to generate
  // only one calc; the second time through it will generate
  // a second calc, and then merge them.
  HepProgramBuilder subprogramBuilder = HepProgram.builder();
  subprogramBuilder.addMatchOrder(HepMatchOrder.TOP_DOWN);
  subprogramBuilder.addMatchLimit(1);
  subprogramBuilder.addRuleInstance(ProjectToCalcRule.INSTANCE);
  subprogramBuilder.addRuleInstance(FilterToCalcRule.INSTANCE);
  subprogramBuilder.addRuleInstance(CalcMergeRule.INSTANCE);

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addSubprogram(subprogramBuilder.build());

  final String sql = "select upper(ename) from\n"
      + "(select lower(ename) as ename from emp where empno = 100)";
  sql(sql).with(programBuilder.build()).check();
}
 
Example #2
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 #3
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a program that invokes heuristic join-order optimization
 * (via {@link org.apache.calcite.rel.rules.JoinToMultiJoinRule},
 * {@link org.apache.calcite.rel.rules.MultiJoin} and
 * {@link org.apache.calcite.rel.rules.LoptOptimizeJoinRule})
 * if there are 6 or more joins (7 or more relations). */
public static Program heuristicJoinOrder(
    final Iterable<? extends RelOptRule> rules,
    final boolean bushy, final int minJoinCount) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final int joinCount = RelOptUtil.countJoins(rel);
    final Program program;
    if (joinCount < minJoinCount) {
      program = ofRules(rules);
    } else {
      // Create a program that gathers together joins as a MultiJoin.
      final HepProgram hep = new HepProgramBuilder()
          .addRuleInstance(FilterJoinRule.FILTER_ON_JOIN)
          .addMatchOrder(HepMatchOrder.BOTTOM_UP)
          .addRuleInstance(JoinToMultiJoinRule.INSTANCE)
          .build();
      final Program program1 =
          of(hep, false, DefaultRelMetadataProvider.INSTANCE);

      // Create a program that contains a rule to expand a MultiJoin
      // into heuristically ordered joins.
      // We use the rule set passed in, but remove JoinCommuteRule and
      // JoinPushThroughJoinRule, because they cause exhaustive search.
      final List<RelOptRule> list = Lists.newArrayList(rules);
      list.removeAll(
          ImmutableList.of(JoinCommuteRule.INSTANCE,
              JoinAssociateRule.INSTANCE,
              JoinPushThroughJoinRule.LEFT,
              JoinPushThroughJoinRule.RIGHT));
      list.add(bushy
          ? MultiJoinOptimizeBushyRule.INSTANCE
          : LoptOptimizeJoinRule.INSTANCE);
      final Program program2 = ofRules(list);

      program = sequence(program1, program2);
    }
    return program.run(
        planner, rel, requiredOutputTraits, materializations, lattices);
  };
}
 
Example #4
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 #5
Source File: PushProjectIntoTableSourceScanRuleTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void setup() {
	util().buildBatchProgram(FlinkBatchProgram.DEFAULT_REWRITE());
	CalciteConfig calciteConfig = TableConfigUtils.getCalciteConfig(util().tableEnv().getConfig());
	calciteConfig.getBatchProgram().get().addLast(
			"rules",
			FlinkHepRuleSetProgramBuilder.<BatchOptimizeContext>newBuilder()
					.setHepRulesExecutionType(HEP_RULES_EXECUTION_TYPE.RULE_SEQUENCE())
					.setHepMatchOrder(HepMatchOrder.BOTTOM_UP)
					.add(RuleSets.ofList(PushProjectIntoTableSourceScanRule.INSTANCE))
					.build()
	);

	String ddl1 =
			"CREATE TABLE MyTable (\n" +
					"  a int,\n" +
					"  b bigint,\n" +
					"  c string\n" +
					") WITH (\n" +
					" 'connector' = 'values',\n" +
					" 'bounded' = 'true'\n" +
					")";
	util().tableEnv().executeSql(ddl1);

	String ddl2 =
			"CREATE TABLE VirtualTable (\n" +
					"  a int,\n" +
					"  b bigint,\n" +
					"  c string,\n" +
					"  d as a + 1\n" +
					") WITH (\n" +
					" 'connector' = 'values',\n" +
					" 'bounded' = 'true'\n" +
					")";
	util().tableEnv().executeSql(ddl2);
}
 
Example #6
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a program that invokes heuristic join-order optimization
 * (via {@link org.apache.calcite.rel.rules.JoinToMultiJoinRule},
 * {@link org.apache.calcite.rel.rules.MultiJoin} and
 * {@link org.apache.calcite.rel.rules.LoptOptimizeJoinRule})
 * if there are 6 or more joins (7 or more relations). */
public static Program heuristicJoinOrder(
    final Iterable<? extends RelOptRule> rules,
    final boolean bushy, final int minJoinCount) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final int joinCount = RelOptUtil.countJoins(rel);
    final Program program;
    if (joinCount < minJoinCount) {
      program = ofRules(rules);
    } else {
      // Create a program that gathers together joins as a MultiJoin.
      final HepProgram hep = new HepProgramBuilder()
          .addRuleInstance(FilterJoinRule.FILTER_ON_JOIN)
          .addMatchOrder(HepMatchOrder.BOTTOM_UP)
          .addRuleInstance(JoinToMultiJoinRule.INSTANCE)
          .build();
      final Program program1 =
          of(hep, false, DefaultRelMetadataProvider.INSTANCE);

      // Create a program that contains a rule to expand a MultiJoin
      // into heuristically ordered joins.
      // We use the rule set passed in, but remove JoinCommuteRule and
      // JoinPushThroughJoinRule, because they cause exhaustive search.
      final List<RelOptRule> list = Lists.newArrayList(rules);
      list.removeAll(
          ImmutableList.of(JoinCommuteRule.INSTANCE,
              JoinAssociateRule.INSTANCE,
              JoinPushThroughJoinRule.LEFT,
              JoinPushThroughJoinRule.RIGHT));
      list.add(bushy
          ? MultiJoinOptimizeBushyRule.INSTANCE
          : LoptOptimizeJoinRule.INSTANCE);
      final Program program2 = ofRules(list);

      program = sequence(program1, program2);
    }
    return program.run(
        planner, rel, requiredOutputTraits, materializations, lattices);
  };
}
 
Example #7
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMatchLimitOneTopDown() throws Exception {
  // Verify that only the top union gets rewritten.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addMatchOrder(HepMatchOrder.TOP_DOWN);
  programBuilder.addMatchLimit(1);
  programBuilder.addRuleInstance(UnionToDistinctRule.INSTANCE);

  sql(UNION_TREE).with(programBuilder.build()).check();
}
 
Example #8
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMatchLimitOneBottomUp() throws Exception {
  // Verify that only the bottom union gets rewritten.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addMatchLimit(1);
  programBuilder.addMatchOrder(HepMatchOrder.BOTTOM_UP);
  programBuilder.addRuleInstance(UnionToDistinctRule.INSTANCE);

  sql(UNION_TREE).with(programBuilder.build()).check();
}
 
Example #9
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testRuleApplyCount() {
  final long applyTimes1 = checkRuleApplyCount(HepMatchOrder.ARBITRARY);
  assertThat(applyTimes1, is(316L));

  final long applyTimes2 = checkRuleApplyCount(HepMatchOrder.DEPTH_FIRST);
  assertThat(applyTimes2, is(87L));
}
 
Example #10
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();
}