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 UNION, INTERSECT and EXCEPT operators
13 # in SELECT statements.
15 # $Id: select4.test,v 1.30 2009/04/16 00:24:24 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Most tests in this file depend on compound-select. But there are a couple
21 # right at the end that test DISTINCT, so we cannot omit the entire file.
25 # Build some test data
28 CREATE TABLE t1(n int, log int);
31 for {set i 1} {$i<32} {incr i} {
32 for {set j 0} {(1<<$j)<$i} {incr j} {}
33 execsql "INSERT INTO t1 VALUES($i,$j)"
40 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
45 do_test select4-1.1a {
46 lsort [execsql {SELECT DISTINCT log FROM t1}]
48 do_test select4-1.1b {
49 lsort [execsql {SELECT n FROM t1 WHERE log=3}]
51 do_test select4-1.1c {
53 SELECT DISTINCT log FROM t1
55 SELECT n FROM t1 WHERE log=3
58 } {0 1 2 3 4 5 5 6 7 8}
59 do_test select4-1.1d {
62 SELECT DISTINCT log FROM t1
64 SELECT n FROM t1 WHERE log=3
68 } {0 1 2 3 4 5 5 6 7 8}
69 execsql {DROP TABLE t2}
70 do_test select4-1.1e {
73 SELECT DISTINCT log FROM t1
75 SELECT n FROM t1 WHERE log=3
79 } {8 7 6 5 5 4 3 2 1 0}
80 execsql {DROP TABLE t2}
81 do_test select4-1.1f {
83 SELECT DISTINCT log FROM t1
85 SELECT n FROM t1 WHERE log=2
88 do_test select4-1.1g {
91 SELECT DISTINCT log FROM t1
93 SELECT n FROM t1 WHERE log=2;
97 execsql {DROP TABLE t2}
101 SELECT log FROM t1 WHERE n IN
102 (SELECT DISTINCT log FROM t1 UNION ALL
103 SELECT n FROM t1 WHERE log=3)
108 do_test select4-1.3 {
109 set v [catch {execsql {
110 SELECT DISTINCT log FROM t1 ORDER BY log
112 SELECT n FROM t1 WHERE log=3
116 } {1 {ORDER BY clause should come after UNION ALL not before}}
120 do_test select4-2.1 {
122 SELECT DISTINCT log FROM t1
124 SELECT n FROM t1 WHERE log=3
127 } {0 1 2 3 4 5 6 7 8}
129 do_test select4-2.2 {
131 SELECT log FROM t1 WHERE n IN
132 (SELECT DISTINCT log FROM t1 UNION
133 SELECT n FROM t1 WHERE log=3)
138 do_test select4-2.3 {
139 set v [catch {execsql {
140 SELECT DISTINCT log FROM t1 ORDER BY log
142 SELECT n FROM t1 WHERE log=3
146 } {1 {ORDER BY clause should come after UNION not before}}
150 do_test select4-3.1.1 {
152 SELECT DISTINCT log FROM t1
154 SELECT n FROM t1 WHERE log=3
158 do_test select4-3.1.2 {
161 SELECT DISTINCT log FROM t1
163 SELECT n FROM t1 WHERE log=3
168 execsql {DROP TABLE t2}
169 do_test select4-3.1.3 {
172 SELECT DISTINCT log FROM t1
174 SELECT n FROM t1 WHERE log=3
179 execsql {DROP TABLE t2}
181 do_test select4-3.2 {
183 SELECT log FROM t1 WHERE n IN
184 (SELECT DISTINCT log FROM t1 EXCEPT
185 SELECT n FROM t1 WHERE log=3)
190 do_test select4-3.3 {
191 set v [catch {execsql {
192 SELECT DISTINCT log FROM t1 ORDER BY log
194 SELECT n FROM t1 WHERE log=3
198 } {1 {ORDER BY clause should come after EXCEPT not before}}
202 do_test select4-4.1.1 {
204 SELECT DISTINCT log FROM t1
206 SELECT n FROM t1 WHERE log=3
211 do_test select4-4.1.2 {
213 SELECT DISTINCT log FROM t1
217 SELECT n FROM t1 WHERE log=3
222 do_test select4-4.1.3 {
225 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
227 SELECT n FROM t1 WHERE log=3
232 execsql {DROP TABLE t2}
233 do_test select4-4.1.4 {
236 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
238 SELECT n FROM t1 WHERE log=3
243 execsql {DROP TABLE t2}
245 do_test select4-4.2 {
247 SELECT log FROM t1 WHERE n IN
248 (SELECT DISTINCT log FROM t1 INTERSECT
249 SELECT n FROM t1 WHERE log=3)
254 do_test select4-4.3 {
255 set v [catch {execsql {
256 SELECT DISTINCT log FROM t1 ORDER BY log
258 SELECT n FROM t1 WHERE log=3
262 } {1 {ORDER BY clause should come after INTERSECT not before}}
264 # Various error messages while processing UNION or INTERSECT
266 do_test select4-5.1 {
267 set v [catch {execsql {
268 SELECT DISTINCT log FROM t2
270 SELECT n FROM t1 WHERE log=3
274 } {1 {no such table: t2}}
275 do_test select4-5.2 {
276 set v [catch {execsql {
277 SELECT DISTINCT log AS "xyzzy" FROM t1
279 SELECT n FROM t1 WHERE log=3
283 } {0 {0 1 2 3 4 5 5 6 7 8}}
284 do_test select4-5.2b {
285 set v [catch {execsql {
286 SELECT DISTINCT log AS xyzzy FROM t1
288 SELECT n FROM t1 WHERE log=3
292 } {0 {0 1 2 3 4 5 5 6 7 8}}
293 do_test select4-5.2c {
294 set v [catch {execsql {
295 SELECT DISTINCT log FROM t1
297 SELECT n FROM t1 WHERE log=3
301 } {1 {1st ORDER BY term does not match any column in the result set}}
302 do_test select4-5.2d {
303 set v [catch {execsql {
304 SELECT DISTINCT log FROM t1
306 SELECT n FROM t1 WHERE log=3
310 } {1 {1st ORDER BY term does not match any column in the result set}}
311 do_test select4-5.2e {
312 set v [catch {execsql {
313 SELECT DISTINCT log FROM t1
315 SELECT n FROM t1 WHERE log=3
319 } {0 {0 1 2 3 4 5 5 6 7 8}}
320 do_test select4-5.2f {
322 SELECT DISTINCT log FROM t1
324 SELECT n FROM t1 WHERE log=3
327 } {0 {0 1 2 3 4 5 5 6 7 8}}
328 do_test select4-5.2g {
330 SELECT DISTINCT log FROM t1
332 SELECT n FROM t1 WHERE log=3
335 } {0 {0 1 2 3 4 5 5 6 7 8}}
336 do_test select4-5.2h {
338 SELECT DISTINCT log FROM t1
340 SELECT n FROM t1 WHERE log=3
343 } {1 {1st ORDER BY term out of range - should be between 1 and 1}}
344 do_test select4-5.2i {
346 SELECT DISTINCT 1, log FROM t1
348 SELECT 2, n FROM t1 WHERE log=3
351 } {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
352 do_test select4-5.2j {
354 SELECT DISTINCT 1, log FROM t1
356 SELECT 2, n FROM t1 WHERE log=3
359 } {0 {1 5 1 4 1 3 1 2 1 1 1 0 2 8 2 7 2 6 2 5}}
360 do_test select4-5.2k {
362 SELECT DISTINCT 1, log FROM t1
364 SELECT 2, n FROM t1 WHERE log=3
367 } {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
368 do_test select4-5.3 {
369 set v [catch {execsql {
370 SELECT DISTINCT log, n FROM t1
372 SELECT n FROM t1 WHERE log=3
376 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
377 do_test select4-5.3-3807-1 {
379 SELECT 1 UNION SELECT 2, 3 UNION SELECT 4, 5 ORDER BY 1;
381 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
382 do_test select4-5.4 {
383 set v [catch {execsql {
384 SELECT log FROM t1 WHERE n=2
386 SELECT log FROM t1 WHERE n=3
388 SELECT log FROM t1 WHERE n=4
390 SELECT log FROM t1 WHERE n=5
396 do_test select4-6.1 {
398 SELECT log, count(*) as cnt FROM t1 GROUP BY log
400 SELECT log, n FROM t1 WHERE n=7
403 } {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
404 do_test select4-6.2 {
406 SELECT log, count(*) FROM t1 GROUP BY log
408 SELECT log, n FROM t1 WHERE n=7
409 ORDER BY count(*), log;
411 } {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
413 # NULLs are indistinct for the UNION operator.
414 # Make sure the UNION operator recognizes this
416 do_test select4-6.3 {
418 SELECT NULL UNION SELECT NULL UNION
419 SELECT 1 UNION SELECT 2 AS 'x'
423 do_test select4-6.3.1 {
425 SELECT NULL UNION ALL SELECT NULL UNION ALL
426 SELECT 1 UNION ALL SELECT 2 AS 'x'
431 # Make sure the DISTINCT keyword treats NULLs as indistinct.
434 do_test select4-6.4 {
437 SELECT NULL, 1 UNION ALL SELECT NULL, 1
441 do_test select4-6.5 {
443 SELECT DISTINCT * FROM (
444 SELECT NULL, 1 UNION ALL SELECT NULL, 1
448 do_test select4-6.6 {
450 SELECT DISTINCT * FROM (
451 SELECT 1,2 UNION ALL SELECT 1,2
457 # Test distinctness of NULL in other ways.
459 do_test select4-6.7 {
461 SELECT NULL EXCEPT SELECT NULL
466 # Make sure column names are correct when a compound select appears as
467 # an expression in the WHERE clause.
469 do_test select4-7.1 {
471 CREATE TABLE t2 AS SELECT log AS 'x', count(*) AS 'y' FROM t1 GROUP BY log;
472 SELECT * FROM t2 ORDER BY x;
474 } {0 1 1 1 2 2 3 4 4 8 5 15}
476 do_test select4-7.2 {
478 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 INTERSECT SELECT x FROM t2)
481 } {n 1 log 0 n 2 log 1 n 3 log 2 n 4 log 2 n 5 log 3}
482 do_test select4-7.3 {
484 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 EXCEPT SELECT x FROM t2)
487 } {n 6 log 3 n 7 log 3}
488 do_test select4-7.4 {
490 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 UNION SELECT x FROM t2)
493 } {n 1 log 0 n 2 log 1}
494 } ;# ifcapable subquery
496 } ;# ifcapable compound
498 # Make sure DISTINCT works appropriately on TEXT and NUMERIC columns.
499 do_test select4-8.1 {
502 CREATE TABLE t3(a text, b float, c text);
503 INSERT INTO t3 VALUES(1, 1.1, '1.1');
504 INSERT INTO t3 VALUES(2, 1.10, '1.10');
505 INSERT INTO t3 VALUES(3, 1.10, '1.1');
506 INSERT INTO t3 VALUES(4, 1.1, '1.10');
507 INSERT INTO t3 VALUES(5, 1.2, '1.2');
508 INSERT INTO t3 VALUES(6, 1.3, '1.3');
512 SELECT DISTINCT b FROM t3 ORDER BY c;
515 do_test select4-8.2 {
517 SELECT DISTINCT c FROM t3 ORDER BY c;
521 # Make sure the names of columns are taken from the right-most subquery
522 # right in a compound query. Ticket #1721
526 do_test select4-9.1 {
528 SELECT x, y FROM t2 UNION SELECT a, b FROM t3 ORDER BY x LIMIT 1
531 do_test select4-9.2 {
533 SELECT x, y FROM t2 UNION ALL SELECT a, b FROM t3 ORDER BY x LIMIT 1
536 do_test select4-9.3 {
538 SELECT x, y FROM t2 EXCEPT SELECT a, b FROM t3 ORDER BY x LIMIT 1
541 do_test select4-9.4 {
543 SELECT x, y FROM t2 INTERSECT SELECT 0 AS a, 1 AS b;
546 do_test select4-9.5 {
548 SELECT 0 AS x, 1 AS y
550 SELECT 2 AS p, 3 AS q
552 SELECT 4 AS a, 5 AS b
558 do_test select4-9.6 {
561 SELECT 0 AS x, 1 AS y
563 SELECT 2 AS p, 3 AS q
565 SELECT 4 AS a, 5 AS b
566 ) ORDER BY 1 LIMIT 1;
569 do_test select4-9.7 {
572 SELECT 0 AS x, 1 AS y
574 SELECT 2 AS p, 3 AS q
576 SELECT 4 AS a, 5 AS b
577 ) ORDER BY x LIMIT 1;
580 } ;# ifcapable subquery
582 do_test select4-9.8 {
584 SELECT 0 AS x, 1 AS y
586 SELECT 2 AS y, -3 AS x
591 do_test select4-9.9.1 {
593 SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a
598 do_test select4-9.9.2 {
600 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a)
604 do_test select4-9.10 {
606 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a)
610 do_test select4-9.11 {
612 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b)
616 do_test select4-9.12 {
618 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b)
622 } ;# ifcapable subquery
624 # Try combining DISTINCT, LIMIT, and OFFSET. Make sure they all work
627 do_test select4-10.1 {
629 SELECT DISTINCT log FROM t1 ORDER BY log
632 do_test select4-10.2 {
634 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 4
637 do_test select4-10.3 {
639 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0
642 do_test select4-10.4 {
644 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1
647 do_test select4-10.5 {
649 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1 OFFSET 2
652 do_test select4-10.6 {
654 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 3 OFFSET 2
657 do_test select4-10.7 {
659 SELECT DISTINCT log FROM t1 ORDER BY +log LIMIT 3 OFFSET 20
662 do_test select4-10.8 {
664 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0 OFFSET 3
667 do_test select4-10.9 {
669 SELECT DISTINCT max(n), log FROM t1 ORDER BY +log; -- LIMIT 2 OFFSET 1
673 # Make sure compound SELECTs with wildly different numbers of columns
674 # do not cause assertion faults due to register allocation issues.
676 do_test select4-11.1 {
678 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
682 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
683 do_test select4-11.2 {
687 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
689 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
690 do_test select4-11.3 {
692 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
696 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
697 do_test select4-11.4 {
701 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
703 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
704 do_test select4-11.5 {
706 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
710 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
711 do_test select4-11.6 {
715 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
717 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
718 do_test select4-11.7 {
720 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
724 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
725 do_test select4-11.8 {
729 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
731 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
733 do_test select4-11.11 {
743 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
745 } {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
746 do_test select4-11.12 {
754 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
758 } {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
759 do_test select4-11.13 {
765 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
771 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
772 do_test select4-11.14 {
776 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
784 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
785 do_test select4-11.15 {
787 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
797 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
799 do_test select4-12.1 {
802 SELECT 1 UNION SELECT 2,3 UNION SELECT 4,5 ORDER BY 1;
804 } {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
806 } ;# ifcapable compound
809 # Ticket [3557ad65a076c] - Incorrect DISTINCT processing with an
810 # indexed query using IN.
812 do_test select4-13.1 {
815 CREATE TABLE t13(a,b);
816 INSERT INTO t13 VALUES(1,1);
817 INSERT INTO t13 VALUES(2,1);
818 INSERT INTO t13 VALUES(3,1);
819 INSERT INTO t13 VALUES(2,2);
820 INSERT INTO t13 VALUES(3,2);
821 INSERT INTO t13 VALUES(4,2);
822 CREATE INDEX t13ab ON t13(a,b);
823 SELECT DISTINCT b from t13 WHERE a IN (1,2,3);
827 # 2014-02-18: Make sure compound SELECTs work with VALUES clauses
829 do_execsql_test select4-14.1 {
830 CREATE TABLE t14(a,b,c);
831 INSERT INTO t14 VALUES(1,2,3),(4,5,6);
832 SELECT * FROM t14 INTERSECT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
834 do_execsql_test select4-14.2 {
835 SELECT * FROM t14 INTERSECT VALUES(1,2,3);
837 do_execsql_test select4-14.3 {
839 UNION VALUES(3,2,1),(2,3,1),(1,2,3),(7,8,9),(4,5,6)
840 UNION SELECT * FROM t14 ORDER BY 1, 2, 3
841 } {1 2 3 2 3 1 3 2 1 4 5 6 7 8 9}
842 do_execsql_test select4-14.4 {
845 UNION SELECT * FROM t14 ORDER BY 1, 2, 3
846 } {1 2 3 3 2 1 4 5 6}
847 do_execsql_test select4-14.5 {
848 SELECT * FROM t14 EXCEPT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
850 do_execsql_test select4-14.6 {
851 SELECT * FROM t14 EXCEPT VALUES(1,2,3)
853 do_execsql_test select4-14.7 {
854 SELECT * FROM t14 EXCEPT VALUES(1,2,3) EXCEPT VALUES(4,5,6)
856 do_execsql_test select4-14.8 {
857 SELECT * FROM t14 EXCEPT VALUES('a','b','c') EXCEPT VALUES(4,5,6)
859 do_execsql_test select4-14.9 {
860 SELECT * FROM t14 UNION ALL VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
861 } {1 2 3 4 5 6 3 2 1 2 3 1 1 2 3 2 1 3}