use of com.facebook.presto.operator.aggregation.Accumulator in project presto by prestodb.
the class TestSpatialPartitioningInternalAggregation method test.
@Test(dataProvider = "partitionCount")
public void test(int partitionCount) {
InternalAggregationFunction function = getFunction();
List<OGCGeometry> geometries = makeGeometries();
Block geometryBlock = makeGeometryBlock(geometries);
Block partitionCountBlock = BlockAssertions.createRLEBlock(partitionCount, geometries.size());
String expectedValue = getSpatialPartitioning(geometries, partitionCount);
AccumulatorFactory accumulatorFactory = function.bind(Ints.asList(0, 1), Optional.empty());
Page page = new Page(geometryBlock, partitionCountBlock);
Accumulator accumulator = accumulatorFactory.createAccumulator(UpdateMemory.NOOP);
accumulator.addInput(page);
String aggregation = (String) BlockAssertions.getOnlyValue(accumulator.getFinalType(), getFinalBlock(accumulator));
assertEquals(aggregation, expectedValue);
GroupedAccumulator groupedAggregation = accumulatorFactory.createGroupedAccumulator(UpdateMemory.NOOP);
groupedAggregation.addInput(createGroupByIdBlock(0, page.getPositionCount()), page);
String groupValue = (String) getGroupValue(groupedAggregation, 0);
assertEquals(groupValue, expectedValue);
}
use of com.facebook.presto.operator.aggregation.Accumulator in project presto by prestodb.
the class TestSpatialPartitioningInternalAggregation method testEmptyPartitionException.
@Test
public void testEmptyPartitionException() {
InternalAggregationFunction function = getFunction();
Block geometryBlock = GEOMETRY.createBlockBuilder(null, 0).build();
Block partitionCountBlock = BlockAssertions.createRLEBlock(10, 0);
Page page = new Page(geometryBlock, partitionCountBlock);
AccumulatorFactory accumulatorFactory = function.bind(Ints.asList(0, 1), Optional.empty());
Accumulator accumulator = accumulatorFactory.createAccumulator(UpdateMemory.NOOP);
accumulator.addInput(page);
try {
getFinalBlock(accumulator);
fail("Should fail creating spatial partition with no rows.");
} catch (PrestoException e) {
assertEquals(e.getErrorCode(), INVALID_FUNCTION_ARGUMENT.toErrorCode());
assertEquals(e.getMessage(), "No rows supplied to spatial partition.");
}
}
use of com.facebook.presto.operator.aggregation.Accumulator in project presto by prestodb.
the class TestEvaluateClassifierPredictions method testEvaluateClassifierPredictions.
@Test
public void testEvaluateClassifierPredictions() {
metadata.registerBuiltInFunctions(extractFunctions(new MLPlugin().getFunctions()));
InternalAggregationFunction aggregation = functionAndTypeManager.getAggregateFunctionImplementation(functionAndTypeManager.lookupFunction("evaluate_classifier_predictions", fromTypes(BIGINT, BIGINT)));
Accumulator accumulator = aggregation.bind(ImmutableList.of(0, 1), Optional.empty()).createAccumulator(UpdateMemory.NOOP);
accumulator.addInput(getPage());
BlockBuilder finalOut = accumulator.getFinalType().createBlockBuilder(null, 1);
accumulator.evaluateFinal(finalOut);
Block block = finalOut.build();
String output = VARCHAR.getSlice(block, 0).toStringUtf8();
List<String> parts = ImmutableList.copyOf(Splitter.on('\n').omitEmptyStrings().split(output));
assertEquals(parts.size(), 7, output);
assertEquals(parts.get(0), "Accuracy: 1/2 (50.00%)");
}
use of com.facebook.presto.operator.aggregation.Accumulator in project presto by prestodb.
the class HashAggregationOperator method getGlobalAggregationOutput.
private Page getGlobalAggregationOutput() {
List<Accumulator> accumulators = accumulatorFactories.stream().map(accumulatorFactory -> accumulatorFactory.createAccumulator(UpdateMemory.NOOP)).collect(Collectors.toList());
// global aggregation output page will only be constructed once,
// so a new PageBuilder is constructed (instead of using PageBuilder.reset)
PageBuilder output = new PageBuilder(globalAggregationGroupIds.size(), types);
for (int groupId : globalAggregationGroupIds) {
output.declarePosition();
int channel = 0;
for (; channel < groupByTypes.size(); channel++) {
if (channel == groupIdChannel.get()) {
output.getBlockBuilder(channel).writeLong(groupId);
} else {
output.getBlockBuilder(channel).appendNull();
}
}
if (hashChannel.isPresent()) {
long hashValue = calculateDefaultOutputHash(groupByTypes, groupIdChannel.get(), groupId);
output.getBlockBuilder(channel++).writeLong(hashValue);
}
for (int j = 0; j < accumulators.size(); channel++, j++) {
if (step.isOutputPartial()) {
accumulators.get(j).evaluateIntermediate(output.getBlockBuilder(channel));
} else {
accumulators.get(j).evaluateFinal(output.getBlockBuilder(channel));
}
}
}
if (output.isEmpty()) {
return null;
}
return output.build();
}
Aggregations