First public contribution.
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.
13 # $Id: selectB.test,v 1.9 2008/08/04 03:51:24 danielk1977 Exp $
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
23 proc test_transform {testname sql1 sql2 results} {
26 db eval "explain $sql1" { lappend ::vdbe1 $opcode }
27 db eval "explain $sql2" { lappend ::vdbe2 $opcode }
29 do_test $testname.transform {
34 do_test $testname.sql1 {
39 do_test $testname.sql2 {
46 CREATE TABLE t1(a, b, c);
47 CREATE TABLE t2(d, e, f);
49 INSERT INTO t1 VALUES( 2, 4, 6);
50 INSERT INTO t1 VALUES( 8, 10, 12);
51 INSERT INTO t1 VALUES(14, 16, 18);
53 INSERT INTO t2 VALUES(3, 6, 9);
54 INSERT INTO t2 VALUES(12, 15, 18);
55 INSERT INTO t2 VALUES(21, 24, 27);
59 for {set ii 1} {$ii <= 2} {incr ii} {
64 CREATE INDEX i1 ON t1(a);
65 CREATE INDEX i2 ON t2(d);
70 test_transform selectB-$ii.2 {
71 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
73 SELECT a FROM t1 UNION ALL SELECT d FROM t2
76 test_transform selectB-$ii.3 {
77 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1
79 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1
82 test_transform selectB-$ii.4 {
84 (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
87 SELECT a FROM t1 WHERE a>10 UNION ALL SELECT d FROM t2 WHERE d>10 ORDER BY 1
90 test_transform selectB-$ii.5 {
92 (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
95 SELECT a FROM t1 WHERE a>10
97 SELECT d FROM t2 WHERE d>10
101 test_transform selectB-$ii.6 {
103 (SELECT a FROM t1 UNION ALL SELECT d FROM t2 WHERE d > 12)
104 WHERE a>10 ORDER BY a
106 SELECT a FROM t1 WHERE a>10
108 SELECT d FROM t2 WHERE d>12 AND d>10
112 test_transform selectB-$ii.7 {
113 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1
116 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2
119 test_transform selectB-$ii.8 {
120 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2) ORDER BY 1
123 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 LIMIT 2 OFFSET 3
126 test_transform selectB-$ii.9 {
128 SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
131 SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
132 } {2 8 14 3 12 21 6 12 18}
134 test_transform selectB-$ii.10 {
136 SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
139 SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
141 } {2 3 6 8 12 12 14 18 21}
143 test_transform selectB-$ii.11 {
145 SELECT a FROM t1 UNION ALL SELECT d FROM t2 UNION ALL SELECT c FROM t1
146 ) WHERE a>=10 ORDER BY 1 LIMIT 3
148 SELECT a FROM t1 WHERE a>=10 UNION ALL SELECT d FROM t2 WHERE d>=10
149 UNION ALL SELECT c FROM t1 WHERE c>=10
153 test_transform selectB-$ii.12 {
154 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2)
156 SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 2
159 test_transform selectB-$ii.13 {
160 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a ASC)
162 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 ASC
165 test_transform selectB-$ii.14 {
166 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC)
168 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC
171 test_transform selectB-$ii.14 {
173 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY a DESC
176 SELECT a FROM t1 UNION ALL SELECT d FROM t2 ORDER BY 1 DESC LIMIT 2 OFFSET 2
179 test_transform selectB-$ii.15 {
181 SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
184 SELECT a, b FROM t1 UNION ALL SELECT d, e FROM t2 ORDER BY a ASC, e DESC
185 } {2 4 3 6 8 10 12 15 14 16 21 24}
188 do_test selectB-3.0 {
195 for {set ii 3} {$ii <= 4} {incr ii} {
198 do_test selectB-4.0 {
200 CREATE INDEX i1 ON t1(a);
201 CREATE INDEX i2 ON t1(b);
202 CREATE INDEX i3 ON t1(c);
203 CREATE INDEX i4 ON t2(d);
204 CREATE INDEX i5 ON t2(e);
205 CREATE INDEX i6 ON t2(f);
210 do_test selectB-$ii.1 {
212 SELECT DISTINCT * FROM
213 (SELECT c FROM t1 UNION ALL SELECT e FROM t2)
218 do_test selectB-$ii.2 {
220 SELECT c, count(*) FROM
221 (SELECT c FROM t1 UNION ALL SELECT e FROM t2)
222 GROUP BY c ORDER BY 1;
224 } {6 2 12 1 15 1 18 1 24 1}
225 do_test selectB-$ii.3 {
227 SELECT c, count(*) FROM
228 (SELECT c FROM t1 UNION ALL SELECT e FROM t2)
229 GROUP BY c HAVING count(*)>1;
232 do_test selectB-$ii.4 {
234 SELECT t4.c, t3.a FROM
235 (SELECT c FROM t1 UNION ALL SELECT e FROM t2) AS t4, t1 AS t3
239 } {6 14 6 14 12 14 15 14 18 14 24 14}
241 do_test selectB-$ii.5 {
245 SELECT a FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
248 do_test selectB-$ii.6 {
250 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
252 SELECT * FROM (SELECT a FROM t1 UNION ALL SELECT d FROM t2)
255 do_test selectB-$ii.7 {
259 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
262 do_test selectB-$ii.8 {
264 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
269 do_test selectB-$ii.9 {
271 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
278 do_test selectB-$ii.10 {
280 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
285 } {27 24 18 15 12 9 6}
286 do_test selectB-$ii.11 {
290 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
293 } {6 9 12 15 18 24 27}
294 do_test selectB-$ii.12 {
296 SELECT c FROM t1 UNION SELECT e FROM t2 UNION ALL SELECT f FROM t2
299 } {6 9 12 15 18 18 24 27}
300 do_test selectB-$ii.13 {
302 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
304 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
309 do_test selectB-$ii.14 {
313 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
317 do_test selectB-$ii.15 {
319 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
325 do_test selectB-$ii.16 {
327 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
329 SELECT * FROM (SELECT e FROM t2 UNION ALL SELECT f FROM t2)
334 do_test selectB-$ii.17 {
337 SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4
342 do_test selectB-$ii.18 {
345 SELECT a FROM t1 UNION ALL SELECT d FROM t2 LIMIT 4 OFFSET 2
350 do_test selectB-$ii.19 {
353 SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
358 do_test selectB-$ii.20 {
360 SELECT DISTINCT * FROM (
361 SELECT DISTINCT (a/10) FROM t1 UNION ALL SELECT DISTINCT(d%2) FROM t2
366 do_test selectB-$ii.21 {
368 SELECT * FROM (SELECT * FROM t1 UNION ALL SELECT * FROM t2) ORDER BY a+b
370 } {2 4 6 3 6 9 8 10 12 12 15 18 14 16 18 21 24 27}
372 do_test selectB-$ii.21 {
374 SELECT * FROM (SELECT 345 UNION ALL SELECT d FROM t2) ORDER BY 1;