Skip to content

Aggregation support improvement #124

Closed as not planned
Closed as not planned

Description

@dai-chen

Is your feature request related to a problem? Please describe.
Currently there are limitations in aggregate query support in both SQL and PPL, including:

  1. Sort by aggregate function results: Composite Aggregation API only supports natural ordering of group fields. Reference. Issue: Select global top N records with grouping by multiple fields #121, ORDER BY doesn't work as expected on cardinality aggregations opendistro-for-elasticsearch/sql#378
  2. Sort null first or last independently: Composite Aggregation API doesn't have such option and only does ASC+NULL_FIRST or DESC+NULL_LAST. Reference.
  3. Only first 1000 buckets returned and users have no control. Issue: Aggregation doesn't honor default query limit size opendistro-for-elasticsearch/sql#1039

The use cases, especially #1, are very common in analytics. The limitation blocks the broad adoption of SQL/PPL.

Describe the solution you'd like
Either provide more complete aggregation or workarounds for different use cases specifically.

Describe alternatives you've considered
N/A

Additional context
Examples:

POST http://localhost:9200/_plugins/_sql/_explain
{
  "query": "SELECT state, AVG(balance) FROM accounts GROUP BY state"
}
{
  "root": {
    "name": "ProjectOperator",
    "description": {
      "fields": "[state, AVG(balance)]"
    },
    "children": [
      {
        "name": "OpenSearchIndexScan",
        "description": {
          "request": "OpenSearchQueryRequest(indexName=accounts, sourceBuilder={\"from\":0,\"size\":0,\"timeout\":\"1m\",\"aggregations\":{\"composite_buckets\":{\"composite\":{\"size\":1000,\"sources\":[{\"state\":{\"terms\":{\"field\":\"state.keyword\",\"missing_bucket\":true,\"order\":\"asc\"}}}]},\"aggregations\":{\"AVG(balance)\":{\"avg\":{\"field\":\"balance\"}}}}}}, searchDone=false)"
        },
        "children": []
      }
    ]
  }
}

Activity

  1. added
    enhancement
    PPL
    on Jun 11, 2021
  2. self-assigned this
    on Aug 4, 2021
  3. penghuo commented on Jan 13, 2022

    @penghuo
    Collaborator

    For 2. Submit feature request in OpenSearch. opensearch-project/OpenSearch#1899
    Working on PR now.

  4. removed their assignment
    on Dec 14, 2022
  5. moved this from Not Started to Done in PPL Trackeron Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PPLPriority-HighSQLenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions