org.apache.calcite.sql.SqlCollation Java Examples

The following examples show how to use org.apache.calcite.sql.SqlCollation. 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: EnumerableStringComparisonTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void testStringComparison(String str1, String str2,
                                  SqlOperator operator, SqlCollation col,
                                  boolean expectedResult) {
  tester()
      .query("?")
      .withRel(builder -> {
        final RexBuilder rexBuilder = builder.getRexBuilder();
        final RelDataType varcharSpecialCollation = createVarcharSpecialCollation(builder, col);
        return builder
            .values(new String[]{"aux"}, false)
            .project(
                Collections.singletonList(
                    builder.call(
                        operator,
                        rexBuilder.makeCast(varcharSpecialCollation, builder.literal(str1)),
                        rexBuilder.makeCast(varcharSpecialCollation, builder.literal(str2)))),
                Collections.singletonList("result"))
            .build();
      })
      .returnsUnordered("result=" + expectedResult);
}
 
Example #4
Source File: SqlTypeFactoryImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDataType createTypeWithCharsetAndCollation(
    RelDataType type,
    Charset charset,
    SqlCollation collation) {
  assert SqlTypeUtil.inCharFamily(type) : type;
  assert charset != null;
  assert collation != null;
  RelDataType newType;
  if (type instanceof BasicSqlType) {
    BasicSqlType sqlType = (BasicSqlType) type;
    newType = sqlType.createWithCharsetAndCollation(charset, collation);
  } else if (type instanceof JavaType) {
    JavaType javaType = (JavaType) type;
    newType =
        new JavaType(
            javaType.getJavaClass(),
            javaType.isNullable(),
            charset,
            collation);
  } else {
    throw Util.needToImplement("need to implement " + type);
  }
  return canonize(newType);
}
 
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: BasicSqlType.java    From Bats with Apache License 2.0 6 votes vote down vote up
/** Internal constructor. */
private BasicSqlType(
    RelDataTypeSystem typeSystem,
    SqlTypeName typeName,
    boolean nullable,
    int precision,
    int scale,
    SqlCollation collation,
    SerializableCharset wrappedCharset) {
  super(typeName, nullable, null);
  this.typeSystem = Objects.requireNonNull(typeSystem);
  this.precision = precision;
  this.scale = scale;
  this.collation = collation;
  this.wrappedCharset = wrappedCharset;
  computeDigest();
}
 
Example #7
Source File: CalciteConvertors.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public static RelDataType getRelDataType(final List<SimpleColumnInfo> columnInfos, final RelDataTypeFactory factory) {
    final RelDataTypeFactory.Builder builder = new RelDataTypeFactory.Builder(factory);
    for (SimpleColumnInfo columnInfo : columnInfos) {
        final JDBCType columnType = columnInfo.getJdbcType();
        final RelDataType type;
        if (columnType == JDBCType.VARCHAR) {
            type = factory.createTypeWithCharsetAndCollation(
                    factory.createSqlType(SqlTypeName.VARCHAR),
                    Charset.defaultCharset(),
                    SqlCollation.IMPLICIT);
        } else if (columnType == JDBCType.LONGVARBINARY) {
            type = factory.createSqlType(SqlTypeName.VARBINARY);
        } else {
            SqlTypeName sqlTypeName = SqlTypeName.getNameForJdbcType(columnType.getVendorTypeNumber());
            if (sqlTypeName == null) {
                sqlTypeName = SqlTypeName.VARCHAR;
            }
            type = factory.createSqlType(sqlTypeName);
        }
        builder.add(columnInfo.getColumnName(), factory.createTypeWithNullability(type, columnInfo.isNullable()));
    }
    return builder.build();
}
 
Example #8
Source File: EnumUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static Expression generateCollatorExpression(SqlCollation collation) {
  if (collation == null || collation.getCollator() == null) {
    return null;
  }

  // Utilities.generateCollator(
  //      new Locale(
  //          collation.getLocale().getLanguage(),
  //          collation.getLocale().getCountry(),
  //          collation.getLocale().getVariant()),
  //      collation.getCollator().getStrength());
  final Locale locale = collation.getLocale();
  final int strength = collation.getCollator().getStrength();
  return Expressions.call(
      Utilities.class,
      "generateCollator",
      Expressions.new_(
          Locale.class,
          Expressions.constant(locale.getLanguage()),
          Expressions.constant(locale.getCountry()),
          Expressions.constant(locale.getVariant())),
      Expressions.constant(strength));
}
 
