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

GlobalAggregation with profile option enabled returns incorrect result #7114

Merged
merged 1 commit into from
Apr 12, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Fixed bug for searchable snapshot to take 'base_path' of blob into account ([#6558](https://github.com/opensearch-project/OpenSearch/pull/6558))
- Fix fuzziness validation ([#5805](https://github.com/opensearch-project/OpenSearch/pull/5805))
- Fix GetSnapshots to not return non-existent snapshots with ignore_unavailable=true ([#6839](https://github.com/opensearch-project/OpenSearch/pull/6839))
- Fix GlobalAggregation with profile option enabled returns incorrect result ([#7114](https://github.com/opensearch-project/OpenSearch/pull/7114))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ public void setupSuiteScopeCluster() throws Exception {
ensureSearchable();
}

public void testWithStatsSubAggregator() throws Exception {
public void testWithStatsSubAggregatorAndProfileEnabled() throws Exception {
testWithStatsSubAggregator(true);
}

public void testWithStatsSubAggregatorAndProfileDisabled() throws Exception {
testWithStatsSubAggregator(false);
}

private void testWithStatsSubAggregator(boolean profileEnabled) throws Exception {
SearchResponse response = client().prepareSearch("idx")
.setQuery(QueryBuilders.termQuery("tag", "tag1"))
.addAggregation(global("global").subAggregation(stats("value_stats").field("value")))
.setProfile(profileEnabled)
.get();

assertSearchResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opensearch.common.lucene.search.Queries;
import org.opensearch.search.aggregations.bucket.global.GlobalAggregator;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.profile.aggregation.ProfilingAggregator;
import org.opensearch.search.profile.query.CollectorResult;
import org.opensearch.search.profile.query.InternalProfileCollector;
import org.opensearch.search.query.QueryPhaseExecutionException;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void preProcess(SearchContext context) {
AggregatorFactories factories = context.aggregations().factories();
aggregators = factories.createTopLevelAggregators(context);
for (int i = 0; i < aggregators.length; i++) {
if (aggregators[i] instanceof GlobalAggregator == false) {
if (!isGlobalAggregator(context, aggregators[i])) {
collectors.add(aggregators[i]);
}
}
Expand Down Expand Up @@ -106,7 +107,7 @@ public void execute(SearchContext context) {
Aggregator[] aggregators = context.aggregations().aggregators();
List<Aggregator> globals = new ArrayList<>();
for (int i = 0; i < aggregators.length; i++) {
if (aggregators[i] instanceof GlobalAggregator) {
if (isGlobalAggregator(context, aggregators[i])) {
globals.add(aggregators[i]);
}
}
Expand Down Expand Up @@ -168,4 +169,16 @@ private Collector createCollector(SearchContext context, List<Aggregator> collec
}
return collector;
}

/**
* Checks if passed in aggregator is of type {@link GlobalAggregator}. This method takes care of Aggregator wrapped in
* {@link ProfilingAggregator} too
* @param context {@link SearchContext}
* @param aggregator input {@link Aggregator} instance to evaluate
* @return true input is {@link GlobalAggregator} instance or false otherwise
*/
private boolean isGlobalAggregator(SearchContext context, Aggregator aggregator) {
return (aggregator instanceof GlobalAggregator
|| (context.getProfilers() != null && ProfilingAggregator.unwrap(aggregator) instanceof GlobalAggregator));
}
}