Search in sources :

Example 1 with Converter

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);
}
Also used : QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) Converter(com.hazelcast.sql.impl.type.converter.Converter)

Example 2 with Converter

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);
}
Also used : HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) Converter(com.hazelcast.sql.impl.type.converter.Converter)

Example 3 with Converter

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);
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) LocalTime(java.time.LocalTime) TimeString(org.apache.calcite.util.TimeString) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) RexLiteral(org.apache.calcite.rex.RexLiteral) SqlLiteral(org.apache.calcite.sql.SqlLiteral) Literal(com.hazelcast.jet.sql.impl.validate.literal.Literal) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) Converter(com.hazelcast.sql.impl.type.converter.Converter) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteContextException(org.apache.calcite.runtime.CalciteContextException) InvocationTargetException(java.lang.reflect.InvocationTargetException) QueryException(com.hazelcast.sql.impl.QueryException) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 4 with Converter

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);
}
Also used : Converter(com.hazelcast.sql.impl.type.converter.Converter)

Example 5 with Converter

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);
}
Also used : Converter(com.hazelcast.sql.impl.type.converter.Converter)

Aggregations

Converter (com.hazelcast.sql.impl.type.converter.Converter)7 BigDecimalConverter (com.hazelcast.sql.impl.type.converter.BigDecimalConverter)2 BigIntegerConverter (com.hazelcast.sql.impl.type.converter.BigIntegerConverter)2 CalendarConverter (com.hazelcast.sql.impl.type.converter.CalendarConverter)2 CharacterConverter (com.hazelcast.sql.impl.type.converter.CharacterConverter)2 DateConverter (com.hazelcast.sql.impl.type.converter.DateConverter)2 InstantConverter (com.hazelcast.sql.impl.type.converter.InstantConverter)2 OffsetDateTimeConverter (com.hazelcast.sql.impl.type.converter.OffsetDateTimeConverter)2 StringConverter (com.hazelcast.sql.impl.type.converter.StringConverter)2 ZonedDateTimeConverter (com.hazelcast.sql.impl.type.converter.ZonedDateTimeConverter)2 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)1 Literal (com.hazelcast.jet.sql.impl.validate.literal.Literal)1 QueryException (com.hazelcast.sql.impl.QueryException)1 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)1 QueryDataTypeFamily (com.hazelcast.sql.impl.type.QueryDataTypeFamily)1 BooleanConverter (com.hazelcast.sql.impl.type.converter.BooleanConverter)1 ByteConverter (com.hazelcast.sql.impl.type.converter.ByteConverter)1 DoubleConverter (com.hazelcast.sql.impl.type.converter.DoubleConverter)1 FloatConverter (com.hazelcast.sql.impl.type.converter.FloatConverter)1 IntegerConverter (com.hazelcast.sql.impl.type.converter.IntegerConverter)1