Java Code Examples for org.apache.calcite.rel.RelNode#explain()

The following examples show how to use org.apache.calcite.rel.RelNode#explain() . 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: RelXmlWriter.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Generates specific XML (sometimes called 'attribute-oriented XML'). Like
 * this:
 *
 * <blockquote><pre>
 * &lt;Join condition="EMP.DEPTNO = DEPT.DEPTNO"&gt;
 *   &lt;Project expr1="x + y" expr2="42"&gt;
 *   &lt;TableAccess table="SALES.EMPS"&gt;
 * &lt;/Join&gt;
 * </pre></blockquote>
 *
 * @param rel    Relational expression
 * @param values List of term-value pairs
 */
private void explainSpecific(
    RelNode rel,
    List<Pair<String, Object>> values) {
  String tagName = rel.getRelTypeName();
  xmlOutput.beginBeginTag(tagName);
  xmlOutput.attribute("id", rel.getId() + "");

  for (Pair<String, Object> value : values) {
    if (value.right instanceof RelNode) {
      continue;
    }
    xmlOutput.attribute(
        value.left,
        value.right.toString());
  }
  xmlOutput.endBeginTag(tagName);
  spacer.add(2);
  for (RelNode input : rel.getInputs()) {
    input.explain(this);
  }
  spacer.subtract(2);
}
 
Example 2
Source File: RelWriterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testPlusOperator() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final RelNode rel = builder
      .scan("EMP")
      .project(
          builder.call(SqlStdOperatorTable.PLUS,
              builder.field("SAL"),
              builder.literal(10)))
      .build();
  RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
      + "LogicalProject($f0=[+($5, 10)])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example 3
Source File: RelWriterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTrim() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder b = RelBuilder.create(config);
  final RelNode rel =
      b.scan("EMP")
          .project(
              b.alias(
                  b.call(SqlStdOperatorTable.TRIM,
                      b.literal(SqlTrimFunction.Flag.BOTH),
                      b.literal(" "),
                      b.field("ENAME")),
                  "trimmed_ename"))
          .build();

  RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  String relJson = jsonWriter.asString();
  final RelOptSchema schema = getSchema(rel);
  final String s = deserializeAndDumpToTextFormat(schema, relJson);
  final String expected = ""
      + "LogicalProject(trimmed_ename=[TRIM(FLAG(BOTH), ' ', $1)])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example 4
Source File: RelWriterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testCorrelateQuery() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final Holder<RexCorrelVariable> v = Holder.of(null);
  RelNode relNode = builder.scan("EMP")
      .variable(v)
      .scan("DEPT")
      .filter(
          builder.equals(builder.field(0), builder.field(v.get(), "DEPTNO")))
      .correlate(
          JoinRelType.INNER, v.get().id, builder.field(2, 0, "DEPTNO"))
      .build();
  RelJsonWriter jsonWriter = new RelJsonWriter();
  relNode.explain(jsonWriter);
  final String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(relNode), relJson);
  final String expected = ""
      + "LogicalCorrelate(correlation=[$cor0], joinType=[inner], requiredColumns=[{7}])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n"
      + "  LogicalFilter(condition=[=($0, $cor0.DEPTNO)])\n"
      + "    LogicalTableScan(table=[[scott, DEPT]])\n";

  assertThat(s, isLinux(expected));
}
 
Example 5
Source File: RelWriterTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInterval() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  SqlIntervalQualifier sqlIntervalQualifier =
      new SqlIntervalQualifier(TimeUnit.DAY, TimeUnit.DAY, SqlParserPos.ZERO);
  BigDecimal value = new BigDecimal(86400000);
  RexLiteral intervalLiteral = builder.getRexBuilder()
      .makeIntervalLiteral(value, sqlIntervalQualifier);
  final RelNode rel = builder
      .scan("EMP")
      .project(
          builder.call(
              SqlStdOperatorTable.TUMBLE_END,
              builder.field("HIREDATE"),
              intervalLiteral))
      .build();
  RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
      + "LogicalProject($f0=[TUMBLE_END($4, 86400000:INTERVAL DAY)])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example 6
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a relational expression to a string.
 */
public static String toString(final RelNode rel, SqlExplainLevel detailLevel) {
    if (rel == null) {
        return null;
    }
    final StringWriter sw = new StringWriter();
    final RelWriter planWriter = new RelWriterImpl(new PrintWriter(sw), detailLevel, false);
    rel.explain(planWriter);
    return sw.toString();
}
 
Example 7
Source File: RelXmlWriter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Generates generic XML (sometimes called 'element-oriented XML'). Like
 * this:
 *
 * <blockquote>
 * <code>
 * &lt;RelNode id="1" type="Join"&gt;<br>
 * &nbsp;&nbsp;&lt;Property name="condition"&gt;EMP.DEPTNO =
 * DEPT.DEPTNO&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&lt;Inputs&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;RelNode id="2" type="Project"&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property name="expr1"&gt;x +
 * y&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property
 * name="expr2"&gt;45&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;/RelNode&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;RelNode id="3" type="TableAccess"&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property
 * name="table"&gt;SALES.EMP&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;/RelNode&gt;<br>
 * &nbsp;&nbsp;&lt;/Inputs&gt;<br>
 * &lt;/RelNode&gt;</code>
 * </blockquote>
 *
 * @param rel    Relational expression
 * @param values List of term-value pairs
 */
