Skip to content

Commit

Permalink
Add count() support for PPL (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
penghuo authored Dec 4, 2020
1 parent 7dc8699 commit 41d48dc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
45 changes: 30 additions & 15 deletions docs/experiment/ppl/cmd/stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,23 @@ stats <aggregation>... [by-clause]...
* aggregation: mandatory. A statistical aggregation function. The argument of aggregation must be field.
* by-clause: optional. The one or more fields to group the results by. **Default**: If no <by-clause> is specified, the stats command returns only one row, which is the aggregation over the entire result set.

Example 1: Calculate the count of events
========================================

Example 1: Calculate the average of a field
The example show calculate the count of events in the accounts.

PPL query::

od> source=accounts | stats count();
fetched rows / total rows = 1/1
+-----------+
| count() |
|-----------|
| 4 |
+-----------+


Example 2: Calculate the average of a field
===========================================

The example show calculate the average age of all the accounts.
Expand All @@ -55,7 +70,7 @@ PPL query::
+------------+


Example 2: Calculate the average of a field by group
Example 3: Calculate the average of a field by group
====================================================

The example show calculate the average age of all the accounts group by gender.
Expand All @@ -72,23 +87,23 @@ PPL query::
+----------+--------------------+


Example 3: Calculate the average and sum of a field by group
============================================================
Example 4: Calculate the average, sum and count of a field by group
===================================================================

The example show calculate the average age and sum age of all the accounts group by gender.
The example show calculate the average age, sum age and count of events of all the accounts group by gender.

PPL query::

od> source=accounts | stats avg(age), sum(age) by gender;
od> source=accounts | stats avg(age), sum(age), count() by gender;
fetched rows / total rows = 2/2
+----------+--------------------+------------+
| gender | avg(age) | sum(age) |
|----------+--------------------+------------|
| F | 28.0 | 28 |
| M | 33.666666666666664 | 101 |
+----------+--------------------+------------+

Example 4: Calculate the maximum of a field
+----------+--------------------+------------+-----------+
| gender | avg(age) | sum(age) | count() |
|----------+--------------------+------------+-----------|
| F | 28.0 | 28 | 1 |
| M | 33.666666666666664 | 101 | 3 |
+----------+--------------------+------------+-----------+

Example 5: Calculate the maximum of a field
===========================================

The example calculates the max age of all the accounts.
Expand All @@ -103,7 +118,7 @@ PPL query::
| 36 |
+------------+

Example 5: Calculate the maximum and minimum of a field by group
Example 6: Calculate the maximum and minimum of a field by group
================================================================

The example calculates the max and min age values of all the accounts group by gender.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public void testStatsCount() throws IOException {
verifyDataRows(response, rows(1000));
}

@Test
public void testStatsCountAll() throws IOException {
JSONObject response =
executeQuery(String.format("source=%s | stats count()", TEST_INDEX_ACCOUNT));
verifySchema(response, schema("count()", null, "integer"));
verifyDataRows(response, rows(1000));
}

@Test
public void testStatsMin() throws IOException {
JSONObject response = executeQuery(String.format(
Expand Down
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenDistroPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ statsAggTerm
/** aggregation functions */
statsFunction
: statsFunctionName LT_PRTHS valueExpression RT_PRTHS #statsFunctionCall
| COUNT LT_PRTHS RT_PRTHS #countAllFunctionCall
| percentileAggFunction #percentileAggFunctionCall
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static com.amazon.opendistroforelasticsearch.sql.ppl.antlr.parser.OpenDistroPPLParser.WcFieldExpressionContext;

import com.amazon.opendistroforelasticsearch.sql.ast.expression.AggregateFunction;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.And;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Argument;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.Compare;
Expand Down Expand Up @@ -169,6 +170,12 @@ public UnresolvedExpression visitStatsFunctionCall(StatsFunctionCallContext ctx)
return new AggregateFunction(ctx.statsFunctionName().getText(), visit(ctx.valueExpression()));
}

@Override
public UnresolvedExpression visitCountAllFunctionCall(
OpenDistroPPLParser.CountAllFunctionCallContext ctx) {
return new AggregateFunction("count", AllFields.of());
}

@Override
public UnresolvedExpression visitPercentileAggFunction(PercentileAggFunctionContext ctx) {
return new AggregateFunction(ctx.PERCENTILE().getText(), visit(ctx.aggField),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static com.amazon.opendistroforelasticsearch.sql.ast.dsl.AstDSL.xor;
import static java.util.Collections.emptyList;

import com.amazon.opendistroforelasticsearch.sql.ast.expression.AllFields;
import com.amazon.opendistroforelasticsearch.sql.ast.expression.DataType;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -324,6 +325,27 @@ public void testPercentileAggFuncExpr() {
));
}

@Test
public void testCountFuncCallExpr() {
assertEqual("source=t | stats count() by b",
agg(
relation("t"),
exprList(
alias(
"count()",
aggregate("count", AllFields.of())
)
),
emptyList(),
exprList(
alias(
"b",
field("b")
)),
defaultStatsArgs()
));
}

@Test
public void testEvalFuncCallExpr() {
assertEqual("source=t | eval f=abs(a)",
Expand Down

0 comments on commit 41d48dc

Please sign in to comment.