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 the IN and BETWEEN operator.
14 # $Id: in.test,v 1.22 2008/08/04 03:51:24 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Generate the test data we will need for the first squences of tests.
24 CREATE TABLE t1(a int, b int);
26 for {set i 1} {$i<=10} {incr i} {
27 execsql "INSERT INTO t1 VALUES($i,[expr {1<<$i}])"
31 SELECT count(*) FROM t1;
35 # Do basic testing of BETWEEN.
38 execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
41 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a}
44 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a}
47 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a}
50 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a}
53 execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b}
54 } {101 102 103 4 5 6 7 8 9 10}
56 # The rest of this file concentrates on testing the IN operator.
57 # Skip this if the library is compiled with SQLITE_OMIT_SUBQUERY
58 # (because the IN operator is unavailable).
65 # Testing of the IN operator using static lists on the right-hand side.
68 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a}
71 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a}
74 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a}
77 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a}
80 execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b}
81 } {1 2 103 104 5 6 7 8 9 10}
84 execsql {SELECT a FROM t1 WHERE b IN (b+8,64)}
87 execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}
90 execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
93 execsql {SELECT a FROM t1 WHERE b IN (max(5,10),20)}
96 execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}
99 set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
101 } {1 {no such column: c}}
103 # Testing the IN operator where the right-hand side is a SELECT
108 WHERE b IN (SELECT b FROM t1 WHERE a<5)
115 WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
121 SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
123 } {101 102 103 104 5 6 7 8 9 10}
125 # Make sure the UPDATE and DELETE commands work with IN-SELECT
130 WHERE b IN (SELECT b FROM t1 WHERE a>8)
132 execsql {SELECT b FROM t1 ORDER BY b}
133 } {2 4 8 16 32 64 128 256 1024 2048}
136 DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
138 execsql {SELECT a FROM t1 ORDER BY a}
142 DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
144 execsql {SELECT a FROM t1 ORDER BY a}
147 # Do an IN with a constant RHS but where the RHS has many, many
148 # elements. We need to test that collisions in the hash table
149 # are resolved properly.
153 INSERT INTO t1 VALUES('hello', 'world');
156 'Do','an','IN','with','a','constant','RHS','but','where','the',
157 'has','many','elements','We','need','to','test','that',
158 'collisions','hash','table','are','resolved','properly',
159 'This','in-set','contains','thirty','one','entries','hello');
163 # Make sure the IN operator works with INTEGER PRIMARY KEY fields.
167 CREATE TABLE ta(a INTEGER PRIMARY KEY, b);
168 INSERT INTO ta VALUES(1,1);
169 INSERT INTO ta VALUES(2,2);
170 INSERT INTO ta VALUES(3,3);
171 INSERT INTO ta VALUES(4,4);
172 INSERT INTO ta VALUES(6,6);
173 INSERT INTO ta VALUES(8,8);
174 INSERT INTO ta VALUES(10,
175 'This is a key that is long enough to require a malloc in the VDBE');
176 SELECT * FROM ta WHERE a<10;
178 } {1 1 2 2 3 3 4 4 6 6 8 8}
181 CREATE TABLE tb(a INTEGER PRIMARY KEY, b);
182 INSERT INTO tb VALUES(1,1);
183 INSERT INTO tb VALUES(2,2);
184 INSERT INTO tb VALUES(3,3);
185 INSERT INTO tb VALUES(5,5);
186 INSERT INTO tb VALUES(7,7);
187 INSERT INTO tb VALUES(9,9);
188 INSERT INTO tb VALUES(11,
189 'This is a key that is long enough to require a malloc in the VDBE');
190 SELECT * FROM tb WHERE a<10;
192 } {1 1 2 2 3 3 5 5 7 7 9 9}
195 SELECT a FROM ta WHERE b IN (SELECT a FROM tb);
200 SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb);
205 SELECT a FROM ta WHERE b IN (SELECT b FROM tb);
210 SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb);
215 SELECT a FROM ta WHERE a IN (SELECT a FROM tb);
220 SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb);
225 SELECT a FROM ta WHERE a IN (SELECT b FROM tb);
230 SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb);
234 # Tests of IN operator against empty sets. (Ticket #185)
238 SELECT a FROM t1 WHERE a IN ();
243 SELECT a FROM t1 WHERE a IN (5);
248 SELECT a FROM t1 WHERE a NOT IN () ORDER BY a;
253 SELECT a FROM t1 WHERE a IN (5) AND b IN ();
258 SELECT a FROM t1 WHERE a IN (5) AND b NOT IN ();
263 SELECT a FROM ta WHERE a IN ();
268 SELECT a FROM ta WHERE a NOT IN ();
274 SELECT b FROM t1 WHERE a IN ('hello','there')
279 SELECT b FROM t1 WHERE a IN ("hello",'there')
283 # Test constructs of the form: expr IN tablename
287 CREATE TABLE t4 AS SELECT a FROM tb;
293 SELECT b FROM t1 WHERE a IN t4;
298 SELECT b FROM t1 WHERE a NOT IN t4;
303 SELECT b FROM t1 WHERE a NOT IN tb;
305 } {1 {only a single result allowed for a SELECT that is part of an expression}}
307 # IN clauses in CHECK constraints. Ticket #1645
313 CHECK( a IN (111,222,333) )
315 INSERT INTO t5 VALUES(111);
321 INSERT INTO t5 VALUES(4);
323 } {1 {constraint failed}}
327 # Type affinity applied to the right-hand side of an IN operator.
331 CREATE TABLE t6(a,b NUMERIC);
332 INSERT INTO t6 VALUES(1,2);
333 INSERT INTO t6 VALUES(2,3);
334 SELECT * FROM t6 WHERE b IN (2);
338 # The '2' should be coerced into 2 because t6.b is NUMERIC
340 SELECT * FROM t6 WHERE b IN ('2');
344 # No coercion should occur here because of the unary + before b.
346 SELECT * FROM t6 WHERE +b IN ('2');
350 # No coercion because column a as affinity NONE
352 SELECT * FROM t6 WHERE a IN ('2');
357 SELECT * FROM t6 WHERE a IN (2);
361 # No coercion because column a as affinity NONE
363 SELECT * FROM t6 WHERE +a IN ('2');
367 # Test error conditions with expressions of the form IN(<compound select>).
372 CREATE TABLE t2(a, b, c);
373 CREATE TABLE t3(a, b, c);
378 SELECT * FROM t2 WHERE a IN (
379 SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2
382 } {1 {only a single result allowed for a SELECT that is part of an expression}}
385 SELECT * FROM t2 WHERE a IN (
386 SELECT a, b FROM t3 UNION SELECT a, b FROM t2
389 } {1 {only a single result allowed for a SELECT that is part of an expression}}
392 SELECT * FROM t2 WHERE a IN (
393 SELECT a, b FROM t3 EXCEPT SELECT a, b FROM t2
396 } {1 {only a single result allowed for a SELECT that is part of an expression}}
399 SELECT * FROM t2 WHERE a IN (
400 SELECT a, b FROM t3 INTERSECT SELECT a, b FROM t2
403 } {1 {only a single result allowed for a SELECT that is part of an expression}}
406 SELECT * FROM t2 WHERE a IN (
407 SELECT a, b FROM t3 UNION ALL SELECT a FROM t2
410 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
413 SELECT * FROM t2 WHERE a IN (
414 SELECT a, b FROM t3 UNION SELECT a FROM t2
417 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
420 SELECT * FROM t2 WHERE a IN (
421 SELECT a, b FROM t3 EXCEPT SELECT a FROM t2
424 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
427 SELECT * FROM t2 WHERE a IN (
428 SELECT a, b FROM t3 INTERSECT SELECT a FROM t2
431 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
436 SELECT * FROM t2 WHERE a IN (
437 SELECT a FROM t3 UNION ALL SELECT a, b FROM t2
440 } {1 {only a single result allowed for a SELECT that is part of an expression}}
443 SELECT * FROM t2 WHERE a IN (
444 SELECT a FROM t3 UNION SELECT a, b FROM t2
447 } {1 {only a single result allowed for a SELECT that is part of an expression}}
450 SELECT * FROM t2 WHERE a IN (
451 SELECT a FROM t3 EXCEPT SELECT a, b FROM t2
454 } {1 {only a single result allowed for a SELECT that is part of an expression}}
457 SELECT * FROM t2 WHERE a IN (
458 SELECT a FROM t3 INTERSECT SELECT a, b FROM t2
461 } {1 {only a single result allowed for a SELECT that is part of an expression}}
464 #------------------------------------------------------------------------
465 # The following tests check that NULL is handled correctly when it
466 # appears as part of a set of values on the right-hand side of an
467 # IN or NOT IN operator.
469 # When it appears in such a set, NULL is handled as an "unknown value".
470 # If, because of the unknown value in the set, the result of the expression
471 # cannot be determined, then it itself evaluates to NULL.
474 # Warm body test to demonstrate the principles being tested:
479 1 IN (NULL, 1, 2), -- The value 1 is a member of the set, return true.
480 3 IN (NULL, 1, 2), -- Ambiguous, return NULL.
481 1 NOT IN (NULL, 1, 2), -- The value 1 is a member of the set, return false.
482 3 NOT IN (NULL, 1, 2) -- Ambiguous, return NULL.
488 CREATE TABLE t7(a, b, c NOT NULL);
489 INSERT INTO t7 VALUES(1, 1, 1);
490 INSERT INTO t7 VALUES(2, 2, 2);
491 INSERT INTO t7 VALUES(3, 3, 3);
492 INSERT INTO t7 VALUES(NULL, 4, 4);
493 INSERT INTO t7 VALUES(NULL, 5, 5);
498 execsql { SELECT 2 IN (SELECT a FROM t7) }
501 execsql { SELECT 6 IN (SELECT a FROM t7) }
505 execsql { SELECT 2 IN (SELECT b FROM t7) }
508 execsql { SELECT 6 IN (SELECT b FROM t7) }
512 execsql { SELECT 2 IN (SELECT c FROM t7) }
515 execsql { SELECT 6 IN (SELECT c FROM t7) }
521 2 NOT IN (SELECT a FROM t7),
522 6 NOT IN (SELECT a FROM t7),
523 2 NOT IN (SELECT b FROM t7),
524 6 NOT IN (SELECT b FROM t7),
525 2 NOT IN (SELECT c FROM t7),
526 6 NOT IN (SELECT c FROM t7)
535 WHERE inside.b BETWEEN outside.b+1 AND outside.b+2
537 FROM t7 AS outside ORDER BY b;
539 } {0 null null null 0}
546 WHERE inside.b BETWEEN outside.b+1 AND outside.b+2
548 FROM t7 AS outside ORDER BY b;
550 } {1 null null null 1}
554 CREATE INDEX i1 ON t7(a);
555 CREATE INDEX i2 ON t7(b);
556 CREATE INDEX i3 ON t7(c);
560 2 IN (SELECT a FROM t7),
561 6 IN (SELECT a FROM t7),
562 2 IN (SELECT b FROM t7),
563 6 IN (SELECT b FROM t7),
564 2 IN (SELECT c FROM t7),
565 6 IN (SELECT c FROM t7)
572 2 NOT IN (SELECT a FROM t7),
573 6 NOT IN (SELECT a FROM t7),
574 2 NOT IN (SELECT b FROM t7),
575 6 NOT IN (SELECT b FROM t7),
576 2 NOT IN (SELECT c FROM t7),
577 6 NOT IN (SELECT c FROM t7)
584 CREATE TABLE a(id INTEGER);
585 INSERT INTO a VALUES(1);
586 INSERT INTO a VALUES(2);
587 INSERT INTO a VALUES(3);
588 CREATE TABLE b(id INTEGER);
589 INSERT INTO b VALUES(NULL);
590 INSERT INTO b VALUES(3);
591 INSERT INTO b VALUES(4);
592 INSERT INTO b VALUES(5);
594 SELECT * FROM a WHERE id NOT IN (SELECT id FROM b);
599 CREATE INDEX i5 ON b(id);
600 SELECT * FROM a WHERE id NOT IN (SELECT id FROM b);