Example #9
Source File: BasicSqlType.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Internal constructor. */
private BasicSqlType(
    RelDataTypeSystem typeSystem,
    SqlTypeName typeName,
    boolean nullable,
    int precision,
    int scale,
    SqlCollation collation,
    SerializableCharset wrappedCharset) {
  super(typeName, nullable, null);
  this.typeSystem = Objects.requireNonNull(typeSystem);
  this.precision = precision;
  this.scale = scale;
  this.collation = collation;
  this.wrappedCharset = wrappedCharset;
  computeDigest();
}
 
Example #10
Source File: SqlTypeFactoryImpl.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelDataType createTypeWithCharsetAndCollation(
    RelDataType type,
    Charset charset,
    SqlCollation collation) {
  assert SqlTypeUtil.inCharFamily(type) : type;
  assert charset != null;
  assert collation != null;
  RelDataType newType;
  if (type instanceof BasicSqlType) {
    BasicSqlType sqlType = (BasicSqlType) type;
    newType = sqlType.createWithCharsetAndCollation(charset, collation);
  } else if (type instanceof JavaType) {
    JavaType javaType = (JavaType) type;
    newType =
        new JavaType(
            javaType.getJavaClass(),
            javaType.isNullable(),
            charset,
            collation);
  } else {
    throw Util.needToImplement("need to implement " + type);
  }
  return canonize(newType);
}
 
Example #11
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 #12
Source File: NlsString.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Internal constructor; other constructors must call it. */
private NlsString(String stringValue, ByteString bytesValue,
    String charsetName, SqlCollation collation) {
  if (charsetName != null) {
    this.charsetName = charsetName.toUpperCase(Locale.ROOT);
    this.charset = SqlUtil.getCharset(charsetName);
  } else {
    this.charsetName = null;
    this.charset = null;
  }
  if ((stringValue != null) == (bytesValue != null)) {
    throw new IllegalArgumentException("Specify stringValue or bytesValue");
  }
  if (bytesValue != null) {
    if (charsetName == null) {
      throw new IllegalArgumentException("Bytes value requires charset");
    }
    SqlUtil.validateCharset(bytesValue, charset);
  } else {
    // Java string can be malformed if LATIN1 is required.
    if (this.charsetName != null
        && (this.charsetName.equals("LATIN1")
        || this.charsetName.equals("ISO-8859-1"))) {
      if (!charset.newEncoder().canEncode(stringValue)) {
        throw RESOURCE.charsetEncoding(stringValue, charset.name()).ex();
      }
    }
  }
  this.collation = collation;
  this.stringValue = stringValue;
  this.bytesValue = bytesValue;
}
 
Example #13
Source File: BasicSqlType.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a type with charset and collation.
 *
 * <p>This must be a character type.
 */
BasicSqlType createWithCharsetAndCollation(Charset charset,
    SqlCollation collation) {
  Preconditions.checkArgument(SqlTypeUtil.inCharFamily(this));
  return new BasicSqlType(this.typeSystem, this.typeName, this.isNullable,
      this.precision, this.scale, collation,
      SerializableCharset.forCharset(charset));
}
 
