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

The following examples show how to use org.apache.calcite.plan.hep.HepPlanner. 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: MaterializedViewFilterScanRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  final RelOptPlanner planner = call.getPlanner();
  final List<RelOptMaterialization> materializations =
      planner.getMaterializations();
  if (!materializations.isEmpty()) {
    RelNode root = filter.copy(filter.getTraitSet(),
        Collections.singletonList((RelNode) scan));
    List<RelOptMaterialization> applicableMaterializations =
        RelOptMaterializations.getApplicableMaterializations(root, materializations);
    for (RelOptMaterialization materialization : applicableMaterializations) {
      if (RelOptUtil.areRowTypesEqual(scan.getRowType(),
          materialization.queryRel.getRowType(), false)) {
        RelNode target = materialization.queryRel;
        final HepPlanner hepPlanner =
            new HepPlanner(program, planner.getContext());
        hepPlanner.setRoot(target);
        target = hepPlanner.findBestExp();
        List<RelNode> subs = new SubstitutionVisitor(target, root)
            .go(materialization.tableRel);
        for (RelNode s : subs) {
          call.transformTo(s);
        }
      }
    }
  }
}
 
Example #3
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 #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: 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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: MaterializedViewFilterScanRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected void apply(RelOptRuleCall call, Filter filter, TableScan scan) {
  final RelOptPlanner planner = call.getPlanner();
  final List<RelOptMaterialization> materializations =
      planner.getMaterializations();
  if (!materializations.isEmpty()) {
    RelNode root = filter.copy(filter.getTraitSet(),
        Collections.singletonList((RelNode) scan));
    List<RelOptMaterialization> applicableMaterializations =
        RelOptMaterializations.getApplicableMaterializations(root, materializations);
    for (RelOptMaterialization materialization : applicableMaterializations) {
      if (RelOptUtil.areRowTypesEqual(scan.getRowType(),
          materialization.queryRel.getRowType(), false)) {
        RelNode target = materialization.queryRel;
        final HepPlanner hepPlanner =
            new HepPlanner(program, planner.getContext());
        hepPlanner.setRoot(target);
        target = hepPlanner.findBestExp();
        List<RelNode> subs = new MaterializedViewSubstitutionVisitor(target, root)
            .go(materialization.tableRel);
        for (RelNode s : subs) {
          call.transformTo(s);
        }
      }
    }
  }
}
 
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: 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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelNode decorrelate(RelNode root) {
    // first adjust count() expression if any
    final RelBuilderFactory f = relBuilderFactory();
    HepProgram program = HepProgram.builder().addRuleInstance(new AdjustProjectForCountAggregateRule(false, f))
            .addRuleInstance(new AdjustProjectForCountAggregateRule(true, f))
            .addRuleInstance(new FilterJoinRule.FilterIntoJoinRule(true, f, FilterJoinRule.TRUE_PREDICATE))
            .addRuleInstance(new FilterProjectTransposeRule(Filter.class, Project.class, true, true, f))
            .addRuleInstance(new FilterCorrelateRule(f)).build();

    HepPlanner planner = createPlanner(program);

    planner.setRoot(root);
    root = planner.findBestExp();

    // Perform decorrelation.
    map.clear();

    final Frame frame = getInvoke(root, null);
    if (frame != null) {
        // has been rewritten; apply rules post-decorrelation
        final HepProgram program2 = HepProgram.builder()
                .addRuleInstance(new FilterJoinRule.FilterIntoJoinRule(true, f, FilterJoinRule.TRUE_PREDICATE))
                .addRuleInstance(new FilterJoinRule.JoinConditionPushRule(f, FilterJoinRule.TRUE_PREDICATE))
                .build();

        final HepPlanner planner2 = createPlanner(program2);
        final RelNode newRoot = frame.r;
        planner2.setRoot(newRoot);
        return planner2.findBestExp();
    }

    return root;
}
 
Example #29
Source File: SqlHintsConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testHintsForCalc() {
  final String sql = "select /*+ resource(mem='1024MB')*/ ename, sal, deptno from emp";
  final RelNode rel = tester.convertSqlToRel(sql).rel;
  final RelHint hint = RelHint.builder("RESOURCE")
      .hintOption("MEM", "1024MB")
      .build();
  // planner rule to convert Project to Calc.
  HepProgram program = new HepProgramBuilder()
      .addRuleInstance(ProjectToCalcRule.INSTANCE)
      .build();
  HepPlanner planner = new HepPlanner(program);
  planner.setRoot(rel);
  RelNode newRel = planner.findBestExp();
  new ValidateHintVisitor(hint, Calc.class).go(newRel);
}
 
Example #30
Source File: RelDecorrelator.java    From Bats 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();
}