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 current build
13 # (which must be either ENABLE_STAT3 or ENABLE_STAT4) works with both stat3
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set testprefix analyzeA
21 ifcapable !stat4&&!stat3 {
26 # Populate the stat3 table according to the current contents of the db
28 proc populate_stat3 {{bDropTable 1}} {
29 # Open a second connection on database "test.db" and run ANALYZE. If this
30 # is an ENABLE_STAT3 build, this is all that is required to create and
31 # populate the sqlite_stat3 table.
36 # Now, if this is an ENABLE_STAT4 build, create and populate the
37 # sqlite_stat3 table based on the stat4 data gathered by the ANALYZE
38 # above. Then drop the sqlite_stat4 table.
41 db2 func lindex lindex
43 PRAGMA writable_schema = on;
44 CREATE TABLE sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample);
45 INSERT INTO sqlite_stat3
46 SELECT DISTINCT tbl, idx,
47 lindex(neq,0), lindex(nlt,0), lindex(ndlt,0), test_extract(sample, 0)
50 if {$bDropTable} { execsql {DROP TABLE sqlite_stat4} db2 }
51 execsql { PRAGMA writable_schema = off }
54 # Modify the database schema cookie to ensure that the other connection
58 CREATE TABLE obscure_tbl_nm(x);
59 DROP TABLE obscure_tbl_nm;
64 # Populate the stat4 table according to the current contents of the db
66 proc populate_stat4 {{bDropTable 1}} {
72 PRAGMA writable_schema = on;
73 CREATE TABLE sqlite_stat4(tbl,idx,neq,nlt,ndlt,sample);
74 INSERT INTO sqlite_stat4
75 SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample)
78 if {$bDropTable} { execsql {DROP TABLE sqlite_stat3} db2 }
79 execsql { PRAGMA writable_schema = off }
82 # Modify the database schema cookie to ensure that the other connection
86 CREATE TABLE obscure_tbl_nm(x);
87 DROP TABLE obscure_tbl_nm;
92 # Populate the stat4 table according to the current contents of the db.
93 # Leave deceptive data in the stat3 table. This data should be ignored
94 # in favour of that from the stat4 table.
96 proc populate_both {} {
97 ifcapable stat4 { populate_stat3 0 }
98 ifcapable stat3 { populate_stat4 0 }
102 PRAGMA writable_schema = on;
103 UPDATE sqlite_stat3 SET idx =
104 CASE idx WHEN 't1b' THEN 't1c' ELSE 't1b'
106 PRAGMA writable_schema = off;
107 CREATE TABLE obscure_tbl_nm(x);
108 DROP TABLE obscure_tbl_nm;
113 foreach {tn analyze_cmd} {
120 execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT) }
121 for {set i 0} {$i < 100} {incr i} {
122 set c [expr int(pow(1.1,$i)/100)]
123 set b [expr 125 - int(pow(1.1,99-$i))/100]
124 execsql {INSERT INTO t1 VALUES($i, $b, $c)}
128 execsql { CREATE INDEX t1b ON t1(b) }
129 execsql { CREATE INDEX t1c ON t1(c) }
132 do_execsql_test 1.$tn.2.1 { SELECT count(*) FROM t1 WHERE b=31 } 1
133 do_execsql_test 1.$tn.2.2 { SELECT count(*) FROM t1 WHERE c=0 } 49
134 do_execsql_test 1.$tn.2.3 { SELECT count(*) FROM t1 WHERE b=125 } 49
135 do_execsql_test 1.$tn.2.4 { SELECT count(*) FROM t1 WHERE c=16 } 1
137 do_eqp_test 1.$tn.2.5 {
138 SELECT * FROM t1 WHERE b = 31 AND c = 0;
139 } {SEARCH TABLE t1 USING INDEX t1b (b=?)}
140 do_eqp_test 1.$tn.2.6 {
141 SELECT * FROM t1 WHERE b = 125 AND c = 16;
142 } {SEARCH TABLE t1 USING INDEX t1c (c=?)}
144 do_execsql_test 1.$tn.3.1 {
145 SELECT count(*) FROM t1 WHERE b BETWEEN 0 AND 50
147 do_execsql_test 1.$tn.3.2 {
148 SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 50
150 do_execsql_test 1.$tn.3.3 {
151 SELECT count(*) FROM t1 WHERE b BETWEEN 75 AND 125
153 do_execsql_test 1.$tn.3.4 {
154 SELECT count(*) FROM t1 WHERE c BETWEEN 75 AND 125
157 do_eqp_test 1.$tn.3.5 {
158 SELECT * FROM t1 WHERE b BETWEEN 0 AND 50 AND c BETWEEN 0 AND 50
159 } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
161 do_eqp_test 1.$tn.3.6 {
162 SELECT * FROM t1 WHERE b BETWEEN 75 AND 125 AND c BETWEEN 75 AND 125
163 } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}
165 do_eqp_test 1.$tn.3.7 {
166 SELECT * FROM t1 WHERE b BETWEEN +0 AND +50 AND c BETWEEN +0 AND +50
167 } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
169 do_eqp_test 1.$tn.3.8 {
171 WHERE b BETWEEN cast('0' AS int) AND cast('50.0' AS real)
172 AND c BETWEEN cast('0' AS numeric) AND cast('50.0' AS real)
173 } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
175 do_eqp_test 1.$tn.3.9 {
176 SELECT * FROM t1 WHERE b BETWEEN +75 AND +125 AND c BETWEEN +75 AND +125
177 } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}
179 do_eqp_test 1.$tn.3.10 {
181 WHERE b BETWEEN cast('75' AS int) AND cast('125.0' AS real)
182 AND c BETWEEN cast('75' AS numeric) AND cast('125.0' AS real)
183 } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}