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 # Test cases for partial indices
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
24 load_static_extension db wholenumber;
26 # Able to parse and manage partial indices
28 CREATE TABLE t1(a,b,c);
29 CREATE INDEX t1a ON t1(a) WHERE a IS NOT NULL;
30 CREATE INDEX t1b ON t1(b) WHERE b>10;
31 CREATE VIRTUAL TABLE nums USING wholenumber;
33 SELECT CASE WHEN value%3!=0 THEN value END, value, value
34 FROM nums WHERE value<=20;
35 SELECT count(a), count(b) FROM t1;
36 PRAGMA integrity_check;
40 # Make sure the count(*) optimization works correctly with
41 # partial indices. Ticket [a5c8ed66cae16243be6] 2013-10-03.
43 do_execsql_test index6-1.1.1 {
44 SELECT count(*) FROM t1;
47 # Error conditions during parsing...
51 CREATE INDEX bad1 ON t1(a,b) WHERE x IS NOT NULL;
53 } {1 {no such column: x}}
56 CREATE INDEX bad1 ON t1(a,b) WHERE EXISTS(SELECT * FROM t1);
58 } {1 {subqueries prohibited in partial index WHERE clauses}}
61 CREATE INDEX bad1 ON t1(a,b) WHERE a!=?1;
63 } {1 {parameters prohibited in partial index WHERE clauses}}
66 CREATE INDEX bad1 ON t1(a,b) WHERE a!=random();
68 } {1 {non-deterministic functions prohibited in partial index WHERE clauses}}
71 CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%';
74 do_execsql_test index6-1.7 {
75 DROP INDEX IF EXISTS bad1;
81 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
82 PRAGMA integrity_check;
84 } {{} 20 t1a {14 1} t1b {10 1} ok}
86 # STAT1 shows the partial indices have a reduced number of
93 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
94 PRAGMA integrity_check;
96 } {{} 20 t1a {20 1} t1b {10 1} ok}
100 UPDATE t1 SET a=NULL WHERE b%3!=0;
101 UPDATE t1 SET b=b+100;
103 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
104 PRAGMA integrity_check;
106 } {{} 20 t1a {6 1} t1b {20 1} ok}
108 do_test index6-1.12 {
110 UPDATE t1 SET a=CASE WHEN b%3!=0 THEN b END;
111 UPDATE t1 SET b=b-100;
113 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
114 PRAGMA integrity_check;
116 } {{} 20 t1a {13 1} t1b {10 1} ok}
118 do_test index6-1.13 {
120 DELETE FROM t1 WHERE b BETWEEN 8 AND 12;
122 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
123 PRAGMA integrity_check;
125 } {{} 15 t1a {10 1} t1b {8 1} ok}
127 do_test index6-1.14 {
131 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
132 PRAGMA integrity_check;
134 } {{} 15 t1a {10 1} t1b {8 1} ok}
136 do_test index6-1.15 {
138 CREATE INDEX t1c ON t1(c);
140 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
141 PRAGMA integrity_check;
143 } {t1a {10 1} t1b {8 1} t1c {15 1} ok}
145 # Queries use partial indices as appropriate times.
149 CREATE TABLE t2(a,b);
150 INSERT INTO t2(a,b) SELECT value, value FROM nums WHERE value<1000;
151 UPDATE t2 SET a=NULL WHERE b%2==0;
152 CREATE INDEX t2a1 ON t2(a) WHERE a IS NOT NULL;
153 SELECT count(*) FROM t2 WHERE a IS NOT NULL;
159 SELECT * FROM t2 WHERE a=5;
161 } {/.* TABLE t2 USING INDEX t2a1 .*/}
162 ifcapable stat4||stat3 {
164 do_test index6-2.3stat4 {
167 SELECT * FROM t2 WHERE a IS NOT NULL;
169 } {/.* TABLE t2 USING INDEX t2a1 .*/}
171 do_test index6-2.3stat4 {
174 SELECT * FROM t2 WHERE a IS NOT NULL AND a>0;
176 } {/.* TABLE t2 USING INDEX t2a1 .*/}
181 SELECT * FROM t2 WHERE a IS NULL;
183 } {~/.*INDEX t2a1.*/}
185 do_execsql_test index6-2.101 {
187 UPDATE t2 SET a=b, b=b+10000;
188 SELECT b FROM t2 WHERE a=15;
190 do_execsql_test index6-2.102 {
191 CREATE INDEX t2a2 ON t2(a) WHERE a<100 OR a>200;
192 SELECT b FROM t2 WHERE a=15;
193 PRAGMA integrity_check;
195 do_execsql_test index6-2.102eqp {
197 SELECT b FROM t2 WHERE a=15;
198 } {~/.*INDEX t2a2.*/}
199 do_execsql_test index6-2.103 {
200 SELECT b FROM t2 WHERE a=15 AND a<100;
202 do_execsql_test index6-2.103eqp {
204 SELECT b FROM t2 WHERE a=15 AND a<100;
206 do_execsql_test index6-2.104 {
207 SELECT b FROM t2 WHERE a=515 AND a>200;
209 do_execsql_test index6-2.104eqp {
211 SELECT b FROM t2 WHERE a=515 AND a>200;
214 # Partial UNIQUE indices
216 do_execsql_test index6-3.1 {
217 CREATE TABLE t3(a,b);
218 INSERT INTO t3 SELECT value, value FROM nums WHERE value<200;
219 UPDATE t3 SET a=999 WHERE b%5!=0;
220 CREATE UNIQUE INDEX t3a ON t3(a) WHERE a<>999;
223 # unable to insert a duplicate row a-value that is not 999.
225 INSERT INTO t3(a,b) VALUES(150, 'test1');
227 } {1 {UNIQUE constraint failed: t3.a}}
229 # can insert multiple rows with a==999 because such rows are not
230 # part of the unique index.
232 INSERT INTO t3(a,b) VALUES(999, 'test1'), (999, 'test2');
235 do_execsql_test index6-3.4 {
236 SELECT count(*) FROM t3 WHERE a=999;
238 integrity_check index6-3.5
240 do_execsql_test index6-4.0 {
242 PRAGMA integrity_check;
245 # Silently ignore database name qualifiers in partial indices.
247 do_execsql_test index6-5.0 {
248 CREATE INDEX t3b ON t3(b) WHERE xyzzy.t3.b BETWEEN 5 AND 10;
249 /* ^^^^^-- ignored */
251 SELECT count(*) FROM t3 WHERE t3.b BETWEEN 5 AND 10;
252 SELECT stat+0 FROM sqlite_stat1 WHERE idx='t3b';
255 # Test case for ticket [2ea3e9fe6379fc3f6ce7e090ce483c1a3a80d6c9] from
256 # 2014-04-13: Partial index causes assertion fault on UPDATE OR REPLACE.
258 do_execsql_test index6-6.0 {
259 CREATE TABLE t6(a,b);
260 CREATE UNIQUE INDEX t6ab ON t1(a,b);
261 CREATE INDEX t6b ON t6(b) WHERE b=1;
262 INSERT INTO t6(a,b) VALUES(123,456);
265 do_execsql_test index6-6.1 {
266 UPDATE OR REPLACE t6 SET b=789;
269 do_execsql_test index6-6.2 {
270 PRAGMA integrity_check;
273 # Test case for ticket [2326c258d02ead33d69faa63de8f4686b9b1b9d9] on
274 # 2015-02-24. Any use of a partial index qualifying constraint inside
275 # the ON clause of a LEFT JOIN was causing incorrect results for all
276 # versions of SQLite 3.8.0 through 3.8.8.
278 do_execsql_test index6-7.0 {
281 INSERT INTO t7a(x) VALUES(1);
282 CREATE INDEX t7ax ON t7a(x) WHERE x=99;
283 PRAGMA automatic_index=OFF;
284 SELECT * FROM t7a LEFT JOIN t7b ON (x=99) ORDER BY x;
286 do_execsql_test index6-7.1 {
287 INSERT INTO t7b(y) VALUES(2);
288 SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x;
290 do_execsql_test index6-7.2 {
291 INSERT INTO t7a(x) VALUES(99);
292 SELECT * FROM t7a LEFT JOIN t7b ON (x=99) ORDER BY x;
294 do_execsql_test index6-7.3 {
295 SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x;
297 do_execsql_test index6-7.4 {
299 SELECT * FROM t7a JOIN t7b ON (x=99) ORDER BY x;
300 } {/USING COVERING INDEX t7ax/}
303 do_execsql_test index6-8.0 {
304 CREATE TABLE t8a(a,b);
305 CREATE TABLE t8b(x,y);
306 CREATE INDEX i8c ON t8b(y) WHERE x = 'value';
308 INSERT INTO t8a VALUES(1, 'one');
309 INSERT INTO t8a VALUES(2, 'two');
310 INSERT INTO t8a VALUES(3, 'three');
312 INSERT INTO t8b VALUES('value', 1);
313 INSERT INTO t8b VALUES('dummy', 2);
314 INSERT INTO t8b VALUES('value', 3);
315 INSERT INTO t8b VALUES('dummy', 4);
318 do_eqp_test index6-8.1 {
319 SELECT * FROM t8a LEFT JOIN t8b ON (x = 'value' AND y = a)
323 `--SEARCH TABLE t8b USING INDEX i8c (y=?)
326 do_execsql_test index6-8.2 {
327 SELECT * FROM t8a LEFT JOIN t8b ON (x = 'value' AND y = a)
334 # 2015-06-11. Assertion fault found by AFL
336 do_execsql_test index6-9.1 {
337 CREATE TABLE t9(a int, b int, c int);
338 CREATE INDEX t9ca ON t9(c,a) WHERE a in (10,12,20);
339 INSERT INTO t9 VALUES(1,1,9),(10,2,35),(11,15,82),(20,19,5),(NULL,7,3);
340 UPDATE t9 SET b=c WHERE a in (10,12,20);
341 SELECT a,b,c,'|' FROM t9 ORDER BY a;
342 } {{} 7 3 | 1 1 9 | 10 35 35 | 11 15 82 | 20 5 5 |}
343 do_execsql_test index6-9.2 {
345 CREATE TABLE t9(a int, b int, c int, PRIMARY KEY(a)) WITHOUT ROWID;
346 CREATE INDEX t9ca ON t9(c,a) WHERE a in (10,12,20);
347 INSERT INTO t9 VALUES(1,1,9),(10,2,35),(11,15,82),(20,19,5);
348 UPDATE t9 SET b=c WHERE a in (10,12,20);
349 SELECT a,b,c,'|' FROM t9 ORDER BY a;
350 } {1 1 9 | 10 35 35 | 11 15 82 | 20 5 5 |}
352 # AND-connected terms in the WHERE clause of a partial index
354 do_execsql_test index6-10.1 {
355 CREATE TABLE t10(a,b,c,d,e INTEGER PRIMARY KEY);
356 INSERT INTO t10 VALUES
361 CREATE INDEX t10x ON t10(d) WHERE a=1 AND b=2 AND c=3;
362 SELECT e FROM t10 WHERE a=1 AND b=2 AND c=3 ORDER BY d;
364 do_execsql_test index6-10.1eqp {
366 SELECT e FROM t10 WHERE a=1 AND b=2 AND c=3 ORDER BY d;
367 } {/USING INDEX t10x/}
368 do_execsql_test index6-10.2 {
369 SELECT e FROM t10 WHERE c=3 AND 2=b AND a=1 ORDER BY d DESC;
371 do_execsql_test index6-10.2eqp {
373 SELECT e FROM t10 WHERE c=3 AND 2=b AND a=1 ORDER BY d DESC;
374 } {/USING INDEX t10x/}
375 do_execsql_test index6-10.3 {
376 SELECT e FROM t10 WHERE a=1 AND b=2 ORDER BY d DESC;
378 do_execsql_test index6-10.3eqp {
380 SELECT e FROM t10 WHERE a=1 AND b=2 ORDER BY d DESC;
381 } {~/USING INDEX t10x/}
383 # A partial index will be used for a full table scan, where possible
384 do_execsql_test index6-11.1 {
385 CREATE TABLE t11(a,b,c);
386 CREATE INDEX t11x ON t11(a) WHERE b<>99;
387 EXPLAIN QUERY PLAN SELECT a FROM t11 WHERE b<>99;
388 } {/USING INDEX t11x/}
389 do_execsql_test index6-11.2 {
390 EXPLAIN QUERY PLAN SELECT a FROM t11 WHERE b<>99 AND c<>98;
391 } {/USING INDEX t11x/}