diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/aggregation/dsl/MetricAggregationBuilder.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/aggregation/dsl/MetricAggregationBuilder.java index aa116877df..a065b13196 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/aggregation/dsl/MetricAggregationBuilder.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/aggregation/dsl/MetricAggregationBuilder.java @@ -99,7 +99,10 @@ public Pair visitNamedAggregator( if (distinct) { switch (node.getFunctionName().getFunctionName()) { case "count": - return make(AggregationBuilders.cardinality(name), expression); + return make( + AggregationBuilders.cardinality(name), + expression, + new SingleValueParser(name)); default: throw new ExpressionEvaluationException(String.format( "unsupported distinct aggregator %s", node.getFunctionName().getFunctionName())); @@ -171,6 +174,12 @@ private AggregationBuilder make(CardinalityAggregationBuilder builder, Expressio return cardinalityHelper.build(expression, builder::field, builder::script); } + private Pair make(CardinalityAggregationBuilder builder, + Expression expression, + MetricParser parser) { + return Pair.of(cardinalityHelper.build(expression, builder::field, builder::script), parser); + } + /** * Replace star or literal with OpenSearch metadata field "_index". Because: 1) Analyzer already * converts * to string literal, literal check here can handle both COUNT(*) and COUNT(1). 2)