Skip to content

[9.0] Avoid O(N^2) in VALUES with ordinals grouping (#130576) #130773

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

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public class ValuesAggregatorBenchmark {
try {
for (String groups : ValuesAggregatorBenchmark.class.getField("groups").getAnnotationsByType(Param.class)[0].value()) {
for (String dataType : ValuesAggregatorBenchmark.class.getField("dataType").getAnnotationsByType(Param.class)[0].value()) {
run(Integer.parseInt(groups), dataType, 10);
run(Integer.parseInt(groups), dataType, 10, 0);
run(Integer.parseInt(groups), dataType, 10, 1);
}
}
} catch (NoSuchFieldException e) {
Expand All @@ -103,7 +104,10 @@ public class ValuesAggregatorBenchmark {
@Param({ BYTES_REF, INT, LONG })
public String dataType;

private static Operator operator(DriverContext driverContext, int groups, String dataType) {
@Param({ "0", "1" })
public int numOrdinalMerges;

private static Operator operator(DriverContext driverContext, int groups, String dataType, int numOrdinalMerges) {
if (groups == 1) {
return new AggregationOperator(
List.of(supplier(dataType).aggregatorFactory(AggregatorMode.SINGLE, List.of(0)).apply(driverContext)),
Expand All @@ -115,7 +119,24 @@ private static Operator operator(DriverContext driverContext, int groups, String
List.of(supplier(dataType).groupingAggregatorFactory(AggregatorMode.SINGLE, List.of(1))),
() -> BlockHash.build(groupSpec, driverContext.blockFactory(), 16 * 1024, false),
driverContext
);
) {
@Override
public Page getOutput() {
mergeOrdinal();
return super.getOutput();
}

// simulate OrdinalsGroupingOperator
void mergeOrdinal() {
var merged = supplier(dataType).groupingAggregatorFactory(AggregatorMode.SINGLE, List.of(1)).apply(driverContext);
for (int i = 0; i < numOrdinalMerges; i++) {
for (int p = 0; p < groups; p++) {
merged.addIntermediateRow(p, aggregators.getFirst(), p);
}
}
aggregators.set(0, merged);
}
};
}

private static AggregatorFunctionSupplier supplier(String dataType) {
Expand Down Expand Up @@ -314,12 +335,12 @@ private static Block groupingBlock(int groups) {

@Benchmark
public void run() {
run(groups, dataType, OP_COUNT);
run(groups, dataType, OP_COUNT, numOrdinalMerges);
}

private static void run(int groups, String dataType, int opCount) {
private static void run(int groups, String dataType, int opCount, int numOrdinalMerges) {
DriverContext driverContext = driverContext();
try (Operator operator = operator(driverContext, groups, dataType)) {
try (Operator operator = operator(driverContext, groups, dataType, numOrdinalMerges)) {
Page page = page(groups, dataType);
for (int i = 0; i < opCount; i++) {
operator.addInput(page.shallowCopy());
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog/130576.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 130576
summary: Avoid O(N^2) in VALUES with ordinals grouping
area: ES|QL
type: bug
issues: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading