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 2}
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 4}
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 4}
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 4}
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 6}
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 2}
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 {} {}}