Search in sources :

Example 16 with LiteralParameters

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

the class FunctionsParserHelper method parseLiteralParameters.

public static Set<String> parseLiteralParameters(Method method) {
    LiteralParameters literalParametersAnnotation = method.getAnnotation(LiteralParameters.class);
    if (literalParametersAnnotation == null) {
        return ImmutableSet.of();
    }
    Set<String> result = new TreeSet<>(CASE_INSENSITIVE_ORDER);
    result.addAll(Arrays.asList(literalParametersAnnotation.value()));
    return result;
}
Also used : LiteralParameters(io.trino.spi.function.LiteralParameters) TreeSet(java.util.TreeSet)

Example 17 with LiteralParameters

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

the class ToIso8601 method toIso8601.

@LiteralParameters({ "p", "n" })
@SqlType("varchar(n)")
@Constraint(variable = "n", expression = RESULT_LENGTH)
public static Slice toIso8601(@LiteralParameter("p") long precision, @SqlType("timestamp(p) with time zone") long packedEpochMillis) {
    long epochMillis = unpackMillisUtc(packedEpochMillis);
    ZoneId zoneId = unpackZoneKey(packedEpochMillis).getZoneId();
    return utf8Slice(format((int) precision, epochMillis, 0, zoneId));
}
Also used : ZoneId(java.time.ZoneId) LiteralParameters(io.trino.spi.function.LiteralParameters) Constraint(io.trino.type.Constraint) SqlType(io.trino.spi.function.SqlType)

Example 18 with LiteralParameters

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

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

the class TimestampWithTimeZoneToTimeCast method cast.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision)")
public static long cast(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") long packedEpochMillis) {
    long epochMillis = unpackMillisUtc(packedEpochMillis);
    ZoneId zoneId = unpackZoneKey(packedEpochMillis).getZoneId();
    return convert(targetPrecision, epochMillis, 0, zoneId);
}
Also used : ZoneId(java.time.ZoneId) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 20 with LiteralParameters

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

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