Java Code Examples for org.apache.calcite.plan.RelOptPlanner#findBestExp()

The following examples show how to use org.apache.calcite.plan.RelOptPlanner#findBestExp() . 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: Programs.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  planner.clear();
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }
  for (RelOptMaterialization materialization : materializations) {
    planner.addMaterialization(materialization);
  }
  for (RelOptLattice lattice : lattices) {
    planner.addLattice(lattice);
  }
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
  }
  planner.setRoot(rel);
  return planner.findBestExp();

}
 
Example 2
Source File: Programs.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  planner.clear();
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }
  for (RelOptMaterialization materialization : materializations) {
    planner.addMaterialization(materialization);
  }
  for (RelOptLattice lattice : lattices) {
    planner.addLattice(lattice);
  }
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
  }
  planner.setRoot(rel);
  return planner.findBestExp();

}
 
Example 3
Source File: BatsOptimizerTest.java    From Bats with Apache License 2.0 5 votes vote down vote up
static void testHepPlanner() throws Exception {
    RelOptPlanner hepPlanner = createHepPlanner();
    RelNode relNode = testSqlToRelConverter(hepPlanner);
    hepPlanner = relNode.getCluster().getPlanner();
    // relNode.getCluster().getPlanner().setExecutor(RexUtil.EXECUTOR);
    hepPlanner.setRoot(relNode);
    relNode = hepPlanner.findBestExp();

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Hep Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
}
 
Example 4
Source File: TestDremioPlanners.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void checkCancelFlag(RelOptPlanner planner) {
  expectedException.expect(UserException.class);
  expectedException.expectMessage("Query was cancelled because planning time exceeded");
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(SqlTypeFactoryImpl.INSTANCE));
  RelNode root = new NoneRel(cluster);
  planner.setRoot(root);
  planner.findBestExp();
}
 
Example 5
Source File: SqlWorker.java    From quark with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
                   RelTraitSet requiredOutputTraits,
                   List<RelOptMaterialization> materializations,
                   List<RelOptLattice> lattices) {
  planner.clear();

  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  //((VolcanoPlanner) planner).registerAbstractRelationalRules();

  RelOptUtil.registerAbstractRels(planner);
  for (RelOptRule rule : ruleSet) {
    planner.addRule(rule);
  }

  planner.addRule(Bindables.BINDABLE_TABLE_SCAN_RULE);
  planner.addRule(ProjectTableScanRule.INSTANCE);
  planner.addRule(ProjectTableScanRule.INTERPRETER);
  planner.addRule(EnumerableInterpreterRule.INSTANCE);

  final CalciteSchema rootSchema = CalciteSchema.from(context.getRootSchema());
  planner.setExecutor(new RexExecutorImpl(null));
  planner.setRoot(rel);

  MaterializationService.setThreadLocal(materializationService);
  plannerHolder.setPlanner(planner);
  populateMaterializationsAndLattice(plannerHolder, rootSchema);
  if (!rel.getTraitSet().equals(requiredOutputTraits)) {
    rel = planner.changeTraits(rel, requiredOutputTraits);
    planner.setRoot(rel);
  }

  RelOptPlanner planner2 = planner.chooseDelegate();
  return planner2.findBestExp();
}
 
