/** 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;
          }
        });
  }
  @Test
  public void test(final DrillbitContext bitContext) throws Exception {
    final DrillConfig c = DrillConfig.create();

    new NonStrictExpectations() {
      {
        bitContext.getMetrics();
        result = new MetricRegistry();
        bitContext.getAllocator();
        result = new TopLevelAllocator();
        bitContext.getConfig();
        result = c;
        bitContext.getCache();
        result = new LocalCache();
      }
    };

    bitContext.getCache().run();

    StoragePluginRegistry r = new StoragePluginRegistry(bitContext);
    SchemaPlus plus = Frameworks.createRootSchema();
    r.init();
    r.getSchemaFactory().registerSchemas(null, plus);

    printSchema(plus, 0);
  }
Exemple #4
0
 /**
  * Initializes a container then calls user-specified code with a planner.
  *
  * @param action Callback containing user-specified code
  * @return Return value from action
  */
 public static <R> R withPlanner(final PlannerAction<R> action) {
   FrameworkConfig config =
       newConfigBuilder() //
           .defaultSchema(Frameworks.createRootSchema(true))
           .build();
   return withPlanner(action, config);
 }
Exemple #5
0
 private Planner getPlanner(RuleSet... ruleSets) {
   return Frameworks.getPlanner(
       ConnectionConfig.Lex.ORACLE,
       new Function1<SchemaPlus, Schema>() {
         public Schema apply(SchemaPlus parentSchema) {
           return new ReflectiveSchema(parentSchema, "hr", new JdbcTest.HrSchema());
         }
       },
       SqlStdOperatorTable.instance(),
       ruleSets);
 }
 /**
  * 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));
 }
  protected void testSqlPlan(String sqlCommands) throws Exception {
    String[] sqlStrings = sqlCommands.split(";");

    final DistributedCache cache = new LocalCache();
    cache.run();

    final SystemOptionManager opt = new SystemOptionManager(cache);
    opt.init();
    final OptionManager sess = new SessionOptionManager(opt);

    new NonStrictExpectations() {
      {
        dbContext.getMetrics();
        result = new MetricRegistry();
        dbContext.getAllocator();
        result = new TopLevelAllocator();
        dbContext.getConfig();
        result = config;
        dbContext.getOptionManager();
        result = opt;
        dbContext.getCache();
        result = cache;
      }
    };

    final StoragePluginRegistry registry = new StoragePluginRegistry(dbContext);
    registry.init();
    final FunctionImplementationRegistry functionRegistry =
        new FunctionImplementationRegistry(config);
    final SchemaPlus root = Frameworks.createRootSchema(false);
    registry.getSchemaFactory().registerSchemas(new UserSession(null, null, null), root);

    new NonStrictExpectations() {
      {
        context.getNewDefaultSchema();
        result = root;
        context.getStorage();
        result = registry;
        context.getFunctionRegistry();
        result = functionRegistry;
        context.getSession();
        result = new UserSession(null, null, null);
        context.getCurrentEndpoint();
        result = DrillbitEndpoint.getDefaultInstance();
        context.getActiveEndpoints();
        result = ImmutableList.of(DrillbitEndpoint.getDefaultInstance());
        context.getPlannerSettings();
        result = new PlannerSettings(sess);
        context.getOptions();
        result = sess;
        context.getConfig();
        result = config;
        context.getCache();
        result = cache;
      }
    };

    for (String sql : sqlStrings) {
      if (sql.trim().isEmpty()) continue;
      DrillSqlWorker worker = new DrillSqlWorker(context);
      PhysicalPlan p = worker.getPlan(sql);
    }
  }
Exemple #8
0
 public PrepareAction() {
   this.config =
       newConfigBuilder() //
           .defaultSchema(Frameworks.createRootSchema(true))
           .build();
 }