Search in sources :

Example 1 with RelJsonWriter

use of org.apache.calcite.rel.externalize.RelJsonWriter in project calcite by apache.

the class RelOptUtil method dumpPlan.

/**
 * Dumps a plan as a string.
 *
 * @param header      Header to print before the plan. Ignored if the format
 *                    is XML
 * @param rel         Relational expression to explain
 * @param format      Output format
 * @param detailLevel Detail level
 * @return Plan
 */
public static String dumpPlan(String header, RelNode rel, SqlExplainFormat format, SqlExplainLevel detailLevel) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    if (!header.equals("")) {
        pw.println(header);
    }
    RelWriter planWriter;
    switch(format) {
        case XML:
            planWriter = new RelXmlWriter(pw, detailLevel);
            break;
        case JSON:
            planWriter = new RelJsonWriter();
            rel.explain(planWriter);
            return ((RelJsonWriter) planWriter).asString();
        default:
            planWriter = new RelWriterImpl(pw, detailLevel, false);
    }
    rel.explain(planWriter);
    pw.flush();
    return sw.toString();
}
Also used : RelXmlWriter(org.apache.calcite.rel.externalize.RelXmlWriter) StringWriter(java.io.StringWriter) RelWriterImpl(org.apache.calcite.rel.externalize.RelWriterImpl) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) RelWriter(org.apache.calcite.rel.RelWriter) PrintWriter(java.io.PrintWriter)

Example 2 with RelJsonWriter

use of org.apache.calcite.rel.externalize.RelJsonWriter in project calcite by apache.

the class SqlToRelConverterExtendedTest method foo.

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(new Frameworks.PlannerAction<Object>() {

        public Object apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            final RelJsonReader reader = new RelJsonReader(cluster, schemas[0], rootSchema);
            try {
                RelNode x = reader.read(json);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) TableScan(org.apache.calcite.rel.core.TableScan) RelOptSchema(org.apache.calcite.plan.RelOptSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelShuttleImpl(org.apache.calcite.rel.RelShuttleImpl) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) IOException(java.io.IOException) RelNode(org.apache.calcite.rel.RelNode) Frameworks(org.apache.calcite.tools.Frameworks) RelJsonReader(org.apache.calcite.rel.externalize.RelJsonReader)

Example 3 with RelJsonWriter

use of org.apache.calcite.rel.externalize.RelJsonWriter in project calcite by apache.

the class RelWriterTest method testWriter.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonWriter} on
 * a simple tree of relational expressions, consisting of a table, a filter
 * and an aggregate node.
 */
@Test
public void testWriter() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            LogicalTableScan scan = LogicalTableScan.create(cluster, relOptSchema.getTableForMember(Arrays.asList("hr", "emps")));
            final RexBuilder rexBuilder = cluster.getRexBuilder();
            LogicalFilter filter = LogicalFilter.create(scan, rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, rexBuilder.makeFieldAccess(rexBuilder.makeRangeReference(scan), "deptno", true), rexBuilder.makeExactLiteral(BigDecimal.TEN)));
            final RelJsonWriter writer = new RelJsonWriter();
            final RelDataType bigIntType = cluster.getTypeFactory().createSqlType(SqlTypeName.BIGINT);
            LogicalAggregate aggregate = LogicalAggregate.create(filter, ImmutableBitSet.of(0), null, ImmutableList.of(AggregateCall.create(SqlStdOperatorTable.COUNT, true, false, ImmutableList.of(1), -1, bigIntType, "c"), AggregateCall.create(SqlStdOperatorTable.COUNT, false, false, ImmutableList.<Integer>of(), -1, bigIntType, "d")));
            aggregate.explain(writer);
            return writer.asString();
        }
    });
    assertThat(s, is(XX));
}
Also used : LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) RelJsonWriter(org.apache.calcite.rel.externalize.RelJsonWriter) LogicalTableScan(org.apache.calcite.rel.logical.LogicalTableScan) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RexBuilder(org.apache.calcite.rex.RexBuilder) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Aggregations

RelJsonWriter (org.apache.calcite.rel.externalize.RelJsonWriter)3 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 Frameworks (org.apache.calcite.tools.Frameworks)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptSchema (org.apache.calcite.plan.RelOptSchema)1 RelNode (org.apache.calcite.rel.RelNode)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1 RelWriter (org.apache.calcite.rel.RelWriter)1 TableScan (org.apache.calcite.rel.core.TableScan)1 RelJsonReader (org.apache.calcite.rel.externalize.RelJsonReader)1 RelWriterImpl (org.apache.calcite.rel.externalize.RelWriterImpl)1 RelXmlWriter (org.apache.calcite.rel.externalize.RelXmlWriter)1 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)1 LogicalFilter (org.apache.calcite.rel.logical.LogicalFilter)1 LogicalTableScan (org.apache.calcite.rel.logical.LogicalTableScan)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1