Search in sources :

Example 46 with VarcharType

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);
}
Also used : BaseEncoding.base16(com.google.common.io.BaseEncoding.base16) UNNECESSARY(java.math.RoundingMode.UNNECESSARY) TinyintType(com.facebook.presto.common.type.TinyintType) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) Strings.padEnd(com.google.common.base.Strings.padEnd) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) BigintType(com.facebook.presto.common.type.BigintType) BooleanType(com.facebook.presto.common.type.BooleanType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) Function(java.util.function.Function) DATE(com.facebook.presto.common.type.DateType.DATE) String.format(java.lang.String.format) BigDecimal(java.math.BigDecimal) SmallintType(com.facebook.presto.common.type.SmallintType) IntegerType(com.facebook.presto.common.type.IntegerType) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Function.identity(java.util.function.Function.identity) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) Optional(java.util.Optional) Type(com.facebook.presto.common.type.Type) DoubleType(com.facebook.presto.common.type.DoubleType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType)

Example 47 with 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);
        }
    }
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 48 with VarcharType

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

VarcharType (com.facebook.presto.common.type.VarcharType)48 DecimalType (com.facebook.presto.common.type.DecimalType)30 Type (com.facebook.presto.common.type.Type)26 CharType (com.facebook.presto.common.type.CharType)23 PrestoException (com.facebook.presto.spi.PrestoException)16 Slice (io.airlift.slice.Slice)16 ArrayType (com.facebook.presto.common.type.ArrayType)14 RowType (com.facebook.presto.common.type.RowType)13 MapType (com.facebook.presto.common.type.MapType)12 ArrayList (java.util.ArrayList)12 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 IntegerType (com.facebook.presto.common.type.IntegerType)10 TimestampType (com.facebook.presto.common.type.TimestampType)10 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)10 BigintType (com.facebook.presto.common.type.BigintType)9 BooleanType (com.facebook.presto.common.type.BooleanType)9 DoubleType (com.facebook.presto.common.type.DoubleType)9 RealType (com.facebook.presto.common.type.RealType)9 SmallintType (com.facebook.presto.common.type.SmallintType)9