Exemple #1
0
 public void testWriteWhile() {
   DeclarationExpression xDecl, yDecl;
   Node node =
       Expressions.block(
           xDecl = Expressions.declare(0, "x", Expressions.constant(10)),
           yDecl = Expressions.declare(0, "y", Expressions.constant(0)),
           Expressions.while_(
               Expressions.lessThan(xDecl.parameter, Expressions.constant(5)),
               Expressions.statement(Expressions.preIncrementAssign(yDecl.parameter))));
   assertEquals(
       "{\n"
           + "  int x = 10;\n"
           + "  int y = 0;\n"
           + "  while (x < 5) {\n"
           + "    ++y;\n"
           + "  }\n"
           + "}\n",
       Expressions.toString(node));
 }
 /**
  * Gets the expression for an input and counts it.
  *
  * @param index Input ordinal
  * @return Expression to which an input should be translated
  */
 private Expression getInput(int index) {
   Slot slot = inputSlots.get(index);
   if (list == null) {
     slot.count++;
   } else {
     if (slot.count > 1 && slot.parameterExpression == null) {
       slot.parameterExpression = Expressions.parameter(slot.expression.type, "current" + index);
       list.add(Expressions.declare(Modifier.FINAL, slot.parameterExpression, slot.expression));
     }
   }
   return slot.parameterExpression != null ? slot.parameterExpression : slot.expression;
 }
Exemple #3
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);
        }
      };
    }
 private Expression translate(RexNode expr) {
   Slot slot = map.get(expr);
   if (slot == null) {
     Expression expression = translate0(expr);
     assert expression != null;
     final ParameterExpression parameter;
     if (!inlineRexSet.contains(expr) && !(expr instanceof RexLocalRef)) {
       parameter = Expressions.parameter(expression.getType(), "v" + map.size());
     } else {
       parameter = null;
     }
     slot = new Slot(parameter, expression);
     if (parameter != null && list != null) {
       list.add(Expressions.declare(Modifier.FINAL, slot.parameterExpression, slot.expression));
     }
     map.put(expr, slot);
   }
   slot.count++;
   return slot.parameterExpression != null ? slot.parameterExpression : slot.expression;
 }
Exemple #5
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));
 }