Search in sources :

Example 6 with LiteralParameters

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

the class SequenceIntervalDayToSecond method sequence.

@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") LongTimestamp start, @SqlType("timestamp(p)") LongTimestamp stop, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long step) {
    // scale to micros
    step = multiplyExact(step, MICROSECONDS_PER_MILLISECOND);
    long startMicros = start.getEpochMicros();
    long stopMicros = stop.getEpochMicros();
    checkValidStep(startMicros, stopMicros, step);
    int length = toIntExact((stopMicros - startMicros) / step + 1L);
    checkMaxEntry(length);
    BlockBuilder blockBuilder = LONG_TYPE.createBlockBuilder(null, length);
    for (long i = 0, epochMicros = startMicros; i < length; ++i, epochMicros += step) {
        writeLongTimestamp(blockBuilder, epochMicros, start.getPicosOfMicro());
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 7 with LiteralParameters

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

the class SequenceIntervalYearToMonth method sequence.

@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") long start, @SqlType("timestamp(p)") long stop, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long step) {
    checkValidStep(start, stop, step);
    int length = toIntExact(DateDiff.diff(MONTH, start, stop) / step + 1);
    checkMaxEntry(length);
    BlockBuilder blockBuilder = SHORT_TYPE.createBlockBuilder(null, length);
    int offset = 0;
    for (int i = 0; i < length; ++i) {
        long value = TimestampPlusIntervalYearToMonth.add(start, offset);
        SHORT_TYPE.writeLong(blockBuilder, value);
        offset += step;
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 8 with LiteralParameters

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

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

the class CurrentTimestamp method shortTimestamp.

@LiteralParameters("p")
@SqlType("timestamp(p) with time zone")
public static long shortTimestamp(@LiteralParameter("p") long precision, ConnectorSession session, // need a dummy value since the type inferencer can't bind type arguments exclusively from return type
@SqlNullable @SqlType("timestamp(p) with time zone") Long dummy) {
    Instant start = session.getStart();
    long epochMillis = start.toEpochMilli();
    if (precision < MAX_SHORT_PRECISION) {
        epochMillis = round(epochMillis, (int) (MAX_SHORT_PRECISION - precision));
    } else {
        long nanosOfMilli = start.getNano() % NANOSECONDS_PER_MILLISECOND;
        if (roundToNearest(nanosOfMilli, NANOSECONDS_PER_MILLISECOND) == NANOSECONDS_PER_MILLISECOND) {
            epochMillis++;
        }
    }
    return packDateTimeWithZone(epochMillis, session.getTimeZoneKey());
}
Also used : Instant(java.time.Instant) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 10 with LiteralParameters

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

the class DateDiff method diff.

@LiteralParameters({ "x", "p" })
@SqlType(StandardTypes.BIGINT)
public static long diff(@SqlType("varchar(x)") Slice unit, @SqlType("timestamp(p) with time zone") LongTimestampWithTimeZone timestamp1, @SqlType("timestamp(p) with time zone") LongTimestampWithTimeZone timestamp2) {
    long epochMillis1 = timestamp1.getEpochMillis();
    long epochMillis2 = timestamp2.getEpochMillis();
    ISOChronology chronology = ISOChronology.getInstanceUTC();
    return getTimestampField(chronology, unit).getDifferenceAsLong(epochMillis2, epochMillis1);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) 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