Skip to content

Commit 9e766db

Browse files
committed
Add unit tests
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 24bf1dd commit 9e766db

4 files changed

Lines changed: 193 additions & 0 deletions

File tree

docs/user/ppl/cmd/chart.rst

Whitespace-only changes.

ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
import org.mockito.Mockito;
6767
import org.opensearch.sql.ast.Node;
6868
import org.opensearch.sql.ast.dsl.AstDSL;
69+
import org.opensearch.sql.ast.expression.AllFields;
6970
import org.opensearch.sql.ast.expression.DataType;
7071
import org.opensearch.sql.ast.expression.Literal;
7172
import org.opensearch.sql.ast.expression.ParseMethod;
7273
import org.opensearch.sql.ast.expression.PatternMethod;
7374
import org.opensearch.sql.ast.expression.PatternMode;
7475
import org.opensearch.sql.ast.expression.SpanUnit;
7576
import org.opensearch.sql.ast.tree.AD;
77+
import org.opensearch.sql.ast.tree.Chart;
7678
import org.opensearch.sql.ast.tree.Kmeans;
7779
import org.opensearch.sql.ast.tree.ML;
7880
import org.opensearch.sql.ast.tree.RareTopN.CommandType;
@@ -1267,4 +1269,94 @@ public void testReplaceCommandWithMultiplePairs() {
12671269
// Test multiple pattern/replacement pairs
12681270
plan("source=t | replace 'a' WITH 'A', 'b' WITH 'B' IN field");
12691271
}
1272+
1273+
@Test
1274+
public void testChartCommandBasic() {
1275+
assertEqual(
1276+
"source=t | chart count() by age",
1277+
Chart.builder()
1278+
.child(relation("t"))
1279+
.columnSplit(alias("age", field("age")))
1280+
.aggregationFunctions(List.of(alias("count()", aggregate("count", AllFields.of()))))
1281+
.arguments(emptyList())
1282+
.build());
1283+
}
1284+
1285+
@Test
1286+
public void testChartCommandWithRowSplit() {
1287+
assertEqual(
1288+
"source=t | chart count() over status by age",
1289+
Chart.builder()
1290+
.child(relation("t"))
1291+
.rowSplit(alias("status", field("status")))
1292+
.columnSplit(alias("age", field("age")))
1293+
.aggregationFunctions(List.of(alias("count()", aggregate("count", AllFields.of()))))
1294+
.arguments(emptyList())
1295+
.build());
1296+
}
1297+
1298+
@Test
1299+
public void testChartCommandWithMultipleAggregations() {
1300+
assertEqual(
1301+
"source=t | chart avg(salary), max(age) by department",
1302+
Chart.builder()
1303+
.child(relation("t"))
1304+
.columnSplit(alias("department", field("department")))
1305+
.aggregationFunctions(
1306+
List.of(
1307+
alias("avg(salary)", aggregate("avg", field("salary"))),
1308+
alias("max(age)", aggregate("max", field("age")))))
1309+
.arguments(emptyList())
1310+
.build());
1311+
}
1312+
1313+
@Test
1314+
public void testChartCommandWithOptions() {
1315+
assertEqual(
1316+
"source=t | chart limit=10 useother=true count() by status",
1317+
Chart.builder()
1318+
.child(relation("t"))
1319+
.columnSplit(alias("status", field("status")))
1320+
.aggregationFunctions(List.of(alias("count()", aggregate("count", AllFields.of()))))
1321+
.arguments(
1322+
exprList(
1323+
argument("limit", intLiteral(10)),
1324+
argument("top", booleanLiteral(true)),
1325+
argument("useother", booleanLiteral(true))))
1326+
.build());
1327+
}
1328+
1329+
@Test
1330+
public void testChartCommandWithAllOptions() {
1331+
assertEqual(
1332+
"source=t | chart limit=5 useother=false otherstr='OTHER' usenull=true nullstr='NULL'"
1333+
+ " avg(balance) by gender",
1334+
Chart.builder()
1335+
.child(relation("t"))
1336+
.columnSplit(alias("gender", field("gender")))
1337+
.aggregationFunctions(
1338+
List.of(alias("avg(balance)", aggregate("avg", field("balance")))))
1339+
.arguments(
1340+
exprList(
1341+
argument("limit", intLiteral(5)),
1342+
argument("top", booleanLiteral(true)),
1343+
argument("useother", booleanLiteral(false)),
1344+
argument("otherstr", stringLiteral("OTHER")),
1345+
argument("usenull", booleanLiteral(true)),
1346+
argument("nullstr", stringLiteral("NULL"))))
1347+
.build());
1348+
}
1349+
1350+
@Test
1351+
public void testChartCommandWithBottomLimit() {
1352+
assertEqual(
1353+
"source=t | chart limit=bottom 3 count() by category",
1354+
Chart.builder()
1355+
.child(relation("t"))
1356+
.columnSplit(alias("category", field("category")))
1357+
.aggregationFunctions(List.of(alias("count()", aggregate("count", AllFields.of()))))
1358+
.arguments(
1359+
exprList(argument("limit", intLiteral(3)), argument("top", booleanLiteral(false))))
1360+
.build());
1361+
}
12701362
}

