Search in sources :

Example 61 with RowType

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);
    }
}
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

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5