org.apache.calcite.linq4j.tree.Types Java Examples

The following examples show how to use org.apache.calcite.linq4j.tree.Types. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReflectiveSchemaTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testQueryProviderSingleColumn() throws Exception {
  Connection connection = CalciteAssert
      .that(CalciteAssert.Config.REGULAR).connect();
  QueryProvider queryProvider = connection.unwrap(QueryProvider.class);
  ParameterExpression e = Expressions.parameter(Employee.class, "e");

  // "Enumerable<T> asEnumerable(final T[] ts)"
  List<Integer> list =
      queryProvider.createQuery(
          Expressions.call(
              Expressions.call(
                  Types.of(Enumerable.class, Employee.class),
                  null,
                  LINQ4J_AS_ENUMERABLE_METHOD,
                  Expressions.constant(new JdbcTest.HrSchema().emps)),
              "asQueryable"),
          Employee.class)
          .select(
              Expressions.<Function1<Employee, Integer>>lambda(
                  Expressions.field(e, "empid"),
                  e))
          .toList();
  assertEquals(Arrays.asList(100, 200, 150, 110), list);
}
 
Example #2
Source File: EnumUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Match an argument expression to method parameter type with best effort
 * @param argument Argument Expression
 * @param parameter Parameter type
 * @return Converted argument expression that matches the parameter type.
 *         Returns null if it is impossible to match.
 */
private static Expression matchMethodParameterType(
    Expression argument, Class parameter) {
  Type argumentType = argument.getType();
  if (Types.isAssignableFrom(parameter, argumentType)) {
    return argument;
  }
  // Object.class is not assignable from primitive types,
  // but the method with Object parameters can accept primitive types.
  // E.g., "array(Object... args)" in SqlFunctions
  if (parameter == Object.class
      && Primitive.of(argumentType) != null) {
    return argument;
  }
  // Convert argument with Object.class type to parameter explicitly
  if (argumentType == Object.class
      && Primitive.of(argumentType) == null) {
    return convert(argument, parameter);
  }
  // assignable types that can be accepted with explicit conversion
  if (parameter == BigDecimal.class
      && Primitive.ofBoxOr(argumentType) != null) {
    return convert(argument, parameter);
  }
  return null;
}
 
Example #3
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Expression toEnumerable(Expression expression) {
  final Type type = expression.getType();
  if (Types.isArray(type)) {
    if (Types.toClass(type).getComponentType().isPrimitive()) {
      expression =
          Expressions.call(BuiltInMethod.AS_LIST.method, expression);
    }
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE.method, expression);
  } else if (Types.isAssignableFrom(Iterable.class, type)
      && !Types.isAssignableFrom(Enumerable.class, type)) {
    return Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method,
        expression);
  } else if (Types.isAssignableFrom(Queryable.class, type)) {
    // Queryable extends Enumerable, but it's too "clever", so we call
    // Queryable.asEnumerable so that operations such as take(int) will be
    // evaluated directly.
    return Expressions.call(expression,
        BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method);
  }
  return expression;
}
 
Example #4
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicMethodCall() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestClass.class,
                              "deterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestClass_dete33e8af1c;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestClass_dete33e8af1c = org.apache.calcite.linq4j.test.DeterministicTest.TestClass.deterministic(1);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #5
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testNonDeterministicMethodCall() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestClass.class,
                              "nonDeterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return org.apache.calcite.linq4j.test.DeterministicTest.TestClass.nonDeterministic(1);\n"
          + "      }\n"
          + "\n"
          + "    };\n"
          + "}\n"));
}
 
Example #6
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicClassDefaultMethod() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestDeterministicClass.class,
                              "deterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestDeterminis9de610da;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestDeterminis9de610da = org.apache.calcite.linq4j.test.DeterministicTest.TestDeterministicClass.deterministic(1);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #7
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicClassNonDeterministicMethod() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestDeterministicClass.class,
                              "nonDeterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return org.apache.calcite.linq4j.test.DeterministicTest.TestDeterministicClass.nonDeterministic(1);\n"
          + "      }\n"
          + "\n"
          + "    };\n"
          + "}\n"));
}
 
Example #8
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns the type of a field.
 *
 * <p>Takes into account {@link org.apache.calcite.adapter.java.Array}
 * annotations if present.
 */
