Search in sources :

Example 31 with ScalarFunction

use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.

the class UrlFunctions method urlExtractParameter.

@SqlNullable
@Description("extract query parameter from url")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice urlExtractParameter(@SqlType("varchar(x)") Slice url, @SqlType("varchar(y)") Slice parameterName) {
    URI uri = parseUrl(url);
    if ((uri == null) || (uri.getQuery() == null)) {
        return null;
    }
    Slice query = slice(uri.getQuery());
    String parameter = parameterName.toStringUtf8();
    Iterable<String> queryArgs = QUERY_SPLITTER.split(query.toStringUtf8());
    for (String queryArg : queryArgs) {
        Iterator<String> arg = ARG_SPLITTER.split(queryArg).iterator();
        if (arg.next().equals(parameter)) {
            if (arg.hasNext()) {
                return utf8Slice(arg.next());
            }
            // first matched key is empty
            return Slices.EMPTY_SLICE;
        }
    }
    // no key matched
    return null;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) URI(java.net.URI) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 32 with ScalarFunction

use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.

the class VarbinaryFunctions method xxhash64.

@Description("compute xxhash64 hash")
@ScalarFunction
@SqlType(StandardTypes.VARBINARY)
public static Slice xxhash64(@SqlType(StandardTypes.VARBINARY) Slice slice) {
    Slice hash = Slices.allocate(Long.BYTES);
    hash.setLong(0, Long.reverseBytes(XxHash64.hash(slice)));
    return hash;
}
Also used : Slice(io.airlift.slice.Slice) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 33 with ScalarFunction

use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.

the class SequenceFunction method sequenceTimestampYearToMonth.

@ScalarFunction("sequence")
@SqlType("array(timestamp)")
public static Block sequenceTimestampYearToMonth(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long start, @SqlType(StandardTypes.TIMESTAMP) long end, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long step) {
    checkCondition(step != 0, INVALID_FUNCTION_ARGUMENT, "interval must not be zero");
    checkCondition(step > 0 ? end >= start : end <= start, INVALID_FUNCTION_ARGUMENT, "sequence end value should be greater than or equal to start value if step is greater than zero otherwise end should be less than start");
    int length = toIntExact(diffTimestamp(session, MONTH, start, end) / step + 1);
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), length);
    int value = 0;
    for (int i = 0; i < length; ++i) {
        BIGINT.writeLong(blockBuilder, DateTimeOperators.timestampPlusIntervalYearToMonth(session, start, value));
        value += step;
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 34 with ScalarFunction

use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.

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(com.facebook.presto.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 35 with ScalarFunction

use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.

the class MathFunctions method widthBucket.

@Description("The bucket number of a value given an array of bins")
@ScalarFunction("width_bucket")
@SqlType(StandardTypes.BIGINT)
public static long widthBucket(@SqlType(StandardTypes.DOUBLE) double operand, @SqlType("array(double)") Block bins) {
    int numberOfBins = bins.getPositionCount();
    checkCondition(numberOfBins > 0, INVALID_FUNCTION_ARGUMENT, "Bins cannot be an empty array");
    checkCondition(!isNaN(operand), INVALID_FUNCTION_ARGUMENT, "Operand cannot be NaN");
    int lower = 0;
    int upper = numberOfBins;
    int index;
    double bin;
    while (lower < upper) {
        if (DOUBLE.getDouble(bins, lower) > DOUBLE.getDouble(bins, upper - 1)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Bin values are not sorted in ascending order");
        }
        index = (lower + upper) / 2;
        bin = DOUBLE.getDouble(bins, index);
        checkCondition(isFinite(bin), INVALID_FUNCTION_ARGUMENT, format("Bin value must be finite, got %s", bin));
        if (operand < bin) {
            upper = index;
        } else {
            lower = index + 1;
        }
    }
    return lower;
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) Constraint(com.facebook.presto.type.Constraint) DecimalOperators.modulusScalarFunction(com.facebook.presto.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)37 SqlType (com.facebook.presto.spi.function.SqlType)36 Description (com.facebook.presto.spi.function.Description)19 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)17 Constraint (com.facebook.presto.type.Constraint)15 Slice (io.airlift.slice.Slice)10 SqlNullable (com.facebook.presto.spi.function.SqlNullable)9 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)7 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 IOException (java.io.IOException)7 PrestoException (com.facebook.presto.spi.PrestoException)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParser (com.fasterxml.jackson.core.JsonParser)6 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Matcher (io.airlift.joni.Matcher)5 ISOChronology (org.joda.time.chrono.ISOChronology)4 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3