generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Enable pushdown optimization for filtered aggregation #4213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
qianheng-aws
merged 15 commits into
opensearch-project:main
from
dai-chen:support-count-eval-function-pushdown
Sep 5, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4981faa
Enable filtered aggregation pushdown
dai-chen dcce4a8
Add basic UT and ignore IT for now
dai-chen 1ba35d6
Enable aggregate case to filter rule and fix UT and IT
dai-chen db01e0a
Add expected json file for no pushdown test
dai-chen b946301
Remove unnecessary aggregate case to filter rule
dai-chen d4798e3
Add UT for IS_TRUE support
dai-chen 3efd962
Add more explain IT
dai-chen cc0d5c5
Refactor UT
dai-chen 9a3daf5
Extract aggregate filter analyzer abstraction
dai-chen 482f101
Add more UT
dai-chen e4b9565
Refactor UT with fluent API
dai-chen fd1cd99
Add UT for distinct count
dai-chen 8f8818c
Address comment by adding UT for script filter pushdown
dai-chen 04d76b9
Merge branch 'main' into support-count-eval-function-pushdown
dai-chen c23d17e
Fix spotless
dai-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
6 changes: 6 additions & 0 deletions
6
integ-test/src/test/resources/expectedOutput/calcite/explain_count_eval_complex_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalAggregate(group=[{}], mature_count=[COUNT($0)])\n LogicalProject($f1=[CASE(SEARCH($10, Sarg[(30..50)]), 1, null:NULL)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n", | ||
| "physical": "EnumerableLimit(fetch=[10000])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]], PushDownContext=[[AGGREGATION->rel#:LogicalAggregate.NONE.[](input=RelSubset#,group={},mature_count=COUNT() FILTER $0)], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":0,\"timeout\":\"1m\",\"aggregations\":{\"mature_count\":{\"filter\":{\"range\":{\"age\":{\"from\":30.0,\"to\":50.0,\"include_lower\":false,\"include_upper\":false,\"boost\":1.0}}},\"aggregations\":{\"mature_count\":{\"value_count\":{\"field\":\"_index\"}}}}}}, requestedTotalSize=2147483647, pageSize=null, startFrom=0)])\n" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
integ-test/src/test/resources/expectedOutput/calcite/explain_count_eval_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalAggregate(group=[{}], mature_count=[COUNT($0)])\n LogicalProject($f1=[CASE(>($10, 30), 1, null:NULL)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n", | ||
| "physical": "EnumerableLimit(fetch=[10000])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]], PushDownContext=[[AGGREGATION->rel#:LogicalAggregate.NONE.[](input=RelSubset#,group={},mature_count=COUNT() FILTER $0)], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":0,\"timeout\":\"1m\",\"aggregations\":{\"mature_count\":{\"filter\":{\"range\":{\"age\":{\"from\":30,\"to\":null,\"include_lower\":false,\"include_upper\":true,\"boost\":1.0}}},\"aggregations\":{\"mature_count\":{\"value_count\":{\"field\":\"_index\"}}}}}}, requestedTotalSize=2147483647, pageSize=null, startFrom=0)])\n" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...rc/test/resources/expectedOutput/calcite_no_pushdown/explain_count_eval_complex_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalAggregate(group=[{}], mature_count=[COUNT($0)])\n LogicalProject($f1=[CASE(SEARCH($10, Sarg[(30..50)]), 1, null:NULL)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n", | ||
| "physical": "EnumerableLimit(fetch=[10000])\n EnumerableAggregate(group=[{}], mature_count=[COUNT() FILTER $0])\n EnumerableCalc(expr#0..18=[{inputs}], expr#19=[Sarg[(30..50)]], expr#20=[SEARCH($t10, $t19)], expr#21=[IS TRUE($t20)], $f1=[$t21])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...g-test/src/test/resources/expectedOutput/calcite_no_pushdown/explain_count_eval_push.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "calcite": { | ||
| "logical": "LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalAggregate(group=[{}], mature_count=[COUNT($0)])\n LogicalProject($f1=[CASE(>($10, 30), 1, null:NULL)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n", | ||
| "physical": "EnumerableLimit(fetch=[10000])\n EnumerableAggregate(group=[{}], mature_count=[COUNT() FILTER $0])\n EnumerableCalc(expr#0..18=[{inputs}], expr#19=[30], expr#20=[>($t10, $t19)], expr#21=[IS TRUE($t20)], $f1=[$t21])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_bank]])\n" | ||
| } | ||
| } |
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
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
72 changes: 72 additions & 0 deletions
72
opensearch/src/main/java/org/opensearch/sql/opensearch/request/AggregateFilterAnalyzer.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.opensearch.request; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.apache.calcite.rel.core.AggregateCall; | ||
| import org.apache.calcite.rel.core.Project; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.opensearch.search.aggregations.AggregationBuilder; | ||
| import org.opensearch.search.aggregations.AggregationBuilders; | ||
| import org.opensearch.sql.opensearch.request.PredicateAnalyzer.QueryExpression; | ||
| import org.opensearch.sql.opensearch.response.agg.FilterParser; | ||
| import org.opensearch.sql.opensearch.response.agg.MetricParser; | ||
|
|
||
| /** Analyzer for converting aggregate filter conditions into OpenSearch filter aggregations. */ | ||
| @RequiredArgsConstructor | ||
| public class AggregateFilterAnalyzer { | ||
|
|
||
| /** Helper containing row type, field types, and cluster context for analysis. */ | ||
| private final AggregateAnalyzer.AggregateBuilderHelper helper; | ||
|
|
||
| /** Project containing filter expressions referenced by aggregate calls. */ | ||
| private final Project project; | ||
|
|
||
| /** | ||
| * Analyzes and applies filter to aggregation if the AggregateCall has a filter condition. | ||
| * | ||
| * @param aggResult the base aggregation and parser to potentially wrap with filter | ||
| * @param aggCall the aggregate call which may contain filter information | ||
| * @param aggFieldName name for the filtered aggregation | ||
| * @return wrapped aggregation with filter if present, otherwise the original result | ||
| * @throws PredicateAnalyzer.ExpressionNotAnalyzableException if filter condition cannot be | ||
| * analyzed | ||
| */ | ||
| public Pair<AggregationBuilder, MetricParser> analyze( | ||
| Pair<AggregationBuilder, MetricParser> aggResult, AggregateCall aggCall, String aggFieldName) | ||
| throws PredicateAnalyzer.ExpressionNotAnalyzableException { | ||
| if (project == null || !aggCall.hasFilter()) { | ||
| return aggResult; | ||
| } | ||
|
|
||
| QueryExpression queryExpression = analyzeAggregateFilter(aggCall); | ||
| return Pair.of( | ||
| buildFilterAggregation(aggResult.getLeft(), aggFieldName, queryExpression), | ||
| buildFilterParser(aggResult.getRight(), aggFieldName)); | ||
| } | ||
|
|
||
| private QueryExpression analyzeAggregateFilter(AggregateCall aggCall) | ||
| throws PredicateAnalyzer.ExpressionNotAnalyzableException { | ||
| RexNode filterCondition = project.getProjects().get(aggCall.filterArg); | ||
| return PredicateAnalyzer.analyzeExpression( | ||
| filterCondition, | ||
| helper.rowType.getFieldNames(), | ||
| helper.fieldTypes, | ||
| helper.rowType, | ||
| helper.cluster); | ||
| } | ||
|
|
||
| private AggregationBuilder buildFilterAggregation( | ||
| AggregationBuilder aggBuilder, String aggFieldName, QueryExpression queryExpression) { | ||
| return AggregationBuilders.filter(aggFieldName, queryExpression.builder()) | ||
| .subAggregation(aggBuilder); | ||
| } | ||
|
|
||
| private MetricParser buildFilterParser(MetricParser aggParser, String aggFieldName) { | ||
| return FilterParser.builder().name(aggFieldName).metricsParser(aggParser).build(); | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.