private Type fieldType(Field field) {
  final Class<?> klass = field.getType();
  final org.apache.calcite.adapter.java.Array array =
      field.getAnnotation(org.apache.calcite.adapter.java.Array.class);
  if (array != null) {
    return new Types.ArrayType(array.component(), array.componentIsNullable(),
        array.maximumCardinality());
  }
  final org.apache.calcite.adapter.java.Map map =
      field.getAnnotation(org.apache.calcite.adapter.java.Map.class);
  if (map != null) {
    return new Types.MapType(map.key(), map.keyIsNullable(), map.value(),
        map.valueIsNullable());
  }
  return klass;
}
 
Example #9
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SyntheticRecordType register(
    final SyntheticRecordType syntheticType) {
  final List<Pair<Type, Boolean>> key =
      new AbstractList<Pair<Type, Boolean>>() {
        public Pair<Type, Boolean> get(int index) {
          final Types.RecordField field =
              syntheticType.getRecordFields().get(index);
          return Pair.of(field.getType(), field.nullable());
        }

        public int size() {
          return syntheticType.getRecordFields().size();
        }
      };
  SyntheticRecordType syntheticType2 = syntheticTypes.get(key);
  if (syntheticType2 == null) {
    syntheticTypes.put(key, syntheticType);
    return syntheticType;
  } else {
    return syntheticType2;
  }
}
 
Example #10
Source File: UdfTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public CallImplementor getImplementor() {
  return (translator, call, nullAs) -> {
    Method lookupMethod =
        Types.lookupMethod(Smalls.AllTypesFunction.class,
            "arrayAppendFun", List.class, Integer.class);
    return Expressions.call(lookupMethod,
        translator.translateList(call.getOperands(), nullAs));
  };
}
 
Example #11
Source File: ReflectiveSchemaTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Object get(ResultSet input) throws SQLException {
  final int type = input.getMetaData().getColumnType(1);
  switch (type) {
  case java.sql.Types.BOOLEAN:
    return input.getBoolean(1);
  case java.sql.Types.TINYINT:
    return input.getByte(1);
  case java.sql.Types.SMALLINT:
    return input.getShort(1);
  case java.sql.Types.INTEGER:
    return input.getInt(1);
  case java.sql.Types.BIGINT:
    return input.getLong(1);
  case java.sql.Types.REAL:
    return input.getFloat(1);
  case java.sql.Types.DOUBLE:
    return input.getDouble(1);
  case java.sql.Types.CHAR:
  case java.sql.Types.VARCHAR:
    return input.getString(1);
  case java.sql.Types.DATE:
    return input.getDate(1);
  case java.sql.Types.TIME:
    return input.getTime(1);
  case java.sql.Types.TIMESTAMP:
    return input.getTimestamp(1);
  case java.sql.Types.DECIMAL:
    return input.getBigDecimal(1);
  default:
    throw new AssertionError(type);
  }
}
 
Example #12
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * The powerful version of {@code org.apache.calcite.linq4j.tree.Expressions#call(
 * Type, String, Iterable<? extends Expression>)}. Try best effort to convert the
 * accepted arguments to match parameter type.
 *
 * @param clazz Class against which method is invoked
 * @param methodName Name of method
 * @param arguments argument expressions
 * @param targetExpression target expression
 *
 * @return MethodCallExpression that call the given name method
 * @throws RuntimeException if no suitable method found
 */
public static MethodCallExpression call(Class clazz, String methodName,
     List<? extends Expression> arguments, Expression targetExpression) {
  Class[] argumentTypes = Types.toClassArray(arguments);
  try {
    Method candidate = clazz.getMethod(methodName, argumentTypes);
    return Expressions.call(targetExpression, candidate, arguments);
  } catch (NoSuchMethodException e) {
    for (Method method : clazz.getMethods()) {
      if (method.getName().equals(methodName)) {
        final boolean varArgs = method.isVarArgs();
        final Class[] parameterTypes = method.getParameterTypes();
        if (Types.allAssignable(varArgs, parameterTypes, argumentTypes)) {
          return Expressions.call(targetExpression, method, arguments);
        }
        // fall through
        final List<? extends Expression> typeMatchedArguments =
            matchMethodParameterTypes(varArgs, parameterTypes, arguments);
        if (typeMatchedArguments != null) {
          return Expressions.call(targetExpression, method, typeMatchedArguments);
        }
      }
    }
    throw new RuntimeException("while resolving method '" + methodName
        + Arrays.toString(argumentTypes) + "' in class " + clazz, e);
  }
}
 
