Search in sources :

Example 1 with AggregationParameterKind

use of io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind in project trino by trinodb.

the class ParametricAggregation method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    // Find implementation matching arguments
    AggregationImplementation concreteImplementation = findMatchingImplementation(boundSignature);
    // Build state factory and serializer
    AccumulatorStateDescriptor<?> accumulatorStateDescriptor = generateAccumulatorStateDescriptor(stateClass);
    // Bind provided dependencies to aggregation method handlers
    FunctionMetadata metadata = getFunctionMetadata();
    FunctionBinding functionBinding = SignatureBinder.bindFunction(metadata.getFunctionId(), metadata.getSignature(), boundSignature);
    MethodHandle inputHandle = bindDependencies(concreteImplementation.getInputFunction(), concreteImplementation.getInputDependencies(), functionBinding, functionDependencies);
    Optional<MethodHandle> removeInputHandle = concreteImplementation.getRemoveInputFunction().map(removeInputFunction -> bindDependencies(removeInputFunction, concreteImplementation.getRemoveInputDependencies(), functionBinding, functionDependencies));
    Optional<MethodHandle> combineHandle = concreteImplementation.getCombineFunction();
    if (getAggregationMetadata().isDecomposable()) {
        checkArgument(combineHandle.isPresent(), "Decomposable method %s does not have a combine method", boundSignature.getName());
        combineHandle = combineHandle.map(combineFunction -> bindDependencies(combineFunction, concreteImplementation.getCombineDependencies(), functionBinding, functionDependencies));
    } else {
        checkArgument(concreteImplementation.getCombineFunction().isEmpty(), "Decomposable method %s does not have a combine method", boundSignature.getName());
    }
    MethodHandle outputHandle = bindDependencies(concreteImplementation.getOutputFunction(), concreteImplementation.getOutputDependencies(), functionBinding, functionDependencies);
    List<AggregationParameterKind> inputParameterKinds = concreteImplementation.getInputParameterKinds();
    inputHandle = normalizeInputMethod(inputHandle, boundSignature, inputParameterKinds);
    removeInputHandle = removeInputHandle.map(function -> normalizeInputMethod(function, boundSignature, inputParameterKinds));
    return new AggregationMetadata(inputHandle, removeInputHandle, combineHandle, outputHandle, ImmutableList.of(accumulatorStateDescriptor));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) FunctionDependencies(io.trino.metadata.FunctionDependencies) FunctionDependencyDeclarationBuilder(io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature(io.trino.metadata.Signature) FunctionMetadata(io.trino.metadata.FunctionMetadata) StateCompiler.getSerializedType(io.trino.operator.aggregation.state.StateCompiler.getSerializedType) AggregationFunctionAdapter.normalizeInputMethod(io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) ParametricFunctionHelpers.bindDependencies(io.trino.operator.ParametricFunctionHelpers.bindDependencies) Collection(java.util.Collection) TrinoException(io.trino.spi.TrinoException) StateCompiler.generateStateFactory(io.trino.operator.aggregation.state.StateCompiler.generateStateFactory) FunctionBinding(io.trino.metadata.FunctionBinding) StateCompiler.generateStateSerializer(io.trino.operator.aggregation.state.StateCompiler.generateStateSerializer) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) AccumulatorState(io.trino.spi.function.AccumulatorState) String.format(java.lang.String.format) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) AMBIGUOUS_FUNCTION_CALL(io.trino.spi.StandardErrorCode.AMBIGUOUS_FUNCTION_CALL) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) List(java.util.List) SignatureBinder(io.trino.metadata.SignatureBinder) BoundSignature(io.trino.metadata.BoundSignature) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FUNCTION_IMPLEMENTATION_MISSING(io.trino.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_MISSING) SqlAggregationFunction(io.trino.metadata.SqlAggregationFunction) FunctionMetadata(io.trino.metadata.FunctionMetadata) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) FunctionBinding(io.trino.metadata.FunctionBinding) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with AggregationParameterKind

use of io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind in project trino by trinodb.

the class QuantileDigestAggregationFunction method specialize.

