Java Code Examples for org.apache.calcite.plan.hep.HepProgram#builder()

The following examples show how to use org.apache.calcite.plan.hep.HepProgram#builder() . 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 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 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
@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 4
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 5
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 6
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Sql withRule(RelOptRule... rules) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return with(builder.build());
}
 
Example 7
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 8
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 9
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testGroup() throws Exception {
  // Verify simultaneous application of a group of rules.
  // Intentionally add them in the wrong order to make sure
  // that order doesn't matter within the group.
  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addGroupBegin();
  programBuilder.addRuleInstance(CalcMergeRule.INSTANCE);
  programBuilder.addRuleInstance(ProjectToCalcRule.INSTANCE);
  programBuilder.addRuleInstance(FilterToCalcRule.INSTANCE);
  programBuilder.addGroupEnd();

  final String sql = "select upper(name) from dept where deptno=20";
  sql(sql).with(programBuilder.build()).check();
}
 
Example 10
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMatchUntilFixpoint() throws Exception {
  // Verify that both unions get rewritten.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addMatchLimit(HepProgram.MATCH_UNTIL_FIXPOINT);
  programBuilder.addRuleInstance(UnionToDistinctRule.INSTANCE);

  sql(UNION_TREE).with(programBuilder.build()).check();
}
 
Example 11
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a program that executes a list of rules in a HEP planner. */
public static Program hep(Iterable<? extends RelOptRule> rules,
    boolean noDag, RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return of(builder.build(), noDag, metadataProvider);
}
 
Example 12
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 13
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 14
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 15
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static Program subQuery(RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addRuleCollection(
      ImmutableList.of(SubQueryRemoveRule.FILTER,
          SubQueryRemoveRule.PROJECT,
          SubQueryRemoveRule.JOIN));
  return of(builder.build(), true, metadataProvider);
}
 
Example 16
Source File: Programs.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a program that executes a list of rules in a HEP planner. */
public static Program hep(Iterable<? extends RelOptRule> rules,
    boolean noDag, RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  for (RelOptRule rule : rules) {
    builder.addRuleInstance(rule);
  }
  return of(builder.build(), noDag, metadataProvider);
}
 
Example 17
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 18
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static Program subQuery(RelMetadataProvider metadataProvider) {
  final HepProgramBuilder builder = HepProgram.builder();
  builder.addRuleCollection(ImmutableList.of((RelOptRule) SubQueryRemoveRule.FILTER,
      SubQueryRemoveRule.PROJECT,
      SubQueryRemoveRule.JOIN));
  return of(builder.build(), true, metadataProvider);
}