Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use streaming aggregation for a correlated scalar subquery #10731

Merged
merged 10 commits into from Jun 20, 2018
Expand Up @@ -14,7 +14,6 @@
package com.facebook.presto.operator;

import com.facebook.presto.memory.context.LocalMemoryContext;
import com.facebook.presto.operator.aggregation.Accumulator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optiopnal] It would be great to add some benchmarks as we have for BenchmarkHashBuildAndJoinOperators

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sopel39 Karol, I created BenchmarkStreamingAggregationOperator using BenchmarkHashBuildAndJoinOperators as an example. The benchmark processes 1.4M rows split into pages 10K rows each. rowsPerGroup parameter determines the number of rows with the same grouping key. Here are the results for 1, 10 and 1,000 rowsPerGroup:

# Run complete. Total time: 00:04:25

Benchmark                                        (rowsPerGroup)  Mode  Cnt    Score   Error  Units
BenchmarkStreamingAggregationOperator.benchmark               1  avgt   30  375.009 ± 6.488  ms/op
BenchmarkStreamingAggregationOperator.benchmark              10  avgt   30   66.805 ± 1.929  ms/op
BenchmarkStreamingAggregationOperator.benchmark            1000  avgt   30   28.444 ± 0.346  ms/op

Processing larger groups is significantly faster than processing small groups. I'm going to explore your idea of reusable accumulators next.

import com.facebook.presto.operator.aggregation.AccumulatorFactory;
import com.facebook.presto.spi.Page;
import com.facebook.presto.spi.PageBuilder;
Expand All @@ -26,7 +25,6 @@

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -178,60 +176,4 @@ public Page getOutput()
state = State.FINISHED;
return pageBuilder.build();
}

private static class Aggregator
{
private final Accumulator aggregation;
private final Step step;
private final int intermediateChannel;

private Aggregator(AccumulatorFactory accumulatorFactory, Step step)
{
if (step.isInputRaw()) {
intermediateChannel = -1;
aggregation = accumulatorFactory.createAccumulator();
}
else {
checkArgument(accumulatorFactory.getInputChannels().size() == 1, "expected 1 input channel for intermediate aggregation");
intermediateChannel = accumulatorFactory.getInputChannels().get(0);
aggregation = accumulatorFactory.createIntermediateAccumulator();
}
this.step = step;
}

public Type getType()
{
if (step.isOutputPartial()) {
return aggregation.getIntermediateType();
}
else {
return aggregation.getFinalType();
}
}

public void processPage(Page page)
{
if (step.isInputRaw()) {
aggregation.addInput(page);
}
else {
aggregation.addIntermediate(page.getBlock(intermediateChannel));
}
}

public void evaluate(BlockBuilder blockBuilder)
{
if (step.isOutputPartial()) {
aggregation.evaluateIntermediate(blockBuilder);
}
else {
aggregation.evaluateFinal(blockBuilder);
}
}

public long getEstimatedSize()
{
return aggregation.getEstimatedSize();
}
}
}
@@ -0,0 +1,79 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.operator;

import com.facebook.presto.operator.aggregation.Accumulator;
import com.facebook.presto.operator.aggregation.AccumulatorFactory;
import com.facebook.presto.spi.Page;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.sql.planner.plan.AggregationNode;

import static com.google.common.base.Preconditions.checkArgument;

class Aggregator
{
private final Accumulator aggregation;
private final AggregationNode.Step step;
private final int intermediateChannel;

Aggregator(AccumulatorFactory accumulatorFactory, AggregationNode.Step step)
{
if (step.isInputRaw()) {
intermediateChannel = -1;
aggregation = accumulatorFactory.createAccumulator();
}
else {
checkArgument(accumulatorFactory.getInputChannels().size() == 1, "expected 1 input channel for intermediate aggregation");
intermediateChannel = accumulatorFactory.getInputChannels().get(0);
aggregation = accumulatorFactory.createIntermediateAccumulator();
}
this.step = step;
}

public Type getType()
{
if (step.isOutputPartial()) {
return aggregation.getIntermediateType();
}
else {
return aggregation.getFinalType();
}
}

public void processPage(Page page)
{
if (step.isInputRaw()) {
aggregation.addInput(page);
}
else {
aggregation.addIntermediate(page.getBlock(intermediateChannel));
}
}

public void evaluate(BlockBuilder blockBuilder)
{
if (step.isOutputPartial()) {
aggregation.evaluateIntermediate(blockBuilder);
}
else {
aggregation.evaluateFinal(blockBuilder);
}
}

public long getEstimatedSize()
{
return aggregation.getEstimatedSize();
}
}