Search in sources :

Example 6 with LongTimeWithTimeZone

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

the class VarcharToTimeWithTimeZoneCast method castToLong.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static LongTimeWithTimeZone castToLong(@LiteralParameter("p") long precision, ConnectorSession session, @SqlType("varchar(x)") Slice value) {
    checkArgument((int) precision > MAX_SHORT_PRECISION && (int) precision <= MAX_PRECISION, "precision out of range");
    Matcher matcher = DateTimes.TIME_PATTERN.matcher(trim(value).toStringUtf8());
    if (!matcher.matches()) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value.toStringUtf8());
    }
    try {
        long picos = parseTime(matcher) * PICOSECONDS_PER_SECOND + parseFraction((int) precision, matcher, 12);
        picos %= PICOSECONDS_PER_DAY;
        int offsetMinutes = parseOffset(session, matcher);
        return new LongTimeWithTimeZone(picos, offsetMinutes);
    } catch (IllegalArgumentException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value.toStringUtf8(), e);
    }
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Aggregations

LongTimeWithTimeZone (io.trino.spi.type.LongTimeWithTimeZone)6 LiteralParameters (io.trino.spi.function.LiteralParameters)5 SqlType (io.trino.spi.function.SqlType)5 TimeZoneKey (io.trino.spi.type.TimeZoneKey)3 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)3 TrinoException (io.trino.spi.TrinoException)2 Matcher (java.util.regex.Matcher)2 TimeZoneNotSupportedException (io.trino.spi.type.TimeZoneNotSupportedException)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1