Search in sources :

Example 1 with CombineFunction

use of io.trino.spi.function.CombineFunction in project trino by trinodb.

the class AggregationFromAnnotationsParser method parseFunctionDefinitions.

public static List<ParametricAggregation> parseFunctionDefinitions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    ImmutableList.Builder<ParametricAggregation> functions = ImmutableList.builder();
    // There must be a single state class and combine function
    Class<? extends AccumulatorState> stateClass = getStateClass(aggregationDefinition);
    Optional<Method> combineFunction = getCombineFunction(aggregationDefinition, stateClass);
    // Each output function defines a new aggregation function
    for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
        AggregationHeader header = parseHeader(aggregationDefinition, outputFunction);
        if (header.isDecomposable()) {
            checkArgument(combineFunction.isPresent(), "Decomposable method %s does not have a combine method", header.getName());
        } else if (combineFunction.isPresent()) {
            log.warn("Aggregation function %s is not decomposable, but has combine method", header.getName());
        }
        // Input functions can have either an exact signature, or generic/calculate signature
        List<AggregationImplementation> exactImplementations = new ArrayList<>();
        List<AggregationImplementation> nonExactImplementations = new ArrayList<>();
        for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
            Optional<Method> removeInputFunction = getRemoveInputFunction(aggregationDefinition, inputFunction);
            AggregationImplementation implementation = parseImplementation(aggregationDefinition, header.getName(), inputFunction, removeInputFunction, outputFunction, combineFunction.filter(function -> header.isDecomposable()));
            if (isGenericOrCalculated(implementation.getSignature())) {
                exactImplementations.add(implementation);
            } else {
                nonExactImplementations.add(implementation);
            }
        }
        // register a set functions for the canonical name, and each alias
        functions.addAll(buildFunctions(header.getName(), header, stateClass, exactImplementations, nonExactImplementations));
        for (String alias : getAliases(aggregationDefinition.getAnnotation(AggregationFunction.class), outputFunction)) {
            functions.addAll(buildFunctions(alias, header, stateClass, exactImplementations, nonExactImplementations));
        }
    }
    return functions.build();
}
Also used : Arrays(java.util.Arrays) Logger(io.airlift.log.Logger) AggregationFunction(io.trino.spi.function.AggregationFunction) OutputFunction(io.trino.spi.function.OutputFunction) ArrayList(java.util.ArrayList) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) RemoveInputFunction(io.trino.spi.function.RemoveInputFunction) CombineFunction(io.trino.spi.function.CombineFunction) Objects.requireNonNull(java.util.Objects.requireNonNull) Parser.parseImplementation(io.trino.operator.aggregation.AggregationImplementation.Parser.parseImplementation) Signature(io.trino.metadata.Signature) Method(java.lang.reflect.Method) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) ParametricFunctionHelpers.signatureWithName(io.trino.operator.ParametricFunctionHelpers.signatureWithName) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) MoreCollectors(com.google.common.collect.MoreCollectors) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) AccumulatorState(io.trino.spi.function.AccumulatorState) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) FunctionsParserHelper(io.trino.operator.annotations.FunctionsParserHelper) Optional(java.util.Optional) InputFunction(io.trino.spi.function.InputFunction) FunctionsParserHelper.parseDescription(io.trino.operator.annotations.FunctionsParserHelper.parseDescription) AnnotatedElement(java.lang.reflect.AnnotatedElement) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) AggregationFunction(io.trino.spi.function.AggregationFunction)

Example 2 with CombineFunction

use of io.trino.spi.function.CombineFunction in project trino by trinodb.

the class ApproximateSetGenericAggregation method combineState.

