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

The following examples show how to use org.apache.calcite.plan.hep.HepProgram. 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: SqlHintsConverterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testHintsPropagateWithDifferentKindOfRels() {
  final String sql = "select /*+ AGG_STRATEGY(TWO_PHASE) */\n"
      + "ename, avg(sal)\n"
      + "from emp group by ename";
  final RelNode rel = tester.convertSqlToRel(sql).rel;
  final RelHint hint = RelHint.builder("AGG_STRATEGY")
      .inheritPath(0)
      .hintOption("TWO_PHASE")
      .build();
  // AggregateReduceFunctionsRule does the transformation:
  // AGG -> PROJECT + AGG
  HepProgram program = new HepProgramBuilder()
      .addRuleInstance(AggregateReduceFunctionsRule.INSTANCE)
      .build();
  HepPlanner planner = new HepPlanner(program);
  planner.setRoot(rel);
  RelNode newRel = planner.findBestExp();
  new ValidateHintVisitor(hint, Aggregate.class).go(newRel);
}
 
Example #2
Source File: UnifyingSubstitutionProvider.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Normalizes the given query and target {@link RelNode}s and finds substitutions on canonical
 * representations.
 *
 * @param query  incoming query
 * @param materialization  materialization to apply
 * @return  list of substitutions
 */
protected List<Substitution> substitute(
    final RelNode query,
    final DremioMaterialization materialization) {

  // Push filters to the bottom, and combine projects on top.
  final HepProgram program = getProgramBuilder().build();
  final HepPlanner hepPlanner = new HepPlanner(program);

  hepPlanner.setRoot(materialization.getQueryRel());
  final RelNode canonicalTarget = hepPlanner.findBestExp();

  hepPlanner.setRoot(query);
  final RelNode canonicalQuery = hepPlanner.findBestExp();

  return substitute(canonicalQuery, canonicalTarget, materialization.getTableRel());
}
 
Example #3
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 #4
Source File: ResultProcessor.java    From quark with Apache License 2.0 6 votes vote down vote up
public static String getParsedSql(RelNode relNode, SqlDialect dialect) throws SQLException {
  if (dialect.getDatabaseProduct() == SqlDialect.DatabaseProduct.HIVE) {
    final HepProgram program = new HepProgramBuilder()
        .addRuleInstance(JoinCalcTransposeRule.LEFT_CALC)
        .addRuleInstance(JoinCalcTransposeRule.RIGHT_CALC)
        .addRuleInstance(CalcMergeRule.INSTANCE)
        .build();
    final RelOptPlanner planner = relNode.getCluster().getPlanner();
    final HepPlanner hepPlanner =
        new HepPlanner(program, planner.getContext());
    hepPlanner.setRoot(relNode);
    relNode = hepPlanner.findBestExp();
  }
  RelToSqlConverter relToSqlConverter = new RelToSqlConverter(dialect);
  RelToSqlConverter.Result res = relToSqlConverter.visitChild(0, relNode);
  SqlNode sqlNode = res.asQuery();
  String result = sqlNode.toSqlString(dialect, false).getSql();
  return result.replace("\n", " ");
}
 
Example #5
Source File: RelFieldTrimmerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testCalcFieldTrimmer0() {
  final RelBuilder builder = RelBuilder.create(config().build());
  final RelNode root =
      builder.scan("EMP")
          .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
          .exchange(RelDistributions.SINGLETON)
          .project(builder.field("EMPNO"), builder.field("ENAME"))
          .build();

  final HepProgram hepProgram = new HepProgramBuilder().
      addRuleInstance(ProjectToCalcRule.INSTANCE).build();

  final HepPlanner hepPlanner = new HepPlanner(hepProgram);
  hepPlanner.setRoot(root);
  final RelNode relNode = hepPlanner.findBestExp();
  final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
  final RelNode trimmed = fieldTrimmer.trim(relNode);

  final String expected = ""
      + "LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
      + "  LogicalExchange(distribution=[single])\n"
      + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
      + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
      + "        LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(trimmed, hasTree(expected));
}
 
Example #6
Source File: MaterializedViewOnlyAggregateRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public MaterializedViewOnlyAggregateRule(RelOptRuleOperand operand,
    RelBuilderFactory relBuilderFactory, String description,
    boolean generateUnionRewriting, HepProgram unionRewritingPullProgram,
    RelOptRule filterProjectTransposeRule,
    RelOptRule filterAggregateTransposeRule,
    RelOptRule aggregateProjectPullUpConstantsRule,
    RelOptRule projectMergeRule) {
  super(
      operand(Aggregate.class, any()),
      relBuilderFactory,
      "MaterializedViewAggregateRule(Aggregate)",
      generateUnionRewriting, unionRewritingPullProgram,
      filterProjectTransposeRule,
      filterAggregateTransposeRule,
      aggregateProjectPullUpConstantsRule,
      projectMergeRule);
}
 
