Search in sources :

Example 1 with AccumulatorFactory

use of com.facebook.presto.operator.aggregation.AccumulatorFactory 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);
}
Also used : OGCGeometry(com.esri.core.geometry.ogc.OGCGeometry) GroupedAccumulator(com.facebook.presto.operator.aggregation.GroupedAccumulator) Accumulator(com.facebook.presto.operator.aggregation.Accumulator) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory) AggregationTestUtils.createGroupByIdBlock(com.facebook.presto.operator.aggregation.AggregationTestUtils.createGroupByIdBlock) AggregationTestUtils.getFinalBlock(com.facebook.presto.operator.aggregation.AggregationTestUtils.getFinalBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) GroupedAccumulator(com.facebook.presto.operator.aggregation.GroupedAccumulator) Test(org.testng.annotations.Test)

Example 2 with AccumulatorFactory

use of com.facebook.presto.operator.aggregation.AccumulatorFactory 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.");
    }
}
Also used : GroupedAccumulator(com.facebook.presto.operator.aggregation.GroupedAccumulator) Accumulator(com.facebook.presto.operator.aggregation.Accumulator) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory) AggregationTestUtils.createGroupByIdBlock(com.facebook.presto.operator.aggregation.AggregationTestUtils.createGroupByIdBlock) AggregationTestUtils.getFinalBlock(com.facebook.presto.operator.aggregation.AggregationTestUtils.getFinalBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) PrestoException(com.facebook.presto.spi.PrestoException) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) Test(org.testng.annotations.Test)

Example 3 with AccumulatorFactory

use of com.facebook.presto.operator.aggregation.AccumulatorFactory 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();
}
Also used : Accumulator(com.facebook.presto.operator.aggregation.Accumulator) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) Page(com.facebook.presto.common.Page) HashAggregationBuilder(com.facebook.presto.operator.aggregation.builder.HashAggregationBuilder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) InMemoryHashAggregationBuilder(com.facebook.presto.operator.aggregation.builder.InMemoryHashAggregationBuilder) MEGABYTE(io.airlift.units.DataSize.Unit.MEGABYTE) PageBuilder(com.facebook.presto.common.PageBuilder) CombineHashFunction(com.facebook.presto.operator.scalar.CombineHashFunction) ImmutableList(com.google.common.collect.ImmutableList) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory) NULL_HASH_CODE(com.facebook.presto.type.TypeUtils.NULL_HASH_CODE) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) Type(com.facebook.presto.common.type.Type) Step(com.facebook.presto.spi.plan.AggregationNode.Step) BigintType(com.facebook.presto.common.type.BigintType) InMemoryHashAggregationBuilder.toTypes(com.facebook.presto.operator.aggregation.builder.InMemoryHashAggregationBuilder.toTypes) SpillableHashAggregationBuilder(com.facebook.presto.operator.aggregation.builder.SpillableHashAggregationBuilder) Collectors(java.util.stream.Collectors) INITIAL_HASH_VALUE(com.facebook.presto.sql.planner.optimizations.HashGenerationOptimizer.INITIAL_HASH_VALUE) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Accumulator(com.facebook.presto.operator.aggregation.Accumulator) DataSize(io.airlift.units.DataSize) List(java.util.List) Optional(java.util.Optional) SpillerFactory(com.facebook.presto.spiller.SpillerFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting) JoinCompiler(com.facebook.presto.sql.gen.JoinCompiler) PageBuilder(com.facebook.presto.common.PageBuilder)

Example 4 with AccumulatorFactory

use of com.facebook.presto.operator.aggregation.AccumulatorFactory in project presto by prestodb.

the class InMemoryHashAggregationBuilder method toTypes.

public static List<Type> toTypes(List<? extends Type> groupByType, Step step, List<AccumulatorFactory> factories, Optional<Integer> hashChannel) {
    ImmutableList.Builder<Type> types = ImmutableList.builder();
    types.addAll(groupByType);
    if (hashChannel.isPresent()) {
        types.add(BIGINT);
    }
    for (AccumulatorFactory factory : factories) {
        // Create an aggregator just to figure out the output type
        // It is fine not to specify a memory reservation callback as it doesn't accept any input
        types.add(new Aggregator(factory, step, Optional.empty(), UpdateMemory.NOOP).getType());
    }
    return types.build();
}
Also used : Type(com.facebook.presto.common.type.Type) ImmutableList(com.google.common.collect.ImmutableList) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory)

