Skip to content

Commit

Permalink
Avoid global ordinals in composite aggregation (#74559)
Browse files Browse the repository at this point in the history
A composite aggregation on a keyword field requires global ordinals today to ensure fast comparisons between
segments. It only needs to keep track of the top N composite buckets, however. Since N is typically small, we can just
use the segment ordinal for comparison when collecting inside a segment and remap ordinals when we go to the next
segment.

Closes #47452
  • Loading branch information
ywelsch committed Jun 29, 2021
1 parent 3f8e41b commit 5cfcb2f
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A specialized {@link PriorityQueue} implementation for composite buckets.
Expand Down Expand Up @@ -56,6 +58,7 @@ public int hashCode() {

private LongArray docCounts;
private boolean afterKeyIsSet = false;
private int leafReaderOrd = -1; // current LeafReaderContext ordinal

/**
* Constructs a composite queue with the specified size and sources.
Expand Down Expand Up @@ -230,14 +233,26 @@ LeafBucketCollector getLeafCollector(Comparable forceLeadSourceValue,
LeafReaderContext context, LeafBucketCollector in) throws IOException {
int last = arrays.length - 1;
LeafBucketCollector collector = in;
boolean requiresRehashingWhenSwitchingLeafReaders = false;
while (last > 0) {
collector = arrays[last--].getLeafCollector(context, collector);
SingleDimensionValuesSource<?> valuesSource = arrays[last--];
requiresRehashingWhenSwitchingLeafReaders |= valuesSource.requiresRehashingWhenSwitchingLeafReaders();
collector = valuesSource.getLeafCollector(context, collector);
}
SingleDimensionValuesSource<?> valuesSource = arrays[last];
requiresRehashingWhenSwitchingLeafReaders |= valuesSource.requiresRehashingWhenSwitchingLeafReaders();
if (forceLeadSourceValue != null) {
collector = arrays[last].getLeafCollector(forceLeadSourceValue, context, collector);
collector = valuesSource.getLeafCollector(forceLeadSourceValue, context, collector);
} else {
collector = arrays[last].getLeafCollector(context, collector);
collector = valuesSource.getLeafCollector(context, collector);
}
boolean switchedLeafReaders = context.ord != leafReaderOrd;
if (map.isEmpty() == false && requiresRehashingWhenSwitchingLeafReaders && switchedLeafReaders) {
List<Map.Entry<Slot, Integer>> entries = map.entrySet().stream().collect(Collectors.toList());
map.clear();
entries.forEach(e -> map.put(e.getKey(), e.getValue()));
}
leafReaderOrd = context.ord;
return collector;
}

Expand Down

This file was deleted.

Loading

0 comments on commit 5cfcb2f

Please sign in to comment.