Example #7
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 #8
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 #9
Source File: Programs.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Creates a program that executes a {@link HepProgram}. */
public static Program of(final HepProgram hepProgram, final boolean noDag,
    final RelMetadataProvider metadataProvider) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final HepPlanner hepPlanner = new HepPlanner(hepProgram,
        null, noDag, null, RelOptCostImpl.FACTORY);

    List<RelMetadataProvider> list = new ArrayList<>();
    if (metadataProvider != null) {
      list.add(metadataProvider);
    }
    hepPlanner.registerMetadataProviders(list);
    RelMetadataProvider plannerChain =
        ChainedRelMetadataProvider.of(list);
    rel.getCluster().setMetadataProvider(plannerChain);

    hepPlanner.setRoot(rel);
    return hepPlanner.findBestExp();
  };
}
 
Example #10
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 #11
Source File: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates a program that executes a {@link HepProgram}. */
public static Program of(final HepProgram hepProgram, final boolean noDag,
    final RelMetadataProvider metadataProvider) {
  return (planner, rel, requiredOutputTraits, materializations, lattices) -> {
    final HepPlanner hepPlanner = new HepPlanner(hepProgram,
        null, noDag, null, RelOptCostImpl.FACTORY);

    List<RelMetadataProvider> list = new ArrayList<>();
    if (metadataProvider != null) {
      list.add(metadataProvider);
    }
    hepPlanner.registerMetadataProviders(list);
    for (RelOptMaterialization materialization : materializations) {
      hepPlanner.addMaterialization(materialization);
    }
    for (RelOptLattice lattice : lattices) {
      hepPlanner.addLattice(lattice);
    }
    RelMetadataProvider plannerChain =
        ChainedRelMetadataProvider.of(list);
    rel.getCluster().setMetadataProvider(plannerChain);

    hepPlanner.setRoot(rel);
    return hepPlanner.findBestExp();
  };
}
 
Example #12
Source File: MaterializedViewProjectAggregateRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public MaterializedViewProjectAggregateRule(RelBuilderFactory relBuilderFactory,
    boolean generateUnionRewriting, HepProgram unionRewritingPullProgram,
    RelOptRule filterProjectTransposeRule,
    RelOptRule filterAggregateTransposeRule,
    RelOptRule aggregateProjectPullUpConstantsRule,
    RelOptRule projectMergeRule) {
  super(
      operand(Project.class,
          operand(Aggregate.class, any())),
      relBuilderFactory,
      "MaterializedViewAggregateRule(Project-Aggregate)",
      generateUnionRewriting, unionRewritingPullProgram,
      filterProjectTransposeRule,
      filterAggregateTransposeRule,
      aggregateProjectPullUpConstantsRule,
      projectMergeRule);
}
 
Example #13
Source File: MaterializedViewSubstitutionVisitorTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelNode canonicalize(RelNode rel) {
  HepProgram program =
      new HepProgramBuilder()
          .addRuleInstance(FilterProjectTransposeRule.INSTANCE)
          .addRuleInstance(FilterMergeRule.INSTANCE)
          .addRuleInstance(FilterJoinRule.FILTER_ON_JOIN)
          .addRuleInstance(FilterJoinRule.JOIN)
          .addRuleInstance(FilterAggregateTransposeRule.INSTANCE)
          .addRuleInstance(ProjectMergeRule.INSTANCE)
          .addRuleInstance(ProjectRemoveRule.INSTANCE)
          .addRuleInstance(ProjectJoinTransposeRule.INSTANCE)
          .addRuleInstance(ProjectSetOpTransposeRule.INSTANCE)
          .addRuleInstance(FilterToCalcRule.INSTANCE)
          .addRuleInstance(ProjectToCalcRule.INSTANCE)
          .addRuleInstance(FilterCalcMergeRule.INSTANCE)
          .addRuleInstance(ProjectCalcMergeRule.INSTANCE)
          .addRuleInstance(CalcMergeRule.INSTANCE)
          .build();
  final HepPlanner hepPlanner = new HepPlanner(program);
  hepPlanner.setRoot(rel);
  return hepPlanner.findBestExp();
}
 
Example #14
Source File: SqlHintsConverterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testHintsPropagationInHepPlannerRules() {
  final String sql = "select /*+ use_hash_join(r, s), use_hash_join(emp, dept) */\n"
      + "ename, job, sal, dept.name\n"
      + "from emp join dept on emp.deptno = dept.deptno";
  final RelNode rel = tester.convertSqlToRel(sql).rel;
  final RelHint hint = RelHint.builder("USE_HASH_JOIN")
      .inheritPath(0)
      .hintOption("EMP")
      .hintOption("DEPT")
      .build();
  // Validate Hep planner.
  HepProgram program = new HepProgramBuilder()
      .addRuleInstance(MockJoinRule.INSTANCE)
      .build();
  HepPlanner planner = new HepPlanner(program);
  planner.setRoot(rel);
  RelNode newRel = planner.findBestExp();
  new ValidateHintVisitor(hint, Join.class).go(newRel);
}
 