private void explainGeneric(
    RelNode rel,
    List<Pair<String, Object>> values) {
  String relType = rel.getRelTypeName();
  xmlOutput.beginBeginTag("RelNode");
  xmlOutput.attribute("type", relType);

  xmlOutput.endBeginTag("RelNode");

  final List<RelNode> inputs = new ArrayList<>();
  for (Pair<String, Object> pair : values) {
    if (pair.right instanceof RelNode) {
      inputs.add((RelNode) pair.right);
      continue;
    }
    if (pair.right == null) {
      continue;
    }
    xmlOutput.beginBeginTag("Property");
    xmlOutput.attribute("name", pair.left);
    xmlOutput.endBeginTag("Property");
    xmlOutput.cdata(pair.right.toString());
    xmlOutput.endTag("Property");
  }
  xmlOutput.beginTag("Inputs", null);
  spacer.add(2);
  for (RelNode input : inputs) {
    input.explain(this);
  }
  spacer.subtract(2);
  xmlOutput.endTag("Inputs");
  xmlOutput.endTag("RelNode");
}
 
Example 8
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testWriteSortExchangeWithRandomDistribution() {
  final RelNode root = createSortPlan(RelDistributions.RANDOM_DISTRIBUTED);
  final RelJsonWriter writer = new RelJsonWriter();
  root.explain(writer);
  final String json = writer.asString();
  final String s = deserializeAndDumpToTextFormat(getSchema(root), json);
  final String expected =
      "LogicalSortExchange(distribution=[random], collation=[[0]])\n"
          + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example 9
Source File: RelJsonWriter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private List<Object> explainInputs(List<RelNode> inputs) {
  final List<Object> list = jsonBuilder.list();
  for (RelNode input : inputs) {
    String id = relIdMap.get(input);
    if (id == null) {
      input.explain(this);
      id = previousId;
    }
    list.add(id);
  }
  return list;
}
 
Example 10
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a relational expression to a string.
 */
public static String toString(
    final RelNode rel,
    SqlExplainLevel detailLevel) {
  if (rel == null) {
    return null;
  }
  final StringWriter sw = new StringWriter();
  final RelWriter planWriter =
      new RelWriterImpl(
          new PrintWriter(sw), detailLevel, false);
  rel.explain(planWriter);
  return sw.toString();
}
 
Example 11
Source File: SqlToRelConverterExtendedTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static void foo(RelNode rel) {
  // Convert rel tree to JSON.
  final RelJsonWriter writer = new RelJsonWriter();
  rel.explain(writer);
  final String json = writer.asString();

  // Find the schema. If there are no tables in the plan, we won't need one.
  final RelOptSchema[] schemas = {null};
  rel.accept(new RelShuttleImpl() {
    @Override public RelNode visit(TableScan scan) {
      schemas[0] = scan.getTable().getRelOptSchema();
      return super.visit(scan);
    }
  });

  // Convert JSON back to rel tree.
  Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> {
    final RelJsonReader reader = new RelJsonReader(
        cluster,
        schemas[0], rootSchema);
    try {
      RelNode x = reader.read(json);
    } catch (IOException e) {
      throw TestUtil.rethrow(e);
    }
    return null;
  });
}
 