Example #13
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Expression fromInternal(Expression operand,
    Type fromType, Type targetType) {
  if (operand == ConstantUntypedNull.INSTANCE) {
    return operand;
  }
  if (!(operand.getType() instanceof Class)) {
    return operand;
  }
  if (Types.isAssignableFrom(targetType, fromType)) {
    return operand;
  }
  if (targetType == java.sql.Date.class) {
    // E.g. from "int" or "Integer" to "java.sql.Date",
    // generate "SqlFunctions.internalToDate".
    if (isA(fromType, Primitive.INT)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_DATE.method, operand);
    }
  } else if (targetType == java.sql.Time.class) {
    // E.g. from "int" or "Integer" to "java.sql.Time",
    // generate "SqlFunctions.internalToTime".
    if (isA(fromType, Primitive.INT)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_TIME.method, operand);
    }
  } else if (targetType == java.sql.Timestamp.class) {
    // E.g. from "long" or "Long" to "java.sql.Timestamp",
    // generate "SqlFunctions.internalToTimestamp".
    if (isA(fromType, Primitive.LONG)) {
      return Expressions.call(BuiltInMethod.INTERNAL_TO_TIMESTAMP.method, operand);
    }
  }
  if (Primitive.is(operand.type)
      && Primitive.isBox(targetType)) {
    // E.g. operand is "int", target is "Long", generate "(long) operand".
    return Expressions.convert_(operand,
        Primitive.ofBox(targetType).primitiveClass);
  }
  return operand;
}
 
Example #14
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
static PhysType of(
    final JavaTypeFactory typeFactory,
    Type javaRowClass) {
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  if (javaRowClass instanceof Types.RecordType) {
    final Types.RecordType recordType = (Types.RecordType) javaRowClass;
    for (Types.RecordField field : recordType.getRecordFields()) {
      builder.add(field.getName(), typeFactory.createType(field.getType()));
    }
  }
  RelDataType rowType = builder.build();
  // Do not optimize if there are 0 or 1 fields.
  return new PhysTypeImpl(typeFactory, rowType, javaRowClass,
      JavaRowFormat.CUSTOM);
}
 
Example #15
Source File: EnumerableRelImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Void visit(NewArrayExpression newArrayExpression) {
  Type type = newArrayExpression.type;
  for (;;) {
    final Type componentType = Types.getComponentType(type);
    if (componentType == null) {
      break;
    }
    type = componentType;
  }
  types.add(type);
  return super.visit(newArrayExpression);
}
 
Example #16
Source File: EnumerableAggregateBase.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void declareParentAccumulator(List<Expression> initExpressions,
    BlockBuilder initBlock, PhysType accPhysType) {
  if (accPhysType.getJavaRowType()
      instanceof JavaTypeFactoryImpl.SyntheticRecordType) {
    // We have to initialize the SyntheticRecordType instance this way, to
    // avoid using a class constructor with too many parameters.
    final JavaTypeFactoryImpl.SyntheticRecordType synType =
        (JavaTypeFactoryImpl.SyntheticRecordType)
            accPhysType.getJavaRowType();
    final ParameterExpression record0_ =
        Expressions.parameter(accPhysType.getJavaRowType(), "record0");
    initBlock.add(Expressions.declare(0, record0_, null));
    initBlock.add(
        Expressions.statement(
            Expressions.assign(record0_,
                Expressions.new_(accPhysType.getJavaRowType()))));
    List<Types.RecordField> fieldList = synType.getRecordFields();
    for (int i = 0; i < initExpressions.size(); i++) {
      Expression right = initExpressions.get(i);
      initBlock.add(
          Expressions.statement(
              Expressions.assign(
                  Expressions.field(record0_, fieldList.get(i)), right)));
    }
    initBlock.add(record0_);
  } else {
    initBlock.add(accPhysType.record(initExpressions));
  }
}
 
