Search in sources :

Example 1 with AccumulatorStateSerializer

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

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

the class StateCompiler method generateStateSerializer.

public static <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
    AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
    if (metadata != null && metadata.stateSerializerClass() != void.class) {
        try {
            return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            throw Throwables.propagate(e);
        }
    }
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(clazz.getSimpleName() + "Serializer"), type(Object.class), type(AccumulatorStateSerializer.class));
    CallSiteBinder callSiteBinder = new CallSiteBinder();
    // Generate constructor
    definition.declareDefaultConstructor(a(PUBLIC));
    List<StateField> fields = enumerateFields(clazz, fieldTypes);
    generateGetSerializedType(definition, fields, callSiteBinder);
    generateSerialize(definition, callSiteBinder, clazz, fields);
    generateDeserialize(definition, callSiteBinder, clazz, fields);
    Class<? extends AccumulatorStateSerializer> serializerClass = defineClass(definition, AccumulatorStateSerializer.class, callSiteBinder.getBindings(), classLoader);
    try {
        return (AccumulatorStateSerializer<T>) serializerClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}
Also used : AccumulatorStateMetadata(com.facebook.presto.spi.function.AccumulatorStateMetadata) AccumulatorStateSerializer(com.facebook.presto.spi.function.AccumulatorStateSerializer) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InvocationTargetException(java.lang.reflect.InvocationTargetException) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder)

Aggregations

AccumulatorStateSerializer (com.facebook.presto.spi.function.AccumulatorStateSerializer)2 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)1 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)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 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 AccumulatorStateFactory (com.facebook.presto.spi.function.AccumulatorStateFactory)1 AccumulatorStateMetadata (com.facebook.presto.spi.function.AccumulatorStateMetadata)1 AggregationFunction (com.facebook.presto.spi.function.AggregationFunction)1 AggregationState (com.facebook.presto.spi.function.AggregationState)1