Search in sources :

Example 21 with LiteralParameters

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

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToLong.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone longToLong(@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));
    return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
Also used : LongTimeWithTimeZone(io.trino.spi.type.LongTimeWithTimeZone) 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 22 with LiteralParameters

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

the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToShort.

@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long shortToShort(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") long packedTimestamp) {
    // source precision is <= 3
    // target precision is <= 9
    TimeZoneKey zoneKey = unpackZoneKey(packedTimestamp);
    long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(packedTimestamp));
    if (targetPrecision <= 3) {
        epochMillis = round(epochMillis, (int) (3 - targetPrecision));
    }
    long nanos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 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)

Example 23 with LiteralParameters

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

the class ApproximateSetAggregation method input.

@InputFunction
@LiteralParameters("x")
public static void input(@AggregationState HyperLogLogState state, @SqlType("varchar(x)") Slice value) {
    HyperLogLog hll = getOrCreateHyperLogLog(state);
    state.addMemoryUsage(-hll.estimatedInMemorySize());
    hll.add(value);
    state.addMemoryUsage(hll.estimatedInMemorySize());
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) LiteralParameters(io.trino.spi.function.LiteralParameters) InputFunction(io.trino.spi.function.InputFunction)

Example 24 with LiteralParameters

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

the class JoniRegexpFunctions method regexpPosition.

@ScalarFunction
@Description("Returns the index of the n-th matched substring starting from the specified position")
@LiteralParameters("x")
@SqlType(StandardTypes.INTEGER)
public static long regexpPosition(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.INTEGER) long start, @SqlType(StandardTypes.INTEGER) long occurrence) {
    // start position cannot be smaller than 1
    if (start < 1) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "start position cannot be smaller than 1");
    }
    // occurrence cannot be smaller than 1
    if (occurrence < 1) {
        throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "occurrence cannot be smaller than 1");
    }
    // returns -1 if start is greater than the length of source
    if (start > SliceUtf8.countCodePoints(source)) {
        return -1;
    }
    Matcher matcher = pattern.matcher(source.getBytes());
    long count = 0;
    // convert char position to byte position
    // subtract 1 because codePointCount starts from zero
    int nextStart = SliceUtf8.offsetOfCodePoint(source, (int) start - 1);
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        // Check whether offset is negative, offset is -1 if no pattern was found or -2 if process was interrupted
        if (offset < 0) {
            return -1;
        }
        if (++count == occurrence) {
            // Plus 1 because position returned start from 1
            return SliceUtf8.countCodePoints(source, 0, matcher.getBegin()) + 1;
        }
        nextStart = getNextStart(source, matcher);
    }
}
Also used : Matcher(io.airlift.joni.Matcher) TrinoException(io.trino.spi.TrinoException) 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 25 with LiteralParameters

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

the class JoniRegexpFunctions method regexpReplace.

@Description("Replaces substrings matching a regular expression by given string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
// to get the formula: x + max(x * y / 2, y) * (x + 1)
@Constraint(variable = "z", expression = "min(2147483647, x + max(x * y / 2, y) * (x + 1))")
@SqlType("varchar(z)")
public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType("varchar(y)") Slice replacement) {
    Matcher matcher = pattern.matcher(source.getBytes());
    SliceOutput sliceOutput = new DynamicSliceOutput(source.length() + replacement.length() * 5);
    int lastEnd = 0;
    // nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        sliceOutput.appendBytes(sliceBetweenReplacements);
        appendReplacement(sliceOutput, source, pattern.regex(), matcher.getEagerRegion(), replacement);
    }
    sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
    return sliceOutput.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Constraint(io.trino.type.Constraint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) Constraint(io.trino.type.Constraint) 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