Search in sources :

Example 41 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class StringFunctions method concat.

// TODO: implement N arguments char concat
@Description("concatenates given character strings")
@ScalarFunction
@LiteralParameters({ "x", "y", "u" })
@Constraint(variable = "u", expression = "x + y")
@SqlType("char(u)")
public static Slice concat(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice left, @SqlType("char(y)") Slice right) {
    int rightLength = right.length();
    if (rightLength == 0) {
        return left;
    }
    Slice paddedLeft = padSpaces(left, x.intValue());
    int leftLength = paddedLeft.length();
    Slice result = Slices.allocate(leftLength + rightLength);
    result.setBytes(0, paddedLeft);
    result.setBytes(leftLength, right);
    return result;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Constraint(io.prestosql.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) Constraint(io.prestosql.type.Constraint) SqlType(io.prestosql.spi.function.SqlType)

Example 42 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class StringFunctions method hammingDistance.

@Description("computes Hamming distance between two strings")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType(StandardTypes.BIGINT)
public static long hammingDistance(@SqlType("varchar(x)") Slice left, @SqlType("varchar(y)") Slice right) {
    int distance = 0;
    int leftPosition = 0;
    int rightPosition = 0;
    while (leftPosition < left.length() && rightPosition < right.length()) {
        int codePointLeft = tryGetCodePointAt(left, leftPosition);
        int codePointRight = tryGetCodePointAt(right, rightPosition);
        // the following code treats them as equal if they happen to be of the same length
        if (codePointLeft != codePointRight) {
            distance++;
        }
        leftPosition += codePointLeft > 0 ? lengthOfCodePoint(codePointLeft) : -codePointLeft;
        rightPosition += codePointRight > 0 ? lengthOfCodePoint(codePointRight) : -codePointRight;
    }
    checkCondition(leftPosition == left.length() && rightPosition == right.length(), INVALID_FUNCTION_ARGUMENT, "The input strings to hamming_distance function must have the same length");
    return distance;
}
Also used : Constraint(io.prestosql.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 43 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class QuantileDigestFunctions method valuesAtQuantilesDouble.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(double)")
public static Block valuesAtQuantilesDouble(@SqlType("qdigest(double)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        DOUBLE.writeDouble(output, sortableLongToDouble(digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i))));
    }
    return output.build();
}
Also used : QuantileDigest(io.airlift.stats.QuantileDigest) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 44 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class QuantileDigestFunctions method valuesAtQuantilesReal.

@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the qdigest is qn.")
@SqlType("array(real)")
public static Block valuesAtQuantilesReal(@SqlType("qdigest(real)") Slice input, @SqlType("array(double)") Block percentilesArrayBlock) {
    QuantileDigest digest = new QuantileDigest(input);
    BlockBuilder output = REAL.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
    for (int i = 0; i < percentilesArrayBlock.getPositionCount(); i++) {
        REAL.writeLong(output, floatToRawIntBits(sortableIntToFloat((int) digest.getQuantile(DOUBLE.getDouble(percentilesArrayBlock, i)))));
    }
    return output.build();
}
Also used : QuantileDigest(io.airlift.stats.QuantileDigest) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) SqlType(io.prestosql.spi.function.SqlType)

Example 45 with Description

use of io.prestosql.spi.function.Description in project hetu-core by openlookeng.

the class JoniRegexpFunctions method regexpExtractAll.

@Description("group(s) extracted using the given pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("array(varchar(x))")
public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int group = toIntExact(groupIndex);
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        Region region = matcher.getEagerRegion();
        int beg = region.beg[group];
        int end = region.end[group];
        if (beg == -1 || end == -1) {
            blockBuilder.appendNull();
        } else {
            Slice slice = source.slice(beg, end - beg);
            VARCHAR.writeSlice(blockBuilder, slice);
        }
    }
    return blockBuilder.build();
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(io.prestosql.type.Constraint) BlockBuilder(io.prestosql.spi.block.BlockBuilder) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

Description (io.prestosql.spi.function.Description)76 ScalarFunction (io.prestosql.spi.function.ScalarFunction)76 SqlType (io.prestosql.spi.function.SqlType)76 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)40 SqlNullable (io.prestosql.spi.function.SqlNullable)34 Slice (io.airlift.slice.Slice)18 LiteralParameters (io.prestosql.spi.function.LiteralParameters)15 Point (com.esri.core.geometry.Point)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)14 Constraint (io.prestosql.type.Constraint)13 MultiPoint (com.esri.core.geometry.MultiPoint)12 BlockBuilder (io.prestosql.spi.block.BlockBuilder)11 PrestoException (io.prestosql.spi.PrestoException)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 GeometryType (io.prestosql.geospatial.GeometryType)6 Envelope (com.esri.core.geometry.Envelope)5 MultiPath (com.esri.core.geometry.MultiPath)5 Matcher (io.airlift.joni.Matcher)5 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)5