ppl/src/test/java/org/opensearch/sql/ppl/parser/AstExpressionBuilderTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.opensearch.sql.ast.dsl.AstDSL.allFields;
1515
import static org.opensearch.sql.ast.dsl.AstDSL.and;
1616
import static org.opensearch.sql.ast.dsl.AstDSL.argument;
17+
import static org.opensearch.sql.ast.dsl.AstDSL.bin;
1718
import static org.opensearch.sql.ast.dsl.AstDSL.booleanLiteral;
1819
import static org.opensearch.sql.ast.dsl.AstDSL.caseWhen;
1920
import static org.opensearch.sql.ast.dsl.AstDSL.cast;
@@ -1605,4 +1606,66 @@ public void testVisitSpanLiteral() {
16051606
.useOther(true)
16061607
.build());
16071608
}
1609+
1610+
@Test
1611+
public void testBinOptionWithSpan() {
1612+
assertEqual(
1613+
"source=t | bin age span=10",
1614+
bin(relation("t"), field("age"), argument("span", intLiteral(10))));
1615+
}
1616+
1617+
@Test
1618+
public void testBinOptionWithBins() {
1619+
assertEqual(
1620+
"source=t | bin age bins=5",
1621+
bin(relation("t"), field("age"), argument("bins", intLiteral(5))));
1622+
}
1623+
1624+
@Test
1625+
public void testBinOptionWithMinspan() {
1626+
assertEqual(
1627+
"source=t | bin age minspan=100",
1628+
bin(relation("t"), field("age"), argument("minspan", intLiteral(100))));
1629+
}
1630+
1631+
@Test
1632+
public void testBinOptionWithAligntimeEarliest() {
1633+
assertEqual(
1634+
"source=t | bin age span=10 aligntime=earliest",
1635+
bin(
1636+
relation("t"),
1637+
field("age"),
1638+
argument("span", intLiteral(10)),
1639+
argument("aligntime", stringLiteral("earliest"))));
1640+
}
1641+
1642+
@Test
1643+
public void testBinOptionWithAligntimeLiteralValue() {
1644+
assertEqual(
1645+
"source=t | bin age span=10 aligntime=1000",
1646+
bin(
1647+
relation("t"),
1648+
field("age"),
1649+
argument("span", intLiteral(10)),
1650+
argument("aligntime", intLiteral(1000))));
1651+
}
1652+
1653+
@Test
1654+
public void testBinOptionWithStartAndEnd() {
1655+
assertEqual(
1656+
"source=t | bin age bins=10 start=0 end=100",
1657+
bin(
1658+
relation("t"),
1659+
field("age"),
1660+
argument("bins", intLiteral(10)),
1661+
argument("start", intLiteral(0)),
1662+
argument("end", intLiteral(100))));
1663+
}
1664+
1665+
@Test
1666+
public void testBinOptionWithTimeSpan() {
1667+
assertEqual(
1668+
"source=t | bin timestamp span=1h",
1669+
bin(relation("t"), field("timestamp"), argument("span", stringLiteral("1h"))));
1670+
}
16081671
}

ppl/src/test/java/org/opensearch/sql/ppl/utils/ArgumentFactoryTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import static org.opensearch.sql.ast.dsl.AstDSL.sort;
2121
import static org.opensearch.sql.ast.dsl.AstDSL.stringLiteral;
2222

23+
import com.google.common.collect.ImmutableList;
2324
import org.junit.Test;
25+
import org.opensearch.sql.ast.expression.AllFields;
2426
import org.opensearch.sql.ast.expression.Argument;
27+
import org.opensearch.sql.ast.tree.Chart;
2528
import org.opensearch.sql.ppl.parser.AstBuilderTest;
2629

2730
public class ArgumentFactoryTest extends AstBuilderTest {
@@ -100,6 +103,41 @@ public void testSortFieldArgument() {
100103
argument("type", stringLiteral("auto"))))));
101104
}
102105

106+
@Test
107+
public void testChartCommandArguments() {
108+
assertEqual(
109+
"source=t | chart limit=5 useother=true otherstr='OTHER_VAL' usenull=false"
110+
+ " nullstr='NULL_VAL' count() by age",
111+
Chart.builder()
112+
.child(relation("t"))
113+
.columnSplit(alias("age", field("age")))
114+
.aggregationFunctions(
115+
ImmutableList.of(alias("count()", aggregate("count", AllFields.of()))))
116+
.arguments(
117+
exprList(
118+
argument("limit", intLiteral(5)),
119+
argument("top", booleanLiteral(true)),
120+
argument("useother", booleanLiteral(true)),
121+
argument("otherstr", stringLiteral("OTHER_VAL")),
122+
argument("usenull", booleanLiteral(false)),
123+
argument("nullstr", stringLiteral("NULL_VAL"))))
124+
.build());
125+
}
126+
127+
@Test
128+
public void testChartCommandBottomArguments() {
129+
assertEqual(
130+
"source=t | chart limit=bottom 3 count() by status",
131+
Chart.builder()
132+
.child(relation("t"))
133+
.columnSplit(alias("status", field("status")))
134+
.aggregationFunctions(
135+
ImmutableList.of(alias("count()", aggregate("count", AllFields.of()))))
136+
.arguments(
137+
exprList(argument("limit", intLiteral(3)), argument("top", booleanLiteral(false))))
138+
.build());
139+
}
140+
103141
@Test
104142
public void testNoArgConstructorForArgumentFactoryShouldPass() {
105143
new ArgumentFactory();

0 commit comments

Comments
 (0)