Java Code Examples for org.apache.calcite.sql2rel.RelDecorrelator

The following examples show how to use org.apache.calcite.sql2rel.RelDecorrelator. These examples are extracted from open source projects. 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 Project: Bats   Source File: Programs.java    License: 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 Project: Bats   Source File: DefaultSqlHandler.java    License: 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 Project: Mycat2   Source File: PlannerImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public RelRoot expandView(RelDataType rowType, String queryString,
                          List<String> schemaPath, List<String> viewPath) {
    if (planner == null) {
        ready();
    }
    SqlParser parser = SqlParser.create(queryString, parserConfig);
    SqlNode sqlNode;
    try {
        sqlNode = parser.parseQuery();
    } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
    }

    final CalciteCatalogReader catalogReader =
            createCatalogReader().withSchemaPath(schemaPath);
    final SqlValidator validator = createSqlValidator(catalogReader);

    final RexBuilder rexBuilder = createRexBuilder();
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    final SqlToRelConverter.Config config = SqlToRelConverter
            .configBuilder()
            .withConfig(sqlToRelConverterConfig)
            .withTrimUnusedFields(false)
            .build();
    final SqlToRelConverter sqlToRelConverter =
            new SqlToRelConverter(this, validator,
                    catalogReader, cluster, convertletTable, config);

    final RelRoot root =
            sqlToRelConverter.convertQuery(sqlNode, true, false);
    final RelRoot root2 =
            root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
    final RelBuilder relBuilder =
            config.getRelBuilderFactory().create(cluster, null);
    return root2.withRel(
            RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 4
Source Project: calcite   Source File: PlannerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RelRoot expandView(RelDataType rowType, String queryString,
    List<String> schemaPath, List<String> viewPath) {
  if (planner == null) {
    ready();
  }
  SqlParser parser = SqlParser.create(queryString, parserConfig);
  SqlNode sqlNode;
  try {
    sqlNode = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException("parse failed", e);
  }

  final CalciteCatalogReader catalogReader =
      createCatalogReader().withSchemaPath(schemaPath);
  final SqlValidator validator = createSqlValidator(catalogReader);

  final RexBuilder rexBuilder = createRexBuilder();
  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  final SqlToRelConverter.Config config = SqlToRelConverter
      .configBuilder()
      .withConfig(sqlToRelConverterConfig)
      .withTrimUnusedFields(false)
      .build();
  final SqlToRelConverter sqlToRelConverter =
      new SqlToRelConverter(this, validator,
          catalogReader, cluster, convertletTable, config);

  final RelRoot root =
      sqlToRelConverter.convertQuery(sqlNode, true, false);
  final RelRoot root2 =
      root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
  final RelBuilder relBuilder =
      config.getRelBuilderFactory().create(cluster, null);
  return root2.withRel(
      RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
 
Example 5
Source Project: calcite   Source File: Programs.java    License: 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 6
Source Project: calcite   Source File: MutableRelTest.java    License: 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 7
Source Project: calcite   Source File: RelOptTestBase.java    License: 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);
}