Example #17
Source File: DeterministicTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testBigIntegerValueOf() {
  // instanceof is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L)),
                          "add",
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L))))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_add_java_math_BigInteger_valued8d57d69;\n"
          + "      }\n"
          + "\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_ = java.math.BigInteger"
          + ".valueOf(42L);\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_add_java_math_BigInteger_valued8d57d69 = $L4J$C$java_math_BigInteger_valueOf_42L_.add($L4J$C$java_math_BigInteger_valueOf_42L_);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #18
Source File: DeterministicTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testStaticField() {
  // instanceof is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(
                          Expressions.field(null, BigInteger.class, "ONE"),
                          "add",
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L))))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return "
          + "$L4J$C$java_math_BigInteger_ONE_add_java_math_BigInteger_valueOf_42L_;\n"
          + "      }\n"
          + "\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_ = java.math.BigInteger"
          + ".valueOf(42L);\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_ONE_add_java_math_BigInteger_valueOf_42L_ = java.math.BigInteger.ONE.add($L4J$C$java_math_BigInteger_valueOf_42L_);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #19
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelDataType createType(Type type) {
  if (type instanceof RelDataType) {
    return (RelDataType) type;
  }
  if (type instanceof SyntheticRecordType) {
    final SyntheticRecordType syntheticRecordType =
        (SyntheticRecordType) type;
    return syntheticRecordType.relType;
  }
  if (type instanceof Types.ArrayType) {
    final Types.ArrayType arrayType = (Types.ArrayType) type;
    final RelDataType componentRelType =
        createType(arrayType.getComponentType());
    return createArrayType(
        createTypeWithNullability(componentRelType,
            arrayType.componentIsNullable()), arrayType.maximumCardinality());
  }
  if (type instanceof Types.MapType) {
    final Types.MapType mapType = (Types.MapType) type;
    final RelDataType keyRelType = createType(mapType.getKeyType());
    final RelDataType valueRelType = createType(mapType.getValueType());
    return createMapType(
        createTypeWithNullability(keyRelType, mapType.keyIsNullable()),
        createTypeWithNullability(valueRelType, mapType.valueIsNullable()));
  }
  if (!(type instanceof Class)) {
    throw new UnsupportedOperationException("TODO: implement " + type);
  }
  final Class clazz = (Class) type;
  switch (Primitive.flavor(clazz)) {
  case PRIMITIVE:
    return createJavaType(clazz);
  case BOX:
    return createJavaType(Primitive.ofBox(clazz).boxClass);
  }
  if (JavaToSqlTypeConversionRules.instance().lookup(clazz) != null) {
    return createJavaType(clazz);
  } else if (clazz.isArray()) {
    return createMultisetType(
        createType(clazz.getComponentType()), -1);
  } else if (List.class.isAssignableFrom(clazz)) {
    return createArrayType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true), -1);
  } else if (Map.class.isAssignableFrom(clazz)) {
    return createMapType(
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true),
        createTypeWithNullability(createSqlType(SqlTypeName.ANY), true));
  } else {
    return createStructType(clazz);
  }
}
 
Example #20
Source File: JavaTypeFactoryImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public List<Types.RecordField> getRecordFields() {
  return fields;
}
 
Example #21
Source File: LixToRelTranslator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode translate(Expression expression) {
  if (expression instanceof MethodCallExpression) {
    final MethodCallExpression call = (MethodCallExpression) expression;
    BuiltInMethod method = BuiltInMethod.MAP.get(call.method);
    if (method == null) {
      throw new UnsupportedOperationException(
          "unknown method " + call.method);
    }
    RelNode input;
    switch (method) {
    case SELECT:
      input = translate(call.targetExpression);
      return LogicalProject.create(input,
          ImmutableList.of(),
          toRex(input, (FunctionExpression) call.expressions.get(0)),
          (List<String>) null);

    case WHERE:
      input = translate(call.targetExpression);
      return LogicalFilter.create(input,
          toRex((FunctionExpression) call.expressions.get(0), input));

    case AS_QUERYABLE:
      return LogicalTableScan.create(cluster,
          RelOptTableImpl.create(null,
              typeFactory.createJavaType(
                  Types.toClass(
                      Types.getElementType(call.targetExpression.getType()))),
              ImmutableList.of(),
              call.targetExpression),
          ImmutableList.of());

    case SCHEMA_GET_TABLE:
      return LogicalTableScan.create(cluster,
          RelOptTableImpl.create(null,
              typeFactory.createJavaType((Class)
                  ((ConstantExpression) call.expressions.get(1)).value),
              ImmutableList.of(),
              call.targetExpression),
          ImmutableList.of());

    default:
      throw new UnsupportedOperationException(
          "unknown method " + call.method);
    }
  }
  throw new UnsupportedOperationException(
      "unknown expression type " + expression.getNodeType());
}
 
