Exemplo n.º 1
0
 public void testClassDecl() {
   final NewExpression newExpression =
       Expressions.new_(
           Object.class,
           Collections.<Expression>emptyList(),
           Arrays.<MemberDeclaration>asList(
               new FieldDeclaration(
                   Modifier.PUBLIC | Modifier.FINAL,
                   new ParameterExpression(String.class, "foo"),
                   Expressions.constant("bar")),
               new ClassDeclaration(
                   Modifier.PUBLIC | Modifier.STATIC,
                   "MyClass",
                   null,
                   Collections.<Type>emptyList(),
                   Arrays.<MemberDeclaration>asList(
                       new FieldDeclaration(
                           0, new ParameterExpression(int.class, "x"), Expressions.constant(0)))),
               new FieldDeclaration(0, new ParameterExpression(int.class, "i"), null)));
   assertEquals(
       "new Object(){\n"
           + "  public final String foo = \"bar\";\n"
           + "  public static class MyClass {\n"
           + "    int x = 0;\n"
           + "  }\n"
           + "  int i;\n"
           + "}",
       Expressions.toString(newExpression));
 }
Exemplo n.º 2
0
 public List<SqlOperator> lookupOperatorOverloads(
     SqlIdentifier opName, SqlFunctionCategory category, SqlSyntax syntax) {
   if (syntax != SqlSyntax.Function) {
     return Collections.emptyList();
   }
   // FIXME: ignoring prefix of opName
   String name = opName.names[opName.names.length - 1];
   List<TableFunction> tableFunctions = rootSchema.getTableFunctions(name);
   if (tableFunctions.isEmpty()) {
     return Collections.emptyList();
   }
   return toOps(name, tableFunctions);
 }
Exemplo n.º 3
0
    public PreparedResult prepareQueryable(Queryable queryable, RelDataType resultType) {
      queryString = null;
      Class runtimeContextClass = Object.class;
      final Argument[] arguments = {new Argument(connectionVariable, runtimeContextClass, null)};
      ClassDeclaration decl = init(arguments);

      final RelOptQuery query = new RelOptQuery(planner);
      final RelOptCluster cluster =
          query.createCluster(env, rexBuilder.getTypeFactory(), rexBuilder);

      RelNode rootRel = new LixToRelTranslator(cluster).translate(queryable);

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

      final RelDataType jdbcType = makeStruct(rexBuilder.getTypeFactory(), resultType);
      fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null);

      // Structured type flattening, view expansion, and plugging in
      // physical storage.
      rootRel = flattenTypes(rootRel, true);

      // Trim unused fields.
      rootRel = trimUnusedFields(rootRel);

      rootRel = optimize(resultType, rootRel);
      containsJava = treeContainsJava(rootRel);

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

      return implement(resultType, rootRel, SqlKind.SELECT, decl, arguments);
    }
Exemplo n.º 4
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);
   }
 }
Exemplo n.º 5
0
 public static Expression translateCondition(
     List<Expression> inputs,
     RexProgram program,
     JavaTypeFactory typeFactory,
     List<Statement> list) {
   List<Expression> x =
       new RexToLixTranslator(program, typeFactory, inputs)
           .translate(list, Collections.singletonList(program.getCondition()));
   assert x.size() == 1;
   return x.get(0);
 }
Exemplo n.º 6
0
  public void testCompile() throws NoSuchMethodException {
    // Creating a parameter for the expression tree.
    ParameterExpression param = Expressions.parameter(String.class);

    // Creating an expression for the method call and specifying its
    // parameter.
    MethodCallExpression methodCall =
        Expressions.call(Integer.class, "valueOf", Collections.<Expression>singletonList(param));

    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    int x =
        Expressions.<Function1<String, Integer>>lambda(
                methodCall, new ParameterExpression[] {param})
            .getFunction()
            .apply("1234");
    assertEquals(1234, x);
  }
