use of com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project presto by prestodb.
the class Histogram method generateAggregation.
private static InternalAggregationFunction generateAggregation(String functionName, Type keyType, Type outputType, HistogramGroupImplementation groupMode) {
DynamicClassLoader classLoader = new DynamicClassLoader(Histogram.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(keyType);
HistogramStateSerializer stateSerializer = new HistogramStateSerializer(keyType, outputType);
Type intermediateType = stateSerializer.getSerializedType();
MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(outputType);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(functionName, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(HistogramState.class, stateSerializer, new HistogramStateFactory(keyType, EXPECTED_SIZE_FOR_HASHING, groupMode))), outputType);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(functionName, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
use of com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor 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));
}
use of com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project presto by prestodb.
the class ReduceAggregationFunction method generateAggregation.
private InternalAggregationFunction generateAggregation(Type inputType, Type stateType) {
DynamicClassLoader classLoader = new DynamicClassLoader(ReduceAggregationFunction.class.getClassLoader());
MethodHandle inputMethodHandle;
MethodHandle combineMethodHandle;
MethodHandle outputMethodHandle;
AccumulatorStateDescriptor stateDescriptor;
if (!supportsComplexTypes && !(stateType.getJavaType() == long.class || stateType.getJavaType() == double.class || stateType.getJavaType() == boolean.class)) {
// See JDK-8017163.
throw new PrestoException(NOT_SUPPORTED, format("State type not enabled for %s: %s", NAME, stateType.getDisplayName()));
}
inputMethodHandle = INPUT_FUNCTION.bindTo(inputType);
combineMethodHandle = COMBINE_FUNCTION;
outputMethodHandle = OUTPUT_FUNCTION.bindTo(stateType);
stateDescriptor = new AccumulatorStateDescriptor(ReduceAggregationState.class, new ReduceAggregationStateSerializer(stateType), new ReduceAggregationStateFactory());
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getNameSuffix(), inputType.getTypeSignature(), ImmutableList.of(inputType.getTypeSignature())), createInputParameterMetadata(inputType, stateType), inputMethodHandle.asType(inputMethodHandle.type().changeParameterType(1, inputType.getJavaType()).changeParameterType(2, stateType.getJavaType())), combineMethodHandle, outputMethodHandle, ImmutableList.of(stateDescriptor), stateType, ImmutableList.of(BinaryFunctionInterface.class, BinaryFunctionInterface.class));
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(getSignature().getNameSuffix(), ImmutableList.of(inputType), ImmutableList.of(stateType), stateType, true, false, factory, ImmutableList.of(BinaryFunctionInterface.class, BinaryFunctionInterface.class));
}
Aggregations