Java Code Examples for org.apache.calcite.sql.type.SqlTypeUtil#inCharFamily()

The following examples show how to use org.apache.calcite.sql.type.SqlTypeUtil#inCharFamily() . 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: RelDataTypeFactoryImpl.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RelDataType copySimpleType(
    RelDataType type,
    boolean nullable) {
  if (type instanceof JavaType) {
    JavaType javaType = (JavaType) type;
    if (SqlTypeUtil.inCharFamily(javaType)) {
      return new JavaType(
          javaType.clazz,
          nullable,
          javaType.charset,
          javaType.collation);
    } else {
      return new JavaType(
          nullable
              ? Primitive.box(javaType.clazz)
              : Primitive.unbox(javaType.clazz),
          nullable);
    }
  } else {
    // REVIEW: RelCrossType if it stays around; otherwise get rid of
    // this comment
    return type;
  }
}
 
Example 2
Source File: SqlJsonObjectFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 3
Source File: RelDataTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RelDataType copySimpleType(
    RelDataType type,
    boolean nullable) {
  if (type instanceof JavaType) {
    JavaType javaType = (JavaType) type;
    if (SqlTypeUtil.inCharFamily(javaType)) {
      return new JavaType(
          javaType.clazz,
          nullable,
          javaType.charset,
          javaType.collation);
    } else {
      return new JavaType(
          nullable
              ? Primitive.box(javaType.clazz)
              : Primitive.unbox(javaType.clazz),
          nullable);
    }
  } else {
    // REVIEW: RelCrossType if it stays around; otherwise get rid of
    // this comment
    return type;
  }
}
 
Example 4
Source File: AbstractTypeCoercion.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Sync the data type additional attributes before casting,
 * i.e. nullability, charset, collation.
 */
RelDataType syncAttributes(
    RelDataType fromType,
    RelDataType toType) {
  RelDataType syncedType = toType;
  if (fromType != null) {
    syncedType = factory.createTypeWithNullability(syncedType, fromType.isNullable());
    if (SqlTypeUtil.inCharOrBinaryFamilies(fromType)
        && SqlTypeUtil.inCharOrBinaryFamilies(toType)) {
      Charset charset = fromType.getCharset();
      SqlCollation collation = fromType.getCollation();
      if (charset != null && SqlTypeUtil.inCharFamily(syncedType)) {
        syncedType = factory.createTypeWithCharsetAndCollation(syncedType,
            charset,
            collation);
      }
    }
  }
  return syncedType;
}
 
Example 5
Source File: SqlJsonObjectFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 6
Source File: RexUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether a {@link RexCall} requires decimal expansion. It
 * usually requires expansion if it has decimal operands.
 *
 * <p>Exceptions to this rule are:
 *
 * <ul>
 * <li>isNull doesn't require expansion
 * <li>It's okay to cast decimals to and from char types
 * <li>It's okay to cast nulls as decimals
 * <li>Casts require expansion if their return type is decimal
 * <li>Reinterpret casts can handle a decimal operand
 * </ul>
 *
 * @param expr    expression possibly in need of expansion
 * @param recurse whether to check nested calls
 * @return whether the expression requires expansion
 */
public static boolean requiresDecimalExpansion(RexNode expr, boolean recurse) {
    if (!(expr instanceof RexCall)) {
        return false;
    }
    RexCall call = (RexCall) expr;

    boolean localCheck = true;
    switch (call.getKind()) {
    case REINTERPRET:
    case IS_NULL:
        localCheck = false;
        break;
    case CAST:
        RelDataType lhsType = call.getType();
        RelDataType rhsType = call.getOperands().get(0).getType();
        if (rhsType.getSqlTypeName() == SqlTypeName.NULL) {
            return false;
        }
        if (SqlTypeUtil.inCharFamily(lhsType) || SqlTypeUtil.inCharFamily(rhsType)) {
            localCheck = false;
        } else if (SqlTypeUtil.isDecimal(lhsType) && (lhsType != rhsType)) {
            return true;
        }
        break;
    default:
        localCheck = call.getOperator().requiresDecimalExpansion();
    }

    if (localCheck) {
        if (SqlTypeUtil.isDecimal(call.getType())) {
            // NOTE jvs 27-Mar-2007: Depending on the type factory, the
            // result of a division may be decimal, even though both inputs
            // are integer.
            return true;
        }
        for (int i = 0; i < call.getOperands().size(); i++) {
            if (SqlTypeUtil.isDecimal(call.getOperands().get(i).getType())) {
                return true;
            }
        }
    }
    return recurse && requiresDecimalExpansion(call.getOperands(), true);
}
 
Example 7
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether a {@link RexCall} requires decimal expansion. It
 * usually requires expansion if it has decimal operands.
 *
 * <p>Exceptions to this rule are:
 *
 * <ul>
 * <li>isNull doesn't require expansion
 * <li>It's okay to cast decimals to and from char types
 * <li>It's okay to cast nulls as decimals
 * <li>Casts require expansion if their return type is decimal
 * <li>Reinterpret casts can handle a decimal operand
 * </ul>
 *
 * @param expr    expression possibly in need of expansion
 * @param recurse whether to check nested calls
 * @return whether the expression requires expansion
 */
public static boolean requiresDecimalExpansion(
    RexNode expr,
    boolean recurse) {
  if (!(expr instanceof RexCall)) {
    return false;
  }
  RexCall call = (RexCall) expr;

  boolean localCheck = true;
  switch (call.getKind()) {
  case REINTERPRET:
  case IS_NULL:
    localCheck = false;
    break;
  case CAST:
    RelDataType lhsType = call.getType();
    RelDataType rhsType = call.operands.get(0).getType();
    if (rhsType.getSqlTypeName() == SqlTypeName.NULL) {
      return false;
    }
    if (SqlTypeUtil.inCharFamily(lhsType)
        || SqlTypeUtil.inCharFamily(rhsType)) {
      localCheck = false;
    } else if (SqlTypeUtil.isDecimal(lhsType)
        && (lhsType != rhsType)) {
      return true;
    }
    break;
  default:
    localCheck = call.getOperator().requiresDecimalExpansion();
  }

  if (localCheck) {
    if (SqlTypeUtil.isDecimal(call.getType())) {
      // NOTE jvs 27-Mar-2007: Depending on the type factory, the
      // result of a division may be decimal, even though both inputs
      // are integer.
      return true;
    }
    for (int i = 0; i < call.operands.size(); i++) {
      if (SqlTypeUtil.isDecimal(call.operands.get(i).getType())) {
        return true;
      }
    }
  }
  return recurse && requiresDecimalExpansion(call.operands, true);
}