diff --git a/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java b/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java index a1bf2b9961..346bd2d28c 100644 --- a/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java +++ b/core/src/main/java/org/opensearch/sql/expression/aggregation/NamedAggregator.java @@ -54,6 +54,8 @@ public class NamedAggregator extends Aggregator { /** * NamedAggregator. + * The aggregator properties {@link #condition} is inherited by named aggregator + * to avoid errors introduced by the property inconsistency. * * @param name name * @param delegated delegated @@ -64,6 +66,7 @@ public NamedAggregator( super(delegated.getFunctionName(), delegated.getArguments(), delegated.returnType); this.name = name; this.delegated = delegated; + this.condition = delegated.condition; } @Override diff --git a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java index 9b42c70e32..fc45f34ffe 100644 --- a/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java +++ b/core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java @@ -36,6 +36,7 @@ import static org.opensearch.sql.ast.dsl.AstDSL.compare; import static org.opensearch.sql.ast.dsl.AstDSL.field; import static org.opensearch.sql.ast.dsl.AstDSL.filter; +import static org.opensearch.sql.ast.dsl.AstDSL.filteredAggregate; import static org.opensearch.sql.ast.dsl.AstDSL.function; import static org.opensearch.sql.ast.dsl.AstDSL.intLiteral; import static org.opensearch.sql.ast.dsl.AstDSL.qualifiedName; @@ -624,4 +625,43 @@ public void limit_offset() { ) ); } + + /** + * SELECT COUNT(NAME) FILTER(WHERE age > 1) FROM test. + * This test is to verify that the aggregator properties are taken + * when wrapping it to {@link org.opensearch.sql.expression.aggregation.NamedAggregator} + */ + @Test + public void named_aggregator_with_condition() { + assertAnalyzeEqual( + LogicalPlanDSL.project( + LogicalPlanDSL.aggregation( + LogicalPlanDSL.relation("schema"), + ImmutableList.of( + DSL.named("count(string_value) filter(where integer_value > 1)", + dsl.count(DSL.ref("string_value", STRING)).condition(dsl.greater(DSL.ref( + "integer_value", INTEGER), DSL.literal(1)))) + ), + emptyList() + ), + DSL.named("count(string_value) filter(where integer_value > 1)", DSL.ref( + "count(string_value) filter(where integer_value > 1)", INTEGER)) + ), + AstDSL.project( + AstDSL.agg( + AstDSL.relation("schema"), + ImmutableList.of( + alias("count(string_value) filter(where integer_value > 1)", filteredAggregate( + "count", qualifiedName("string_value"), function( + ">", qualifiedName("integer_value"), intLiteral(1))))), + emptyList(), + emptyList(), + emptyList() + ), + AstDSL.alias("count(string_value) filter(where integer_value > 1)", filteredAggregate( + "count", qualifiedName("string_value"), function( + ">", qualifiedName("integer_value"), intLiteral(1)))) + ) + ); + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/AggregationIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/AggregationIT.java new file mode 100644 index 0000000000..3cbb222afe --- /dev/null +++ b/integ-test/src/test/java/org/opensearch/sql/sql/AggregationIT.java @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + * + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + * + */ + +package org.opensearch.sql.sql; + +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; +import static org.opensearch.sql.util.MatcherUtils.rows; +import static org.opensearch.sql.util.MatcherUtils.schema; +import static org.opensearch.sql.util.MatcherUtils.verifyDataRows; +import static org.opensearch.sql.util.MatcherUtils.verifySchema; + +import java.io.IOException; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; +import org.opensearch.sql.legacy.SQLIntegTestCase; + +public class AggregationIT extends SQLIntegTestCase { + @Override + protected void init() throws Exception { + loadIndex(Index.BANK); + } + + @Test + void filteredAggregateWithSubquery() throws IOException { + JSONObject response = executeQuery( + "SELECT COUNT(*) FILTER(WHERE age > 35) FROM (SELECT * FROM " + TEST_INDEX_BANK + + ") AS a"); + verifySchema(response, schema("COUNT(*)", null, "integer")); + verifyDataRows(response, rows(3)); + } +} diff --git a/integ-test/src/test/resources/correctness/queries/subquries.txt b/integ-test/src/test/resources/correctness/queries/subqueries.txt similarity index 100% rename from integ-test/src/test/resources/correctness/queries/subquries.txt rename to integ-test/src/test/resources/correctness/queries/subqueries.txt