Java Code Examples for org.apache.calcite.linq4j.tree.Expressions#convert_()

The following examples show how to use org.apache.calcite.linq4j.tree.Expressions#convert_() . 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: JdbcToEnumerableConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
private UnaryExpression getTimeZoneExpression(
    EnumerableRelImplementor implementor) {
  return Expressions.convert_(
      Expressions.call(
          implementor.getRootExpression(),
          "get",
          Expressions.constant("timeZone")),
      TimeZone.class);
}
 
Example 2
Source File: EnumerableLimit.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Expression getExpression(RexNode offset) {
  if (offset instanceof RexDynamicParam) {
    final RexDynamicParam param = (RexDynamicParam) offset;
    return Expressions.convert_(
        Expressions.call(DataContext.ROOT,
            BuiltInMethod.DATA_CONTEXT_GET.method,
            Expressions.constant("?" + param.getIndex())),
        Integer.class);
  } else {
    return Expressions.constant(RexLiteral.intValue(offset));
  }
}
 
Example 3
Source File: RexImpTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override Expression implementSafe(RexToLixTranslator translator,
    RexCall call, List<Expression> argValueList) {
  final Expression argValue = argValueList.get(0);
  final UnaryExpression e = Expressions.makeUnary(expressionType, argValue);
  if (e.type.equals(argValue.type)) {
    return e;
  }
  // Certain unary operators do not preserve type. For example, the "-"
  // operator applied to a "byte" expression returns an "int".
  return Expressions.convert_(e, argValue.type);
}
 
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: Schemas.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Converts a schema expression to a given type by calling the
 * {@link SchemaPlus#unwrap(Class)} method. */
public static Expression unwrap(Expression call, Class type) {
  return Expressions.convert_(
      Expressions.call(call, BuiltInMethod.SCHEMA_PLUS_UNWRAP.method,
          Expressions.constant(type)),
      type);
}
 
Example 6
Source File: JdbcToEnumerableConverter.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void generateGet(EnumerableRelImplementor implementor,
    PhysType physType, BlockBuilder builder, ParameterExpression resultSet_,
    int i, Expression target, Expression calendar_,
    SqlDialect.CalendarPolicy calendarPolicy) {
  final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i));
  final RelDataType fieldType =
      physType.getRowType().getFieldList().get(i).getType();
  final List<Expression> dateTimeArgs = new ArrayList<>();
  dateTimeArgs.add(Expressions.constant(i + 1));
  SqlTypeName sqlTypeName = fieldType.getSqlTypeName();
  boolean offset = false;
  switch (calendarPolicy) {
  case LOCAL:
    dateTimeArgs.add(calendar_);
    break;
  case NULL:
    // We don't specify a calendar at all, so we don't add an argument and
    // instead use the version of the getXXX that doesn't take a Calendar
    break;
  case DIRECT:
    sqlTypeName = SqlTypeName.ANY;
    break;
  case SHIFT:
    switch (sqlTypeName) {
    case TIMESTAMP:
    case DATE:
      offset = true;
    }
    break;
  }
  final Expression source;
  switch (sqlTypeName) {
  case DATE:
  case TIME:
  case TIMESTAMP:
    source = Expressions.call(
        getMethod(sqlTypeName, fieldType.isNullable(), offset),
        Expressions.<Expression>list()
            .append(
                Expressions.call(resultSet_,
                    getMethod2(sqlTypeName), dateTimeArgs))
        .appendIf(offset, getTimeZoneExpression(implementor)));
    break;
  case ARRAY:
    final Expression x = Expressions.convert_(
        Expressions.call(resultSet_, jdbcGetMethod(primitive),
            Expressions.constant(i + 1)),
        java.sql.Array.class);
    source = Expressions.call(BuiltInMethod.JDBC_ARRAY_TO_LIST.method, x);
    break;
  default:
    source = Expressions.call(
        resultSet_, jdbcGetMethod(primitive), Expressions.constant(i + 1));
  }
  builder.add(
      Expressions.statement(
          Expressions.assign(
              target, source)));

  // [CALCITE-596] If primitive type columns contain null value, returns null
  // object
  if (primitive != null) {
    builder.add(
        Expressions.ifThen(
            Expressions.call(resultSet_, "wasNull"),
            Expressions.statement(
                Expressions.assign(target,
                    Expressions.constant(null)))));
  }
}
 
