Search in sources :

Example 1 with SqlType

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

the class CreateHll method createHll.

@ScalarFunction
@SqlType(StandardTypes.HYPER_LOG_LOG)
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) {
    HyperLogLog hll = HyperLogLog.newInstance(4096);
    hll.add(value);
    return hll.serialize();
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with SqlType

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

the class BindableAggregationFunction method getParameterMetadata.

private static List<ParameterMetadata> getParameterMetadata(@Nullable Method method, List<Type> inputTypes) {
    if (method == null) {
        return null;
    }
    ImmutableList.Builder<ParameterMetadata> builder = ImmutableList.builder();
    Annotation[][] annotations = method.getParameterAnnotations();
    String methodName = method.getDeclaringClass() + "." + method.getName();
    checkArgument(annotations.length > 0, "At least @AggregationState argument is required for each of aggregation functions.");
    int inputId = 0;
    int i = 0;
    if (annotations[0].length == 0) {
        // Backward compatibility - first argument without annotations is interpreted as State argument
        builder.add(new ParameterMetadata(STATE));
        i++;
    }
    for (; i < annotations.length; i++) {
        Annotation baseTypeAnnotation = baseTypeAnnotation(annotations[i], methodName);
        if (baseTypeAnnotation instanceof SqlType) {
            builder.add(fromSqlType(inputTypes.get(i - 1), isParameterBlock(annotations[i]), isParameterNullable(annotations[i]), methodName));
        } else if (baseTypeAnnotation instanceof BlockIndex) {
            builder.add(new ParameterMetadata(BLOCK_INDEX));
        } else if (baseTypeAnnotation instanceof AggregationState) {
            builder.add(new ParameterMetadata(STATE));
        } else {
            throw new IllegalArgumentException("Unsupported annotation: " + annotations[i]);
        }
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) SqlType(com.facebook.presto.spi.function.SqlType) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata) AggregationState(com.facebook.presto.spi.function.AggregationState) Annotation(java.lang.annotation.Annotation)

Example 3 with SqlType

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

the class AggregationCompiler method getInputTypesSignatures.

private static List<TypeSignature> getInputTypesSignatures(Method inputFunction) {
    // FIXME Literal parameters should be part of class annotations.
    ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
    Set<String> literalParameters = getLiteralParameter(inputFunction);
    Annotation[][] parameterAnnotations = inputFunction.getParameterAnnotations();
    for (Annotation[] annotations : parameterAnnotations) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof SqlType) {
                String typeName = ((SqlType) annotation).value();
                builder.add(parseTypeSignature(typeName, literalParameters));
            }
        }
    }
    return builder.build();
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) SqlType(com.facebook.presto.spi.function.SqlType) Annotation(java.lang.annotation.Annotation)

Example 4 with SqlType

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

the class ArrayUnionFunction method bigintUnion.

@SqlType("array(bigint)")
public static Block bigintUnion(@SqlType("array(bigint)") Block leftArray, @SqlType("array(bigint)") Block rightArray) {
    int leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    LongSet set = new LongOpenHashSet(leftArrayCount + rightArrayCount);
    BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), leftArrayCount + rightArrayCount);
    AtomicBoolean containsNull = new AtomicBoolean(false);
    appendBigintArray(leftArray, containsNull, set, distinctElementBlockBuilder);
    appendBigintArray(rightArray, containsNull, set, distinctElementBlockBuilder);
    return distinctElementBlockBuilder.build();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongSet(it.unimi.dsi.fastutil.longs.LongSet) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with SqlType

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

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