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.13 2005/01/21 03:12:16 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 {int(pow(2,$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 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg]
86 } {1 {right-hand side of IN operator must be constant}}
88 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg]
90 } {1 {right-hand side of IN operator must be constant}}
92 execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
95 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10),20)}} msg]
97 } {1 {right-hand side of IN operator must be constant}}
99 set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg]
101 } {1 {right-hand side of IN operator must be constant}}
103 set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
105 } {1 {no such column: c}}
107 # Testing the IN operator where the right-hand side is a SELECT
112 WHERE b IN (SELECT b FROM t1 WHERE a<5)
119 WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
125 SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
127 } {101 102 103 104 5 6 7 8 9 10}
129 # Make sure the UPDATE and DELETE commands work with IN-SELECT
134 WHERE b IN (SELECT b FROM t1 WHERE a>8)
136 execsql {SELECT b FROM t1 ORDER BY b}
137 } {2 4 8 16 32 64 128 256 1024 2048}
140 DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
142 execsql {SELECT a FROM t1 ORDER BY a}
146 DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
148 execsql {SELECT a FROM t1 ORDER BY a}
151 # Do an IN with a constant RHS but where the RHS has many, many
152 # elements. We need to test that collisions in the hash table
153 # are resolved properly.
157 INSERT INTO t1 VALUES('hello', 'world');
160 'Do','an','IN','with','a','constant','RHS','but','where','the',
161 'has','many','elements','We','need','to','test','that',
162 'collisions','hash','table','are','resolved','properly',
163 'This','in-set','contains','thirty','one','entries','hello');
167 # Make sure the IN operator works with INTEGER PRIMARY KEY fields.
171 CREATE TABLE ta(a INTEGER PRIMARY KEY, b);
172 INSERT INTO ta VALUES(1,1);
173 INSERT INTO ta VALUES(2,2);
174 INSERT INTO ta VALUES(3,3);
175 INSERT INTO ta VALUES(4,4);
176 INSERT INTO ta VALUES(6,6);
177 INSERT INTO ta VALUES(8,8);
178 INSERT INTO ta VALUES(10,
179 'This is a key that is long enough to require a malloc in the VDBE');
180 SELECT * FROM ta WHERE a<10;
182 } {1 1 2 2 3 3 4 4 6 6 8 8}
185 CREATE TABLE tb(a INTEGER PRIMARY KEY, b);
186 INSERT INTO tb VALUES(1,1);
187 INSERT INTO tb VALUES(2,2);
188 INSERT INTO tb VALUES(3,3);
189 INSERT INTO tb VALUES(5,5);
190 INSERT INTO tb VALUES(7,7);
191 INSERT INTO tb VALUES(9,9);
192 INSERT INTO tb VALUES(11,
193 'This is a key that is long enough to require a malloc in the VDBE');
194 SELECT * FROM tb WHERE a<10;
196 } {1 1 2 2 3 3 5 5 7 7 9 9}
199 SELECT a FROM ta WHERE b IN (SELECT a FROM tb);
204 SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb);
209 SELECT a FROM ta WHERE b IN (SELECT b FROM tb);
214 SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb);
219 SELECT a FROM ta WHERE a IN (SELECT a FROM tb);
224 SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb);
229 SELECT a FROM ta WHERE a IN (SELECT b FROM tb);
234 SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb);
238 # Tests of IN operator against empty sets. (Ticket #185)
242 SELECT a FROM t1 WHERE a IN ();
247 SELECT a FROM t1 WHERE a IN (5);
252 SELECT a FROM t1 WHERE a NOT IN () ORDER BY a;
257 SELECT a FROM t1 WHERE a IN (5) AND b IN ();
262 SELECT a FROM t1 WHERE a IN (5) AND b NOT IN ();
267 SELECT a FROM ta WHERE a IN ();
272 SELECT a FROM ta WHERE a NOT IN ();
278 SELECT b FROM t1 WHERE a IN ('hello','there')
283 SELECT b FROM t1 WHERE a IN ("hello",'there')
287 # Test constructs of the form: expr IN tablename
291 CREATE TABLE t4 AS SELECT a FROM tb;
297 SELECT b FROM t1 WHERE a IN t4;
302 SELECT b FROM t1 WHERE a NOT IN t4;
307 SELECT b FROM t1 WHERE a NOT IN tb;
309 } {1 {only a single result allowed for a SELECT that is part of an expression}}