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);
}
}
Aggregations