2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the IN and BETWEEN operator.
17 # $Id: in.test,v 1.11 2004/01/15 03:30:25 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Generate the test data we will need for the first squences of tests.
25 set fd [open data1.txt w]
26 for {set i 1} {$i<=10} {incr i} {
27 puts $fd "$i\t[expr {int(pow(2,$i))}]"
31 CREATE TABLE t1(a int, b int);
32 COPY t1 FROM 'data1.txt';
34 file delete -force data1.txt
35 execsql {SELECT count(*) FROM t1}
38 # Do basic testing of BETWEEN.
41 execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
44 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a}
47 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a}
50 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a}
53 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a}
56 execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b}
57 } {101 102 103 4 5 6 7 8 9 10}
60 # Testing of the IN operator using static lists on the right-hand side.
63 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a}
66 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a}
69 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a}
72 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a}
75 execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b}
76 } {1 2 103 104 5 6 7 8 9 10}
79 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg]
81 } {1 {right-hand side of IN operator must be constant}}
83 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg]
85 } {1 {right-hand side of IN operator must be constant}}
87 execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
90 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10),20)}} msg]
92 } {1 {right-hand side of IN operator must be constant}}
94 set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg]
96 } {1 {right-hand side of IN operator must be constant}}
98 set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
100 } {1 {no such column: c}}
102 # Testing the IN operator where the right-hand side is a SELECT
107 WHERE b IN (SELECT b FROM t1 WHERE a<5)
114 WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
120 SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
122 } {101 102 103 104 5 6 7 8 9 10}
124 # Make sure the UPDATE and DELETE commands work with IN-SELECT
129 WHERE b IN (SELECT b FROM t1 WHERE a>8)
131 execsql {SELECT b FROM t1 ORDER BY b}
132 } {2 4 8 16 32 64 128 256 1024 2048}
135 DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
137 execsql {SELECT a FROM t1 ORDER BY a}
141 DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
143 execsql {SELECT a FROM t1 ORDER BY a}
146 # Do an IN with a constant RHS but where the RHS has many, many
147 # elements. We need to test that collisions in the hash table
148 # are resolved properly.
152 INSERT INTO t1 VALUES('hello', 'world');
155 'Do','an','IN','with','a','constant','RHS','but','where','the',
156 'has','many','elements','We','need','to','test','that',
157 'collisions','hash','table','are','resolved','properly',
158 'This','in-set','contains','thirty','one','entries','hello');
162 # Make sure the IN operator works with INTEGER PRIMARY KEY fields.
166 CREATE TABLE ta(a INTEGER PRIMARY KEY, b);
167 INSERT INTO ta VALUES(1,1);
168 INSERT INTO ta VALUES(2,2);
169 INSERT INTO ta VALUES(3,3);
170 INSERT INTO ta VALUES(4,4);
171 INSERT INTO ta VALUES(6,6);
172 INSERT INTO ta VALUES(8,8);
173 INSERT INTO ta VALUES(10,
174 'This is a key that is long enough to require a malloc in the VDBE');
175 SELECT * FROM ta WHERE a<10;
177 } {1 1 2 2 3 3 4 4 6 6 8 8}
180 CREATE TABLE tb(a INTEGER PRIMARY KEY, b);
181 INSERT INTO tb VALUES(1,1);
182 INSERT INTO tb VALUES(2,2);
183 INSERT INTO tb VALUES(3,3);
184 INSERT INTO tb VALUES(5,5);
185 INSERT INTO tb VALUES(7,7);
186 INSERT INTO tb VALUES(9,9);
187 INSERT INTO tb VALUES(11,
188 'This is a key that is long enough to require a malloc in the VDBE');
189 SELECT * FROM tb WHERE a<10;
191 } {1 1 2 2 3 3 5 5 7 7 9 9}
194 SELECT a FROM ta WHERE b IN (SELECT a FROM tb);
199 SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb);
204 SELECT a FROM ta WHERE b IN (SELECT b FROM tb);
209 SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb);
214 SELECT a FROM ta WHERE a IN (SELECT a FROM tb);
219 SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb);
224 SELECT a FROM ta WHERE a IN (SELECT b FROM tb);
229 SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb);
233 # Tests of IN operator against empty sets. (Ticket #185)
237 SELECT a FROM t1 WHERE a IN ();
242 SELECT a FROM t1 WHERE a IN (5);
247 SELECT a FROM t1 WHERE a NOT IN () ORDER BY a;
252 SELECT a FROM t1 WHERE a IN (5) AND b IN ();
257 SELECT a FROM t1 WHERE a IN (5) AND b NOT IN ();
262 SELECT a FROM ta WHERE a IN ();
267 SELECT a FROM ta WHERE a NOT IN ();
273 SELECT b FROM t1 WHERE a IN ('hello','there')
278 SELECT b FROM t1 WHERE a IN ("hello",'there')
282 # Test constructs of the form: expr IN tablename
286 CREATE TABLE t4 AS SELECT a FROM tb;
292 SELECT b FROM t1 WHERE a IN t4;
297 SELECT b FROM t1 WHERE a NOT IN t4;
302 SELECT b FROM t1 WHERE a NOT IN tb;
304 } {1 {only a single result allowed for a SELECT that is part of an expression}}