sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the IN and BETWEEN operator. sl@0: # sl@0: # $Id: in.test,v 1.22 2008/08/04 03:51:24 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Generate the test data we will need for the first squences of tests. sl@0: # sl@0: do_test in-1.0 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a int, b int); sl@0: } sl@0: for {set i 1} {$i<=10} {incr i} { sl@0: execsql "INSERT INTO t1 VALUES($i,[expr {1<<$i}])" sl@0: } sl@0: execsql { sl@0: COMMIT; sl@0: SELECT count(*) FROM t1; sl@0: } sl@0: } {10} sl@0: sl@0: # Do basic testing of BETWEEN. sl@0: # sl@0: do_test in-1.1 { sl@0: execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a} sl@0: } {4 5} sl@0: do_test in-1.2 { sl@0: execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a} sl@0: } {1 2 3 6 7 8 9 10} sl@0: do_test in-1.3 { sl@0: execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a} sl@0: } {1 2 3 4} sl@0: do_test in-1.4 { sl@0: execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a} sl@0: } {5 6 7 8 9 10} sl@0: do_test in-1.6 { sl@0: execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a} sl@0: } {1 2 3 4 9} sl@0: do_test in-1.7 { sl@0: execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b} sl@0: } {101 102 103 4 5 6 7 8 9 10} sl@0: sl@0: # The rest of this file concentrates on testing the IN operator. sl@0: # Skip this if the library is compiled with SQLITE_OMIT_SUBQUERY sl@0: # (because the IN operator is unavailable). sl@0: # sl@0: ifcapable !subquery { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Testing of the IN operator using static lists on the right-hand side. sl@0: # sl@0: do_test in-2.1 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a} sl@0: } {3 4 5} sl@0: do_test in-2.2 { sl@0: execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a} sl@0: } {1 2 6 7 8 9 10} sl@0: do_test in-2.3 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a} sl@0: } {3 4 5 9} sl@0: do_test in-2.4 { sl@0: execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a} sl@0: } {1 2 6 7 8 9 10} sl@0: do_test in-2.5 { sl@0: execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b} sl@0: } {1 2 103 104 5 6 7 8 9 10} sl@0: sl@0: do_test in-2.6 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (b+8,64)} sl@0: } {6} sl@0: do_test in-2.7 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)} sl@0: } {4 5 6 7 8 9 10} sl@0: do_test in-2.8 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b} sl@0: } {4 5} sl@0: do_test in-2.9 { sl@0: execsql {SELECT a FROM t1 WHERE b IN (max(5,10),20)} sl@0: } {} sl@0: do_test in-2.10 { sl@0: execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))} sl@0: } {} sl@0: do_test in-2.11 { sl@0: set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg] sl@0: lappend v $msg sl@0: } {1 {no such column: c}} sl@0: sl@0: # Testing the IN operator where the right-hand side is a SELECT sl@0: # sl@0: do_test in-3.1 { sl@0: execsql { sl@0: SELECT a FROM t1 sl@0: WHERE b IN (SELECT b FROM t1 WHERE a<5) sl@0: ORDER BY a sl@0: } sl@0: } {1 2 3 4} sl@0: do_test in-3.2 { sl@0: execsql { sl@0: SELECT a FROM t1 sl@0: WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512 sl@0: ORDER BY a sl@0: } sl@0: } {1 2 3 4 9} sl@0: do_test in-3.3 { sl@0: execsql { sl@0: SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b sl@0: } sl@0: } {101 102 103 104 5 6 7 8 9 10} sl@0: sl@0: # Make sure the UPDATE and DELETE commands work with IN-SELECT sl@0: # sl@0: do_test in-4.1 { sl@0: execsql { sl@0: UPDATE t1 SET b=b*2 sl@0: WHERE b IN (SELECT b FROM t1 WHERE a>8) sl@0: } sl@0: execsql {SELECT b FROM t1 ORDER BY b} sl@0: } {2 4 8 16 32 64 128 256 1024 2048} sl@0: do_test in-4.2 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8) sl@0: } sl@0: execsql {SELECT a FROM t1 ORDER BY a} sl@0: } {1 2 3 4 5 6 7 8} sl@0: do_test in-4.3 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4) sl@0: } sl@0: execsql {SELECT a FROM t1 ORDER BY a} sl@0: } {5 6 7 8} sl@0: sl@0: # Do an IN with a constant RHS but where the RHS has many, many sl@0: # elements. We need to test that collisions in the hash table sl@0: # are resolved properly. sl@0: # sl@0: do_test in-5.1 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES('hello', 'world'); sl@0: SELECT * FROM t1 sl@0: WHERE a IN ( sl@0: 'Do','an','IN','with','a','constant','RHS','but','where','the', sl@0: 'has','many','elements','We','need','to','test','that', sl@0: 'collisions','hash','table','are','resolved','properly', sl@0: 'This','in-set','contains','thirty','one','entries','hello'); sl@0: } sl@0: } {hello world} sl@0: sl@0: # Make sure the IN operator works with INTEGER PRIMARY KEY fields. sl@0: # sl@0: do_test in-6.1 { sl@0: execsql { sl@0: CREATE TABLE ta(a INTEGER PRIMARY KEY, b); sl@0: INSERT INTO ta VALUES(1,1); sl@0: INSERT INTO ta VALUES(2,2); sl@0: INSERT INTO ta VALUES(3,3); sl@0: INSERT INTO ta VALUES(4,4); sl@0: INSERT INTO ta VALUES(6,6); sl@0: INSERT INTO ta VALUES(8,8); sl@0: INSERT INTO ta VALUES(10, sl@0: 'This is a key that is long enough to require a malloc in the VDBE'); sl@0: SELECT * FROM ta WHERE a<10; sl@0: } sl@0: } {1 1 2 2 3 3 4 4 6 6 8 8} sl@0: do_test in-6.2 { sl@0: execsql { sl@0: CREATE TABLE tb(a INTEGER PRIMARY KEY, b); sl@0: INSERT INTO tb VALUES(1,1); sl@0: INSERT INTO tb VALUES(2,2); sl@0: INSERT INTO tb VALUES(3,3); sl@0: INSERT INTO tb VALUES(5,5); sl@0: INSERT INTO tb VALUES(7,7); sl@0: INSERT INTO tb VALUES(9,9); sl@0: INSERT INTO tb VALUES(11, sl@0: 'This is a key that is long enough to require a malloc in the VDBE'); sl@0: SELECT * FROM tb WHERE a<10; sl@0: } sl@0: } {1 1 2 2 3 3 5 5 7 7 9 9} sl@0: do_test in-6.3 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE b IN (SELECT a FROM tb); sl@0: } sl@0: } {1 2 3} sl@0: do_test in-6.4 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb); sl@0: } sl@0: } {4 6 8 10} sl@0: do_test in-6.5 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE b IN (SELECT b FROM tb); sl@0: } sl@0: } {1 2 3 10} sl@0: do_test in-6.6 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb); sl@0: } sl@0: } {4 6 8} sl@0: do_test in-6.7 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a IN (SELECT a FROM tb); sl@0: } sl@0: } {1 2 3} sl@0: do_test in-6.8 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb); sl@0: } sl@0: } {4 6 8 10} sl@0: do_test in-6.9 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a IN (SELECT b FROM tb); sl@0: } sl@0: } {1 2 3} sl@0: do_test in-6.10 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb); sl@0: } sl@0: } {4 6 8 10} sl@0: sl@0: # Tests of IN operator against empty sets. (Ticket #185) sl@0: # sl@0: do_test in-7.1 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE a IN (); sl@0: } sl@0: } {} sl@0: do_test in-7.2 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE a IN (5); sl@0: } sl@0: } {5} sl@0: do_test in-7.3 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE a NOT IN () ORDER BY a; sl@0: } sl@0: } {5 6 7 8 hello} sl@0: do_test in-7.4 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE a IN (5) AND b IN (); sl@0: } sl@0: } {} sl@0: do_test in-7.5 { sl@0: execsql { sl@0: SELECT a FROM t1 WHERE a IN (5) AND b NOT IN (); sl@0: } sl@0: } {5} sl@0: do_test in-7.6 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a IN (); sl@0: } sl@0: } {} sl@0: do_test in-7.7 { sl@0: execsql { sl@0: SELECT a FROM ta WHERE a NOT IN (); sl@0: } sl@0: } {1 2 3 4 6 8 10} sl@0: sl@0: do_test in-8.1 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a IN ('hello','there') sl@0: } sl@0: } {world} sl@0: do_test in-8.2 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a IN ("hello",'there') sl@0: } sl@0: } {world} sl@0: sl@0: # Test constructs of the form: expr IN tablename sl@0: # sl@0: do_test in-9.1 { sl@0: execsql { sl@0: CREATE TABLE t4 AS SELECT a FROM tb; sl@0: SELECT * FROM t4; sl@0: } sl@0: } {1 2 3 5 7 9 11} sl@0: do_test in-9.2 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a IN t4; sl@0: } sl@0: } {32 128} sl@0: do_test in-9.3 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a NOT IN t4; sl@0: } sl@0: } {64 256 world} sl@0: do_test in-9.4 { sl@0: catchsql { sl@0: SELECT b FROM t1 WHERE a NOT IN tb; sl@0: } sl@0: } {1 {only a single result allowed for a SELECT that is part of an expression}} sl@0: sl@0: # IN clauses in CHECK constraints. Ticket #1645 sl@0: # sl@0: do_test in-10.1 { sl@0: execsql { sl@0: CREATE TABLE t5( sl@0: a INTEGER, sl@0: CHECK( a IN (111,222,333) ) sl@0: ); sl@0: INSERT INTO t5 VALUES(111); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {111} sl@0: do_test in-10.2 { sl@0: catchsql { sl@0: INSERT INTO t5 VALUES(4); sl@0: } sl@0: } {1 {constraint failed}} sl@0: sl@0: # Ticket #1821 sl@0: # sl@0: # Type affinity applied to the right-hand side of an IN operator. sl@0: # sl@0: do_test in-11.1 { sl@0: execsql { sl@0: CREATE TABLE t6(a,b NUMERIC); sl@0: INSERT INTO t6 VALUES(1,2); sl@0: INSERT INTO t6 VALUES(2,3); sl@0: SELECT * FROM t6 WHERE b IN (2); sl@0: } sl@0: } {1 2} sl@0: do_test in-11.2 { sl@0: # The '2' should be coerced into 2 because t6.b is NUMERIC sl@0: execsql { sl@0: SELECT * FROM t6 WHERE b IN ('2'); sl@0: } sl@0: } {1 2} sl@0: do_test in-11.3 { sl@0: # No coercion should occur here because of the unary + before b. sl@0: execsql { sl@0: SELECT * FROM t6 WHERE +b IN ('2'); sl@0: } sl@0: } {} sl@0: do_test in-11.4 { sl@0: # No coercion because column a as affinity NONE sl@0: execsql { sl@0: SELECT * FROM t6 WHERE a IN ('2'); sl@0: } sl@0: } {} sl@0: do_test in-11.5 { sl@0: execsql { sl@0: SELECT * FROM t6 WHERE a IN (2); sl@0: } sl@0: } {2 3} sl@0: do_test in-11.6 { sl@0: # No coercion because column a as affinity NONE sl@0: execsql { sl@0: SELECT * FROM t6 WHERE +a IN ('2'); sl@0: } sl@0: } {} sl@0: sl@0: # Test error conditions with expressions of the form IN(). sl@0: # sl@0: ifcapable compound { sl@0: do_test in-12.1 { sl@0: execsql { sl@0: CREATE TABLE t2(a, b, c); sl@0: CREATE TABLE t3(a, b, c); sl@0: } sl@0: } {} sl@0: do_test in-12.2 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {only a single result allowed for a SELECT that is part of an expression}} sl@0: do_test in-12.3 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a, b FROM t3 UNION SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {only a single result allowed for a SELECT that is part of an expression}} sl@0: do_test in-12.4 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a, b FROM t3 EXCEPT SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {only a single result allowed for a SELECT that is part of an expression}} sl@0: do_test in-12.5 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a, b FROM t3 INTERSECT SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {only a single result allowed for a SELECT that is part of an expression}} sl@0: do_test in-12.6 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a FROM t3 UNION ALL SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}} sl@0: do_test in-12.7 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a FROM t3 UNION SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} sl@0: do_test in-12.8 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a FROM t3 EXCEPT SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}} sl@0: do_test in-12.9 { sl@0: catchsql { sl@0: SELECT * FROM t2 WHERE a IN ( sl@0: SELECT a FROM t3 INTERSECT SELECT a, b FROM t2 sl@0: ); sl@0: } sl@0: } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}} sl@0: } sl@0: sl@0: sl@0: #------------------------------------------------------------------------ sl@0: # The following tests check that NULL is handled correctly when it sl@0: # appears as part of a set of values on the right-hand side of an sl@0: # IN or NOT IN operator. sl@0: # sl@0: # When it appears in such a set, NULL is handled as an "unknown value". sl@0: # If, because of the unknown value in the set, the result of the expression sl@0: # cannot be determined, then it itself evaluates to NULL. sl@0: # sl@0: sl@0: # Warm body test to demonstrate the principles being tested: sl@0: # sl@0: do_test in-13.1 { sl@0: db nullvalue "null" sl@0: execsql { SELECT sl@0: 1 IN (NULL, 1, 2), -- The value 1 is a member of the set, return true. sl@0: 3 IN (NULL, 1, 2), -- Ambiguous, return NULL. sl@0: 1 NOT IN (NULL, 1, 2), -- The value 1 is a member of the set, return false. sl@0: 3 NOT IN (NULL, 1, 2) -- Ambiguous, return NULL. sl@0: } sl@0: } {1 null 0 null} sl@0: sl@0: do_test in-13.2 { sl@0: execsql { sl@0: CREATE TABLE t7(a, b, c NOT NULL); sl@0: INSERT INTO t7 VALUES(1, 1, 1); sl@0: INSERT INTO t7 VALUES(2, 2, 2); sl@0: INSERT INTO t7 VALUES(3, 3, 3); sl@0: INSERT INTO t7 VALUES(NULL, 4, 4); sl@0: INSERT INTO t7 VALUES(NULL, 5, 5); sl@0: } sl@0: } {} sl@0: sl@0: do_test in-13.3 { sl@0: execsql { SELECT 2 IN (SELECT a FROM t7) } sl@0: } {1} sl@0: do_test in-13.4 { sl@0: execsql { SELECT 6 IN (SELECT a FROM t7) } sl@0: } {null} sl@0: sl@0: do_test in-13.5 { sl@0: execsql { SELECT 2 IN (SELECT b FROM t7) } sl@0: } {1} sl@0: do_test in-13.6 { sl@0: execsql { SELECT 6 IN (SELECT b FROM t7) } sl@0: } {0} sl@0: sl@0: do_test in-13.7 { sl@0: execsql { SELECT 2 IN (SELECT c FROM t7) } sl@0: } {1} sl@0: do_test in-13.8 { sl@0: execsql { SELECT 6 IN (SELECT c FROM t7) } sl@0: } {0} sl@0: sl@0: do_test in-13.9 { sl@0: execsql { sl@0: SELECT sl@0: 2 NOT IN (SELECT a FROM t7), sl@0: 6 NOT IN (SELECT a FROM t7), sl@0: 2 NOT IN (SELECT b FROM t7), sl@0: 6 NOT IN (SELECT b FROM t7), sl@0: 2 NOT IN (SELECT c FROM t7), sl@0: 6 NOT IN (SELECT c FROM t7) sl@0: } sl@0: } {0 null 0 1 0 1} sl@0: sl@0: do_test in-13.10 { sl@0: execsql { sl@0: SELECT b IN ( sl@0: SELECT inside.a sl@0: FROM t7 AS inside sl@0: WHERE inside.b BETWEEN outside.b+1 AND outside.b+2 sl@0: ) sl@0: FROM t7 AS outside ORDER BY b; sl@0: } sl@0: } {0 null null null 0} sl@0: sl@0: do_test in-13.11 { sl@0: execsql { sl@0: SELECT b NOT IN ( sl@0: SELECT inside.a sl@0: FROM t7 AS inside sl@0: WHERE inside.b BETWEEN outside.b+1 AND outside.b+2 sl@0: ) sl@0: FROM t7 AS outside ORDER BY b; sl@0: } sl@0: } {1 null null null 1} sl@0: sl@0: do_test in-13.12 { sl@0: execsql { sl@0: CREATE INDEX i1 ON t7(a); sl@0: CREATE INDEX i2 ON t7(b); sl@0: CREATE INDEX i3 ON t7(c); sl@0: } sl@0: execsql { sl@0: SELECT sl@0: 2 IN (SELECT a FROM t7), sl@0: 6 IN (SELECT a FROM t7), sl@0: 2 IN (SELECT b FROM t7), sl@0: 6 IN (SELECT b FROM t7), sl@0: 2 IN (SELECT c FROM t7), sl@0: 6 IN (SELECT c FROM t7) sl@0: } sl@0: } {1 null 1 0 1 0} sl@0: sl@0: do_test in-13.13 { sl@0: execsql { sl@0: SELECT sl@0: 2 NOT IN (SELECT a FROM t7), sl@0: 6 NOT IN (SELECT a FROM t7), sl@0: 2 NOT IN (SELECT b FROM t7), sl@0: 6 NOT IN (SELECT b FROM t7), sl@0: 2 NOT IN (SELECT c FROM t7), sl@0: 6 NOT IN (SELECT c FROM t7) sl@0: } sl@0: } {0 null 0 1 0 1} sl@0: sl@0: do_test in-13.14 { sl@0: execsql { sl@0: BEGIN TRANSACTION; sl@0: CREATE TABLE a(id INTEGER); sl@0: INSERT INTO a VALUES(1); sl@0: INSERT INTO a VALUES(2); sl@0: INSERT INTO a VALUES(3); sl@0: CREATE TABLE b(id INTEGER); sl@0: INSERT INTO b VALUES(NULL); sl@0: INSERT INTO b VALUES(3); sl@0: INSERT INTO b VALUES(4); sl@0: INSERT INTO b VALUES(5); sl@0: COMMIT; sl@0: SELECT * FROM a WHERE id NOT IN (SELECT id FROM b); sl@0: } sl@0: } {} sl@0: do_test in-13.14 { sl@0: execsql { sl@0: CREATE INDEX i5 ON b(id); sl@0: SELECT * FROM a WHERE id NOT IN (SELECT id FROM b); sl@0: } sl@0: } {} sl@0: sl@0: sl@0: do_test in-13.X { sl@0: db nullvalue "" sl@0: } {} sl@0: sl@0: finish_test