Skip to content

Commit

Permalink
Capture local ranges. Context getter is volatile
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <[email protected]>
  • Loading branch information
finnegancarroll committed Aug 18, 2024
1 parent 95dc749 commit d05b2e3
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.opensearch.search.aggregations.bucket.BucketsAggregator;
import org.opensearch.search.aggregations.bucket.filterrewrite.CompositeAggregatorBridge;
import org.opensearch.search.aggregations.bucket.filterrewrite.FilterRewriteOptimizationContext;
import org.opensearch.search.aggregations.bucket.filterrewrite.PackedValueRanges;
import org.opensearch.search.aggregations.bucket.missing.MissingOrder;
import org.opensearch.search.aggregations.bucket.terms.LongKeyedBucketOrds;
import org.opensearch.search.internal.SearchContext;
Expand Down Expand Up @@ -218,8 +219,9 @@ protected int rangeMax() {
}

@Override
protected long getOrd(int rangeIdx) {
long rangeStart = LongPoint.decodeDimension(filterRewriteOptimizationContext.getRanges().getLower(rangeIdx), 0);
protected long getOrd(int rangeIdx, PackedValueRanges ranges) {
assert(ranges != null);
long rangeStart = LongPoint.decodeDimension(ranges.getLower(rangeIdx), 0);
rangeStart = this.getFieldType().convertNanosToMillis(rangeStart);
long ord = bucketOrds.add(0, getRoundingPrepared().round(rangeStart));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected int rangeMax() {
/**
* Translate an index of the packed value range array to an agg bucket ordinal.
*/
protected long getOrd(int rangeIdx) {
protected long getOrd(int rangeIdx, PackedValueRanges ranges) {
return rangeIdx;
}

Expand All @@ -109,7 +109,7 @@ public final FilterRewriteOptimizationContext.DebugInfo tryOptimize(
ranges,
rangeMax(),
(activeIndex, docID) -> {
long ord = this.getOrd(activeIndex);
long ord = this.getOrd(activeIndex, ranges);
try {
incrementDocCount.accept(ord, (long) 1);
sub.collect(docID, ord);
Expand All @@ -124,7 +124,7 @@ public final FilterRewriteOptimizationContext.DebugInfo tryOptimize(
ranges,
rangeMax(),
(activeIndex, docCount) -> {
long ord = this.getOrd(activeIndex);
long ord = this.getOrd(activeIndex, ranges);
incrementDocCount.accept(ord, (long) docCount);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private boolean canOptimize(final Object parent, SearchContext context) throws I

boolean canOptimize = aggregatorBridge.canOptimize();
if (canOptimize) {
aggregatorBridge.setRangesConsumer(this::setRanges);
aggregatorBridge.setRangesConsumer((PackedValueRanges ranges) -> {
this.ranges = ranges;
});

this.shardId = context.indexShard().shardId().toString();

Expand All @@ -81,14 +83,6 @@ private boolean canOptimize(final Object parent, SearchContext context) throws I
return canOptimize;
}

public void setRanges(PackedValueRanges ranges) {
this.ranges = ranges;
}

public PackedValueRanges getRanges() {
return this.ranges;
}

/**
* Try to populate the bucket doc counts for aggregation
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,8 @@ protected Prepared getRoundingPrepared() {
}

@Override
protected long getOrd(int rangeIdx) {
PackedValueRanges ranges = filterRewriteOptimizationContext.getRanges();
protected long getOrd(int rangeIdx, PackedValueRanges ranges) {
assert(ranges != null);

long rangeStart = LongPoint.decodeDimension(ranges.getLower(rangeIdx), 0);
rangeStart = this.getFieldType().convertNanosToMillis(rangeStart);
long ord = getBucketOrds().add(0, getRoundingPrepared().round(rangeStart));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ protected long[] processHardBounds(long[] bounds) {
}

@Override
protected long getOrd(int rangeIdx) {
PackedValueRanges ranges = filterRewriteOptimizationContext.getRanges();
protected long getOrd(int rangeIdx, PackedValueRanges ranges) {
assert(ranges != null);

long rangeStart = LongPoint.decodeDimension(ranges.getLower(rangeIdx), 0);
rangeStart = this.getFieldType().convertNanosToMillis(rangeStart);
long ord = bucketOrds.add(0, getRoundingPrepared().round(rangeStart));
Expand Down

0 comments on commit d05b2e3

Please sign in to comment.