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();
}
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;
}
});
}
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));
}
Aggregations