Search in sources :

Example 96 with SqlType

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

the class DateTimeFunctions method fromISO8601Date.

@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(@SqlType("varchar(x)") Slice iso8601DateTime) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY);
    DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
    return MILLISECONDS.toDays(dateTime.getMillis());
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 97 with SqlType

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

the class DateTimeFunctions method fromIso8601TimestampNanos.

@ScalarFunction("from_iso8601_timestamp_nanos")
@LiteralParameters("x")
@SqlType("timestamp(9) with time zone")
public static LongTimestampWithTimeZone fromIso8601TimestampNanos(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
    java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ISO_DATE_TIME;
    String datetimeString = iso8601DateTime.toStringUtf8();
    TemporalAccessor parsedDatetime;
    try {
        parsedDatetime = formatter.parseBest(datetimeString, ZonedDateTime::from, LocalDateTime::from);
    } catch (DateTimeParseException e) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, e);
    }
    ZonedDateTime zonedDatetime;
    if (parsedDatetime instanceof ZonedDateTime) {
        zonedDatetime = (ZonedDateTime) parsedDatetime;
    } else {
        zonedDatetime = ((LocalDateTime) parsedDatetime).atZone(session.getTimeZoneKey().getZoneId());
    }
    long picosOfSecond = zonedDatetime.getNano() * ((long) PICOSECONDS_PER_NANOSECOND);
    TimeZoneKey zone = TimeZoneKey.getTimeZoneKey(zonedDatetime.getZone().getId());
    return LongTimestampWithTimeZone.fromEpochSecondsAndFraction(zonedDatetime.toEpochSecond(), picosOfSecond, zone);
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) ZonedDateTime(java.time.ZonedDateTime) String.format(java.lang.String.format) TrinoException(io.trino.spi.TrinoException) TimeZoneKey(io.trino.spi.type.TimeZoneKey) ScalarFunction(io.trino.spi.function.ScalarFunction) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 98 with SqlType

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

the class JoniRegexpFunctions method regexpSplit.

@ScalarFunction
@LiteralParameters("x")
@Description("Returns array of strings split by pattern")
@SqlType("array(varchar(x))")
public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher = pattern.matcher(source.getBytes());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int lastEnd = 0;
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Slice slice = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        VARCHAR.writeSlice(blockBuilder, slice);
    }
    VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
    return blockBuilder.build();
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Constraint(io.trino.type.Constraint) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 99 with SqlType

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

the class JoniRegexpFunctions method regexpCount.

@ScalarFunction
@Description("Returns the number of times that a pattern occurs in a string")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long regexpCount(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher = pattern.matcher(source.getBytes());
    int count = 0;
    // Start from zero, implies the first byte
    int nextStart = 0;
    while (true) {
        // mather.search returns `source.length` if `nextStart` equals `source.length - 1`.
        // It should return -1 if `nextStart` is greater than `source.length - 1`.
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset < 0) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        count++;
    }
    return count;
}
Also used : Matcher(io.airlift.joni.Matcher) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 100 with SqlType

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

the class JoniRegexpFunctions method regexpLike.

@Description("Returns whether the pattern is contained within the string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static boolean regexpLike(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
    Matcher matcher;
    int offset;
    if (source.hasByteArray()) {
        offset = source.byteArrayOffset();
        matcher = pattern.regex().matcher(source.byteArray(), offset, offset + source.length());
    } else {
        offset = 0;
        matcher = pattern.matcher(source.getBytes());
    }
    return matcher.search(offset, offset + source.length(), Option.DEFAULT) != -1;
}
Also used : Matcher(io.airlift.joni.Matcher) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) 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