Java Code Examples for org.apache.calcite.plan.RelOptUtil#toString()

The following examples show how to use org.apache.calcite.plan.RelOptUtil#toString() . 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   File: BatsOptimizerTest.java    License: 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
private String getPlanDump(RelOptPlanner planner) {
  if (planner == null) {
    return null;
  }

  // Use VolcanoPlanner#dump to get more detailed information
  if (planner instanceof VolcanoPlanner) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    ((VolcanoPlanner) planner).dump(pw);
    pw.flush();
    return sw.toString();
  }

  // Print the current tree otherwise
  RelNode root = planner.getRoot();
  return RelOptUtil.toString(root);
}
 
Example 3
Source Project: quark   File: QuarkTestUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void checkParsedRelString(String sql,
                                        Properties info,
                                        ImmutableList<String> expected,
                                        ImmutableList<String> unexpected)
    throws QuarkException, SQLException {
  SqlQueryParser parser = new SqlQueryParser(info);
  RelNode relNode = parser.parse(sql).getRelNode();
  String relStr = RelOptUtil.toString(relNode);

  for (String expectedPlan : expected) {
    assertTrue("Final Plan should use table " + expectedPlan,
        relStr.contains(expectedPlan));
  }

  for (String unexpectedPlan : unexpected) {
    assertFalse("Final Plan should not use table " + unexpectedPlan,
        relStr.contains(unexpectedPlan));
  }
}
 
Example 4
Source Project: quark   File: QuarkTestUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void checkParsedRelString(String sql,
                                        SqlQueryParser parser,
                                        ImmutableList<String> expected,
                                        ImmutableList<String> unexpected)
    throws QuarkException, SQLException {
  RelNode relNode = parser.parse(sql).getRelNode();
  String relStr = RelOptUtil.toString(relNode);

  for (String expectedPlan : expected) {
    assertTrue("Final Plan should use table " + expectedPlan,
        relStr.contains(expectedPlan));
  }

  for (String unexpectedPlan : unexpected) {
    assertFalse("Final Plan should not use table " + unexpectedPlan,
        relStr.contains(unexpectedPlan));
  }
}
 
Example 5
Source Project: calcite   File: PlannerTest.java    License: Apache License 2.0 6 votes vote down vote up
public String checkTpchQuery(String tpchTestQuery) throws Exception {
  final SchemaPlus schema =
      Frameworks.createRootSchema(true).add("tpch",
          new ReflectiveSchema(new TpchSchema()));

  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(SqlParser.configBuilder().setLex(Lex.MYSQL).build())
      .defaultSchema(schema)
      .programs(Programs.ofRules(Programs.RULE_SET))
      .build();
  String plan;
  try (Planner p = Frameworks.getPlanner(config)) {
    SqlNode n = p.parse(tpchTestQuery);
    n = p.validate(n);
    RelNode r = p.rel(n).project();
    plan = RelOptUtil.toString(r);
  }
  return plan;
}
 
Example 6
Source Project: calcite   File: SqlToRelTestBase.java    License: Apache License 2.0 6 votes vote down vote up
public void assertConvertsTo(
    String sql,
    String plan,
    boolean trim) {
  String sql2 = getDiffRepos().expand("sql", sql);
  RelNode rel = convertSqlToRel(sql2).project();

  assertNotNull(rel);
  assertValid(rel);

  if (trim) {
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
    final RelFieldTrimmer trimmer = createFieldTrimmer(relBuilder);
    rel = trimmer.trim(rel);
    assertNotNull(rel);
    assertValid(rel);
  }

  // NOTE jvs 28-Mar-2006:  insert leading newline so
  // that plans come out nicely stacked instead of first
  // line immediately after CDATA start
  String actual = NL + RelOptUtil.toString(rel);
  diffRepos.assertEquals("plan", plan, actual);
}
 
Example 7
Source Project: calcite   File: MutableRelTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testUpdateInputOfUnion() {
  MutableRel mutableRel = createMutableRel(
      "select sal from emp where deptno = 10"
          + "union select sal from emp where ename like 'John%'");
  MutableRel childMutableRel = createMutableRel(
      "select sal from emp where deptno = 12");
  mutableRel.setInput(0, childMutableRel);
  String actual = RelOptUtil.toString(MutableRels.fromMutable(mutableRel));
  String expected = ""
      + "LogicalUnion(all=[false])\n"
      + "  LogicalProject(SAL=[$5])\n"
      + "    LogicalFilter(condition=[=($7, 12)])\n"
      + "      LogicalTableScan(table=[[CATALOG, SALES, EMP]])\n"
      + "  LogicalProject(SAL=[$5])\n"
      + "    LogicalFilter(condition=[LIKE($1, 'John%')])\n"
      + "      LogicalTableScan(table=[[CATALOG, SALES, EMP]])\n";
  MatcherAssert.assertThat(actual, Matchers.isLinux(expected));
}
 