Example 7
Source File: EnumerableTableSpool.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  // TODO for the moment only LAZY read & write is supported
  if (readType != Type.LAZY || writeType != Type.LAZY) {
    throw new UnsupportedOperationException(
        "EnumerableTableSpool supports for the moment only LAZY read and LAZY write");
  }

  //  ModifiableTable t = (ModifiableTable) root.getRootSchema().getTable(tableName);
  //  return lazyCollectionSpool(t.getModifiableCollection(), <inputExp>);

  BlockBuilder builder = new BlockBuilder();

  RelNode input = getInput();
  Result inputResult = implementor.visitChild(this, 0, (EnumerableRel) input, pref);

  String tableName = table.getQualifiedName().get(table.getQualifiedName().size() - 1);
  Expression tableExp = Expressions.convert_(
      Expressions.call(
          Expressions.call(
              implementor.getRootExpression(),
              BuiltInMethod.DATA_CONTEXT_GET_ROOT_SCHEMA.method),
          BuiltInMethod.SCHEMA_GET_TABLE.method,
          Expressions.constant(tableName, String.class)),
      ModifiableTable.class);
  Expression collectionExp = Expressions.call(
      tableExp,
      BuiltInMethod.MODIFIABLE_TABLE_GET_MODIFIABLE_COLLECTION.method);

  Expression inputExp = builder.append("input", inputResult.block);

  Expression spoolExp = Expressions.call(
      BuiltInMethod.LAZY_COLLECTION_SPOOL.method,
      collectionExp,
      inputExp);
  builder.add(spoolExp);

  PhysType physType = PhysTypeImpl.of(
      implementor.getTypeFactory(),
      getRowType(),
      pref.prefer(inputResult.format));
  return implementor.result(physType, builder.toBlock());
}
 
