Java Code Examples for org.apache.calcite.rel.type.RelDataType#getCollation()

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getCollation() . 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: SqlTypeUtil.java    From Bats with Apache License 2.0 7 votes vote down vote up
/**
 * Adds collation and charset to a character type, returns other types
 * unchanged.
 *
 * @param type        Type
 * @param typeFactory Type factory
 * @return Type with added charset and collation, or unchanged type if it is
 * not a char type.
 */
public static RelDataType addCharsetAndCollation(
    RelDataType type,
    RelDataTypeFactory typeFactory) {
  if (!inCharFamily(type)) {
    return type;
  }
  Charset charset = type.getCharset();
  if (charset == null) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = type.getCollation();
  if (collation == null) {
    collation = SqlCollation.IMPLICIT;
  }

  // todo: should get the implicit collation from repository
  //   instead of null
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
  return type;
}
 
Example 2
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 3
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Adds collation and charset to a character type, returns other types
 * unchanged.
 *
 * @param type        Type
 * @param typeFactory Type factory
 * @return Type with added charset and collation, or unchanged type if it is
 * not a char type.
 */
public static RelDataType addCharsetAndCollation(
    RelDataType type,
    RelDataTypeFactory typeFactory) {
  if (!inCharFamily(type)) {
    return type;
  }
  Charset charset = type.getCharset();
  if (charset == null) {
    charset = typeFactory.getDefaultCharset();
  }
  SqlCollation collation = type.getCollation();
  if (collation == null) {
    collation = SqlCollation.IMPLICIT;
  }

  // todo: should get the implicit collation from repository
  //   instead of null
  type =
      typeFactory.createTypeWithCharsetAndCollation(
          type,
          charset,
          collation);
  SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(type);
  return type;
}
 
Example 4
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void checkCollation(
    String expression,
    String expectedCollationName,
    SqlCollation.Coercibility expectedCoercibility) {
  for (String sql : buildQueries(expression)) {
    RelDataType actualType = getColumnType(sql);
    SqlCollation collation = actualType.getCollation();

    assertEquals(
        expectedCollationName, collation.getCollationName());
    assertEquals(expectedCoercibility, collation.getCoercibility());
  }
}
 
Example 5
Source File: RexBuilder.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Converts the type of a value to comply with
 * {@link org.apache.calcite.rex.RexLiteral#valueMatchesType}. */
private static Object clean(Object o, RelDataType type) {
  if (o == null) {
    return null;
  }
  switch (type.getSqlTypeName()) {
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case INTERVAL_YEAR:
  case INTERVAL_YEAR_MONTH:
  case INTERVAL_MONTH:
  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:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).longValue());
  case FLOAT:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).doubleValue(), MathContext.DECIMAL32)
        .stripTrailingZeros();
  case REAL:
  case DOUBLE:
    if (o instanceof BigDecimal) {
      return o;
    }
    return new BigDecimal(((Number) o).doubleValue(), MathContext.DECIMAL64)
        .stripTrailingZeros();
  case CHAR:
  case VARCHAR:
    if (o instanceof NlsString) {
      return o;
    }
    return new NlsString((String) o, type.getCharset().name(),
        type.getCollation());
  case TIME:
    if (o instanceof TimeString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return TimeString.fromCalendarFields((Calendar) o);
    } else {
      return TimeString.fromMillisOfDay((Integer) o);
    }
  case TIME_WITH_LOCAL_TIME_ZONE:
    if (o instanceof TimeString) {
      return o;
    } else {
      return TimeString.fromMillisOfDay((Integer) o);
    }
  case DATE:
    if (o instanceof DateString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return DateString.fromCalendarFields((Calendar) o);
    } else {
      return DateString.fromDaysSinceEpoch((Integer) o);
    }
  case TIMESTAMP:
    if (o instanceof TimestampString) {
      return o;
    } else if (o instanceof Calendar) {
      if (!((Calendar) o).getTimeZone().equals(DateTimeUtils.UTC_ZONE)) {
        throw new AssertionError();
      }
      return TimestampString.fromCalendarFields((Calendar) o);
    } else {
      return TimestampString.fromMillisSinceEpoch((Long) o);
    }
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    if (o instanceof TimestampString) {
      return o;
    } else {
      return TimestampString.fromMillisSinceEpoch((Long) o);
    }
  default:
    return o;
  }
}