Example #14
Source File: RexLiteralImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Computes if data type can be omitted from the digset.
 * <p>For instance, {@code 1:BIGINT} has to keep data type while {@code 1:INT}
 * should be represented as just {@code 1}.
 *
 * <p>Implementation assumption: this method should be fast. In fact might call
 * {@link NlsString#getValue()} which could decode the string, however we rely on the cache there.
 *
 * @see RexLiteral#computeDigest(RexDigestIncludeType)
 * @param value value of the literal
 * @param type type of the literal
 * @return NO_TYPE when type can be omitted, ALWAYS otherwise
 */
private static RexDigestIncludeType shouldIncludeType(Comparable value, RelDataType type) {
    if (type.isNullable()) {
        // This means "null literal", so we require a type for it
        // There might be exceptions like AND(null, true) which are handled by RexCall#computeDigest
        return RexDigestIncludeType.ALWAYS;
    }
    // The variable here simplifies debugging (one can set a breakpoint at return)
    // final ensures we set the value in all the branches, and it ensures the value is set just once
    final RexDigestIncludeType includeType;
    if (type.getSqlTypeName() == SqlTypeName.BOOLEAN || type.getSqlTypeName() == SqlTypeName.INTEGER
            || type.getSqlTypeName() == SqlTypeName.SYMBOL) {
        // We don't want false:BOOLEAN NOT NULL, so we don't print type information for
        // non-nullable BOOLEAN and INTEGER
        includeType = RexDigestIncludeType.NO_TYPE;
    } else if (type.getSqlTypeName() == SqlTypeName.CHAR && value instanceof NlsString) {
        NlsString nlsString = (NlsString) value;

        // Ignore type information for 'Bar':CHAR(3)
        if (((nlsString.getCharset() != null && type.getCharset().equals(nlsString.getCharset()))
                || (nlsString.getCharset() == null && SqlCollation.IMPLICIT.getCharset().equals(type.getCharset())))
                && nlsString.getCollation().equals(type.getCollation())
                && ((NlsString) value).getValue().length() == type.getPrecision()) {
            includeType = RexDigestIncludeType.NO_TYPE;
        } else {
            includeType = RexDigestIncludeType.ALWAYS;
        }
    } else if (type.getPrecision() == 0 && (type.getSqlTypeName() == SqlTypeName.TIME
            || type.getSqlTypeName() == SqlTypeName.TIMESTAMP || type.getSqlTypeName() == SqlTypeName.DATE)) {
        // Ignore type information for '12:23:20':TIME(0)
        // Note that '12:23:20':TIME WITH LOCAL TIME ZONE
        includeType = RexDigestIncludeType.NO_TYPE;
    } else {
        includeType = RexDigestIncludeType.ALWAYS;
    }
    return includeType;
}
 
Example #15
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 #16
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 #17
Source File: NlsString.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Internal constructor; other constructors must call it. */
private NlsString(String stringValue, ByteString bytesValue,
    String charsetName, SqlCollation collation) {
  if (charsetName != null) {
    this.charsetName = charsetName.toUpperCase(Locale.ROOT);
    this.charset = SqlUtil.getCharset(charsetName);
  } else {
    this.charsetName = null;
    this.charset = null;
  }
  if ((stringValue != null) == (bytesValue != null)) {
    throw new IllegalArgumentException("Specify stringValue or bytesValue");
  }
  if (bytesValue != null) {
    if (charsetName == null) {
      throw new IllegalArgumentException("Bytes value requires charset");
    }
    SqlUtil.validateCharset(bytesValue, charset);
  } else {
    // Java string can be malformed if LATIN1 is required.
    if (this.charsetName != null
        && (this.charsetName.equals("LATIN1")
        || this.charsetName.equals("ISO-8859-1"))) {
      if (!charset.newEncoder().canEncode(stringValue)) {
        throw RESOURCE.charsetEncoding(stringValue, charset.name()).ex();
      }
    }
  }
  this.collation = collation;
  this.stringValue = stringValue;
  this.bytesValue = bytesValue;
}
 
Example #18
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 #19
Source File: RelDataTypeFactoryImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public JavaType(
    Class clazz,
    boolean nullable,
    Charset charset,
    SqlCollation collation) {
  super(fieldsOf(clazz));
  this.clazz = clazz;
  this.nullable = nullable;
  assert (charset != null) == SqlTypeUtil.inCharFamily(this)
      : "Need to be a chartype";
  this.charset = charset;
  this.collation = collation;
  computeDigest();
}
 
Example #20
Source File: BasicSqlType.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a type with charset and collation.
 *
 * <p>This must be a character type.
 */
BasicSqlType createWithCharsetAndCollation(Charset charset,
    SqlCollation collation) {
  Preconditions.checkArgument(SqlTypeUtil.inCharFamily(this));
  return new BasicSqlType(this.typeSystem, this.typeName, this.isNullable,
      this.precision, this.scale, collation,
      SerializableCharset.forCharset(charset));
}
 
Example #21
Source File: SqlAttributeDefinition.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a SqlAttributeDefinition; use {@link SqlDdlNodes#attribute}. */
SqlAttributeDefinition(SqlParserPos pos, SqlIdentifier name,
    SqlDataTypeSpec dataType, SqlNode expression, SqlCollation collation) {
  super(pos);
  this.name = name;
  this.dataType = dataType;
  this.expression = expression;
  this.collation = collation;
}
 
Example #22
Source File: RelDataTypeFactoryImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
public JavaType(
    Class clazz,
    boolean nullable,
    Charset charset,
    SqlCollation collation) {
  super(fieldsOf(clazz));
  this.clazz = clazz;
  this.nullable = nullable;
  assert (charset != null) == SqlTypeUtil.inCharFamily(this)
      : "Need to be a chartype";
  this.charset = charset;
  this.collation = collation;
  computeDigest();
}
 
Example #23
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 #24
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void unparse(
    SqlWriter writer,
    SqlCall call,
    int leftPrec,
    int rightPrec) {
  final SqlWriter.Frame frame = writer.startList("", "");
  SqlCollation collation = null;
  for (Ord<SqlNode> operand : Ord.zip(call.getOperandList())) {
    SqlLiteral rand = (SqlLiteral) operand.e;
    if (operand.i > 0) {
      // SQL:2003 says there must be a newline between string
      // fragments.
      writer.newlineAndIndent();
    }
    if (rand instanceof SqlCharStringLiteral) {
      NlsString nls = ((SqlCharStringLiteral) rand).getNlsString();
      if (operand.i == 0) {
        collation = nls.getCollation();

        // print with prefix
        writer.literal(nls.asSql(true, false, writer.getDialect()));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false, writer.getDialect()));
      }
    } else if (operand.i == 0) {
      // print with prefix
      rand.unparse(writer, leftPrec, rightPrec);
    } else {
      // print without prefix
      if (rand.getTypeName() == SqlTypeName.BINARY) {
        BitString bs = (BitString) rand.getValue();
        writer.literal("'" + bs.toHexString() + "'");
      } else {
        writer.literal("'" + rand.toValue() + "'");
      }
    }
  }
  if (collation != null) {
    collation.unparse(writer);
  }
  writer.endList(frame);
}
 
