Search in sources :

Example 1 with AggregationState

use of com.facebook.presto.spi.function.AggregationState 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 2 with AggregationState

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

the class BindableAggregationFunction method baseTypeAnnotation.

private static Annotation baseTypeAnnotation(Annotation[] annotations, String methodName) {
    List<Annotation> baseTypes = Arrays.asList(annotations).stream().filter(annotation -> annotation instanceof SqlType || annotation instanceof BlockIndex || annotation instanceof AggregationState).collect(toImmutableList());
    checkArgument(baseTypes.size() == 1, "Parameter of %s must have exactly one of @SqlType, @BlockIndex", methodName);
    boolean nullable = isParameterNullable(annotations);
    boolean isBlock = isParameterBlock(annotations);
    Annotation annotation = baseTypes.get(0);
    checkArgument((!isBlock && !nullable) || (annotation instanceof SqlType), "%s contains a parameter with @BlockPosition and/or @NullablePosition that is not @SqlType", methodName);
    return annotation;
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) MethodHandle(java.lang.invoke.MethodHandle) Arrays(java.util.Arrays) AggregationCompiler.isParameterBlock(com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterBlock) TypeManager(com.facebook.presto.spi.type.TypeManager) AggregationUtils.generateAggregationName(com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName) MethodHandles.lookup(java.lang.invoke.MethodHandles.lookup) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) AggregationState(com.facebook.presto.spi.function.AggregationState) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) Type(com.facebook.presto.spi.type.Type) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlAggregationFunction(com.facebook.presto.metadata.SqlAggregationFunction) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) AggregationCompiler.isParameterNullable(com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterNullable) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) BoundVariables(com.facebook.presto.metadata.BoundVariables) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) Signature(com.facebook.presto.metadata.Signature) Throwables(com.google.common.base.Throwables) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) BLOCK_INDEX(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX) List(java.util.List) AccumulatorStateSerializer(com.facebook.presto.spi.function.AccumulatorStateSerializer) Annotation(java.lang.annotation.Annotation) AccumulatorStateFactory(com.facebook.presto.spi.function.AccumulatorStateFactory) StateCompiler(com.facebook.presto.operator.aggregation.state.StateCompiler) SqlType(com.facebook.presto.spi.function.SqlType) STATE(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) SqlType(com.facebook.presto.spi.function.SqlType) AggregationState(com.facebook.presto.spi.function.AggregationState) Annotation(java.lang.annotation.Annotation)

Example 3 with AggregationState

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

the class AggregationCompiler method findAggregationStateParamId.

public static int findAggregationStateParamId(Method method, int id) {
    int currentParamId = 0;
    int found = 0;
    for (Annotation[] annotations : method.getParameterAnnotations()) {
        for (Annotation annotation : annotations) {
            if (annotation instanceof AggregationState) {
                if (found++ == id) {
                    return currentParamId;
                }
            }
        }
        currentParamId++;
    }
    // some third party aggregates may assume that State will be id-th parameter
    return id;
}
Also used : AggregationState(com.facebook.presto.spi.function.AggregationState) Constraint(com.facebook.presto.type.Constraint) LongVariableConstraint(com.facebook.presto.metadata.LongVariableConstraint) Annotation(java.lang.annotation.Annotation)

Aggregations

AggregationState (com.facebook.presto.spi.function.AggregationState)3 Annotation (java.lang.annotation.Annotation)3 ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)2 ParameterMetadata.fromSqlType (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType)2 SqlType (com.facebook.presto.spi.function.SqlType)2 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)2 ImmutableList (com.google.common.collect.ImmutableList)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)1 LongVariableConstraint (com.facebook.presto.metadata.LongVariableConstraint)1 Signature (com.facebook.presto.metadata.Signature)1 SignatureBinder.applyBoundVariables (com.facebook.presto.metadata.SignatureBinder.applyBoundVariables)1 SqlAggregationFunction (com.facebook.presto.metadata.SqlAggregationFunction)1 AggregationCompiler.isParameterBlock (com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterBlock)1 AggregationCompiler.isParameterNullable (com.facebook.presto.operator.aggregation.AggregationCompiler.isParameterNullable)1 BLOCK_INDEX (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX)1 STATE (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE)1 AggregationUtils.generateAggregationName (com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName)1 StateCompiler (com.facebook.presto.operator.aggregation.state.StateCompiler)1