File: select3.test.lua

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (402 lines) | stat: -rwxr-xr-x 9,789 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(38)

--!./tcltestrunner.lua
-- 2001 September 15
--
-- The author disclaims copyright to this source code.  In place of
-- a legal notice, here is a blessing:
--
--    May you do good and not evil.
--    May you find forgiveness for yourself and forgive others.
--    May you share freely, never taking more than you give.
--
-------------------------------------------------------------------------
-- This file implements regression tests for sql library.  The
-- focus of this file is testing aggregate functions and the
-- GROUP BY and HAVING clauses of SELECT statements.
--
-- $Id: select3.test,v 1.23 2008/01/16 18:20:42 danielk1977 Exp $
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
-- Build some test data
--
test:do_test("select3-1.0", function()
  test:execsql [[
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(n int primary key, log int);
    START TRANSACTION;
  ]]
  for i = 1, 32-1 do -- in X(0, "X!for", [=[["set i 1","$i<32","incr i"]]=]) do
     j = 0
     while bit.lshift(1, j) < i do
	-- for _ in X(0, "X!for", [=[["set j 0","(1<<$j)<$i","incr j"]]=]) do
	j = j + 1
     end
    test:execsql(string.format("INSERT INTO t1 VALUES(%s,%s)", i, j))
  end
  test:execsql [[
    COMMIT
  ]]
  return test:execsql "SELECT DISTINCT log FROM t1 ORDER BY log"
end, {
  -- <select3-1.0>
  0, 1, 2, 3, 4, 5
  -- </select3-1.0>
})

-- Basic aggregate functions.
--
test:do_execsql_test("select3-1.1", [[
  SELECT count(*) FROM t1
]], {
  -- <select3-1.1>
  31
  -- </select3-1.1>
})

test:do_execsql_test("select3-1.2", [[
  SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
  FROM t1
]], {
  -- <select3-1.2>
  1, 0, 31, 5, 496, 124, 16.0, 4.0
  -- </select3-1.2>
})

test:do_execsql_test("select3-1.3", [[
  SELECT max(n)/avg(n), max(log)/avg(log) FROM t1
]], {
  -- <select3-1.3>
  1.9375, 1.25
  -- </select3-1.3>
})

-- Try some basic GROUP BY clauses
--
test:do_execsql_test("select3-2.1", [[
  SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.1>
  0, 1, 1, 1, 2, 2, 3, 4, 4, 8, 5, 15
  -- </select3-2.1>
})

test:do_execsql_test("select3-2.2", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.2>
  0, 1, 1, 2, 2, 3, 3, 5, 4, 9, 5, 17
  -- </select3-2.2>
})

test:do_execsql_test("select3-2.3.1", [[
  SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.3.1>
  0, 1.0, 1, 2.0, 2, 3.5, 3, 6.5, 4, 12.5, 5, 24.0
  -- </select3-2.3.1>
})

test:do_execsql_test("select3-2.3.2", [[
  SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.3.2>
  0, 2.0, 1, 3.0, 2, 4.5, 3, 7.5, 4, 13.5, 5, 25.0
  -- </select3-2.3.2>
})

test:do_execsql_test("select3-2.4", [[
  SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.4>
  0, 0.0, 1, 0.0, 2, 0.5, 3, 1.5, 4, 3.5, 5, 7.0
  -- </select3-2.4>
})

test:do_execsql_test("select3-2.5", [[
  SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log
]], {
  -- <select3-2.5>
  1, 0.0, 3, 0.0, 5, 0.5, 7, 1.5, 9, 3.5, 11, 7.0
  -- </select3-2.5>
})

test:do_execsql_test("select3-2.6", [[
  SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
]], {
  -- <select3-2.6>
  1, 1, 3, 1, 5, 2, 7, 4, 9, 8, 11, 15
  -- </select3-2.6>
})

test:do_execsql_test("select3-2.7", [[
  SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y, x
]], {
  -- <select3-2.7>
  1, 1, 3, 1, 5, 2, 7, 4, 9, 8, 11, 15
  -- </select3-2.7>
})

test:do_execsql_test("select3-2.8", [[
  SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
]], {
  -- <select3-2.8>
  11, 15, 9, 8, 7, 4, 5, 2, 3, 1, 1, 1
  -- </select3-2.8>
})

-- MUST_WORK_TEST
--do_test select3-2.9 {
--  catchsql {
--    SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
--  }
--} {1 {GROUP BY terms must not be non-integer constants}}
test:do_catchsql_test("select3-2.10", [[
  SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
]], {
  -- <select3-2.10>
  1, "Error at GROUP BY in place 1: term out of range - should be between 1 and 2"
  -- </select3-2.10>
})

test:do_catchsql_test("select3-2.11", [[
  SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
]], {
  -- <select3-2.11>
  1, "Error at GROUP BY in place 1: term out of range - should be between 1 and 2"
  -- </select3-2.11>
})

test:do_catchsql_test("select3-2.12", [[
  SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
]], {
  -- <select3-2.12>
  0, {0, 1, 1, 1, 2, 2, 3, 4, 4, 8, 5, 15}
  -- </select3-2.12>
})

-- Cannot have an empty GROUP BY
test:do_catchsql_test("select3-2.13", [[
  SELECT log, count(*) FROM t1 GROUP BY ORDER BY log;
]], {
  -- <select3-2.13>
  1, [[At line 1 at or near position 41: keyword 'ORDER' is reserved. Please use double quotes if 'ORDER' is an identifier.]]
  -- </select3-2.13>
})

test:do_catchsql_test("select3-2.14", [[
  SELECT log, count(*) FROM t1 GROUP BY;
]], {
  -- <select3-2.14>
  1, [[Syntax error at line 1 near ';']]
  -- </select3-2.14>
})

-- Cannot have a HAVING without a GROUP BY
--
test:do_catchsql_test("select3-3.1", [[
  SELECT log, count(*) FROM t1 HAVING log>=4
]], {
  -- <select3-3.1>
  1, "Failed to execute SQL statement: HAVING argument must appear in the GROUP BY clause or be used in an aggregate function"
  -- </select3-3.1>
})

-- Toss in some HAVING clauses
--
test:do_execsql_test("select3-4.1", [[
  SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log
]], {
  -- <select3-4.1>
  4, 8, 5, 15
  -- </select3-4.1>
})

test:do_execsql_test("select3-4.2", [[
  SELECT log, count(*) FROM t1 
  GROUP BY log 
  HAVING count(*)>=4 
  ORDER BY log
]], {
  -- <select3-4.2>
  3, 4, 4, 8, 5, 15
  -- </select3-4.2>
})

test:do_execsql_test("select3-4.3", [[
  SELECT log, count(*) FROM t1 
  GROUP BY log 
  HAVING count(*)>=4 
  ORDER BY max(n)+0
]], {
  -- <select3-4.3>
  3, 4, 4, 8, 5, 15
  -- </select3-4.3>
})

test:do_execsql_test("select3-4.4", [[
  SELECT log AS x, count(*) AS y FROM t1 
  GROUP BY x
  HAVING y>=4 
  ORDER BY max(n)+0
]], {
  -- <select3-4.4>
  3, 4, 4, 8, 5, 15
  -- </select3-4.4>
})

test:do_execsql_test("select3-4.5", [[
  SELECT log AS x FROM t1 
  GROUP BY x
  HAVING count(*)>=4 
  ORDER BY max(n)+0
]], {
  -- <select3-4.5>
  3, 4, 5
  -- </select3-4.5>
})

test:do_execsql_test("select3-5.1", [[
  SELECT log, count(*), avg(n), max(n+log*2) FROM t1 
  GROUP BY log 
  ORDER BY max(n+log*2)+0, avg(n)+0
]], {
  -- <select3-5.1>
  0, 1, 1.0, 1, 1, 1, 2.0, 4, 2, 2, 3.5, 8, 3, 4, 6.5, 14, 4, 8, 12.5, 24, 5, 15, 24.0, 41
  -- </select3-5.1>
})

test:do_execsql_test("select3-5.2", [[
  SELECT log, count(*), avg(n), max(n+log*2) FROM t1 
  GROUP BY log 
  ORDER BY max(n+log*2)+0, GREATEST(log,avg(n))+0
]], {
  -- <select3-5.2>
  0, 1, 1.0, 1, 1, 1, 2.0, 4, 2, 2, 3.5, 8, 3, 4, 6.5, 14, 4, 8, 12.5, 24, 5, 15, 24.0, 41
  -- </select3-5.2>
})

-- Test sorting of GROUP BY results in the presence of an index
-- on the GROUP BY column.
--
test:do_execsql_test("select3-6.1", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
]], {
  -- <select3-6.1>
  0, 1, 1, 2, 2, 3, 3, 5, 4, 9, 5, 17
  -- </select3-6.1>
})

test:do_execsql_test("select3-6.2", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
]], {
  -- <select3-6.2>
  5, 17, 4, 9, 3, 5, 2, 3, 1, 2, 0, 1
  -- </select3-6.2>
})

test:do_execsql_test("select3-6.3", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
]], {
  -- <select3-6.3>
  0, 1, 1, 2, 2, 3, 3, 5, 4, 9, 5, 17
  -- </select3-6.3>
})

test:do_execsql_test("select3-6.4", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
]], {
  -- <select3-6.4>
  5, 17, 4, 9, 3, 5, 2, 3, 1, 2, 0, 1
  -- </select3-6.4>
})

test:do_execsql_test("select3-6.5", [[
  CREATE INDEX i1 ON t1(log);
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
]], {
  -- <select3-6.5>
  0, 1, 1, 2, 2, 3, 3, 5, 4, 9, 5, 17
  -- </select3-6.5>
})

test:do_execsql_test("select3-6.6", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
]], {
  -- <select3-6.6>
  5, 17, 4, 9, 3, 5, 2, 3, 1, 2, 0, 1
  -- </select3-6.6>
})

test:do_execsql_test("select3-6.7", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
]], {
  -- <select3-6.7>
  0, 1, 1, 2, 2, 3, 3, 5, 4, 9, 5, 17
  -- </select3-6.7>
})

test:do_execsql_test("select3-6.8", [[
  SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
]], {
  -- <select3-6.8>
  5, 17, 4, 9, 3, 5, 2, 3, 1, 2, 0, 1
  -- </select3-6.8>
})

-- Sometimes an aggregate query can return no rows at all.
--
test:do_execsql_test("select3-7.1", [[
  DROP TABLE IF EXISTS t2;
  CREATE TABLE t2(a  INT primary key,b INT );
  INSERT INTO t2 VALUES(1,2);
  SELECT a, sum(b) FROM t2 WHERE b=5 GROUP BY a;
]], {
  -- <select3-7.1>
  
  -- </select3-7.1>
})

test:do_execsql_test("select3-7.2", [[
  SELECT a, sum(b) FROM t2 WHERE b=5;
]], {
  -- <select3-7.2>
  "", ""
  -- </select3-7.2>
})

-- If a table column is of typeNUMBER but we are storing integer values
-- in it, the values are stored as integers to take up less space.  The
-- values are converted by toNUMBER as they are read out of the table.
-- Make sure the GROUP BY clause does this conversion correctly.
-- Ticket #2251.
--
test:do_execsql_test("select3-8.1", [[
  DROP TABLE IF EXISTS A;
  CREATE TABLE A (
    A1 DOUBLE,
    A2 TEXT,
    A3 DOUBLE,
    id int primary key
  );
  INSERT INTO A VALUES(39136,'ABC',1201900000, 1);
  INSERT INTO A VALUES(39136,'ABC',1207000000, 2);
  SELECT typeof(sum(a3)) FROM a;
]], {
  -- <select3-8.1>
  "double"
  -- </select3-8.1>
})

test:do_execsql_test("select3-8.2", [[
  SELECT typeof(sum(a3)) FROM a GROUP BY a1;
]], {
  -- <select3-8.2>
  "double"
  -- </select3-8.2>
})

test:finish_test()