Java Code Examples for org.apache.calcite.linq4j.tree.Primitive#ofBoxOr()

The following examples show how to use org.apache.calcite.linq4j.tree.Primitive#ofBoxOr() . 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: 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 2
Source File: JdbcToSparkConverter.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SparkRel.Result implementSpark(SparkRel.Implementor implementor) {
  // Generate:
  //   ResultSetEnumerable.of(schema.getDataSource(), "select ...")
  final BlockBuilder list = new BlockBuilder();
  final JdbcRel child = (JdbcRel) getInput();
  final PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(), getRowType(),
          JavaRowFormat.CUSTOM);
  final JdbcConvention jdbcConvention =
      (JdbcConvention) child.getConvention();
  String sql = generateSql(jdbcConvention.dialect);
  if (CalciteSystemProperty.DEBUG.value()) {
    System.out.println("[" + sql + "]");
  }
  final Expression sqlLiteral =
      list.append("sql", Expressions.constant(sql));
  final List<Primitive> primitives = new ArrayList<>();
  for (int i = 0; i < getRowType().getFieldCount(); i++) {
    final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i));
    primitives.add(primitive != null ? primitive : Primitive.OTHER);
  }
  final Expression primitivesLiteral =
      list.append("primitives",
          Expressions.constant(
              primitives.toArray(new Primitive[0])));
  final Expression enumerable =
      list.append(
          "enumerable",
          Expressions.call(
              BuiltInMethod.RESULT_SET_ENUMERABLE_OF.method,
              Expressions.call(
                  Expressions.convert_(
                      jdbcConvention.expression,
                      JdbcSchema.class),
                  BuiltInMethod.JDBC_SCHEMA_DATA_SOURCE.method),
              sqlLiteral,
              primitivesLiteral));
  list.add(
      Expressions.return_(null, enumerable));
  return implementor.result(physType, list.toBlock());
}
 
Example 3
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 4
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) {
  // neither nullable:
  //   return x OP y
  // x nullable
  //   null_returns_null
  //     return x == null ? null : x OP y
  //   ignore_null
  //     return x == null ? null : y
  // x, y both nullable
  //   null_returns_null
  //     return x == null || y == null ? null : x OP y
  //   ignore_null
  //     return x == null ? y : y == null ? x : x OP y
  if (backupMethodName != null) {
    // If one or both operands have ANY type, use the late-binding backup
    // method.
    if (anyAnyOperands(call)) {
      return callBackupMethodAnyType(translator, call, argValueList);
    }

    final Type type0 = argValueList.get(0).getType();
    final Type type1 = argValueList.get(1).getType();
    final SqlBinaryOperator op = (SqlBinaryOperator) call.getOperator();
    final RelDataType relDataType0 = call.getOperands().get(0).getType();
    final Expression fieldComparator = generateCollatorExpression(relDataType0.getCollation());
    if (fieldComparator != null) {
      argValueList.add(fieldComparator);
    }
    final Primitive primitive = Primitive.ofBoxOr(type0);
    if (primitive == null
        || type1 == BigDecimal.class
        || COMPARISON_OPERATORS.contains(op)
        && !COMP_OP_TYPES.contains(primitive)) {
      return Expressions.call(SqlFunctions.class, backupMethodName,
          argValueList);
    }
    // When checking equals or not equals on two primitive boxing classes
    // (i.e. Long x, Long y), we should fall back to call `SqlFunctions.eq(x, y)`
    // or `SqlFunctions.ne(x, y)`, rather than `x == y`
    final Primitive boxPrimitive0 = Primitive.ofBox(type0);
    final Primitive boxPrimitive1 = Primitive.ofBox(type1);
    if (EQUALS_OPERATORS.contains(op)
        && boxPrimitive0 != null && boxPrimitive1 != null) {
      return Expressions.call(SqlFunctions.class, backupMethodName,
          argValueList);
    }
  }
  return Expressions.makeBinary(expressionType,
      argValueList.get(0), argValueList.get(1));
}