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

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getCharset() . 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: SqlDialect.java    From Bats with Apache License 2.0 6 votes vote down vote up
public SqlNode getCastSpec(RelDataType type) {
  if (type instanceof BasicSqlType) {
    int precision = type.getPrecision();
    switch (type.getSqlTypeName()) {
    case VARCHAR:
      // if needed, adjust varchar length to max length supported by the system
      int maxPrecision = getTypeSystem().getMaxPrecision(type.getSqlTypeName());
      if (type.getPrecision() > maxPrecision) {
        precision = maxPrecision;
      }
    }
    return new SqlDataTypeSpec(
        new SqlIdentifier(type.getSqlTypeName().name(), SqlParserPos.ZERO),
            precision,
            type.getScale(),
            type.getCharset() != null
                && supportsCharSet()
                ? type.getCharset().name()
                : null,
            null,
            SqlParserPos.ZERO);
  }
  return SqlTypeUtil.convertTypeToSpec(type);
}
 
Example 3
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether two types both have different character sets. If one
 * or the other type has no character set (e.g. in cast from INT to
 * VARCHAR), that is not a mismatch.
 *
 * @param t1 first type
 * @param t2 second type
 * @return true iff mismatched
 */
public static boolean areCharacterSetsMismatched(
    RelDataType t1,
    RelDataType t2) {
  if (isAny(t1) || isAny(t2)) {
    return false;
  }

  Charset cs1 = t1.getCharset();
  Charset cs2 = t2.getCharset();
  if ((cs1 != null) && (cs2 != null)) {
    if (!cs1.equals(cs2)) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: SqlDialect.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns SqlNode for type in "cast(column as type)", which might be
* different between databases by type name, precision etc. */
public SqlNode getCastSpec(RelDataType type) {
  if (type instanceof BasicSqlType) {
    int maxPrecision = -1;
    switch (type.getSqlTypeName()) {
    case VARCHAR:
      // if needed, adjust varchar length to max length supported by the system
      maxPrecision = getTypeSystem().getMaxPrecision(type.getSqlTypeName());
    }
    String charSet = type.getCharset() != null && supportsCharSet()
        ? type.getCharset().name()
        : null;
    return SqlTypeUtil.convertTypeToSpec(type, charSet, maxPrecision);
  }
  return SqlTypeUtil.convertTypeToSpec(type);
}
 
Example 5
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 6
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether two types both have different character sets. If one
 * or the other type has no character set (e.g. in cast from INT to
 * VARCHAR), that is not a mismatch.
 *
 * @param t1 first type
 * @param t2 second type
 * @return true iff mismatched
 */
public static boolean areCharacterSetsMismatched(
    RelDataType t1,
    RelDataType t2) {
  if (isAny(t1) || isAny(t2)) {
    return false;
  }

  Charset cs1 = t1.getCharset();
  Charset cs2 = t2.getCharset();
  if ((cs1 != null) && (cs2 != null)) {
    if (!cs1.equals(cs2)) {
      return true;
    }
  }
  return false;
}
 
Example 7
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 8
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a type represents Unicode character data.
 *
 * @param type type to test
 * @return whether type represents Unicode character data
 */
public static boolean isUnicode(RelDataType type) {
  Charset charset = type.getCharset();
  if (charset == null) {
    return false;
  }
  return charset.name().startsWith("UTF");
}
 
Example 9
Source File: DremioSqlDialect.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected static SqlNode getVarcharWithPrecision(DremioSqlDialect dialect, RelDataType type, int precision) {
  return new SqlDataTypeSpec(
    new SqlIdentifier(type.getSqlTypeName().name(), SqlParserPos.ZERO),
    precision,
    type.getScale(),
    type.getCharset() != null && dialect.supportsCharSet()
      ? type.getCharset().name() : null,
    null,
    SqlParserPos.ZERO);
}
 
Example 10
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private SqlNode toSql(RelDataType type) {
  switch (dialect.getDatabaseProduct()) {
    case MYSQL:
      switch (type.getSqlTypeName()) {
        case VARCHAR:
          // MySQL doesn't have a VARCHAR type, only CHAR.
          return new SqlDataTypeSpec(new SqlIdentifier("CHAR", POS),
              type.getPrecision(), -1, null, null, POS);
        case INTEGER:
          return new SqlDataTypeSpec(new SqlIdentifier("_UNSIGNED", POS),
              type.getPrecision(), -1, null, null, POS);
      }
      break;
  }
  if (type instanceof BasicSqlType) {
    return new SqlDataTypeSpec(
        new SqlIdentifier(type.getSqlTypeName().name(), POS),
        type.getPrecision(),
        type.getScale(),
        type.getCharset() != null
            && dialect.supportsCharSet()
            ? type.getCharset().name()
            : null,
        null,
        POS);
  }

  return SqlTypeUtil.convertTypeToSpec(type);
  //throw new AssertionError(type); // TODO: implement
}
 
Example 11
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a type represents Unicode character data.
 *
 * @param type type to test
 * @return whether type represents Unicode character data
 */
public static boolean isUnicode(RelDataType type) {
  Charset charset = type.getCharset();
  if (charset == null) {
    return false;
  }
  return charset.name().startsWith("UTF");
}
 
Example 12
Source File: AbstractSqlTester.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void checkCharset(
    String expression,
    Charset expectedCharset) {
  for (String sql : buildQueries(expression)) {
    RelDataType actualType = getColumnType(sql);
    Charset actualCharset = actualType.getCharset();

    if (!expectedCharset.equals(actualCharset)) {
      fail("\n"
          + "Expected=" + expectedCharset.name() + "\n"
          + "  actual=" + actualCharset.name());
    }
  }
}