Search in sources :

Example 41 with SqlType

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

the class ArrayUnionFunction method union.

@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    TypedSet typedSet = new TypedSet(type, leftArrayCount + rightArrayCount);
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftArrayCount + rightArrayCount);
    appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
    appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 42 with SqlType

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

the class ColorFunctions method bar.

@ScalarFunction
@SqlType(StandardTypes.VARCHAR)
public static Slice bar(@SqlType(StandardTypes.DOUBLE) double percent, @SqlType(StandardTypes.BIGINT) long width, @SqlType(ColorType.NAME) long lowColor, @SqlType(ColorType.NAME) long highColor) {
    long count = (int) (percent * width);
    count = Math.min(width, count);
    count = Math.max(0, count);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < count; i++) {
        float fraction = (float) (i * 1.0 / (width - 1));
        int color = interpolate(fraction, lowColor, highColor);
        builder.append(ansiColorEscape(color)).append('█');
    }
    // reset
    builder.append(ANSI_RESET);
    // pad to force column to be the requested width
    for (long i = count; i < width; ++i) {
        builder.append(' ');
    }
    return utf8Slice(builder.toString());
}
Also used : Constraint(com.facebook.presto.type.Constraint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 43 with SqlType

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

the class JoniRegexpFunctions method regexpExtract.

@SqlNullable
@Description("returns regex group of extracted string with a pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice regexpExtract(@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());
    int group = toIntExact(groupIndex);
    int offset = matcher.search(0, source.length(), Option.DEFAULT);
    if (offset == -1) {
        return null;
    }
    Region region = matcher.getEagerRegion();
    int beg = region.beg[group];
    int end = region.end[group];
    if (beg == -1) {
        // end == -1 must be true
        return null;
    }
    Slice slice = source.slice(beg, end - beg);
    return slice;
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(com.facebook.presto.type.Constraint) 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 44 with SqlType

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

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) Regex 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;
        }
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        sliceOutput.appendBytes(sliceBetweenReplacements);
        appendReplacement(sliceOutput, source, pattern, 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(com.facebook.presto.type.Constraint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) Constraint(com.facebook.presto.type.Constraint) SqlType(com.facebook.presto.spi.function.SqlType)

Example 45 with SqlType

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

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(new BlockBuilderStatus(), 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(com.facebook.presto.type.Constraint) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) 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

SqlType (com.facebook.presto.spi.function.SqlType)74 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)36 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)24 TypeParameter (com.facebook.presto.spi.function.TypeParameter)24 PrestoException (com.facebook.presto.spi.PrestoException)22 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)20 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)19 Description (com.facebook.presto.spi.function.Description)18 Constraint (com.facebook.presto.type.Constraint)15 SqlNullable (com.facebook.presto.spi.function.SqlNullable)14 Slice (io.airlift.slice.Slice)13 IOException (java.io.IOException)12 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)11 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)11 JsonParser (com.fasterxml.jackson.core.JsonParser)11 TypeParameterSpecialization (com.facebook.presto.spi.function.TypeParameterSpecialization)9 JsonToken (com.fasterxml.jackson.core.JsonToken)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)6 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)5