Java Code Examples for org.apache.calcite.plan.volcano.VolcanoPlanner#addRelTraitDef()

The following examples show how to use org.apache.calcite.plan.volcano.VolcanoPlanner#addRelTraitDef() . 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: BatsOptimizerTest.java    From Bats with Apache License 2.0 6 votes vote down vote up
static void testVolcanoPlanner() throws Exception {
    VolcanoPlanner volcanoPlanner = createVolcanoPlanner();
    volcanoPlanner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    // volcanoPlanner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    // addRules(volcanoPlanner);
    volcanoPlanner.addRule(ReduceExpressionsRule.PROJECT_INSTANCE);
    // volcanoPlanner.addRule(EnumerableRules.ENUMERABLE_PROJECT_RULE);

    RelNode relNode = testSqlToRelConverter(volcanoPlanner);
    volcanoPlanner.setRoot(relNode);
    relNode = volcanoPlanner.findBestExp(); // 在这一步出错

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Volcano Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
}
 
Example 2
Source File: PlannerImpl.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void ready() {
    switch (state) {
        case STATE_0_CLOSED:
            reset();
    }
    ensure(State.STATE_1_RESET);
    planner = new VolcanoPlanner(costFactory, context);
    RelOptUtil.registerDefaultRules(planner,
            connectionConfig.materializationsEnabled(),
            Hook.ENABLE_BINDABLE.get(false));
    planner.setExecutor(executor);

    state = State.STATE_2_READY;

    // If user specify own traitDef, instead of default default trait,
    // register the trait def specified in traitDefs.
    if (this.traitDefs == null) {
        planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
        if (CalciteSystemProperty.ENABLE_COLLATION_TRAIT.value()) {
            planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
        }
    } else {
        for (RelTraitDef def : this.traitDefs) {
            planner.addRelTraitDef(def);
        }
    }
}
 
Example 3
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ParseResult convert_(Context context, String sql, boolean analyze,
    boolean fail, CalciteCatalogReader catalogReader, SqlValidator validator,
    SqlNode sqlNode1) {
  final JavaTypeFactory typeFactory = context.getTypeFactory();
  final Convention resultConvention =
      enableBindable ? BindableConvention.INSTANCE
          : EnumerableConvention.INSTANCE;
  // Use the Volcano because it can handle the traits.
  final VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  final SqlToRelConverter.ConfigBuilder configBuilder =
      SqlToRelConverter.configBuilder().withTrimUnusedFields(true);

  final CalcitePreparingStmt preparingStmt =
      new CalcitePreparingStmt(this, context, catalogReader, typeFactory,
          context.getRootSchema(), null, createCluster(planner, new RexBuilder(typeFactory)),
          resultConvention, createConvertletTable());
  final SqlToRelConverter converter =
      preparingStmt.getSqlToRelConverter(validator, catalogReader,
          configBuilder.build());

  final RelRoot root = converter.convertQuery(sqlNode1, false, true);
  if (analyze) {
    return analyze_(validator, sql, sqlNode1, root, fail);
  }
  return new ConvertResult(this, validator, sql, sqlNode1,
      validator.getValidatedNodeType(sqlNode1), root);
}
 
Example 4
Source File: PlannerImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void ready() {
  switch (state) {
  case STATE_0_CLOSED:
    reset();
  }
  ensure(State.STATE_1_RESET);

  RelDataTypeSystem typeSystem =
      connectionConfig.typeSystem(RelDataTypeSystem.class,
          RelDataTypeSystem.DEFAULT);
  typeFactory = new JavaTypeFactoryImpl(typeSystem);
  planner = new VolcanoPlanner(costFactory, context);
  RelOptUtil.registerDefaultRules(planner,
      connectionConfig.materializationsEnabled(),
      Hook.ENABLE_BINDABLE.get(false));
  planner.setExecutor(executor);

  state = State.STATE_2_READY;

  // If user specify own traitDef, instead of default default trait,
  // register the trait def specified in traitDefs.
  if (this.traitDefs == null) {
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    if (CalciteSystemProperty.ENABLE_COLLATION_TRAIT.value()) {
      planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    }
  } else {
    for (RelTraitDef def : this.traitDefs) {
      planner.addRelTraitDef(def);
    }
  }
}
 