@CombineFunction
public static void combineState(@AggregationState HyperLogLogState state, @AggregationState HyperLogLogState otherState) {
    HyperLogLog input = otherState.getHyperLogLog();
    HyperLogLog previous = state.getHyperLogLog();
    if (previous == null) {
        state.setHyperLogLog(input);
        state.addMemoryUsage(input.estimatedInMemorySize());
    } else {
        state.addMemoryUsage(-previous.estimatedInMemorySize());
        previous.mergeWith(input);
        state.addMemoryUsage(previous.estimatedInMemorySize());
    }
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) CombineFunction(io.trino.spi.function.CombineFunction)

Example 3 with CombineFunction

use of io.trino.spi.function.CombineFunction in project trino by trinodb.

the class ApproximateDoublePercentileArrayAggregations method combine.

@CombineFunction
public static void combine(@AggregationState TDigestAndPercentileArrayState state, TDigestAndPercentileArrayState otherState) {
    TDigest otherDigest = otherState.getDigest();
    TDigest digest = state.getDigest();
    if (digest == null) {
        state.setDigest(otherDigest);
        state.addMemoryUsage(otherDigest.estimatedInMemorySizeInBytes());
    } else {
        state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes());
        digest.mergeWith(otherDigest);
        state.addMemoryUsage(digest.estimatedInMemorySizeInBytes());
    }
    state.setPercentiles(otherState.getPercentiles());
}
Also used : TDigest(io.airlift.stats.TDigest) CombineFunction(io.trino.spi.function.CombineFunction)

Example 4 with CombineFunction

use of io.trino.spi.function.CombineFunction in project trino by trinodb.

the class ApproximateSetAggregation method combineState.

@CombineFunction
public static void combineState(@AggregationState HyperLogLogState state, @AggregationState HyperLogLogState otherState) {
    HyperLogLog input = otherState.getHyperLogLog();
    HyperLogLog previous = state.getHyperLogLog();
    if (previous == null) {
        state.setHyperLogLog(input);
        state.addMemoryUsage(input.estimatedInMemorySize());
    } else {
        state.addMemoryUsage(-previous.estimatedInMemorySize());
        previous.mergeWith(input);
        state.addMemoryUsage(previous.estimatedInMemorySize());
    }
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) CombineFunction(io.trino.spi.function.CombineFunction)

Example 5 with CombineFunction

use of io.trino.spi.function.CombineFunction in project trino by trinodb.

the class ApproximateCountDistinctAggregation method combineState.

@CombineFunction
public static void combineState(@AggregationState HyperLogLogState state, @AggregationState HyperLogLogState otherState) {
    HyperLogLog input = otherState.getHyperLogLog();
    HyperLogLog previous = state.getHyperLogLog();
    if (previous == null) {
        state.setHyperLogLog(input);
        state.addMemoryUsage(input.estimatedInMemorySize());
    } else {
        state.addMemoryUsage(-previous.estimatedInMemorySize());
        previous.mergeWith(input);
        state.addMemoryUsage(previous.estimatedInMemorySize());
    }
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) CombineFunction(io.trino.spi.function.CombineFunction)

Aggregations

CombineFunction (io.trino.spi.function.CombineFunction)8 TDigest (io.airlift.stats.TDigest)3 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Strings.emptyToNull (com.google.common.base.Strings.emptyToNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 MoreCollectors (com.google.common.collect.MoreCollectors)1 Logger (io.airlift.log.Logger)1 QuantileDigest (io.airlift.stats.QuantileDigest)1 Signature (io.trino.metadata.Signature)1 ParametricFunctionHelpers.signatureWithName (io.trino.operator.ParametricFunctionHelpers.signatureWithName)1 ParametricImplementationsGroup (io.trino.operator.ParametricImplementationsGroup)1 Parser.parseImplementation (io.trino.operator.aggregation.AggregationImplementation.Parser.parseImplementation)1 FunctionsParserHelper (io.trino.operator.annotations.FunctionsParserHelper)1 FunctionsParserHelper.parseDescription (io.trino.operator.annotations.FunctionsParserHelper.parseDescription)1 AccumulatorState (io.trino.spi.function.AccumulatorState)1 AggregationFunction (io.trino.spi.function.AggregationFunction)1