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 in WITHOUT ROWID tables
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
24 # Capture the output of a pragma in a TEMP table.
26 proc capture_pragma {db tabname sql} {
27 $db eval "DROP TABLE IF EXISTS temp.$tabname"
32 set ins "INSERT INTO $tabname VALUES"
33 set crtab "CREATE TEMP TABLE $tabname "
36 append ins ${sep}\$x($col)
37 append crtab ${sep}\"$col\"
49 load_static_extension db wholenumber;
51 # Able to parse and manage partial indices
53 CREATE TABLE t1(a,b,c PRIMARY KEY) WITHOUT rowid;
54 CREATE INDEX t1a ON t1(a) WHERE a IS NOT NULL;
55 CREATE INDEX t1b ON t1(b) WHERE b>10;
56 CREATE VIRTUAL TABLE nums USING wholenumber;
58 SELECT CASE WHEN value%3!=0 THEN value END, value, value
59 FROM nums WHERE value<=20;
60 SELECT count(a), count(b) FROM t1;
61 PRAGMA integrity_check;
65 # (The "partial" column of the PRAGMA index_list output is...)
66 # EVIDENCE-OF: R-34457-09668 "1" if the index is a partial index and "0"
70 capture_pragma db out {PRAGMA index_list(t1)}
71 db eval {SELECT "name", "partial", '|' FROM out ORDER BY "name"}
72 } {sqlite_autoindex_t1_1 0 | t1a 1 | t1b 1 |}
74 # Make sure the count(*) optimization works correctly with
75 # partial indices. Ticket [a5c8ed66cae16243be6] 2013-10-03.
77 do_execsql_test index7-1.1.1 {
78 SELECT count(*) FROM t1;
81 # Error conditions during parsing...
85 CREATE INDEX bad1 ON t1(a,b) WHERE x IS NOT NULL;
87 } {1 {no such column: x}}
90 CREATE INDEX bad1 ON t1(a,b) WHERE EXISTS(SELECT * FROM t1);
92 } {1 {subqueries prohibited in partial index WHERE clauses}}
95 CREATE INDEX bad1 ON t1(a,b) WHERE a!=?1;
97 } {1 {parameters prohibited in partial index WHERE clauses}}
100 CREATE INDEX bad1 ON t1(a,b) WHERE a!=random();
102 } {1 {non-deterministic functions prohibited in partial index WHERE clauses}}
105 CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%';
108 do_execsql_test index7-1.7 {
109 INSERT INTO t1(a,b,c)
110 VALUES('abcde',1,101),('abdef',2,102),('xyz',3,103),('abcz',4,104);
111 SELECT c FROM t1 WHERE a NOT LIKE 'abc%' AND a=7 ORDER BY +b;
113 do_execsql_test index7-1.7eqp {
115 SELECT b FROM t1 WHERE a NOT LIKE 'abc%' AND a=7 ORDER BY +b;
116 } {/SEARCH TABLE t1 USING COVERING INDEX bad1 /}
117 do_execsql_test index7-1.8 {
118 DELETE FROM t1 WHERE c>=101;
119 DROP INDEX IF EXISTS bad1;
122 do_test index7-1.10 {
125 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
126 PRAGMA integrity_check;
128 } {t1 {20 1} t1a {14 1} t1b {10 1} ok}
130 # STAT1 shows the partial indices have a reduced number of
133 do_test index7-1.11 {
137 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
138 PRAGMA integrity_check;
140 } {t1 {20 1} t1a {20 1} t1b {10 1} ok}
142 do_test index7-1.11b {
144 UPDATE t1 SET a=NULL WHERE b%3!=0;
145 UPDATE t1 SET b=b+100;
147 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
148 PRAGMA integrity_check;
150 } {t1 {20 1} t1a {6 1} t1b {20 1} ok}
152 do_test index7-1.12 {
154 UPDATE t1 SET a=CASE WHEN b%3!=0 THEN b END;
155 UPDATE t1 SET b=b-100;
157 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
158 PRAGMA integrity_check;
160 } {t1 {20 1} t1a {13 1} t1b {10 1} ok}
162 do_test index7-1.13 {
164 DELETE FROM t1 WHERE b BETWEEN 8 AND 12;
166 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
167 PRAGMA integrity_check;
169 } {t1 {15 1} t1a {10 1} t1b {8 1} ok}
171 do_test index7-1.14 {
175 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
176 PRAGMA integrity_check;
178 } {t1 {15 1} t1a {10 1} t1b {8 1} ok}
180 do_test index7-1.15 {
182 CREATE INDEX t1c ON t1(c);
184 SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
185 PRAGMA integrity_check;
187 } {t1 {15 1} t1a {10 1} t1b {8 1} t1c {15 1} ok}
189 # Queries use partial indices as appropriate times.
193 CREATE TABLE t2(a,b PRIMARY KEY) without rowid;
194 INSERT INTO t2(a,b) SELECT value, value FROM nums WHERE value<1000;
195 UPDATE t2 SET a=NULL WHERE b%5==0;
196 CREATE INDEX t2a1 ON t2(a) WHERE a IS NOT NULL;
197 SELECT count(*) FROM t2 WHERE a IS NOT NULL;
203 SELECT * FROM t2 WHERE a=5;
205 } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/}
206 ifcapable stat4||stat3 {
207 do_test index7-2.3stat4 {
210 SELECT * FROM t2 WHERE a IS NOT NULL;
212 } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/}
214 do_test index7-2.3stat4 {
217 SELECT * FROM t2 WHERE a IS NOT NULL AND a>0;
219 } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/}
224 SELECT * FROM t2 WHERE a IS NULL;
226 } {~/.*INDEX t2a1.*/}
228 do_execsql_test index7-2.101 {
230 UPDATE t2 SET a=b, b=b+10000;
231 SELECT b FROM t2 WHERE a=15;
233 do_execsql_test index7-2.102 {
234 CREATE INDEX t2a2 ON t2(a) WHERE a<100 OR a>200;
235 SELECT b FROM t2 WHERE a=15;
236 PRAGMA integrity_check;
238 do_execsql_test index7-2.102eqp {
240 SELECT b FROM t2 WHERE a=15;
241 } {~/.*INDEX t2a2.*/}
242 do_execsql_test index7-2.103 {
243 SELECT b FROM t2 WHERE a=15 AND a<100;
245 do_execsql_test index7-2.103eqp {
247 SELECT b FROM t2 WHERE a=15 AND a<100;
249 do_execsql_test index7-2.104 {
250 SELECT b FROM t2 WHERE a=515 AND a>200;
252 do_execsql_test index7-2.104eqp {
254 SELECT b FROM t2 WHERE a=515 AND a>200;
257 # Partial UNIQUE indices
259 do_execsql_test index7-3.1 {
260 CREATE TABLE t3(a,b PRIMARY KEY) without rowid;
261 INSERT INTO t3 SELECT value, value FROM nums WHERE value<200;
262 UPDATE t3 SET a=999 WHERE b%5!=0;
263 CREATE UNIQUE INDEX t3a ON t3(a) WHERE a<>999;
266 # unable to insert a duplicate row a-value that is not 999.
268 INSERT INTO t3(a,b) VALUES(150, 'test1');
270 } {1 {UNIQUE constraint failed: t3.a}}
272 # can insert multiple rows with a==999 because such rows are not
273 # part of the unique index.
275 INSERT INTO t3(a,b) VALUES(999, 'test1'), (999, 'test2');
278 do_execsql_test index7-3.4 {
279 SELECT count(*) FROM t3 WHERE a=999;
281 integrity_check index7-3.5
283 do_execsql_test index7-4.0 {
285 PRAGMA integrity_check;
288 # Silently ignore database name qualifiers in partial indices.
290 do_execsql_test index7-5.0 {
291 CREATE INDEX t3b ON t3(b) WHERE xyzzy.t3.b BETWEEN 5 AND 10;
292 /* ^^^^^-- ignored */
294 SELECT count(*) FROM t3 WHERE t3.b BETWEEN 5 AND 10;
295 SELECT stat+0 FROM sqlite_stat1 WHERE idx='t3b';
298 # Verify that the problem identified by ticket [98d973b8f5] has been fixed.
300 do_execsql_test index7-6.1 {
301 CREATE TABLE t5(a, b);
302 CREATE TABLE t4(c, d);
303 INSERT INTO t5 VALUES(1, 'xyz');
304 INSERT INTO t4 VALUES('abc', 'not xyz');
305 SELECT * FROM (SELECT * FROM t5 WHERE a=1 AND b='xyz'), t4 WHERE c='abc';
309 do_execsql_test index7-6.2 {
310 CREATE INDEX i4 ON t4(c) WHERE d='xyz';
311 SELECT * FROM (SELECT * FROM t5 WHERE a=1 AND b='xyz'), t4 WHERE c='abc';
315 do_execsql_test index7-6.3 {
316 CREATE VIEW v4 AS SELECT * FROM t4;
317 INSERT INTO t4 VALUES('def', 'xyz');
318 SELECT * FROM v4 WHERE d='xyz' AND c='def'
322 do_eqp_test index7-6.4 {
323 SELECT * FROM v4 WHERE d='xyz' AND c='def'
325 0 0 0 {SEARCH TABLE t4 USING INDEX i4 (c=?)}
327 do_catchsql_test index7-6.5 {
328 CREATE INDEX t5a ON t5(a) WHERE a=#1;
329 } {1 {near "#1": syntax error}}