Example #22
Source File: ExpressionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test 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");
  BlockStatement 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.emptyList(),
                  Arrays.asList(
                      Expressions.fieldDecl(
                          Modifier.PUBLIC | Modifier.FINAL,
                          Expressions.parameter(
                              String.class,
                              "qux"),
                          Expressions.constant("xyzzy")),
                      Expressions.methodDecl(
                          Modifier.PUBLIC,
                          Integer.TYPE,
                          "size",
                          Collections.emptyList(),
                          Blocks.toFunctionBlock(
                              Expressions.call(
                                  bazParameter,
                                  "size",
                                  Collections.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",
                                  ImmutableList.of())))))));
  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));
}
 
Example #23
Source File: SparkRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Result implementSpark(Implementor implementor) {
  final JavaTypeFactory typeFactory = implementor.getTypeFactory();
  final BlockBuilder builder = new BlockBuilder();
  final SparkRel child = (SparkRel) getInput();

  final Result result = implementor.visitInput(this, 0, child);

  final PhysType physType =
      PhysTypeImpl.of(
          typeFactory, getRowType(), JavaRowFormat.CUSTOM);

  // final RDD<Employee> inputRdd = <<child adapter>>;
  // return inputRdd.flatMap(
  //   new FlatMapFunction<Employee, X>() {
  //          public List<X> call(Employee e) {
  //              if (!(e.empno < 10)) {
  //                  return Collections.emptyList();
  //              }
  //              return Collections.singletonList(
  //                  new X(...)));
  //          }
  //      })


  Type outputJavaType = physType.getJavaRowType();
  final Type rddType =
      Types.of(
          JavaRDD.class, outputJavaType);
  Type inputJavaType = result.physType.getJavaRowType();
  final Expression inputRdd_ =
      builder.append(
          "inputRdd",
          result.block);

  BlockBuilder builder2 = new BlockBuilder();

  final ParameterExpression e_ =
      Expressions.parameter(inputJavaType, "e");
  if (program.getCondition() != null) {
    Expression condition =
        RexToLixTranslator.translateCondition(
            program,
            typeFactory,
            builder2,
            new RexToLixTranslator.InputGetterImpl(
                Collections.singletonList(
                    Pair.of((Expression) e_, result.physType))),
            null, implementor.getConformance());
    builder2.add(
        Expressions.ifThen(
            Expressions.not(condition),
            Expressions.return_(null,
                Expressions.call(
                    BuiltInMethod.COLLECTIONS_EMPTY_LIST.method))));
  }

  final SqlConformance conformance = SqlConformanceEnum.DEFAULT;
  List<Expression> expressions =
      RexToLixTranslator.translateProjects(
          program,
          typeFactory,
          conformance,
          builder2,
          null,
          DataContext.ROOT,
          new RexToLixTranslator.InputGetterImpl(
              Collections.singletonList(
                  Pair.of((Expression) e_, result.physType))),
          null);
  builder2.add(
      Expressions.return_(null,
          Expressions.convert_(
              Expressions.call(
                  BuiltInMethod.COLLECTIONS_SINGLETON_LIST.method,
                  physType.record(expressions)),
              List.class)));

  final BlockStatement callBody = builder2.toBlock();
  builder.add(
      Expressions.return_(
          null,
          Expressions.call(
              inputRdd_,
              SparkMethod.RDD_FLAT_MAP.method,
              Expressions.lambda(
                  SparkRuntime.CalciteFlatMapFunction.class,
                  callBody,
                  e_))));
  return implementor.result(physType, builder.toBlock());
}
 
