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 contains automated tests used to verify that the sqlite_stat3
13 # functionality is working. The tests in this file are based on a subset
14 # of the sqlite_stat4 tests in analyze9.test.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix analyzeB
27 CREATE TABLE t1(a TEXT, b TEXT);
28 INSERT INTO t1 VALUES('(0)', '(0)');
29 INSERT INTO t1 VALUES('(1)', '(1)');
30 INSERT INTO t1 VALUES('(2)', '(2)');
31 INSERT INTO t1 VALUES('(3)', '(3)');
32 INSERT INTO t1 VALUES('(4)', '(4)');
33 CREATE INDEX i1 ON t1(a, b);
42 SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3;
51 if {[permutation] != "utf16"} {
53 SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3;
64 #-------------------------------------------------------------------------
65 # This is really just to test SQL user function "test_decode".
69 CREATE TABLE t1(a, b, c);
70 INSERT INTO t1(a) VALUES('some text');
71 INSERT INTO t1(a) VALUES(14);
72 INSERT INTO t1(a) VALUES(NULL);
73 INSERT INTO t1(a) VALUES(22.0);
74 INSERT INTO t1(a) VALUES(x'656667');
75 CREATE INDEX i1 ON t1(a, b, c);
77 SELECT quote(sample) FROM sqlite_stat3;
79 NULL 14 22.0 {'some text'} X'656667'
82 #-------------------------------------------------------------------------
86 CREATE TABLE t2(a, b);
87 CREATE INDEX i2 ON t2(a, b);
92 for {set i 0} {$i < 1000} {incr i} {
94 set b [expr int(rand() * 15.0)]
95 execsql { INSERT INTO t2 VALUES($a, $b) }
100 db func lindex lindex
102 # Each value of "a" occurs exactly 10 times in the table.
104 do_execsql_test 3.3.1 {
105 SELECT count(*) FROM t2 GROUP BY a;
106 } [lrange [string repeat "10 " 100] 0 99]
108 # The first element in the "nEq" list of all samples should therefore be 10.
110 do_execsql_test 3.3.2 {
112 SELECT nEq FROM sqlite_stat3;
113 } [lrange [string repeat "10 " 100] 0 23]
115 #-------------------------------------------------------------------------
117 do_execsql_test 3.4 {
118 DROP TABLE IF EXISTS t1;
119 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
120 INSERT INTO t1 VALUES(1, 1, 'one-a');
121 INSERT INTO t1 VALUES(11, 1, 'one-b');
122 INSERT INTO t1 VALUES(21, 1, 'one-c');
123 INSERT INTO t1 VALUES(31, 1, 'one-d');
124 INSERT INTO t1 VALUES(41, 1, 'one-e');
125 INSERT INTO t1 VALUES(51, 1, 'one-f');
126 INSERT INTO t1 VALUES(61, 1, 'one-g');
127 INSERT INTO t1 VALUES(71, 1, 'one-h');
128 INSERT INTO t1 VALUES(81, 1, 'one-i');
129 INSERT INTO t1 VALUES(91, 1, 'one-j');
130 INSERT INTO t1 SELECT a+1,2,'two' || substr(c,4) FROM t1;
131 INSERT INTO t1 SELECT a+2,3,'three'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
132 INSERT INTO t1 SELECT a+3,4,'four'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
133 INSERT INTO t1 SELECT a+4,5,'five'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
134 INSERT INTO t1 SELECT a+5,6,'six'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
135 CREATE INDEX t1b ON t1(b);
137 SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND 60;
138 } {three-d three-e three-f}
141 #-------------------------------------------------------------------------
142 # These tests verify that the sample selection for stat3 appears to be
143 # working as designed.
147 db func lindex lindex
148 db func lrange lrange
150 do_execsql_test 4.0 {
151 DROP TABLE IF EXISTS t1;
152 CREATE TABLE t1(a, b, c);
153 CREATE INDEX i1 ON t1(c, b, a);
157 proc insert_filler_rows_n {iStart args} {
161 foreach {k v} $args {
162 if {[info exists A($k)]==0} { error "no such option: $k" }
165 if {[llength $args] % 2} {
166 error "option requires an argument: [lindex $args end]"
169 for {set i 0} {$i < $A(-nval)} {incr i} {
170 set iVal [expr $iStart+$i]
171 for {set j 0} {$j < $A(-ncopy)} {incr j} {
172 execsql { INSERT INTO t1 VALUES($iVal, $iVal, $iVal) }
179 insert_filler_rows_n 0 -ncopy 10 -nval 19
180 insert_filler_rows_n 20 -ncopy 1 -nval 100
183 INSERT INTO t1(c, b, a) VALUES(200, 1, 'a');
184 INSERT INTO t1(c, b, a) VALUES(200, 1, 'b');
185 INSERT INTO t1(c, b, a) VALUES(200, 1, 'c');
187 INSERT INTO t1(c, b, a) VALUES(200, 2, 'e');
188 INSERT INTO t1(c, b, a) VALUES(200, 2, 'f');
190 INSERT INTO t1(c, b, a) VALUES(201, 3, 'g');
191 INSERT INTO t1(c, b, a) VALUES(201, 4, 'h');
194 SELECT count(*) FROM sqlite_stat3;
195 SELECT count(*) FROM t1;
199 do_execsql_test 4.2 {
200 SELECT neq, nlt, ndlt, sample FROM sqlite_stat3 ORDER BY rowid LIMIT 16;
220 do_execsql_test 4.3 {
221 SELECT neq, nlt, ndlt, sample FROM sqlite_stat3
222 ORDER BY rowid DESC LIMIT 2;
228 do_execsql_test 4.4 { SELECT count(DISTINCT c) FROM t1 WHERE c<201 } 120
229 do_execsql_test 4.5 { SELECT count(DISTINCT c) FROM t1 WHERE c<200 } 119
235 CREATE TABLE t1(o,t INTEGER PRIMARY KEY);
236 CREATE INDEX i1 ON t1(o);
238 for {set i 0} {$i<10000} {incr i [expr (($i<1000)?1:10)]} {
239 execsql { INSERT INTO t1 VALUES('x', $i) }
244 SELECT count(*) FROM sqlite_stat3;
247 do_execsql_test 4.8 {
248 SELECT sample FROM sqlite_stat3;
252 #-------------------------------------------------------------------------
253 # The following would cause a crash at one point.
256 do_execsql_test 5.1 {
257 PRAGMA encoding = 'utf-16';
262 #-------------------------------------------------------------------------
263 # This was also crashing (corrupt sqlite_stat3 table).
266 do_execsql_test 6.1 {
267 CREATE TABLE t1(a, b);
268 CREATE INDEX i1 ON t1(a);
269 CREATE INDEX i2 ON t1(b);
270 INSERT INTO t1 VALUES(1, 1);
271 INSERT INTO t1 VALUES(2, 2);
272 INSERT INTO t1 VALUES(3, 3);
273 INSERT INTO t1 VALUES(4, 4);
274 INSERT INTO t1 VALUES(5, 5);
276 PRAGMA writable_schema = 1;
277 CREATE TEMP TABLE x1 AS
278 SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat3
279 ORDER BY (rowid%5), rowid;
280 DELETE FROM sqlite_stat3;
281 INSERT INTO sqlite_stat3 SELECT * FROM x1;
282 PRAGMA writable_schema = 0;
283 ANALYZE sqlite_master;
285 do_execsql_test 6.2 {
286 SELECT * FROM t1 WHERE a = 'abc';
289 #-------------------------------------------------------------------------
290 # The following tests experiment with adding corrupted records to the
291 # 'sample' column of the sqlite_stat3 table.
294 sqlite3_db_config_lookaside db 0 0 0
296 do_execsql_test 7.1 {
297 CREATE TABLE t1(a, b);
298 CREATE INDEX i1 ON t1(a, b);
299 INSERT INTO t1 VALUES(1, 1);
300 INSERT INTO t1 VALUES(2, 2);
301 INSERT INTO t1 VALUES(3, 3);
302 INSERT INTO t1 VALUES(4, 4);
303 INSERT INTO t1 VALUES(5, 5);
305 UPDATE sqlite_stat3 SET sample = X'' WHERE rowid = 1;
306 ANALYZE sqlite_master;
309 do_execsql_test 7.2 {
310 UPDATE sqlite_stat3 SET sample = X'FFFF';
311 ANALYZE sqlite_master;
312 SELECT * FROM t1 WHERE a = 1;
315 do_execsql_test 7.3 {
317 UPDATE sqlite_stat3 SET neq = '0 0 0';
318 ANALYZE sqlite_master;
319 SELECT * FROM t1 WHERE a = 1;
322 do_execsql_test 7.4 {
324 UPDATE sqlite_stat3 SET ndlt = '0 0 0';
325 ANALYZE sqlite_master;
326 SELECT * FROM t1 WHERE a = 3;
329 do_execsql_test 7.5 {
331 UPDATE sqlite_stat3 SET nlt = '0 0 0';
332 ANALYZE sqlite_master;
333 SELECT * FROM t1 WHERE a = 5;
336 #-------------------------------------------------------------------------
339 do_execsql_test 8.1 {
340 CREATE TABLE t1(x TEXT);
341 CREATE INDEX i1 ON t1(x);
342 INSERT INTO t1 VALUES('1');
343 INSERT INTO t1 VALUES('2');
344 INSERT INTO t1 VALUES('3');
345 INSERT INTO t1 VALUES('4');
348 do_execsql_test 8.2 {
349 SELECT * FROM t1 WHERE x = 3;
352 #-------------------------------------------------------------------------
355 do_execsql_test 9.1 {
356 CREATE TABLE t1(a, b, c, d, e);
357 CREATE INDEX i1 ON t1(a, b, c, d);
358 CREATE INDEX i2 ON t1(e);
362 for {set i 0} {$i < 100} {incr i} {
363 execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
365 for {set i 0} {$i < 20} {incr i} {
366 execsql "INSERT INTO t1 VALUES('x', 'y', 'z', 101, $i)"
368 for {set i 102} {$i < 200} {incr i} {
369 execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
376 SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=101 AND e=5;
377 } {/t1 USING INDEX i1/}
379 SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=99 AND e=5;
380 } {/t1 USING INDEX i1/}
382 set value_d [expr 101]
384 SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
385 } {/t1 USING INDEX i1/}
386 set value_d [expr 99]
388 SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
389 } {/t1 USING INDEX i1/}
391 #-------------------------------------------------------------------------
392 # Check that the planner takes stat3 data into account when considering
393 # "IS NULL" and "IS NOT NULL" constraints.
395 do_execsql_test 10.1.1 {
396 DROP TABLE IF EXISTS t3;
397 CREATE TABLE t3(a, b);
398 CREATE INDEX t3a ON t3(a);
399 CREATE INDEX t3b ON t3(b);
402 for {set i 1} {$i < 100} {incr i} {
403 if {$i>90} { set a $i } else { set a NULL }
405 execsql "INSERT INTO t3 VALUES($a, $b)"
410 SELECT * FROM t3 WHERE a IS NULL AND b = 2
411 } {/t3 USING INDEX t3b/}
413 SELECT * FROM t3 WHERE a IS NOT NULL AND b = 2
414 } {/t3 USING INDEX t3a/}
416 #-------------------------------------------------------------------------
417 # Check that stat3 data is used correctly with non-default collation
420 foreach {tn schema} {
422 CREATE TABLE t4(a COLLATE nocase, b);
423 CREATE INDEX t4a ON t4(a);
424 CREATE INDEX t4b ON t4(b);
427 CREATE TABLE t4(a, b);
428 CREATE INDEX t4a ON t4(a COLLATE nocase);
429 CREATE INDEX t4b ON t4(b);
433 do_test 11.$tn.1 { execsql $schema } {}
436 for {set i 0} {$i < 100} {incr i} {
437 if { ($i % 10)==0 } { set a ABC } else { set a DEF }
439 execsql { INSERT INTO t4 VALUES($a, $b) }
444 do_eqp_test 11.$tn.3 {
445 SELECT * FROM t4 WHERE a = 'def' AND b = 3;
446 } {/t4 USING INDEX t4b/}
449 set sql "SELECT * FROM t4 WHERE a = 'abc' AND b = 3;"
450 do_eqp_test 11.$tn.4 $sql {/t4 USING INDEX t4a/}
453 set sql "SELECT * FROM t4 WHERE a = 'abc' COLLATE nocase AND b = 3;"
454 do_eqp_test 11.$tn.5 $sql {/t4 USING INDEX t4a/}
456 set sql "SELECT * FROM t4 WHERE a COLLATE nocase = 'abc' AND b = 3;"
457 do_eqp_test 11.$tn.6 $sql {/t4 USING INDEX t4a/}
461 #-------------------------------------------------------------------------
462 # Test that nothing untoward happens if the stat3 table contains entries
463 # for indexes that do not exist. Or NULL values in the idx column.
464 # Or NULL values in any of the other columns.
467 do_execsql_test 15.1 {
468 CREATE TABLE x1(a, b, UNIQUE(a, b));
469 INSERT INTO x1 VALUES(1, 2);
470 INSERT INTO x1 VALUES(3, 4);
471 INSERT INTO x1 VALUES(5, 6);
473 INSERT INTO sqlite_stat3 VALUES(NULL, NULL, NULL, NULL, NULL, NULL);
477 do_execsql_test 15.2 { SELECT * FROM x1 } {1 2 3 4 5 6}
479 do_execsql_test 15.3 {
480 INSERT INTO sqlite_stat3 VALUES(42, 42, 42, 42, 42, 42);
484 do_execsql_test 15.4 { SELECT * FROM x1 } {1 2 3 4 5 6}
486 do_execsql_test 15.5 {
487 UPDATE sqlite_stat1 SET stat = NULL;
491 do_execsql_test 15.6 { SELECT * FROM x1 } {1 2 3 4 5 6}
493 do_execsql_test 15.7 {
495 UPDATE sqlite_stat1 SET tbl = 'no such tbl';
499 do_execsql_test 15.8 { SELECT * FROM x1 } {1 2 3 4 5 6}
501 do_execsql_test 15.9 {
503 UPDATE sqlite_stat3 SET neq = NULL, nlt=NULL, ndlt=NULL;
507 do_execsql_test 15.10 { SELECT * FROM x1 } {1 2 3 4 5 6}
509 # This is just for coverage....
510 do_execsql_test 15.11 {
512 UPDATE sqlite_stat1 SET stat = stat || ' unordered';
516 do_execsql_test 15.12 { SELECT * FROM x1 } {1 2 3 4 5 6}
518 #-------------------------------------------------------------------------
519 # Test that allocations used for sqlite_stat3 samples are included in
520 # the quantity returned by SQLITE_DBSTATUS_SCHEMA_USED.
522 set one [string repeat x 1000]
523 set two [string repeat x 2000]
527 CREATE TABLE t1(a, UNIQUE(a));
528 INSERT INTO t1 VALUES($one);
531 set nByte [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
535 CREATE TABLE t1(a, UNIQUE(a));
536 INSERT INTO t1 VALUES($two);
539 set nByte2 [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
541 expr {$nByte2 > $nByte+950 && $nByte2 < $nByte+1050}
544 #-------------------------------------------------------------------------
545 # Test that stat3 data may be used with partial indexes.
550 CREATE TABLE t1(a, b, c, d);
551 CREATE INDEX i1 ON t1(a, b) WHERE d IS NOT NULL;
552 INSERT INTO t1 VALUES(-1, -1, -1, NULL);
553 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
554 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
555 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
556 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
557 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
558 INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
561 for {set i 0} {$i < 32} {incr i} {
562 execsql { INSERT INTO t1 VALUES($i%2, $b, $i/2, 'abc') }
564 execsql {ANALYZE main.t1}
567 do_catchsql_test 17.1.2 {
569 } {1 {no such table: temp.t1}}
572 SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
575 SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
578 do_execsql_test 17.4 {
579 CREATE INDEX i2 ON t1(c) WHERE d IS NOT NULL;
583 SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
586 SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=0 AND c=10;
589 #-------------------------------------------------------------------------
594 CREATE TABLE t1(a, b);
595 CREATE INDEX i1 ON t1(a, b);
597 for {set i 0} {$i < 9} {incr i} {
599 INSERT INTO t1 VALUES($i, 0);
600 INSERT INTO t1 VALUES($i, 0);
601 INSERT INTO t1 VALUES($i, 0);
602 INSERT INTO t1 VALUES($i, 0);
603 INSERT INTO t1 VALUES($i, 0);
604 INSERT INTO t1 VALUES($i, 0);
605 INSERT INTO t1 VALUES($i, 0);
606 INSERT INTO t1 VALUES($i, 0);
607 INSERT INTO t1 VALUES($i, 0);
608 INSERT INTO t1 VALUES($i, 0);
609 INSERT INTO t1 VALUES($i, 0);
610 INSERT INTO t1 VALUES($i, 0);
611 INSERT INTO t1 VALUES($i, 0);
612 INSERT INTO t1 VALUES($i, 0);
613 INSERT INTO t1 VALUES($i, 0);
617 execsql { SELECT count(*) FROM sqlite_stat3 }
620 #-------------------------------------------------------------------------
627 CREATE TABLE t1(x, y);
628 CREATE INDEX i1 ON t1(x, y);
629 CREATE VIEW v1 AS SELECT * FROM t1;
635 proc authproc {op args} {
636 if {$op == "SQLITE_ANALYZE"} { return "SQLITE_DENY" }
643 CREATE TABLE t1(x, y);
644 CREATE VIEW v1 AS SELECT * FROM t1;
647 } {1 {not authorized}}
650 #-------------------------------------------------------------------------
653 proc r {args} { expr rand() }
655 db func lrange lrange
658 CREATE TABLE t1(a,b,c,d);
659 CREATE INDEX i1 ON t1(a,b,c,d);
661 for {set i 0} {$i < 16} {incr i} {
663 INSERT INTO t1 VALUES($i, r(), r(), r());
664 INSERT INTO t1 VALUES($i, $i, r(), r());
665 INSERT INTO t1 VALUES($i, $i, $i, r());
666 INSERT INTO t1 VALUES($i, $i, $i, $i);
667 INSERT INTO t1 VALUES($i, $i, $i, $i);
668 INSERT INTO t1 VALUES($i, $i, $i, r());
669 INSERT INTO t1 VALUES($i, $i, r(), r());
670 INSERT INTO t1 VALUES($i, r(), r(), r());
674 do_execsql_test 20.2 { ANALYZE }
675 for {set i 0} {$i<16} {incr i} {
677 do_execsql_test 20.3.$i {
678 SELECT count(*) FROM sqlite_stat3 WHERE sample=$val