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. The
12 # focus of this file is testing that an index may be used as a covering
13 # index when there are OR expressions in the WHERE clause.
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 set ::testprefix whereD
22 CREATE TABLE t(i,j,k,m,n);
23 CREATE INDEX ijk ON t(i,j,k);
24 CREATE INDEX jmn ON t(j,m,n);
26 INSERT INTO t VALUES(3, 3, 'three', 3, 'tres');
27 INSERT INTO t VALUES(2, 2, 'two', 2, 'dos');
28 INSERT INTO t VALUES(1, 1, 'one', 1, 'uno');
29 INSERT INTO t VALUES(4, 4, 'four', 4, 'cuatro');
33 SELECT k FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2);
36 SELECT k FROM t WHERE (i=1 AND j=1) OR (+i=2 AND j=2);
39 SELECT n FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2);
42 SELECT k, n FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2);
45 SELECT k FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2) OR (i=3 AND j=3);
48 SELECT n FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2) OR (i=3 AND j=3);
51 SELECT k FROM t WHERE (i=1 AND j=1) OR (j=2 AND m=2);
54 SELECT k FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2) OR (j=3 AND m=3);
56 do_execsql_test 1.10 {
57 SELECT n FROM t WHERE (i=1 AND j=1) OR (i=2 AND j=2) OR (j=3 AND m=3);
59 do_execsql_test 1.11 {
60 SELECT k FROM t WHERE (i=1 AND j=1) OR (j=2 AND m=2) OR (i=3 AND j=3);
62 do_execsql_test 1.12 {
63 SELECT n FROM t WHERE (i=1 AND j=1) OR (j=2 AND m=2) OR (i=3 AND j=3);
65 do_execsql_test 1.13 {
66 SELECT k FROM t WHERE (j=1 AND m=1) OR (i=2 AND j=2) OR (i=3 AND j=3);
68 do_execsql_test 1.14 {
69 SELECT k FROM t WHERE (i=1 AND j=1) OR (j=2 AND i=2) OR (i=3 AND j=3);
71 do_execsql_test 1.15 {
72 SELECT k FROM t WHERE (i=1 AND j=2) OR (i=2 AND j=1) OR (i=3 AND j=4);
74 do_execsql_test 1.16 {
75 SELECT k FROM t WHERE (i=1 AND (j=1 or j=2)) OR (i=3 AND j=3);
79 CREATE TABLE t1(a,b,c,d);
80 CREATE INDEX t1b ON t1(b);
81 CREATE INDEX t1c ON t1(c);
82 CREATE INDEX t1d ON t1(d);
84 CREATE INDEX t2y ON t2(y);
86 INSERT INTO t1 VALUES(1,2,3,4);
87 INSERT INTO t1 VALUES(5,6,7,8);
88 INSERT INTO t2 VALUES(1,2);
89 INSERT INTO t2 VALUES(2,7);
90 INSERT INTO t2 VALUES(3,4);
93 SELECT a, x FROM t1 JOIN t2 ON +y=d OR x=7 ORDER BY a, x;
96 SELECT a, x FROM t1 JOIN t2 ON y=d OR x=7 ORDER BY a, x;
100 # Similar to [do_execsql_test], except that two elements are appended
101 # to the result - the string "search" and the number of times test variable
102 # sqlite3_search_count is incremented by running the supplied SQL. e.g.
104 # do_searchcount_test 1.0 { SELECT * FROM t1 } {x y search 2}
106 proc do_searchcount_test {tn sql res} {
107 uplevel [subst -nocommands {
109 set ::sqlite_search_count 0
110 concat [db eval {$sql}] search [set ::sqlite_search_count]
115 do_execsql_test 3.0 {
116 CREATE TABLE t3(a, b, c);
117 CREATE UNIQUE INDEX i3 ON t3(a, b);
118 INSERT INTO t3 VALUES(1, 'one', 'i');
119 INSERT INTO t3 VALUES(3, 'three', 'iii');
120 INSERT INTO t3 VALUES(6, 'six', 'vi');
121 INSERT INTO t3 VALUES(2, 'two', 'ii');
122 INSERT INTO t3 VALUES(4, 'four', 'iv');
123 INSERT INTO t3 VALUES(5, 'five', 'v');
125 CREATE TABLE t4(x PRIMARY KEY, y);
126 INSERT INTO t4 VALUES('a', 'one');
127 INSERT INTO t4 VALUES('b', 'two');
130 do_searchcount_test 3.1 {
131 SELECT a, b FROM t3 WHERE (a=1 AND b='one') OR (a=2 AND b='two')
132 } {1 one 2 two search 4}
134 do_searchcount_test 3.2 {
135 SELECT a, c FROM t3 WHERE (a=1 AND b='one') OR (a=2 AND b='two')
136 } {1 i 2 ii search 6}
138 do_searchcount_test 3.4.1 {
139 SELECT y FROM t4 WHERE x='a'
141 do_searchcount_test 3.4.2 {
142 SELECT a, b FROM t3 WHERE
143 (a=1 AND b=(SELECT y FROM t4 WHERE x='a'))
145 } {1 one 2 two search 6}
146 do_searchcount_test 3.4.3 {
147 SELECT a, b FROM t3 WHERE
149 OR (a=1 AND b=(SELECT y FROM t4 WHERE x='a'))
150 } {2 two 1 one search 6}
151 do_searchcount_test 3.4.4 {
152 SELECT a, b FROM t3 WHERE
153 (a=2 AND b=(SELECT y FROM t4 WHERE x='b'))
154 OR (a=1 AND b=(SELECT y FROM t4 WHERE x='a'))
155 } {2 two 1 one search 8}
157 do_searchcount_test 3.5.1 {
158 SELECT a, b FROM t3 WHERE (a=1 AND b='one') OR rowid=4
159 } {1 one 2 two search 2}
160 do_searchcount_test 3.5.2 {
161 SELECT a, c FROM t3 WHERE (a=1 AND b='one') OR rowid=4
162 } {1 i 2 ii search 3}
164 # Ticket [d02e1406a58ea02d] (2012-10-04)
165 # LEFT JOIN with an OR in the ON clause causes segfault
169 CREATE TABLE t41(a,b,c);
170 INSERT INTO t41 VALUES(1,2,3), (4,5,6);
171 CREATE TABLE t42(d,e,f);
172 INSERT INTO t42 VALUES(3,6,9), (4,8,12);
173 SELECT * FROM t41 AS x LEFT JOIN t42 AS y ON (y.d=x.c) OR (y.e=x.b);
175 } {1 2 3 3 6 9 4 5 6 {} {} {}}
178 CREATE INDEX t42d ON t42(d);
179 CREATE INDEX t42e ON t42(e);
180 SELECT * FROM t41 AS x LEFT JOIN t42 AS y ON (y.d=x.c) OR (y.e=x.b);
182 } {1 2 3 3 6 9 4 5 6 {} {} {}}
185 SELECT * FROM t41 AS x LEFT JOIN t42 AS y ON (y.d=x.c) OR (y.d=x.b);
187 } {1 2 3 3 6 9 4 5 6 {} {} {}}
189 # Ticket [bc1aea7b725f276177]
190 # Incorrect result on LEFT JOIN with OR constraints and an ORDER BY clause.
192 do_execsql_test 4.4 {
193 CREATE TABLE t44(a INTEGER, b INTEGER);
194 INSERT INTO t44 VALUES(1,2);
195 INSERT INTO t44 VALUES(3,4);
198 LEFT JOIN (SELECT a AS c, b AS d FROM t44) AS y ON a=c
199 WHERE d=4 OR d IS NULL;
201 do_execsql_test 4.5 {
204 LEFT JOIN (SELECT a AS c, b AS d FROM t44) AS y ON a=c
205 WHERE d=4 OR d IS NULL
208 do_execsql_test 4.6 {
209 CREATE TABLE t46(c INTEGER, d INTEGER);
210 INSERT INTO t46 SELECT a, b FROM t44;
211 SELECT * FROM t44 LEFT JOIN t46 ON a=c
212 WHERE d=4 OR d IS NULL;
214 do_execsql_test 4.7 {
215 SELECT * FROM t44 LEFT JOIN t46 ON a=c
216 WHERE d=4 OR d IS NULL
220 # Verify fix of a bug reported on the mailing list by Peter Reid
222 do_execsql_test 5.1 {
223 DROP TABLE IF EXISTS t;
224 CREATE TABLE t(c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17);
225 CREATE INDEX tc0 ON t(c0);
226 CREATE INDEX tc1 ON t(c1);
227 CREATE INDEX tc2 ON t(c2);
228 CREATE INDEX tc3 ON t(c3);
229 CREATE INDEX tc4 ON t(c4);
230 CREATE INDEX tc5 ON t(c5);
231 CREATE INDEX tc6 ON t(c6);
232 CREATE INDEX tc7 ON t(c7);
233 CREATE INDEX tc8 ON t(c8);
234 CREATE INDEX tc9 ON t(c9);
235 CREATE INDEX tc10 ON t(c10);
236 CREATE INDEX tc11 ON t(c11);
237 CREATE INDEX tc12 ON t(c12);
238 CREATE INDEX tc13 ON t(c13);
239 CREATE INDEX tc14 ON t(c14);
240 CREATE INDEX tc15 ON t(c15);
241 CREATE INDEX tc16 ON t(c16);
242 CREATE INDEX tc17 ON t(c17);
244 INSERT INTO t(c0, c16) VALUES (1,1);
246 SELECT * FROM t WHERE
247 c0=1 or c1=1 or c2=1 or c3=1 or
248 c4=1 or c5=1 or c6=1 or c7=1 or
249 c8=1 or c9=1 or c10=1 or c11=1 or
250 c12=1 or c13=1 or c14=1 or c15=1 or
252 } {1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} 1 {}}
253 do_execsql_test 5.2 {
255 INSERT INTO t(c0,c17) VALUES(1,1);
256 SELECT * FROM t WHERE
257 c0=1 or c1=1 or c2=1 or c3=1 or
258 c4=1 or c5=1 or c6=1 or c7=1 or
259 c8=1 or c9=1 or c10=1 or c11=1 or
260 c12=1 or c13=1 or c14=1 or c15=1 or
262 } {1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} 1}
263 do_execsql_test 5.3 {
265 INSERT INTO t(c0,c15) VALUES(1,1);
266 SELECT * FROM t WHERE
267 c0=1 or c1=1 or c2=1 or c3=1 or
268 c4=1 or c5=1 or c6=1 or c7=1 or
269 c8=1 or c9=1 or c10=1 or c11=1 or
270 c12=1 or c13=1 or c14=1 or c15=1 or
272 } {1 {} {} {} {} {} {} {} {} {} {} {} {} {} {} 1 {} {}}
274 #-------------------------------------------------------------------------
275 do_execsql_test 6.1 {
276 CREATE TABLE x1(a, b, c, d, e);
277 CREATE INDEX x1a ON x1(a);
278 CREATE INDEX x1bc ON x1(b, c);
279 CREATE INDEX x1cd ON x1(c, d);
281 INSERT INTO x1 VALUES(1, 2, 3, 4, 'A');
282 INSERT INTO x1 VALUES(5, 6, 7, 8, 'B');
283 INSERT INTO x1 VALUES(9, 10, 11, 12, 'C');
284 INSERT INTO x1 VALUES(13, 14, 15, 16, 'D');
287 do_searchcount_test 6.2.1 {
288 SELECT e FROM x1 WHERE b=2 OR c=7;
290 do_searchcount_test 6.2.2 {
291 SELECT c FROM x1 WHERE b=2 OR c=7;
294 do_searchcount_test 6.3.1 {
295 SELECT e FROM x1 WHERE a=1 OR b=10;
297 do_searchcount_test 6.3.2 {
298 SELECT c FROM x1 WHERE a=1 OR b=10;
300 do_searchcount_test 6.3.3 {
301 SELECT rowid FROM x1 WHERE a=1 OR b=10;
304 do_searchcount_test 6.4.1 {
305 SELECT a FROM x1 WHERE b BETWEEN 1 AND 4 OR c BETWEEN 8 AND 12
307 do_searchcount_test 6.4.2 {
308 SELECT b, c FROM x1 WHERE b BETWEEN 1 AND 4 OR c BETWEEN 8 AND 12
309 } {2 3 10 11 search 5}
310 do_searchcount_test 6.4.3 {
311 SELECT rowid, c FROM x1 WHERE b BETWEEN 1 AND 4 OR c BETWEEN 8 AND 12
312 } {1 3 3 11 search 4}
314 do_searchcount_test 6.5.1 {
315 SELECT a FROM x1 WHERE rowid = 2 OR c=11
317 do_searchcount_test 6.5.2 {
318 SELECT d FROM x1 WHERE rowid = 2 OR c=11
320 do_searchcount_test 6.5.3 {
321 SELECT d FROM x1 WHERE c=11 OR rowid = 2
323 do_searchcount_test 6.5.4 {
324 SELECT a FROM x1 WHERE c=11 OR rowid = 2
327 do_searchcount_test 6.6.1 {
328 SELECT rowid FROM x1 WHERE a=1 OR b=6 OR c=11
330 do_searchcount_test 6.6.2 {
331 SELECT c FROM x1 WHERE a=1 OR b=6 OR c=11
333 do_searchcount_test 6.6.3 {
334 SELECT c FROM x1 WHERE c=11 OR a=1 OR b=6
336 do_searchcount_test 6.6.4 {
337 SELECT c FROM x1 WHERE b=6 OR c=11 OR a=1
340 #-------------------------------------------------------------------------
342 do_execsql_test 7.0 {
343 CREATE TABLE y1(a, b);
344 CREATE TABLE y2(x, y);
345 CREATE INDEX y2xy ON y2(x, y);
346 INSERT INTO y1 VALUES(1, 1);
347 INSERT INTO y2 VALUES(3, 3);
350 do_execsql_test 7.1 {
351 SELECT * FROM y1 LEFT JOIN y2 ON ((x=1 AND y=b) OR (x=2 AND y=b))
354 do_execsql_test 7.3 {
355 CREATE TABLE foo (Id INTEGER PRIMARY KEY, fa INTEGER, fb INTEGER);
356 CREATE TABLE bar (Id INTEGER PRIMARY KEY, ba INTEGER, bb INTEGER);
358 INSERT INTO foo VALUES(1, 1, 1);
359 INSERT INTO foo VALUES(2, 1, 2);
360 INSERT INTO foo VALUES(3, 1, 3);
361 INSERT INTO foo VALUES(4, 1, 4);
362 INSERT INTO foo VALUES(5, 1, 5);
363 INSERT INTO foo VALUES(6, 1, 6);
364 INSERT INTO foo VALUES(7, 1, 7);
365 INSERT INTO foo VALUES(8, 1, 8);
366 INSERT INTO foo VALUES(9, 1, 9);
368 INSERT INTO bar VALUES(NULL, 1, 1);
369 INSERT INTO bar VALUES(NULL, 2, 2);
370 INSERT INTO bar VALUES(NULL, 3, 3);
371 INSERT INTO bar VALUES(NULL, 1, 4);
372 INSERT INTO bar VALUES(NULL, 2, 5);
373 INSERT INTO bar VALUES(NULL, 3, 6);
374 INSERT INTO bar VALUES(NULL, 1, 7);
375 INSERT INTO bar VALUES(NULL, 2, 8);
376 INSERT INTO bar VALUES(NULL, 3, 9);
379 do_execsql_test 7.4 {
381 bar.Id, bar.ba, bar.bb, foo.fb
382 FROM foo LEFT JOIN bar
383 ON (bar.ba = 1 AND bar.bb = foo.fb)
384 OR (bar.ba = 5 AND bar.bb = foo.fb);
397 do_execsql_test 7.5 {
398 CREATE INDEX idx_bar ON bar(ba, bb);
400 bar.Id, bar.ba, bar.bb, foo.fb
401 FROM foo LEFT JOIN bar
402 ON (bar.ba = 1 AND bar.bb = foo.fb)
403 OR (bar.ba = 5 AND bar.bb = foo.fb);