-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working multiple filtered/unfiltered GROUP BY
- Loading branch information
Showing
7 changed files
with
424 additions
and
99 deletions.
There are no files selected for viewing
219 changes: 219 additions & 0 deletions
219
pinot-core/src/main/java/org/apache/pinot/core/operator/query/FilteredGroupByOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.pinot.core.operator.query; | ||
|
||
import java.util.Collection; | ||
import java.util.IdentityHashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.apache.pinot.common.request.context.ExpressionContext; | ||
import org.apache.pinot.common.utils.DataSchema; | ||
import org.apache.pinot.core.common.Operator; | ||
import org.apache.pinot.core.data.table.IntermediateRecord; | ||
import org.apache.pinot.core.data.table.TableResizer; | ||
import org.apache.pinot.core.operator.BaseOperator; | ||
import org.apache.pinot.core.operator.ExecutionStatistics; | ||
import org.apache.pinot.core.operator.blocks.TransformBlock; | ||
import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock; | ||
import org.apache.pinot.core.operator.transform.TransformOperator; | ||
import org.apache.pinot.core.query.aggregation.function.AggregationFunction; | ||
import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult; | ||
import org.apache.pinot.core.query.aggregation.groupby.DefaultGroupByExecutor; | ||
import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder; | ||
import org.apache.pinot.core.query.aggregation.groupby.GroupKeyGenerator; | ||
import org.apache.pinot.core.query.request.context.QueryContext; | ||
import org.apache.pinot.core.util.GroupByUtils; | ||
import org.apache.pinot.spi.trace.Tracing; | ||
|
||
|
||
/** | ||
* The <code>FilteredGroupByOperator</code> class provides the operator for group-by query on a single segment when | ||
* there are 1 or more filter expressions on aggregations. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public class FilteredGroupByOperator extends BaseOperator<GroupByResultsBlock> { | ||
private static final String EXPLAIN_NAME = "GROUP_BY_FILTERED"; | ||
|
||
@Nullable | ||
private final AggregationFunction[] _aggregationFunctions; | ||
private final List<Pair<AggregationFunction[], TransformOperator>> _aggFunctionsWithTransformOperator; | ||
private final ExpressionContext[] _groupByExpressions; | ||
private final long _numTotalDocs; | ||
private long _numDocsScanned; | ||
private long _numEntriesScannedInFilter; | ||
private long _numEntriesScannedPostFilter; | ||
private final DataSchema _dataSchema; | ||
private final QueryContext _queryContext; | ||
private TableResizer _tableResizer; | ||
private GroupKeyGenerator _groupKeyGenerator = null; | ||
|
||
public FilteredGroupByOperator( | ||
@Nullable AggregationFunction[] aggregationFunctions, | ||
List<Pair<AggregationFunction[], TransformOperator>> aggFunctionsWithTransformOperator, | ||
ExpressionContext[] groupByExpressions, | ||
long numTotalDocs, | ||
QueryContext queryContext) { | ||
_aggregationFunctions = aggregationFunctions; | ||
_aggFunctionsWithTransformOperator = aggFunctionsWithTransformOperator; | ||
_groupByExpressions = groupByExpressions; | ||
_numTotalDocs = numTotalDocs; | ||
_queryContext = queryContext; | ||
_tableResizer = null; | ||
|
||
// NOTE: The indexedTable expects that the data schema will have group by columns before aggregation columns | ||
int numGroupByExpressions = groupByExpressions.length; | ||
int numAggregationFunctions = aggregationFunctions.length; | ||
int numColumns = numGroupByExpressions + numAggregationFunctions; | ||
String[] columnNames = new String[numColumns]; | ||
DataSchema.ColumnDataType[] columnDataTypes = new DataSchema.ColumnDataType[numColumns]; | ||
|
||
// Extract column names and data types for group-by columns | ||
for (int i = 0; i < numGroupByExpressions; i++) { | ||
ExpressionContext groupByExpression = groupByExpressions[i]; | ||
columnNames[i] = groupByExpression.toString(); | ||
columnDataTypes[i] = DataSchema.ColumnDataType.fromDataTypeSV( | ||
// TODO(egalpin): is this actually correct? | ||
aggFunctionsWithTransformOperator.get(i).getRight().getResultMetadata(groupByExpression).getDataType()); | ||
} | ||
|
||
// Extract column names and data types for aggregation functions | ||
for (int i = 0; i < numAggregationFunctions; i++) { | ||
AggregationFunction aggregationFunction = aggregationFunctions[i]; | ||
int index = numGroupByExpressions + i; | ||
columnNames[index] = aggregationFunction.getResultColumnName(); | ||
columnDataTypes[index] = aggregationFunction.getIntermediateResultColumnType(); | ||
} | ||
|
||
_dataSchema = new DataSchema(columnNames, columnDataTypes); | ||
} | ||
|
||
@Override | ||
protected GroupByResultsBlock getNextBlock() { | ||
// TODO(egalpin): Support Startree query resolution when possible, even with FILTER expressions | ||
assert _aggregationFunctions != null; | ||
boolean numGroupsLimitReached = false; | ||
int numAggregations = _aggregationFunctions.length; | ||
|
||
GroupByResultHolder[] groupByResultHolders = new GroupByResultHolder[numAggregations]; | ||
IdentityHashMap<AggregationFunction, Integer> resultHolderIndexMap = new IdentityHashMap<>(numAggregations); | ||
for (int i = 0; i < numAggregations; i++) { | ||
resultHolderIndexMap.put(_aggregationFunctions[i], i); | ||
} | ||
|
||
for (Pair<AggregationFunction[], TransformOperator> filteredAggregation : _aggFunctionsWithTransformOperator) { | ||
TransformOperator transformOperator = filteredAggregation.getRight(); | ||
AggregationFunction[] filteredAggFunctions = filteredAggregation.getLeft(); | ||
|
||
// Perform aggregation group-by on all the blocks | ||
DefaultGroupByExecutor groupByExecutor; | ||
if (_groupKeyGenerator == null) { | ||
groupByExecutor = new DefaultGroupByExecutor(_queryContext, filteredAggFunctions, _groupByExpressions, | ||
transformOperator); | ||
_groupKeyGenerator = groupByExecutor.getGroupKeyGenerator(); | ||
} else { | ||
groupByExecutor = new DefaultGroupByExecutor(_queryContext, filteredAggFunctions, _groupByExpressions, | ||
transformOperator, _groupKeyGenerator); | ||
} | ||
|
||
int numDocsScanned = 0; | ||
TransformBlock transformBlock; | ||
while ((transformBlock = transformOperator.nextBlock()) != null) { | ||
numDocsScanned += transformBlock.getNumDocs(); | ||
groupByExecutor.process(transformBlock); | ||
} | ||
|
||
_numDocsScanned += numDocsScanned; | ||
_numEntriesScannedInFilter += transformOperator.getExecutionStatistics().getNumEntriesScannedInFilter(); | ||
_numEntriesScannedPostFilter += (long) numDocsScanned * transformOperator.getNumColumnsProjected(); | ||
GroupByResultHolder[] filterGroupByResults = groupByExecutor.getGroupByResultHolders(); | ||
for (int i = 0; i < filteredAggFunctions.length; i++) { | ||
groupByResultHolders[resultHolderIndexMap.get(filteredAggFunctions[i])] = filterGroupByResults[i]; | ||
} | ||
} | ||
|
||
for (GroupByResultHolder groupByResultHolder : groupByResultHolders) { | ||
groupByResultHolder.ensureCapacity(_groupKeyGenerator.getNumKeys()); | ||
} | ||
|
||
// Check if the groups limit is reached | ||
numGroupsLimitReached = _groupKeyGenerator.getNumKeys() >= _queryContext.getNumGroupsLimit(); | ||
Tracing.activeRecording().setNumGroups(_queryContext.getNumGroupsLimit(), _groupKeyGenerator.getNumKeys()); | ||
|
||
// Trim the groups when iff: | ||
// - Query has ORDER BY clause | ||
// - Segment group trim is enabled | ||
// - There are more groups than the trim size | ||
// TODO: Currently the groups are not trimmed if there is no ordering specified. Consider ordering on group-by | ||
// columns if no ordering is specified. | ||
int minGroupTrimSize = _queryContext.getMinSegmentGroupTrimSize(); | ||
if (_queryContext.getOrderByExpressions() != null && minGroupTrimSize > 0) { | ||
int trimSize = GroupByUtils.getTableCapacity(_queryContext.getLimit(), minGroupTrimSize); | ||
if (_groupKeyGenerator.getNumKeys() > trimSize) { | ||
if (_tableResizer == null) { | ||
_tableResizer = new TableResizer(_dataSchema, _queryContext); | ||
} | ||
Collection<IntermediateRecord> intermediateRecords = | ||
_tableResizer.trimInSegmentResults(_groupKeyGenerator, groupByResultHolders, trimSize); | ||
GroupByResultsBlock resultsBlock = new GroupByResultsBlock(_dataSchema, intermediateRecords); | ||
resultsBlock.setNumGroupsLimitReached(numGroupsLimitReached); | ||
return resultsBlock; | ||
} | ||
} | ||
|
||
AggregationGroupByResult aggGroupByResult = | ||
new AggregationGroupByResult(_groupKeyGenerator, _aggregationFunctions, groupByResultHolders); | ||
GroupByResultsBlock resultsBlock = new GroupByResultsBlock(_dataSchema, aggGroupByResult); | ||
resultsBlock.setNumGroupsLimitReached(numGroupsLimitReached); | ||
return resultsBlock; | ||
} | ||
|
||
@Override | ||
public List<Operator> getChildOperators() { | ||
return _aggFunctionsWithTransformOperator.stream().map(Pair::getRight).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public ExecutionStatistics getExecutionStatistics() { | ||
return new ExecutionStatistics(_numDocsScanned, _numEntriesScannedInFilter, _numEntriesScannedPostFilter, | ||
_numTotalDocs); | ||
} | ||
|
||
@Override | ||
public String toExplainString() { | ||
StringBuilder stringBuilder = new StringBuilder(EXPLAIN_NAME).append("(groupKeys:"); | ||
if (_groupByExpressions.length > 0) { | ||
stringBuilder.append(_groupByExpressions[0].toString()); | ||
for (int i = 1; i < _groupByExpressions.length; i++) { | ||
stringBuilder.append(", ").append(_groupByExpressions[i].toString()); | ||
} | ||
} | ||
|
||
stringBuilder.append(", aggregations:"); | ||
if (_aggregationFunctions.length > 0) { | ||
stringBuilder.append(_aggregationFunctions[0].toExplainString()); | ||
for (int i = 1; i < _aggregationFunctions.length; i++) { | ||
stringBuilder.append(", ").append(_aggregationFunctions[i].toExplainString()); | ||
} | ||
} | ||
|
||
return stringBuilder.append(')').toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.