Search in sources :

Example 91 with MapType

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)))));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) MapType(com.facebook.presto.common.type.MapType)

Example 92 with MapType

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);
    }
}
Also used : Iterables.transform(com.google.common.collect.Iterables.transform) TestingPrestoServer(com.facebook.presto.server.testing.TestingPrestoServer) ZonedDateTime(java.time.ZonedDateTime) BigintEnumType(com.facebook.presto.common.type.BigintEnumType) QueryData(com.facebook.presto.client.QueryData) JsonType(com.facebook.presto.common.type.JsonType) BigDecimal(java.math.BigDecimal) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) DEFAULT_PRECISION(com.facebook.presto.testing.MaterializedResult.DEFAULT_PRECISION) Map(java.util.Map) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) VarcharEnumType(com.facebook.presto.common.type.VarcharEnumType) OffsetTime(java.time.OffsetTime) TIME(com.facebook.presto.common.type.TimeType.TIME) Function(com.google.common.base.Function) IntervalYearMonth(com.facebook.presto.client.IntervalYearMonth) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Set(java.util.Set) VarcharType(com.facebook.presto.common.type.VarcharType) Collectors(java.util.stream.Collectors) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) LocalDate(java.time.LocalDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) Optional(java.util.Optional) TIME_WITH_TIME_ZONE(com.facebook.presto.common.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) INTERVAL_YEAR_MONTH(com.facebook.presto.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) TIMESTAMP_WITH_TIME_ZONE(com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) TINYINT(com.facebook.presto.common.type.TinyintType.TINYINT) LocalDateTime(java.time.LocalDateTime) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) AtomicReference(java.util.concurrent.atomic.AtomicReference) DATE(com.facebook.presto.common.type.DateType.DATE) REAL(com.facebook.presto.common.type.RealType.REAL) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) ImmutableList(com.google.common.collect.ImmutableList) INTERVAL_DAY_TIME(com.facebook.presto.type.IntervalDayTimeType.INTERVAL_DAY_TIME) PrestoWarning(com.facebook.presto.spi.PrestoWarning) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) TypeWithName(com.facebook.presto.common.type.TypeWithName) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Session(com.facebook.presto.Session) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) QueryStatusInfo(com.facebook.presto.client.QueryStatusInfo) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Collectors.toList(java.util.stream.Collectors.toList) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) MaterializedRow(com.facebook.presto.testing.MaterializedRow) DateTimeFormatter(java.time.format.DateTimeFormatter) IntervalDayTime(com.facebook.presto.client.IntervalDayTime) IPADDRESS(com.facebook.presto.type.IpAddressType.IPADDRESS) RowType(com.facebook.presto.common.type.RowType) JsonType(com.facebook.presto.common.type.JsonType) TypeWithName(com.facebook.presto.common.type.TypeWithName) VarcharType(com.facebook.presto.common.type.VarcharType) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) RowType(com.facebook.presto.common.type.RowType) LocalDate(java.time.LocalDate) MapType(com.facebook.presto.common.type.MapType) BigDecimal(java.math.BigDecimal) ArrayType(com.facebook.presto.common.type.ArrayType) DateTimeParseException(java.time.format.DateTimeParseException) ZonedDateTime(java.time.ZonedDateTime) VarcharEnumType(com.facebook.presto.common.type.VarcharEnumType) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) DecimalType(com.facebook.presto.common.type.DecimalType) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) BigintEnumType(com.facebook.presto.common.type.BigintEnumType) Map(java.util.Map)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5