Search in sources :

Example 1 with TimeType

use of com.facebook.presto.common.type.TimeType in project presto by prestodb.

the class LiteralInterpreter method evaluate.

public static Object evaluate(ConnectorSession session, ConstantExpression node) {
    Type type = node.getType();
    SqlFunctionProperties properties = session.getSqlFunctionProperties();
    if (node.getValue() == null) {
        return null;
    }
    if (type instanceof BooleanType) {
        return node.getValue();
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return node.getValue();
    }
    if (type instanceof DoubleType) {
        return node.getValue();
    }
    if (type instanceof RealType) {
        Long number = (Long) node.getValue();
        return intBitsToFloat(number.intValue());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(node.getValue() instanceof Long);
            return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
        }
        checkState(node.getValue() instanceof Slice);
        Slice value = (Slice) node.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType);
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) node.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return new SqlVarbinary(((Slice) node.getValue()).getBytes());
    }
    if (type instanceof DateType) {
        return new SqlDate(((Long) node.getValue()).intValue());
    }
    if (type instanceof TimeType) {
        if (properties.isLegacyTimestamp()) {
            return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
        }
        return new SqlTime((long) node.getValue());
    }
    if (type instanceof TimestampType) {
        try {
            if (properties.isLegacyTimestamp()) {
                return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
            }
            return new SqlTimestamp((long) node.getValue());
        } catch (RuntimeException e) {
            throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
        }
    }
    if (type instanceof IntervalDayTimeType) {
        return new SqlIntervalDayTime((long) node.getValue());
    }
    if (type instanceof IntervalYearMonthType) {
        return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
    }
    if (type.getJavaType().equals(Slice.class)) {
        // DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
        return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
    }
    // We should not fail at the moment; just return the raw value (block, regex, etc) to the user
    return node.getValue();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 2 with TimeType

use of com.facebook.presto.common.type.TimeType in project presto by prestodb.

the class ExpressionConverter method getIcebergLiteralValue.

private static Object getIcebergLiteralValue(Type type, Marker marker) {
    if (type instanceof IntegerType) {
        return toIntExact((long) marker.getValue());
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact((long) marker.getValue()));
    }
    // TODO: Remove this conversion once we move to next iceberg version
    if (type instanceof DateType) {
        return toIntExact(((Long) marker.getValue()));
    }
    if (type instanceof TimestampType || type instanceof TimeType) {
        return MILLISECONDS.toMicros((Long) marker.getValue());
    }
    if (type instanceof VarcharType) {
        return ((Slice) marker.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return ByteBuffer.wrap(((Slice) marker.getValue()).getBytes());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        Object value = requireNonNull(marker.getValue(), "The value of the marker must be non-null");
        if (Decimals.isShortDecimal(decimalType)) {
            checkArgument(value instanceof Long, "A short decimal should be represented by a Long value but was %s", value.getClass().getName());
            return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
        }
        checkArgument(value instanceof Slice, "A long decimal should be represented by a Slice value but was %s", value.getClass().getName());
        return new BigDecimal(Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
    }
    return marker.getValue();
}
Also used : IntegerType(com.facebook.presto.common.type.IntegerType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) RealType(com.facebook.presto.common.type.RealType) DateType(com.facebook.presto.common.type.DateType) BigDecimal(java.math.BigDecimal) TimeType(com.facebook.presto.common.type.TimeType)

Aggregations

DateType (com.facebook.presto.common.type.DateType)2 DecimalType (com.facebook.presto.common.type.DecimalType)2 IntegerType (com.facebook.presto.common.type.IntegerType)2 RealType (com.facebook.presto.common.type.RealType)2 TimeType (com.facebook.presto.common.type.TimeType)2 TimestampType (com.facebook.presto.common.type.TimestampType)2 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)2 VarcharType (com.facebook.presto.common.type.VarcharType)2 Slice (io.airlift.slice.Slice)2 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 BigintType (com.facebook.presto.common.type.BigintType)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 CharType (com.facebook.presto.common.type.CharType)1 DoubleType (com.facebook.presto.common.type.DoubleType)1 SmallintType (com.facebook.presto.common.type.SmallintType)1 SqlDate (com.facebook.presto.common.type.SqlDate)1 SqlTime (com.facebook.presto.common.type.SqlTime)1 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)1 SqlVarbinary (com.facebook.presto.common.type.SqlVarbinary)1 TinyintType (com.facebook.presto.common.type.TinyintType)1