use of com.facebook.presto.common.type.TimestampType 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();
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class MetastoreUtil method convertRawValueToString.
public static String convertRawValueToString(Object value, Type type) {
String val;
if (value == null) {
val = HIVE_DEFAULT_DYNAMIC_PARTITION;
} else if (type instanceof CharType) {
Slice slice = (Slice) value;
val = padSpaces(slice, type).toStringUtf8();
} else if (type instanceof VarcharType) {
Slice slice = (Slice) value;
val = slice.toStringUtf8();
} else if (type instanceof DecimalType && !((DecimalType) type).isShort()) {
Slice slice = (Slice) value;
val = Decimals.toString(slice, ((DecimalType) type).getScale());
} else if (type instanceof DecimalType && ((DecimalType) type).isShort()) {
val = Decimals.toString((long) value, ((DecimalType) type).getScale());
} else if (type instanceof DateType) {
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC();
val = dateTimeFormatter.print(TimeUnit.DAYS.toMillis((long) value));
} else if (type instanceof TimestampType) {
// we don't have time zone info, so just add a wildcard
val = PARTITION_VALUE_WILDCARD;
} else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType || type instanceof DoubleType || type instanceof RealType || type instanceof BooleanType) {
val = value.toString();
} else {
throw new PrestoException(NOT_SUPPORTED, format("Unsupported partition key type: %s", type.getDisplayName()));
}
return val;
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class DruidPushdownUtils method getLiteralAsString.
// Copied from com.facebook.presto.sql.planner.LiteralInterpreter.evaluate
public static String getLiteralAsString(ConnectorSession session, ConstantExpression node) {
Type type = node.getType();
if (node.getValue() == null) {
throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Null constant expression: " + node + " with value of type: " + type);
}
if (type instanceof BooleanType) {
return String.valueOf(((Boolean) node.getValue()).booleanValue());
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
Number number = (Number) node.getValue();
return format("%d", number.longValue());
}
if (type instanceof DoubleType) {
return node.getValue().toString();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return format("%f", 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).toString();
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
}
if (type instanceof VarcharType || type instanceof CharType) {
return "'" + ((Slice) node.getValue()).toStringUtf8() + "'";
}
if (type instanceof TimestampType) {
return getTimestampLiteralAsString(session, (long) node.getValue());
}
if (type instanceof TimestampWithTimeZoneType) {
return getTimestampLiteralAsString(session, new SqlTimestampWithTimeZone((long) node.getValue()).getMillisUtc());
}
throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Cannot handle the constant expression: " + node + " with value of type: " + type);
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class TestWriterBlockRawSize method testTimestampType.
@Test
public void testTimestampType() {
Type timestampType = TimestampType.TIMESTAMP;
ColumnWriter columnWriter = createColumnWriter(timestampType);
BlockBuilder blockBuilder = timestampType.createBlockBuilder(null, NUM_ELEMENTS * 2);
for (int i = 0; i < NUM_ELEMENTS; i++) {
blockBuilder.appendNull();
timestampType.writeLong(blockBuilder, i);
}
long rawSize = columnWriter.writeBlock(blockBuilder.build());
long expectedSize = NUM_ELEMENTS * (1 + Long.BYTES + Integer.BYTES);
assertEquals(rawSize, expectedSize);
}
use of com.facebook.presto.common.type.TimestampType in project presto by prestodb.
the class PinotPushdownUtils method getLiteralAsString.
// Copied from com.facebook.presto.sql.planner.LiteralInterpreter.evaluate
public static String getLiteralAsString(ConstantExpression node) {
Type type = node.getType();
if (node.getValue() == null) {
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), String.format("Null constant expression %s with value of type %s", node, type));
}
if (type instanceof BooleanType) {
return String.valueOf(((Boolean) node.getValue()).booleanValue());
}
if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
Number number = (Number) node.getValue();
return format("%d", number.longValue());
}
if (type instanceof DoubleType) {
return node.getValue().toString();
}
if (type instanceof RealType) {
Long number = (Long) node.getValue();
return format("%f", 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).toString();
}
checkState(node.getValue() instanceof Slice);
Slice value = (Slice) node.getValue();
return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
}
if (type instanceof VarcharType || type instanceof CharType) {
return "'" + ((Slice) node.getValue()).toStringUtf8() + "'";
}
if (type instanceof TimestampType || type instanceof DateType) {
return node.getValue().toString();
}
if (type instanceof TimestampWithTimeZoneType) {
Long millisUtc = DateTimeEncoding.unpackMillisUtc((Long) node.getValue());
return millisUtc.toString();
}
throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), String.format("Cannot handle the constant expression %s with value of type %s", node, type));
}
Aggregations