Skip to content

Add composite bucket aggregation support#8

Open
varun-st wants to merge 1 commit into
pr/range-aggregation-from-datafusionfrom
pr/composite-aggregation
Open

Add composite bucket aggregation support#8
varun-st wants to merge 1 commit into
pr/range-aggregation-from-datafusionfrom
pr/composite-aggregation

Conversation

@varun-st

@varun-st varun-st commented Apr 7, 2026

Copy link
Copy Markdown
Owner

Description

Adds support for composite bucket aggregations to the DSL query executor plugin, enabling multi-dimensional grouping with efficient cursor-based pagination.

Changes

Core Implementation

CompositeGrouping
• Container for multiple source groupings (terms, histogram, date_histogram)
• Implements cursor-based pagination via buildAfterKeyFilter()
• Generates lexicographic comparison filters: (col1 > v1) OR (col1 = v1 AND col2 > v2) OR ...

CompositeBucketTranslator
• Converts composite aggregations to internal representation
• Extracts per-source sort direction and missing value ordering
• Builds InternalComposite response with proper bucket keys

GroupingUtils
• flatten(): Flattens composite groupings into individual sources
• hasExpressionGrouping(): Detects expression-based groupings (histogram, date_histogram)

AggregateConverter & AggregationMetadataBuilder
• Extended to handle composite groupings
• Injects after-key filter for pagination before GROUP BY
• Processes each source grouping for metadata generation

Key Features

✅ Multi-dimensional grouping (terms + histogram + date_histogram)
✅ Cursor-based pagination with after parameter
✅ Per-source sort direction (ASC/DESC)
✅ Per-source missing value ordering (FIRST/LAST)
✅ Lexicographic ordering for consistent pagination
✅ Nested sub-aggregations support

SQL Translation

Composite aggregations translate to a single SQL query with:
• Multi-column GROUP BY for all sources
• WHERE clause for cursor-based filtering (when after is provided)
• Computed expressions for histogram/date_histogram sources

Example:
json
{
"composite": {
"sources": [
{ "category": { "terms": { "field": "category" } } },
{ "price_bucket": { "histogram": { "field": "price", "interval": 100 } } }
],
"after": { "category": "Electronics", "price_bucket": 200 }
}
}

Generated SQL:
sql
SELECT
category,
FLOOR((price - 0) / 100) * 100 AS price_bucket,
COUNT(*) AS _count
FROM table
WHERE (category > 'Electronics')
OR (category = 'Electronics' AND FLOOR((price - 0) / 100) * 100 > 200)
GROUP BY category, FLOOR((price - 0) / 100) * 100
ORDER BY category ASC, FLOOR((price - 0) / 100) * 100 ASC

Testing

Unit Tests:
• CompositeGroupingTests: Field name extraction, after-key filter generation
• CompositeBucketTranslatorTests: Source conversion, ordering configuration
• GroupingUtilsTests: Flattening and expression detection

Integration Tests:
• Basic composite with single/multiple sources
• Pagination with no duplicate results across pages
• Multi-dimensional pagination with lexicographic ordering
• Custom sort order and missing value handling

Implements composite aggregations with multi-dimensional grouping and cursor-based pagination.

- CompositeGrouping: Wraps multiple source groupings (terms, histogram, date_histogram)
- CompositeBucketTranslator: Converts composite aggregations to SQL with per-source ordering
- Pagination: Lexicographic filtering via buildAfterKeyFilter() for efficient cursor navigation
- GroupingUtils: Helper methods for flattening and detecting expression groupings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant