Skip to content

[RFC] Paginating composite aggregation or pushdown size_limit to bucket size #4304

Closed
3 of 3 issues completed

Description

@LantaoJin

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:

  1. | 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 to multi-terms aggregation #4282
  2. | 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 to multi-terms aggregation #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.

Activity

  1. added
    enhancement
    PPL
    on Sep 16, 2025
  2. changed the title [-][FEATURE] The bucket size in SQL should equals to `search.max_buckets`[/-] [+][Bug] The bucket size in SQL should equals to `search.max_buckets`[/+] on Sep 16, 2025
  3. changed the title [-][Bug] The bucket size in SQL should equals to `search.max_buckets`[/-] [+][BUG] The bucket size in SQL should equals to `search.max_buckets`[/+] on Sep 16, 2025
  4. self-assigned this
    on Sep 16, 2025
  5. added
    bug
    and removed
    enhancement
    on Sep 16, 2025
  6. changed the title [-][BUG] The bucket size in SQL should equals to `search.max_buckets`[/-] [+][RFC] A better bucket size in aggregation[/+] on Sep 18, 2025
  7. changed the title [-][RFC] A better bucket size in aggregation[/-] [+][RFC] Paginating composite aggregation[/+] on Sep 18, 2025
  8. changed the title [-][RFC] Paginating composite aggregation[/-] [+][RFC] Paginating composite aggregation or pushdown size_limit to bucket size[/+] on Sep 18, 2025
  9. added
    enhancement
    on Sep 18, 2025
  10. penghuo commented on Sep 22, 2025

    @penghuo
    Collaborator

    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 #4282. The only problem is without paginating, the non-pushdown case in Calcite could be still incorrect.

    In non-pushdown case, we should use paginating to get all buckets, right? what is concern?

  11. Swiddis commented on Sep 22, 2025

    @Swiddis
    Collaborator

    | stats count() as cnt by product | sort cnt

    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 product only internally handles 10 buckets, that'd be a big win for memory savings.

  12. moved this from Not Started to Done in PPL Trackeron Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLbugclickbenchenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions