xref: /sqlite-3.40.0/test/select4.test (revision c023e03e)
1# 2001 September 15
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
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.
9#
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.
14#
15# $Id: select4.test,v 1.13 2003/02/02 12:41:27 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22set fd [open data1.txt w]
23for {set i 1} {$i<32} {incr i} {
24  for {set j 0} {pow(2,$j)<$i} {incr j} {}
25  puts $fd "$i\t$j"
26}
27close $fd
28execsql {
29  CREATE TABLE t1(n int, log int);
30  COPY t1 FROM 'data1.txt'
31}
32file delete data1.txt
33
34do_test select4-1.0 {
35  execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
36} {0 1 2 3 4 5}
37
38# Union All operator
39#
40do_test select4-1.1a {
41  lsort [execsql {SELECT DISTINCT log FROM t1}]
42} {0 1 2 3 4 5}
43do_test select4-1.1b {
44  lsort [execsql {SELECT n FROM t1 WHERE log=3}]
45} {5 6 7 8}
46do_test select4-1.1c {
47  execsql {
48    SELECT DISTINCT log FROM t1
49    UNION ALL
50    SELECT n FROM t1 WHERE log=3
51    ORDER BY log;
52  }
53} {0 1 2 3 4 5 5 6 7 8}
54do_test select4-1.1d {
55  execsql {
56    CREATE TABLE t2 AS
57      SELECT DISTINCT log FROM t1
58      UNION ALL
59      SELECT n FROM t1 WHERE log=3
60      ORDER BY log;
61    SELECT * FROM t2;
62  }
63} {0 1 2 3 4 5 5 6 7 8}
64execsql {DROP TABLE t2}
65do_test select4-1.1e {
66  execsql {
67    CREATE TABLE t2 AS
68      SELECT DISTINCT log FROM t1
69      UNION ALL
70      SELECT n FROM t1 WHERE log=3
71      ORDER BY log DESC;
72    SELECT * FROM t2;
73  }
74} {8 7 6 5 5 4 3 2 1 0}
75execsql {DROP TABLE t2}
76do_test select4-1.1f {
77  execsql {
78    SELECT DISTINCT log FROM t1
79    UNION ALL
80    SELECT n FROM t1 WHERE log=2
81  }
82} {0 1 2 3 4 5 3 4}
83do_test select4-1.1g {
84  execsql {
85    CREATE TABLE t2 AS
86      SELECT DISTINCT log FROM t1
87      UNION ALL
88      SELECT n FROM t1 WHERE log=2;
89    SELECT * FROM t2;
90  }
91} {0 1 2 3 4 5 3 4}
92execsql {DROP TABLE t2}
93do_test select4-1.2 {
94  execsql {
95    SELECT log FROM t1 WHERE n IN
96      (SELECT DISTINCT log FROM t1 UNION ALL
97       SELECT n FROM t1 WHERE log=3)
98    ORDER BY log;
99  }
100} {0 1 2 2 3 3 3 3}
101do_test select4-1.3 {
102  set v [catch {execsql {
103    SELECT DISTINCT log FROM t1 ORDER BY log
104    UNION ALL
105    SELECT n FROM t1 WHERE log=3
106    ORDER BY log;
107  }} msg]
108  lappend v $msg
109} {1 {ORDER BY clause should come after UNION ALL not before}}
110
111# Union operator
112#
113do_test select4-2.1 {
114  execsql {
115    SELECT DISTINCT log FROM t1
116    UNION
117    SELECT n FROM t1 WHERE log=3
118    ORDER BY log;
119  }
120} {0 1 2 3 4 5 6 7 8}
121do_test select4-2.2 {
122  execsql {
123    SELECT log FROM t1 WHERE n IN
124      (SELECT DISTINCT log FROM t1 UNION
125       SELECT n FROM t1 WHERE log=3)
126    ORDER BY log;
127  }
128} {0 1 2 2 3 3 3 3}
129do_test select4-2.3 {
130  set v [catch {execsql {
131    SELECT DISTINCT log FROM t1 ORDER BY log
132    UNION
133    SELECT n FROM t1 WHERE log=3
134    ORDER BY log;
135  }} msg]
136  lappend v $msg
137} {1 {ORDER BY clause should come after UNION not before}}
138
139# Except operator
140#
141do_test select4-3.1.1 {
142  execsql {
143    SELECT DISTINCT log FROM t1
144    EXCEPT
145    SELECT n FROM t1 WHERE log=3
146    ORDER BY log;
147  }
148} {0 1 2 3 4}
149do_test select4-3.1.2 {
150  execsql {
151    CREATE TABLE t2 AS
152      SELECT DISTINCT log FROM t1
153      EXCEPT
154      SELECT n FROM t1 WHERE log=3
155      ORDER BY log;
156    SELECT * FROM t2;
157  }
158} {0 1 2 3 4}
159execsql {DROP TABLE t2}
160do_test select4-3.1.3 {
161  execsql {
162    CREATE TABLE t2 AS
163      SELECT DISTINCT log FROM t1
164      EXCEPT
165      SELECT n FROM t1 WHERE log=3
166      ORDER BY log DESC;
167    SELECT * FROM t2;
168  }
169} {4 3 2 1 0}
170execsql {DROP TABLE t2}
171do_test select4-3.2 {
172  execsql {
173    SELECT log FROM t1 WHERE n IN
174      (SELECT DISTINCT log FROM t1 EXCEPT
175       SELECT n FROM t1 WHERE log=3)
176    ORDER BY log;
177  }
178} {0 1 2 2}
179do_test select4-3.3 {
180  set v [catch {execsql {
181    SELECT DISTINCT log FROM t1 ORDER BY log
182    EXCEPT
183    SELECT n FROM t1 WHERE log=3
184    ORDER BY log;
185  }} msg]
186  lappend v $msg
187} {1 {ORDER BY clause should come after EXCEPT not before}}
188
189# Intersect operator
190#
191do_test select4-4.1.1 {
192  execsql {
193    SELECT DISTINCT log FROM t1
194    INTERSECT
195    SELECT n FROM t1 WHERE log=3
196    ORDER BY log;
197  }
198} {5}
199do_test select4-4.1.2 {
200  execsql {
201    SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
202    INTERSECT
203    SELECT n FROM t1 WHERE log=3
204    ORDER BY log;
205  }
206} {5 6}
207do_test select4-4.1.3 {
208  execsql {
209    CREATE TABLE t2 AS
210      SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
211      INTERSECT
212      SELECT n FROM t1 WHERE log=3
213      ORDER BY log;
214    SELECT * FROM t2;
215  }
216} {5 6}
217execsql {DROP TABLE t2}
218do_test select4-4.1.4 {
219  execsql {
220    CREATE TABLE t2 AS
221      SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
222      INTERSECT
223      SELECT n FROM t1 WHERE log=3
224      ORDER BY log DESC;
225    SELECT * FROM t2;
226  }
227} {6 5}
228execsql {DROP TABLE t2}
229do_test select4-4.2 {
230  execsql {
231    SELECT log FROM t1 WHERE n IN
232      (SELECT DISTINCT log FROM t1 INTERSECT
233       SELECT n FROM t1 WHERE log=3)
234    ORDER BY log;
235  }
236} {3}
237do_test select4-4.3 {
238  set v [catch {execsql {
239    SELECT DISTINCT log FROM t1 ORDER BY log
240    INTERSECT
241    SELECT n FROM t1 WHERE log=3
242    ORDER BY log;
243  }} msg]
244  lappend v $msg
245} {1 {ORDER BY clause should come after INTERSECT not before}}
246
247# Various error messages while processing UNION or INTERSECT
248#
249do_test select4-5.1 {
250  set v [catch {execsql {
251    SELECT DISTINCT log FROM t2
252    UNION ALL
253    SELECT n FROM t1 WHERE log=3
254    ORDER BY log;
255  }} msg]
256  lappend v $msg
257} {1 {no such table: t2}}
258do_test select4-5.2 {
259  set v [catch {execsql {
260    SELECT DISTINCT log AS "xyzzy" FROM t1
261    UNION ALL
262    SELECT n FROM t1 WHERE log=3
263    ORDER BY xyzzy;
264  }} msg]
265  lappend v $msg
266} {0 {0 1 2 3 4 5 5 6 7 8}}
267do_test select4-5.2b {
268  set v [catch {execsql {
269    SELECT DISTINCT log AS xyzzy FROM t1
270    UNION ALL
271    SELECT n FROM t1 WHERE log=3
272    ORDER BY 'xyzzy';
273  }} msg]
274  lappend v $msg
275} {0 {0 1 2 3 4 5 5 6 7 8}}
276do_test select4-5.2c {
277  set v [catch {execsql {
278    SELECT DISTINCT log FROM t1
279    UNION ALL
280    SELECT n FROM t1 WHERE log=3
281    ORDER BY 'xyzzy';
282  }} msg]
283  lappend v $msg
284} {1 {ORDER BY term number 1 does not match any result column}}
285do_test select4-5.2d {
286  set v [catch {execsql {
287    SELECT DISTINCT log FROM t1
288    INTERSECT
289    SELECT n FROM t1 WHERE log=3
290    ORDER BY 'xyzzy';
291  }} msg]
292  lappend v $msg
293} {1 {ORDER BY term number 1 does not match any result column}}
294do_test select4-5.2e {
295  set v [catch {execsql {
296    SELECT DISTINCT log FROM t1
297    UNION ALL
298    SELECT n FROM t1 WHERE log=3
299    ORDER BY n;
300  }} msg]
301  lappend v $msg
302} {0 {0 1 2 3 4 5 5 6 7 8}}
303do_test select4-5.2f {
304  catchsql {
305    SELECT DISTINCT log FROM t1
306    UNION ALL
307    SELECT n FROM t1 WHERE log=3
308    ORDER BY log;
309  }
310} {0 {0 1 2 3 4 5 5 6 7 8}}
311do_test select4-5.2g {
312  catchsql {
313    SELECT DISTINCT log FROM t1
314    UNION ALL
315    SELECT n FROM t1 WHERE log=3
316    ORDER BY 1;
317  }
318} {0 {0 1 2 3 4 5 5 6 7 8}}
319do_test select4-5.2h {
320  catchsql {
321    SELECT DISTINCT log FROM t1
322    UNION ALL
323    SELECT n FROM t1 WHERE log=3
324    ORDER BY 2;
325  }
326} {1 {ORDER BY position 2 should be between 1 and 1}}
327do_test select4-5.2i {
328  catchsql {
329    SELECT DISTINCT 1, log FROM t1
330    UNION ALL
331    SELECT 2, n FROM t1 WHERE log=3
332    ORDER BY 2, 1;
333  }
334} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
335do_test select4-5.2j {
336  catchsql {
337    SELECT DISTINCT 1, log FROM t1
338    UNION ALL
339    SELECT 2, n FROM t1 WHERE log=3
340    ORDER BY 1, 2 DESC;
341  }
342} {0 {1 5 1 4 1 3 1 2 1 1 1 0 2 8 2 7 2 6 2 5}}
343do_test select4-5.2k {
344  catchsql {
345    SELECT DISTINCT 1, log FROM t1
346    UNION ALL
347    SELECT 2, n FROM t1 WHERE log=3
348    ORDER BY n, 1;
349  }
350} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
351do_test select4-5.3 {
352  set v [catch {execsql {
353    SELECT DISTINCT log, n FROM t1
354    UNION ALL
355    SELECT n FROM t1 WHERE log=3
356    ORDER BY log;
357  }} msg]
358  lappend v $msg
359} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
360do_test select4-5.4 {
361  set v [catch {execsql {
362    SELECT log FROM t1 WHERE n=2
363    UNION ALL
364    SELECT log FROM t1 WHERE n=3
365    UNION ALL
366    SELECT log FROM t1 WHERE n=4
367    UNION ALL
368    SELECT log FROM t1 WHERE n=5
369    ORDER BY log;
370  }} msg]
371  lappend v $msg
372} {0 {1 2 2 3}}
373
374do_test select4-6.1 {
375  execsql {
376    SELECT log, count(*) as cnt FROM t1 GROUP BY log
377    UNION
378    SELECT log, n FROM t1 WHERE n=7
379    ORDER BY cnt, log;
380  }
381} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
382do_test select4-6.2 {
383  execsql {
384    SELECT log, count(*) FROM t1 GROUP BY log
385    UNION
386    SELECT log, n FROM t1 WHERE n=7
387    ORDER BY count(*), log;
388  }
389} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
390
391# NULLs are indistinct for the UNION operator.
392# Make sure the UNION operator recognizes this
393#
394do_test select4-6.3 {
395  execsql {
396    SELECT NULL UNION SELECT NULL UNION
397    SELECT 1 UNION SELECT 2 AS 'x'
398    ORDER BY x;
399  }
400} {{} 1 2}
401do_test select4-6.3.1 {
402  execsql {
403    SELECT NULL UNION ALL SELECT NULL UNION ALL
404    SELECT 1 UNION ALL SELECT 2 AS 'x'
405    ORDER BY x;
406  }
407} {{} {} 1 2}
408
409# Make sure the DISTINCT keyword treats NULLs as indistinct.
410#
411do_test select4-6.4 {
412  execsql {
413    SELECT * FROM (
414       SELECT NULL, 1 UNION ALL SELECT NULL, 1
415    );
416  }
417} {{} 1 {} 1}
418do_test select4-6.5 {
419  execsql {
420    SELECT DISTINCT * FROM (
421       SELECT NULL, 1 UNION ALL SELECT NULL, 1
422    );
423  }
424} {{} 1}
425do_test select4-6.6 {
426  execsql {
427    SELECT DISTINCT * FROM (
428       SELECT 1,2  UNION ALL SELECT 1,2
429    );
430  }
431} {1 2}
432
433# Test distinctness of NULL in other ways.
434#
435do_test select4-6.7 {
436  execsql {
437    SELECT NULL EXCEPT SELECT NULL
438  }
439} {}
440
441
442# Make sure column names are correct when a compound select appears as
443# an expression in the WHERE clause.
444#
445do_test select4-7.1 {
446  execsql {
447    CREATE TABLE t2 AS SELECT log AS 'x', count(*) AS 'y' FROM t1 GROUP BY log;
448    SELECT * FROM t2 ORDER BY x;
449  }
450} {0 1 1 1 2 2 3 4 4 8 5 15}
451do_test select4-7.2 {
452  execsql2 {
453    SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 INTERSECT SELECT x FROM t2)
454    ORDER BY n
455  }
456} {n 1 log 0 n 2 log 1 n 3 log 2 n 4 log 2 n 5 log 3}
457do_test select4-7.3 {
458  execsql2 {
459    SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 EXCEPT SELECT x FROM t2)
460    ORDER BY n LIMIT 2
461  }
462} {n 6 log 3 n 7 log 3}
463do_test select4-7.4 {
464  execsql2 {
465    SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 UNION SELECT x FROM t2)
466    ORDER BY n LIMIT 2
467  }
468} {n 1 log 0 n 2 log 1}
469
470# Make sure DISTINCT works appropriately on TEXT and NUMERIC columns.
471#
472do_test select4-8.1 {
473  execsql {
474    BEGIN;
475    CREATE TABLE t3(a text, b float, c text);
476    INSERT INTO t3 VALUES(1, 1.1, '1.1');
477    INSERT INTO t3 VALUES(2, 1.10, '1.10');
478    INSERT INTO t3 VALUES(3, 1.10, '1.1');
479    INSERT INTO t3 VALUES(4, 1.1, '1.10');
480    INSERT INTO t3 VALUES(5, 1.2, '1.2');
481    INSERT INTO t3 VALUES(6, 1.3, '1.3');
482    COMMIT;
483  }
484  execsql {
485    SELECT DISTINCT b FROM t3 ORDER BY c;
486  }
487} {1.1 1.2 1.3}
488do_test select4-8.2 {
489  execsql {
490    SELECT DISTINCT c FROM t3 ORDER BY c;
491  }
492} {1.1 1.10 1.2 1.3}
493
494
495finish_test
496