Search in sources :

Example 1 with LiteralParameters

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

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

Example 2 with LiteralParameters

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

the class AggregationCompiler method getLiteralParameter.

private static Set<String> getLiteralParameter(Method inputFunction) {
    ImmutableSet.Builder<String> literalParametersBuilder = ImmutableSet.builder();
    Annotation[] literalParameters = inputFunction.getAnnotations();
    for (Annotation annotation : literalParameters) {
        if (annotation instanceof LiteralParameters) {
            for (String literal : ((LiteralParameters) annotation).value()) {
                literalParametersBuilder.add(literal);
            }
        }
    }
    return literalParametersBuilder.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) Annotation(java.lang.annotation.Annotation)

Example 3 with LiteralParameters

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

the class CharacterStringCasts method varcharToCharSaturatedFloorCast.

@ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
@SqlType("char(y)")
@LiteralParameters({ "x", "y" })
public static // Char(y) value that is smaller than the original Varchar(x) value. This is fine though for usage in TupleDomainTranslator.
Slice varcharToCharSaturatedFloorCast(@LiteralParameter("y") Long y, @SqlType("varchar(x)") Slice slice) {
    Slice trimmedSlice = trimSpaces(slice);
    int trimmedTextLength = countCodePoints(trimmedSlice);
    int numberOfTrailingSpaces = slice.length() - trimmedSlice.length();
    // if Varchar(x) value length (including spaces) is greater than y, we can just truncate it
    if (trimmedTextLength + numberOfTrailingSpaces >= y) {
        return truncateToLength(trimmedSlice, y.intValue());
    }
    if (trimmedTextLength == 0) {
        return EMPTY_SLICE;
    }
    // and also remove one additional trailing character to get smaller Char(y) value
    return trimmedSlice.slice(0, offsetOfCodePoint(trimmedSlice, trimmedTextLength - 1));
}
Also used : Slice(io.airlift.slice.Slice) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with LiteralParameters

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

the class ColorFunctions method color.

@ScalarFunction
@LiteralParameters("x")
@SqlType(ColorType.NAME)
public static long color(@SqlType("varchar(x)") Slice color) {
    int rgb = parseRgb(color);
    if (rgb != -1) {
        return rgb;
    }
    // encode system colors (0-15) as negative values, offset by one
    try {
        SystemColor systemColor = SystemColor.valueOf(upper(color).toStringUtf8());
        int index = systemColor.getIndex();
        return -(index + 1);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid color: '%s'", color.toStringUtf8()), e);
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) Constraint(com.facebook.presto.type.Constraint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with LiteralParameters

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

the class JsonFunctions method jsonParse.

@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.JSON)
public static Slice jsonParse(@SqlType("varchar(x)") Slice slice) {
    try {
        byte[] in = slice.getBytes();
        SliceOutput dynamicSliceOutput = new DynamicSliceOutput(in.length);
        SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(in, Object.class));
        return dynamicSliceOutput.slice();
    } catch (Exception e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert '%s' to JSON", slice.toStringUtf8()));
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) PrestoException(com.facebook.presto.spi.PrestoException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) 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