Search in sources :

Example 1 with AccumulatorStateDescriptor

use of io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project hetu-core by openlookeng.

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 aggregationMetadata = 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(aggregationMetadata, classLoader));
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) ParameterMetadata(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata) Type(io.prestosql.spi.type.Type) ParameterType(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType) Signature(io.prestosql.spi.function.Signature) TypeSignature(io.prestosql.spi.type.TypeSignature) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with AccumulatorStateDescriptor

use of io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project hetu-core by openlookeng.

the class QuantileDigestAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type valueType, QuantileDigestType outputType, int arity) {
    DynamicClassLoader classLoader = new DynamicClassLoader(QuantileDigestAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = getInputTypes(valueType, arity);
    QuantileDigestStateSerializer stateSerializer = new QuantileDigestStateSerializer(valueType);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(inputTypes), getMethodHandle(valueType, arity), COMBINE_FUNCTION, OUTPUT_FUNCTION.bindTo(stateSerializer), ImmutableList.of(new AccumulatorStateDescriptor(QuantileDigestState.class, stateSerializer, new QuantileDigestStateFactory())), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) QuantileDigestType(io.prestosql.spi.type.QuantileDigestType) Type(io.prestosql.spi.type.Type) QuantileDigestStateSerializer(io.prestosql.operator.aggregation.state.QuantileDigestStateSerializer) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) QuantileDigestStateFactory(io.prestosql.operator.aggregation.state.QuantileDigestStateFactory)

Example 3 with AccumulatorStateDescriptor

use of io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project hetu-core by openlookeng.

the class DecimalAverageAggregation method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type) {
    checkArgument(type instanceof DecimalType, "type must be Decimal");
    DynamicClassLoader classLoader = new DynamicClassLoader(DecimalAverageAggregation.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    MethodHandle inputFunction;
    MethodHandle outputFunction;
    Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowAndLongState.class;
    AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowAndLongStateSerializer();
    if (((DecimalType) type).isShort()) {
        inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
        outputFunction = SHORT_DECIMAL_OUTPUT_FUNCTION;
    } else {
        inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
        outputFunction = LONG_DECIMAL_OUTPUT_FUNCTION;
    }
    inputFunction = inputFunction.bindTo(type);
    outputFunction = outputFunction.bindTo(type);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowAndLongStateFactory())), type);
    Type intermediateType = stateSerializer.getSerializedType();
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), type, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowAndLongStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) DecimalType(io.prestosql.spi.type.DecimalType) LongDecimalWithOverflowAndLongStateSerializer(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Example 4 with AccumulatorStateDescriptor

use of io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project hetu-core by openlookeng.

the class DecimalSumAggregation method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type inputType, Type outputType) {
    checkArgument(inputType instanceof DecimalType, "type must be Decimal");
    DynamicClassLoader classLoader = new DynamicClassLoader(DecimalSumAggregation.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(inputType);
    MethodHandle inputFunction;
    Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowState.class;
    AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowStateSerializer();
    if (((DecimalType) inputType).isShort()) {
        inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
    } else {
        inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
    }
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(inputType), inputFunction.bindTo(inputType), COMBINE_FUNCTION, LONG_DECIMAL_OUTPUT_FUNCTION.bindTo(outputType), ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowStateFactory())), outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowState) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) LongDecimalWithOverflowStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateFactory) DecimalType(io.prestosql.spi.type.DecimalType) LongDecimalWithOverflowStateSerializer(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Example 5 with AccumulatorStateDescriptor

use of io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor in project hetu-core by openlookeng.

the class ArrayAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type, ArrayAggGroupImplementation groupMode) {
    DynamicClassLoader classLoader = new DynamicClassLoader(ArrayAggregationFunction.class.getClassLoader());
    AccumulatorStateSerializer<?> stateSerializer = new ArrayAggregationStateSerializer(type);
    AccumulatorStateFactory<?> stateFactory = new ArrayAggregationStateFactory(type, groupMode);
    List<Type> inputTypes = ImmutableList.of(type);
    Type outputType = new ArrayType(type);
    Type intermediateType = stateSerializer.getSerializedType();
    List<ParameterMetadata> inputParameterMetadata = createInputParameterMetadata(type);
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(type);
    MethodHandle combineFunction = COMBINE_FUNCTION.bindTo(type);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(type);
    Class<? extends AccumulatorState> stateInterface = ArrayAggregationState.class;
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), inputParameterMetadata, inputFunction, combineFunction, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, stateFactory)), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) GenericAccumulatorFactoryBinder(io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder) InternalAggregationFunction(io.prestosql.operator.aggregation.InternalAggregationFunction) ParameterMetadata(io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata) ArrayType(io.prestosql.spi.type.ArrayType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) AggregationMetadata(io.prestosql.operator.aggregation.AggregationMetadata) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

AccumulatorStateDescriptor (io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)20 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)19 Type (io.prestosql.spi.type.Type)17 MethodHandle (java.lang.invoke.MethodHandle)9 AggregationMetadata (io.prestosql.operator.aggregation.AggregationMetadata)5 ParameterMetadata (io.prestosql.operator.aggregation.AggregationMetadata.ParameterMetadata)5 GenericAccumulatorFactoryBinder (io.prestosql.operator.aggregation.GenericAccumulatorFactoryBinder)5 InternalAggregationFunction (io.prestosql.operator.aggregation.InternalAggregationFunction)5 ArrayType (io.prestosql.spi.type.ArrayType)5 NullableDoubleState (io.prestosql.operator.aggregation.state.NullableDoubleState)3 ClassDefinition (io.airlift.bytecode.ClassDefinition)2 BlockPositionStateSerializer (io.prestosql.operator.aggregation.state.BlockPositionStateSerializer)2 KeyValuePairStateSerializer (io.prestosql.operator.aggregation.state.KeyValuePairStateSerializer)2 KeyValuePairsStateFactory (io.prestosql.operator.aggregation.state.KeyValuePairsStateFactory)2 LongState (io.prestosql.operator.aggregation.state.LongState)2 NullableLongState (io.prestosql.operator.aggregation.state.NullableLongState)2 QuantileDigestStateFactory (io.prestosql.operator.aggregation.state.QuantileDigestStateFactory)2 QuantileDigestStateSerializer (io.prestosql.operator.aggregation.state.QuantileDigestStateSerializer)2 OperatorType (io.prestosql.spi.function.OperatorType)2 DecimalType (io.prestosql.spi.type.DecimalType)2