Search in sources :

Example 11 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<?> row = (List<?>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class AtTimeZone method atTimeZone.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static LongTimeWithTimeZone atTimeZone(ConnectorSession session, @SqlType("time(p) with time zone") LongTimeWithTimeZone time, @SqlType("varchar(x)") Slice zoneId) {
    TimeZoneKey zoneKey;
    try {
        zoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId));
    }
    int offsetMinutes = getOffsetMinutes(session.getStart(), zoneKey);
    long picos = time.getPicoseconds() - (time.getOffsetMinutes() - offsetMinutes) * PICOSECONDS_PER_MINUTE;
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), offsetMinutes);
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 13 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class AtTimeZone method atTimeZone.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static long atTimeZone(ConnectorSession session, @SqlType("time(p) with time zone") long packedTime, @SqlType("varchar(x)") Slice zoneId) {
    TimeZoneKey zoneKey;
    try {
        zoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId));
    }
    int offsetMinutes = getOffsetMinutes(session.getStart(), zoneKey);
    long nanos = unpackTimeNanos(packedTime) - (unpackOffsetMinutes(packedTime) - offsetMinutes) * NANOSECONDS_PER_MINUTE;
    return packTimeWithTimeZone(floorMod(nanos, NANOSECONDS_PER_DAY), offsetMinutes);
}
Also used : TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 14 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone shortToLong(@SqlType("timestamp(sourcePrecision) with time zone") long timestamp) {
    // source precision is <= 3
    // target precision is > 9
    TimeZoneKey zoneKey = unpackZoneKey(timestamp);
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(timestamp));
    long millis = floorMod(epochMillis, MILLISECONDS_PER_DAY);
    return new LongTimeWithTimeZone(millis * PICOSECONDS_PER_MILLISECOND, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 15 with TimeZoneKey

use of io.trino.spi.type.TimeZoneKey in project trino by trinodb.

the class WithTimeZone method toLong.

private static LongTimestampWithTimeZone toLong(long epochMicros, int picosOfMicro, Slice zoneId) {
    TimeZoneKey toTimeZoneKey;
    try {
        toTimeZoneKey = getTimeZoneKey(zoneId.toStringUtf8());
    } catch (TimeZoneNotSupportedException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId.toStringUtf8()));
    }
    DateTimeZone toDateTimeZone = getDateTimeZone(toTimeZoneKey);
    long epochMillis = scaleEpochMicrosToMillis(epochMicros);
    epochMillis = UTC.getMillisKeepLocal(toDateTimeZone, epochMillis);
    int picosOfMilli = getMicrosOfMilli(epochMicros) * PICOSECONDS_PER_MICROSECOND + picosOfMicro;
    return LongTimestampWithTimeZone.fromEpochMillisAndFraction(epochMillis, picosOfMilli, toTimeZoneKey);
}
Also used : TrinoException(io.trino.spi.TrinoException) TimeZoneNotSupportedException(io.trino.spi.type.TimeZoneNotSupportedException) TimeZoneKey.getTimeZoneKey(io.trino.spi.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(io.trino.spi.type.TimeZoneKey) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(io.trino.util.DateTimeZoneIndex.getDateTimeZone)

Aggregations

TimeZoneKey (io.trino.spi.type.TimeZoneKey)16 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)11 LiteralParameters (io.trino.spi.function.LiteralParameters)9 SqlType (io.trino.spi.function.SqlType)9 TrinoException (io.trino.spi.TrinoException)6 TimeZoneNotSupportedException (io.trino.spi.type.TimeZoneNotSupportedException)4 LongTimeWithTimeZone (io.trino.spi.type.LongTimeWithTimeZone)3 DateTimeZone (org.joda.time.DateTimeZone)3 Type (io.trino.spi.type.Type)2 VarcharType (io.trino.spi.type.VarcharType)2 DateTimeZoneIndex.getDateTimeZone (io.trino.util.DateTimeZoneIndex.getDateTimeZone)2 ZonedDateTime (java.time.ZonedDateTime)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Slice (io.airlift.slice.Slice)1 PageListBuilder (io.trino.plugin.iceberg.util.PageListBuilder)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 InMemoryRecordSet (io.trino.spi.connector.InMemoryRecordSet)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1