Search in sources :

Example 21 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters 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)

Aggregations

LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)21 SqlType (com.facebook.presto.spi.function.SqlType)19 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)17 Description (com.facebook.presto.spi.function.Description)12 Constraint (com.facebook.presto.type.Constraint)12 Slice (io.airlift.slice.Slice)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)6 PrestoException (com.facebook.presto.spi.PrestoException)5 SqlNullable (com.facebook.presto.spi.function.SqlNullable)5 Matcher (io.airlift.joni.Matcher)5 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 IOException (java.io.IOException)3 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)2 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 Region (io.airlift.joni.Region)2 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)2