forked from xinyual/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplainIT.java
More file actions
465 lines (419 loc) · 16.8 KB
/
Copy pathExplainIT.java
File metadata and controls
465 lines (419 loc) · 16.8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.ppl;
import static org.hamcrest.Matchers.containsString;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
import static org.opensearch.sql.util.MatcherUtils.assertJsonEqualsIgnoreId;
import java.io.IOException;
import org.junit.Ignore;
import org.junit.jupiter.api.Test;
import org.opensearch.client.ResponseException;
import org.opensearch.sql.legacy.TestUtils;
public class ExplainIT extends PPLIntegTestCase {
@Override
public void init() throws Exception {
super.init();
loadIndex(Index.ACCOUNT);
loadIndex(Index.BANK);
loadIndex(Index.DATE_FORMATS);
}
@Test
public void testExplain() throws IOException {
String expected = loadExpectedPlan("explain_output.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where age > 30 "
+ "| stats avg(age) AS avg_age by state, city "
+ "| sort state "
+ "| fields - city "
+ "| eval age2 = avg_age + 2 "
+ "| dedup age2 "
+ "| fields age2"));
}
@Test
public void testFilterPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_filter_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where age > 30 "
+ "| where age < 40 "
+ "| where balance > 10000 "
+ "| fields age"));
}
@Test
public void testFilterByCompareStringTimestampPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_filter_push_compare_timestamp_string.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_bank"
+ "| where birthdate > '2016-12-08 00:00:00.000000000' "
+ "| where birthdate < '2018-11-09 00:00:00.000000000' "));
}
@Test
public void testFilterByCompareStringDatePushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_filter_push_compare_date_string.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_date_formats | fields yyyy-MM-dd"
+ "| where yyyy-MM-dd > '2016-12-08 00:00:00.123456789' "
+ "| where yyyy-MM-dd < '2018-11-09 00:00:00.000000000' "));
}
@Test
public void testFilterByCompareStringTimePushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_filter_push_compare_time_string.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_date_formats | fields custom_time"
+ "| where custom_time > '2016-12-08 12:00:00.123456789' "
+ "| where custom_time < '2018-11-09 19:00:00.123456789' "));
}
@Test
public void testFilterAndAggPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_filter_agg_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where age > 30 "
+ "| stats avg(age) AS avg_age by state, city"));
}
@Test
public void testSortPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_sort_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| sort age "
+ "| where age > 30"
+ "| fields age"));
}
@Test
public void testSortWithAggregationExplain() throws IOException {
// Sorts whose by fields are aggregators should not be pushed down
String expected = loadExpectedPlan("explain_sort_agg_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| stats avg(age) AS avg_age by state, city "
+ "| sort avg_age"));
// sorts whose by fields are not aggregators can be pushed down.
// This test is covered in testExplain
}
@Test
public void testMultiSortPushDownExplain() throws IOException {
// TODO: Fix the expected output in expectedOutput/ppl/explain_multi_sort_push.json (v2)
// balance and gender should take precedence over account_number and firstname
String expected = loadExpectedPlan("explain_multi_sort_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account "
+ "| sort account_number, firstname, address, balance "
+ "| sort - balance, - gender, address "
+ "| fields account_number, firstname, address, balance, gender"));
}
@Test
public void testSortThenAggregatePushDownExplain() throws IOException {
// TODO: Remove pushed-down sort in DSL in expectedOutput/ppl/explain_sort_then_agg_push.json
// existing collations should be eliminated when pushing down aggregations (v2)
String expected = loadExpectedPlan("explain_sort_then_agg_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| sort balance, age "
+ "| stats avg(balance) by state"));
}
@Test
public void testSortWithRenameExplain() throws IOException {
String expected = loadExpectedPlan("explain_sort_rename_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account "
+ "| rename firstname as name "
+ "| eval alias = name "
+ "| sort alias "
+ "| fields alias"));
}
/**
* Pushdown SORT and LIMIT Sort should be pushed down since DSL process sort before limit when
* they coexist
*/
@Test
public void testSortThenLimitExplain() throws IOException {
String expected = loadExpectedPlan("explain_sort_then_limit_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| sort age "
+ "| head 5 "
+ "| fields age"));
}
/**
* Push down LIMIT only Sort should NOT be pushed down since DSL process limit before sort when
* they coexist
*/
@Test
public void testLimitThenSortExplain() throws IOException {
// TODO: Fix the expected output in expectedOutput/ppl/explain_limit_then_sort_push.json (v2)
// limit-then-sort should not be pushed down.
String expected = loadExpectedPlan("explain_limit_then_sort_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 5 "
+ "| sort age "
+ "| fields age"));
}
@Test
public void testLimitPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_limit_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| eval ageMinus = age - 30 "
+ "| head 5 "
+ "| fields ageMinus"));
}
@Test
public void testLimitWithFilterPushdownExplain() throws IOException {
String expectedFilterThenLimit = loadExpectedPlan("explain_filter_then_limit_push.json");
assertJsonEqualsIgnoreId(
expectedFilterThenLimit,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where age > 30 "
+ "| head 5 "
+ "| fields age"));
// The filter in limit-then-filter queries should not be pushed since the current DSL will
// execute it as filter-then-limit
String expectedLimitThenFilter = loadExpectedPlan("explain_limit_then_filter_push.json");
assertJsonEqualsIgnoreId(
expectedLimitThenFilter,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 5 "
+ "| where age > 30 "
+ "| fields age"));
}
@Test
public void testMultipleLimitExplain() throws IOException {
String expected5Then10 = loadExpectedPlan("explain_limit_5_10_push.json");
assertJsonEqualsIgnoreId(
expected5Then10,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 5 "
+ "| head 10 "
+ "| fields age"));
String expected10Then5 = loadExpectedPlan("explain_limit_10_5_push.json");
assertJsonEqualsIgnoreId(
expected10Then5,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 10 "
+ "| head 5 "
+ "| fields age"));
String expected10from1then10from2 = loadExpectedPlan("explain_limit_10from1_10from2_push.json");
assertJsonEqualsIgnoreId(
expected10from1then10from2,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 10 from 1 "
+ "| head 10 from 2 "
+ "| fields age"));
// The second limit should not be pushed down for limit-filter-limit queries
String expected10ThenFilterThen5 = loadExpectedPlan("explain_limit_10_filter_5_push.json");
assertJsonEqualsIgnoreId(
expected10ThenFilterThen5,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 10 "
+ "| where age > 30 "
+ "| head 5 "
+ "| fields age"));
}
@Test
public void testLimitWithMultipleOffsetPushdownExplain() throws IOException {
String expected = loadExpectedPlan("explain_limit_offsets_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 10 from 1 "
+ "| head 5 from 2 "
+ "| fields age"));
}
@Test
public void testFillNullPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_fillnull_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ " | fillnull with -1 in age,balance | fields age, balance"));
}
@Test
public void testTrendlinePushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_trendline_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 5 "
+ "| trendline sma(2, age) as ageTrend "
+ "| fields ageTrend"));
}
@Test
public void testTrendlineWithSortPushDownExplain() throws IOException {
String expected = loadExpectedPlan("explain_trendline_sort_push.json");
// Sort will not be pushed down because there's a head before it.
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| head 5 "
+ "| trendline sort age sma(2, age) as ageTrend "
+ "| fields ageTrend"));
}
@Test
public void testExplainModeUnsupportedInV2() throws IOException {
try {
executeQueryToString(
"explain cost source=opensearch-sql_test_index_account | where age = 20 | fields name,"
+ " city");
} catch (ResponseException e) {
final String entity = TestUtils.getResponseBody(e.getResponse());
assertThat(entity, containsString("Explain mode COST is not supported in v2 engine"));
}
}
@Test
public void testPatternsSimplePatternMethodWithoutAggExplain() throws IOException {
// TODO: Correct calcite expected result once pushdown is supported
String expected = loadExpectedPlan("explain_patterns_simple_pattern.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString("source=opensearch-sql_test_index_account | patterns email"));
}
@Test
public void testPatternsSimplePatternMethodWithAggPushDownExplain() throws IOException {
// TODO: Correct calcite expected result once pushdown is supported
String expected = loadExpectedPlan("explain_patterns_simple_pattern_agg_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account | patterns email mode=aggregation"));
}
@Test
public void testPatternsBrainMethodWithAggPushDownExplain() throws IOException {
// TODO: Correct calcite expected result once pushdown is supported
String expected = loadExpectedPlan("explain_patterns_brain_agg_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| patterns email method=brain mode=aggregation"));
}
@Test
public void testStatsBySpan() throws IOException {
String expected = loadExpectedPlan("explain_stats_by_span.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
String.format("source=%s | stats count() by span(age,10)", TEST_INDEX_BANK)));
}
@Test
public void testStatsByTimeSpan() throws IOException {
String expected = loadExpectedPlan("explain_stats_by_timespan.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
String.format("source=%s | stats count() by span(birthdate,1m)", TEST_INDEX_BANK)));
expected = loadExpectedPlan("explain_stats_by_timespan2.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
String.format("source=%s | stats count() by span(birthdate,1M)", TEST_INDEX_BANK)));
}
@Test
public void testSingleFieldRelevanceQueryFunctionExplain() throws IOException {
// This test is only applicable if pushdown is enabled
if (!isPushdownEnabled()) {
return;
}
String expected =
isCalciteEnabled()
? loadFromFile("expectedOutput/calcite/explain_single_field_relevance_push.json")
: loadFromFile("expectedOutput/ppl/explain_single_field_relevance_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where match(email, '*@gmail.com', boost=1.0)"));
}
@Test
public void testMultiFieldsRelevanceQueryFunctionExplain() throws IOException {
// This test is only applicable if pushdown is enabled
if (!isPushdownEnabled()) {
return;
}
String expected =
isCalciteEnabled()
? loadFromFile("expectedOutput/calcite/explain_multi_fields_relevance_push.json")
: loadFromFile("expectedOutput/ppl/explain_multi_fields_relevance_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account"
+ "| where simple_query_string(['email', name 4.0], 'gmail',"
+ " default_operator='or', analyzer=english)"));
}
@Ignore("The serialized string is unstable because of function properties")
@Test
public void testFilterScriptPushDownExplain() throws Exception {
String expected = loadExpectedPlan("explain_filter_script_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account | where firstname ='Amber' and age - 2 = 30 |"
+ " fields firstname, age"));
}
@Ignore("The serialized string is unstable because of function properties")
@Test
public void testFilterFunctionScriptPushDownExplain() throws Exception {
String expected = loadExpectedPlan("explain_filter_function_script_push.json");
assertJsonEqualsIgnoreId(
expected,
explainQueryToString(
"source=opensearch-sql_test_index_account | where length(firstname) = 5 and abs(age) ="
+ " 32 and balance = 39225 | fields firstname, age"));
}
protected String loadExpectedPlan(String fileName) throws IOException {
String prefix;
if (isCalciteEnabled()) {
if (isPushdownEnabled()) {
prefix = "expectedOutput/calcite/";
} else {
prefix = "expectedOutput/calcite_no_pushdown/";
}
} else {
prefix = "expectedOutput/ppl/";
}
return loadFromFile(prefix + fileName);
}
}