/** Unit test for {@link org.eigenbase.rel.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(new ReflectiveSchema(rootSchema, "hr", 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, false, SqlExplainLevel.EXPPLAN_ATTRIBUTES); } }); assertThat( s, is( "AggregateRel(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT()])\n" + " FilterRel(condition=[=($1, 10)])\n" + " TableAccessRel(table=[[hr, emps]])\n")); }
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(TableAccessRelBase 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; } }); }
/** * Unit test for {@link org.eigenbase.rel.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(new ReflectiveSchema(rootSchema, "hr", new JdbcTest.HrSchema())); TableAccessRel table = new TableAccessRel( cluster, relOptSchema.getTableForMember(Arrays.asList("hr", "emps"))); final RexBuilder rexBuilder = cluster.getRexBuilder(); FilterRel filter = new FilterRel( cluster, table, rexBuilder.makeCall( SqlStdOperatorTable.EQUALS, rexBuilder.makeFieldAccess( rexBuilder.makeRangeReference(table.getRowType()), "deptno", true), rexBuilder.makeExactLiteral(BigDecimal.TEN))); final RelJsonWriter writer = new RelJsonWriter(); final RelDataType intType = cluster.getTypeFactory().createSqlType(SqlTypeName.INTEGER); AggregateRel aggregate = new AggregateRel( cluster, filter, BitSets.of(0), ImmutableList.of( new AggregateCall( SqlStdOperatorTable.COUNT, true, ImmutableList.of(1), intType, "c"), new AggregateCall( SqlStdOperatorTable.COUNT, false, ImmutableList.<Integer>of(), intType, "d"))); aggregate.explain(writer); return writer.asString(); } }); assertThat(s, is(XX)); }