Skip to content
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

Fix the aggregation filter missing in named aggregators #123

Merged
merged 5 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class NamedAggregator extends Aggregator<AggregationState> {

/**
* 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
Expand All @@ -64,6 +66,7 @@ public NamedAggregator(
super(delegated.getFunctionName(), delegated.getArguments(), delegated.returnType);
this.name = name;
this.delegated = delegated;
this.condition = delegated.condition;
}

@Override
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/org/opensearch/sql/analysis/AnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))))
)
);
}
}
40 changes: 40 additions & 0 deletions integ-test/src/test/java/org/opensearch/sql/sql/AggregationIT.java
Original file line number Diff line number Diff line change
@@ -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));
}
}