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 UNION, INTERSECT and EXCEPT operators
16 # in SELECT statements.
18 # $Id: select4.test,v 1.13 2003/02/02 12:41:27 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Build some test data
25 set fd [open data1.txt w]
26 for {set i 1} {$i<32} {incr i} {
27 for {set j 0} {pow(2,$j)<$i} {incr j} {}
32 CREATE TABLE t1(n int, log int);
33 COPY t1 FROM 'data1.txt'
38 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
43 do_test select4-1.1a {
44 lsort [execsql {SELECT DISTINCT log FROM t1}]
46 do_test select4-1.1b {
47 lsort [execsql {SELECT n FROM t1 WHERE log=3}]
49 do_test select4-1.1c {
51 SELECT DISTINCT log FROM t1
53 SELECT n FROM t1 WHERE log=3
56 } {0 1 2 3 4 5 5 6 7 8}
57 do_test select4-1.1d {
60 SELECT DISTINCT log FROM t1
62 SELECT n FROM t1 WHERE log=3
66 } {0 1 2 3 4 5 5 6 7 8}
67 execsql {DROP TABLE t2}
68 do_test select4-1.1e {
71 SELECT DISTINCT log FROM t1
73 SELECT n FROM t1 WHERE log=3
77 } {8 7 6 5 5 4 3 2 1 0}
78 execsql {DROP TABLE t2}
79 do_test select4-1.1f {
81 SELECT DISTINCT log FROM t1
83 SELECT n FROM t1 WHERE log=2
86 do_test select4-1.1g {
89 SELECT DISTINCT log FROM t1
91 SELECT n FROM t1 WHERE log=2;
95 execsql {DROP TABLE t2}
98 SELECT log FROM t1 WHERE n IN
99 (SELECT DISTINCT log FROM t1 UNION ALL
100 SELECT n FROM t1 WHERE log=3)
104 do_test select4-1.3 {
105 set v [catch {execsql {
106 SELECT DISTINCT log FROM t1 ORDER BY log
108 SELECT n FROM t1 WHERE log=3
112 } {1 {ORDER BY clause should come after UNION ALL not before}}
116 do_test select4-2.1 {
118 SELECT DISTINCT log FROM t1
120 SELECT n FROM t1 WHERE log=3
123 } {0 1 2 3 4 5 6 7 8}
124 do_test select4-2.2 {
126 SELECT log FROM t1 WHERE n IN
127 (SELECT DISTINCT log FROM t1 UNION
128 SELECT n FROM t1 WHERE log=3)
132 do_test select4-2.3 {
133 set v [catch {execsql {
134 SELECT DISTINCT log FROM t1 ORDER BY log
136 SELECT n FROM t1 WHERE log=3
140 } {1 {ORDER BY clause should come after UNION not before}}
144 do_test select4-3.1.1 {
146 SELECT DISTINCT log FROM t1
148 SELECT n FROM t1 WHERE log=3
152 do_test select4-3.1.2 {
155 SELECT DISTINCT log FROM t1
157 SELECT n FROM t1 WHERE log=3
162 execsql {DROP TABLE t2}
163 do_test select4-3.1.3 {
166 SELECT DISTINCT log FROM t1
168 SELECT n FROM t1 WHERE log=3
173 execsql {DROP TABLE t2}
174 do_test select4-3.2 {
176 SELECT log FROM t1 WHERE n IN
177 (SELECT DISTINCT log FROM t1 EXCEPT
178 SELECT n FROM t1 WHERE log=3)
182 do_test select4-3.3 {
183 set v [catch {execsql {
184 SELECT DISTINCT log FROM t1 ORDER BY log
186 SELECT n FROM t1 WHERE log=3
190 } {1 {ORDER BY clause should come after EXCEPT not before}}
194 do_test select4-4.1.1 {
196 SELECT DISTINCT log FROM t1
198 SELECT n FROM t1 WHERE log=3
202 do_test select4-4.1.2 {
204 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
206 SELECT n FROM t1 WHERE log=3
210 do_test select4-4.1.3 {
213 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
215 SELECT n FROM t1 WHERE log=3
220 execsql {DROP TABLE t2}
221 do_test select4-4.1.4 {
224 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
226 SELECT n FROM t1 WHERE log=3
231 execsql {DROP TABLE t2}
232 do_test select4-4.2 {
234 SELECT log FROM t1 WHERE n IN
235 (SELECT DISTINCT log FROM t1 INTERSECT
236 SELECT n FROM t1 WHERE log=3)
240 do_test select4-4.3 {
241 set v [catch {execsql {
242 SELECT DISTINCT log FROM t1 ORDER BY log
244 SELECT n FROM t1 WHERE log=3
248 } {1 {ORDER BY clause should come after INTERSECT not before}}
250 # Various error messages while processing UNION or INTERSECT
252 do_test select4-5.1 {
253 set v [catch {execsql {
254 SELECT DISTINCT log FROM t2
256 SELECT n FROM t1 WHERE log=3
260 } {1 {no such table: t2}}
261 do_test select4-5.2 {
262 set v [catch {execsql {
263 SELECT DISTINCT log AS "xyzzy" FROM t1
265 SELECT n FROM t1 WHERE log=3
269 } {0 {0 1 2 3 4 5 5 6 7 8}}
270 do_test select4-5.2b {
271 set v [catch {execsql {
272 SELECT DISTINCT log AS xyzzy FROM t1
274 SELECT n FROM t1 WHERE log=3
278 } {0 {0 1 2 3 4 5 5 6 7 8}}
279 do_test select4-5.2c {
280 set v [catch {execsql {
281 SELECT DISTINCT log FROM t1
283 SELECT n FROM t1 WHERE log=3
287 } {1 {ORDER BY term number 1 does not match any result column}}
288 do_test select4-5.2d {
289 set v [catch {execsql {
290 SELECT DISTINCT log FROM t1
292 SELECT n FROM t1 WHERE log=3
296 } {1 {ORDER BY term number 1 does not match any result column}}
297 do_test select4-5.2e {
298 set v [catch {execsql {
299 SELECT DISTINCT log FROM t1
301 SELECT n FROM t1 WHERE log=3
305 } {0 {0 1 2 3 4 5 5 6 7 8}}
306 do_test select4-5.2f {
308 SELECT DISTINCT log FROM t1
310 SELECT n FROM t1 WHERE log=3
313 } {0 {0 1 2 3 4 5 5 6 7 8}}
314 do_test select4-5.2g {
316 SELECT DISTINCT log FROM t1
318 SELECT n FROM t1 WHERE log=3
321 } {0 {0 1 2 3 4 5 5 6 7 8}}
322 do_test select4-5.2h {
324 SELECT DISTINCT log FROM t1
326 SELECT n FROM t1 WHERE log=3
329 } {1 {ORDER BY position 2 should be between 1 and 1}}
330 do_test select4-5.2i {
332 SELECT DISTINCT 1, log FROM t1
334 SELECT 2, n FROM t1 WHERE log=3
337 } {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
338 do_test select4-5.2j {
340 SELECT DISTINCT 1, log FROM t1
342 SELECT 2, n FROM t1 WHERE log=3
345 } {0 {1 5 1 4 1 3 1 2 1 1 1 0 2 8 2 7 2 6 2 5}}
346 do_test select4-5.2k {
348 SELECT DISTINCT 1, log FROM t1
350 SELECT 2, n FROM t1 WHERE log=3
353 } {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
354 do_test select4-5.3 {
355 set v [catch {execsql {
356 SELECT DISTINCT log, n FROM t1
358 SELECT n FROM t1 WHERE log=3
362 } {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
363 do_test select4-5.4 {
364 set v [catch {execsql {
365 SELECT log FROM t1 WHERE n=2
367 SELECT log FROM t1 WHERE n=3
369 SELECT log FROM t1 WHERE n=4
371 SELECT log FROM t1 WHERE n=5
377 do_test select4-6.1 {
379 SELECT log, count(*) as cnt FROM t1 GROUP BY log
381 SELECT log, n FROM t1 WHERE n=7
384 } {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
385 do_test select4-6.2 {
387 SELECT log, count(*) FROM t1 GROUP BY log
389 SELECT log, n FROM t1 WHERE n=7
390 ORDER BY count(*), log;
392 } {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
394 # NULLs are indistinct for the UNION operator.
395 # Make sure the UNION operator recognizes this
397 do_test select4-6.3 {
399 SELECT NULL UNION SELECT NULL UNION
400 SELECT 1 UNION SELECT 2 AS 'x'
404 do_test select4-6.3.1 {
406 SELECT NULL UNION ALL SELECT NULL UNION ALL
407 SELECT 1 UNION ALL SELECT 2 AS 'x'
412 # Make sure the DISTINCT keyword treats NULLs as indistinct.
414 do_test select4-6.4 {
417 SELECT NULL, 1 UNION ALL SELECT NULL, 1
421 do_test select4-6.5 {
423 SELECT DISTINCT * FROM (
424 SELECT NULL, 1 UNION ALL SELECT NULL, 1
428 do_test select4-6.6 {
430 SELECT DISTINCT * FROM (
431 SELECT 1,2 UNION ALL SELECT 1,2
436 # Test distinctness of NULL in other ways.
438 do_test select4-6.7 {
440 SELECT NULL EXCEPT SELECT NULL
445 # Make sure column names are correct when a compound select appears as
446 # an expression in the WHERE clause.
448 do_test select4-7.1 {
450 CREATE TABLE t2 AS SELECT log AS 'x', count(*) AS 'y' FROM t1 GROUP BY log;
451 SELECT * FROM t2 ORDER BY x;
453 } {0 1 1 1 2 2 3 4 4 8 5 15}
454 do_test select4-7.2 {
456 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 INTERSECT SELECT x FROM t2)
459 } {n 1 log 0 n 2 log 1 n 3 log 2 n 4 log 2 n 5 log 3}
460 do_test select4-7.3 {
462 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 EXCEPT SELECT x FROM t2)
465 } {n 6 log 3 n 7 log 3}
466 do_test select4-7.4 {
468 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 UNION SELECT x FROM t2)
471 } {n 1 log 0 n 2 log 1}
473 # Make sure DISTINCT works appropriately on TEXT and NUMERIC columns.
475 do_test select4-8.1 {
478 CREATE TABLE t3(a text, b float, c text);
479 INSERT INTO t3 VALUES(1, 1.1, '1.1');
480 INSERT INTO t3 VALUES(2, 1.10, '1.10');
481 INSERT INTO t3 VALUES(3, 1.10, '1.1');
482 INSERT INTO t3 VALUES(4, 1.1, '1.10');
483 INSERT INTO t3 VALUES(5, 1.2, '1.2');
484 INSERT INTO t3 VALUES(6, 1.3, '1.3');
488 SELECT DISTINCT b FROM t3 ORDER BY c;
491 do_test select4-8.2 {
493 SELECT DISTINCT c FROM t3 ORDER BY c;