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 } {/(SEARCH|SCAN) t2 USING INDEX t2a1 /}
164 do_test index6-2.3stat4 {
167 SELECT * FROM t2 WHERE a IS NOT NULL;
169 } {/(SEARCH|SCAN) t2 USING INDEX t2a1 /}
171 do_test index6-2.3stat4 {
174 SELECT * FROM t2 WHERE a IS NOT NULL AND a>0;
176 } {/(SEARCH|SCANE) t2 USING INDEX t2a1 /}
181 SELECT * FROM t2 WHERE a IS NULL;
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 t8b USING INDEX i8c (y=?) LEFT-JOIN
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/}
394 # Ticket https://www.sqlite.org/src/info/1d958d90596593a7
395 # NOT IN operator fails when using a partial index.
397 do_execsql_test index6-12.1 {
398 DROP TABLE IF EXISTS t1;
399 DROP TABLE IF EXISTS t2;
400 CREATE TABLE t1(a,b);
401 INSERT INTO t1 VALUES(1,1);
402 INSERT INTO t1 VALUES(2,2);
404 INSERT INTO t2 VALUES(1);
405 INSERT INTO t2 VALUES(2);
406 SELECT 'one', * FROM t2 WHERE x NOT IN (SELECT a FROM t1);
407 CREATE INDEX t1a ON t1(a) WHERE b=1;
408 SELECT 'two', * FROM t2 WHERE x NOT IN (SELECT a FROM t1);
410 do_execsql_test index6-12.2 {
411 SELECT x FROM t2 WHERE x IN (SELECT a FROM t1) ORDER BY +x;
415 # Ticket https://www.sqlite.org/src/tktview/5c6955204c392ae763a95
416 # Theorem prover error
418 do_execsql_test index6-13.1 {
419 DROP TABLE IF EXISTS t0;
421 CREATE INDEX index_0 ON t0(c0) WHERE c0 NOT NULL;
422 INSERT INTO t0(c0) VALUES (NULL);
423 SELECT * FROM t0 WHERE c0 OR 1;
427 # Ticket https://sqlite.org/src/tktview/8025674847
429 do_execsql_test index6-14.1 {
430 CREATE TABLE IF NOT EXISTS t0 (c0, c1);
431 CREATE INDEX IF NOT EXISTS i0 ON t0(c0, c1) WHERE c0 NOT NULL;
432 INSERT INTO t0(c0, c1) VALUES(NULL, 'row');
433 SELECT * FROM t0 WHERE t0.c0 IS NOT 1;
436 do_execsql_test index6-14.2 {
437 SELECT * FROM t0 WHERE CASE c0 WHEN 0 THEN 0 ELSE 1 END;
441 # Ticket https://www.sqlite.org/src/info/a6408d42b9f44462
442 # Ticket https://www.sqlite.org/src/info/fba33c8b1df6a915
443 # https://sqlite.org/src/info/bac716244fddac1fe841
445 do_execsql_test index6-15.1 {
448 INSERT INTO t0(c0) VALUES (NULL);
449 CREATE INDEX i0 ON t0(1) WHERE c0 NOT NULL;
450 SELECT 1 FROM t0 WHERE (t0.c0 IS FALSE) IS FALSE;
452 do_execsql_test index6-15.2 {
453 SELECT 1 FROM t0 WHERE (t0.c0 IS FALSE) BETWEEN FALSE AND TRUE;
455 do_execsql_test index6-15.3 {
456 SELECT 1 FROM t0 WHERE TRUE BETWEEN (t0.c0 IS FALSE) AND TRUE;
458 do_execsql_test index6-15.4 {
459 SELECT 1 FROM t0 WHERE FALSE BETWEEN FALSE AND (t0.c0 IS FALSE);
461 do_execsql_test index6-15.5 {
462 SELECT 1 FROM t0 WHERE (c0 IS FALSE) IN (FALSE);
466 # Ticket https://sqlite.org/src/info/767a8cbc6d20bd68
467 do_execsql_test index6-16.1 {
469 CREATE TABLE t0(c0 COLLATE NOCASE, c1);
470 CREATE INDEX i0 ON t0(0) WHERE c0 >= c1;
471 INSERT INTO t0 VALUES('a', 'B');
472 SELECT c1 <= c0, c0 >= c1 FROM t0;
474 do_execsql_test index6-16.2 {
475 SELECT 2 FROM t0 WHERE c0 >= c1;
477 do_execsql_test index6-16.3 {
478 SELECT 3 FROM t0 WHERE c1 <= c0;
482 # Ticket https://sqlite.org/src/tktview/a9efb42811fa41ee286e8
485 do_execsql_test index6-17.1 {
487 CREATE INDEX i0 ON t0(0) WHERE c0 GLOB c0;
488 INSERT INTO t0 VALUES (0);
489 CREATE UNIQUE INDEX i1 ON t0(0);
490 PRAGMA integrity_check;
492 do_execsql_test index6-17.2 {
493 CREATE UNIQUE INDEX i2 ON t0(0);
494 REPLACE INTO t0 VALUES(0);
495 PRAGMA integrity_check;
497 do_execsql_test index6-17.3 {
498 SELECT COUNT(*) FROM t0 WHERE t0.c0 GLOB t0.c0;
502 # Forum https://sqlite.org/forum/forumpost/d813704d7c
504 do_execsql_test index6-18.1 {
505 CREATE TABLE t1(a INT, b INT);
506 INSERT INTO t1 VALUES(10,10);
507 CREATE UNIQUE INDEX t1b ON t1(b) WHERE a>NULL;
508 SELECT * FROM t1 WHERE a IS NOT NULL;
512 # https://sqlite.org/forum/forumpost/c4676c4956
513 # Cannot do a scan of a partial index on the left table of a RIGHT JOIN
514 # since that will cause extra rows to appear in the output during the
515 # right-join no-match loop. The following testcase is verify using
519 do_execsql_test index6-19.1 {
520 CREATE TABLE t1(a INT, b INT);
521 INSERT INTO t1(a) VALUES(2);
522 CREATE TABLE t2(c INT);
523 CREATE INDEX i0 ON t2(c) WHERE c=3;
524 CREATE TABLE t3(d INT);
525 INSERT INTO t3 VALUES(1);
527 do_execsql_test index6-19.2 {
528 SELECT * FROM t2 RIGHT JOIN t3 ON d<>0 LEFT JOIN t1 ON c=3 WHERE t1.a<>0;