Exemplo n.º 7
0
  public void testWrite() {
    assertEquals(
        "1 + 2.0F + 3L + Long.valueOf(4L)",
        Expressions.toString(
            Expressions.add(
                Expressions.add(
                    Expressions.add(Expressions.constant(1), Expressions.constant(2F, Float.TYPE)),
                    Expressions.constant(3L, Long.TYPE)),
                Expressions.constant(4L, Long.class))));

    assertEquals(
        "new java.math.BigDecimal(31415926L, 7)",
        Expressions.toString(Expressions.constant(BigDecimal.valueOf(314159260, 8))));

    // Parentheses needed, to override the left-associativity of +.
    assertEquals(
        "1 + (2 + 3)",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(1),
                Expressions.add(Expressions.constant(2), Expressions.constant(3)))));

    // No parentheses needed; higher precedence of * achieves the desired
    // effect.
    assertEquals(
        "1 + 2 * 3",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(1),
                Expressions.multiply(Expressions.constant(2), Expressions.constant(3)))));

    assertEquals(
        "1 * (2 + 3)",
        Expressions.toString(
            Expressions.multiply(
                Expressions.constant(1),
                Expressions.add(Expressions.constant(2), Expressions.constant(3)))));

    // Parentheses needed, to overcome right-associativity of =.
    assertEquals(
        "(1 = 2) = 3",
        Expressions.toString(
            Expressions.assign(
                Expressions.assign(Expressions.constant(1), Expressions.constant(2)),
                Expressions.constant(3))));

    // Ternary operator.
    assertEquals(
        "1 < 2 ? (3 < 4 ? 5 : 6) : 7 < 8 ? 9 : 10",
        Expressions.toString(
            Expressions.condition(
                Expressions.lessThan(Expressions.constant(1), Expressions.constant(2)),
                Expressions.condition(
                    Expressions.lessThan(Expressions.constant(3), Expressions.constant(4)),
                    Expressions.constant(5),
                    Expressions.constant(6)),
                Expressions.condition(
                    Expressions.lessThan(Expressions.constant(7), Expressions.constant(8)),
                    Expressions.constant(9),
                    Expressions.constant(10)))));

    assertEquals(
        "0 + (double) (2 + 3)",
        Expressions.toString(
            Expressions.add(
                Expressions.constant(0),
                Expressions.convert_(
                    Expressions.add(Expressions.constant(2), Expressions.constant(3)),
                    Double.TYPE))));

    assertEquals(
        "a.empno",
        Expressions.toString(
            Expressions.field(Expressions.parameter(Linq4jTest.Employee.class, "a"), "empno")));

    assertEquals(
        "java.util.Collections.EMPTY_LIST",
        Expressions.toString(Expressions.field(null, Collections.class, "EMPTY_LIST")));

    final ParameterExpression paramX = Expressions.parameter(String.class, "x");
    assertEquals(
        "new net.hydromatic.linq4j.function.Function1() {\n"
            + "  public int apply(String x) {\n"
            + "    return x.length();\n"
            + "  }\n"
            + "  public Object apply(Object x) {\n"
            + "    return apply(\n"
            + "      (String) x);\n"
            + "  }\n"
            + "}\n",
        Expressions.toString(
            Expressions.lambda(
                Function1.class,
                Expressions.call(paramX, "length", Collections.<Expression>emptyList()),
                Arrays.asList(paramX))));

    assertEquals(
        "new String[] {\n" + "  \"foo\",\n" + "  null,\n" + "  \"bar\\\"baz\"}",
        Expressions.toString(
            Expressions.newArrayInit(
                String.class,
                Arrays.<Expression>asList(
                    Expressions.constant("foo"),
                    Expressions.constant(null),
                    Expressions.constant("bar\"baz")))));

    assertEquals(
        "(int) ((String) (Object) \"foo\").length()",
        Expressions.toString(
            Expressions.convert_(
                Expressions.call(
                    Expressions.convert_(
                        Expressions.convert_(Expressions.constant("foo"), Object.class),
                        String.class),
                    "length",
                    Collections.<Expression>emptyList()),
                Integer.TYPE)));

    // resolving a static method
    assertEquals(
        "Integer.valueOf(\"0123\")",
        Expressions.toString(
            Expressions.call(
                Integer.class,
                "valueOf",
                Collections.<Expression>singletonList(Expressions.constant("0123")))));

    // precedence of not and instanceof
    assertEquals(
        "!(o instanceof String)",
        Expressions.toString(
            Expressions.not(
                Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class))));

    // not not
    assertEquals(
        "!!(o instanceof String)",
        Expressions.toString(
            Expressions.not(
                Expressions.not(
                    Expressions.typeIs(Expressions.parameter(Object.class, "o"), String.class)))));
  }
Exemplo n.º 8
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));
 }
Exemplo n.º 9
0
  <T> PrepareResult<T> prepare_(
      Context context, String sql, Queryable<T> queryable, Type elementType) {
    final JavaTypeFactory typeFactory = context.getTypeFactory();
    OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory);
    final OptiqPreparingStmt preparingStmt =
        new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema());
    preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE);

    final RelDataType x;
    final PreparedResult preparedResult;
    if (sql != null) {
      assert queryable == null;
      SqlParser parser = new SqlParser(sql);
      SqlNode sqlNode;
      try {
        sqlNode = parser.parseQuery();
      } catch (SqlParseException e) {
        throw new RuntimeException("parse failed", e);
      }
      final Schema rootSchema = context.getRootSchema();
      SqlValidator validator =
          new SqlValidatorImpl(
              new ChainedSqlOperatorTable(
                  Arrays.<SqlOperatorTable>asList(
                      SqlStdOperatorTable.instance(),
                      new MySqlOperatorTable(rootSchema, typeFactory))),
              catalogReader,
              typeFactory,
              SqlConformance.Default) {};
      preparedResult = preparingStmt.prepareSql(sqlNode, Object.class, validator, true);
      x = validator.getValidatedNodeType(sqlNode);
    } else {
      assert queryable != null;
      x = context.getTypeFactory().createType(elementType);
      preparedResult = preparingStmt.prepareQueryable(queryable, x);
    }

    // TODO: parameters
    final List<Parameter> parameters = Collections.emptyList();
    // TODO: column meta data
    final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>();
    RelDataType jdbcType = makeStruct(typeFactory, x);
    for (RelDataTypeField field : jdbcType.getFields()) {
      RelDataType type = field.getType();
      SqlTypeName sqlTypeName = type.getSqlTypeName();
      columns.add(
          new ColumnMetaData(
              columns.size(),
              false,
              true,
              false,
              false,
              type.isNullable() ? 1 : 0,
              true,
              0,
              field.getName(),
              field.getName(),
              null,
              sqlTypeName.allowsPrec() && false ? type.getPrecision() : -1,
              sqlTypeName.allowsScale() ? type.getScale() : -1,
              null,
              null,
              sqlTypeName.getJdbcOrdinal(),
              sqlTypeName.getName(),
              true,
              false,
              false,
              null));
    }
    return new PrepareResult<T>(sql, parameters, columns, (Enumerable<T>) preparedResult.execute());
  }
Exemplo n.º 10
0
 public List<RelCollation> getCollationList() {
   return Collections.emptyList();
 }