sl@0
|
1 |
# 2002 May 24
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library.
|
sl@0
|
12 |
#
|
sl@0
|
13 |
# This file implements tests for joins, including outer joins involving
|
sl@0
|
14 |
# virtual tables. The test cases in this file are copied from the file
|
sl@0
|
15 |
# join.test, and some of the comments still reflect that.
|
sl@0
|
16 |
#
|
sl@0
|
17 |
# $Id: vtab6.test,v 1.4 2008/07/12 14:52:21 drh Exp $
|
sl@0
|
18 |
|
sl@0
|
19 |
set testdir [file dirname $argv0]
|
sl@0
|
20 |
source $testdir/tester.tcl
|
sl@0
|
21 |
|
sl@0
|
22 |
ifcapable !vtab {
|
sl@0
|
23 |
finish_test
|
sl@0
|
24 |
return
|
sl@0
|
25 |
}
|
sl@0
|
26 |
|
sl@0
|
27 |
register_echo_module [sqlite3_connection_pointer db]
|
sl@0
|
28 |
|
sl@0
|
29 |
execsql {
|
sl@0
|
30 |
CREATE TABLE real_t1(a,b,c);
|
sl@0
|
31 |
CREATE TABLE real_t2(b,c,d);
|
sl@0
|
32 |
CREATE TABLE real_t3(c,d,e);
|
sl@0
|
33 |
CREATE TABLE real_t4(d,e,f);
|
sl@0
|
34 |
CREATE TABLE real_t5(a INTEGER PRIMARY KEY);
|
sl@0
|
35 |
CREATE TABLE real_t6(a INTEGER);
|
sl@0
|
36 |
CREATE TABLE real_t7 (x, y);
|
sl@0
|
37 |
CREATE TABLE real_t8 (a integer primary key, b);
|
sl@0
|
38 |
CREATE TABLE real_t9(a INTEGER PRIMARY KEY, b);
|
sl@0
|
39 |
CREATE TABLE real_t10(x INTEGER PRIMARY KEY, y);
|
sl@0
|
40 |
CREATE TABLE real_t11(p INTEGER PRIMARY KEY, q);
|
sl@0
|
41 |
CREATE TABLE real_t12(a,b);
|
sl@0
|
42 |
CREATE TABLE real_t13(b,c);
|
sl@0
|
43 |
CREATE TABLE real_t21(a,b,c);
|
sl@0
|
44 |
CREATE TABLE real_t22(p,q);
|
sl@0
|
45 |
}
|
sl@0
|
46 |
foreach t [list t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t21 t22] {
|
sl@0
|
47 |
execsql "CREATE VIRTUAL TABLE $t USING echo(real_$t)"
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
do_test vtab6-1.1 {
|
sl@0
|
51 |
execsql {
|
sl@0
|
52 |
INSERT INTO t1 VALUES(1,2,3);
|
sl@0
|
53 |
INSERT INTO t1 VALUES(2,3,4);
|
sl@0
|
54 |
INSERT INTO t1 VALUES(3,4,5);
|
sl@0
|
55 |
SELECT * FROM t1;
|
sl@0
|
56 |
}
|
sl@0
|
57 |
} {1 2 3 2 3 4 3 4 5}
|
sl@0
|
58 |
do_test vtab6-1.2 {
|
sl@0
|
59 |
execsql {
|
sl@0
|
60 |
INSERT INTO t2 VALUES(1,2,3);
|
sl@0
|
61 |
INSERT INTO t2 VALUES(2,3,4);
|
sl@0
|
62 |
INSERT INTO t2 VALUES(3,4,5);
|
sl@0
|
63 |
SELECT * FROM t2;
|
sl@0
|
64 |
}
|
sl@0
|
65 |
} {1 2 3 2 3 4 3 4 5}
|
sl@0
|
66 |
|
sl@0
|
67 |
do_test vtab6-1.3 {
|
sl@0
|
68 |
execsql2 {
|
sl@0
|
69 |
SELECT * FROM t1 NATURAL JOIN t2;
|
sl@0
|
70 |
}
|
sl@0
|
71 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
72 |
do_test vtab6-1.3.1 {
|
sl@0
|
73 |
execsql2 {
|
sl@0
|
74 |
SELECT * FROM t2 NATURAL JOIN t1;
|
sl@0
|
75 |
}
|
sl@0
|
76 |
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
|
sl@0
|
77 |
do_test vtab6-1.3.2 {
|
sl@0
|
78 |
execsql2 {
|
sl@0
|
79 |
SELECT * FROM t2 AS x NATURAL JOIN t1;
|
sl@0
|
80 |
}
|
sl@0
|
81 |
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
|
sl@0
|
82 |
do_test vtab6-1.3.3 {
|
sl@0
|
83 |
execsql2 {
|
sl@0
|
84 |
SELECT * FROM t2 NATURAL JOIN t1 AS y;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
} {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
|
sl@0
|
87 |
do_test vtab6-1.3.4 {
|
sl@0
|
88 |
execsql {
|
sl@0
|
89 |
SELECT b FROM t1 NATURAL JOIN t2;
|
sl@0
|
90 |
}
|
sl@0
|
91 |
} {2 3}
|
sl@0
|
92 |
do_test vtab6-1.4.1 {
|
sl@0
|
93 |
execsql2 {
|
sl@0
|
94 |
SELECT * FROM t1 INNER JOIN t2 USING(b,c);
|
sl@0
|
95 |
}
|
sl@0
|
96 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
97 |
do_test vtab6-1.4.2 {
|
sl@0
|
98 |
execsql2 {
|
sl@0
|
99 |
SELECT * FROM t1 AS x INNER JOIN t2 USING(b,c);
|
sl@0
|
100 |
}
|
sl@0
|
101 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
102 |
do_test vtab6-1.4.3 {
|
sl@0
|
103 |
execsql2 {
|
sl@0
|
104 |
SELECT * FROM t1 INNER JOIN t2 AS y USING(b,c);
|
sl@0
|
105 |
}
|
sl@0
|
106 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
107 |
do_test vtab6-1.4.4 {
|
sl@0
|
108 |
execsql2 {
|
sl@0
|
109 |
SELECT * FROM t1 AS x INNER JOIN t2 AS y USING(b,c);
|
sl@0
|
110 |
}
|
sl@0
|
111 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
112 |
do_test vtab6-1.4.5 {
|
sl@0
|
113 |
execsql {
|
sl@0
|
114 |
SELECT b FROM t1 JOIN t2 USING(b);
|
sl@0
|
115 |
}
|
sl@0
|
116 |
} {2 3}
|
sl@0
|
117 |
do_test vtab6-1.5 {
|
sl@0
|
118 |
execsql2 {
|
sl@0
|
119 |
SELECT * FROM t1 INNER JOIN t2 USING(b);
|
sl@0
|
120 |
}
|
sl@0
|
121 |
} {a 1 b 2 c 3 c 3 d 4 a 2 b 3 c 4 c 4 d 5}
|
sl@0
|
122 |
do_test vtab6-1.6 {
|
sl@0
|
123 |
execsql2 {
|
sl@0
|
124 |
SELECT * FROM t1 INNER JOIN t2 USING(c);
|
sl@0
|
125 |
}
|
sl@0
|
126 |
} {a 1 b 2 c 3 b 2 d 4 a 2 b 3 c 4 b 3 d 5}
|
sl@0
|
127 |
do_test vtab6-1.7 {
|
sl@0
|
128 |
execsql2 {
|
sl@0
|
129 |
SELECT * FROM t1 INNER JOIN t2 USING(c,b);
|
sl@0
|
130 |
}
|
sl@0
|
131 |
} {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
|
sl@0
|
132 |
|
sl@0
|
133 |
do_test vtab6-1.8 {
|
sl@0
|
134 |
execsql {
|
sl@0
|
135 |
SELECT * FROM t1 NATURAL CROSS JOIN t2;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
} {1 2 3 4 2 3 4 5}
|
sl@0
|
138 |
do_test vtab6-1.9 {
|
sl@0
|
139 |
execsql {
|
sl@0
|
140 |
SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
|
sl@0
|
141 |
}
|
sl@0
|
142 |
} {1 2 3 4 2 3 4 5}
|
sl@0
|
143 |
do_test vtab6-1.10 {
|
sl@0
|
144 |
execsql {
|
sl@0
|
145 |
SELECT * FROM t1 NATURAL INNER JOIN t2;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
} {1 2 3 4 2 3 4 5}
|
sl@0
|
148 |
do_test vtab6-1.11 {
|
sl@0
|
149 |
execsql {
|
sl@0
|
150 |
SELECT * FROM t1 INNER JOIN t2 USING(b,c);
|
sl@0
|
151 |
}
|
sl@0
|
152 |
} {1 2 3 4 2 3 4 5}
|
sl@0
|
153 |
do_test vtab6-1.12 {
|
sl@0
|
154 |
execsql {
|
sl@0
|
155 |
SELECT * FROM t1 natural inner join t2;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
} {1 2 3 4 2 3 4 5}
|
sl@0
|
158 |
|
sl@0
|
159 |
ifcapable subquery {
|
sl@0
|
160 |
do_test vtab6-1.13 {
|
sl@0
|
161 |
execsql2 {
|
sl@0
|
162 |
SELECT * FROM t1 NATURAL JOIN
|
sl@0
|
163 |
(SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
|
sl@0
|
164 |
}
|
sl@0
|
165 |
} {a 1 b 2 c 3 d 4 e 5}
|
sl@0
|
166 |
do_test vtab6-1.14 {
|
sl@0
|
167 |
execsql2 {
|
sl@0
|
168 |
SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
|
sl@0
|
169 |
NATURAL JOIN t1
|
sl@0
|
170 |
}
|
sl@0
|
171 |
} {c 3 d 4 e 5 a 1 b 2}
|
sl@0
|
172 |
}
|
sl@0
|
173 |
|
sl@0
|
174 |
do_test vtab6-1.15 {
|
sl@0
|
175 |
execsql {
|
sl@0
|
176 |
INSERT INTO t3 VALUES(2,3,4);
|
sl@0
|
177 |
INSERT INTO t3 VALUES(3,4,5);
|
sl@0
|
178 |
INSERT INTO t3 VALUES(4,5,6);
|
sl@0
|
179 |
SELECT * FROM t3;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
} {2 3 4 3 4 5 4 5 6}
|
sl@0
|
182 |
do_test vtab6-1.16 {
|
sl@0
|
183 |
execsql {
|
sl@0
|
184 |
SELECT * FROM t1 natural join t2 natural join t3;
|
sl@0
|
185 |
}
|
sl@0
|
186 |
} {1 2 3 4 5 2 3 4 5 6}
|
sl@0
|
187 |
do_test vtab6-1.17 {
|
sl@0
|
188 |
execsql2 {
|
sl@0
|
189 |
SELECT * FROM t1 natural join t2 natural join t3;
|
sl@0
|
190 |
}
|
sl@0
|
191 |
} {a 1 b 2 c 3 d 4 e 5 a 2 b 3 c 4 d 5 e 6}
|
sl@0
|
192 |
do_test vtab6-1.18 {
|
sl@0
|
193 |
execsql {
|
sl@0
|
194 |
INSERT INTO t4 VALUES(2,3,4);
|
sl@0
|
195 |
INSERT INTO t4 VALUES(3,4,5);
|
sl@0
|
196 |
INSERT INTO t4 VALUES(4,5,6);
|
sl@0
|
197 |
SELECT * FROM t4;
|
sl@0
|
198 |
}
|
sl@0
|
199 |
} {2 3 4 3 4 5 4 5 6}
|
sl@0
|
200 |
do_test vtab6-1.19.1 {
|
sl@0
|
201 |
execsql {
|
sl@0
|
202 |
SELECT * FROM t1 natural join t2 natural join t4;
|
sl@0
|
203 |
}
|
sl@0
|
204 |
} {1 2 3 4 5 6}
|
sl@0
|
205 |
do_test vtab6-1.19.2 {
|
sl@0
|
206 |
execsql2 {
|
sl@0
|
207 |
SELECT * FROM t1 natural join t2 natural join t4;
|
sl@0
|
208 |
}
|
sl@0
|
209 |
} {a 1 b 2 c 3 d 4 e 5 f 6}
|
sl@0
|
210 |
do_test vtab6-1.20 {
|
sl@0
|
211 |
execsql {
|
sl@0
|
212 |
SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
|
sl@0
|
213 |
}
|
sl@0
|
214 |
} {1 2 3 4 5}
|
sl@0
|
215 |
|
sl@0
|
216 |
do_test vtab6-2.1 {
|
sl@0
|
217 |
execsql {
|
sl@0
|
218 |
SELECT * FROM t1 NATURAL LEFT JOIN t2;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
} {1 2 3 4 2 3 4 5 3 4 5 {}}
|
sl@0
|
221 |
do_test vtab6-2.2 {
|
sl@0
|
222 |
execsql {
|
sl@0
|
223 |
SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
|
sl@0
|
224 |
}
|
sl@0
|
225 |
} {1 2 3 {} 2 3 4 1 3 4 5 2}
|
sl@0
|
226 |
do_test vtab6-2.3 {
|
sl@0
|
227 |
catchsql {
|
sl@0
|
228 |
SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
|
sl@0
|
229 |
}
|
sl@0
|
230 |
} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
|
sl@0
|
231 |
do_test vtab6-2.4 {
|
sl@0
|
232 |
execsql {
|
sl@0
|
233 |
SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
|
sl@0
|
234 |
}
|
sl@0
|
235 |
} {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
|
sl@0
|
236 |
do_test vtab6-2.5 {
|
sl@0
|
237 |
execsql {
|
sl@0
|
238 |
SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
|
sl@0
|
239 |
}
|
sl@0
|
240 |
} {2 3 4 {} {} {} 3 4 5 1 2 3}
|
sl@0
|
241 |
do_test vtab6-2.6 {
|
sl@0
|
242 |
execsql {
|
sl@0
|
243 |
SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
|
sl@0
|
244 |
}
|
sl@0
|
245 |
} {1 2 3 {} {} {} 2 3 4 {} {} {}}
|
sl@0
|
246 |
|
sl@0
|
247 |
do_test vtab6-3.1 {
|
sl@0
|
248 |
catchsql {
|
sl@0
|
249 |
SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
} {1 {a NATURAL join may not have an ON or USING clause}}
|
sl@0
|
252 |
do_test vtab6-3.2 {
|
sl@0
|
253 |
catchsql {
|
sl@0
|
254 |
SELECT * FROM t1 NATURAL JOIN t2 USING(b);
|
sl@0
|
255 |
}
|
sl@0
|
256 |
} {1 {a NATURAL join may not have an ON or USING clause}}
|
sl@0
|
257 |
do_test vtab6-3.3 {
|
sl@0
|
258 |
catchsql {
|
sl@0
|
259 |
SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
|
sl@0
|
260 |
}
|
sl@0
|
261 |
} {1 {cannot have both ON and USING clauses in the same join}}
|
sl@0
|
262 |
do_test vtab6-3.4 {
|
sl@0
|
263 |
catchsql {
|
sl@0
|
264 |
SELECT * FROM t1 JOIN t2 USING(a);
|
sl@0
|
265 |
}
|
sl@0
|
266 |
} {1 {cannot join using column a - column not present in both tables}}
|
sl@0
|
267 |
do_test vtab6-3.5 {
|
sl@0
|
268 |
catchsql {
|
sl@0
|
269 |
SELECT * FROM t1 USING(a);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
} {0 {1 2 3 2 3 4 3 4 5}}
|
sl@0
|
272 |
do_test vtab6-3.6 {
|
sl@0
|
273 |
catchsql {
|
sl@0
|
274 |
SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
|
sl@0
|
275 |
}
|
sl@0
|
276 |
} {1 {no such column: t3.a}}
|
sl@0
|
277 |
do_test vtab6-3.7 {
|
sl@0
|
278 |
catchsql {
|
sl@0
|
279 |
SELECT * FROM t1 INNER OUTER JOIN t2;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
} {1 {unknown or unsupported join type: INNER OUTER}}
|
sl@0
|
282 |
do_test vtab6-3.7 {
|
sl@0
|
283 |
catchsql {
|
sl@0
|
284 |
SELECT * FROM t1 LEFT BOGUS JOIN t2;
|
sl@0
|
285 |
}
|
sl@0
|
286 |
} {1 {unknown or unsupported join type: LEFT BOGUS}}
|
sl@0
|
287 |
|
sl@0
|
288 |
do_test vtab6-4.1 {
|
sl@0
|
289 |
execsql {
|
sl@0
|
290 |
BEGIN;
|
sl@0
|
291 |
INSERT INTO t6 VALUES(NULL);
|
sl@0
|
292 |
INSERT INTO t6 VALUES(NULL);
|
sl@0
|
293 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
294 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
295 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
296 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
297 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
298 |
INSERT INTO t6 SELECT * FROM t6;
|
sl@0
|
299 |
COMMIT;
|
sl@0
|
300 |
}
|
sl@0
|
301 |
execsql {
|
sl@0
|
302 |
SELECT * FROM t6 NATURAL JOIN t5;
|
sl@0
|
303 |
}
|
sl@0
|
304 |
} {}
|
sl@0
|
305 |
do_test vtab6-4.2 {
|
sl@0
|
306 |
execsql {
|
sl@0
|
307 |
SELECT * FROM t6, t5 WHERE t6.a<t5.a;
|
sl@0
|
308 |
}
|
sl@0
|
309 |
} {}
|
sl@0
|
310 |
do_test vtab6-4.3 {
|
sl@0
|
311 |
execsql {
|
sl@0
|
312 |
SELECT * FROM t6, t5 WHERE t6.a>t5.a;
|
sl@0
|
313 |
}
|
sl@0
|
314 |
} {}
|
sl@0
|
315 |
do_test vtab6-4.4 {
|
sl@0
|
316 |
execsql {
|
sl@0
|
317 |
UPDATE t6 SET a='xyz';
|
sl@0
|
318 |
SELECT * FROM t6 NATURAL JOIN t5;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
} {}
|
sl@0
|
321 |
do_test vtab6-4.6 {
|
sl@0
|
322 |
execsql {
|
sl@0
|
323 |
SELECT * FROM t6, t5 WHERE t6.a<t5.a;
|
sl@0
|
324 |
}
|
sl@0
|
325 |
} {}
|
sl@0
|
326 |
do_test vtab6-4.7 {
|
sl@0
|
327 |
execsql {
|
sl@0
|
328 |
SELECT * FROM t6, t5 WHERE t6.a>t5.a;
|
sl@0
|
329 |
}
|
sl@0
|
330 |
} {}
|
sl@0
|
331 |
do_test vtab6-4.8 {
|
sl@0
|
332 |
execsql {
|
sl@0
|
333 |
UPDATE t6 SET a=1;
|
sl@0
|
334 |
SELECT * FROM t6 NATURAL JOIN t5;
|
sl@0
|
335 |
}
|
sl@0
|
336 |
} {}
|
sl@0
|
337 |
do_test vtab6-4.9 {
|
sl@0
|
338 |
execsql {
|
sl@0
|
339 |
SELECT * FROM t6, t5 WHERE t6.a<t5.a;
|
sl@0
|
340 |
}
|
sl@0
|
341 |
} {}
|
sl@0
|
342 |
do_test vtab6-4.10 {
|
sl@0
|
343 |
execsql {
|
sl@0
|
344 |
SELECT * FROM t6, t5 WHERE t6.a>t5.a;
|
sl@0
|
345 |
}
|
sl@0
|
346 |
} {}
|
sl@0
|
347 |
|
sl@0
|
348 |
# A test for ticket #247.
|
sl@0
|
349 |
#
|
sl@0
|
350 |
do_test vtab6-7.1 {
|
sl@0
|
351 |
execsql {
|
sl@0
|
352 |
INSERT INTO t7 VALUES ("pa1", 1);
|
sl@0
|
353 |
INSERT INTO t7 VALUES ("pa2", NULL);
|
sl@0
|
354 |
INSERT INTO t7 VALUES ("pa3", NULL);
|
sl@0
|
355 |
INSERT INTO t7 VALUES ("pa4", 2);
|
sl@0
|
356 |
INSERT INTO t7 VALUES ("pa30", 131);
|
sl@0
|
357 |
INSERT INTO t7 VALUES ("pa31", 130);
|
sl@0
|
358 |
INSERT INTO t7 VALUES ("pa28", NULL);
|
sl@0
|
359 |
|
sl@0
|
360 |
INSERT INTO t8 VALUES (1, "pa1");
|
sl@0
|
361 |
INSERT INTO t8 VALUES (2, "pa4");
|
sl@0
|
362 |
INSERT INTO t8 VALUES (3, NULL);
|
sl@0
|
363 |
INSERT INTO t8 VALUES (4, NULL);
|
sl@0
|
364 |
INSERT INTO t8 VALUES (130, "pa31");
|
sl@0
|
365 |
INSERT INTO t8 VALUES (131, "pa30");
|
sl@0
|
366 |
|
sl@0
|
367 |
SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
|
sl@0
|
368 |
}
|
sl@0
|
369 |
} {1 999 999 2 131 130 999}
|
sl@0
|
370 |
|
sl@0
|
371 |
# Make sure a left join where the right table is really a view that
|
sl@0
|
372 |
# is itself a join works right. Ticket #306.
|
sl@0
|
373 |
#
|
sl@0
|
374 |
ifcapable view {
|
sl@0
|
375 |
do_test vtab6-8.1 {
|
sl@0
|
376 |
execsql {
|
sl@0
|
377 |
BEGIN;
|
sl@0
|
378 |
INSERT INTO t9 VALUES(1,11);
|
sl@0
|
379 |
INSERT INTO t9 VALUES(2,22);
|
sl@0
|
380 |
INSERT INTO t10 VALUES(1,2);
|
sl@0
|
381 |
INSERT INTO t10 VALUES(3,3);
|
sl@0
|
382 |
INSERT INTO t11 VALUES(2,111);
|
sl@0
|
383 |
INSERT INTO t11 VALUES(3,333);
|
sl@0
|
384 |
CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
|
sl@0
|
385 |
COMMIT;
|
sl@0
|
386 |
SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
|
sl@0
|
387 |
}
|
sl@0
|
388 |
} {1 11 1 111 2 22 {} {}}
|
sl@0
|
389 |
ifcapable subquery {
|
sl@0
|
390 |
do_test vtab6-8.2 {
|
sl@0
|
391 |
execsql {
|
sl@0
|
392 |
SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
|
sl@0
|
393 |
ON( a=x);
|
sl@0
|
394 |
}
|
sl@0
|
395 |
} {1 11 1 111 2 22 {} {}}
|
sl@0
|
396 |
}
|
sl@0
|
397 |
do_test vtab6-8.3 {
|
sl@0
|
398 |
execsql {
|
sl@0
|
399 |
SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
|
sl@0
|
400 |
}
|
sl@0
|
401 |
} {1 111 1 11 3 333 {} {}}
|
sl@0
|
402 |
} ;# ifcapable view
|
sl@0
|
403 |
|
sl@0
|
404 |
# Ticket #350 describes a scenario where LEFT OUTER JOIN does not
|
sl@0
|
405 |
# function correctly if the right table in the join is really
|
sl@0
|
406 |
# subquery.
|
sl@0
|
407 |
#
|
sl@0
|
408 |
# To test the problem, we generate the same LEFT OUTER JOIN in two
|
sl@0
|
409 |
# separate selects but with on using a subquery and the other calling
|
sl@0
|
410 |
# the table directly. Then connect the two SELECTs using an EXCEPT.
|
sl@0
|
411 |
# Both queries should generate the same results so the answer should
|
sl@0
|
412 |
# be an empty set.
|
sl@0
|
413 |
#
|
sl@0
|
414 |
ifcapable compound {
|
sl@0
|
415 |
do_test vtab6-9.1 {
|
sl@0
|
416 |
execsql {
|
sl@0
|
417 |
BEGIN;
|
sl@0
|
418 |
INSERT INTO t12 VALUES(1,11);
|
sl@0
|
419 |
INSERT INTO t12 VALUES(2,22);
|
sl@0
|
420 |
INSERT INTO t13 VALUES(22,222);
|
sl@0
|
421 |
COMMIT;
|
sl@0
|
422 |
}
|
sl@0
|
423 |
} {}
|
sl@0
|
424 |
|
sl@0
|
425 |
ifcapable subquery {
|
sl@0
|
426 |
do_test vtab6-9.1.1 {
|
sl@0
|
427 |
execsql {
|
sl@0
|
428 |
SELECT * FROM t12 NATURAL LEFT JOIN t13
|
sl@0
|
429 |
EXCEPT
|
sl@0
|
430 |
SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
|
sl@0
|
431 |
}
|
sl@0
|
432 |
} {}
|
sl@0
|
433 |
}
|
sl@0
|
434 |
ifcapable view {
|
sl@0
|
435 |
do_test vtab6-9.2 {
|
sl@0
|
436 |
execsql {
|
sl@0
|
437 |
CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
|
sl@0
|
438 |
SELECT * FROM t12 NATURAL LEFT JOIN t13
|
sl@0
|
439 |
EXCEPT
|
sl@0
|
440 |
SELECT * FROM t12 NATURAL LEFT JOIN v13;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
} {}
|
sl@0
|
443 |
} ;# ifcapable view
|
sl@0
|
444 |
} ;# ifcapable compound
|
sl@0
|
445 |
|
sl@0
|
446 |
ifcapable subquery {
|
sl@0
|
447 |
do_test vtab6-10.1 {
|
sl@0
|
448 |
execsql {
|
sl@0
|
449 |
CREATE INDEX i22 ON real_t22(q);
|
sl@0
|
450 |
SELECT a FROM t21 LEFT JOIN t22 ON b=p WHERE q=
|
sl@0
|
451 |
(SELECT max(m.q) FROM t22 m JOIN t21 n ON n.b=m.p WHERE n.c=1);
|
sl@0
|
452 |
}
|
sl@0
|
453 |
} {}
|
sl@0
|
454 |
} ;# ifcapable subquery
|
sl@0
|
455 |
|
sl@0
|
456 |
do_test vtab6-11.1.0 {
|
sl@0
|
457 |
execsql {
|
sl@0
|
458 |
CREATE TABLE ab_r(a, b);
|
sl@0
|
459 |
CREATE TABLE bc_r(b, c);
|
sl@0
|
460 |
|
sl@0
|
461 |
CREATE VIRTUAL TABLE ab USING echo(ab_r);
|
sl@0
|
462 |
CREATE VIRTUAL TABLE bc USING echo(bc_r);
|
sl@0
|
463 |
|
sl@0
|
464 |
INSERT INTO ab VALUES(1, 2);
|
sl@0
|
465 |
INSERT INTO bc VALUES(2, 3);
|
sl@0
|
466 |
}
|
sl@0
|
467 |
} {}
|
sl@0
|
468 |
|
sl@0
|
469 |
do_test vtab6-11.1.1 {
|
sl@0
|
470 |
execsql {
|
sl@0
|
471 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
472 |
}
|
sl@0
|
473 |
} {1 2 3}
|
sl@0
|
474 |
do_test vtab6-11.1.2 {
|
sl@0
|
475 |
execsql {
|
sl@0
|
476 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
477 |
}
|
sl@0
|
478 |
} {1 2 3}
|
sl@0
|
479 |
|
sl@0
|
480 |
set ::echo_module_cost 1.0
|
sl@0
|
481 |
|
sl@0
|
482 |
do_test vtab6-11.1.3 {
|
sl@0
|
483 |
execsql {
|
sl@0
|
484 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
485 |
}
|
sl@0
|
486 |
} {1 2 3}
|
sl@0
|
487 |
do_test vtab6-11.1.4 {
|
sl@0
|
488 |
execsql {
|
sl@0
|
489 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
490 |
}
|
sl@0
|
491 |
} {1 2 3}
|
sl@0
|
492 |
|
sl@0
|
493 |
|
sl@0
|
494 |
do_test vtab6-11.2.0 {
|
sl@0
|
495 |
execsql {
|
sl@0
|
496 |
CREATE INDEX ab_i ON ab_r(b);
|
sl@0
|
497 |
}
|
sl@0
|
498 |
} {}
|
sl@0
|
499 |
|
sl@0
|
500 |
unset ::echo_module_cost
|
sl@0
|
501 |
|
sl@0
|
502 |
do_test vtab6-11.2.1 {
|
sl@0
|
503 |
execsql {
|
sl@0
|
504 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
505 |
}
|
sl@0
|
506 |
} {1 2 3}
|
sl@0
|
507 |
do_test vtab6-11.2.2 {
|
sl@0
|
508 |
execsql {
|
sl@0
|
509 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
510 |
}
|
sl@0
|
511 |
} {1 2 3}
|
sl@0
|
512 |
|
sl@0
|
513 |
set ::echo_module_cost 1.0
|
sl@0
|
514 |
|
sl@0
|
515 |
do_test vtab6-11.2.3 {
|
sl@0
|
516 |
execsql {
|
sl@0
|
517 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
518 |
}
|
sl@0
|
519 |
} {1 2 3}
|
sl@0
|
520 |
do_test vtab6-11.2.4 {
|
sl@0
|
521 |
execsql {
|
sl@0
|
522 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
523 |
}
|
sl@0
|
524 |
} {1 2 3}
|
sl@0
|
525 |
|
sl@0
|
526 |
unset ::echo_module_cost
|
sl@0
|
527 |
db close
|
sl@0
|
528 |
sqlite3 db test.db
|
sl@0
|
529 |
register_echo_module [sqlite3_connection_pointer db]
|
sl@0
|
530 |
|
sl@0
|
531 |
do_test vtab6-11.3.1 {
|
sl@0
|
532 |
execsql {
|
sl@0
|
533 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
534 |
}
|
sl@0
|
535 |
} {1 2 3}
|
sl@0
|
536 |
|
sl@0
|
537 |
do_test vtab6-11.3.2 {
|
sl@0
|
538 |
execsql {
|
sl@0
|
539 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
540 |
}
|
sl@0
|
541 |
} {1 2 3}
|
sl@0
|
542 |
|
sl@0
|
543 |
set ::echo_module_cost 1.0
|
sl@0
|
544 |
|
sl@0
|
545 |
do_test vtab6-11.3.3 {
|
sl@0
|
546 |
execsql {
|
sl@0
|
547 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
548 |
}
|
sl@0
|
549 |
} {1 2 3}
|
sl@0
|
550 |
do_test vtab6-11.3.4 {
|
sl@0
|
551 |
execsql {
|
sl@0
|
552 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
553 |
}
|
sl@0
|
554 |
} {1 2 3}
|
sl@0
|
555 |
|
sl@0
|
556 |
unset ::echo_module_cost
|
sl@0
|
557 |
|
sl@0
|
558 |
set ::echo_module_ignore_usable 1
|
sl@0
|
559 |
db cache flush
|
sl@0
|
560 |
|
sl@0
|
561 |
do_test vtab6-11.4.1 {
|
sl@0
|
562 |
catchsql {
|
sl@0
|
563 |
SELECT a, b, c FROM ab NATURAL JOIN bc;
|
sl@0
|
564 |
}
|
sl@0
|
565 |
} {1 {table ab: xBestIndex returned an invalid plan}}
|
sl@0
|
566 |
do_test vtab6-11.4.2 {
|
sl@0
|
567 |
catchsql {
|
sl@0
|
568 |
SELECT a, b, c FROM bc NATURAL JOIN ab;
|
sl@0
|
569 |
}
|
sl@0
|
570 |
} {1 {table ab: xBestIndex returned an invalid plan}}
|
sl@0
|
571 |
|
sl@0
|
572 |
unset ::echo_module_ignore_usable
|
sl@0
|
573 |
|
sl@0
|
574 |
finish_test
|