use of io.prestosql.spi.type.SqlTime in project hetu-core by openlookeng.
the class TestDateTimeOperators method testTimeRepresentation.
@Test
public void testTimeRepresentation() {
// PST -> PDT date
testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
// PDT -> PST date
testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
// PDT date
testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
// PST date
testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000'", TIME, new SqlTime(9000000));
testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000'", TIME, new SqlTime(12600000));
}
use of io.prestosql.spi.type.SqlTime in project hetu-core by openlookeng.
the class MaterializedResult method writeValue.
private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
if (value == null) {
blockBuilder.appendNull();
} else if (BIGINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).longValue());
} else if (INTEGER.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).intValue());
} else if (SMALLINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).shortValue());
} else if (TINYINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).byteValue());
} else if (REAL.equals(type)) {
type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
} else if (DOUBLE.equals(type)) {
type.writeDouble(blockBuilder, ((Number) value).doubleValue());
} else if (BOOLEAN.equals(type)) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (JSON.equals(type)) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (type instanceof VarcharType) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (type instanceof CharType) {
type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
} else if (VARBINARY.equals(type)) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
} else if (DATE.equals(type)) {
int days = ((SqlDate) value).getDays();
type.writeLong(blockBuilder, days);
} else if (TIME.equals(type)) {
SqlTime time = (SqlTime) value;
if (time.isLegacyTimestamp()) {
type.writeLong(blockBuilder, time.getMillisUtc());
} else {
type.writeLong(blockBuilder, time.getMillis());
}
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
} else if (TIMESTAMP.equals(type)) {
long millisUtc = ((SqlTimestamp) value).getMillis();
type.writeLong(blockBuilder, millisUtc);
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
} else if (ARRAY.equals(type.getTypeSignature().getBase())) {
List<Object> list = (List<Object>) value;
Type elementType = ((ArrayType) type).getElementType();
BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
for (Object element : list) {
writeValue(elementType, arrayBlockBuilder, element);
}
blockBuilder.closeEntry();
} else if (MAP.equals(type.getTypeSignature().getBase())) {
Map<Object, Object> map = (Map<Object, Object>) value;
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Entry<Object, Object> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
List<Object> row = (List<Object>) value;
List<Type> fieldTypes = type.getTypeParameters();
BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
for (int field = 0; field < row.size(); field++) {
writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
}
blockBuilder.closeEntry();
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
use of io.prestosql.spi.type.SqlTime in project hetu-core by openlookeng.
the class MaterializedResult method convertToTestTypes.
private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) {
List<Object> convertedValues = new ArrayList<>();
for (int field = 0; field < prestoRow.getFieldCount(); field++) {
Object prestoValue = prestoRow.getField(field);
Object convertedValue;
if (prestoValue instanceof SqlDate) {
convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays());
} else if (prestoValue instanceof SqlTime) {
convertedValue = DateTimeFormatter.ISO_LOCAL_TIME.parse(prestoValue.toString(), LocalTime::from);
} else if (prestoValue instanceof SqlTimeWithTimeZone) {
// Political timezone cannot be represented in OffsetTime and there isn't any better representation.
long millisUtc = ((SqlTimeWithTimeZone) prestoValue).getMillisUtc();
ZoneOffset zone = toZoneOffset(((SqlTimeWithTimeZone) prestoValue).getTimeZoneKey());
convertedValue = OffsetTime.of(LocalTime.ofNanoOfDay(MILLISECONDS.toNanos(millisUtc) + SECONDS.toNanos(zone.getTotalSeconds())), zone);
} else if (prestoValue instanceof SqlTimestamp) {
convertedValue = SqlTimestamp.JSON_FORMATTER.parse(prestoValue.toString(), LocalDateTime::from);
} else if (prestoValue instanceof SqlTimestampWithTimeZone) {
convertedValue = Instant.ofEpochMilli(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc()).atZone(ZoneId.of(((SqlTimestampWithTimeZone) prestoValue).getTimeZoneKey().getId()));
} else if (prestoValue instanceof SqlDecimal) {
convertedValue = ((SqlDecimal) prestoValue).toBigDecimal();
} else {
convertedValue = prestoValue;
}
convertedValues.add(convertedValue);
}
return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
}
use of io.prestosql.spi.type.SqlTime in project hetu-core by openlookeng.
the class LiteralInterpreter method evaluate.
public static Object evaluate(ConstantExpression node) {
Type type = node.getType();
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 (node.getValue() instanceof String) ? node.getValue() : ((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) {
return new SqlTime((long) node.getValue());
}
if (type instanceof TimestampType) {
try {
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();
}
Aggregations