blob: 648de04d00eb31881df2d9041f6774af2de761d9 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh92dba242000-06-08 00:28:51 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh92dba242000-06-08 00:28:51 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh92dba242000-06-08 00:28:51 +00009#
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#
drhb19a2bc2001-09-16 00:13:26 +000015# $Id: select4.test,v 1.4 2001/09/16 00:13:28 drh Exp $
drh92dba242000-06-08 00:28:51 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
drh92cd52f2000-06-08 01:55:29 +000022set 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
drh92dba242000-06-08 00:28:51 +000034do_test select4-1.0 {
drh92dba242000-06-08 00:28:51 +000035 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.2 {
55 execsql {
56 SELECT log FROM t1 WHERE n IN
57 (SELECT DISTINCT log FROM t1 UNION ALL
58 SELECT n FROM t1 WHERE log=3)
59 ORDER BY log;
60 }
61} {0 1 2 2 3 3 3 3}
drh92cd52f2000-06-08 01:55:29 +000062do_test select4-1.3 {
63 set v [catch {execsql {
64 SELECT DISTINCT log FROM t1 ORDER BY log
65 UNION ALL
66 SELECT n FROM t1 WHERE log=3
67 ORDER BY log;
68 }} msg]
69 lappend v $msg
70} {1 {ORDER BY clause should come after UNION ALL not before}}
drh92dba242000-06-08 00:28:51 +000071
72# Union operator
73#
74do_test select4-2.1 {
75 execsql {
76 SELECT DISTINCT log FROM t1
77 UNION
78 SELECT n FROM t1 WHERE log=3
79 ORDER BY log;
80 }
81} {0 1 2 3 4 5 6 7 8}
82do_test select4-2.2 {
83 execsql {
84 SELECT log FROM t1 WHERE n IN
85 (SELECT DISTINCT log FROM t1 UNION
86 SELECT n FROM t1 WHERE log=3)
87 ORDER BY log;
88 }
89} {0 1 2 2 3 3 3 3}
drh92cd52f2000-06-08 01:55:29 +000090do_test select4-2.3 {
91 set v [catch {execsql {
92 SELECT DISTINCT log FROM t1 ORDER BY log
93 UNION
94 SELECT n FROM t1 WHERE log=3
95 ORDER BY log;
96 }} msg]
97 lappend v $msg
98} {1 {ORDER BY clause should come after UNION not before}}
drh92dba242000-06-08 00:28:51 +000099
100# Except operator
101#
102do_test select4-3.1 {
103 execsql {
104 SELECT DISTINCT log FROM t1
105 EXCEPT
106 SELECT n FROM t1 WHERE log=3
107 ORDER BY log;
108 }
109} {0 1 2 3 4}
110do_test select4-3.2 {
111 execsql {
112 SELECT log FROM t1 WHERE n IN
113 (SELECT DISTINCT log FROM t1 EXCEPT
114 SELECT n FROM t1 WHERE log=3)
115 ORDER BY log;
116 }
117} {0 1 2 2}
drh92cd52f2000-06-08 01:55:29 +0000118do_test select4-3.3 {
119 set v [catch {execsql {
120 SELECT DISTINCT log FROM t1 ORDER BY log
121 EXCEPT
122 SELECT n FROM t1 WHERE log=3
123 ORDER BY log;
124 }} msg]
125 lappend v $msg
126} {1 {ORDER BY clause should come after EXCEPT not before}}
drh92dba242000-06-08 00:28:51 +0000127
128# Intersect operator
129#
130do_test select4-4.1 {
131 execsql {
132 SELECT DISTINCT log FROM t1
133 INTERSECT
134 SELECT n FROM t1 WHERE log=3
135 ORDER BY log;
136 }
137} {5}
138do_test select4-4.2 {
139 execsql {
140 SELECT log FROM t1 WHERE n IN
141 (SELECT DISTINCT log FROM t1 INTERSECT
142 SELECT n FROM t1 WHERE log=3)
143 ORDER BY log;
144 }
145} {3}
drh92cd52f2000-06-08 01:55:29 +0000146do_test select4-4.3 {
147 set v [catch {execsql {
148 SELECT DISTINCT log FROM t1 ORDER BY log
149 INTERSECT
150 SELECT n FROM t1 WHERE log=3
151 ORDER BY log;
152 }} msg]
153 lappend v $msg
154} {1 {ORDER BY clause should come after INTERSECT not before}}
155
156# Various error messages while processing UNION or INTERSECT
157#
158do_test select4-5.1 {
159 set v [catch {execsql {
160 SELECT DISTINCT log FROM t2
161 UNION ALL
162 SELECT n FROM t1 WHERE log=3
163 ORDER BY log;
164 }} msg]
165 lappend v $msg
166} {1 {no such table: t2}}
167do_test select4-5.2 {
168 set v [catch {execsql {
169 SELECT DISTINCT log AS "xyzzy" FROM t1
170 UNION ALL
171 SELECT n FROM t1 WHERE log=3
172 ORDER BY xyzzy;
173 }} msg]
174 lappend v $msg
175} {0 {0 1 2 3 4 5 5 6 7 8}}
176do_test select4-5.2b {
177 set v [catch {execsql {
178 SELECT DISTINCT log xyzzy FROM t1
179 UNION ALL
180 SELECT n FROM t1 WHERE log=3
181 ORDER BY 'xyzzy';
182 }} msg]
183 lappend v $msg
184} {0 {0 1 2 3 4 5 5 6 7 8}}
185do_test select4-5.2c {
186 set v [catch {execsql {
187 SELECT DISTINCT log FROM t1
188 UNION ALL
189 SELECT n FROM t1 WHERE log=3
190 ORDER BY 'xyzzy';
191 }} msg]
192 lappend v $msg
193} {1 {ORDER BY term number 1 does not match any result column}}
194do_test select4-5.2d {
195 set v [catch {execsql {
196 SELECT DISTINCT log FROM t1
197 INTERSECT
198 SELECT n FROM t1 WHERE log=3
199 ORDER BY 'xyzzy';
200 }} msg]
201 lappend v $msg
202} {1 {ORDER BY term number 1 does not match any result column}}
203do_test select4-5.2e {
204 set v [catch {execsql {
205 SELECT DISTINCT log FROM t1
206 UNION ALL
207 SELECT n FROM t1 WHERE log=3
208 ORDER BY n;
209 }} msg]
210 lappend v $msg
211} {0 {0 1 2 3 4 5 5 6 7 8}}
212do_test select4-5.3 {
213 set v [catch {execsql {
214 SELECT DISTINCT log, n FROM t1
215 UNION ALL
216 SELECT n FROM t1 WHERE log=3
217 ORDER BY log;
218 }} msg]
219 lappend v $msg
220} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
221do_test select4-5.4 {
222 set v [catch {execsql {
223 SELECT log FROM t1 WHERE n=2
224 UNION ALL
225 SELECT log FROM t1 WHERE n=3
226 UNION ALL
227 SELECT log FROM t1 WHERE n=4
228 UNION ALL
229 SELECT log FROM t1 WHERE n=5
230 ORDER BY log;
231 }} msg]
232 lappend v $msg
233} {0 {1 2 2 3}}
drh92dba242000-06-08 00:28:51 +0000234
drh4cfa7932000-06-08 15:10:46 +0000235do_test select4-6.1 {
236 execsql {
237 SELECT log, count(*) as cnt FROM t1 GROUP BY log
238 UNION
239 SELECT log, n FROM t1 WHERE n=7
240 ORDER BY cnt, log;
241 }
242} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
243do_test select4-6.2 {
244 execsql {
245 SELECT log, count(*) FROM t1 GROUP BY log
246 UNION
247 SELECT log, n FROM t1 WHERE n=7
248 ORDER BY count(*), log;
249 }
250} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
251
drh92dba242000-06-08 00:28:51 +0000252finish_test