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

Dynamic evaluation of GroupBy Initial Capacity #14001

Merged
merged 16 commits into from
Oct 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
package org.apache.pinot.core.query.aggregation.groupby;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.apache.pinot.common.request.context.ExpressionContext;
import org.apache.pinot.common.request.context.FilterContext;
import org.apache.pinot.common.request.context.predicate.InPredicate;
import org.apache.pinot.common.request.context.predicate.Predicate;
import org.apache.pinot.core.common.BlockValSet;
import org.apache.pinot.core.data.table.IntermediateRecord;
import org.apache.pinot.core.data.table.TableResizer;
Expand Down Expand Up @@ -110,7 +117,11 @@ public DefaultGroupByExecutor(QueryContext queryContext, AggregationFunction[] a

// Initialize result holders
int maxNumResults = _groupKeyGenerator.getGlobalGroupKeyUpperBound();
Integer optimalGroupByResultHolderCapacity = getGroupByResultHolderCapacityBasedOnFilterPredicate(queryContext);
praveenc7 marked this conversation as resolved.
Show resolved Hide resolved
int initialCapacity = Math.min(maxNumResults, maxInitialResultHolderCapacity);
if (optimalGroupByResultHolderCapacity != null) {
initialCapacity = Math.min(optimalGroupByResultHolderCapacity, initialCapacity);
}
int numAggregationFunctions = _aggregationFunctions.length;
_groupByResultHolders = new GroupByResultHolder[numAggregationFunctions];
for (int i = 0; i < numAggregationFunctions; i++) {
Expand Down Expand Up @@ -183,4 +194,26 @@ public GroupKeyGenerator getGroupKeyGenerator() {
public GroupByResultHolder[] getGroupByResultHolders() {
return _groupByResultHolders;
}

private Integer getGroupByResultHolderCapacityBasedOnFilterPredicate(QueryContext queryContext) {
if (queryContext.getFilter() == null || queryContext.getGroupByExpressions() == null) {
praveenc7 marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

List<FilterContext> filterContexts = queryContext.getFilter().getChildren() != null
? queryContext.getFilter().getChildren()
: List.of(queryContext.getFilter());

Map<ExpressionContext, InPredicate> predicateMap = filterContexts.stream()
.map(FilterContext::getPredicate)
.filter(predicate -> predicate.getType() == Predicate.Type.IN)
praveenc7 marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toMap(Predicate::getLhs, predicate -> (InPredicate) predicate));

OptionalInt result = queryContext.getGroupByExpressions().stream()
praveenc7 marked this conversation as resolved.
Show resolved Hide resolved
.map(predicateMap::get)
.filter(Objects::nonNull)
.mapToInt(inPredicate -> inPredicate.getValues().size())
.max();
return result.isPresent() ? result.getAsInt() : null;
}
}
Loading