-
Notifications
You must be signed in to change notification settings - Fork 90
/
ItAggregatesTest.java
465 lines (381 loc) · 18.1 KB
/
ItAggregatesTest.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.sql.engine;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;
import org.apache.ignite.internal.sql.engine.util.QueryChecker;
import org.apache.ignite.internal.testframework.WithSystemProperty;
import org.apache.ignite.lang.IgniteException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Group of tests to verify aggregation functions.
*/
public class ItAggregatesTest extends AbstractBasicIntegrationTest {
private static String[] disabledRules = {"MapReduceHashAggregateConverterRule", "MapReduceSortAggregateConverterRule",
"ColocatedHashAggregateConverterRule", "ColocatedSortAggregateConverterRule"};
private static final int ROWS = 103;
/**
* Before all.
*/
@BeforeAll
static void initTestData() {
createAndPopulateTable();
sql("CREATE TABLE test (id INT PRIMARY KEY, grp0 INT, grp1 INT, val0 INT, val1 INT) WITH replicas=2,partitions=10");
sql("CREATE TABLE test_one_col_idx (pk INT PRIMARY KEY, col0 INT)");
// TODO: https://issues.apache.org/jira/browse/IGNITE-17304 uncomment this
// sql("CREATE INDEX test_idx ON test(grp0, grp1)");
// sql("CREATE INDEX test_one_col_idx_idx ON test_one_col_idx(col0)");
for (int i = 0; i < ROWS; i++) {
sql("INSERT INTO test (id, grp0, grp1, val0, val1) VALUES (?, ?, ?, ?, ?)", i, i / 10, i / 100, 1, 2);
sql("INSERT INTO test_one_col_idx (pk, col0) VALUES (?, ?)", i, i);
}
}
@ParameterizedTest
@MethodSource("provideRules")
public void aggregateWithSumAndHaving(String[] rules) {
var res = sql(
appendDisabledRules("SELECT SUM(val0), SUM(val1), grp0 FROM TEST GROUP BY grp0 HAVING SUM(val1) > 10", rules));
assertEquals(ROWS / 10, res.size());
res.forEach(r -> {
long s0 = (Long) r.get(0);
long s1 = (Long) r.get(1);
assertEquals(s0 * 2, s1);
});
}
@ParameterizedTest
@MethodSource("provideRules")
public void correctCollationsOnAgg(String[] rules) {
var cursors = sql(
appendDisabledRules("SELECT PK FROM TEST_ONE_COL_IDX WHERE col0 IN (SELECT col0 FROM TEST_ONE_COL_IDX)", rules));
assertEquals(ROWS, cursors.size());
}
@ParameterizedTest
@MethodSource("provideRules")
public void countOfNonNumericField(String[] rules) {
assertQuery("select count(name) from person").disableRules(rules).returns(4L).check();
assertQuery("select count(*) from person").disableRules(rules).returns(5L).check();
assertQuery("select count(1) from person").disableRules(rules).returns(5L).check();
assertQuery("select count(null) from person").disableRules(rules).returns(0L).check();
assertQuery("select count(*) from person where salary < 0").disableRules(rules).returns(0L).check();
assertQuery("select count(*) from person where salary < 0 and salary > 0").disableRules(rules).returns(0L).check();
assertQuery("select count(case when name like 'R%' then 1 else null end) from person").disableRules(rules).returns(2L).check();
assertQuery("select count(case when name not like 'I%' then 1 else null end) from person").disableRules(rules).returns(2L).check();
assertQuery("select count(name) from person where salary > 10").disableRules(rules).returns(1L).check();
assertQuery("select count(*) from person where salary > 10").disableRules(rules).returns(2L).check();
assertQuery("select count(1) from person where salary > 10").disableRules(rules).returns(2L).check();
assertQuery("select count(*) from person where name is not null").disableRules(rules).returns(4L).check();
assertQuery("select count(name) filter (where salary > 10) from person").disableRules(rules).returns(1L).check();
assertQuery("select count(*) filter (where salary > 10) from person").disableRules(rules).returns(2L).check();
assertQuery("select count(1) filter (where salary > 10) from person").disableRules(rules).returns(2L).check();
assertQuery("select salary, count(name) from person group by salary order by salary")
.disableRules(rules)
.returns(10d, 3L)
.returns(15d, 1L)
.check();
// same query, but grouping by alias
assertQuery("select salary as sal, count(name) from person group by sal order by sal")
.disableRules(rules)
.returns(10d, 3L)
.returns(15d, 1L)
.check();
// same query, but grouping by ordinal
assertQuery("select salary, count(name) from person group by 1 order by 1")
.disableRules(rules)
.returns(10d, 3L)
.returns(15d, 1L)
.check();
assertQuery("select salary * salary / 5, count(name) from person group by (salary * salary / 5) order by (salary * salary / 5)")
.disableRules(rules)
.returns(20d, 3L)
.returns(45d, 1L)
.check();
// same query, but grouping by alias
assertQuery("select (salary * salary / 5) as sal, count(name) from person group by sal order by sal")
.disableRules(rules)
.returns(20d, 3L)
.returns(45d, 1L)
.check();
// same query, but grouping by ordinal
assertQuery("select salary * salary / 5, count(name) from person group by 1 order by 1")
.disableRules(rules)
.returns(20d, 3L)
.returns(45d, 1L)
.check();
assertQuery("select salary, count(*) from person group by salary order by salary")
.disableRules(rules)
.returns(10d, 3L)
.returns(15d, 2L)
.check();
assertQuery("select salary, count(1) from person group by salary order by salary")
.disableRules(rules)
.returns(10d, 3L)
.returns(15d, 2L)
.check();
assertQuery("select salary, count(1), sum(1) from person group by salary order by salary")
.disableRules(rules)
.returns(10d, 3L, 3L)
.returns(15d, 2L, 2L)
.check();
assertQuery("select salary, name, count(1), sum(salary) from person group by salary, name order by salary")
.disableRules(rules)
.returns(10d, "Igor", 1L, 10d)
.returns(10d, "Roma", 2L, 20d)
.returns(15d, "Ilya", 1L, 15d)
.returns(15d, null, 1L, 15d)
.check();
assertQuery("select salary, count(name) from person group by salary having salary < 10 order by salary")
.disableRules(rules)
.check();
assertQuery("select count(name), name from person group by name")
.disableRules(rules)
.returns(1L, "Igor")
.returns(1L, "Ilya")
.returns(2L, "Roma")
.returns(0L, null)
.check();
assertQuery("select avg(salary) from person")
.disableRules(rules)
.returns(12.0)
.check();
assertQuery("select name, salary from person where person.salary > (select avg(person.salary) from person)")
.disableRules(rules)
.returns(null, 15d)
.returns("Ilya", 15d)
.check();
assertQuery("select avg(salary) from (select avg(salary) as salary from person union all select salary from person)")
.disableRules(rules)
.returns(12d)
.check();
}
@ParameterizedTest
@MethodSource("provideRules")
public void testMultipleRowsFromSingleAggr(String[] rules) {
assertThrows(
IgniteException.class,
() -> assertQuery("SELECT (SELECT name FROM person)").disableRules(rules).check()
);
assertThrows(
IgniteException.class,
() -> assertQuery("SELECT t.id, (SELECT x FROM TABLE(system_range(1, 5))) FROM person t").disableRules(rules).check()
);
assertThrows(
IgniteException.class,
() -> assertQuery("SELECT t.id, (SELECT x FROM "
+ "TABLE(system_range(t.id, t.id + 1))) FROM person t").disableRules(rules).check()
);
assertQuery("SELECT t.id, (SELECT x FROM TABLE(system_range(t.id, t.id))) FROM person t").disableRules(rules).check();
}
@ParameterizedTest
@MethodSource("provideRules")
public void testAnyValAggr(String[] rules) {
var res = sql(appendDisabledRules("select any_value(name) from person", rules));
assertEquals(1, res.size());
Object val = res.get(0).get(0);
assertTrue("Igor".equals(val) || "Roma".equals(val) || "Ilya".equals(val), "Unexpected value: " + val);
// Test with grouping.
res = sql(appendDisabledRules("select any_value(name), salary from person group by salary order by salary", rules));
assertEquals(2, res.size());
val = res.get(0).get(0);
assertTrue("Igor".equals(val) || "Roma".equals(val), "Unexpected value: " + val);
val = res.get(1).get(0);
assertEquals("Ilya", val);
}
@ParameterizedTest
@MethodSource("provideRules")
public void testColocatedAggregate(String[] rules) {
try {
sql("CREATE TABLE t1(id INT, val0 VARCHAR, val1 VARCHAR, val2 VARCHAR, PRIMARY KEY(id, val1)) "
+ "COLOCATE BY (val1)");
sql("CREATE TABLE t2(id INT, val0 VARCHAR, val1 VARCHAR, val2 VARCHAR, PRIMARY KEY(id, val1)) "
+ "COLOCATE BY (val1)");
for (int i = 0; i < 100; i++) {
sql("INSERT INTO t1 VALUES (?, ?, ?, ?)", i, "val" + i, "val" + i % 2, "val" + i);
}
sql("INSERT INTO t2 VALUES (0, 'val0', 'val0', 'val0'), (1, 'val1', 'val1', 'val1')");
String sql = "SELECT val1, count(val2) FROM t1 GROUP BY val1";
assertQuery(sql)
.disableRules(rules)
.returns("val0", 50L)
.returns("val1", 50L)
.check();
sql = "SELECT t2.val1, agg.cnt "
+ "FROM t2 JOIN (SELECT val1, COUNT(val2) AS cnt FROM t1 GROUP BY val1) AS agg ON t2.val1 = agg.val1";
assertQuery(sql)
.disableRules(rules)
.returns("val0", 50L)
.returns("val1", 50L)
.check();
} finally {
sql("DROP TABLE IF EXISTS t1");
sql("DROP TABLE IF EXISTS t2");
}
}
@Test
public void distinctAggregateWithoutAggregateFunction() {
var sql = "select distinct name from person";
assertQuery(sql)
.matches(QueryChecker.matches(".*Colocated.*Aggregate.*Exchange.*"))
.returns("Igor")
.returns("Ilya")
.returns("Roma")
.returns(null)
.check();
assertQuery(sql, AggregateType.HASH)
.matches(QueryChecker.matches(".*ReduceHashAggregate.*Exchange.*MapHashAggregate.*"))
.returns("Igor")
.returns("Ilya")
.returns("Roma")
.returns(null)
.check();
assertQuery(sql, AggregateType.SORT)
.matches(QueryChecker.matches(".*ReduceSortAggregate.*Exchange.*MapSortAggregate.*"))
.returns("Igor")
.returns("Ilya")
.returns("Roma")
.returns(null)
.check();
}
@ParameterizedTest
@MethodSource("provideRules")
@WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true")
public void testDifferentAgg(String[] rules) {
try {
sql("CREATE TABLE testMe (a INTEGER, b INTEGER, s VARCHAR);");
sql("INSERT INTO testMe VALUES (11, 1, 'hello'), (12, 2, 'world'), (11, 3, NULL)");
sql("INSERT INTO testMe VALUES (11, 3, 'hello'), (12, 2, 'world'), (10, 5, 'ahello'), (13, 6, 'world')");
assertQuery("SELECT DISTINCT(a) as a FROM testMe ORDER BY a")
.disableRules(rules)
.returns(10)
.returns(11)
.returns(12)
.returns(13)
.check();
assertQuery("SELECT COUNT(*) FROM testMe")
.disableRules(rules)
.returns(7L)
.check();
// Such kind of queries can`t be processed with
if (Arrays.stream(rules).anyMatch(r -> r.contains("MapReduceSortAggregateConverterRule"))) {
assertQuery("SELECT COUNT(a), COUNT(DISTINCT(b)) FROM testMe")
.disableRules(rules)
.returns(7L, 5L)
.check();
}
assertQuery("SELECT COUNT(a) as a, s FROM testMe GROUP BY s ORDER BY a, s")
.disableRules(rules)
.returns(1L, "ahello")
.returns(1L, null)
.returns(2L, "hello")
.returns(3L, "world")
.check();
assertQuery("SELECT COUNT(a) as a, AVG(a) as b, MIN(a), MIN(b), s FROM testMe GROUP BY s ORDER BY a, b")
.disableRules(rules)
.returns(1L, 10, 10, 5, "ahello")
.returns(1L, 11, 11, 3, null)
.returns(2L, 11, 11, 1, "hello")
.returns(3L, 12, 12, 2, "world")
.check();
assertQuery("SELECT COUNT(a) as a, AVG(a) as bb, MIN(a), MIN(b), s FROM testMe GROUP BY s, b ORDER BY a, s")
.disableRules(rules)
.returns(1L, 10, 10, 5, "ahello")
.returns(1L, 11, 11, 1, "hello")
.returns(1L, 11, 11, 3, "hello")
.returns(1L, 13, 13, 6, "world")
.returns(1L, 11, 11, 3, null)
.returns(2L, 12, 12, 2, "world")
.check();
assertQuery("SELECT COUNT(a) FROM testMe")
.disableRules(rules)
.returns(7L)
.check();
assertQuery("SELECT COUNT(DISTINCT(a)) FROM testMe")
.disableRules(rules)
.returns(4L)
.check();
assertQuery("SELECT COUNT(a), COUNT(s), COUNT(*) FROM testMe")
.disableRules(rules)
.returns(7L, 6L, 7L)
.check();
assertQuery("SELECT AVG(a) FROM testMe")
.disableRules(rules)
.returns(11)
.check();
assertQuery("SELECT MIN(a) FROM testMe")
.disableRules(rules)
.returns(10)
.check();
assertQuery("SELECT COUNT(a), COUNT(DISTINCT(a)) FROM testMe")
.disableRules(rules)
.returns(7L, 4L)
.check();
assertQuery("SELECT COUNT(a), COUNT(DISTINCT a), SUM(a), SUM(DISTINCT a) FROM testMe")
.disableRules(rules)
.returns(7L, 4L, 80L, 46L)
.check();
} finally {
sql("DROP TABLE IF EXISTS testMe");
}
}
@ParameterizedTest
@MethodSource("provideRules")
@WithSystemProperty(key = "IMPLICIT_PK_ENABLED", value = "true")
public void checkEmptyTable(String[] rules) {
sql("CREATE TABLE t (a INTEGER, b INTEGER)");
try {
assertQuery("SELECT min(b) FROM t GROUP BY a")
.disableRules(rules)
.returnNothing().check();
} finally {
sql("DROP TABLE t");
}
}
static String[][] makePermutations(String[] rules) {
String[][] out = new String[rules.length][rules.length - 1];
for (int i = 0; i < disabledRules.length; ++i) {
int pos = 0;
for (int ruleIdx = 0; ruleIdx < disabledRules.length; ++ruleIdx) {
if (ruleIdx == i) {
continue;
}
out[i][pos++] = disabledRules[ruleIdx];
}
}
return out;
}
private static Stream<Arguments> provideRules() {
return Arrays.stream(makePermutations(disabledRules)).map(k -> Arguments.of((Object) k));
}
private String appendDisabledRules(String sql, String[] rules) {
sql = sql.toLowerCase(Locale.ENGLISH);
int pos = sql.indexOf("select");
assert pos >= 0;
String newSql = sql.substring(0, pos + "select".length() + 1);
newSql += " /*+ DISABLE_RULE( '";
newSql += String.join("' ,'", rules);
newSql += "') */ ";
newSql += sql.substring(pos + "select".length() + 1);
return newSql;
}
}