Java Code Examples for org.apache.calcite.sql.SqlCollation#IMPLICIT

The following examples show how to use org.apache.calcite.sql.SqlCollation#IMPLICIT . 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: 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 3
Source File: RelDataTypeFactoryImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType createJavaType(Class clazz) {
  final JavaType javaType =
      clazz == String.class
          ? new JavaType(clazz, true, getDefaultCharset(),
              SqlCollation.IMPLICIT)
          : new JavaType(clazz);
  return canonize(javaType);
}
 
Example 4
Source File: RelDataTypeFactoryImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType createJavaType(Class clazz) {
  final JavaType javaType =
      clazz == String.class
          ? new JavaType(clazz, true, getDefaultCharset(),
              SqlCollation.IMPLICIT)
          : new JavaType(clazz);
  return canonize(javaType);
}
 
Example 5
Source File: BasicSqlType.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
  // Called to make the digest, which equals() compares;
  // so equivalent data types must produce identical type strings.

  sb.append(typeName.name());
  boolean printPrecision = precision != PRECISION_NOT_SPECIFIED;
  boolean printScale = scale != SCALE_NOT_SPECIFIED;

  if (printPrecision) {
    sb.append('(');
    sb.append(getPrecision());
    if (printScale) {
      sb.append(", ");
      sb.append(getScale());
    }
    sb.append(')');
  }
  if (!withDetail) {
    return;
  }
  if (wrappedCharset != null
      && !SqlCollation.IMPLICIT.getCharset().equals(wrappedCharset.getCharset())) {
    sb.append(" CHARACTER SET \"");
    sb.append(wrappedCharset.getCharset().name());
    sb.append("\"");
  }
  if (collation != null
      && collation != SqlCollation.IMPLICIT && collation != SqlCollation.COERCIBLE) {
    sb.append(" COLLATE \"");
    sb.append(collation.getCollationName());
    sb.append("\"");
  }
}
 
Example 6
Source File: UtilTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testNlsStringClone() {
  final NlsString s = new NlsString("foo", "LATIN1", SqlCollation.IMPLICIT);
  assertThat(s.toString(), is("_LATIN1'foo'"));
  final Object s2 = s.clone();
  assertThat(s2, instanceOf(NlsString.class));
  assertThat(s2, not(sameInstance((Object) s)));
  assertThat(s2.toString(), is(s.toString()));
}
 
Example 7
Source File: BasicSqlType.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected void generateTypeString(StringBuilder sb, boolean withDetail) {
  // Called to make the digest, which equals() compares;
  // so equivalent data types must produce identical type strings.

  sb.append(typeName.name());
  boolean printPrecision = precision != PRECISION_NOT_SPECIFIED;
  boolean printScale = scale != SCALE_NOT_SPECIFIED;

  // for the digest, print the precision when defaulted,
  // since (for instance) TIME is equivalent to TIME(0).
  if (withDetail) {
    // -1 means there is no default value for precision
    if (typeName.allowsPrec()
        && typeSystem.getDefaultPrecision(typeName) > -1) {
      printPrecision = true;
    }
    if (typeName.getDefaultScale() > -1) {
      printScale = true;
    }
  }

  if (printPrecision) {
    sb.append('(');
    sb.append(getPrecision());
    if (printScale) {
      sb.append(", ");
      sb.append(getScale());
    }
    sb.append(')');
  }
  if (!withDetail) {
    return;
  }
  if (wrappedCharset != null
      && !SqlCollation.IMPLICIT.getCharset().equals(wrappedCharset.getCharset())) {
    sb.append(" CHARACTER SET \"");
    sb.append(wrappedCharset.getCharset().name());
    sb.append("\"");
  }
  if (collation != null
      && collation != SqlCollation.IMPLICIT && collation != SqlCollation.COERCIBLE) {
    sb.append(" COLLATE \"");
    sb.append(collation.getCollationName());
    sb.append("\"");
  }
}
 
Example 8
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Test string literal encoding.
 */
@Test void testStringLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType varchar =
      typeFactory.createSqlType(SqlTypeName.VARCHAR);
  final RexBuilder builder = new RexBuilder(typeFactory);

  final NlsString latin1 = new NlsString("foobar", "LATIN1", SqlCollation.IMPLICIT);
  final NlsString utf8 = new NlsString("foobar", "UTF8", SqlCollation.IMPLICIT);

  RexNode literal = builder.makePreciseStringLiteral("foobar");
  assertEquals("'foobar'", literal.toString());
  literal = builder.makePreciseStringLiteral(
      new ByteString(new byte[] { 'f', 'o', 'o', 'b', 'a', 'r'}),
      "UTF8",
      SqlCollation.IMPLICIT);
  assertEquals("_UTF8'foobar'", literal.toString());
  assertEquals("_UTF8'foobar':CHAR(6) CHARACTER SET \"UTF-8\"",
      ((RexLiteral) literal).computeDigest(RexDigestIncludeType.ALWAYS));
  literal = builder.makePreciseStringLiteral(
      new ByteString("\u82f1\u56fd".getBytes(StandardCharsets.UTF_8)),
      "UTF8",
      SqlCollation.IMPLICIT);
  assertEquals("_UTF8'\u82f1\u56fd'", literal.toString());
  // Test again to check decode cache.
  literal = builder.makePreciseStringLiteral(
      new ByteString("\u82f1".getBytes(StandardCharsets.UTF_8)),
      "UTF8",
      SqlCollation.IMPLICIT);
  assertEquals("_UTF8'\u82f1'", literal.toString());
  try {
    literal = builder.makePreciseStringLiteral(
        new ByteString("\u82f1\u56fd".getBytes(StandardCharsets.UTF_8)),
        "GB2312",
        SqlCollation.IMPLICIT);
    fail("expected exception, got " + literal);
  } catch (RuntimeException e) {
    assertThat(e.getMessage(), containsString("Failed to encode"));
  }
  literal = builder.makeLiteral(latin1, varchar, false);
  assertEquals("_LATIN1'foobar'", literal.toString());
  literal = builder.makeLiteral(utf8, varchar, false);
  assertEquals("_UTF8'foobar'", literal.toString());
}