use of com.hazelcast.sql.impl.type.converter.Converter in project hazelcast by hazelcast.
the class HazelcastRexBuilder method makeLiteral.
@Override
public RexNode makeLiteral(Object value, RelDataType type, boolean allowCast) {
if (type.getSqlTypeName() == ANY && value instanceof Number) {
Converter converter = Converters.getConverter(value.getClass());
if (converter != null) {
QueryDataTypeFamily typeFamily = converter.getTypeFamily();
if (typeFamily.isNumericInteger()) {
int bitWidth = HazelcastIntegerType.bitWidthOf(((Number) value).longValue());
type = HazelcastIntegerType.create(bitWidth, false);
} else {
SqlTypeName typeName = HazelcastTypeUtils.toCalciteType(typeFamily);
type = HazelcastTypeFactory.INSTANCE.createSqlType(typeName);
}
}
}
return super.makeLiteral(value, type, allowCast);
}
use of com.hazelcast.sql.impl.type.converter.Converter in project hazelcast by hazelcast.
the class JsonCreationUtil method serializeValue.
public static String serializeValue(final Object value) {
if (value == null) {
return "null";
}
if (value instanceof HazelcastJsonValue) {
return value.toString();
}
final Converter converter = Converters.getConverter(value.getClass());
final Object result;
switch(converter.getTypeFamily()) {
case TIME:
case DATE:
case TIMESTAMP:
case TIMESTAMP_WITH_TIME_ZONE:
result = converter.asVarchar(value);
break;
default:
result = value;
}
return SERIALIZER.toJson(result);
}
use of com.hazelcast.sql.impl.type.converter.Converter in project hazelcast by hazelcast.
the class HazelcastSqlToRelConverter method convertCast.
/**
* Convert CAST expression fixing several Apache Calcite problems with literals along the way (see inline JavaDoc).
*/
private RexNode convertCast(SqlCall call, Blackboard blackboard) {
SqlNode operand = call.operand(0);
RexNode convertedOperand = blackboard.convertExpression(operand);
RelDataType from = validator.getValidatedNodeType(operand);
RelDataType to = validator.getValidatedNodeType(call);
QueryDataType fromType = HazelcastTypeUtils.toHazelcastType(from);
QueryDataType toType = HazelcastTypeUtils.toHazelcastType(to);
Literal literal = LiteralUtils.literal(convertedOperand);
if (literal != null && ((RexLiteral) convertedOperand).getTypeName() != SqlTypeName.NULL) {
// types to ensure that we throw consistent error messages for all literal-related conversions errors.
try {
// The literal's type might be different from the operand type for example here:
// CAST(CAST(42 AS SMALLINT) AS TINYINT)
// The operand of the outer cast is validated as a SMALLINT, however the operand, thanks to the
// simplification in RexBuilder.makeCast(), is converted to a literal [42:SMALLINT]. And LiteralUtils converts
// this operand to [42:TINYINT] - we have to use the literal's type instead of the validated operand type.
QueryDataType actualFromType = HazelcastTypeUtils.toHazelcastTypeFromSqlTypeName(literal.getTypeName());
toType.getConverter().convertToSelf(actualFromType.getConverter(), literal.getValue());
} catch (Exception e) {
throw literalConversionException(validator, call, literal, toType, e);
}
// DOUBLE literals are converted to a string with scientific conventions (e.g., 1.1E1 instead of 11.0);
if (SqlTypeName.CHAR_TYPES.contains(to.getSqlTypeName())) {
return getRexBuilder().makeLiteral(literal.getStringValue(), to, true);
}
// To workaround the problem, we perform the conversion manually.
if (SqlTypeName.CHAR_TYPES.contains(from.getSqlTypeName()) && to.getSqlTypeName() == SqlTypeName.TIME) {
LocalTime time = fromType.getConverter().asTime(literal.getStringValue());
TimeString timeString = new TimeString(time.getHour(), time.getMinute(), time.getSecond());
return getRexBuilder().makeLiteral(timeString, to, true);
}
// be "true". See CastFunctionIntegrationTest.testApproximateTypeSimplification - it will fail without this fix.
if (fromType.getTypeFamily().isNumeric()) {
if (toType.getTypeFamily().isNumericApproximate()) {
Converter converter = Converters.getConverter(literal.getValue().getClass());
Object convertedValue = toType.getConverter().convertToSelf(converter, literal.getValue());
return getRexBuilder().makeLiteral(convertedValue, to, false);
}
}
}
if (literal != null && HazelcastTypeUtils.isJsonType(to)) {
return getRexBuilder().makeCall(HazelcastJsonParseFunction.INSTANCE, convertedOperand);
}
// Delegate to Apache Calcite.
return getRexBuilder().makeCast(to, convertedOperand);
}
use of com.hazelcast.sql.impl.type.converter.Converter in project hazelcast by hazelcast.
the class CastExpression method eval.
@SuppressWarnings("unchecked")
@Override
public T eval(Row row, ExpressionEvalContext context) {
Object value = operand.eval(row, context);
if (value == null) {
return null;
}
Converter fromConverter = operand.getType().getConverter();
Converter toConverter = resultType.getConverter();
return (T) toConverter.convertToSelf(fromConverter, value);
}
use of com.hazelcast.sql.impl.type.converter.Converter in project hazelcast by hazelcast.
the class ConstantExpression method create.
public static ConstantExpression<?> create(Object value, QueryDataType type) {
if (value == null) {
return new ConstantExpression<>(null, type);
}
assert type.getTypeFamily() != QueryDataTypeFamily.NULL;
Converter valueConverter = Converters.getConverter(value.getClass());
Converter typeConverter = type.getConverter();
value = typeConverter.convertToSelf(valueConverter, value);
return new ConstantExpression<>(value, type);
}
Aggregations