Skip to content

Commit

Permalink
[BugFix] Enforce AVG to return double data type (opendistro-for-elast…
Browse files Browse the repository at this point in the history
…icsearch#437)

* Enforce AVG to return double. Update UT, add IT.
  • Loading branch information
zhongnansu authored Apr 22, 2020
1 parent bf4810d commit 5870bbf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.Type;
import com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.TypeExpression;

import static com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.base.ESDataType.DOUBLE;
import static com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.base.ESDataType.ES_TYPE;
import static com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.base.ESDataType.INTEGER;
import static com.amazon.opendistroforelasticsearch.sql.antlr.semantic.types.base.ESDataType.NUMBER;
Expand All @@ -33,7 +34,7 @@ public enum AggregateFunction implements TypeExpression {
),
MAX(func(T(NUMBER)).to(T)),
MIN(func(T(NUMBER)).to(T)),
AVG(func(T(NUMBER)).to(T)),
AVG(func(T(NUMBER)).to(DOUBLE)),
SUM(func(T(NUMBER)).to(T));

private TypeExpressionSpec[] specifications;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void avgFunctionCallOnBooleanFieldShouldFail() {
expectValidationFailWithErrorMessages(
"SELECT AVG(p.active) FROM semantics s, s.projects p",
"Function [AVG] cannot work with [BOOLEAN].",
"Usage: AVG(NUMBER T) -> T"
"Usage: AVG(NUMBER T) -> DOUBLE"
);
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public void useAvgFunctionCallWithTextFieldInHavingClauseShouldFail() {
expectValidationFailWithErrorMessages(
"SELECT city FROM semantics GROUP BY city HAVING AVG(address) > 10",
"Function [AVG] cannot work with [TEXT].",
"Usage: AVG(NUMBER T) -> T"
"Usage: AVG(NUMBER T) -> DOUBLE"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AggregationExpressionIT extends SQLIntegTestCase {
@Override
protected void init() throws Exception {
loadIndex(Index.ACCOUNT);
loadIndex(Index.BANK);
}

@Test
Expand Down Expand Up @@ -66,6 +67,33 @@ public void noGroupKeyMaxAddLiteralShouldPass() {
verifyDataRows(response, rows(41));
}

@Test
public void noGroupKeyAvgOnIntegerShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT AVG(age) as avg " +
"FROM %s",
Index.BANK.getName()));

verifySchema(response, schema("avg", "avg", "double"));
verifyDataRows(response, rows(34));
}

@Test
public void hasGroupKeyAvgOnIntegerShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, AVG(age) as avg " +
"FROM %s " +
"GROUP BY gender",
Index.BANK.getName()));

verifySchema(response,
schema("gender", null, "text"),
schema("avg", "avg", "double"));
verifyDataRows(response,
rows("m", 34.25),
rows("f", 33.666666666666664d));
}

@Test
public void hasGroupKeyMaxAddMinShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,9 @@ public static String getBankIndexMapping() {
" \"type\": \"text\"\n" +
" },\n" +
" \"gender\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"type\": \"text\",\n" +
" \"fielddata\": true\n" +
" }," +
" \"lastname\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
Expand Down

0 comments on commit 5870bbf

Please sign in to comment.