use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class DataType method varcharDataType.
private static DataType<String> varcharDataType(Optional<Integer> length, String properties) {
String prefix = length.map(size -> "varchar(" + size + ")").orElse("varchar");
String suffix = properties.isEmpty() ? "" : " " + properties;
VarcharType varcharType = length.map(VarcharType::createVarcharType).orElse(createUnboundedVarcharType());
return stringDataType(prefix + suffix, varcharType);
}
use of com.facebook.presto.common.type.VarcharType in project presto by prestodb.
the class PrometheusRecordCursor method writeObject.
private static void writeObject(BlockBuilder builder, Type type, Object obj) {
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
BlockBuilder arrayBuilder = builder.beginBlockEntry();
for (Object item : (List<?>) obj) {
writeObject(arrayBuilder, elementType, item);
}
builder.closeEntry();
} else if (type instanceof MapType) {
MapType mapType = (MapType) type;
BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
writeObject(mapBlockBuilder, mapType.getKeyType(), entry.getKey());
writeObject(mapBlockBuilder, mapType.getValueType(), entry.getValue());
}
builder.closeEntry();
} else {
if (BOOLEAN.equals(type) || TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type) || DOUBLE.equals(type) || type instanceof VarcharType) {
TypeUtils.writeNativeValue(type, builder, obj);
}
}
}
use of com.facebook.presto.common.type.VarcharType 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