Search in sources :

Example 1 with RelJsonReader

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

the class RelWriterTest method testReader.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}.
 */
@Test
public void testReader() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema);
            RelNode node;
            try {
                node = reader.read(XX);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
        }
    });
    assertThat(s, isLinux("LogicalAggregate(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT()])\n" + "  LogicalFilter(condition=[=($1, 10)])\n" + "    LogicalTableScan(table=[[hr, emps]])\n"));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) IOException(java.io.IOException) RelNode(org.apache.calcite.rel.RelNode) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RelJsonReader(org.apache.calcite.rel.externalize.RelJsonReader) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Example 2 with RelJsonReader

use of org.apache.calcite.rel.externalize.RelJsonReader 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)

Aggregations

IOException (java.io.IOException)2 RelNode (org.apache.calcite.rel.RelNode)2 RelJsonReader (org.apache.calcite.rel.externalize.RelJsonReader)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 Frameworks (org.apache.calcite.tools.Frameworks)2 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptSchema (org.apache.calcite.plan.RelOptSchema)1 RelShuttleImpl (org.apache.calcite.rel.RelShuttleImpl)1 TableScan (org.apache.calcite.rel.core.TableScan)1 RelJsonWriter (org.apache.calcite.rel.externalize.RelJsonWriter)1 JdbcTest (org.apache.calcite.test.JdbcTest)1 Test (org.junit.Test)1