@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
    QuantileDigestType outputType = (QuantileDigestType) boundSignature.getReturnType();
    Type valueType = outputType.getValueType();
    int arity = boundSignature.getArity();
    QuantileDigestStateSerializer stateSerializer = new QuantileDigestStateSerializer(valueType);
    MethodHandle inputFunction = getMethodHandle(valueType, arity);
    inputFunction = normalizeInputMethod(inputFunction, boundSignature, ImmutableList.<AggregationParameterKind>builder().add(STATE).addAll(getInputTypes(valueType, arity).stream().map(ignored -> INPUT_CHANNEL).collect(Collectors.toList())).build());
    return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(COMBINE_FUNCTION), OUTPUT_FUNCTION.bindTo(stateSerializer), ImmutableList.of(new AccumulatorStateDescriptor<>(QuantileDigestState.class, stateSerializer, new QuantileDigestStateFactory())));
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) QuantileDigest(io.airlift.stats.QuantileDigest) Signature.comparableTypeParameter(io.trino.metadata.Signature.comparableTypeParameter) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) MethodHandles.insertArguments(java.lang.invoke.MethodHandles.insertArguments) FunctionNullability(io.trino.metadata.FunctionNullability) Type(io.trino.spi.type.Type) DEFAULT_WEIGHT(io.trino.operator.scalar.QuantileDigestFunctions.DEFAULT_WEIGHT) QuantileDigestStateFactory(io.trino.operator.aggregation.state.QuantileDigestStateFactory) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) QDIGEST(io.trino.spi.type.StandardTypes.QDIGEST) ImmutableList(com.google.common.collect.ImmutableList) TypeSignature.parametricType(io.trino.spi.type.TypeSignature.parametricType) FloatingPointBitsConverterUtil.floatToSortableInt(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.floatToSortableInt) QuantileDigestStateSerializer(io.trino.operator.aggregation.state.QuantileDigestStateSerializer) Signature(io.trino.metadata.Signature) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) AggregationFunctionAdapter.normalizeInputMethod(io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod) QuantileDigestFunctions.verifyWeight(io.trino.operator.scalar.QuantileDigestFunctions.verifyWeight) STATE(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind.STATE) INPUT_CHANNEL(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind.INPUT_CHANNEL) Collections.nCopies(java.util.Collections.nCopies) QuantileDigestType(io.trino.spi.type.QuantileDigestType) StandardTypes(io.trino.spi.type.StandardTypes) QuantileDigestState(io.trino.operator.aggregation.state.QuantileDigestState) FloatingPointBitsConverterUtil.doubleToSortableLong(io.trino.operator.aggregation.FloatingPointBitsConverterUtil.doubleToSortableLong) AGGREGATE(io.trino.metadata.FunctionKind.AGGREGATE) Collectors(java.util.stream.Collectors) DEFAULT_ACCURACY(io.trino.operator.scalar.QuantileDigestFunctions.DEFAULT_ACCURACY) String.format(java.lang.String.format) AggregationParameterKind(io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) List(java.util.List) QuantileDigestFunctions.verifyAccuracy(io.trino.operator.scalar.QuantileDigestFunctions.verifyAccuracy) BIGINT(io.trino.spi.type.BigintType.BIGINT) BoundSignature(io.trino.metadata.BoundSignature) Optional(java.util.Optional) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlAggregationFunction(io.trino.metadata.SqlAggregationFunction) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) Type(io.trino.spi.type.Type) TypeSignature.parametricType(io.trino.spi.type.TypeSignature.parametricType) QuantileDigestType(io.trino.spi.type.QuantileDigestType) QuantileDigestStateSerializer(io.trino.operator.aggregation.state.QuantileDigestStateSerializer) AccumulatorStateDescriptor(io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) QuantileDigestStateFactory(io.trino.operator.aggregation.state.QuantileDigestStateFactory) QuantileDigestType(io.trino.spi.type.QuantileDigestType) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)2 BoundSignature (io.trino.metadata.BoundSignature)2 AGGREGATE (io.trino.metadata.FunctionKind.AGGREGATE)2 FunctionMetadata (io.trino.metadata.FunctionMetadata)2 Signature (io.trino.metadata.Signature)2 SqlAggregationFunction (io.trino.metadata.SqlAggregationFunction)2 AggregationParameterKind (io.trino.operator.aggregation.AggregationFunctionAdapter.AggregationParameterKind)2 AggregationFunctionAdapter.normalizeInputMethod (io.trino.operator.aggregation.AggregationFunctionAdapter.normalizeInputMethod)2 AccumulatorStateDescriptor (io.trino.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)2 String.format (java.lang.String.format)2 MethodHandle (java.lang.invoke.MethodHandle)2 List (java.util.List)2 Optional (java.util.Optional)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 QuantileDigest (io.airlift.stats.QuantileDigest)1 FunctionBinding (io.trino.metadata.FunctionBinding)1 FunctionDependencies (io.trino.metadata.FunctionDependencies)1 FunctionDependencyDeclaration (io.trino.metadata.FunctionDependencyDeclaration)1