Example 5
Source File: TopDownOptTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Query(String sql) {
  this.sql = sql;

  planner = new VolcanoPlanner();
  // Always use top-down optimization
  planner.setTopDownOpt(true);
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);

  RelOptUtil.registerDefaultRules(planner, false, false);

  // Remove to Keep deterministic join order.
  planner.removeRule(JoinCommuteRule.INSTANCE);
  planner.removeRule(JoinPushThroughJoinRule.LEFT);
  planner.removeRule(JoinPushThroughJoinRule.RIGHT);

  // Always use sorted agg.
  planner.addRule(EnumerableRules.ENUMERABLE_SORTED_AGGREGATE_RULE);
  planner.removeRule(EnumerableRules.ENUMERABLE_AGGREGATE_RULE);

  // pushing down sort should be handled by top-down optimization.
  planner.removeRule(SortProjectTransposeRule.INSTANCE);

  // Sort will only be pushed down by traits propagation.
  planner.removeRule(SortJoinTransposeRule.INSTANCE);
  planner.removeRule(SortJoinCopyRule.INSTANCE);
}
 
Example 6
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
  planInfos = new PlanInfo[queries];
  VolcanoPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRule(FilterToCalcRule.INSTANCE);
  planner.addRule(ProjectToCalcRule.INSTANCE);
  planner.addRule(EnumerableRules.ENUMERABLE_CALC_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_JOIN_RULE);
  planner.addRule(EnumerableRules.ENUMERABLE_VALUES_RULE);

  RelDataTypeFactory typeFactory =
      new JavaTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeSystem.DEFAULT);
  RelOptCluster cluster = RelOptCluster.create(planner, new RexBuilder(typeFactory));
  RelTraitSet desiredTraits =
      cluster.traitSet().replace(EnumerableConvention.INSTANCE);

  RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(cluster, null);
  // Generates queries of the following form depending on the configuration parameters.
  // SELECT `t`.`name`
  // FROM (VALUES  (1, 'Value0')) AS `t` (`id`, `name`)
  // INNER JOIN (VALUES  (1, 'Value1')) AS `t` (`id`, `name`) AS `t0` ON `t`.`id` = `t0`.`id`
  // INNER JOIN (VALUES  (2, 'Value2')) AS `t` (`id`, `name`) AS `t1` ON `t`.`id` = `t1`.`id`
  // INNER JOIN (VALUES  (3, 'Value3')) AS `t` (`id`, `name`) AS `t2` ON `t`.`id` = `t2`.`id`
  // INNER JOIN ...
  // WHERE
  //  `t`.`name` = 'name0' OR
  //  `t`.`name` = 'name1' OR
  //  `t`.`name` = 'name2' OR
  //  ...
  //  OR `t`.`id` = 0
  // The last disjunction (i.e, t.id = $i) is what makes the queries different from one another
  // by assigning a different constant literal.
  for (int i = 0; i < queries; i++) {
    relBuilder.values(new String[]{"id", "name"}, 1, "Value" + 0);
    for (int j = 1; j <= joins; j++) {
      relBuilder
          .values(new String[]{"id", "name"}, j, "Value" + j)
          .join(JoinRelType.INNER, "id");
    }

    List<RexNode> disjunctions = new ArrayList<>();
    for (int j = 0; j < whereClauseDisjunctions; j++) {
      disjunctions.add(
          relBuilder.equals(
              relBuilder.field("name"),
              relBuilder.literal("name" + j)));
    }
    disjunctions.add(
        relBuilder.equals(
            relBuilder.field("id"),
            relBuilder.literal(i)));
    RelNode query =
        relBuilder
            .filter(relBuilder.or(disjunctions))
            .project(relBuilder.field("name"))
            .build();

    RelNode query0 = planner.changeTraits(query, desiredTraits);
    planner.setRoot(query0);

    PlanInfo info = new PlanInfo();
    EnumerableRel plan = (EnumerableRel) planner.findBestExp();

    EnumerableRelImplementor relImplementor =
        new EnumerableRelImplementor(plan.getCluster().getRexBuilder(), new HashMap<>());
    info.classExpr = relImplementor.implementRoot(plan, EnumerableRel.Prefer.ARRAY);
    info.javaCode =
        Expressions.toString(info.classExpr.memberDeclarations, "\n", false);

    ICompilerFactory compilerFactory;
    try {
      compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
    } catch (Exception e) {
      throw new IllegalStateException(
          "Unable to instantiate java compiler", e);
    }
    IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
    cbe.setClassName(info.classExpr.name);
    cbe.setExtendedClass(Utilities.class);
    cbe.setImplementedInterfaces(
        plan.getRowType().getFieldCount() == 1
            ? new Class[]{Bindable.class, Typed.class}
            : new Class[]{ArrayBindable.class});
    cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
    info.cbe = cbe;
    planInfos[i] = info;
  }

}