Java Code Examples for org.apache.calcite.sql2rel.RelDecorrelator#decorrelateQuery()

The following examples show how to use org.apache.calcite.sql2rel.RelDecorrelator#decorrelateQuery() . 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 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example 2
Source File: DefaultSqlHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelNode convertToRel(SqlNode node) {
  final RelNode convertedNode = config.getConverter().toRel(node).rel;
  log("INITIAL", convertedNode, logger, null);
  RelNode transformedNode = transform(PlannerType.HEP,
      PlannerPhase.SUBQUERY_REWRITE, convertedNode);

  RelNode decorrelatedNode = RelDecorrelator.decorrelateQuery(transformedNode,
      DrillRelFactories.LOGICAL_BUILDER.create(transformedNode.getCluster(), null));

  return transform(PlannerType.HEP, PlannerPhase.WINDOW_REWRITE, decorrelatedNode);
}
 
Example 3
Source File: Programs.java    From calcite 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) {
  final CalciteConnectionConfig config =
      planner.getContext().unwrap(CalciteConnectionConfig.class);
  if (config != null && config.forceDecorrelate()) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    return RelDecorrelator.decorrelateQuery(rel, relBuilder);
  }
  return rel;
}
 
Example 4
Source File: MutableRelTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Verifies that after conversion to and from a MutableRel, the new
 * RelNode remains identical to the original RelNode. */
private static void checkConvertMutableRel(
    String rel, String sql, boolean decorrelate, List<RelOptRule> rules) {
  final SqlToRelTestBase test = new SqlToRelTestBase() {
  };
  RelNode origRel = test.createTester().convertSqlToRel(sql).rel;
  if (decorrelate) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(origRel.getCluster(), null);
    origRel = RelDecorrelator.decorrelateQuery(origRel, relBuilder);
  }
  if (rules != null) {
    final HepProgram hepProgram =
        new HepProgramBuilder().addRuleCollection(rules).build();
    final HepPlanner hepPlanner = new HepPlanner(hepProgram);
    hepPlanner.setRoot(origRel);
    origRel = hepPlanner.findBestExp();
  }
  // Convert to and from a mutable rel.
  final MutableRel mutableRel = MutableRels.toMutable(origRel);
  final RelNode newRel = MutableRels.fromMutable(mutableRel);

  // Check if the mutable rel digest contains the target rel.
  final String mutableRelStr = mutableRel.deep();
  final String msg1 =
      "Mutable rel: " + mutableRelStr + " does not contain target rel: " + rel;
  assertTrue(mutableRelStr.contains(rel), msg1);

  // Check if the mutable rel's row-type is identical to the original
  // rel's row-type.
  final RelDataType origRelType = origRel.getRowType();
  final RelDataType mutableRelType = mutableRel.rowType;
  final String msg2 =
      "Mutable rel's row type does not match with the original rel.\n"
      + "Original rel type: " + origRelType
      + ";\nMutable rel type: " + mutableRelType;
  assertTrue(
      equal(
          "origRelType", origRelType,
          "mutableRelType", mutableRelType,
          IGNORE),
      msg2);

  // Check if the new rel converted from the mutable rel is identical
  // to the original rel.
  final String origRelStr = RelOptUtil.toString(origRel);
  final String newRelStr = RelOptUtil.toString(newRel);
  final String msg3 =
      "The converted new rel is different from the original rel.\n"
      + "Original rel: " + origRelStr + ";\nNew rel: " + newRelStr;
  assertEquals(origRelStr, newRelStr, msg3);
}
 
Example 5
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);
}