Search in sources :

Example 1 with TimeZoneKey

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

the class WithTimeZone method shortPrecision.

@LiteralParameters({ "x", "p" })
@SqlType("timestamp(p) with time zone")
public static long shortPrecision(@LiteralParameter("p") long precision, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice zoneId) {
    verify(precision <= 3, "Expected precision <= 3");
    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);
    return packDateTimeWithZone(UTC.getMillisKeepLocal(toDateTimeZone, scaleEpochMicrosToMillis(timestamp)), 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) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 2 with TimeZoneKey

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

the class TimestampWithTimeZoneToDateCast method cast.

@LiteralParameters("p")
@SqlType(StandardTypes.DATE)
public static long cast(@SqlType("timestamp(p) with time zone") long timestamp) {
    long epochMillis = unpackMillisUtc(timestamp);
    TimeZoneKey zoneKey = unpackZoneKey(timestamp);
    return toDate(epochMillis, zoneKey);
}
Also used : 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 3 with TimeZoneKey

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

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToShort.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long longToShort(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") LongTimestampWithTimeZone timestamp) {
    // source precision is > 3
    // target precision is <= 9
    TimeZoneKey zoneKey = getTimeZoneKey(timestamp.getTimeZoneKey());
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(timestamp.getEpochMillis());
    // combine epochMillis with picosOfMilli from the timestamp. We compute modulo 24 to avoid overflow when rescaling epocMilli to picoseconds
    long picos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 12) + timestamp.getPicosOfMilli();
    picos = round(picos, (int) (12 - targetPrecision));
    picos = floorMod(picos, PICOSECONDS_PER_DAY);
    long nanos = rescale(picos, 12, 9);
    return packTimeWithTimeZone(nanos, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : 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 4 with TimeZoneKey

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

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone longToLong(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") LongTimestampWithTimeZone timestamp) {
    // source precision is > 3
    // target precision is > 9
    TimeZoneKey zoneKey = getTimeZoneKey(timestamp.getTimeZoneKey());
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(timestamp.getEpochMillis());
    // combine epochMillis with picosOfMilli from the timestamp. We compute modulo 24 to avoid overflow when rescaling epocMilli to picoseconds
    long picos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 12) + timestamp.getPicosOfMilli();
    picos = round(picos, (int) (12 - targetPrecision));
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), 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 5 with TimeZoneKey

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

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToShort.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long shortToShort(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") long packedTimestamp) {
    // source precision is <= 3
    // target precision is <= 9
    TimeZoneKey zoneKey = unpackZoneKey(packedTimestamp);
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(packedTimestamp));
    if (targetPrecision <= 3) {
        epochMillis = round(epochMillis, (int) (3 - targetPrecision));
    }
    long nanos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 9);
    return packTimeWithTimeZone(nanos, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : 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)

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