Example 8
Source Project: Bats   File: BatsOptimizerTest.java    License: Apache License 2.0 5 votes vote down vote up
static RelNode testSqlToRelConverter(RelOptPlanner planner) throws Exception {
    RexBuilder rexBuilder = createRexBuilder();
    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    RelOptTable.ViewExpander viewExpander = ViewExpanders.simpleContext(cluster);

    Pair<SqlNode, SqlValidator> pair = testSqlValidator();
    SqlNode sqlNode = pair.left;
    SqlValidator validator = pair.right;
    CatalogReader catalogReader = createCalciteCatalogReader();
    SqlRexConvertletTable convertletTable = StandardConvertletTable.INSTANCE;
    SqlToRelConverter.Config config = SqlToRelConverter.Config.DEFAULT;
    // 不转换成EnumerableTableScan,而是LogicalTableScan
    config = SqlToRelConverter.configBuilder().withConvertTableAccess(false).build();

    SqlToRelConverter converter = new SqlToRelConverter(viewExpander, validator, catalogReader, cluster,
            convertletTable, config);

    boolean needsValidation = false;
    boolean top = false;
    RelRoot root = converter.convertQuery(sqlNode, needsValidation, top);
    RelNode relNode = root.rel;

    String plan = RelOptUtil.toString(relNode);
    System.out.println("Logical Plan:");
    System.out.println("------------------------------------------------------------------");
    System.out.println(plan);
    System.out.println();

    // testPrograms(root.rel);

    return relNode;
}
 
Example 9
Source Project: Bats   File: BatsOptimizerTest.java    License: 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 10
Source Project: Bats   File: BatsOptimizerTest.java    License: Apache License 2.0 5 votes vote down vote up
static void testPrograms(RelNode relNode) {
    final RelOptPlanner planner = relNode.getCluster().getPlanner();
    final Program program = Programs.ofRules(ReduceExpressionsRule.PROJECT_INSTANCE);
    relNode = program.run(planner, relNode, relNode.getTraitSet(), ImmutableList.of(), ImmutableList.of());
    String plan = RelOptUtil.toString(relNode);
    System.out.println(plan);
}
 
Example 11
Source Project: Bats   File: DefaultSqlHandler.java    License: Apache License 2.0 5 votes vote down vote up
protected void log(final String description, final RelNode node, final Logger logger, Stopwatch watch) {
  if (logger.isDebugEnabled()) {
    final String plan = RelOptUtil.toString(node, SqlExplainLevel.ALL_ATTRIBUTES);
    final String time = watch == null ? "" : String.format(" (%dms)", watch.elapsed(TimeUnit.MILLISECONDS));
    logger.debug(String.format("%s%s:\n%s", description, time, plan));
  }
}
 
Example 12
Source Project: Bats   File: ExplainHandler.java    License: Apache License 2.0 5 votes vote down vote up
public LogicalExplain(RelNode node, SqlExplainLevel level, QueryContext context) {
  this.text = RelOptUtil.toString(node, level);
  DrillImplementor implementor = new DrillImplementor(new DrillParseContext(context.getPlannerSettings()), ResultMode.LOGICAL);
  implementor.go( (DrillRel) node);
  LogicalPlan plan = implementor.getPlan();
  this.json = plan.unparse(context.getLpPersistence());
}
 
Example 13
Source Project: dremio-oss   File: PrelTransformer.java    License: Apache License 2.0 5 votes vote down vote up
public static void log(final String description, final RelNode node, final Logger logger, Stopwatch watch) {
  if (logger.isDebugEnabled()) {
    final String plan = RelOptUtil.toString(node, SqlExplainLevel.ALL_ATTRIBUTES);
    final String time = watch == null ? "" : String.format(" (%dms)", watch.elapsed(TimeUnit.MILLISECONDS));
    logger.debug(String.format("%s%s:\n%s", description, time, plan));
  }
}
 
Example 14
Source Project: calcite   File: PlannerTest.java    License: Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-569">[CALCITE-569]
 * ArrayIndexOutOfBoundsException when deducing collation</a>. */
@Test void testOrderByNonSelectColumn() throws Exception {
  final SchemaPlus schema = Frameworks.createRootSchema(true)
      .add("tpch", new ReflectiveSchema(new TpchSchema()));

  String query = "select t.psPartkey from\n"
      + "(select ps.psPartkey from `tpch`.`partsupp` ps\n"
      + "order by ps.psPartkey, ps.psSupplyCost) t\n"
      + "order by t.psPartkey";

  List<RelTraitDef> traitDefs = new ArrayList<>();
  traitDefs.add(ConventionTraitDef.INSTANCE);
  traitDefs.add(RelCollationTraitDef.INSTANCE);
  final SqlParser.Config parserConfig =
      SqlParser.configBuilder().setLex(Lex.MYSQL).build();
  FrameworkConfig config = Frameworks.newConfigBuilder()
      .parserConfig(parserConfig)
      .defaultSchema(schema)
      .traitDefs(traitDefs)
      .programs(Programs.ofRules(Programs.RULE_SET))
      .build();
  String plan;
  try (Planner p = Frameworks.getPlanner(config)) {
    SqlNode n = p.parse(query);
    n = p.validate(n);
    RelNode r = p.rel(n).project();
    plan = RelOptUtil.toString(r);
    plan = Util.toLinux(plan);
  }
  assertThat(plan,
      equalTo("LogicalSort(sort0=[$0], dir0=[ASC])\n"
      + "  LogicalProject(psPartkey=[$0])\n"
      + "    LogicalTableScan(table=[[tpch, partsupp]])\n"));
}
 
