Search in sources :

Example 61 with LiteralParameters

use of io.trino.spi.function.LiteralParameters in project trino by trinodb.

the class VarcharToTimeWithTimeZoneCast method castToShort.

@LiteralParameters({ "x", "p" })
@SqlType("time(p) with time zone")
public static long castToShort(@LiteralParameter("p") long precision, ConnectorSession session, @SqlType("varchar(x)") Slice value) {
    checkArgument((int) precision <= MAX_SHORT_PRECISION, "precision must be less than max short timestamp precision");
    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 nanos = parseTime(matcher) * NANOSECONDS_PER_SECOND + parseFraction((int) precision, matcher, 9);
        nanos %= NANOSECONDS_PER_DAY;
        int offsetMinutes = parseOffset(session, matcher);
        return packTimeWithTimeZone(nanos, offsetMinutes);
    } catch (IllegalArgumentException e) {
        throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value.toStringUtf8(), e);
    }
}
Also used : Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 62 with LiteralParameters

use of io.trino.spi.function.LiteralParameters 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

LiteralParameters (io.trino.spi.function.LiteralParameters)62 SqlType (io.trino.spi.function.SqlType)60 ScalarFunction (io.trino.spi.function.ScalarFunction)25 Constraint (io.trino.type.Constraint)21 Description (io.trino.spi.function.Description)19 TrinoException (io.trino.spi.TrinoException)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)11 Matcher (io.airlift.joni.Matcher)9 Slice (io.airlift.slice.Slice)9 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)9 BlockBuilder (io.trino.spi.block.BlockBuilder)9 TimeZoneKey (io.trino.spi.type.TimeZoneKey)9 TimeZoneKey.getTimeZoneKey (io.trino.spi.type.TimeZoneKey.getTimeZoneKey)8 SqlNullable (io.trino.spi.function.SqlNullable)7 ZoneId (java.time.ZoneId)7 ScalarOperator (io.trino.spi.function.ScalarOperator)5 LongTimeWithTimeZone (io.trino.spi.type.LongTimeWithTimeZone)5 ISOChronology (org.joda.time.chrono.ISOChronology)5 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)4 Instant (java.time.Instant)4