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));
}
Aggregations