Example #25
Source File: RexLiteral.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Computes if data type can be omitted from the digset.
 * <p>For instance, {@code 1:BIGINT} has to keep data type while {@code 1:INT}
 * should be represented as just {@code 1}.
 *
 * <p>Implementation assumption: this method should be fast. In fact might call
 * {@link NlsString#getValue()} which could decode the string, however we rely on the cache there.
 *
 * @see RexLiteral#computeDigest(RexDigestIncludeType)
 * @param value value of the literal
 * @param type type of the literal
 * @return NO_TYPE when type can be omitted, ALWAYS otherwise
 */
private static RexDigestIncludeType shouldIncludeType(Comparable value, RelDataType type) {
  if (type.isNullable()) {
    // This means "null literal", so we require a type for it
    // There might be exceptions like AND(null, true) which are handled by RexCall#computeDigest
    return RexDigestIncludeType.ALWAYS;
  }
  // The variable here simplifies debugging (one can set a breakpoint at return)
  // final ensures we set the value in all the branches, and it ensures the value is set just once
  final RexDigestIncludeType includeType;
  if (type.getSqlTypeName() == SqlTypeName.BOOLEAN
      || type.getSqlTypeName() == SqlTypeName.INTEGER
      || type.getSqlTypeName() == SqlTypeName.SYMBOL) {
    // We don't want false:BOOLEAN NOT NULL, so we don't print type information for
    // non-nullable BOOLEAN and INTEGER
    includeType = RexDigestIncludeType.NO_TYPE;
  } else if (type.getSqlTypeName() == SqlTypeName.CHAR
          && value instanceof NlsString) {
    NlsString nlsString = (NlsString) value;

    // Ignore type information for 'Bar':CHAR(3)
    if ((
        (nlsString.getCharset() != null && type.getCharset().equals(nlsString.getCharset()))
        || (nlsString.getCharset() == null
        && SqlCollation.IMPLICIT.getCharset().equals(type.getCharset())))
        && nlsString.getCollation().equals(type.getCollation())
        && ((NlsString) value).getValue().length() == type.getPrecision()) {
      includeType = RexDigestIncludeType.NO_TYPE;
    } else {
      includeType = RexDigestIncludeType.ALWAYS;
    }
  } else if (type.getPrecision() == 0 && (
             type.getSqlTypeName() == SqlTypeName.TIME
          || type.getSqlTypeName() == SqlTypeName.TIMESTAMP
          || type.getSqlTypeName() == SqlTypeName.DATE)) {
    // Ignore type information for '12:23:20':TIME(0)
    // Note that '12:23:20':TIME WITH LOCAL TIME ZONE
    includeType = RexDigestIncludeType.NO_TYPE;
  } else {
    includeType = RexDigestIncludeType.ALWAYS;
  }
  return includeType;
}
 
Example #26
Source File: BasicSqlType.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlCollation getCollation() {
  return collation;
}
 
Example #27
Source File: NlsString.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlCollation getCollation() {
  return collation;
}
 
Example #28
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());
}
 
Example #29
Source File: SqlValidatorTestCase.java    From calcite with Apache License 2.0 4 votes vote down vote up
void checkCollation(
String sql,
String expectedCollationName,
SqlCollation.Coercibility expectedCoercibility);
 
Example #30
Source File: SqlValidatorTestCase.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void collation(String expectedCollationName,
    SqlCollation.Coercibility expectedCoercibility) {
  tester.checkCollation(sql, expectedCollationName, expectedCoercibility);
}