Example 6
Source File: FrameworksTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void executeQuery(FrameworkConfig config,
    @SuppressWarnings("SameParameterValue") String query, boolean debug)
    throws RelConversionException, SqlParseException, ValidationException {
  Planner planner = Frameworks.getPlanner(config);
  if (debug) {
    System.out.println("Query:" + query);
  }
  SqlNode n = planner.parse(query);
  n = planner.validate(n);
  RelNode root = planner.rel(n).project();
  if (debug) {
    System.out.println(
        RelOptUtil.dumpPlan("-- Logical Plan", root, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  RelOptCluster cluster = root.getCluster();
  final RelOptPlanner optPlanner = cluster.getPlanner();

  RelTraitSet desiredTraits  =
      cluster.traitSet().replace(EnumerableConvention.INSTANCE);
  final RelNode newRoot = optPlanner.changeTraits(root, desiredTraits);
  if (debug) {
    System.out.println(
        RelOptUtil.dumpPlan("-- Mid Plan", newRoot, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
  optPlanner.setRoot(newRoot);
  RelNode bestExp = optPlanner.findBestExp();
  if (debug) {
    System.out.println(
        RelOptUtil.dumpPlan("-- Best Plan", bestExp, SqlExplainFormat.TEXT,
            SqlExplainLevel.DIGEST_ATTRIBUTES));
  }
}
 
Example 7
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 4 votes vote down vote up
private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException,
        SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
    SchemaPlus subSchema = getSchemaForTableSpace(defaultTableSpace);
    if (subSchema == null) {
        clearCache();
        throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
    }
    Properties props = new Properties();
    props.put(CalciteConnectionProperty.TIME_ZONE.camelName(), TimeZone.getDefault().getID());
    props.put(CalciteConnectionProperty.LOCALE.camelName(), Locale.ROOT.toString());
    final CalciteConnectionConfigImpl calciteRuntimeContextConfig = new CalciteConnectionConfigImpl(props);

    final FrameworkConfig config = Frameworks.newConfigBuilder()
            .parserConfig(SQL_PARSER_CONFIG)
            .defaultSchema(subSchema)
            .traitDefs(TRAITS)
            .context(new Context() {
                @Override
                public <C> C unwrap(Class<C> aClass) {
                    if (aClass == CalciteConnectionConfigImpl.class
                            || aClass == CalciteConnectionConfig.class) {
                        return (C) calciteRuntimeContextConfig;
                    }
                    return null;
                }
            })
            // define the rules you want to apply
            .programs(Programs.ofRules(Programs.RULE_SET))
            .build();
    Planner planner = Frameworks.getPlanner(config);
    if (LOG.isLoggable(Level.FINER)) {
        LOG.log(Level.FINER, "Query: {0}", query);
    }
    try {
        SqlNode n = planner.parse(query);
        n = planner.validate(n);
        RelNode logicalPlan = planner.rel(n).project();
        if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
            LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[]{query,
                RelOptUtil.dumpPlan("-- Logical Plan", logicalPlan, SqlExplainFormat.TEXT,
                SqlExplainLevel.ALL_ATTRIBUTES)});
        }
        RelDataType originalRowType = logicalPlan.getRowType();
        RelOptCluster cluster = logicalPlan.getCluster();
        final RelOptPlanner optPlanner = cluster.getPlanner();

        optPlanner.addRule(ReduceExpressionsRule.FILTER_INSTANCE);
        RelTraitSet desiredTraits =
                cluster.traitSet()
                        .replace(EnumerableConvention.INSTANCE);
        final RelCollation collation =
                logicalPlan instanceof Sort
                        ? ((Sort) logicalPlan).collation
                        : null;
        if (collation != null) {
            desiredTraits = desiredTraits.replace(collation);
        }
        final RelNode newRoot = optPlanner.changeTraits(logicalPlan, desiredTraits);
        optPlanner.setRoot(newRoot);
        RelNode bestExp = optPlanner.findBestExp();
        if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
            LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[]{query,
                RelOptUtil.dumpPlan("-- Best  Plan", bestExp, SqlExplainFormat.TEXT,
                SqlExplainLevel.ALL_ATTRIBUTES)});
        }

        return new PlannerResult(bestExp, originalRowType, logicalPlan, n);
    } catch (AssertionError err) {
        throw new StatementExecutionException("Internal Calcite error " + err, err);
    }
}
 
Example 8
Source File: PigRelBuilderStyleTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RelNode optimizeWithVolcano(RelNode root) {
  RelOptPlanner planner = getVolcanoPlanner(root);
  return planner.findBestExp();
}
 
Example 9
Source File: RelOptTestBase.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Checks the plan for a SQL statement before/after executing a given rule,
 * with a pre-program to prepare the tree.
 *
 * @param tester     Tester
 * @param preProgram Program to execute before comparing before state
 * @param planner    Planner
 * @param sql        SQL query
 * @param unchanged  Whether the rule is to have no effect
 */
private void checkPlanning(Tester tester, HepProgram preProgram,
    RelOptPlanner planner, String sql, boolean unchanged) {
  final DiffRepository diffRepos = getDiffRepos();
  String sql2 = diffRepos.expand("sql", sql);
  final RelRoot root = tester.convertSqlToRel(sql2);
  final RelNode relInitial = root.rel;

  assertNotNull(relInitial);

  List<RelMetadataProvider> list = new ArrayList<>();
  list.add(DefaultRelMetadataProvider.INSTANCE);
  planner.registerMetadataProviders(list);
  RelMetadataProvider plannerChain =
      ChainedRelMetadataProvider.of(list);
  final RelOptCluster cluster = relInitial.getCluster();
  cluster.setMetadataProvider(plannerChain);

  RelNode relBefore;
  if (preProgram == null) {
    relBefore = relInitial;
  } else {
    HepPlanner prePlanner = new HepPlanner(preProgram);
    prePlanner.setRoot(relInitial);
    relBefore = prePlanner.findBestExp();
  }

  assertThat(relBefore, notNullValue());

  final String planBefore = NL + RelOptUtil.toString(relBefore);
  diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
  SqlToRelTestBase.assertValid(relBefore);

  if (planner instanceof VolcanoPlanner) {
    relBefore = planner.changeTraits(relBefore,
        relBefore.getTraitSet().replace(EnumerableConvention.INSTANCE));
  }
  planner.setRoot(relBefore);
  RelNode r = planner.findBestExp();
  if (tester.isLateDecorrelate()) {
    final String planMid = NL + RelOptUtil.toString(r);
    diffRepos.assertEquals("planMid", "${planMid}", planMid);
    SqlToRelTestBase.assertValid(r);
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(cluster, null);
    r = RelDecorrelator.decorrelateQuery(r, relBuilder);
  }
  final String planAfter = NL + RelOptUtil.toString(r);
  if (unchanged) {
    assertThat(planAfter, is(planBefore));
  } else {
    diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
    if (planBefore.equals(planAfter)) {
      throw new AssertionError("Expected plan before and after is the same.\n"
          + "You must use unchanged=true or call checkUnchanged");
    }
  }
  SqlToRelTestBase.assertValid(r);
}