Example 15
@Test void testProjectDigestWithOneTrivialField() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final RelNode rel = builder
      .scan("EMP")
      .project(builder.field("EMPNO"))
      .build();
  String digest = RelOptUtil.toString(rel, SqlExplainLevel.DIGEST_ATTRIBUTES);
  final String expected = ""
      + "LogicalProject(inputs=[0])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(digest, isLinux(expected));
}
 
Example 16
Source Project: calcite   File: CalciteAssert.java    License: Apache License 2.0 5 votes vote down vote up
static Function<RelNode, Void> checkRel(final String expected,
    final AtomicInteger counter) {
  return relNode -> {
    if (counter != null) {
      counter.incrementAndGet();
    }
    String s = RelOptUtil.toString(relNode);
    assertThat(s, containsStringLinux(expected));
    return null;
  };
}
 
Example 17
Source Project: calcite   File: SqlHintsConverterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testUseMergeJoin() {
  final String sql = "select /*+ use_merge_join(emp, dept) */\n"
      + "ename, job, sal, dept.name\n"
      + "from emp join dept on emp.deptno = dept.deptno";
  RelOptPlanner planner = new VolcanoPlanner();
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  Tester tester1 = tester.withDecorrelation(true)
      .withClusterFactory(
          relOptCluster -> RelOptCluster.create(planner, relOptCluster.getRexBuilder()));
  final RelNode rel = tester1.convertSqlToRel(sql).rel;
  RuleSet ruleSet = RuleSets.ofList(
      EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE,
      EnumerableRules.ENUMERABLE_JOIN_RULE,
      EnumerableRules.ENUMERABLE_PROJECT_RULE,
      EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE,
      EnumerableRules.ENUMERABLE_SORT_RULE,
      AbstractConverter.ExpandConversionRule.INSTANCE);
  Program program = Programs.of(ruleSet);
  RelTraitSet toTraits = rel
      .getCluster()
      .traitSet()
      .replace(EnumerableConvention.INSTANCE);

  RelNode relAfter = program.run(planner, rel, toTraits,
      Collections.emptyList(), Collections.emptyList());

  String planAfter = NL + RelOptUtil.toString(relAfter);
  getDiffRepos().assertEquals("planAfter", "${planAfter}", planAfter);
}
 
Example 18
@Override
public void planRelTransform(final PlannerPhase phase, RelOptPlanner planner, final RelNode before, final RelNode after, final long millisTaken) {
  final boolean noTransform = before == after;
  final String planAsString = toStringOrEmpty(after, noTransform || phase.forceVerbose());
  final long millisTakenFinalize = (phase.useMaterializations) ? millisTaken - (findMaterializationMillis + normalizationMillis + substitutionMillis) : millisTaken;
  if (phase.useMaterializations) {
    planPhases.add(PlanPhaseProfile.newBuilder()
      .setPhaseName("Substitution")
      .setDurationMillis(substitutionMillis)
      .setPlan("")
      .build());
  }

  PlanPhaseProfile.Builder b = PlanPhaseProfile.newBuilder()
      .setPhaseName(phase.description)
      .setDurationMillis(millisTakenFinalize)
      .setPlan(planAsString);

  // dump state of volcano planner to troubleshoot costing issues (or long planning issues).
  if (verbose || noTransform) {
    final String dump = getPlanDump(planner);
    if (dump != null) {
      b.setPlannerDump(dump);
    }
    //System.out.println(Thread.currentThread().getName() + ":\n" + dump);
  }

  planPhases.add(b.build());

  if (verbose && phase.useMaterializations && planner instanceof VolcanoPlanner && numSubstitutions > 0) {
    try {
      Map<String, RelNode> bestPlansWithReflections = new CheapestPlanWithReflectionVisitor((VolcanoPlanner) planner).getBestPlansWithReflections();
      for (String reflection : bestPlansWithReflections.keySet()) {
        String plan = RelOptUtil.toString(bestPlansWithReflections.get(reflection), SqlExplainLevel.ALL_ATTRIBUTES);
        LayoutMaterializedViewProfile profile = mapIdToAccelerationProfile.get(reflection);
        if (profile != null) {
          mapIdToAccelerationProfile.put(
            reflection,
            LayoutMaterializedViewProfile.newBuilder(profile)
              .setOptimizedPlanBytes(ByteString.copyFrom(plan.getBytes()))
              .build()
          );
        }
      }
    } catch (Exception e) {
      logger.debug("Failed to find best plans with reflections", e);
    }
  }
}
 
Example 19
Source Project: calcite   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 20
Source Project: calcite   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);
}