Example 5 with AccumulatorFactory

use of com.facebook.presto.operator.aggregation.AccumulatorFactory in project presto by prestodb.

the class TestAggregationOperator method testDistinctMaskWithNull.

@Test
public void testDistinctMaskWithNull() {
    AccumulatorFactory distinctFactory = COUNT.bind(ImmutableList.of(0), Optional.of(1), ImmutableList.of(BIGINT, BOOLEAN), ImmutableList.of(), ImmutableList.of(), null, // distinct
    true, new JoinCompiler(MetadataManager.createTestMetadataManager(), new FeaturesConfig()), ImmutableList.of(), false, TEST_SESSION, new TempStorageStandaloneSpillerFactory(new TestingTempStorageManager(), new BlockEncodingManager(), new NodeSpillConfig(), new FeaturesConfig(), new SpillerStats()));
    OperatorFactory operatorFactory = new AggregationOperatorFactory(0, new PlanNodeId("test"), Step.SINGLE, ImmutableList.of(distinctFactory), false);
    DriverContext driverContext = createTaskContext(executor, scheduledExecutor, TEST_SESSION).addPipelineContext(0, true, true, false).addDriverContext();
    ByteArrayBlock trueMaskAllNull = new ByteArrayBlock(4, Optional.of(new boolean[] { true, true, true, true }), /* all positions are null */
    new byte[] { 1, 1, 1, 1 });
    /* non-zero value is true, all masks are true */
    Block trueNullRleMask = new RunLengthEncodedBlock(trueMaskAllNull.getSingleValueBlock(0), 4);
    List<Page> input = ImmutableList.of(new Page(4, createLongsBlock(1, 2, 3, 4), trueMaskAllNull), new Page(4, createLongsBlock(5, 6, 7, 8), trueNullRleMask));
    MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT).row(// all rows should be filtered by nulls
    0L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : JoinCompiler(com.facebook.presto.sql.gen.JoinCompiler) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) AggregationOperatorFactory(com.facebook.presto.operator.AggregationOperator.AggregationOperatorFactory) TempStorageStandaloneSpillerFactory(com.facebook.presto.spiller.TempStorageStandaloneSpillerFactory) NodeSpillConfig(com.facebook.presto.spiller.NodeSpillConfig) Page(com.facebook.presto.common.Page) SpillerStats(com.facebook.presto.spiller.SpillerStats) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) TestingTempStorageManager(com.facebook.presto.testing.TestingTempStorageManager) AggregationOperatorFactory(com.facebook.presto.operator.AggregationOperator.AggregationOperatorFactory) AccumulatorFactory(com.facebook.presto.operator.aggregation.AccumulatorFactory) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) Block(com.facebook.presto.common.block.Block) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Aggregations

AccumulatorFactory (com.facebook.presto.operator.aggregation.AccumulatorFactory)5 Page (com.facebook.presto.common.Page)4 Block (com.facebook.presto.common.block.Block)3 Accumulator (com.facebook.presto.operator.aggregation.Accumulator)3 Type (com.facebook.presto.common.type.Type)2 AggregationTestUtils.createGroupByIdBlock (com.facebook.presto.operator.aggregation.AggregationTestUtils.createGroupByIdBlock)2 AggregationTestUtils.getFinalBlock (com.facebook.presto.operator.aggregation.AggregationTestUtils.getFinalBlock)2 GroupedAccumulator (com.facebook.presto.operator.aggregation.GroupedAccumulator)2 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)2 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)2 JoinCompiler (com.facebook.presto.sql.gen.JoinCompiler)2 ImmutableList (com.google.common.collect.ImmutableList)2 Test (org.testng.annotations.Test)2 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)1 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)1 PageBuilder (com.facebook.presto.common.PageBuilder)1 BlockEncodingManager (com.facebook.presto.common.block.BlockEncodingManager)1 ByteArrayBlock (com.facebook.presto.common.block.ByteArrayBlock)1 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)1 BigintType (com.facebook.presto.common.type.BigintType)1