Search in sources :

Example 1 with DecimalType

use of io.delta.standalone.types.DecimalType in project presto by prestodb.

the class DeltaTypeUtils method convertPartitionValue.

public static Object convertPartitionValue(String columnName, String valueString, Type type) {
    if (valueString == null) {
        return null;
    }
    try {
        if (type.equals(BOOLEAN)) {
            checkArgument(valueString.equalsIgnoreCase("true") || valueString.equalsIgnoreCase("false"));
            return Boolean.valueOf(valueString);
        }
        if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
            return parseLong(valueString);
        }
        if (type.equals(REAL)) {
            return (long) floatToRawIntBits(parseFloat(valueString));
        }
        if (type.equals(DOUBLE)) {
            return parseDouble(valueString);
        }
        if (type instanceof VarcharType) {
            Slice value = utf8Slice(valueString);
            VarcharType varcharType = (VarcharType) type;
            if (!varcharType.isUnbounded() && SliceUtf8.countCodePoints(value) > varcharType.getLengthSafe()) {
                throw new IllegalArgumentException();
            }
            return value;
        }
        if (type.equals(VARBINARY)) {
            return utf8Slice(valueString);
        }
        if (isShortDecimal(type) || isLongDecimal(type)) {
            com.facebook.presto.common.type.DecimalType decimalType = (com.facebook.presto.common.type.DecimalType) type;
            BigDecimal decimal = new BigDecimal(valueString);
            decimal = decimal.setScale(decimalType.getScale(), BigDecimal.ROUND_UNNECESSARY);
            if (decimal.precision() > decimalType.getPrecision()) {
                throw new IllegalArgumentException();
            }
            BigInteger unscaledValue = decimal.unscaledValue();
            return isShortDecimal(type) ? unscaledValue.longValue() : Decimals.encodeUnscaledValue(unscaledValue);
        }
        if (type.equals(DATE)) {
            return LocalDate.parse(valueString, DateTimeFormatter.ISO_LOCAL_DATE).toEpochDay();
        }
        if (type.equals(TIMESTAMP)) {
            // Delta partition serialized value contains up to the second precision
            return Timestamp.valueOf(valueString).toLocalDateTime().toEpochSecond(ZoneOffset.UTC) * 1_000;
        }
        throw new PrestoException(DELTA_UNSUPPORTED_COLUMN_TYPE, format("Unsupported data type '%s' for partition column %s", type, columnName));
    } catch (IllegalArgumentException | DateTimeParseException e) {
        throw new PrestoException(DELTA_INVALID_PARTITION_VALUE, format("Can not parse partition value '%s' of type '%s' for partition column '%s'", valueString, type, columnName), e);
    }
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) PrestoException(com.facebook.presto.spi.PrestoException) BigDecimal(java.math.BigDecimal) DateTimeParseException(java.time.format.DateTimeParseException) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) DecimalType(io.delta.standalone.types.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) BigInteger(java.math.BigInteger)

Aggregations

DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)1 VarcharType (com.facebook.presto.common.type.VarcharType)1 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)1 PrestoException (com.facebook.presto.spi.PrestoException)1 Slice (io.airlift.slice.Slice)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1 DecimalType (io.delta.standalone.types.DecimalType)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 DateTimeParseException (java.time.format.DateTimeParseException)1