Example 8
Source File: RexImpTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override Expression implementSafe(final RexToLixTranslator translator,
    final RexCall call, final List<Expression> argValueList) {
  final RexNode operand0 = call.getOperands().get(0);
  Expression trop0 = argValueList.get(0);
  final SqlTypeName typeName1 =
      call.getOperands().get(1).getType().getSqlTypeName();
  Expression trop1 = argValueList.get(1);
  final SqlTypeName typeName = call.getType().getSqlTypeName();
  switch (operand0.getType().getSqlTypeName()) {
  case DATE:
    switch (typeName) {
    case TIMESTAMP:
      trop0 = Expressions.convert_(
          Expressions.multiply(trop0,
              Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
          long.class);
      break;
    default:
      switch (typeName1) {
      case INTERVAL_DAY:
      case INTERVAL_DAY_HOUR:
      case INTERVAL_DAY_MINUTE:
      case INTERVAL_DAY_SECOND:
      case INTERVAL_HOUR:
      case INTERVAL_HOUR_MINUTE:
      case INTERVAL_HOUR_SECOND:
      case INTERVAL_MINUTE:
      case INTERVAL_MINUTE_SECOND:
      case INTERVAL_SECOND:
        trop1 = Expressions.convert_(
            Expressions.divide(trop1,
                Expressions.constant(DateTimeUtils.MILLIS_PER_DAY)),
            int.class);
      }
    }
    break;
  case TIME:
    trop1 = Expressions.convert_(trop1, int.class);
    break;
  }
  switch (typeName1) {
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
    switch (call.getKind()) {
    case MINUS:
      trop1 = Expressions.negate(trop1);
    }
    switch (typeName) {
    case TIME:
      return Expressions.convert_(trop0, long.class);
    default:
      final BuiltInMethod method =
          operand0.getType().getSqlTypeName() == SqlTypeName.TIMESTAMP
              ? BuiltInMethod.ADD_MONTHS
              : BuiltInMethod.ADD_MONTHS_INT;
      return Expressions.call(method.method, trop0, trop1);
    }

  case INTERVAL_DAY:
  case INTERVAL_DAY_HOUR:
  case INTERVAL_DAY_MINUTE:
  case INTERVAL_DAY_SECOND:
  case INTERVAL_HOUR:
  case INTERVAL_HOUR_MINUTE:
  case INTERVAL_HOUR_SECOND:
  case INTERVAL_MINUTE:
  case INTERVAL_MINUTE_SECOND:
  case INTERVAL_SECOND:
    switch (call.getKind()) {
    case MINUS:
      return normalize(typeName, Expressions.subtract(trop0, trop1));
    default:
      return normalize(typeName, Expressions.add(trop0, trop1));
    }

  default:
    switch (call.getKind()) {
    case MINUS:
      switch (typeName) {
      case INTERVAL_YEAR:
      case INTERVAL_YEAR_MONTH:
      case INTERVAL_MONTH:
        return Expressions.call(BuiltInMethod.SUBTRACT_MONTHS.method,
            trop0, trop1);
      }
      TimeUnit fromUnit =
          typeName1 == SqlTypeName.DATE ? TimeUnit.DAY : TimeUnit.MILLISECOND;
      TimeUnit toUnit = TimeUnit.MILLISECOND;
      return multiplyDivide(
          Expressions.convert_(Expressions.subtract(trop0, trop1),
              (Class) long.class),
          fromUnit.multiplier, toUnit.multiplier);
    default:
      throw new AssertionError(call);
    }
  }
}
 
Example 9
Source File: EnumUtilsTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testTypeConvertFromPrimitiveToBox() {
  final Expression intVariable =
      Expressions.parameter(0, int.class, "intV");

  // (byte)(int) -> Byte: Byte.valueOf((byte) intV)
  final Expression bytePrimitiveConverted =
      Expressions.convert_(intVariable, byte.class);
  final Expression converted0 =
      EnumUtils.convert(bytePrimitiveConverted, Byte.class);
  assertThat(Expressions.toString(converted0),
      is("Byte.valueOf((byte) intV)"));

  // (char)(int) -> Character: Character.valueOf((char) intV)
  final Expression characterPrimitiveConverted =
      Expressions.convert_(intVariable, char.class);
  final Expression converted1 =
      EnumUtils.convert(characterPrimitiveConverted, Character.class);
  assertThat(Expressions.toString(converted1),
      is("Character.valueOf((char) intV)"));

  // (short)(int) -> Short: Short.valueOf((short) intV)
  final Expression shortPrimitiveConverted =
      Expressions.convert_(intVariable, short.class);
  final Expression converted2 =
      EnumUtils.convert(shortPrimitiveConverted, Short.class);
  assertThat(Expressions.toString(converted2),
      is("Short.valueOf((short) intV)"));

  // (long)(int) -> Long: Long.valueOf(intV)
  final Expression longPrimitiveConverted =
      Expressions.convert_(intVariable, long.class);
  final Expression converted3 =
      EnumUtils.convert(longPrimitiveConverted, Long.class);
  assertThat(Expressions.toString(converted3),
      is("Long.valueOf(intV)"));

  // (float)(int) -> Float: Float.valueOf(intV)
  final Expression floatPrimitiveConverted =
      Expressions.convert_(intVariable, float.class);
  final Expression converted4 =
      EnumUtils.convert(floatPrimitiveConverted, Float.class);
  assertThat(Expressions.toString(converted4),
      is("Float.valueOf(intV)"));

  // (double)(int) -> Double: Double.valueOf(intV)
  final Expression doublePrimitiveConverted =
      Expressions.convert_(intVariable, double.class);
  final Expression converted5 =
      EnumUtils.convert(doublePrimitiveConverted, Double.class);
  assertThat(Expressions.toString(converted5),
      is("Double.valueOf(intV)"));

  final Expression byteConverted =
      EnumUtils.convert(intVariable, Byte.class);
  assertThat(Expressions.toString(byteConverted),
      is("Byte.valueOf((byte) intV)"));

  final Expression shortConverted =
      EnumUtils.convert(intVariable, Short.class);
  assertThat(Expressions.toString(shortConverted),
      is("Short.valueOf((short) intV)"));

  final Expression integerConverted =
      EnumUtils.convert(intVariable, Integer.class);
  assertThat(Expressions.toString(integerConverted),
      is("Integer.valueOf(intV)"));

  final Expression longConverted =
      EnumUtils.convert(intVariable, Long.class);
  assertThat(Expressions.toString(longConverted),
      is("Long.valueOf((long) intV)"));

  final Expression floatConverted =
      EnumUtils.convert(intVariable, Float.class);
  assertThat(Expressions.toString(floatConverted),
      is("Float.valueOf((float) intV)"));

  final Expression doubleConverted =
      EnumUtils.convert(intVariable, Double.class);
  assertThat(Expressions.toString(doubleConverted),
      is("Double.valueOf((double) intV)"));
}
 
Example 10
Source File: TypeFinderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testConvertExpression() {
  UnaryExpression expr = Expressions.convert_(Expressions.new_(String.class), Object.class);
  assertJavaCodeContains("(Object) new String()\n", expr);
  assertTypeContains(Arrays.asList(String.class, Object.class), expr);
}