/
analyze3.test.lua
executable file
·647 lines (589 loc) · 17 KB
/
analyze3.test.lua
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env tarantool
local test = require("sqltester")
test:plan(37)
--!./tcltestrunner.lua
-- 2009 August 06
--
-- 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. This file
-- implements tests for range and LIKE constraints that use bound variables
-- instead of literal constant arguments.
--
------------------------------------------------------------------------
-- Test Organization:
--
-- analyze3-1.*: Test that the values of bound parameters are considered
-- in the same way as constants when planning queries that
-- use range constraints.
--
-- analyze3-2.*: Test that the values of bound parameters are considered
-- in the same way as constants when planning queries that
-- use LIKE expressions in the WHERE clause.
--
-- analyze3-3.*: Test that binding to a variable does not invalidate the
-- query plan when there is no way in which replanning the
-- query may produce a superior outcome.
--
-- analyze3-4.*: Test that SQL or authorization callback errors occuring
-- within sqlReprepare() are handled correctly.
--
-- analyze3-5.*: Check that the query plans of applicable statements are
-- invalidated if the values of SQL parameter are modified
-- using the clear_bindings() or transfer_bindings() APIs.
--
-- analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
--
-- analyze3-7.*: Test that some memory leaks discovered by fuzz testing
-- have been fixed.
--
---------------------------------------------------------------------------
--
-- analyze3-1.1.1:
-- Create a table with two columns. Populate the first column (affinity
-- INTEGER) with integer values from 100 to 1100. Create an index on this
-- column. ANALYZE the table.
--
-- analyze3-1.1.2 - 3.1.3
-- Show that there are two possible plans for querying the table with
-- a range constraint on the indexed column - "full table scan" or "use
-- the index". When the range is specified using literal values, sql
-- is able to pick the best plan based on the samples in sql_stat3.
--
-- analyze3-1.1.4 - 3.1.9
-- Show that using SQL variables produces the same results as using
-- literal values to constrain the range scan.
--
-- These tests also check that the compiler code considers column
-- affinities when estimating the number of rows scanned by the "use
-- index strategy".
--
test:do_test(
"analyze3-1.1.1",
function()
test:execsql([[
CREATE TABLE t1(id INT PRIMARY KEY, x INTEGER, y INT );
CREATE INDEX i1 ON t1(x);
START TRANSACTION;
]])
for i=1,1000 do
test:execsql(string.format(" INSERT INTO t1 VALUES(%s, %s+99, %s-1) ", i, i, i))
end
test:execsql([[
COMMIT;
ANALYZE;
]])
return test:execsql([[ SELECT count(*)>0 FROM "_sql_stat4"; ]])
end, {
-- <analyze3-1.1.1>
1
-- </analyze3-1.1.1>
})
test:do_execsql_test(
"analyze3-1.1.x",
[[
SELECT count(*) FROM t1 WHERE x>200 AND x<300;
]], {
-- <analyze3-1.1.x>
99
-- </analyze3-1.1.x>
})
test:do_execsql_test(
"analyze3-1.1.x",
[[
SELECT count(*) FROM t1 WHERE x>0 AND x<1100;
]], {
-- <analyze3-1.1.x>
1000
-- </analyze3-1.1.x>
})
-- The first of the following two SELECT statements visits 99 rows. So
-- it is better to use the index. But the second visits every row in
-- the table (1000 in total) so it is better to do a full-table scan.
--
test:do_eqp_test(
"analyze3-1.1.2",
[[
SELECT sum(y) FROM t1 WHERE x>200 AND x<300
]], {
-- <analyze3-1.1.2>
{0, 0, 0, "SEARCH TABLE T1 USING COVERING INDEX I1 (X>? AND X<?)"}
-- </analyze3-1.1.2>
})
test:do_eqp_test(
"analyze3-1.1.3",
[[
SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
]], {
-- Tarantool: index is always covering, thus there is no need to scan table.
-- <analyze3-1.1.3>
-- 0, 0, 0, "SCAN TABLE t1"
{0, 0, 0, "SEARCH TABLE T1 USING COVERING INDEX I1 (X>? AND X<?)"}
-- </analyze3-1.1.3>
})
test:do_sf_execsql_test(
"analyze3-1.1.4",
[[
SELECT sum(y) FROM t1 WHERE x>200 AND x<300
]], {
-- <analyze3-1.1.4>
-- 199, 0, 14850
-- Tarantool: index is always covering, hence number of searc counts is 100
-- we're unable to get steps var as there're no prepared stmts
100, {14850}
-- </analyze3-1.1.4>
})
test:do_test(
"analyze3-1.1.5",
function()
return test:sf_execsql([[ SELECT sum(y) FROM t1 WHERE x>'200' AND x<'300' ]])
end, {
-- <analyze3-1.1.5>
100, {14850}
-- </analyze3-1.1.5>
})
test:do_test(
"analyze3-1.1.6",
function()
return test:sf_execsql(" SELECT sum(y) FROM t1 WHERE x>200.0 AND x<300.0 ")
end, {
-- <analyze3-1.1.6>
100, {14850}
-- </analyze3-1.1.6>
})
test:do_sf_execsql_test(
"analyze3-1.1.7",
[[
SELECT sum(y) FROM t1 WHERE x>0 AND x<1100
]], {
-- <analyze3-1.1.7>
1000, {499500}
-- </analyze3-1.1.7>
})
test:do_test(
"analyze3-1.1.8",
function()
return test:sf_execsql([[ SELECT sum(y) FROM t1 WHERE x>'0' AND x<'1100' ]])
end, {
-- <analyze3-1.1.8>
1000, {499500}
-- </analyze3-1.1.8>
})
test:do_test(
"analyze3-1.1.9",
function()
return test:sf_execsql(" SELECT sum(y) FROM t1 WHERE x>0.0 AND x<1100.0 ")
end, {
-- <analyze3-1.1.9>
1000, {499500}
-- </analyze3-1.1.9>
})
-- The following tests are similar to the block above. The difference is
-- that the indexed column has TEXT affinity in this case. In the tests
-- above the affinity is INTEGER.
--
test:do_execsql_test(
"analyze3-1.2.1",
[[
CREATE TABLE t2(id INTEGER PRIMARY KEY, x TEXT, y INT);
START TRANSACTION;
INSERT INTO t2 SELECT * FROM t1;
COMMIT;
CREATE INDEX i2 ON t2(x);
ANALYZE;
]], {
-- <analyze3-1.2.1>
-- </analyze3-1.2.1>
})
test:do_execsql_test(
"analyze3-2.1.x",
[[
SELECT count(*) FROM t2 WHERE x>1 AND x<2;
]], {
-- <analyze3-2.1.x>
0
-- </analyze3-2.1.x>
})
test:do_execsql_test(
"analyze3-2.1.x",
[[
SELECT count(*) FROM t2 WHERE x>0 AND x<99;
]], {
-- <analyze3-2.1.x>
0
-- </analyze3-2.1.x>
})
-- Types of column and search value don't match, so
-- index search can't be used here.
--
test:do_eqp_test(
"analyze3-1.2.2",
[[
SELECT sum(y) FROM t2 WHERE x>1 AND x<2
]], {
-- <analyze3-1.2.2>
{0, 0, 0, "SCAN TABLE T2"}
-- </analyze3-1.2.2>
})
-- Tarantool: same as for 1.1.3
test:do_eqp_test(
"analyze3-1.2.3",
[[
SELECT sum(y) FROM t2 WHERE x>0 AND x<99
]], {
-- <analyze3-1.2.3>
-- 0, 0, 0, "SCAN TABLE t2"
{0, 0, 0, "SCAN TABLE T2"}
-- </analyze3-1.2.3>
})
test:do_sf_execsql_test(
"analyze3-1.2.4",
[[
SELECT sum(y) FROM t2 WHERE x>12 AND x<20
]], {
-- <analyze3-1.2.4>
999, {""}
-- </analyze3-1.2.4>
})
test:do_test(
"analyze3-1.2.5",
function()
return test:sf_execsql([[SELECT typeof('12'), typeof('20'), sum(y) FROM t2 WHERE x>'12' AND x<'20']])
end, {
-- <analyze3-1.2.5>
81, {"text", "text", 4760}
-- </analyze3-1.2.5>
})
test:do_test(
"analyze3-1.2.6",
function()
return test:sf_execsql("SELECT typeof(12), typeof(20), sum(y) FROM t2 WHERE x>12 AND x<20")
end, {
-- <analyze3-1.2.6>
999, {"integer", "integer", ""}
-- </analyze3-1.2.6>
})
test:do_sf_execsql_test(
"analyze3-1.2.7",
[[
SELECT sum(y) FROM t2 WHERE x>0 AND x<99
]], {
-- <analyze3-1.2.7>
999, {""}
-- </analyze3-1.2.7>
})
test:do_test(
"analyze3-1.2.8",
function()
return test:sf_execsql([[SELECT typeof('0'), typeof('99'), sum(y) FROM t2 WHERE x>'0' AND x<'99']])
end, {
-- <analyze3-1.2.8>
991, {"text", "text", 490555}
-- </analyze3-1.2.8>
})
test:do_test(
"analyze3-1.2.9",
function()
return test:sf_execsql("SELECT typeof(0), typeof(99), sum(y) FROM t2 WHERE x>0 AND x<99")
end, {
-- <analyze3-1.2.9>
999, {"integer", "integer", ""}
-- </analyze3-1.2.9>
})
-- Same tests a third time. This time, column x has INTEGER affinity and
-- is not the leftmost column of the table. This triggered a bug causing
-- sql to use sub-optimal query plans in 3.6.18 and earlier.
--
test:do_execsql_test(
"analyze3-1.3.1",
[[
CREATE TABLE t3(id INTEGER PRIMARY KEY, y INT, x INTEGER);
START TRANSACTION;
INSERT INTO t3 SELECT id, y, x FROM t1;
COMMIT;
CREATE INDEX i3 ON t3(x);
ANALYZE;
]], {
-- <analyze3-1.3.1>
-- </analyze3-1.3.1>
})
test:do_execsql_test(
"analyze3-1.3.x",
[[
SELECT count(*) FROM t3 WHERE x>200 AND x<300;
]], {
-- <analyze3-1.3.x>
99
-- </analyze3-1.3.x>
})
test:do_execsql_test(
"analyze3-1.3.y",
[[
SELECT count(*) FROM t3 WHERE x>0 AND x<1100
]], {
-- <analyze3-1.3.x>
1000
-- </analyze3-1.3.x>
})
test:do_eqp_test(
"analyze3-1.3.2",
[[
SELECT sum(y) FROM t3 WHERE x>200 AND x<300
]], {
-- <analyze3-1.3.2>
{0, 0, 0, "SEARCH TABLE T3 USING COVERING INDEX I3 (X>? AND X<?)"}
-- </analyze3-1.3.2>
})
-- Tarantool: same as 1.1.3
test:do_eqp_test(
"analyze3-1.3.3",
[[
SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
]], {
-- <analyze3-1.3.3>
-- 0, 0, 0, "SCAN TABLE t3"
{0, 0, 0, "SEARCH TABLE T3 USING COVERING INDEX I3 (X>? AND X<?)"}
-- </analyze3-1.3.3>
})
test:do_sf_execsql_test(
"analyze3-1.3.4",
[[
SELECT sum(y) FROM t3 WHERE x>200 AND x<300
]], {
-- <analyze3-1.3.4>
100, {14850}
-- </analyze3-1.3.4>
})
test:do_test(
"analyze3-1.3.5",
function()
return test:sf_execsql([[ SELECT sum(y) FROM t3 WHERE x>'200' AND x<'300' ]])
end, {
-- <analyze3-1.3.5>
100, {14850}
-- </analyze3-1.3.5>
})
test:do_test(
"analyze3-1.3.6",
function()
return test:sf_execsql(" SELECT sum(y) FROM t3 WHERE x>200 AND x<300 ")
end, {
-- <analyze3-1.3.6>
100, {14850}
-- </analyze3-1.3.6>
})
test:do_sf_execsql_test(
"analyze3-1.3.7",
[[
SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
]], {
-- <analyze3-1.3.7>
1000, {499500}
-- </analyze3-1.3.7>
})
test:do_test(
"analyze3-1.3.8",
function()
return test:sf_execsql([[ SELECT sum(y) FROM t3 WHERE x>'0' AND x<'1100' ]])
end, {
-- <analyze3-1.3.8>
1000, {499500}
-- </analyze3-1.3.8>
})
test:do_test(
"analyze3-1.3.9",
function()
return test:sf_execsql(" SELECT sum(y) FROM t3 WHERE x>0 AND x<1100 ")
end, {
-- <analyze3-1.3.9>
1000, {499500}
-- </analyze3-1.3.9>
})
---------------------------------------------------------------------------
-- Test that the values of bound SQL variables may be used for the LIKE
-- optimization.
--
-- Tarantool: commented until #2121 is resolved.
-- X(269, "X!cmd", [=[["drop_all_tables"]]=])
-- test:do_test(
-- "analyze3-2.1",
-- function()
-- test:execsql([[
-- PRAGMA case_sensitive_like=off;
-- BEGIN;
-- CREATE TABLE t1(a INT , b TEXT COLLATE nocase);
-- CREATE INDEX i1 ON t1(b);
-- ]])
-- for _ in X(0, "X!for", [=[["set i 0","$i < 1000","incr i"]]=]) do
-- t = ""
-- t = t .. test.lindex({a, b, c, d, e, f, g, h, i, j}, (i / 100))
-- t = t .. test.lindex({a, b, c, d, e, f, g, h, i, j}, X(284, "X!cmd", [=[["expr",["(",["i"],"\/10)%10"]]]=]))
-- t = t .. test.lindex({a, b, c, d, e, f, g, h, i, j}, X(285, "X!cmd", [=[["expr",["(",["i"],"%10)"]]]=]))
-- test:execsql(string.format(" INSERT INTO t1 VALUES(%s, %s) ", i, t))
-- end
-- return test:execsql("COMMIT")
-- end, {
-- -- <analyze3-2.1>
-- -- </analyze3-2.1>
-- })
-- test:do_eqp_test(
-- "analyze3-2.2",
-- [[
-- SELECT count(a) FROM t1 WHERE b LIKE 'a%'
-- ]], {
-- -- <analyze3-2.2>
-- 0, 0, 0, "SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?)"
-- -- </analyze3-2.2>
-- })
-- test:do_eqp_test(
-- "analyze3-2.3",
-- [[
-- SELECT count(a) FROM t1 WHERE b LIKE '%a'
-- ]], {
-- -- <analyze3-2.3>
-- 0, 0, 0, "SCAN TABLE t1"
-- -- </analyze3-2.3>
-- })
-- test:do_sf_execsql_test(
-- "analyze3-2.4",
-- [[
-- SELECT count(*) FROM t1 WHERE b LIKE 'a%'
-- ]], {
-- -- <analyze3-2.4>
-- 102, 0, 100
-- -- </analyze3-2.4>
-- })
-- test:do_sf_execsql_test(
-- "analyze3-2.5",
-- [[
-- SELECT count(*) FROM t1 WHERE b LIKE '%a'
-- ]], {
-- -- <analyze3-2.5>
-- 999, 999, 100
-- -- </analyze3-2.5>
-- })
-- test:do_test(
-- "analyze3-2.6",
-- function()
-- like = "a%"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.6>
-- 102, 0, 100
-- -- </analyze3-2.6>
-- })
-- test:do_test(
-- "analyze3-2.7",
-- function()
-- like = "%a"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.7>
-- 999, 999, 100
-- -- </analyze3-2.7>
-- })
-- test:do_test(
-- "analyze3-2.8",
-- function()
-- like = "a"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.8>
-- 102, 0, 0
-- -- </analyze3-2.8>
-- })
-- test:do_test(
-- "analyze3-2.9",
-- function()
-- like = "ab"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.9>
-- 12, 0, 0
-- -- </analyze3-2.9>
-- })
-- test:do_test(
-- "analyze3-2.10",
-- function()
-- like = "abc"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.10>
-- 3, 0, 1
-- -- </analyze3-2.10>
-- })
-- test:do_test(
-- "analyze3-2.11",
-- function()
-- like = "a_c"
-- return sf_execsql(" SELECT count(*) FROM t1 WHERE b LIKE $like ")
-- end, {
-- -- <analyze3-2.11>
-- 102, 0, 10
-- -- </analyze3-2.11>
-- })
---------------------------------------------------------------------------
test:do_test(
"analyze3-6.1",
function()
test:execsql(" DROP TABLE IF EXISTS t1 ")
test:execsql(" CREATE TABLE t1(id INTEGER PRIMARY KEY, a REAL, b TEXT, c REAL) ")
test:execsql("START TRANSACTION")
for i=1,1000 do
test:execsql(string.format("INSERT INTO t1 VALUES(%s, %s, 'x', %s)", i, ((i-1) / 100), ((i-1) / 10)))
end
test:execsql("COMMIT")
test:execsql([[
DROP INDEX IF EXISTS i1 ON t1;
DROP INDEX IF EXISTS i2 ON t1;
CREATE INDEX i2 ON t1(c);
CREATE INDEX i1 ON t1(a, b);
]])
return test:execsql("ANALYZE")
end, {
-- <analyze3-6.1>
-- </analyze3-6.1>
})
test:do_eqp_test(
"analyze3-6-3",
[[
SELECT * FROM t1 WHERE a = 5 AND c = 13;
]], {
-- <analyze3-6-3>
{0, 0, 0, "SEARCH TABLE T1 USING COVERING INDEX I2 (C=?)"}
-- </analyze3-6-3>
})
test:do_eqp_test(
"analyze3-6-2",
[[
SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
]], {
-- <analyze3-6-2>
{0, 0, 0, "SEARCH TABLE T1 USING COVERING INDEX I2 (C=?)"}
-- </analyze3-6-2>
})
-------------------------------------------------------------------------------
-- 2015-04-20.
-- Memory leak in sqlStat4ProbeFree(). (Discovered while fuzzing.)
--
test:do_execsql_test(
"analyze-7.1",
[[
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT , c INT );
INSERT INTO t1 VALUES(1,1,'0000');
CREATE INDEX t0b ON t1(b);
ANALYZE;
SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND hex(1);
]], {
-- <analyze-7.1>
-- </analyze-7.1>
})
test:finish_test()