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 ();
271 SELECT a FROM ta WHERE a NOT IN ();
277 SELECT * FROM ta LEFT JOIN tb ON (ta.b=tb.b) WHERE ta.a IN ();
286 SELECT b FROM t1 WHERE a IN ('hello','there')
290 sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
292 SELECT b FROM t1 WHERE a IN ("hello",'there')
296 # Test constructs of the form: expr IN tablename
300 CREATE TABLE t4 AS SELECT a FROM tb;
306 SELECT b FROM t1 WHERE a IN t4;
311 SELECT b FROM t1 WHERE a NOT IN t4;
316 SELECT b FROM t1 WHERE a NOT IN tb;
318 } {1 {sub-select returns 2 columns - expected 1}}
320 # IN clauses in CHECK constraints. Ticket #1645
326 CHECK( a IN (111,222,333) )
328 INSERT INTO t5 VALUES(111);
334 INSERT INTO t5 VALUES(4);
336 } {1 {CHECK constraint failed: a IN (111,222,333)}}
340 # Type affinity applied to the right-hand side of an IN operator.
344 CREATE TABLE t6(a,b NUMERIC);
345 INSERT INTO t6 VALUES(1,2);
346 INSERT INTO t6 VALUES(2,3);
347 SELECT * FROM t6 WHERE b IN (2);
351 # The '2' should be coerced into 2 because t6.b is NUMERIC
353 SELECT * FROM t6 WHERE b IN ('2');
357 # No coercion should occur here because of the unary + before b.
359 SELECT * FROM t6 WHERE +b IN ('2');
363 # No coercion because column a as affinity NONE
365 SELECT * FROM t6 WHERE a IN ('2');
370 SELECT * FROM t6 WHERE a IN (2);
374 # No coercion because column a as affinity NONE
376 SELECT * FROM t6 WHERE +a IN ('2');
380 # Test error conditions with expressions of the form IN(<compound select>).
385 CREATE TABLE t2(a, b, c);
386 CREATE TABLE t3(a, b, c);
391 SELECT * FROM t2 WHERE a IN (
392 SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2
395 } {1 {sub-select returns 2 columns - expected 1}}
398 SELECT * FROM t2 WHERE a IN (
399 SELECT a, b FROM t3 UNION SELECT a, b FROM t2
402 } {1 {sub-select returns 2 columns - expected 1}}
405 SELECT * FROM t2 WHERE a IN (
406 SELECT a, b FROM t3 EXCEPT SELECT a, b FROM t2
409 } {1 {sub-select returns 2 columns - expected 1}}
412 SELECT * FROM t2 WHERE a IN (
413 SELECT a, b FROM t3 INTERSECT SELECT a, b FROM t2
416 } {1 {sub-select returns 2 columns - expected 1}}
419 SELECT * FROM t2 WHERE a IN (
420 SELECT a, b FROM t3 UNION ALL SELECT a FROM t2
423 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
426 SELECT * FROM t2 WHERE a IN (
427 SELECT a, b FROM t3 UNION SELECT a FROM t2
430 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
433 SELECT * FROM t2 WHERE a IN (
434 SELECT a, b FROM t3 EXCEPT SELECT a FROM t2
437 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
440 SELECT * FROM t2 WHERE a IN (
441 SELECT a, b FROM t3 INTERSECT SELECT a FROM t2
444 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
450 SELECT * FROM t2 WHERE a IN (
451 SELECT a FROM t3 UNION ALL SELECT a, b FROM t2
454 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
457 SELECT * FROM t2 WHERE a IN (
458 SELECT a FROM t3 UNION SELECT a, b FROM t2
461 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
464 SELECT * FROM t2 WHERE a IN (
465 SELECT a FROM t3 EXCEPT SELECT a, b FROM t2
468 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
471 SELECT * FROM t2 WHERE a IN (
472 SELECT a FROM t3 INTERSECT SELECT a, b FROM t2
475 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
478 SELECT * FROM t2 WHERE a IN (
479 SELECT a, b FROM t3 UNION ALL SELECT a, b FROM t2
482 } {1 {sub-select returns 2 columns - expected 1}}
485 SELECT * FROM t2 WHERE a IN (
486 SELECT a, b FROM t3 UNION ALL SELECT a FROM t2
489 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
490 }; #ifcapable compound
493 #------------------------------------------------------------------------
494 # The following tests check that NULL is handled correctly when it
495 # appears as part of a set of values on the right-hand side of an
496 # IN or NOT IN operator.
498 # When it appears in such a set, NULL is handled as an "unknown value".
499 # If, because of the unknown value in the set, the result of the expression
500 # cannot be determined, then it itself evaluates to NULL.
503 # Warm body test to demonstrate the principles being tested:
508 1 IN (NULL, 1, 2), -- The value 1 is a member of the set, return true.
509 3 IN (NULL, 1, 2), -- Ambiguous, return NULL.
510 1 NOT IN (NULL, 1, 2), -- The value 1 is a member of the set, return false.
511 3 NOT IN (NULL, 1, 2) -- Ambiguous, return NULL.
517 CREATE TABLE t7(a, b, c NOT NULL);
518 INSERT INTO t7 VALUES(1, 1, 1);
519 INSERT INTO t7 VALUES(2, 2, 2);
520 INSERT INTO t7 VALUES(3, 3, 3);
521 INSERT INTO t7 VALUES(NULL, 4, 4);
522 INSERT INTO t7 VALUES(NULL, 5, 5);
527 execsql { SELECT 2 IN (SELECT a FROM t7) }
530 execsql { SELECT 6 IN (SELECT a FROM t7) }
534 execsql { SELECT 2 IN (SELECT b FROM t7) }
537 execsql { SELECT 6 IN (SELECT b FROM t7) }
541 execsql { SELECT 2 IN (SELECT c FROM t7) }
544 execsql { SELECT 6 IN (SELECT c FROM t7) }
550 2 NOT IN (SELECT a FROM t7),
551 6 NOT IN (SELECT a FROM t7),
552 2 NOT IN (SELECT b FROM t7),
553 6 NOT IN (SELECT b FROM t7),
554 2 NOT IN (SELECT c FROM t7),
555 6 NOT IN (SELECT c FROM t7)
564 WHERE inside.b BETWEEN outside.b+1 AND outside.b+2
566 FROM t7 AS outside ORDER BY b;
568 } {0 null null null 0}
575 WHERE inside.b BETWEEN outside.b+1 AND outside.b+2
577 FROM t7 AS outside ORDER BY b;
579 } {1 null null null 1}
583 CREATE INDEX i1 ON t7(a);
584 CREATE INDEX i2 ON t7(b);
585 CREATE INDEX i3 ON t7(c);
589 2 IN (SELECT a FROM t7),
590 6 IN (SELECT a FROM t7),
591 2 IN (SELECT b FROM t7),
592 6 IN (SELECT b FROM t7),
593 2 IN (SELECT c FROM t7),
594 6 IN (SELECT c FROM t7)
601 2 NOT IN (SELECT a FROM t7),
602 6 NOT IN (SELECT a FROM t7),
603 2 NOT IN (SELECT b FROM t7),
604 6 NOT IN (SELECT b FROM t7),
605 2 NOT IN (SELECT c FROM t7),
606 6 NOT IN (SELECT c FROM t7)
613 CREATE TABLE a(id INTEGER);
614 INSERT INTO a VALUES(1);
615 INSERT INTO a VALUES(2);
616 INSERT INTO a VALUES(3);
617 CREATE TABLE b(id INTEGER);
618 INSERT INTO b VALUES(NULL);
619 INSERT INTO b VALUES(3);
620 INSERT INTO b VALUES(4);
621 INSERT INTO b VALUES(5);
623 SELECT * FROM a WHERE id NOT IN (SELECT id FROM b);
628 CREATE INDEX i5 ON b(id);
629 SELECT * FROM a WHERE id NOT IN (SELECT id FROM b);
635 SELECT 0 WHERE (SELECT 0,0) OR (0 IN (1,2));
637 } {1 {sub-select returns 2 columns - expected 1}}
644 # At one point the following was causing valgrind to report a "jump
645 # depends on unitialized location" problem.
647 do_execsql_test in-14.0 {
649 INSERT INTO c1 VALUES(1), (2), (4), (3);
651 do_execsql_test in-14.1 {
652 SELECT * FROM c1 WHERE a IN (SELECT a FROM c1) ORDER BY 1
655 # 2019-02-20 Ticket https://www.sqlite.org/src/tktview/df46dfb631f75694fbb97033b69
657 do_execsql_test in-15.0 {
658 DROP TABLE IF EXISTS t1;
659 CREATE TABLE IF NOT EXISTS t1(id INTEGER PRIMARY KEY);
660 INSERT INTO t1 VALUES(1);
661 SELECT a.id FROM t1 AS a JOIN t1 AS b ON a.id=b.id WHERE a.id IN (1,2,3);
663 do_execsql_test in-15.1 {
664 DROP TABLE IF EXISTS t2;
665 CREATE TABLE t2(a INTEGER PRIMARY KEY,b);
666 INSERT INTO t2 VALUES(1,11);
667 INSERT INTO t2 VALUES(2,22);
668 INSERT INTO t2 VALUES(3,33);
669 SELECT b, a IN (3,4,5) FROM t2 ORDER BY b;
671 do_execsql_test in-15.2 {
672 DROP TABLE IF EXISTS t3;
673 CREATE TABLE t3(x INTEGER PRIMARY KEY);
674 INSERT INTO t3 VALUES(8);
675 SELECT CASE WHEN x NOT IN (5,6,7) THEN 'yes' ELSE 'no' END FROM t3;
676 SELECT CASE WHEN x NOT IN (NULL,6,7) THEN 'yes' ELSE 'no' END FROM t3;
678 do_execsql_test in-15.3 {
679 SELECT CASE WHEN x NOT IN (5,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3;
680 SELECT CASE WHEN x NOT IN (NULL,6,7) OR x=0 THEN 'yes' ELSE 'no' END FROM t3;
682 do_execsql_test in-15.4 {
683 DROP TABLE IF EXISTS t4;
684 CREATE TABLE t4(a INTEGER PRIMARY KEY, b INT);
685 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
686 INSERT INTO t4(a,b) SELECT x, x+100 FROM c;
687 SELECT b FROM t4 WHERE a IN (3,null,8) ORDER BY +b;
689 do_execsql_test in-15.5 {
690 SELECT b FROM t4 WHERE a NOT IN (3,null,8);
692 do_execsql_test in-15.6 {
693 DROP TABLE IF EXISTS t5;
694 DROP TABLE IF EXISTS t6;
695 CREATE TABLE t5(id INTEGER PRIMARY KEY, name TEXT);
696 CREATE TABLE t6(id INTEGER PRIMARY KEY, name TEXT, t5_id INT);
697 INSERT INTO t5 VALUES(1,'Alice'),(2,'Emma');
698 INSERT INTO t6 VALUES(1,'Bob',1),(2,'Cindy',1),(3,'Dave',2);
700 FROM t5 AS 'a' JOIN t5 AS 'b' ON b.id=a.id
705 AND t6.t5_id IS NOT NULL
708 FROM (SELECT t6.id, count(*) AS x
718 #-------------------------------------------------------------------------
720 do_execsql_test in-16.0 {
721 CREATE TABLE x1(a, b);
722 INSERT INTO x1(a) VALUES(1), (2), (3), (4), (5), (6);
723 CREATE INDEX x1i ON x1(a, b);
726 do_execsql_test in-16.1 {
728 WHERE a IN (SELECT a FROM x1 WHERE (a%2)==0)
732 do_execsql_test in-16.2 {
734 WHERE a IN (SELECT a FROM x1 WHERE (a%7)==0)
739 # https://www.sqlite.org/src/info/57353f8243c637c0
741 do_execsql_test in-17.1 {
744 do_execsql_test in-17.2 {
745 SELECT 1 IN ('1' COLLATE nocase);
747 do_execsql_test in-17.3 {
748 SELECT 1 IN (CAST('1' AS text));
750 do_execsql_test in-17.4 {
751 SELECT 1 IN (CAST('1' AS text) COLLATE nocase);
754 # 2019-08-27 ticket https://sqlite.org/src/info/dbaf8a6820be1ece
756 do_execsql_test in-18.1 {
757 DROP TABLE IF EXISTS t0;
758 CREATE TABLE t0(c0 INT UNIQUE);
759 INSERT INTO t0(c0) VALUES (1);
760 SELECT * FROM t0 WHERE '1' IN (t0.c0);
763 # 2019-09-02 ticket https://www.sqlite.org/src/info/2841e99d104c6436
764 # For the IN_INDEX_NOOP optimization, apply REAL affinity to the LHS
765 # values prior to comparison if the RHS has REAL affinity.
767 # Also ticket https://sqlite.org/src/info/29f635e0af71234b
769 do_execsql_test in-19.10 {
770 DROP TABLE IF EXISTS t0;
771 CREATE TABLE t0(c0 REAL UNIQUE);
772 INSERT INTO t0(c0) VALUES(2.0625E00);
773 SELECT 1 FROM t0 WHERE c0 IN ('2.0625');
775 do_execsql_test in-19.20 {
776 SELECT c0 IN ('2.0625') FROM t0;
778 do_execsql_test in-19.21 {
779 SELECT c0 = ('2.0625') FROM t0;
781 do_execsql_test in-19.22 {
782 SELECT c0 = ('0.20625e+01') FROM t0;
784 do_execsql_test in-19.30 {
785 SELECT c0 IN ('2.0625',2,3) FROM t0;
787 do_execsql_test in-19.40 {
789 CREATE TABLE t0(c0 TEXT, c1 REAL, c2, PRIMARY KEY(c2, c0, c1));
790 CREATE INDEX i0 ON t0(c1 IN (c0));
791 INSERT INTO t0(c0, c2) VALUES (0, NULL) ON CONFLICT(c2, c1, c0) DO NOTHING;
792 PRAGMA integrity_check;
797 do_execsql_test in-20.1 {
798 SELECT (1 IN (2 IS TRUE));
801 # Forum post: https://sqlite.org/forum/forumpost/5782619992.
804 do_execsql_test in-21.1 {
806 SELECT COUNT(*) FROM t0 ORDER BY (t0.c0 IN ());
809 # Ignore extra parentheses around a subquery on the RHS of an IN operator,
810 # because that is what PostgreSQL does.
812 do_execsql_test in-22.1 {
813 DROP TABLE IF EXISTS t1;
814 CREATE TABLE t1(x INT PRIMARY KEY, y INT);
815 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<8)
816 INSERT INTO t1(x,y) SELECT x, x*100 FROM c;
817 DROP TABLE IF EXISTS t2;
818 CREATE TABLE t2(a INT);
819 INSERT INTO t2 VALUES(2),(4),(6);
820 SELECT * FROM t1 WHERE x IN (SELECT a FROM t2);
821 } {2 200 4 400 6 600}
822 do_execsql_test in-22.2 {
823 SELECT * FROM t1 WHERE x IN ((SELECT a FROM t2));
824 } {2 200 4 400 6 600}
825 do_execsql_test in-22.3 {
826 SELECT * FROM t1 WHERE x IN (((SELECT a FROM t2)));
827 } {2 200 4 400 6 600}
828 do_execsql_test in-22.4 {
829 SELECT * FROM t1 WHERE x IN ((((((SELECT a FROM t2))))));
830 } {2 200 4 400 6 600}
832 # 2023-04-04 https://sqlite.org/forum/forumpost/dc16ec63d3
833 # Faulty assert() statement in the IN optimization.
835 do_execsql_test in-23.0 {
836 DROP TABLE IF EXISTS t4;
837 CREATE TABLE t4(a TEXT, b INT);
838 INSERT INTO t4(a,b) VALUES('abc',0),('ABC',1),('def',2);
839 CREATE INDEX t4x ON t4(a, +a COLLATE NOCASE);
840 SELECT a0.a, group_concat(a1.a) AS b
841 FROM t4 AS a0 JOIN t4 AS a1
843 HAVING (SELECT sum( (a1.a == +a0.a COLLATE NOCASE) IN (SELECT b FROM t4)));
844 } {ABC abc,ABC,def abc abc,ABC,def def abc,ABC,def}
845 do_execsql_test in-23.0-b {
846 SELECT a0.a, group_concat(a1.a) AS b
847 FROM t4 AS a0 JOIN t4 AS a1
849 HAVING (SELECT sum( (a1.a GLOB +a0.a COLLATE NOCASE) IN (SELECT b FROM t4)));
850 } {ABC abc,ABC,def abc abc,ABC,def def abc,ABC,def}
852 # Follow-up forum/forumpost/0713a16a44
854 do_execsql_test in-23.1 {
857 WHERE (SELECT count(0=NOT+a COLLATE NOCASE IN (SELECT 0))