Example #15
Source File: RelFieldTrimmerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCalcFieldTrimmer1() {
  final RelBuilder builder = RelBuilder.create(config().build());
  final RelNode root =
      builder.scan("EMP")
          .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
          .exchange(RelDistributions.SINGLETON)
          .filter(
              builder.call(SqlStdOperatorTable.GREATER_THAN,
                  builder.field("EMPNO"), builder.literal(100)))
          .build();

  final HepProgram hepProgram = new HepProgramBuilder()
      .addRuleInstance(ProjectToCalcRule.INSTANCE)
      .addRuleInstance(FilterToCalcRule.INSTANCE)
      .build();

  final HepPlanner hepPlanner = new HepPlanner(hepProgram);
  hepPlanner.setRoot(root);
  final RelNode relNode = hepPlanner.findBestExp();
  final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
  final RelNode trimmed = fieldTrimmer.trim(relNode);

  final String expected = ""
      + "LogicalCalc(expr#0..2=[{inputs}], expr#3=[100], expr#4=[>($t0, $t3)], proj#0."
      + ".2=[{exprs}], $condition=[$t4])\n"
      + "  LogicalExchange(distribution=[single])\n"
      + "    LogicalCalc(expr#0..2=[{inputs}], proj#0..2=[{exprs}])\n"
      + "      LogicalProject(EMPNO=[$0], ENAME=[$1], DEPTNO=[$7])\n"
      + "        LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(trimmed, hasTree(expected));
}
 
Example #16
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 #17
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMaterialization() throws Exception {
  HepPlanner planner = new HepPlanner(HepProgram.builder().build());
  RelNode tableRel = tester.convertSqlToRel("select * from dept").rel;
  RelNode queryRel = tableRel;
  RelOptMaterialization mat1 = new RelOptMaterialization(
      tableRel, queryRel, null, ImmutableList.of("default", "mv"));
  planner.addMaterialization(mat1);
  assertEquals(planner.getMaterializations().size(), 1);
  assertEquals(planner.getMaterializations().get(0), mat1);
  planner.clear();
  assertEquals(planner.getMaterializations().size(), 0);
}
 
Example #18
Source File: InterpreterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testInterpretSemiJoin() throws Exception {
  final String sql = "select x, y from (values (1, 'a'), (2, 'b'), (3, 'c')) as t(x, y)\n"
      + "where x in\n"
      + "(select x from (values (1, 'd'), (3, 'g')) as t2(x, y))";
  SqlNode validate = planner.validate(planner.parse(sql));
  RelNode convert = planner.rel(validate).rel;
  final HepProgram program = new HepProgramBuilder()
      .addRuleInstance(SemiJoinRule.PROJECT)
      .build();
  final HepPlanner hepPlanner = new HepPlanner(program);
  hepPlanner.setRoot(convert);
  final RelNode relNode = hepPlanner.findBestExp();
  final Interpreter interpreter = new Interpreter(dataContext, relNode);
  assertRows(interpreter, true, "[1, a]", "[3, c]");
}
 
Example #19
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCalcColumnOriginsTable() {
  final String sql = "select name,deptno from dept where deptno > 10";
  final RelNode relNode = convertSql(sql);
  final HepProgram program = new HepProgramBuilder().
      addRuleInstance(ProjectToCalcRule.INSTANCE).build();
  final HepPlanner planner = new HepPlanner(program);
  planner.setRoot(relNode);
  final RelNode calc = planner.findBestExp();
  final RelMetadataQuery mq = calc.getCluster().getMetadataQuery();
  final RelColumnOrigin nameColumn = mq.getColumnOrigin(calc, 0);
  assertThat(nameColumn.getOriginColumnOrdinal(), is(1));
  final RelColumnOrigin deptnoColumn = mq.getColumnOrigin(calc, 1);
  assertThat(deptnoColumn.getOriginColumnOrdinal(), is(0));
}
 
Example #20
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 #21
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
Sql(Tester tester, String sql, HepProgram preProgram, RelOptPlanner planner,
    ImmutableMap<Hook, Consumer> hooks,
    ImmutableList<Function<Tester, Tester>> transforms) {
  this.tester = tester;
  this.sql = sql;
  this.preProgram = preProgram;
  this.planner = planner;
  this.hooks = hooks;
  this.transforms = transforms;
}
 
