Search in sources :

Example 21 with SqlType

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

the class VarbinaryFunctions method reverse.

@Description("Reverse a given varbinary")
@ScalarFunction("reverse")
@SqlType(StandardTypes.VARBINARY)
public static Slice reverse(@SqlType("varbinary") Slice inputSlice) {
    if (inputSlice.length() == 0) {
        return EMPTY_SLICE;
    }
    int length = inputSlice.length();
    Slice reverse = Slices.allocate(length);
    for (int i = 0; i < length; i++) {
        reverse.setByte(i, inputSlice.getByte((length - 1) - i));
    }
    return reverse;
}
Also used : Slice(io.airlift.slice.Slice) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 22 with SqlType

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

the class FormatDateTime method format.

@LiteralParameters({ "x", "p" })
@SqlType(StandardTypes.VARCHAR)
public static Slice format(ConnectorSession session, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice formatString) {
    // TODO: currently, date formatting only supports up to millis, so we round to that unit
    timestamp = scaleEpochMicrosToMillis(round(timestamp, 3));
    if (datetimeFormatSpecifiesZone(formatString)) {
        // Timezone is unknown for TIMESTAMP w/o TZ so it cannot be printed out.
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "format_datetime for TIMESTAMP type, cannot use 'Z' nor 'z' in format, as this type does not contain TZ information");
    }
    ISOChronology chronology = ISOChronology.getInstanceUTC();
    try {
        return utf8Slice(DateTimeFormat.forPattern(formatString.toStringUtf8()).withChronology(chronology).withLocale(session.getLocale()).print(timestamp));
    } catch (Exception e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) TrinoException(io.trino.spi.TrinoException) TrinoException(io.trino.spi.TrinoException) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 23 with SqlType

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

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

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

Aggregations

SqlType (io.trino.spi.function.SqlType)176 ScalarFunction (io.trino.spi.function.ScalarFunction)107 Description (io.trino.spi.function.Description)90 LiteralParameters (io.trino.spi.function.LiteralParameters)60 SqlNullable (io.trino.spi.function.SqlNullable)56 BlockBuilder (io.trino.spi.block.BlockBuilder)52 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 TrinoException (io.trino.spi.TrinoException)37 Constraint (io.trino.type.Constraint)23 Slice (io.airlift.slice.Slice)22 TypeParameter (io.trino.spi.function.TypeParameter)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 JsonParser (com.fasterxml.jackson.core.JsonParser)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 ScalarOperator (io.trino.spi.function.ScalarOperator)13 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)11 Matcher (io.airlift.joni.Matcher)9