Skip to content

Fix stats aggregation returning zero results with size:0#20427

Merged
sandeshkr419 merged 4 commits intoopensearch-project:mainfrom
prudhvigodithi:aggs_bug
Jan 22, 2026
Merged

Fix stats aggregation returning zero results with size:0#20427
sandeshkr419 merged 4 commits intoopensearch-project:mainfrom
prudhvigodithi:aggs_bug

Conversation

@prudhvigodithi
Copy link
Copy Markdown
Member

@prudhvigodithi prudhvigodithi commented Jan 16, 2026

Description

While testing intra segment #20202 noticed the bug where for stats aggs collectRange method uses maximum instead of max variable. See the SumAggregator which works correctly.

Before fix

curl -s "localhost:9200/big5/_search?pretty" -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "metrics_stats": { "stats": { "field": "metrics.size" } } } }'
{
  "took" : 3,
  "timed_out" : false,
  "terminated_early" : true,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "metrics_stats" : {
      "count" : 0,
      "min" : null,
      "max" : null,
      "avg" : null,
      "sum" : 0.0
    }
  }
}

After fix

curl -s "localhost:9200/big5/_search?pretty" -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "metrics_stats": { "stats": { "field": "metrics.size" } } } }'
{
  "took" : 1080,
  "timed_out" : false,
  "terminated_early" : true,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "metrics_stats" : {
      "count" : 116000000,
      "min" : 1000.0,
      "max" : 9999.0,
      "avg" : 2438.516547112069,
      "sum" : 2.82867919465E11
    }
  }
}

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • Functionality includes testing.
  • API changes companion pull request created, if applicable.
  • Public documentation issue/PR created, if applicable.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Prudhvi Godithi <pgodithi@amazon.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jan 16, 2026

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

📝 Walkthrough

Walkthrough

Modified the collectRange method in StatsAggregator to use the explicit max parameter instead of a stored maximum value from maxes.get(0) when determining the range of documents to process. A corresponding test was added to validate the per-leaf range collection behavior.

Changes

Cohort / File(s) Summary
Core Logic
server/src/main/java/org/opensearch/search/aggregations/metrics/StatsAggregator.java
Modified loop termination condition in collectRange from iterating while doc < maxes.get(0) to doc < max, switching from stored value to explicit parameter-based range boundary.
Test Coverage
server/src/test/java/org/opensearch/search/aggregations/metrics/StatsAggregatorTests.java
Added testCollectRange() method with supporting imports (LeafReaderContext, LeafBucketCollector) to validate per-leaf collection logic by building expected statistics through direct value summation and asserting count, sum, min, max, and average.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

bug, Search:Aggregations

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing stats aggregation to return correct results when size:0 is specified.
Description check ✅ Passed The description includes problem explanation with code reference and before/after examples, but the Related Issues section is incomplete and the Check List items are not marked.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@prudhvigodithi prudhvigodithi added the v3.5.0 Issues and PRs related to version 3.5.0 label Jan 16, 2026
@prudhvigodithi prudhvigodithi changed the title Stats aggregation bug fix with size:0 Fix stats aggregation returning zero results with size:0 Jan 16, 2026
Signed-off-by: Prudhvi Godithi <pgodithi@amazon.com>
@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 374d98a: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 374d98a: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 8b2d689: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Copy Markdown
Contributor

❌ Gradle check result for 8b2d689: FAILURE

Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change?

@github-actions
Copy link
Copy Markdown
Contributor

✅ Gradle check result for 8b2d689: SUCCESS

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.23%. Comparing base (be7b387) to head (8b2d689).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main   #20427      +/-   ##
============================================
- Coverage     73.28%   73.23%   -0.05%     
- Complexity    71825    71868      +43     
============================================
  Files          5793     5793              
  Lines        328844   328846       +2     
  Branches      47343    47343              
============================================
- Hits         240978   240827     -151     
- Misses        68571    68743     +172     
+ Partials      19295    19276      -19     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@prudhvigodithi
Copy link
Copy Markdown
Member Author

Thanks @bowenlan-amzn, adding @asimmahmood1 @jainankitk

Copy link
Copy Markdown
Contributor

@asimmahmood1 asimmahmood1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! I'm surprised it wasn't caught in testing.

Looks like this was introduced in 3.4: #20067

I think Codecov report should've caught that is line isn't tested:

https://app.codecov.io/gh/opensearch-project/OpenSearch/pull/20427?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=checks&utm_campaign=pr+comments&utm_term=opensearch-project

@prudhvigodithi prudhvigodithi added the Search Search query, autocomplete ...etc label Jan 20, 2026
@github-project-automation github-project-automation bot moved this from Todo to In Progress in Performance Roadmap Jan 22, 2026
@sandeshkr419 sandeshkr419 merged commit a7c85f2 into opensearch-project:main Jan 22, 2026
40 of 44 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in Performance Roadmap Jan 22, 2026
Copy link
Copy Markdown
Contributor

@jainankitk jainankitk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @prudhvigodithi for fixing this. Hopefully we don't have similar issue for other aggregators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Search Search query, autocomplete ...etc v3.5.0 Issues and PRs related to version 3.5.0

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants