use of io.trino.spi.function.InputFunction in project trino by trinodb.
the class MergeHyperLogLogAggregation method input.
@InputFunction
public static void input(@AggregationState HyperLogLogState state, @SqlType(StandardTypes.HYPER_LOG_LOG) Slice value) {
HyperLogLog input = HyperLogLog.newInstance(value);
merge(state, input);
}
use of io.trino.spi.function.InputFunction in project trino by trinodb.
the class TDigestAggregationFunction method input.
@InputFunction
public static void input(@AggregationState TDigestState state, @SqlType(StandardTypes.DOUBLE) double value) {
TDigest tdigest = state.getTDigest();
if (tdigest == null) {
tdigest = new TDigest();
state.setTDigest(tdigest);
state.addMemoryUsage(tdigest.estimatedInMemorySizeInBytes());
}
state.addMemoryUsage(-tdigest.estimatedInMemorySizeInBytes());
tdigest.add(value);
state.addMemoryUsage(tdigest.estimatedInMemorySizeInBytes());
}
use of io.trino.spi.function.InputFunction in project trino by trinodb.
the class TDigestAggregationFunction method weightedInput.
@InputFunction
public static void weightedInput(@AggregationState TDigestState state, @SqlType(StandardTypes.DOUBLE) double value, @SqlType(StandardTypes.DOUBLE) double weight) {
TDigest tdigest = state.getTDigest();
if (tdigest == null) {
tdigest = new TDigest();
state.setTDigest(tdigest);
state.addMemoryUsage(tdigest.estimatedInMemorySizeInBytes());
}
state.addMemoryUsage(-tdigest.estimatedInMemorySizeInBytes());
tdigest.add(value, verifyWeight(weight));
state.addMemoryUsage(tdigest.estimatedInMemorySizeInBytes());
}
use of io.trino.spi.function.InputFunction in project trino by trinodb.
the class SpatialPartitioningInternalAggregateFunction method input.
@InputFunction
public static void input(SpatialPartitioningState state, @SqlType(GEOMETRY_TYPE_NAME) Slice slice, @SqlType(INTEGER) long partitionCount) {
Envelope envelope = deserializeEnvelope(slice);
if (envelope.isEmpty()) {
return;
}
Rectangle extent = new Rectangle(envelope.getXMin(), envelope.getYMin(), envelope.getXMax(), envelope.getYMax());
if (state.getCount() == 0) {
state.setPartitionCount(toIntExact(partitionCount));
state.setExtent(extent);
state.setSamples(new ArrayList<>());
} else {
state.setExtent(state.getExtent().merge(extent));
}
// use reservoir sampling
List<Rectangle> samples = state.getSamples();
if (samples.size() <= MAX_SAMPLE_COUNT) {
samples.add(extent);
} else {
long sampleIndex = ThreadLocalRandom.current().nextLong(state.getCount());
if (sampleIndex < MAX_SAMPLE_COUNT) {
samples.set(toIntExact(sampleIndex), extent);
}
}
state.setCount(state.getCount() + 1);
}
use of io.trino.spi.function.InputFunction 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();
}
Aggregations