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 #***********************************************************************
11 # This file implements regression tests for SQLite library.
12 # This file implements tests for the ANALYZE command.
14 # $Id: analyze.test,v 1.9 2008/08/11 18:44:58 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # There is nothing to test if ANALYZE is disable for this build.
21 ifcapable {!analyze} {
26 # Basic sanity checks.
32 } {1 {no such table: no_such_table}}
35 SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
40 ANALYZE no_such_db.no_such_table
42 } {1 {unknown database no_such_db}}
45 SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
48 do_test analyze-1.5.1 {
53 do_test analyze-1.5.2 {
55 PRAGMA empty_result_callbacks=1;
61 SELECT count(*) FROM sqlite_master WHERE name='sqlite_stat1'
64 do_test analyze-1.6.2 {
66 CREATE INDEX stat1idx ON sqlite_stat1(idx);
68 } {1 {table sqlite_stat1 may not be indexed}}
69 do_test analyze-1.6.3 {
71 CREATE INDEX main.stat1idx ON SQLite_stat1(idx);
73 } {1 {table sqlite_stat1 may not be indexed}}
76 SELECT * FROM sqlite_stat1 WHERE idx NOT NULL
86 SELECT * FROM sqlite_stat1 WHERE idx NOT NULL
89 do_test analyze-1.10 {
95 do_test analyze-1.11 {
97 SELECT * FROM sqlite_stat1
100 do_test analyze-1.12 {
105 do_test analyze-1.13 {
107 SELECT * FROM sqlite_stat1
111 # Create some indices that can be analyzed. But do not yet add
112 # data. Without data in the tables, no analysis is done.
114 do_test analyze-2.1 {
116 CREATE INDEX t1i1 ON t1(a);
118 SELECT * FROM sqlite_stat1 ORDER BY idx;
121 do_test analyze-2.2 {
123 CREATE INDEX t1i2 ON t1(b);
125 SELECT * FROM sqlite_stat1 ORDER BY idx;
128 do_test analyze-2.3 {
130 CREATE INDEX t1i3 ON t1(a,b);
132 SELECT * FROM sqlite_stat1 ORDER BY idx;
136 # Start adding data to the table. Verify that the analysis
139 do_test analyze-3.1 {
141 INSERT INTO t1 VALUES(1,2);
142 INSERT INTO t1 VALUES(1,3);
144 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
146 } {t1i1 {2 2} t1i2 {2 1} t1i3 {2 2 1}}
147 do_test analyze-3.2 {
149 INSERT INTO t1 VALUES(1,4);
150 INSERT INTO t1 VALUES(1,5);
152 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
154 } {t1i1 {4 4} t1i2 {4 1} t1i3 {4 4 1}}
155 do_test analyze-3.3 {
157 INSERT INTO t1 VALUES(2,5);
159 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
161 } {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1}}
162 do_test analyze-3.4 {
164 CREATE TABLE t2 AS SELECT * FROM t1;
165 CREATE INDEX t2i1 ON t2(a);
166 CREATE INDEX t2i2 ON t2(b);
167 CREATE INDEX t2i3 ON t2(a,b);
169 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
171 } {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2} t2i3 {5 3 1}}
172 do_test analyze-3.5 {
176 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
178 } {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2}}
179 do_test analyze-3.6 {
182 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
184 } {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3} t2i2 {5 2}}
185 do_test analyze-3.7 {
189 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
191 } {t1i1 {5 3} t1i2 {5 2} t1i3 {5 3 1} t2i1 {5 3}}
192 do_test analyze-3.8 {
194 CREATE TABLE t3 AS SELECT a, b, rowid AS c, 'hi' AS d FROM t1;
195 CREATE INDEX t3i1 ON t3(a);
196 CREATE INDEX t3i2 ON t3(a,b,c,d);
197 CREATE INDEX t3i3 ON t3(d,b,c,a);
200 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
203 do_test analyze-3.9 {
206 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
208 } {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
210 do_test analyze-3.10 {
212 CREATE TABLE [silly " name](a, b, c);
213 CREATE INDEX 'foolish '' name' ON [silly " name](a, b);
214 CREATE INDEX 'another foolish '' name' ON [silly " name](c);
215 INSERT INTO [silly " name] VALUES(1, 2, 3);
216 INSERT INTO [silly " name] VALUES(4, 5, 6);
218 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
220 } {{another foolish ' name} {2 1} {foolish ' name} {2 1 1} t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
221 do_test analyze-3.11 {
223 DROP INDEX "foolish ' name";
224 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
226 } {{another foolish ' name} {2 1} t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
227 do_test analyze-3.11 {
229 DROP TABLE "silly "" name";
230 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
232 } {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1}}
234 # Try corrupting the sqlite_stat1 table and make sure the
235 # database is still able to function.
237 do_test analyze-4.0 {
240 CREATE TABLE t4(x,y,z);
241 CREATE INDEX t4i1 ON t4(x);
242 CREATE INDEX t4i2 ON t4(y);
243 INSERT INTO t4 SELECT a,b,c FROM t3;
250 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
252 } {t3i1 {5 3} t3i2 {5 3 1 1 1} t3i3 {5 5 2 1 1} t4i1 {5 3} t4i2 {5 2}}
253 do_test analyze-4.1 {
255 PRAGMA writable_schema=on;
256 INSERT INTO sqlite_stat1 VALUES(null,null,null);
257 PRAGMA writable_schema=off;
262 SELECT * FROM t4 WHERE x=1234;
265 do_test analyze-4.2 {
267 PRAGMA writable_schema=on;
268 DELETE FROM sqlite_stat1;
269 INSERT INTO sqlite_stat1 VALUES('t4','t4i1','nonsense');
270 INSERT INTO sqlite_stat1 VALUES('t4','t4i2','120897349817238741092873198273409187234918720394817209384710928374109827172901827349871928741910');
271 PRAGMA writable_schema=off;
276 SELECT * FROM t4 WHERE x=1234;
279 do_test analyze-4.3 {
281 INSERT INTO sqlite_stat1 VALUES('t4','xyzzy','0 1 2 3');
286 SELECT * FROM t4 WHERE x=1234;
290 # Verify that DROP TABLE and DROP INDEX remove entries from the
291 # sqlite_stat1, sqlite_stat3 and sqlite_stat4 tables.
293 do_test analyze-5.0 {
297 INSERT INTO t3 VALUES(1,2,3,4);
298 INSERT INTO t3 VALUES(5,6,7,8);
299 INSERT INTO t3 SELECT a+8, b+8, c+8, d+8 FROM t3;
300 INSERT INTO t3 SELECT a+16, b+16, c+16, d+16 FROM t3;
301 INSERT INTO t3 SELECT a+32, b+32, c+32, d+32 FROM t3;
302 INSERT INTO t3 SELECT a+64, b+64, c+64, d+64 FROM t3;
303 INSERT INTO t4 SELECT a, b, c FROM t3;
305 SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
306 SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
308 } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
309 ifcapable stat4||stat3 {
310 ifcapable stat4 {set stat sqlite_stat4} else {set stat sqlite_stat3}
311 do_test analyze-5.1 {
313 SELECT DISTINCT idx FROM $stat ORDER BY 1;
314 SELECT DISTINCT tbl FROM $stat ORDER BY 1;
316 } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
318 do_test analyze-5.2 {
321 SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
322 SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
324 } {t3i1 t3i3 t4i1 t4i2 t3 t4}
325 ifcapable stat4||stat3 {
326 do_test analyze-5.3 {
328 SELECT DISTINCT idx FROM $stat ORDER BY 1;
329 SELECT DISTINCT tbl FROM $stat ORDER BY 1;
331 } {t3i1 t3i3 t4i1 t4i2 t3 t4}
333 do_test analyze-5.4 {
336 SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
337 SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
340 ifcapable stat4||stat3 {
341 do_test analyze-5.5 {
343 SELECT DISTINCT idx FROM $stat ORDER BY 1;
344 SELECT DISTINCT tbl FROM $stat ORDER BY 1;
349 # This test corrupts the database file so it must be the last test
352 do_test analyze-99.1 {
354 PRAGMA writable_schema=on;
355 UPDATE sqlite_master SET sql='nonsense' WHERE name='sqlite_stat1';
358 catch { sqlite3 db test.db }
362 } {1 {malformed database schema (sqlite_stat1)}}