Search in sources :

Example 16 with ParameterMetadata

use of com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata in project presto by prestodb.

the class ParametricAggregation method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables variables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    // Bind variables
    Signature boundSignature = applyBoundVariables(getSignature(), variables, arity);
    // Find implementation matching arguments
    AggregationImplementation concreteImplementation = findMatchingImplementation(boundSignature, variables, functionAndTypeManager);
    // Build argument and return Types from signatures
    List<Type> inputTypes = boundSignature.getArgumentTypes().stream().map(functionAndTypeManager::getType).collect(toImmutableList());
    Type outputType = functionAndTypeManager.getType(boundSignature.getReturnType());
    // Create classloader for additional aggregation dependencies
    Class<?> definitionClass = concreteImplementation.getDefinitionClass();
    DynamicClassLoader classLoader = new DynamicClassLoader(definitionClass.getClassLoader(), getClass().getClassLoader());
    // Build state factory and serializer
    Class<?> stateClass = concreteImplementation.getStateClass();
    AccumulatorStateSerializer<?> stateSerializer = getAccumulatorStateSerializer(concreteImplementation, variables, functionAndTypeManager, stateClass, classLoader);
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateClass, classLoader);
    // Bind provided dependencies to aggregation method handlers
    MethodHandle inputHandle = bindDependencies(concreteImplementation.getInputFunction(), concreteImplementation.getInputDependencies(), variables, functionAndTypeManager);
    MethodHandle combineHandle = bindDependencies(concreteImplementation.getCombineFunction(), concreteImplementation.getCombineDependencies(), variables, functionAndTypeManager);
    MethodHandle outputHandle = bindDependencies(concreteImplementation.getOutputFunction(), concreteImplementation.getOutputDependencies(), variables, functionAndTypeManager);
    // Build metadata of input parameters
    List<ParameterMetadata> parametersMetadata = buildParameterMetadata(concreteImplementation.getInputParameterMetadataTypes(), inputTypes);
    // Generate Aggregation name
    String aggregationName = generateAggregationName(getSignature().getNameSuffix(), outputType.getTypeSignature(), signaturesFromTypes(inputTypes));
    // Collect all collected data in Metadata
    AggregationMetadata metadata = new AggregationMetadata(aggregationName, parametersMetadata, inputHandle, combineHandle, outputHandle, ImmutableList.of(new AccumulatorStateDescriptor(stateClass, stateSerializer, stateFactory)), outputType);
    // Create specialized InternalAggregregationFunction for Presto
    return new InternalAggregationFunction(getSignature().getNameSuffix(), inputTypes, ImmutableList.of(stateSerializer.getSerializedType()), outputType, details.isDecomposable(), details.isOrderSensitive(), new LazyAccumulatorFactoryBinder(metadata, classLoader));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) ParameterMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata) ParameterType(com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType) Type(com.facebook.presto.common.type.Type) TypeSignature(com.facebook.presto.common.type.TypeSignature) Signature(com.facebook.presto.spi.function.Signature) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)16 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)8 AccumulatorStateDescriptor (com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)7 Type (com.facebook.presto.common.type.Type)6 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)5 ArrayType (com.facebook.presto.common.type.ArrayType)4 MethodHandle (java.lang.invoke.MethodHandle)4 GroupByIdBlock (com.facebook.presto.operator.GroupByIdBlock)3 AggregationMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata)3 GenericAccumulatorFactoryBinder (com.facebook.presto.operator.aggregation.GenericAccumulatorFactoryBinder)3 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)3 ArrayList (java.util.ArrayList)3 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)2 Variable (com.facebook.presto.bytecode.Variable)2 ForLoop (com.facebook.presto.bytecode.control.ForLoop)2 IfStatement (com.facebook.presto.bytecode.control.IfStatement)2 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)2 Block (com.facebook.presto.common.block.Block)2 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)2 ParameterType (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType)2