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 # $Id: in4.test,v 1.4 2009/06/05 17:09:12 drh Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
19 CREATE TABLE t1(a, b);
20 CREATE INDEX i1 ON t1(a);
25 SELECT * FROM t1 WHERE a IN ('aaa', 'bbb', 'ccc');
30 INSERT INTO t1 VALUES('aaa', 1);
31 INSERT INTO t1 VALUES('ddd', 2);
32 INSERT INTO t1 VALUES('ccc', 3);
33 INSERT INTO t1 VALUES('eee', 4);
34 SELECT b FROM t1 WHERE a IN ('aaa', 'bbb', 'ccc');
39 SELECT a FROM t1 WHERE rowid IN (1, 3);
44 SELECT a FROM t1 WHERE rowid IN ();
49 SELECT a FROM t1 WHERE a IN ('ddd');
55 CREATE TABLE t2(a INTEGER PRIMARY KEY, b TEXT);
56 INSERT INTO t2 VALUES(-1, '-one');
57 INSERT INTO t2 VALUES(0, 'zero');
58 INSERT INTO t2 VALUES(1, 'one');
59 INSERT INTO t2 VALUES(2, 'two');
60 INSERT INTO t2 VALUES(3, 'three');
65 execsql { SELECT b FROM t2 WHERE a IN (0, 2) }
69 execsql { SELECT b FROM t2 WHERE a IN (2, 0) }
73 execsql { SELECT b FROM t2 WHERE a IN (2, -1) }
77 execsql { SELECT b FROM t2 WHERE a IN (NULL, 3) }
81 execsql { SELECT b FROM t2 WHERE a IN (1.0, 2.1) }
85 execsql { SELECT b FROM t2 WHERE a IN ('1', '2') }
89 execsql { SELECT b FROM t2 WHERE a IN ('', '0.0.0', '2') }
92 # The following block of tests test expressions of the form:
96 # i.e. IN expressions with a literal empty set.
98 # This has led to crashes on more than one occasion. Test case in4-3.2
99 # was added in reponse to a bug reported on the mailing list on 11/7/2008.
100 # See also tickets #3602 and #185.
104 DROP TABLE IF EXISTS t1;
105 DROP TABLE IF EXISTS t2;
106 CREATE TABLE t1(x, id);
107 CREATE TABLE t2(x, id);
108 INSERT INTO t1 VALUES(NULL, NULL);
109 INSERT INTO t1 VALUES(0, NULL);
110 INSERT INTO t1 VALUES(1, 3);
111 INSERT INTO t1 VALUES(2, 4);
112 INSERT INTO t1 VALUES(3, 5);
113 INSERT INTO t1 VALUES(4, 6);
114 INSERT INTO t2 VALUES(0, NULL);
115 INSERT INTO t2 VALUES(4, 1);
116 INSERT INTO t2 VALUES(NULL, 1);
117 INSERT INTO t2 VALUES(NULL, NULL);
122 SELECT x FROM t1 WHERE id IN () AND x IN (SELECT x FROM t2 WHERE id=1)
127 CREATE TABLE t3(x, y, z);
128 CREATE INDEX t3i1 ON t3(x, y);
129 INSERT INTO t3 VALUES(1, 1, 1);
130 INSERT INTO t3 VALUES(10, 10, 10);
132 execsql { SELECT * FROM t3 WHERE x IN () }
135 execsql { SELECT * FROM t3 WHERE x = 10 AND y IN () }
138 execsql { SELECT * FROM t3 WHERE x IN () AND y = 10 }
141 execsql { SELECT * FROM t3 WHERE x IN () OR x = 10 }
144 execsql { SELECT * FROM t3 WHERE y IN () }
147 execsql { SELECT x IN() AS a FROM t3 WHERE a }
150 execsql { SELECT x IN() AS a FROM t3 WHERE NOT a }
153 execsql { SELECT * FROM t3 WHERE oid IN () }
156 execsql { SELECT * FROM t3 WHERE x IN (1, 2) OR y IN ()}
159 execsql { SELECT * FROM t3 WHERE x IN (1, 2) AND y IN ()}
162 # Tests for "... IN (?)" and "... NOT IN (?)". In other words, tests
163 # for when the RHS of IN is a single expression. This should work the
164 # same as the == and <> operators.
166 do_execsql_test in4-3.21 {
167 SELECT * FROM t3 WHERE x=10 AND y IN (10);
169 do_execsql_test in4-3.22 {
170 SELECT * FROM t3 WHERE x IN (10) AND y=10;
172 do_execsql_test in4-3.23 {
173 SELECT * FROM t3 WHERE x IN (10) AND y IN (10);
175 do_execsql_test in4-3.24 {
176 SELECT * FROM t3 WHERE x=1 AND y NOT IN (10);
178 do_execsql_test in4-3.25 {
179 SELECT * FROM t3 WHERE x NOT IN (10) AND y=1;
181 do_execsql_test in4-3.26 {
182 SELECT * FROM t3 WHERE x NOT IN (10) AND y NOT IN (10);
185 # The query planner recognizes that "x IN (?)" only generates a
186 # single match and can use this information to optimize-out ORDER BY
189 do_execsql_test in4-3.31 {
191 CREATE UNIQUE INDEX t3xy ON t3(x,y);
193 SELECT *, '|' FROM t3 A, t3 B
194 WHERE A.x=10 AND A.y IN (10)
195 AND B.x=1 AND B.y IN (1);
197 do_execsql_test in4-3.32 {
199 SELECT *, '|' FROM t3 A, t3 B
200 WHERE A.x=10 AND A.y IN (10)
201 AND B.x=1 AND B.y IN (1);
202 } {~/B-TREE/} ;# No separate sorting pass
203 do_execsql_test in4-3.33 {
204 SELECT *, '|' FROM t3 A, t3 B
205 WHERE A.x IN (10) AND A.y=10
206 AND B.x IN (1) AND B.y=1;
208 do_execsql_test in4-3.34 {
210 SELECT *, '|' FROM t3 A, t3 B
211 WHERE A.x IN (10) AND A.y=10
212 AND B.x IN (1) AND B.y=1;
213 } {~/B-TREE/} ;# No separate sorting pass
215 # An expression of the form "x IN (?,?)" creates an ephemeral table to
216 # hold the list of values on the RHS. But "x IN (?)" does not create
217 # an ephemeral table.
219 do_execsql_test in4-3.41 {
220 SELECT * FROM t3 WHERE x IN (10,11);
222 do_execsql_test in4-3.42 {
224 SELECT * FROM t3 WHERE x IN (10,11);
226 do_execsql_test in4-3.43 {
227 SELECT * FROM t3 WHERE x IN (10);
229 do_execsql_test in4-3.44 {
231 SELECT * FROM t3 WHERE x IN (10);
233 do_execsql_test in4-3.45 {
234 SELECT * FROM t3 WHERE x NOT IN (10,11,99999);
236 do_execsql_test in4-3.46 {
238 SELECT * FROM t3 WHERE x NOT IN (10,11,99999);
240 do_execsql_test in4-3.47 {
241 SELECT * FROM t3 WHERE x NOT IN (10);
243 do_execsql_test in4-3.48 {
245 SELECT * FROM t3 WHERE x NOT IN (10);
248 # Make sure that when "x IN (?)" is converted into "x==?" that collating
249 # sequence and affinity computations do not get messed up.
251 do_execsql_test in4-4.1 {
252 CREATE TABLE t4a(a TEXT, b TEXT COLLATE nocase, c);
253 INSERT INTO t4a VALUES('ABC','abc',1);
254 INSERT INTO t4a VALUES('def','xyz',2);
255 INSERT INTO t4a VALUES('ghi','ghi',3);
256 SELECT c FROM t4a WHERE a=b ORDER BY c;
258 do_execsql_test in4-4.2 {
259 SELECT c FROM t4a WHERE b=a ORDER BY c;
261 do_execsql_test in4-4.3 {
262 SELECT c FROM t4a WHERE (a||'')=b ORDER BY c;
264 do_execsql_test in4-4.4 {
265 SELECT c FROM t4a WHERE (a||'')=(b||'') ORDER BY c;
267 do_execsql_test in4-4.5 {
268 SELECT c FROM t4a WHERE a IN (b) ORDER BY c;
270 do_execsql_test in4-4.6 {
271 SELECT c FROM t4a WHERE (a||'') IN (b) ORDER BY c;
275 do_execsql_test in4-4.11 {
276 CREATE TABLE t4b(a TEXT, b NUMERIC, c);
277 INSERT INTO t4b VALUES('1.0',1,4);
278 SELECT c FROM t4b WHERE a=b;
280 do_execsql_test in4-4.12 {
281 SELECT c FROM t4b WHERE b=a;
283 do_execsql_test in4-4.13 {
284 SELECT c FROM t4b WHERE +a=b;
286 do_execsql_test in4-4.14 {
287 SELECT c FROM t4b WHERE a=+b;
289 do_execsql_test in4-4.15 {
290 SELECT c FROM t4b WHERE +b=a;
292 do_execsql_test in4-4.16 {
293 SELECT c FROM t4b WHERE b=+a;
295 do_execsql_test in4-4.17 {
296 SELECT c FROM t4b WHERE a IN (b);
298 do_execsql_test in4-4.18 {
299 SELECT c FROM t4b WHERE b IN (a);
301 do_execsql_test in4-4.19 {
302 SELECT c FROM t4b WHERE +b IN (a);
305 do_execsql_test in4-5.1 {
306 CREATE TABLE t5(c INTEGER PRIMARY KEY, d TEXT COLLATE nocase);
307 INSERT INTO t5 VALUES(17, 'fuzz');
308 SELECT 1 FROM t5 WHERE 'fuzz' IN (d); -- match
309 SELECT 2 FROM t5 WHERE 'FUZZ' IN (d); -- no match
310 SELECT 3 FROM t5 WHERE d IN ('fuzz'); -- match
311 SELECT 4 FROM t5 WHERE d IN ('FUZZ'); -- match
314 # An expression of the form "x IN (y)" can be used as "x=y" by the
315 # query planner when computing transitive constraints or to run the
316 # query using an index on y.
318 do_execsql_test in4-6.1 {
319 CREATE TABLE t6a(a INTEGER PRIMARY KEY, b);
320 INSERT INTO t6a VALUES(1,2),(3,4),(5,6);
321 CREATE TABLE t6b(c INTEGER PRIMARY KEY, d);
322 INSERT INTO t6b VALUES(4,44),(5,55),(6,66);
324 SELECT * FROM t6a, t6b WHERE a=3 AND b IN (c);
326 do_execsql_test in4-6.1-eqp {
328 SELECT * FROM t6a, t6b WHERE a=3 AND b IN (c);
330 do_execsql_test in4-6.2 {
331 SELECT * FROM t6a, t6b WHERE a=3 AND c IN (b);
333 do_execsql_test in4-6.2-eqp {
335 SELECT * FROM t6a, t6b WHERE a=3 AND c IN (b);