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 {functions prohibited in partial index WHERE clauses}}
71 CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%';
73 } {1 {functions prohibited in partial index WHERE clauses}}
78 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
79 PRAGMA integrity_check;
81 } {{} 20 t1a {14 1} t1b {10 1} ok}
83 # STAT1 shows the partial indices have a reduced number of
90 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
91 PRAGMA integrity_check;
93 } {{} 20 t1a {20 1} t1b {10 1} ok}
97 UPDATE t1 SET a=NULL WHERE b%3!=0;
98 UPDATE t1 SET b=b+100;
100 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
101 PRAGMA integrity_check;
103 } {{} 20 t1a {6 1} t1b {20 1} ok}
105 do_test index6-1.12 {
107 UPDATE t1 SET a=CASE WHEN b%3!=0 THEN b END;
108 UPDATE t1 SET b=b-100;
110 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
111 PRAGMA integrity_check;
113 } {{} 20 t1a {13 1} t1b {10 1} ok}
115 do_test index6-1.13 {
117 DELETE FROM t1 WHERE b BETWEEN 8 AND 12;
119 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
120 PRAGMA integrity_check;
122 } {{} 15 t1a {10 1} t1b {8 1} ok}
124 do_test index6-1.14 {
128 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
129 PRAGMA integrity_check;
131 } {{} 15 t1a {10 1} t1b {8 1} ok}
133 do_test index6-1.15 {
135 CREATE INDEX t1c ON t1(c);
137 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
138 PRAGMA integrity_check;
140 } {t1a {10 1} t1b {8 1} t1c {15 1} ok}
142 # Queries use partial indices as appropriate times.
146 CREATE TABLE t2(a,b);
147 INSERT INTO t2(a,b) SELECT value, value FROM nums WHERE value<1000;
148 UPDATE t2 SET a=NULL WHERE b%2==0;
149 CREATE INDEX t2a1 ON t2(a) WHERE a IS NOT NULL;
150 SELECT count(*) FROM t2 WHERE a IS NOT NULL;
156 SELECT * FROM t2 WHERE a=5;
158 } {/.* TABLE t2 USING INDEX t2a1 .*/}
159 ifcapable stat4||stat3 {
161 do_test index6-2.3stat4 {
164 SELECT * FROM t2 WHERE a IS NOT NULL;
166 } {/.* TABLE t2 USING INDEX t2a1 .*/}
168 do_test index6-2.3stat4 {
171 SELECT * FROM t2 WHERE a IS NOT NULL AND a>0;
173 } {/.* TABLE t2 USING INDEX t2a1 .*/}
178 SELECT * FROM t2 WHERE a IS NULL;
180 } {~/.*INDEX t2a1.*/}
182 do_execsql_test index6-2.101 {
184 UPDATE t2 SET a=b, b=b+10000;
185 SELECT b FROM t2 WHERE a=15;
187 do_execsql_test index6-2.102 {
188 CREATE INDEX t2a2 ON t2(a) WHERE a<100 OR a>200;
189 SELECT b FROM t2 WHERE a=15;
190 PRAGMA integrity_check;
192 do_execsql_test index6-2.102eqp {
194 SELECT b FROM t2 WHERE a=15;
195 } {~/.*INDEX t2a2.*/}
196 do_execsql_test index6-2.103 {
197 SELECT b FROM t2 WHERE a=15 AND a<100;
199 do_execsql_test index6-2.103eqp {
201 SELECT b FROM t2 WHERE a=15 AND a<100;
203 do_execsql_test index6-2.104 {
204 SELECT b FROM t2 WHERE a=515 AND a>200;
206 do_execsql_test index6-2.104eqp {
208 SELECT b FROM t2 WHERE a=515 AND a>200;
211 # Partial UNIQUE indices
213 do_execsql_test index6-3.1 {
214 CREATE TABLE t3(a,b);
215 INSERT INTO t3 SELECT value, value FROM nums WHERE value<200;
216 UPDATE t3 SET a=999 WHERE b%5!=0;
217 CREATE UNIQUE INDEX t3a ON t3(a) WHERE a<>999;
220 # unable to insert a duplicate row a-value that is not 999.
222 INSERT INTO t3(a,b) VALUES(150, 'test1');
224 } {1 {UNIQUE constraint failed: t3.a}}
226 # can insert multiple rows with a==999 because such rows are not
227 # part of the unique index.
229 INSERT INTO t3(a,b) VALUES(999, 'test1'), (999, 'test2');
232 do_execsql_test index6-3.4 {
233 SELECT count(*) FROM t3 WHERE a=999;
235 integrity_check index6-3.5
237 do_execsql_test index6-4.0 {
239 PRAGMA integrity_check;
242 # Silently ignore database name qualifiers in partial indices.
244 do_execsql_test index6-5.0 {
245 CREATE INDEX t3b ON t3(b) WHERE xyzzy.t3.b BETWEEN 5 AND 10;
246 /* ^^^^^-- ignored */
248 SELECT count(*) FROM t3 WHERE t3.b BETWEEN 5 AND 10;
249 SELECT stat+0 FROM sqlite_stat1 WHERE idx='t3b';
252 # Test case for ticket [2ea3e9fe6379fc3f6ce7e090ce483c1a3a80d6c9] from
253 # 2014-04-13: Partial index causes assertion fault on UPDATE OR REPLACE.
255 do_execsql_test index6-6.0 {
256 CREATE TABLE t6(a,b);
257 CREATE UNIQUE INDEX t6ab ON t1(a,b);
258 CREATE INDEX t6b ON t6(b) WHERE b=1;
259 INSERT INTO t6(a,b) VALUES(123,456);
262 do_execsql_test index6-6.1 {
263 UPDATE OR REPLACE t6 SET b=789;
266 do_execsql_test index6-6.2 {
267 PRAGMA integrity_check;