Skip to content

Commit

Permalink
Fail fast filter optimization in any case where segment is not match all
Browse files Browse the repository at this point in the history
Signed-off-by: Finn Carroll <carrofin@amazon.com>
  • Loading branch information
finnegancarroll committed Aug 9, 2024
1 parent 252742b commit ae267fa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,49 @@ setup:
- match: { aggregations.my_range.buckets.3.from: 1.5 }
- is_false: aggregations.my_range.buckets.3.to
- match: { aggregations.my_range.buckets.3.doc_count: 2 }

---
"Range query and aggregation test":
- do:
bulk:
refresh: true
index: range-agg-w-query
body:
- '{"index": {}}'
- '{"routing": "route1", "v": -10}'
- '{"index": {}}'
- '{"routing": "route1", "v": -5}'
- '{"index": {}}'
- '{"routing": "route1", "v": 10}'
- '{"index": {}}'
- '{"routing": "route2", "v": 15}'
- '{"index": {}}'
- '{"routing": "route2", "v": 20}'

- do:
search:
index: range-agg-w-query
body:
query:
bool:
must:
match_all: {}
filter:
- terms:
routing:
- "route1"
aggregations:
aggregationName:
range:
field: v
keyed: true
ranges:
- to: 0
key: "0"
- from: 0
key: "1"
_source: false

- match: { hits.total.value: 3 }
- match: { aggregations.aggregationName.buckets.0.doc_count: 2 }
- match: { aggregations.aggregationName.buckets.1.doc_count: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ void setRanges(Ranges ranges) {
public boolean tryOptimize(final LeafReaderContext leafCtx, final BiConsumer<Long, Long> incrementDocCount, boolean segmentMatchAll)
throws IOException {
segments.incrementAndGet();
if (!canOptimize) {
return false;
}

if (!canOptimize) return false;
if (!segmentMatchAll) return false;
if (leafCtx.reader().hasDeletions()) return false;

PointValues values = leafCtx.reader().getPointValues(aggregatorBridge.fieldType.name());
if (values == null) return false;
// only proceed if every document corresponds to exactly one point
Expand All @@ -120,7 +118,7 @@ public boolean tryOptimize(final LeafReaderContext leafCtx, final BiConsumer<Lon
return false;
}

Ranges ranges = getRanges(leafCtx, segmentMatchAll);
Ranges ranges = getRanges(leafCtx);
if (ranges == null) return false;

consumeDebugInfo(aggregatorBridge.tryOptimize(values, incrementDocCount, ranges));
Expand All @@ -132,10 +130,10 @@ public boolean tryOptimize(final LeafReaderContext leafCtx, final BiConsumer<Lon
return true;
}

Ranges getRanges(LeafReaderContext leafCtx, boolean segmentMatchAll) {
Ranges getRanges(LeafReaderContext leafCtx) {
if (!preparedAtShardLevel) {
try {
return getRangesFromSegment(leafCtx, segmentMatchAll);
return getRangesFromSegment(leafCtx);
} catch (IOException e) {
logger.warn("Failed to build ranges from segment.", e);
return null;
Expand All @@ -148,11 +146,7 @@ Ranges getRanges(LeafReaderContext leafCtx, boolean segmentMatchAll) {
* Even when ranges cannot be built at shard level, we can still build ranges
* at segment level when it's functionally match-all at segment level
*/
private Ranges getRangesFromSegment(LeafReaderContext leafCtx, boolean segmentMatchAll) throws IOException {
if (!segmentMatchAll) {
return null;
}

private Ranges getRangesFromSegment(LeafReaderContext leafCtx) throws IOException {
logger.debug("Shard {} segment {} functionally match all documents. Build the fast filter", shardId, leafCtx.ord);
return aggregatorBridge.tryBuildRangesFromSegment(leafCtx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import java.util.function.Function;

import static org.opensearch.core.xcontent.ConstructingObjectParser.optionalConstructorArg;
import static org.opensearch.search.aggregations.bucket.filterrewrite.DateHistogramAggregatorBridge.segmentMatchAll;

/**
* Aggregate all docs that match given ranges.
Expand Down Expand Up @@ -310,7 +311,7 @@ public ScoreMode scoreMode() {

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
boolean optimized = filterRewriteOptimizationContext.tryOptimize(ctx, this::incrementBucketDocCount, false);
boolean optimized = filterRewriteOptimizationContext.tryOptimize(ctx, this::incrementBucketDocCount, segmentMatchAll(context, ctx));
if (optimized) throw new CollectionTerminatedException();

final SortedNumericDoubleValues values = valuesSource.doubleValues(ctx);
Expand Down

0 comments on commit ae267fa

Please sign in to comment.