use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class MapColumnValidator method generateChecksumColumns.
@Override
public List<SingleColumn> generateChecksumColumns(Column column) {
checkArgument(column.getType() instanceof MapType, "Expect MapType, found %s", column.getType().getDisplayName());
Type keyType = ((MapType) column.getType()).getKeyType();
Type valueType = ((MapType) column.getType()).getValueType();
Expression checksum = functionCall("checksum", column.getExpression());
Expression keysChecksum = generateArrayChecksum(functionCall("map_keys", column.getExpression()), new ArrayType(keyType));
Expression valuesChecksum = generateArrayChecksum(functionCall("map_values", column.getExpression()), new ArrayType(valueType));
Expression mapCardinalityChecksum = functionCall("checksum", functionCall("cardinality", column.getExpression()));
Expression mapCardinalitySum = new CoalesceExpression(functionCall("sum", functionCall("cardinality", column.getExpression())), new LongLiteral("0"));
return ImmutableList.of(new SingleColumn(checksum, Optional.of(delimitedIdentifier(getChecksumColumnAlias(column)))), new SingleColumn(keysChecksum, Optional.of(delimitedIdentifier(getKeysChecksumColumnAlias(column)))), new SingleColumn(valuesChecksum, Optional.of(delimitedIdentifier(getValuesChecksumColumnAlias(column)))), new SingleColumn(mapCardinalityChecksum, Optional.of(delimitedIdentifier(getCardinalityChecksumColumnAlias(column)))), new SingleColumn(mapCardinalitySum, Optional.of(delimitedIdentifier(getCardinalitySumColumnAlias(column)))));
}
use of com.facebook.presto.common.type.MapType 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