Repository navigation
Description
Problem Statement
The current maximum number of aggregation bucket of SQL/PPL is 1000 as hard code, which could cause data correctness problem.
Current State
We implement group-by aggregation with CompositeAggregationBuilder, the bucket size is an interface of this builder, the number of composite buckets to return. The default value is 10 if not specified.
Problem Case:
| stats count() as cnt by product | sort cnt
Query only sorts the count in 1000 buckets. If the actual distinct value of product is greater than 1000, the result is incorrect. [FEATURE] Push down bucket aggregation with sorting by metrics tomulti-termsaggregation #4282| stats count() as cnt by product, the result of the query sometimes confused users since not all products display in the result as long as the number of bucket is greater than 1000.
Long-Term Goals
- The value of bucket size should not cause correctness problem.
- The value of bucket size should not cause performance problem.
- It should be configurable.
- The value or related limitation should make sense to main customers.
Proposal
Composite Aggregation supports paginating
Approach
If a request results in more than size buckets, then size buckets are returned. In this case, the results contain an after_key object containing the key of the next bucket in the list. To retrieve the next size buckets of the request, send the request again, supplying the after_key in the after parameter.
Alternative
Since we always has a system limit as the maximum amount of rows of PPL execution results, which controlled by config plugins.query.size_limit with default value 10000. We can push down the size_limit or head n to bucket size, ref #3961 , the current implementation still remains the max size to 1000, it only pushdown the limit which is less then 1000 to the bucket size. Instead, we should push down the size_limit to bucket size if no limit specified.
Implementation Discussion
- The Proposal could resolve the correctness issue of problem case (1), but when the cardinality of bucket is large, the performance could be worse and implementation could be overkill. An optimization for the problem case (2) is stopping paginating when reach the value
size_limit. - The Alternative works in most cases, for the problem case (2), because the existence of
size_limit, paginating is not required; for the problem of (1), we can resolve it by [FEATURE] Push down bucket aggregation with sorting by metrics tomulti-termsaggregation #4282. The only problem is without paginating, the non-pushdown case in Calcite could be still incorrect. - We could implement the feature step by step: see sub-issues.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
[-][FEATURE] The bucket size in SQL should equals to `search.max_buckets`[/-][+][Bug] The bucket size in SQL should equals to `search.max_buckets`[/+][-][Bug] The bucket size in SQL should equals to `search.max_buckets`[/-][+][BUG] The bucket size in SQL should equals to `search.max_buckets`[/+][-][BUG] The bucket size in SQL should equals to `search.max_buckets`[/-][+][RFC] A better bucket size in aggregation[/+][-][RFC] A better bucket size in aggregation[/-][+][RFC] Paginating composite aggregation[/+][-][RFC] Paginating composite aggregation[/-][+][RFC] Paginating composite aggregation or pushdown size_limit to bucket size[/+]penghuo commentedon Sep 22, 2025
In non-pushdown case, we should use paginating to get all buckets, right? what is concern?
Swiddis commentedon Sep 22, 2025
I think this specific scenario should be special-cased to pushdown the sort to the aggregation with the limit, because paginating through all the buckets will be expensive if this is further piped into something like
head. OpenSearch natively supports sorting by result count in terms (desc) and in rare_terms (asc).If something like
top productonly internally handles 10 buckets, that'd be a big win for memory savings.