3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file implements regression tests for SQLite library. This file
13 # implements tests for the extra functionality provided by the ANALYZE
14 # command when the library is compiled with SQLITE_ENABLE_STAT2 defined.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
25 set testprefix analyze2
27 # Do not use a codec for tests in this file, as the database file is
28 # manipulated directly using tcl scripts (using the [hexio_write] command).
32 #--------------------------------------------------------------------
35 # analyze2-1.*: Tests to verify that ANALYZE creates and populates the
36 # sqlite_stat2 table as expected.
38 # analyze2-2.*: Test that when a table has two indexes on it and either
39 # index may be used for the scan, the index suggested by
40 # the contents of sqlite_stat2 table is prefered.
42 # analyze2-3.*: Similar to the previous block of tests, but using tables
43 # that contain a mixture of NULL, numeric, text and blob
46 # analyze2-4.*: Check that when an indexed column uses a collation other
47 # than BINARY, the collation is taken into account when
48 # using the contents of sqlite_stat2 to estimate the cost
51 # analyze2-5.*: Check that collation sequences are used as described above
52 # even when the only available version of the collation
53 # function require UTF-16 encoded arguments.
55 # analyze2-6.*: Check that the library behaves correctly when one of the
56 # sqlite_stat2 or sqlite_stat1 tables are missing.
58 # analyze2-7.*: Check that in a shared-schema situation, nothing goes
59 # wrong if sqlite_stat2 data is read by one connection,
60 # and freed by another.
63 proc eqp {sql {db db}} {
64 uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
67 do_test analyze2-1.1 {
68 execsql { CREATE TABLE t1(x PRIMARY KEY) }
69 for {set i 0} {$i < 1000} {incr i} {
70 execsql { INSERT INTO t1 VALUES($i) }
74 SELECT * FROM sqlite_stat2;
76 } [list t1 sqlite_autoindex_t1_1 0 50 \
77 t1 sqlite_autoindex_t1_1 1 149 \
78 t1 sqlite_autoindex_t1_1 2 249 \
79 t1 sqlite_autoindex_t1_1 3 349 \
80 t1 sqlite_autoindex_t1_1 4 449 \
81 t1 sqlite_autoindex_t1_1 5 549 \
82 t1 sqlite_autoindex_t1_1 6 649 \
83 t1 sqlite_autoindex_t1_1 7 749 \
84 t1 sqlite_autoindex_t1_1 8 849 \
85 t1 sqlite_autoindex_t1_1 9 949 \
88 do_test analyze2-1.2 {
90 DELETE FROM t1 WHERe x>9;
92 SELECT tbl, idx, group_concat(sample, ' ') FROM sqlite_stat2;
94 } {t1 sqlite_autoindex_t1_1 {0 1 2 3 4 5 6 7 8 9}}
95 do_test analyze2-1.3 {
97 DELETE FROM t1 WHERE x>8;
99 SELECT * FROM sqlite_stat2;
102 do_test analyze2-1.4 {
106 SELECT * FROM sqlite_stat2;
110 do_test analyze2-2.1 {
114 CREATE TABLE t1(x, y);
115 CREATE INDEX t1_x ON t1(x);
116 CREATE INDEX t1_y ON t1(y);
118 for {set i 0} {$i < 1000} {incr i} {
119 execsql { INSERT INTO t1 VALUES($i, $i) }
125 SELECT * FROM t1 WHERE x>500 AND y>700
127 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>?) (~100 rows)}
130 SELECT * FROM t1 WHERE x>700 AND y>500
132 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>?) (~100 rows)}
135 SELECT * FROM t1 WHERE y>700 AND x>500
137 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>?) (~100 rows)}
140 SELECT * FROM t1 WHERE y>500 AND x>700
142 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>?) (~100 rows)}
145 SELECT * FROM t1 WHERE x BETWEEN 100 AND 200 AND y BETWEEN 400 AND 700
147 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>? AND x<?) (~25 rows)}
150 SELECT * FROM t1 WHERE x BETWEEN 100 AND 500 AND y BETWEEN 400 AND 700
152 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>? AND y<?) (~75 rows)}
155 SELECT * FROM t1 WHERE x BETWEEN -400 AND -300 AND y BETWEEN 100 AND 300
157 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>? AND x<?) (~12 rows)}
160 SELECT * FROM t1 WHERE x BETWEEN 100 AND 300 AND y BETWEEN -400 AND -300
162 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>? AND y<?) (~12 rows)}
165 SELECT * FROM t1 WHERE x BETWEEN 500 AND 100 AND y BETWEEN 100 AND 300
167 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>? AND x<?) (~12 rows)}
170 SELECT * FROM t1 WHERE x BETWEEN 100 AND 300 AND y BETWEEN 500 AND 100
172 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>? AND y<?) (~12 rows)}
175 do_test analyze2-3.1 {
176 set alphabet [list a b c d e f g h i j]
178 for {set i 0} {$i < 1000} {incr i} {
179 set str [lindex $alphabet [expr ($i/100)%10]]
180 append str [lindex $alphabet [expr ($i/ 10)%10]]
181 append str [lindex $alphabet [expr ($i/ 1)%10]]
182 execsql { INSERT INTO t1 VALUES($str, $str) }
187 SELECT tbl,idx,group_concat(sample,' ')
192 } {t1 t1_x {100 299 499 699 899 ajj cjj ejj gjj ijj}}
193 do_test analyze2-3.2 {
195 SELECT tbl,idx,group_concat(sample,' ')
200 } {t1 t1_y {100 299 499 699 899 ajj cjj ejj gjj ijj}}
203 SELECT * FROM t1 WHERE x BETWEEN 100 AND 500 AND y BETWEEN 'a' AND 'b'
205 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>? AND y<?) (~50 rows)}
208 SELECT * FROM t1 WHERE x BETWEEN 100 AND 400 AND y BETWEEN 'a' AND 'h'
210 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x>? AND x<?) (~100 rows)}
213 SELECT * FROM t1 WHERE x<'a' AND y>'h'
215 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>?) (~66 rows)}
218 SELECT * FROM t1 WHERE x<444 AND y>'h'
220 0 0 0 {SEARCH TABLE t1 USING INDEX t1_y (y>?) (~66 rows)}
223 SELECT * FROM t1 WHERE x<221 AND y>'g'
225 0 0 0 {SEARCH TABLE t1 USING INDEX t1_x (x<?) (~66 rows)}
228 do_test analyze2-4.1 {
229 execsql { CREATE TABLE t3(a COLLATE nocase, b) }
230 execsql { CREATE INDEX t3a ON t3(a) }
231 execsql { CREATE INDEX t3b ON t3(b) }
232 set alphabet [list A b C d E f G h I j]
234 for {set i 0} {$i < 1000} {incr i} {
235 set str [lindex $alphabet [expr ($i/100)%10]]
236 append str [lindex $alphabet [expr ($i/ 10)%10]]
237 append str [lindex $alphabet [expr ($i/ 1)%10]]
238 execsql { INSERT INTO t3 VALUES($str, $str) }
243 do_test analyze2-4.2 {
245 PRAGMA automatic_index=OFF;
246 SELECT tbl,idx,group_concat(sample,' ')
250 PRAGMA automatic_index=ON;
252 } {t3 t3a {AfA bEj CEj dEj EEj fEj GEj hEj IEj jEj}}
253 do_test analyze2-4.3 {
255 SELECT tbl,idx,group_concat(sample,' ')
260 } {t3 t3b {AbA CIj EIj GIj IIj bIj dIj fIj hIj jIj}}
263 SELECT * FROM t3 WHERE a > 'A' AND a < 'C' AND b > 'A' AND b < 'C'
265 0 0 0 {SEARCH TABLE t3 USING INDEX t3b (b>? AND b<?) (~11 rows)}
268 SELECT * FROM t3 WHERE a > 'A' AND a < 'c' AND b > 'A' AND b < 'c'
270 0 0 0 {SEARCH TABLE t3 USING INDEX t3a (a>? AND a<?) (~22 rows)}
274 proc test_collate {enc lhs rhs} {
276 return [string compare $lhs $rhs]
278 do_test analyze2-5.1 {
279 add_test_collate db 0 0 1
280 execsql { CREATE TABLE t4(x COLLATE test_collate) }
281 execsql { CREATE INDEX t4x ON t4(x) }
282 set alphabet [list a b c d e f g h i j]
284 for {set i 0} {$i < 1000} {incr i} {
285 set str [lindex $alphabet [expr ($i/100)%10]]
286 append str [lindex $alphabet [expr ($i/ 10)%10]]
287 append str [lindex $alphabet [expr ($i/ 1)%10]]
288 execsql { INSERT INTO t4 VALUES($str) }
293 do_test analyze2-5.2 {
295 SELECT tbl,idx,group_concat(sample,' ')
300 } {t4 t4x {afa bej cej dej eej fej gej hej iej jej}}
302 SELECT * FROM t4 WHERE x>'ccc'
303 } {0 0 0 {SEARCH TABLE t4 USING COVERING INDEX t4x (x>?) (~800 rows)}}
305 SELECT * FROM t4 AS t41, t4 AS t42 WHERE t41.x>'ccc' AND t42.x>'ggg'
307 0 0 1 {SEARCH TABLE t4 AS t42 USING COVERING INDEX t4x (x>?) (~300 rows)}
308 0 1 0 {SEARCH TABLE t4 AS t41 USING COVERING INDEX t4x (x>?) (~800 rows)}
311 SELECT * FROM t4 AS t41, t4 AS t42 WHERE t41.x>'ddd' AND t42.x>'ccc'
313 0 0 0 {SEARCH TABLE t4 AS t41 USING COVERING INDEX t4x (x>?) (~700 rows)}
314 0 1 1 {SEARCH TABLE t4 AS t42 USING COVERING INDEX t4x (x>?) (~800 rows)}
318 #--------------------------------------------------------------------
319 # These tests, analyze2-6.*, verify that the library behaves correctly
320 # when one of the sqlite_stat1 and sqlite_stat2 tables is missing.
322 # If the sqlite_stat1 table is not present, then the sqlite_stat2
323 # table is not read. However, if it is the sqlite_stat2 table that
324 # is missing, the data in the sqlite_stat1 table is still used.
326 # Tests analyze2-6.1.* test the libary when the sqlite_stat2 table
327 # is missing. Tests analyze2-6.2.* test the library when sqlite_stat1
330 do_test analyze2-6.0 {
332 DROP TABLE IF EXISTS t4;
333 CREATE TABLE t5(a, b); CREATE INDEX t5i ON t5(a, b);
334 CREATE TABLE t6(a, b); CREATE INDEX t6i ON t6(a, b);
336 for {set ii 0} {$ii < 20} {incr ii} {
338 INSERT INTO t5 VALUES($ii, $ii);
339 INSERT INTO t6 VALUES($ii/10, $ii/10);
343 CREATE TABLE master AS
344 SELECT * FROM sqlite_master WHERE name LIKE 'sqlite_stat%'
348 do_test analyze2-6.1.1 {
349 eqp {SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
351 t6.a = 1 AND t6.b = 1
353 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a=? AND b=?) (~9 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
354 do_test analyze2-6.1.2 {
357 eqp {SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
359 t6.a = 1 AND t6.b = 1
361 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a=?) (~1 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
362 do_test analyze2-6.1.3 {
364 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
366 t6.a = 1 AND t6.b = 1
368 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a=?) (~1 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
369 do_test analyze2-6.1.4 {
371 PRAGMA writable_schema = 1;
372 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat2';
375 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
377 t6.a = 1 AND t6.b = 1
379 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a=?) (~1 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
380 do_test analyze2-6.1.5 {
382 PRAGMA writable_schema = 1;
383 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat1';
386 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
388 t6.a = 1 AND t6.b = 1
390 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a=? AND b=?) (~9 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
391 do_test analyze2-6.1.6 {
393 PRAGMA writable_schema = 1;
394 INSERT INTO sqlite_master SELECT * FROM master;
397 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
399 t6.a = 1 AND t6.b = 1
401 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a=?) (~1 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
403 do_test analyze2-6.2.1 {
405 DELETE FROM sqlite_stat1;
406 DELETE FROM sqlite_stat2;
409 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
410 t5.a>1 AND t5.a<15 AND
413 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a>? AND a<?) (~60000 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
414 do_test analyze2-6.2.2 {
417 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
418 t5.a>1 AND t5.a<15 AND
421 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
422 do_test analyze2-6.2.3 {
424 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
425 t5.a>1 AND t5.a<15 AND
428 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
429 do_test analyze2-6.2.4 {
431 PRAGMA writable_schema = 1;
432 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat1';
435 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
436 t5.a>1 AND t5.a<15 AND
439 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a>? AND a<?) (~60000 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
440 do_test analyze2-6.2.5 {
442 PRAGMA writable_schema = 1;
443 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat2';
446 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
447 t5.a>1 AND t5.a<15 AND
450 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a>? AND a<?) (~60000 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
451 do_test analyze2-6.2.6 {
453 PRAGMA writable_schema = 1;
454 INSERT INTO sqlite_master SELECT * FROM master;
458 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
459 t5.a>1 AND t5.a<15 AND
462 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
464 #--------------------------------------------------------------------
465 # These tests, analyze2-7.*, test that the sqlite_stat2 functionality
466 # works in shared-cache mode. Note that these tests reuse the database
467 # created for the analyze2-6.* tests.
469 ifcapable shared_cache {
471 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
473 proc incr_schema_cookie {zDb} {
474 foreach iOffset {24 40} {
475 set cookie [hexio_get_int [hexio_read $zDb $iOffset 4]]
477 hexio_write $zDb $iOffset [hexio_render_int32 $cookie]
481 do_test analyze2-7.1 {
486 execsql { SELECT count(*) FROM t5 } db1
488 do_test analyze2-7.2 {
489 incr_schema_cookie test.db
490 execsql { SELECT count(*) FROM t5 } db2
492 do_test analyze2-7.3 {
493 incr_schema_cookie test.db
494 execsql { SELECT count(*) FROM t5 } db1
496 do_test analyze2-7.4 {
497 incr_schema_cookie test.db
498 execsql { SELECT count(*) FROM t5 } db2
501 do_test analyze2-7.5 {
502 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
503 t5.a>1 AND t5.a<15 AND
506 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
507 do_test analyze2-7.6 {
508 incr_schema_cookie test.db
509 execsql { SELECT * FROM sqlite_master } db2
510 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
511 t5.a>1 AND t5.a<15 AND
514 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
515 do_test analyze2-7.7 {
516 incr_schema_cookie test.db
517 execsql { SELECT * FROM sqlite_master } db1
518 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
519 t5.a>1 AND t5.a<15 AND
522 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
524 do_test analyze2-7.8 {
525 execsql { DELETE FROM sqlite_stat2 } db2
526 execsql { SELECT * FROM sqlite_master } db1
527 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
528 t5.a>1 AND t5.a<15 AND
531 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
532 do_test analyze2-7.9 {
533 execsql { SELECT * FROM sqlite_master } db2
534 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
535 t5.a>1 AND t5.a<15 AND
538 } {0 0 1 {SEARCH TABLE t6 USING COVERING INDEX t6i (a>?) (~1 rows)} 0 1 0 {SEARCH TABLE t5 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
540 do_test analyze2-7.10 {
541 incr_schema_cookie test.db
542 execsql { SELECT * FROM sqlite_master } db1
543 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
544 t5.a>1 AND t5.a<15 AND
547 } {0 0 0 {SEARCH TABLE t5 USING COVERING INDEX t5i (a>? AND a<?) (~1 rows)} 0 1 1 {SEARCH TABLE t6 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
551 sqlite3_enable_shared_cache $::enable_shared_cache