Skip to content

Commit a1f584f

Browse files
Added some integration tests for fillnull
Signed-off-by: Norman Jordan <norman.jordan@improving.com>
1 parent bbe955a commit a1f584f

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

integ-test/src/test/java/org/opensearch/sql/ppl/ExplainIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public void testLimitPushDownExplain() throws Exception {
8989
+ "| fields ageMinus"));
9090
}
9191

92+
@Test
93+
public void testFillNullPushDownExplain() throws Exception {
94+
String expected = loadFromFile("expectedOutput/ppl/explain_fillnull_push.json");
95+
96+
assertJsonEquals(
97+
expected,
98+
explainQueryToString(
99+
"source=opensearch-sql_test_index_account"
100+
+ "| fields age, balance "
101+
+ "| fillnull with -1 in age,balance"));
102+
}
103+
92104
String loadFromFile(String filename) throws Exception {
93105
URI uri = Resources.getResource(filename).toURI();
94106
return new String(Files.readAllBytes(Paths.get(uri)));
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.sql.ppl;
7+
8+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_CALCS;
9+
import static org.opensearch.sql.util.MatcherUtils.rows;
10+
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
11+
12+
import java.io.IOException;
13+
import org.json.JSONObject;
14+
import org.junit.jupiter.api.Test;
15+
16+
public class FillNullCommandIT extends PPLIntegTestCase {
17+
@Override
18+
public void init() throws IOException {
19+
loadIndex(Index.CALCS);
20+
}
21+
22+
@Test
23+
public void testFillNullSameValueOneField() throws IOException {
24+
JSONObject result =
25+
executeQuery(
26+
String.format(
27+
"source=%s | fields str2, num0 | fillnull with -1 in num0", TEST_INDEX_CALCS));
28+
verifyDataRows(
29+
result,
30+
rows("one", 12.3),
31+
rows("two", -12.3),
32+
rows("three", 15.7),
33+
rows(null, -15.7),
34+
rows("five", 3.5),
35+
rows("six", -3.5),
36+
rows(null, 0),
37+
rows("eight", -1),
38+
rows("nine", 10),
39+
rows("ten", -1),
40+
rows("eleven", -1),
41+
rows("twelve", -1),
42+
rows(null, -1),
43+
rows("fourteen", -1),
44+
rows("fifteen", -1),
45+
rows("sixteen", -1),
46+
rows(null, -1));
47+
}
48+
49+
@Test
50+
public void testFillNullSameValueTwoFields() throws IOException {
51+
JSONObject result =
52+
executeQuery(
53+
String.format(
54+
"source=%s | fields num0, num2 | fillnull with -1 in num0,num2", TEST_INDEX_CALCS));
55+
verifyDataRows(
56+
result,
57+
rows(12.3, 17.86),
58+
rows(-12.3, 16.73),
59+
rows(15.7, -1),
60+
rows(-15.7, 8.51),
61+
rows(3.5, 6.46),
62+
rows(-3.5, 8.98),
63+
rows(0, 11.69),
64+
rows(-1, 17.25),
65+
rows(10, -1),
66+
rows(-1, 11.5),
67+
rows(-1, 6.8),
68+
rows(-1, 3.79),
69+
rows(-1, -1),
70+
rows(-1, 13.04),
71+
rows(-1, -1),
72+
rows(-1, 10.98),
73+
rows(-1, 7.87));
74+
}
75+
76+
@Test
77+
public void testFillNullVariousValuesOneField() throws IOException {
78+
JSONObject result =
79+
executeQuery(
80+
String.format(
81+
"source=%s | fields str2, num0 | fillnull using num0 = -1", TEST_INDEX_CALCS));
82+
verifyDataRows(
83+
result,
84+
rows("one", 12.3),
85+
rows("two", -12.3),
86+
rows("three", 15.7),
87+
rows(null, -15.7),
88+
rows("five", 3.5),
89+
rows("six", -3.5),
90+
rows(null, 0),
91+
rows("eight", -1),
92+
rows("nine", 10),
93+
rows("ten", -1),
94+
rows("eleven", -1),
95+
rows("twelve", -1),
96+
rows(null, -1),
97+
rows("fourteen", -1),
98+
rows("fifteen", -1),
99+
rows("sixteen", -1),
100+
rows(null, -1));
101+
}
102+
103+
@Test
104+
public void testFillNullVariousValuesTwoFields() throws IOException {
105+
JSONObject result =
106+
executeQuery(
107+
String.format(
108+
"source=%s | fields num0, num2 | fillnull using num0 = -1, num2 = -2",
109+
TEST_INDEX_CALCS));
110+
verifyDataRows(
111+
result,
112+
rows(12.3, 17.86),
113+
rows(-12.3, 16.73),
114+
rows(15.7, -2),
115+
rows(-15.7, 8.51),
116+
rows(3.5, 6.46),
117+
rows(-3.5, 8.98),
118+
rows(0, 11.69),
119+
rows(-1, 17.25),
120+
rows(10, -2),
121+
rows(-1, 11.5),
122+
rows(-1, 6.8),
123+
rows(-1, 3.79),
124+
rows(-1, -2),
125+
rows(-1, 13.04),
126+
rows(-1, -2),
127+
rows(-1, 10.98),
128+
rows(-1, 7.87));
129+
}
130+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"root": {
3+
"name": "ProjectOperator",
4+
"description": {
5+
"fields": "[age, balance]"
6+
},
7+
"children": [
8+
{
9+
"name":"EvalOperator",
10+
"description": {
11+
"expressions": {
12+
"balance": "ifnull(balance, -1)",
13+
"age":"ifnull(age, -1)"
14+
}
15+
},
16+
"children": [
17+
{
18+
"name": "ProjectOperator",
19+
"description": {
20+
"fields": "[age, balance]"
21+
},
22+
"children": [
23+
{
24+
"name": "OpenSearchIndexScan",
25+
"description": {
26+
"request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account, sourceBuilder={\"from\":0,\"size\":10000,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"balance\",\"age\"],\"excludes\":[]}}, needClean=true, searchDone=false, pitId=null, cursorKeepAlive=null, searchAfter=null, searchResponse=null)"
27+
},
28+
"children": []
29+
}
30+
]
31+
}
32+
]
33+
}
34+
]
35+
}
36+
}

ppl/src/test/java/org/opensearch/sql/ppl/antlr/PPLSyntaxParserTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,16 @@ public void testCanParseTimestampdiffFunction() {
417417
new PPLSyntaxParser()
418418
.parse("SOURCE=test | eval k = TIMESTAMPDIFF(WEEK,'2003-01-02','2003-01-02')"));
419419
}
420+
421+
@Test
422+
public void testCanParseFillNullSameValue() {
423+
assertNotNull(new PPLSyntaxParser().parse("SOURCE=test | fillnull with 0 in a"));
424+
assertNotNull(new PPLSyntaxParser().parse("SOURCE=test | fillnull with 0 in a,b"));
425+
}
426+
427+
@Test
428+
public void testCanParseFillNullVariousValues() {
429+
assertNotNull(new PPLSyntaxParser().parse("SOURCE=test | fillnull using a = 0"));
430+
assertNotNull(new PPLSyntaxParser().parse("SOURCE=test | fillnull using a = 0, b = 1"));
431+
}
420432
}

0 commit comments

Comments
 (0)