Skip to content

Commit 727a573

Browse files
committed
enable pushdown distinct count approx
Signed-off-by: xinyual <xinyual@amazon.com>
1 parent 1382da6 commit 727a573

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteExplainIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ public void testExplainOnAggregationWithSumEnhancement() throws IOException {
619619
TEST_INDEX_BANK)));
620620
}
621621

622+
@Test
623+
public void testStatsDistinctCountApproxFunctionExplainWithPushDown() throws IOException {
624+
enabledOnlyWhenPushdownIsEnabled();
625+
String query =
626+
"source=opensearch-sql_test_index_account | stats distinct_count_approx(state) as"
627+
+ " distinct_states by gender";
628+
var result = explainQueryToString(query);
629+
String expected =
630+
loadFromFile(
631+
"expectedOutput/calcite/explain_agg_with_distinct_count_approx_enhancement.json");
632+
assertJsonEqualsIgnoreId(expected, result);
633+
}
634+
622635
@Test
623636
public void testExplainRegexMatchInWhereWithScriptPushdown() throws IOException {
624637
enabledOnlyWhenPushdownIsEnabled();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"calcite":{
3+
"logical":"LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalProject(distinct_states=[$1], gender=[$0])\n LogicalAggregate(group=[{0}], distinct_states=[DISTINCT_COUNT_APPROX($1)])\n LogicalProject(gender=[$4], state=[$7])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n",
4+
"physical":"CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[AGGREGATION->rel#:LogicalAggregate.NONE.[](input=RelSubset#,group={0},distinct_states=DISTINCT_COUNT_APPROX($1)), PROJECT->[distinct_states, gender], LIMIT->10000], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":0,\"timeout\":\"1m\",\"aggregations\":{\"composite_buckets\":{\"composite\":{\"size\":1000,\"sources\":[{\"gender\":{\"terms\":{\"field\":\"gender.keyword\",\"missing_bucket\":true,\"missing_order\":\"first\",\"order\":\"asc\"}}}]},\"aggregations\":{\"distinct_states\":{\"cardinality\":{\"field\":\"state.keyword\"}}}}}}, requestedTotalSize=2147483647, pageSize=null, startFrom=0)])\n"
5+
}
6+
}

opensearch/src/main/java/org/opensearch/sql/opensearch/executor/OpenSearchExecutionEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package org.opensearch.sql.opensearch.executor;
77

8+
import static org.opensearch.sql.expression.function.BuiltinFunctionName.DISTINCT_COUNT_APPROX;
9+
810
import java.security.AccessController;
911
import java.security.PrivilegedAction;
1012
import java.sql.PreparedStatement;
@@ -55,8 +57,6 @@
5557
import org.opensearch.sql.planner.physical.PhysicalPlan;
5658
import org.opensearch.sql.storage.TableScanOperator;
5759

58-
import static org.opensearch.sql.expression.function.BuiltinFunctionName.DISTINCT_COUNT_APPROX;
59-
6060
/** OpenSearch execution engine implementation. */
6161
public class OpenSearchExecutionEngine implements ExecutionEngine {
6262
private static final Logger logger = LogManager.getLogger(OpenSearchExecutionEngine.class);

opensearch/src/main/java/org/opensearch/sql/opensearch/request/AggregateAnalyzer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,11 @@ yield switch (functionName) {
495495
}
496496
yield Pair.of(aggBuilder, new SinglePercentileParser(aggFieldName));
497497
}
498-
case DISTINCT_COUNT_APPROX -> Pair.of(
499-
helper.build(
500-
!args.isEmpty() ? args.getFirst() : null,
501-
AggregationBuilders.cardinality(aggFieldName)),
502-
new SingleValueParser(aggFieldName));
498+
case DISTINCT_COUNT_APPROX -> Pair.of(
499+
helper.build(
500+
!args.isEmpty() ? args.getFirst() : null,
501+
AggregationBuilders.cardinality(aggFieldName)),
502+
new SingleValueParser(aggFieldName));
503503
default -> throw new AggregateAnalyzer.AggregateAnalyzerException(
504504
String.format("Unsupported push-down aggregator %s", aggCall.getAggregation()));
505505
};

0 commit comments

Comments
 (0)