Example #24
Source File: EnumerableMatch.java    From calcite with Apache License 2.0 4 votes vote down vote up
private Expression implementEmitter(EnumerableRelImplementor implementor,
    PhysType physType, PhysType inputPhysType) {
  final ParameterExpression rows_ =
      Expressions.parameter(Types.of(List.class, inputPhysType.getJavaRowType()), "rows");
  final ParameterExpression rowStates_ =
      Expressions.parameter(List.class, "rowStates");
  final ParameterExpression symbols_ =
      Expressions.parameter(List.class, "symbols");
  final ParameterExpression match_ =
      Expressions.parameter(int.class, "match");
  final ParameterExpression consumer_ =
      Expressions.parameter(Consumer.class, "consumer");
  final ParameterExpression i_ = Expressions.parameter(int.class, "i");

  final ParameterExpression row_ =
      Expressions.parameter(inputPhysType.getJavaRowType(), "row");

  final BlockBuilder builder2 = new BlockBuilder();

  // Add loop variable initialization
  builder2.add(
      Expressions.declare(0, row_,
          EnumUtils.convert(
              Expressions.call(rows_, BuiltInMethod.LIST_GET.method, i_),
              inputPhysType.getJavaRowType())));

  RexBuilder rexBuilder = new RexBuilder(implementor.getTypeFactory());
  RexProgramBuilder rexProgramBuilder =
      new RexProgramBuilder(inputPhysType.getRowType(), rexBuilder);
  for (Map.Entry<String, RexNode> entry : measures.entrySet()) {
    rexProgramBuilder.addProject(entry.getValue(), entry.getKey());
  }

  final RexToLixTranslator translator = RexToLixTranslator.forAggregation(
      (JavaTypeFactory) getCluster().getTypeFactory(),
      builder2,
      new PassedRowsInputGetter(row_, rows_, inputPhysType),
      implementor.getConformance());

  final ParameterExpression result_ =
      Expressions.parameter(physType.getJavaRowType());

  builder2.add(
      Expressions.declare(Modifier.FINAL, result_,
          Expressions.new_(physType.getJavaRowType())));
  Ord.forEach(measures.values(), (measure, i) ->
      builder2.add(
          Expressions.statement(
              Expressions.assign(physType.fieldReference(result_, i),
                  implementMeasure(translator, rows_, symbols_, i_, row_,
                      measure)))));
  builder2.add(
      Expressions.statement(
          Expressions.call(consumer_, BuiltInMethod.CONSUMER_ACCEPT.method,
              result_)));

  final BlockBuilder builder = new BlockBuilder();

  // Loop Length

  // we have to use an explicit for (int i = ...) loop, as we need to know later
  // which of the matched rows are already passed (in MatchUtils), so foreach cannot be used
  builder.add(
      Expressions.for_(
          Expressions.declare(0, i_, Expressions.constant(0)),
          Expressions.lessThan(i_,
              Expressions.call(rows_, BuiltInMethod.COLLECTION_SIZE.method)),
          Expressions.preIncrementAssign(i_),
          builder2.toBlock()));

  return Expressions.new_(
      Types.of(Enumerables.Emitter.class), NO_EXPRS,
      Expressions.list(
          EnumUtils.overridingMethodDecl(
              BuiltInMethod.EMITTER_EMIT.method,
              ImmutableList.of(rows_, rowStates_, symbols_, match_,
                  consumer_),
              builder.toBlock())));
}
 
Example #25
Source File: EnumerableMatch.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Generates code for a predicate. */
private Expression implementPredicate(PhysType physType,
    ParameterExpression rows_, BlockStatement body) {
  final List<MemberDeclaration> memberDeclarations = new ArrayList<>();
  ParameterExpression row_ = Expressions.parameter(
      Types.of(MemoryFactory.Memory.class,
          physType.getJavaRowType()), "row_");
  Expressions.assign(row_,
      Expressions.call(rows_, BuiltInMethod.MEMORY_GET0.method));

  // Implement the Predicate here based on the pattern definition

  // Add a predicate method:
  //
  //   public boolean test(E row, List<E> rows) {
  //     return ...;
  //   }
  memberDeclarations.add(
      EnumUtils.overridingMethodDecl(
          BuiltInMethod.PREDICATE_TEST.method,
          ImmutableList.of(row_), body));
  if (EnumerableRules.BRIDGE_METHODS) {
    // Add a bridge method:
    //
    //   public boolean test(Object row, Object rows) {
    //     return this.test(row, (List) rows);
    //   }
    final ParameterExpression row0_ =
        Expressions.parameter(Object.class, "row");
    final ParameterExpression rowsO_ =
        Expressions.parameter(Object.class, "rows");
    BlockBuilder bridgeBody = new BlockBuilder();
    bridgeBody.add(
        Expressions.return_(null,
            Expressions.call(
                Expressions.parameter(Comparable.class, "this"),
                BuiltInMethod.PREDICATE_TEST.method,
                Expressions.convert_(row0_,
                    Types.of(MemoryFactory.Memory.class,
                        physType.getJavaRowType())))));
    memberDeclarations.add(
        EnumUtils.overridingMethodDecl(
            BuiltInMethod.PREDICATE_TEST.method,
            ImmutableList.of(row0_), bridgeBody.toBlock()));
  }
  return Expressions.new_(Types.of(Predicate.class), NO_EXPRS,
      memberDeclarations);
}
 
