use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class TestingPrestoClient method convertToRowValue.
private static Object convertToRowValue(Type type, Object value) {
if (value == null) {
return null;
}
if (BOOLEAN.equals(type)) {
return value;
} else if (TINYINT.equals(type)) {
return ((Number) value).byteValue();
} else if (SMALLINT.equals(type)) {
return ((Number) value).shortValue();
} else if (INTEGER.equals(type)) {
return ((Number) value).intValue();
} else if (BIGINT.equals(type)) {
return ((Number) value).longValue();
} else if (DOUBLE.equals(type)) {
return ((Number) value).doubleValue();
} else if (REAL.equals(type)) {
return ((Number) value).floatValue();
} else if (type instanceof VarcharType) {
return value;
} else if (isCharType(type)) {
return value;
} else if (VARBINARY.equals(type)) {
return value;
} else if (DATE.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(((String) value), LocalDate::from);
} else if (TIME.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_TIME.parse(((String) value), LocalTime::from);
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
// Only zone-offset timezones are supported (TODO remove political timezones support for TIME WITH TIME ZONE)
try {
return timeWithUtcZoneFormat.parse(((String) value), LocalTime::from).atOffset(ZoneOffset.UTC);
} catch (DateTimeParseException e) {
return timeWithZoneOffsetFormat.parse(((String) value), OffsetTime::from);
}
} else if (TIMESTAMP.equals(type)) {
return SqlTimestamp.JSON_FORMATTER.parse((String) value, LocalDateTime::from);
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
return timestampWithTimeZoneFormat.parse((String) value, ZonedDateTime::from);
} else if (INTERVAL_DAY_TIME.equals(type)) {
return new SqlIntervalDayTime(IntervalDayTime.parseMillis(String.valueOf(value)));
} else if (INTERVAL_YEAR_MONTH.equals(type)) {
return new SqlIntervalYearMonth(IntervalYearMonth.parseMonths(String.valueOf(value)));
} else if (IPADDRESS.equals(type)) {
return value;
} else if (type instanceof ArrayType) {
return ((List<Object>) value).stream().map(element -> convertToRowValue(((ArrayType) type).getElementType(), element)).collect(toList());
} else if (type instanceof MapType) {
return ((Map<Object, Object>) value).entrySet().stream().collect(Collectors.toMap(e -> convertToRowValue(((MapType) type).getKeyType(), e.getKey()), e -> convertToRowValue(((MapType) type).getValueType(), e.getValue())));
} else if (type instanceof RowType) {
Map<String, Object> data = (Map<String, Object>) value;
RowType rowType = (RowType) type;
return rowType.getFields().stream().map(field -> convertToRowValue(field.getType(), data.get(field.getName().get()))).collect(toList());
} else if (type instanceof DecimalType) {
return new BigDecimal((String) value);
} else if (type instanceof JsonType) {
return value;
} else if (type instanceof VarcharEnumType) {
return value;
} else if (type instanceof BigintEnumType) {
return ((Number) value).longValue();
} else if (type instanceof TypeWithName) {
return convertToRowValue(((TypeWithName) type).getType(), value);
} else if (type.getTypeSignature().getBase().equals("ObjectId")) {
return value;
} else {
throw new AssertionError("unhandled type: " + type);
}
}
Aggregations