Search in sources :

Example 11 with LiteralParameters

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

the class StringFunctions method substring.

@Description("Substring of given length starting at an index")
@ScalarFunction(alias = "substr")
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice substring(@SqlType("varchar(x)") Slice utf8, @SqlType(StandardTypes.BIGINT) long start, @SqlType(StandardTypes.BIGINT) long length) {
    if (start == 0 || (length <= 0) || (utf8.length() == 0)) {
        return Slices.EMPTY_SLICE;
    }
    int startCodePoint = Ints.saturatedCast(start);
    int lengthCodePoints = Ints.saturatedCast(length);
    if (startCodePoint > 0) {
        int indexStart = offsetOfCodePoint(utf8, startCodePoint - 1);
        if (indexStart < 0) {
            // before beginning of string
            return Slices.EMPTY_SLICE;
        }
        int indexEnd = offsetOfCodePoint(utf8, indexStart, lengthCodePoints);
        if (indexEnd < 0) {
            // after end of string
            indexEnd = utf8.length();
        }
        return utf8.slice(indexStart, indexEnd - indexStart);
    }
    // negative start is relative to end of string
    int codePoints = countCodePoints(utf8);
    startCodePoint += codePoints;
    // before beginning of string
    if (startCodePoint < 0) {
        return Slices.EMPTY_SLICE;
    }
    int indexStart = offsetOfCodePoint(utf8, startCodePoint);
    int indexEnd;
    if (startCodePoint + lengthCodePoints < codePoints) {
        indexEnd = offsetOfCodePoint(utf8, indexStart, lengthCodePoints);
    } else {
        indexEnd = utf8.length();
    }
    return utf8.slice(indexStart, indexEnd - indexStart);
}
Also used : Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) 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 12 with LiteralParameters

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

the class StringFunctions method levenshteinDistance.

@Description("Computes Levenshtein distance between two strings")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType(StandardTypes.BIGINT)
public static long levenshteinDistance(@SqlType("varchar(x)") Slice left, @SqlType("varchar(y)") Slice right) {
    int[] leftCodePoints = castToCodePoints(left);
    int[] rightCodePoints = castToCodePoints(right);
    if (leftCodePoints.length < rightCodePoints.length) {
        int[] tempCodePoints = leftCodePoints;
        leftCodePoints = rightCodePoints;
        rightCodePoints = tempCodePoints;
    }
    if (rightCodePoints.length == 0) {
        return leftCodePoints.length;
    }
    checkCondition((leftCodePoints.length * (rightCodePoints.length - 1)) <= 1_000_000, INVALID_FUNCTION_ARGUMENT, "The combined inputs for Levenshtein distance are too large");
    int[] distances = new int[rightCodePoints.length];
    for (int i = 0; i < rightCodePoints.length; i++) {
        distances[i] = i + 1;
    }
    for (int i = 0; i < leftCodePoints.length; i++) {
        int leftUpDistance = distances[0];
        if (leftCodePoints[i] == rightCodePoints[0]) {
            distances[0] = i;
        } else {
            distances[0] = Math.min(i, distances[0]) + 1;
        }
        for (int j = 1; j < rightCodePoints.length; j++) {
            int leftUpDistanceNext = distances[j];
            if (leftCodePoints[i] == rightCodePoints[j]) {
                distances[j] = leftUpDistance;
            } else {
                distances[j] = Math.min(distances[j - 1], Math.min(leftUpDistance, distances[j])) + 1;
            }
            leftUpDistance = leftUpDistanceNext;
        }
    }
    return distances[rightCodePoints.length - 1];
}
Also used : Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) 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 13 with LiteralParameters

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

the class StringFunctions method translate.

@Description("Translate characters from the source string based on original and translations strings")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
@SqlType(StandardTypes.VARCHAR)
public static Slice translate(@SqlType("varchar(x)") Slice source, @SqlType("varchar(y)") Slice from, @SqlType("varchar(z)") Slice to) {
    int[] fromCodePoints = castToCodePoints(from);
    int[] toCodePoints = castToCodePoints(to);
    Int2IntOpenHashMap map = new Int2IntOpenHashMap(fromCodePoints.length);
    for (int index = 0; index < fromCodePoints.length; index++) {
        int fromCodePoint = fromCodePoints[index];
        map.putIfAbsent(fromCodePoint, index < toCodePoints.length ? toCodePoints[index] : -1);
    }
    int[] sourceCodePoints = castToCodePoints(source);
    int[] targetCodePoints = new int[sourceCodePoints.length];
    int targetPositions = 0;
    int targetBytes = 0;
    for (int index = 0; index < sourceCodePoints.length; index++) {
        int codePoint = sourceCodePoints[index];
        if (map.containsKey(codePoint)) {
            int translatedCodePoint = map.get(codePoint);
            if (translatedCodePoint == -1) {
                continue;
            }
            codePoint = translatedCodePoint;
        }
        targetCodePoints[targetPositions++] = codePoint;
        targetBytes += lengthOfCodePoint(codePoint);
    }
    Slice target = Slices.allocate(targetBytes);
    int offset = 0;
    for (int index = 0; index < targetPositions; index++) {
        offset += setCodePointAt(targetCodePoints[index], target, offset);
    }
    return target;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Int2IntOpenHashMap(it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap) Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) 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 14 with LiteralParameters

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

the class TimeToTimestampWithTimeZoneCast method castToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("timestamp(targetPrecision) with time zone")
public static LongTimestampWithTimeZone castToLong(@LiteralParameter("sourcePrecision") long sourcePrecision, @LiteralParameter("targetPrecision") long targetPrecision, ConnectorSession session, @SqlType("time(sourcePrecision)") long time) {
    ZoneId zoneId = session.getTimeZoneKey().getZoneId();
    long epochSeconds = getEpochSeconds(session, time, zoneId);
    long picoFraction = getPicoFraction(sourcePrecision, targetPrecision, time);
    long epochMillis = computeEpochMillis(session, zoneId, epochSeconds, picoFraction);
    int picosOfMilli = (int) (picoFraction % PICOSECONDS_PER_MILLISECOND);
    return LongTimestampWithTimeZone.fromEpochMillisAndFraction(epochMillis, picosOfMilli, session.getTimeZoneKey().getKey());
}
Also used : ZoneId(java.time.ZoneId) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 15 with LiteralParameters

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

the class TimeToTimestampWithTimeZoneCast method castToShort.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("timestamp(targetPrecision) with time zone")
public static long castToShort(@LiteralParameter("sourcePrecision") long sourcePrecision, @LiteralParameter("targetPrecision") long targetPrecision, ConnectorSession session, @SqlType("time(sourcePrecision)") long time) {
    ZoneId zoneId = session.getTimeZoneKey().getZoneId();
    long epochSeconds = getEpochSeconds(session, time, zoneId);
    long picoFraction = getPicoFraction(sourcePrecision, targetPrecision, time);
    long epochMillis = computeEpochMillis(session, zoneId, epochSeconds, picoFraction);
    return packDateTimeWithZone(epochMillis, session.getTimeZoneKey());
}
Also used : ZoneId(java.time.ZoneId) 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