Add composite bucket aggregation support#8
Open
varun-st wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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