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