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

The following examples show how to use org.apache.calcite.sql.SqlCollation. These examples are extracted from open source projects. 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 Project: Bats   Source File: BasicSqlType.java    License: 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 2
Source Project: Bats   Source File: SqlTypeUtil.java    License: 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 Project: Bats   Source File: SqlTypeFactoryImpl.java    License: 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 4
Source Project: Mycat2   Source File: CalciteConvertors.java    License: 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 5
Source Project: calcite   Source File: EnumUtils.java    License: 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 6
Source Project: calcite   Source File: AbstractTypeCoercion.java    License: 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 7
Source Project: calcite   Source File: BasicSqlType.java    License: 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 8
Source Project: calcite   Source File: SqlTypeUtil.java    License: 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 9
Source Project: calcite   Source File: SqlTypeFactoryImpl.java    License: 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 10
Source Project: calcite   Source File: EnumerableStringComparisonTest.java    License: 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 11
Source Project: Bats   Source File: RelDataTypeFactoryImpl.java    License: 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 12
Source Project: Bats   Source File: RelDataTypeFactoryImpl.java    License: 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 13
Source Project: Bats   Source File: BasicSqlType.java    License: 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 Project: Bats   Source File: NlsString.java    License: 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 15
Source Project: Bats   Source File: RexLiteralImpl.java    License: 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 16
Source Project: calcite   Source File: RelDataTypeFactoryImpl.java    License: 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 17
Source Project: calcite   Source File: RelDataTypeFactoryImpl.java    License: 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 18
Source Project: calcite   Source File: SqlAttributeDefinition.java    License: 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 19
Source Project: calcite   Source File: BasicSqlType.java    License: 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 20
Source Project: calcite   Source File: BasicSqlType.java    License: 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 21
Source Project: calcite   Source File: NlsString.java    License: 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 22
Source Project: calcite   Source File: AbstractSqlTester.java    License: 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 23
Source Project: calcite   Source File: UtilTest.java    License: 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 24
Source Project: Bats   Source File: RelDataTypeImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlCollation getCollation() {
  return null;
}
 
Example 25
Source Project: Bats   Source File: RelDataTypeFactoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlCollation getCollation() {
  return this.collation;
}
 
Example 26
Source Project: Bats   Source File: SqlLiteralChainOperator.java    License: 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));
      } else {
        // print without prefix
        writer.literal(nls.asSql(false, false));
      }
    } 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, 0, 0);
  }
  writer.endList(frame);
}
 
Example 27
Source Project: Bats   Source File: BasicSqlType.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlCollation getCollation() {
  return collation;
}
 
Example 28
Source Project: Bats   Source File: BasicSqlType.java    License: 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 29
Source Project: Bats   Source File: NlsString.java    License: Apache License 2.0 4 votes vote down vote up
public SqlCollation getCollation() {
  return collation;
}
 
Example 30
Source Project: dremio-oss   Source File: SqlVarCharStringLiteral.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return the collation
 */
public SqlCollation getCollation() {
  return getNlsString().getCollation();
}