Example #26
Source File: KylinEnumerableUnion.java    From kylin with Apache License 2.0 4 votes vote down vote up
public KylinEnumerableUnion(RelOptCluster cluster, RelTraitSet traitSet, List<RelNode> inputs, boolean all) {
    super(cluster, traitSet, inputs, all);

    unionArray = Types.lookupMethod(ExtendedEnumerable.class, "union", Enumerable.class, EqualityComparer.class);
    arrayComparer = Types.lookupMethod(Functions.class, "arrayComparer");
}
 
Example #27
Source File: RexNodeToJavaCodeCompiler.java    From streamline with Apache License 2.0 4 votes vote down vote up
/**
 * Defines a constructor.
 */
StreamlineBuiltInMethod(Class clazz, Class... argumentTypes) {
  this(null, Types.lookupConstructor(clazz, argumentTypes), null);
}
 
Example #28
Source File: RexNodeToJavaCodeCompiler.java    From streamline with Apache License 2.0 4 votes vote down vote up
/**
 * Defines a method.
 */
StreamlineBuiltInMethod(Class clazz, String methodName, Class... argumentTypes) {
  this(Types.lookupMethod(clazz, methodName, argumentTypes), null, null);
}
 
Example #29
Source File: ReflectiveSchemaTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Test that uses a JDBC connection as a linq4j
 * {@link org.apache.calcite.linq4j.QueryProvider}.
 *
 * @throws Exception on error
 */
@Test void testQueryProvider() throws Exception {
  Connection connection = CalciteAssert
      .that(CalciteAssert.Config.REGULAR).connect();
  QueryProvider queryProvider = connection.unwrap(QueryProvider.class);
  ParameterExpression e = Expressions.parameter(Employee.class, "e");

  // "Enumerable<T> asEnumerable(final T[] ts)"
  List<Object[]> list =
      queryProvider.createQuery(
          Expressions.call(
              Expressions.call(
                  Types.of(Enumerable.class, Employee.class),
                  null,
                  LINQ4J_AS_ENUMERABLE_METHOD,
                  Expressions.constant(
                      new JdbcTest.HrSchema().emps)),
              "asQueryable"),
          Employee.class)
          .where(
              Expressions.lambda(
                  Expressions.lessThan(
                      Expressions.field(
                          e, "empid"),
                      Expressions.constant(160)),
                  e))
          .where(
              Expressions.lambda(
                  Expressions.greaterThan(
                      Expressions.field(
                          e, "empid"),
                      Expressions.constant(140)),
                  e))
          .select(
              Expressions.<Function1<Employee, Object[]>>lambda(
                  Expressions.new_(
                      Object[].class,
                      Expressions.field(
                          e, "empid"),
                      Expressions.call(
                          Expressions.field(
                              e, "name"),
                          "toUpperCase")),
                  e))
          .toList();
  assertEquals(1, list.size());
  assertEquals(2, list.get(0).length);
  assertEquals(150, list.get(0)[0]);
  assertEquals("SEBASTIAN", list.get(0)[1]);
}
 
Example #30
Source File: RexToJavaCompiler.java    From samza with Apache License 2.0 4 votes vote down vote up
/**
 * Defines a method.
 */
SamzaBuiltInMethod(Class clazz, String methodName, Class... argumentTypes) {
  this.method = Types.lookupMethod(clazz, methodName, argumentTypes);
}