예제 #1
0
 private static List<Expression> simpleList(BlockExpression expression) {
   Expression simple = Blocks.simple(expression);
   if (simple instanceof NewExpression) {
     NewExpression newExpression = (NewExpression) simple;
     return newExpression.arguments;
   } else {
     return Collections.singletonList(simple);
   }
 }
예제 #2
0
    @Override
    protected PreparedExecution implement(
        RelDataType rowType,
        RelNode rootRel,
        SqlKind sqlKind,
        ClassDeclaration decl,
        Argument[] args) {
      RelDataType resultType = rootRel.getRowType();
      boolean isDml = sqlKind.belongsTo(SqlKind.DML);
      javaCompiler = createCompiler();
      EnumerableRelImplementor relImplementor =
          getRelImplementor(rootRel.getCluster().getRexBuilder());
      BlockExpression expr = relImplementor.implementRoot((EnumerableRel) rootRel);
      ParameterExpression root0 = Expressions.parameter(DataContext.class, "root0");
      String s =
          Expressions.toString(
              Blocks.create(
                  Expressions.declare(
                      Modifier.FINAL, (ParameterExpression) schema.getExpression(), root0),
                  expr),
              false);

      final Executable executable;
      try {
        executable =
            (Executable)
                ExpressionEvaluator.createFastScriptEvaluator(
                    s, Executable.class, new String[] {root0.name});
      } catch (Exception e) {
        throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n" + s, e);
      }

      if (timingTracer != null) {
        timingTracer.traceTime("end codegen");
      }

      if (timingTracer != null) {
        timingTracer.traceTime("end compilation");
      }

      return new PreparedExecution(
          null, rootRel, resultType, isDml, mapTableModOp(isDml, sqlKind), null) {
        public Object execute() {
          return executable.execute(schema);
        }
      };
    }
예제 #3
0
 public void testWriteAnonymousClass() {
   // final List<String> baz = Arrays.asList("foo", "bar");
   // new AbstractList<String>() {
   //     public int size() {
   //         return baz.size();
   //     }
   //     public String get(int index) {
   //         return ((String) baz.get(index)).toUpperCase();
   //     }
   // }
   final ParameterExpression bazParameter =
       Expressions.parameter(Types.of(List.class, String.class), "baz");
   final ParameterExpression indexParameter = Expressions.parameter(Integer.TYPE, "index");
   BlockExpression e =
       Expressions.block(
           Expressions.declare(
               Modifier.FINAL,
               bazParameter,
               Expressions.call(
                   Arrays.class,
                   "asList",
                   Arrays.<Expression>asList(
                       Expressions.constant("foo"), Expressions.constant("bar")))),
           Expressions.statement(
               Expressions.new_(
                   Types.of(AbstractList.class, String.class),
                   Collections.<Expression>emptyList(),
                   Arrays.<MemberDeclaration>asList(
                       Expressions.fieldDecl(
                           Modifier.PUBLIC | Modifier.FINAL,
                           Expressions.parameter(String.class, "qux"),
                           Expressions.constant("xyzzy")),
                       Expressions.methodDecl(
                           Modifier.PUBLIC,
                           Integer.TYPE,
                           "size",
                           Collections.<ParameterExpression>emptyList(),
                           Blocks.toFunctionBlock(
                               Expressions.call(
                                   bazParameter, "size", Collections.<Expression>emptyList()))),
                       Expressions.methodDecl(
                           Modifier.PUBLIC,
                           String.class,
                           "get",
                           Arrays.asList(indexParameter),
                           Blocks.toFunctionBlock(
                               Expressions.call(
                                   Expressions.convert_(
                                       Expressions.call(
                                           bazParameter,
                                           "get",
                                           Arrays.<Expression>asList(indexParameter)),
                                       String.class),
                                   "toUpperCase",
                                   Collections.<Expression>emptyList())))))));
   assertEquals(
       "{\n"
           + "  final java.util.List<String> baz = java.util.Arrays.asList(\"foo\", \"bar\");\n"
           + "  new java.util.AbstractList<String>(){\n"
           + "    public final String qux = \"xyzzy\";\n"
           + "    public int size() {\n"
           + "      return baz.size();\n"
           + "    }\n"
           + "\n"
           + "    public String get(int index) {\n"
           + "      return ((String) baz.get(index)).toUpperCase();\n"
           + "    }\n"
           + "\n"
           + "  };\n"
           + "}\n",
       Expressions.toString(e));
 }
예제 #4
0
 public RexNode toRex(BlockExpression expression) {
   return toRex(Blocks.simple(expression));
 }