/** * Internal method to create a call to a literal. Code outside this package should call one of the * type-specific methods such as {@link #makeDateLiteral(Calendar)}, {@link * #makeLiteral(boolean)}, {@link #makeLiteral(String)}. * * @param o Value of literal, must be appropriate for the type * @param type Type of literal * @param typeName SQL type of literal * @return Literal */ protected RexLiteral makeLiteral(Comparable o, RelDataType type, SqlTypeName typeName) { // All literals except NULL have NOT NULL types. type = typeFactory.createTypeWithNullability(type, o == null); if (typeName == SqlTypeName.CHAR) { // Character literals must have a charset and collation. Populate // from the type if necessary. assert o instanceof NlsString; NlsString nlsString = (NlsString) o; if ((nlsString.getCollation() == null) || (nlsString.getCharset() == null)) { assert type.getSqlTypeName() == SqlTypeName.CHAR; assert type.getCharset().name() != null; assert type.getCollation() != null; o = new NlsString(nlsString.getValue(), type.getCharset().name(), type.getCollation()); } } return new RexLiteral(o, type, typeName); }
/** * 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; } final Calendar calendar; switch (type.getSqlTypeName()) { case TINYINT: case SMALLINT: case INTEGER: case BIGINT: case DECIMAL: case INTERVAL_YEAR_MONTH: case INTERVAL_DAY_TIME: if (o instanceof BigDecimal) { return o; } return new BigDecimal(((Number) o).longValue()); case FLOAT: case REAL: case DOUBLE: if (o instanceof BigDecimal) { return o; } return new BigDecimal(((Number) o).doubleValue()); 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 Calendar) { return o; } calendar = Calendar.getInstance(DateTimeUtils.GMT_ZONE); calendar.setTimeInMillis((Integer) o); return calendar; case DATE: if (o instanceof Calendar) { return o; } calendar = Calendar.getInstance(DateTimeUtils.GMT_ZONE); calendar.setTimeInMillis(0); calendar.add(Calendar.DAY_OF_YEAR, (Integer) o); return calendar; case TIMESTAMP: if (o instanceof Calendar) { return o; } calendar = Calendar.getInstance(DateTimeUtils.GMT_ZONE); calendar.setTimeInMillis((Long) o); return calendar; default: return o; } }
public static void checkCharsetAndCollateConsistentIfCharType(RelDataType type) { // (every charset must have a default collation) if (SqlTypeUtil.inCharFamily(type)) { Charset strCharset = type.getCharset(); Charset colCharset = type.getCollation().getCharset(); assert null != strCharset; assert null != colCharset; if (!strCharset.equals(colCharset)) { if (false) { // todo: enable this checking when we have a charset to // collation mapping throw new Error( type.toString() + " was found to have charset '" + strCharset.name() + "' and a mismatched collation charset '" + colCharset.name() + "'"); } } } }