forked from xinyual/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhereCommandIT.java
More file actions
258 lines (232 loc) · 8.56 KB
/
Copy pathWhereCommandIT.java
File metadata and controls
258 lines (232 loc) · 8.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.ppl;
import static org.hamcrest.CoreMatchers.containsString;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_TIME;
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 java.util.stream.Collectors;
import org.hamcrest.MatcherAssert;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.opensearch.sql.data.type.ExprCoreType;
public class WhereCommandIT extends PPLIntegTestCase {
@Override
public void init() throws Exception {
super.init();
loadIndex(Index.ACCOUNT);
loadIndex(Index.BANK_WITH_NULL_VALUES);
loadIndex(Index.GAME_OF_THRONES);
loadIndex(Index.DATETIME);
}
@Test
public void testWhereWithLogicalExpr() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | fields firstname | where firstname='Amber' | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"));
}
@Test
public void testWhereWithMultiLogicalExpr() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s "
+ "| where firstname='Amber' lastname='Duke' age=32 "
+ "| fields firstname, lastname, age",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber", "Duke", 32));
}
@Test
public void testMultipleWhereCommands() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s "
+ "| where firstname='Amber' "
+ "| fields lastname, age"
+ "| where lastname='Duke' "
+ "| fields age "
+ "| where age=32 "
+ "| fields age",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows(32));
}
@Test
public void testWhereEquivalentSortCommand() throws IOException {
assertEquals(
executeQueryToString(
String.format("source=%s | where firstname='Amber'", TEST_INDEX_ACCOUNT)),
executeQueryToString(String.format("source=%s firstname='Amber'", TEST_INDEX_ACCOUNT)));
}
@Test
public void testLikeFunction() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | fields firstname | where like(firstname, 'Ambe_') | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"));
}
@Test
public void testIsNullFunction() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | where isnull(age) | fields firstname",
TEST_INDEX_BANK_WITH_NULL_VALUES));
verifyDataRows(result, rows("Virginia"));
}
@Test
public void testIsNotNullFunction() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | where isnotnull(age) and like(firstname, 'Ambe_%%') | fields"
+ " firstname",
TEST_INDEX_BANK_WITH_NULL_VALUES));
verifyDataRows(result, rows("Amber JOHnny"));
}
@Test
public void testWhereWithMetadataFields() throws IOException {
JSONObject result =
executeQuery(
String.format("source=%s | where _id='1' | fields firstname", TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"));
}
@Test
public void testWhereWithMetadataFields2() throws IOException {
JSONObject result =
executeQuery(String.format("source=%s | where _id='1'", TEST_INDEX_ACCOUNT));
verifyDataRows(
result,
rows(
1,
"Amber",
"880 Holmes Lane",
39225,
"M",
"Brogan",
"Pyrami",
"IL",
32,
"amberduke@pyrami.com",
"Duke"));
}
@Test
public void testWhereWithIn() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | where firstname in ('Amber') | fields firstname", TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"));
result =
executeQuery(
String.format(
"source=%s | where firstname in ('Amber', 'Dale') | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"), rows("Dale"));
result =
executeQuery(
String.format(
"source=%s | where balance in (4180, 5686.0) | fields balance",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows(4180), rows(5686));
}
@Test
public void testWhereWithNotIn() throws IOException {
JSONObject result =
executeQuery(
String.format(
"source=%s | where account_number < 4 | where firstname not in ('Amber', 'Levine')"
+ " | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Roberta"), rows("Bradshaw"));
result =
executeQuery(
String.format(
"source=%s | where account_number < 4 | where not firstname in ('Amber', 'Levine')"
+ " | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Roberta"), rows("Bradshaw"));
result =
executeQuery(
String.format(
"source=%s | where not firstname not in ('Amber', 'Dale') | fields firstname",
TEST_INDEX_ACCOUNT));
verifyDataRows(result, rows("Amber"), rows("Dale"));
}
@Test
public void testInWithIncompatibleType() {
Exception e =
assertThrows(
Exception.class,
() -> {
executeQuery(
String.format(
"source=%s | where balance in (4180, 5686, '6077') | fields firstname",
TEST_INDEX_ACCOUNT));
});
MatcherAssert.assertThat(e.getMessage(), containsString(getIncompatibleTypeErrMsg()));
}
protected String getIncompatibleTypeErrMsg() {
return String.format(
"function expected %s, but got %s",
ExprCoreType.coreTypes().stream()
.map(type -> String.format("[%s,%s]", type.typeName(), type.typeName()))
.collect(Collectors.joining(",", "{", "}")),
"[LONG,STRING]");
}
@Test
public void testFilterScriptPushDown() throws IOException {
JSONObject actual =
executeQuery(
String.format(
"source=%s | where firstname ='Amber' and age - 2.0 = 30 | fields firstname, age",
TEST_INDEX_ACCOUNT));
verifySchema(actual, schema("firstname", "string"), schema("age", "bigint"));
verifyDataRows(actual, rows("Amber", 32));
}
@Test
public void testFilterScriptPushDownWithCalciteStdFunction() throws IOException {
JSONObject actual =
executeQuery(
String.format(
"source=%s | where length(firstname) = 5 and abs(age) = 32 and balance = 39225 |"
+ " fields firstname, age",
TEST_INDEX_ACCOUNT));
verifySchema(actual, schema("firstname", "string"), schema("age", "bigint"));
verifyDataRows(actual, rows("Amber", 32));
}
@Test
public void testFilterScriptPushDownWithPPLBuiltInFunction() throws IOException {
JSONObject actual =
executeQuery(
String.format("source=%s | where month(login_time) = 1", TEST_INDEX_DATE_TIME));
verifySchema(actual, schema("birthday", "timestamp"), schema("login_time", "timestamp"));
verifyDataRows(
actual,
rows(null, "2015-01-01 00:00:00"),
rows(null, "2015-01-01 12:10:30"),
rows(null, "1970-01-19 08:31:22.955"));
}
@Test
public void testFilterScriptPushDownWithCalciteStdLibraryFunction() throws IOException {
JSONObject actual =
executeQuery(
String.format(
"source=%s | where left(firstname, 3) = 'Ama' | fields firstname",
TEST_INDEX_ACCOUNT));
verifySchema(actual, schema("firstname", "string"));
verifyDataRows(actual, rows("Amalia"), rows("Amanda"));
}
}