Java Code Examples for org.apache.calcite.linq4j.tree.Expression#getType()

The following examples show how to use org.apache.calcite.linq4j.tree.Expression#getType() . 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: 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 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: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ParameterExpression genValueStatement(
    final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList,
    final Expression condition) {
  List<Expression> optimizedArgValueList = argValueList;
  if (harmonize) {
    optimizedArgValueList =
        harmonize(optimizedArgValueList, translator, call);
  }
  optimizedArgValueList = unboxIfNecessary(optimizedArgValueList);

  final Expression callValue =
      implementSafe(translator, call, optimizedArgValueList);

  // In general, RexCall's type is correct for code generation
  // and thus we should ensure the consistency.
  // However, for some special cases (e.g., TableFunction),
  // the implementation's type is correct, we can't convert it.
  final SqlOperator op = call.getOperator();
  final Type returnType = translator.typeFactory.getJavaClass(call.getType());
  final boolean noConvert = (returnType == null)
          || (returnType == callValue.getType())
          || (op instanceof SqlUserDefinedTableMacro)
          || (op instanceof SqlUserDefinedTableFunction);
  final Expression convertedCallValue =
          noConvert
          ? callValue
          : EnumUtils.convert(callValue, returnType);

  final Expression valueExpression =
      Expressions.condition(condition,
          getIfTrue(convertedCallValue.getType(), argValueList),
          convertedCallValue);
  final ParameterExpression value =
      Expressions.parameter(convertedCallValue.getType(),
          translator.getBlockBuilder().newName(getVariableName() + "_value"));
  translator.getBlockBuilder().add(
      Expressions.declare(Modifier.FINAL, value, valueExpression));
  return value;
}
 
Example 4
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 5
Source File: EnumUtils.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Convert {@code operand} to target type {@code toType}.
 *
 * @param operand The expression to convert
 * @param toType  Target type
 * @return A new expression with type {@code toType} or original if there
 * is no need to convert
 */
public static Expression convert(Expression operand, Type toType) {
  final Type fromType = operand.getType();
  return EnumUtils.convert(operand, fromType, toType);
}