Search in sources :

Example 21 with AccumulatorStateDescriptor

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);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Type(com.facebook.presto.common.type.Type) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(com.facebook.presto.operator.aggregation.GenericAccumulatorFactoryBinder) AggregationMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 22 with AccumulatorStateDescriptor

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

Example 23 with AccumulatorStateDescriptor

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));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ReduceAggregationState(com.facebook.presto.operator.aggregation.state.ReduceAggregationState) ReduceAggregationStateSerializer(com.facebook.presto.operator.aggregation.state.ReduceAggregationStateSerializer) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) BinaryFunctionInterface(com.facebook.presto.sql.gen.lambda.BinaryFunctionInterface) PrestoException(com.facebook.presto.spi.PrestoException) MethodHandle(java.lang.invoke.MethodHandle) ReduceAggregationStateFactory(com.facebook.presto.operator.aggregation.state.ReduceAggregationStateFactory)

Aggregations

AccumulatorStateDescriptor (com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)23 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)22 Type (com.facebook.presto.common.type.Type)20 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayType (com.facebook.presto.common.type.ArrayType)7 AggregationMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata)7 ParameterMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata)7 GenericAccumulatorFactoryBinder (com.facebook.presto.operator.aggregation.GenericAccumulatorFactoryBinder)7 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)7 MapType (com.facebook.presto.common.type.MapType)3 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)2 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)2 OperatorType (com.facebook.presto.common.function.OperatorType)2 DecimalType (com.facebook.presto.common.type.DecimalType)2 BlockPositionStateSerializer (com.facebook.presto.operator.aggregation.state.BlockPositionStateSerializer)2 KeyValuePairStateSerializer (com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer)2 KeyValuePairsStateFactory (com.facebook.presto.operator.aggregation.state.KeyValuePairsStateFactory)2 LongState (com.facebook.presto.operator.aggregation.state.LongState)2 NullableDoubleState (com.facebook.presto.operator.aggregation.state.NullableDoubleState)2 SetAggregationState (com.facebook.presto.operator.aggregation.state.SetAggregationState)2