/**
* Copyright (C) 2014-2016 LinkedIn Corp. (pinot-core@linkedin.com)
*
* 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.linkedin.pinot.core.operator.query;
import com.linkedin.pinot.common.request.GroupBy;
import com.linkedin.pinot.core.common.Block;
import com.linkedin.pinot.core.common.BlockId;
import com.linkedin.pinot.core.operator.BaseOperator;
import com.linkedin.pinot.core.operator.ExecutionStatistics;
import com.linkedin.pinot.core.operator.blocks.IntermediateResultsBlock;
import com.linkedin.pinot.core.operator.blocks.TransformBlock;
import com.linkedin.pinot.core.operator.transform.TransformExpressionOperator;
import com.linkedin.pinot.core.query.aggregation.AggregationFunctionContext;
import com.linkedin.pinot.core.query.aggregation.groupby.DefaultGroupByExecutor;
import com.linkedin.pinot.core.query.aggregation.groupby.GroupByExecutor;
import javax.annotation.Nonnull;
/**
* The <code>AggregationOperator</code> class provides the operator for aggregation group-by query on a single segment.
*/
public class AggregationGroupByOperator extends BaseOperator {
private static final String OPERATOR_NAME = "AggregationGroupByOperator";
private final AggregationFunctionContext[] _aggregationFunctionContexts;
private final GroupBy _groupBy;
private final int _numGroupsLimit;
private final TransformExpressionOperator _transformOperator;
private final long _numTotalRawDocs;
private ExecutionStatistics _executionStatistics;
public AggregationGroupByOperator(@Nonnull AggregationFunctionContext[] aggregationFunctionContexts,
@Nonnull GroupBy groupBy, int numGroupsLimit, @Nonnull TransformExpressionOperator transformOperator,
long numTotalRawDocs) {
_aggregationFunctionContexts = aggregationFunctionContexts;
_groupBy = groupBy;
_numGroupsLimit = numGroupsLimit;
_transformOperator = transformOperator;
_numTotalRawDocs = numTotalRawDocs;
}
@Override
public boolean open() {
_transformOperator.open();
return true;
}
@Override
public Block getNextBlock() {
int numDocsScanned = 0;
// Perform aggregation group-by on all the blocks.
GroupByExecutor groupByExecutor =
new DefaultGroupByExecutor(_aggregationFunctionContexts, _groupBy, _numGroupsLimit);
groupByExecutor.init();
TransformBlock transformBlock;
while ((transformBlock = (TransformBlock) _transformOperator.nextBlock()) != null) {
numDocsScanned += transformBlock.getNumDocs();
groupByExecutor.process(transformBlock);
}
groupByExecutor.finish();
// Create execution statistics.
long numEntriesScannedInFilter = _transformOperator.getExecutionStatistics().getNumEntriesScannedInFilter();
long numEntriesScannedPostFilter = numDocsScanned * _transformOperator.getNumProjectionColumns();
_executionStatistics =
new ExecutionStatistics(numDocsScanned, numEntriesScannedInFilter, numEntriesScannedPostFilter,
_numTotalRawDocs);
// Build intermediate result block based on aggregation group-by result from the executor.
return new IntermediateResultsBlock(_aggregationFunctionContexts, groupByExecutor.getResult());
}
@Override
public Block getNextBlock(BlockId blockId) {
throw new UnsupportedOperationException();
}
@Override
public String getOperatorName() {
return OPERATOR_NAME;
}
@Override
public boolean close() {
_transformOperator.close();
return true;
}
@Override
public ExecutionStatistics getExecutionStatistics() {
return _executionStatistics;
}
}