Search in sources :

Example 1 with AggregationFunction

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

the class AggregationCompiler method generateBindableAggregationFunctions.

public static List<BindableAggregationFunction> generateBindableAggregationFunctions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    DynamicClassLoader classLoader = new DynamicClassLoader(aggregationDefinition.getClassLoader());
    ImmutableList.Builder<BindableAggregationFunction> builder = ImmutableList.builder();
    for (Class<?> stateClass : getStateClasses(aggregationDefinition)) {
        AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
        for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
            for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
                List<LongVariableConstraint> longVariableConstraints = parseLongVariableConstraints(inputFunction);
                for (String name : getNames(outputFunction, aggregationAnnotation)) {
                    List<TypeSignature> inputTypes = getInputTypesSignatures(inputFunction);
                    TypeSignature outputType = TypeSignature.parseTypeSignature(outputFunction.getAnnotation(OutputFunction.class).value());
                    builder.add(new BindableAggregationFunction(new Signature(name, FunctionKind.AGGREGATE, // TODO parse constrains from annotations
                    ImmutableList.of(), longVariableConstraints, outputType, inputTypes, false), getDescription(aggregationDefinition, outputFunction), aggregationAnnotation.decomposable(), aggregationDefinition, stateClass, inputFunction, outputFunction));
                }
            }
        }
    }
    return builder.build();
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Method(java.lang.reflect.Method) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) LongVariableConstraint(com.facebook.presto.metadata.LongVariableConstraint) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 2 with AggregationFunction

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

the class AggregationFromAnnotationsParser method parseFunctionDefinitions.

public static List<ParametricAggregation> parseFunctionDefinitions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    ImmutableList.Builder<ParametricAggregation> builder = ImmutableList.builder();
    for (Class<?> stateClass : getStateClasses(aggregationDefinition)) {
        Method combineFunction = getCombineFunction(aggregationDefinition, stateClass);
        Optional<Method> aggregationStateSerializerFactory = getAggregationStateSerializerFactory(aggregationDefinition, stateClass);
        for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
            for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
                for (AggregationHeader header : parseHeaders(aggregationDefinition, outputFunction)) {
                    AggregationImplementation onlyImplementation = parseImplementation(aggregationDefinition, header, stateClass, inputFunction, outputFunction, combineFunction, aggregationStateSerializerFactory);
                    ParametricImplementationsGroup<AggregationImplementation> implementations = ParametricImplementationsGroup.of(onlyImplementation);
                    builder.add(new ParametricAggregation(implementations.getSignature(), header, implementations));
                }
            }
        }
    }
    return builder.build();
}
Also used : AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Method(java.lang.reflect.Method)

Example 3 with AggregationFunction

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

the class AggregationFromAnnotationsParser method parseHeader.

private static AggregationHeader parseHeader(AnnotatedElement aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    return new AggregationHeader(aggregationAnnotation.value(), parseDescription(aggregationDefinition), aggregationAnnotation.decomposable(), aggregationAnnotation.isOrderSensitive(), aggregationAnnotation.visibility());
}
Also used : AggregationFunction(com.facebook.presto.spi.function.AggregationFunction)

Example 4 with AggregationFunction

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

the class BindableAggregationFunction method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables variables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    // bind variables
    Signature boundSignature = applyBoundVariables(getSignature(), variables, arity);
    List<Type> inputTypes = boundSignature.getArgumentTypes().stream().map(x -> typeManager.getType(x)).collect(toImmutableList());
    Type outputType = typeManager.getType(boundSignature.getReturnType());
    AggregationFunction aggregationAnnotation = definitionClass.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    DynamicClassLoader classLoader = new DynamicClassLoader(definitionClass.getClassLoader(), getClass().getClassLoader());
    AggregationMetadata metadata;
    AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    Method combineFunction = AggregationCompiler.getCombineFunction(definitionClass, stateClass);
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClass, classLoader);
    try {
        MethodHandle inputHandle = lookup().unreflect(inputFunction);
        MethodHandle combineHandle = lookup().unreflect(combineFunction);
        MethodHandle outputHandle = outputFunction == null ? null : lookup().unreflect(outputFunction);
        metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), outputType.getTypeSignature(), signaturesFromTypes(inputTypes)), getParameterMetadata(inputFunction, inputTypes), inputHandle, combineHandle, outputHandle, stateClass, stateSerializer, stateFactory, outputType);
    } catch (IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
    AccumulatorFactoryBinder factory = new LazyAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, outputType, decomposable, factory);
}
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) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Method(java.lang.reflect.Method) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) SqlAggregationFunction(com.facebook.presto.metadata.SqlAggregationFunction) Type(com.facebook.presto.spi.type.Type) ParameterMetadata.fromSqlType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType) SqlType(com.facebook.presto.spi.function.SqlType) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) MethodHandle(java.lang.invoke.MethodHandle)

Example 5 with AggregationFunction

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

the class AggregationCompiler method getNames.

private static List<String> getNames(@Nullable Method outputFunction, AggregationFunction aggregationAnnotation) {
    List<String> defaultNames = ImmutableList.<String>builder().add(aggregationAnnotation.value()).addAll(Arrays.asList(aggregationAnnotation.alias())).build();
    if (outputFunction == null) {
        return defaultNames;
    }
    AggregationFunction annotation = outputFunction.getAnnotation(AggregationFunction.class);
    if (annotation == null) {
        return defaultNames;
    } else {
        return ImmutableList.<String>builder().add(annotation.value()).addAll(Arrays.asList(annotation.alias())).build();
    }
}
Also used : AggregationFunction(com.facebook.presto.spi.function.AggregationFunction)

Aggregations

AggregationFunction (com.facebook.presto.spi.function.AggregationFunction)6 ImmutableList (com.google.common.collect.ImmutableList)3 Method (java.lang.reflect.Method)3 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)2 Signature (com.facebook.presto.metadata.Signature)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)2 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)1 LongVariableConstraint (com.facebook.presto.metadata.LongVariableConstraint)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 ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)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 ParameterMetadata.fromSqlType (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.fromSqlType)1 AggregationUtils.generateAggregationName (com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName)1 StateCompiler (com.facebook.presto.operator.aggregation.state.StateCompiler)1