Example #22
Source File: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a AbstractMaterializedViewRule. */
protected AbstractMaterializedViewRule(RelOptRuleOperand operand, RelBuilderFactory relBuilderFactory,
        String description, boolean generateUnionRewriting, HepProgram unionRewritingPullProgram,
        boolean fastBailOut) {
    super(operand, relBuilderFactory, description);
    this.generateUnionRewriting = generateUnionRewriting;
    this.unionRewritingPullProgram = unionRewritingPullProgram;
    this.fastBailOut = fastBailOut;
}
 
Example #23
Source File: RelFieldTrimmerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCalcFieldTrimmer2() {
  final RelBuilder builder = RelBuilder.create(config().build());
  final RelNode root =
      builder.scan("EMP")
          .project(builder.field("EMPNO"), builder.field("ENAME"), builder.field("DEPTNO"))
          .exchange(RelDistributions.SINGLETON)
          .filter(
              builder.call(SqlStdOperatorTable.GREATER_THAN,
                  builder.field("EMPNO"), builder.literal(100)))
          .project(builder.field("EMPNO"), builder.field("ENAME"))
          .build();

  final HepProgram hepProgram = new HepProgramBuilder()
      .addRuleInstance(ProjectToCalcRule.INSTANCE)
      .addRuleInstance(FilterToCalcRule.INSTANCE)
      .addRuleInstance(CalcMergeRule.INSTANCE).build();

  final HepPlanner hepPlanner = new HepPlanner(hepProgram);
  hepPlanner.setRoot(root);
  final RelNode relNode = hepPlanner.findBestExp();
  final RelFieldTrimmer fieldTrimmer = new RelFieldTrimmer(null, builder);
  final RelNode trimmed = fieldTrimmer.trim(relNode);

  final String expected = ""
      + "LogicalCalc(expr#0..1=[{inputs}], expr#2=[100], expr#3=[>($t0, $t2)], proj#0."
      + ".1=[{exprs}], $condition=[$t3])\n"
      + "  LogicalExchange(distribution=[single])\n"
      + "    LogicalCalc(expr#0..1=[{inputs}], proj#0..1=[{exprs}])\n"
      + "      LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
      + "        LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(trimmed, hasTree(expected));
}
 
Example #24
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 #25
Source File: MaterializedViewAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a MaterializedViewAggregateRule. */
protected MaterializedViewAggregateRule(RelOptRuleOperand operand,
    RelBuilderFactory relBuilderFactory, String description,
    boolean generateUnionRewriting, HepProgram unionRewritingPullProgram,
    RelOptRule filterProjectTransposeRule,
    RelOptRule filterAggregateTransposeRule,
    RelOptRule aggregateProjectPullUpConstantsRule,
    RelOptRule projectMergeRule) {
  super(operand, relBuilderFactory, description, generateUnionRewriting,
      unionRewritingPullProgram, false);
  this.filterProjectTransposeRule = filterProjectTransposeRule;
  this.filterAggregateTransposeRule = filterAggregateTransposeRule;
  this.aggregateProjectPullUpConstantsRule = aggregateProjectPullUpConstantsRule;
  this.projectMergeRule = projectMergeRule;
}
 
Example #26
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private HepPlanner createPlanner(HepProgram program) {
  // Create a planner with a hook to update the mapping tables when a
  // node is copied when it is registered.
  return new HepPlanner(
      program,
      context,
      true,
      createCopyHook(),
      RelOptCostImpl.FACTORY);
}
 
Example #27
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode removeCorrelationViaRule(RelNode root) {
  final RelBuilderFactory f = relBuilderFactory();
  HepProgram program = HepProgram.builder()
      .addRuleInstance(new RemoveSingleAggregateRule(f))
      .addRuleInstance(new RemoveCorrelationForScalarProjectRule(f))
      .addRuleInstance(new RemoveCorrelationForScalarAggregateRule(f))
      .build();

  HepPlanner planner = createPlanner(program);

  planner.setRoot(root);
  return planner.findBestExp();
}
 
Example #28
Source File: Interpreter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelNode optimize(RelNode rootRel) {
  final HepProgram hepProgram = new HepProgramBuilder()
      .addRuleInstance(CalcSplitRule.INSTANCE)
      .addRuleInstance(FilterTableScanRule.INSTANCE)
      .addRuleInstance(FilterTableScanRule.INTERPRETER)
      .addRuleInstance(ProjectTableScanRule.INSTANCE)
      .addRuleInstance(ProjectTableScanRule.INTERPRETER)
      .addRuleInstance(AggregateReduceFunctionsRule.INSTANCE)
      .build();
  final HepPlanner planner = new HepPlanner(hepProgram);
  planner.setRoot(rootRel);
  rootRel = planner.findBestExp();
  return rootRel;
}
 
Example #29
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 #30
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);
}