Example 12
Source File: SqlToRelConverterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testExplainAsXml() {
  String sql = "select 1 + 2, 3 from (values (true))";
  final RelNode rel = tester.convertSqlToRel(sql).rel;
  StringWriter sw = new StringWriter();
  PrintWriter pw = new PrintWriter(sw);
  RelXmlWriter planWriter =
      new RelXmlWriter(pw, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  rel.explain(planWriter);
  pw.flush();
  TestUtil.assertEqualsVerbose(
      "<RelNode type=\"LogicalProject\">\n"
          + "\t<Property name=\"EXPR$0\">\n"
          + "\t\t+(1, 2)\n"
          + "\t</Property>\n"
          + "\t<Property name=\"EXPR$1\">\n"
          + "\t\t3\n"
          + "\t</Property>\n"
          + "\t<Inputs>\n"
          + "\t\t<RelNode type=\"LogicalValues\">\n"
          + "\t\t\t<Property name=\"tuples\">\n"
          + "\t\t\t\t[{ true }]\n"
          + "\t\t\t</Property>\n"
          + "\t\t\t<Inputs/>\n"
          + "\t\t</RelNode>\n"
          + "\t</Inputs>\n"
          + "</RelNode>\n",
      Util.toLinux(sw.toString()));
}
 
Example 13
Source File: RelJsonWriter.java    From Bats with Apache License 2.0 5 votes vote down vote up
private List<Object> explainInputs(List<RelNode> inputs) {
  final List<Object> list = jsonBuilder.list();
  for (RelNode input : inputs) {
    String id = relIdMap.get(input);
    if (id == null) {
      input.explain(this);
      id = previousId;
    }
    list.add(id);
  }
  return list;
}
 
Example 14
Source File: RelXmlWriter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Generates generic XML (sometimes called 'element-oriented XML'). Like
 * this:
 *
 * <blockquote>
 * <code>
 * &lt;RelNode id="1" type="Join"&gt;<br>
 * &nbsp;&nbsp;&lt;Property name="condition"&gt;EMP.DEPTNO =
 * DEPT.DEPTNO&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&lt;Inputs&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;RelNode id="2" type="Project"&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property name="expr1"&gt;x +
 * y&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property
 * name="expr2"&gt;45&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;/RelNode&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;RelNode id="3" type="TableAccess"&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;Property
 * name="table"&gt;SALES.EMP&lt;/Property&gt;<br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&lt;/RelNode&gt;<br>
 * &nbsp;&nbsp;&lt;/Inputs&gt;<br>
 * &lt;/RelNode&gt;</code>
 * </blockquote>
 *
 * @param rel    Relational expression
 * @param values List of term-value pairs
 */
private void explainGeneric(
    RelNode rel,
    List<Pair<String, Object>> values) {
  String relType = rel.getRelTypeName();
  xmlOutput.beginBeginTag("RelNode");
  xmlOutput.attribute("type", relType);

  xmlOutput.endBeginTag("RelNode");

  final List<RelNode> inputs = new ArrayList<>();
  for (Pair<String, Object> pair : values) {
    if (pair.right instanceof RelNode) {
      inputs.add((RelNode) pair.right);
      continue;
    }
    if (pair.right == null) {
      continue;
    }
    xmlOutput.beginBeginTag("Property");
    xmlOutput.attribute("name", pair.left);
    xmlOutput.endBeginTag("Property");
    xmlOutput.cdata(pair.right.toString());
    xmlOutput.endTag("Property");
  }
  xmlOutput.beginTag("Inputs", null);
  spacer.add(2);
  for (RelNode input : inputs) {
    input.explain(this);
  }
  spacer.subtract(2);
  xmlOutput.endTag("Inputs");
  xmlOutput.endTag("RelNode");
}
 
Example 15
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAggregateWithAlias() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  // The rel node stands for sql: SELECT max(SAL) as max_sal from EMP group by JOB;
  final RelNode rel = builder
      .scan("EMP")
      .project(
          builder.field("JOB"),
          builder.field("SAL"))
      .aggregate(
          builder.groupKey("JOB"),
          builder.max("max_sal", builder.field("SAL")))
      .project(
          builder.field("max_sal"))
      .build();
  final RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  final String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
      + "LogicalProject(max_sal=[$1])\n"
      + "  LogicalAggregate(group=[{0}], max_sal=[MAX($1)])\n"
      + "    LogicalProject(JOB=[$2], SAL=[$5])\n"
      + "      LogicalTableScan(table=[[scott, EMP]])\n";

  assertThat(s, isLinux(expected));
}
 
Example 16
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testAggregateWithoutAlias() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  // The rel node stands for sql: SELECT max(SAL) from EMP group by JOB;
  final RelNode rel = builder
      .scan("EMP")
      .project(
          builder.field("JOB"),
          builder.field("SAL"))
      .aggregate(
          builder.groupKey("JOB"),
          builder.max(builder.field("SAL")))
      .project(
          builder.field(1))
      .build();
  final RelJsonWriter jsonWriter = new RelJsonWriter();
  rel.explain(jsonWriter);
  final String relJson = jsonWriter.asString();
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
      + "LogicalProject($f1=[$1])\n"
      + "  LogicalAggregate(group=[{0}], agg#0=[MAX($1)])\n"
      + "    LogicalProject(JOB=[$2], SAL=[$5])\n"
      + "      LogicalTableScan(table=[[scott, EMP]])\n";

  assertThat(s, isLinux(expected));
}
 
Example 17
Source File: SubstitutionUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static int hash(RelNode rel) {
  Hasher hasher = new Hasher();
  PrintWriter pw = new PrintWriter(hasher, false);
  rel.explain(new RelWriterImpl(pw, SqlExplainLevel.DIGEST_ATTRIBUTES, false));
  return hasher.hash;
}
 
Example 18
Source File: RelJsonWriter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void explainInputs(List<RelNode> inputs) {
  for (RelNode input : inputs) {
    input.explain(this);
  }
}
 
Example 19
Source File: NumberingRelWriter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void explainInputs(List<RelNode> inputs) {
  for (RelNode input : inputs) {
    input.explain(this);
  }
}
 
Example 20
Source File: RelWriterImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void explainInputs(List<RelNode> inputs) {
  for (RelNode input : inputs) {
    input.explain(this);
  }
}