xref: /titanic_41/usr/src/lib/libsqlite/test/join.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2002 May 24
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.
15*c5c4113dSnw141292#
16*c5c4113dSnw141292# This file implements tests for joins, including outer joins.
17*c5c4113dSnw141292#
18*c5c4113dSnw141292# $Id: join.test,v 1.11 2003/09/27 13:39:40 drh Exp $
19*c5c4113dSnw141292
20*c5c4113dSnw141292set testdir [file dirname $argv0]
21*c5c4113dSnw141292source $testdir/tester.tcl
22*c5c4113dSnw141292
23*c5c4113dSnw141292do_test join-1.1 {
24*c5c4113dSnw141292  execsql {
25*c5c4113dSnw141292    CREATE TABLE t1(a,b,c);
26*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,2,3);
27*c5c4113dSnw141292    INSERT INTO t1 VALUES(2,3,4);
28*c5c4113dSnw141292    INSERT INTO t1 VALUES(3,4,5);
29*c5c4113dSnw141292    SELECT * FROM t1;
30*c5c4113dSnw141292  }
31*c5c4113dSnw141292} {1 2 3 2 3 4 3 4 5}
32*c5c4113dSnw141292do_test join-1.2 {
33*c5c4113dSnw141292  execsql {
34*c5c4113dSnw141292    CREATE TABLE t2(b,c,d);
35*c5c4113dSnw141292    INSERT INTO t2 VALUES(1,2,3);
36*c5c4113dSnw141292    INSERT INTO t2 VALUES(2,3,4);
37*c5c4113dSnw141292    INSERT INTO t2 VALUES(3,4,5);
38*c5c4113dSnw141292    SELECT * FROM t2;
39*c5c4113dSnw141292  }
40*c5c4113dSnw141292} {1 2 3 2 3 4 3 4 5}
41*c5c4113dSnw141292
42*c5c4113dSnw141292do_test join-1.3 {
43*c5c4113dSnw141292  execsql2 {
44*c5c4113dSnw141292    SELECT * FROM t1 NATURAL JOIN t2;
45*c5c4113dSnw141292  }
46*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
47*c5c4113dSnw141292do_test join-1.3.1 {
48*c5c4113dSnw141292  execsql2 {
49*c5c4113dSnw141292    SELECT * FROM t2 NATURAL JOIN t1;
50*c5c4113dSnw141292  }
51*c5c4113dSnw141292} {t2.b 2 t2.c 3 t2.d 4 t1.a 1 t2.b 3 t2.c 4 t2.d 5 t1.a 2}
52*c5c4113dSnw141292do_test join-1.4 {
53*c5c4113dSnw141292  execsql2 {
54*c5c4113dSnw141292    SELECT * FROM t1 INNER JOIN t2 USING(b,c);
55*c5c4113dSnw141292  }
56*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
57*c5c4113dSnw141292do_test join-1.5 {
58*c5c4113dSnw141292  execsql2 {
59*c5c4113dSnw141292    SELECT * FROM t1 INNER JOIN t2 USING(b);
60*c5c4113dSnw141292  }
61*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.c 4 t2.d 5}
62*c5c4113dSnw141292do_test join-1.6 {
63*c5c4113dSnw141292  execsql2 {
64*c5c4113dSnw141292    SELECT * FROM t1 INNER JOIN t2 USING(c);
65*c5c4113dSnw141292  }
66*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.b 2 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.b 3 t2.d 5}
67*c5c4113dSnw141292do_test join-1.7 {
68*c5c4113dSnw141292  execsql2 {
69*c5c4113dSnw141292    SELECT * FROM t1 INNER JOIN t2 USING(c,b);
70*c5c4113dSnw141292  }
71*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
72*c5c4113dSnw141292
73*c5c4113dSnw141292do_test join-1.8 {
74*c5c4113dSnw141292  execsql {
75*c5c4113dSnw141292    SELECT * FROM t1 NATURAL CROSS JOIN t2;
76*c5c4113dSnw141292  }
77*c5c4113dSnw141292} {1 2 3 4 2 3 4 5}
78*c5c4113dSnw141292do_test join-1.9 {
79*c5c4113dSnw141292  execsql {
80*c5c4113dSnw141292    SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
81*c5c4113dSnw141292  }
82*c5c4113dSnw141292} {1 2 3 4 2 3 4 5}
83*c5c4113dSnw141292do_test join-1.10 {
84*c5c4113dSnw141292  execsql {
85*c5c4113dSnw141292    SELECT * FROM t1 NATURAL INNER JOIN t2;
86*c5c4113dSnw141292  }
87*c5c4113dSnw141292} {1 2 3 4 2 3 4 5}
88*c5c4113dSnw141292do_test join-1.11 {
89*c5c4113dSnw141292  execsql {
90*c5c4113dSnw141292    SELECT * FROM t1 INNER JOIN t2 USING(b,c);
91*c5c4113dSnw141292  }
92*c5c4113dSnw141292} {1 2 3 4 2 3 4 5}
93*c5c4113dSnw141292do_test join-1.12 {
94*c5c4113dSnw141292  execsql {
95*c5c4113dSnw141292    SELECT * FROM t1 natural inner join t2;
96*c5c4113dSnw141292  }
97*c5c4113dSnw141292} {1 2 3 4 2 3 4 5}
98*c5c4113dSnw141292do_test join-1.13 {
99*c5c4113dSnw141292  execsql2 {
100*c5c4113dSnw141292    SELECT * FROM t1 NATURAL JOIN
101*c5c4113dSnw141292      (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
102*c5c4113dSnw141292  }
103*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t3.d 4 t3.e 5}
104*c5c4113dSnw141292do_test join-1.14 {
105*c5c4113dSnw141292  execsql2 {
106*c5c4113dSnw141292    SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
107*c5c4113dSnw141292        NATURAL JOIN t1
108*c5c4113dSnw141292  }
109*c5c4113dSnw141292} {tx.c 3 tx.d 4 tx.e 5 t1.a 1 t1.b 2}
110*c5c4113dSnw141292
111*c5c4113dSnw141292do_test join-1.15 {
112*c5c4113dSnw141292  execsql {
113*c5c4113dSnw141292    CREATE TABLE t3(c,d,e);
114*c5c4113dSnw141292    INSERT INTO t3 VALUES(2,3,4);
115*c5c4113dSnw141292    INSERT INTO t3 VALUES(3,4,5);
116*c5c4113dSnw141292    INSERT INTO t3 VALUES(4,5,6);
117*c5c4113dSnw141292    SELECT * FROM t3;
118*c5c4113dSnw141292  }
119*c5c4113dSnw141292} {2 3 4 3 4 5 4 5 6}
120*c5c4113dSnw141292do_test join-1.16 {
121*c5c4113dSnw141292  execsql {
122*c5c4113dSnw141292    SELECT * FROM t1 natural join t2 natural join t3;
123*c5c4113dSnw141292  }
124*c5c4113dSnw141292} {1 2 3 4 5 2 3 4 5 6}
125*c5c4113dSnw141292do_test join-1.17 {
126*c5c4113dSnw141292  execsql2 {
127*c5c4113dSnw141292    SELECT * FROM t1 natural join t2 natural join t3;
128*c5c4113dSnw141292  }
129*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t3.e 5 t1.a 2 t1.b 3 t1.c 4 t2.d 5 t3.e 6}
130*c5c4113dSnw141292do_test join-1.18 {
131*c5c4113dSnw141292  execsql {
132*c5c4113dSnw141292    CREATE TABLE t4(d,e,f);
133*c5c4113dSnw141292    INSERT INTO t4 VALUES(2,3,4);
134*c5c4113dSnw141292    INSERT INTO t4 VALUES(3,4,5);
135*c5c4113dSnw141292    INSERT INTO t4 VALUES(4,5,6);
136*c5c4113dSnw141292    SELECT * FROM t4;
137*c5c4113dSnw141292  }
138*c5c4113dSnw141292} {2 3 4 3 4 5 4 5 6}
139*c5c4113dSnw141292do_test join-1.19 {
140*c5c4113dSnw141292  execsql {
141*c5c4113dSnw141292    SELECT * FROM t1 natural join t2 natural join t4;
142*c5c4113dSnw141292  }
143*c5c4113dSnw141292} {1 2 3 4 5 6}
144*c5c4113dSnw141292do_test join-1.19 {
145*c5c4113dSnw141292  execsql2 {
146*c5c4113dSnw141292    SELECT * FROM t1 natural join t2 natural join t4;
147*c5c4113dSnw141292  }
148*c5c4113dSnw141292} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t4.e 5 t4.f 6}
149*c5c4113dSnw141292do_test join-1.20 {
150*c5c4113dSnw141292  execsql {
151*c5c4113dSnw141292    SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
152*c5c4113dSnw141292  }
153*c5c4113dSnw141292} {1 2 3 4 5}
154*c5c4113dSnw141292
155*c5c4113dSnw141292do_test join-2.1 {
156*c5c4113dSnw141292  execsql {
157*c5c4113dSnw141292    SELECT * FROM t1 NATURAL LEFT JOIN t2;
158*c5c4113dSnw141292  }
159*c5c4113dSnw141292} {1 2 3 4 2 3 4 5 3 4 5 {}}
160*c5c4113dSnw141292do_test join-2.2 {
161*c5c4113dSnw141292  execsql {
162*c5c4113dSnw141292    SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
163*c5c4113dSnw141292  }
164*c5c4113dSnw141292} {1 2 3 {} 2 3 4 1 3 4 5 2}
165*c5c4113dSnw141292do_test join-2.3 {
166*c5c4113dSnw141292  catchsql {
167*c5c4113dSnw141292    SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
168*c5c4113dSnw141292  }
169*c5c4113dSnw141292} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
170*c5c4113dSnw141292do_test join-2.4 {
171*c5c4113dSnw141292  execsql {
172*c5c4113dSnw141292    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
173*c5c4113dSnw141292  }
174*c5c4113dSnw141292} {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
175*c5c4113dSnw141292do_test join-2.5 {
176*c5c4113dSnw141292  execsql {
177*c5c4113dSnw141292    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
178*c5c4113dSnw141292  }
179*c5c4113dSnw141292} {2 3 4 {} {} {} 3 4 5 1 2 3}
180*c5c4113dSnw141292do_test join-2.6 {
181*c5c4113dSnw141292  execsql {
182*c5c4113dSnw141292    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
183*c5c4113dSnw141292  }
184*c5c4113dSnw141292} {1 2 3 {} {} {} 2 3 4 {} {} {}}
185*c5c4113dSnw141292
186*c5c4113dSnw141292do_test join-3.1 {
187*c5c4113dSnw141292  catchsql {
188*c5c4113dSnw141292    SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
189*c5c4113dSnw141292  }
190*c5c4113dSnw141292} {1 {a NATURAL join may not have an ON or USING clause}}
191*c5c4113dSnw141292do_test join-3.2 {
192*c5c4113dSnw141292  catchsql {
193*c5c4113dSnw141292    SELECT * FROM t1 NATURAL JOIN t2 USING(b);
194*c5c4113dSnw141292  }
195*c5c4113dSnw141292} {1 {a NATURAL join may not have an ON or USING clause}}
196*c5c4113dSnw141292do_test join-3.3 {
197*c5c4113dSnw141292  catchsql {
198*c5c4113dSnw141292    SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
199*c5c4113dSnw141292  }
200*c5c4113dSnw141292} {1 {cannot have both ON and USING clauses in the same join}}
201*c5c4113dSnw141292do_test join-3.4 {
202*c5c4113dSnw141292  catchsql {
203*c5c4113dSnw141292    SELECT * FROM t1 JOIN t2 USING(a);
204*c5c4113dSnw141292  }
205*c5c4113dSnw141292} {1 {cannot join using column a - column not present in both tables}}
206*c5c4113dSnw141292do_test join-3.5 {
207*c5c4113dSnw141292  catchsql {
208*c5c4113dSnw141292    SELECT * FROM t1 USING(a);
209*c5c4113dSnw141292  }
210*c5c4113dSnw141292} {0 {1 2 3 2 3 4 3 4 5}}
211*c5c4113dSnw141292do_test join-3.6 {
212*c5c4113dSnw141292  catchsql {
213*c5c4113dSnw141292    SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
214*c5c4113dSnw141292  }
215*c5c4113dSnw141292} {1 {no such column: t3.a}}
216*c5c4113dSnw141292do_test join-3.7 {
217*c5c4113dSnw141292  catchsql {
218*c5c4113dSnw141292    SELECT * FROM t1 INNER OUTER JOIN t2;
219*c5c4113dSnw141292  }
220*c5c4113dSnw141292} {1 {unknown or unsupported join type: INNER OUTER}}
221*c5c4113dSnw141292do_test join-3.7 {
222*c5c4113dSnw141292  catchsql {
223*c5c4113dSnw141292    SELECT * FROM t1 LEFT BOGUS JOIN t2;
224*c5c4113dSnw141292  }
225*c5c4113dSnw141292} {1 {unknown or unsupported join type: LEFT BOGUS}}
226*c5c4113dSnw141292
227*c5c4113dSnw141292do_test join-4.1 {
228*c5c4113dSnw141292  execsql {
229*c5c4113dSnw141292    BEGIN;
230*c5c4113dSnw141292    CREATE TABLE t5(a INTEGER PRIMARY KEY);
231*c5c4113dSnw141292    CREATE TABLE t6(a INTEGER);
232*c5c4113dSnw141292    INSERT INTO t6 VALUES(NULL);
233*c5c4113dSnw141292    INSERT INTO t6 VALUES(NULL);
234*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
235*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
236*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
237*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
238*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
239*c5c4113dSnw141292    INSERT INTO t6 SELECT * FROM t6;
240*c5c4113dSnw141292    COMMIT;
241*c5c4113dSnw141292  }
242*c5c4113dSnw141292  execsql {
243*c5c4113dSnw141292    SELECT * FROM t6 NATURAL JOIN t5;
244*c5c4113dSnw141292  }
245*c5c4113dSnw141292} {}
246*c5c4113dSnw141292do_test join-4.2 {
247*c5c4113dSnw141292  execsql {
248*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
249*c5c4113dSnw141292  }
250*c5c4113dSnw141292} {}
251*c5c4113dSnw141292do_test join-4.3 {
252*c5c4113dSnw141292  execsql {
253*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
254*c5c4113dSnw141292  }
255*c5c4113dSnw141292} {}
256*c5c4113dSnw141292do_test join-4.4 {
257*c5c4113dSnw141292  execsql {
258*c5c4113dSnw141292    UPDATE t6 SET a='xyz';
259*c5c4113dSnw141292    SELECT * FROM t6 NATURAL JOIN t5;
260*c5c4113dSnw141292  }
261*c5c4113dSnw141292} {}
262*c5c4113dSnw141292do_test join-4.6 {
263*c5c4113dSnw141292  execsql {
264*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
265*c5c4113dSnw141292  }
266*c5c4113dSnw141292} {}
267*c5c4113dSnw141292do_test join-4.7 {
268*c5c4113dSnw141292  execsql {
269*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
270*c5c4113dSnw141292  }
271*c5c4113dSnw141292} {}
272*c5c4113dSnw141292do_test join-4.8 {
273*c5c4113dSnw141292  execsql {
274*c5c4113dSnw141292    UPDATE t6 SET a=1;
275*c5c4113dSnw141292    SELECT * FROM t6 NATURAL JOIN t5;
276*c5c4113dSnw141292  }
277*c5c4113dSnw141292} {}
278*c5c4113dSnw141292do_test join-4.9 {
279*c5c4113dSnw141292  execsql {
280*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a<t5.a;
281*c5c4113dSnw141292  }
282*c5c4113dSnw141292} {}
283*c5c4113dSnw141292do_test join-4.10 {
284*c5c4113dSnw141292  execsql {
285*c5c4113dSnw141292    SELECT * FROM t6, t5 WHERE t6.a>t5.a;
286*c5c4113dSnw141292  }
287*c5c4113dSnw141292} {}
288*c5c4113dSnw141292
289*c5c4113dSnw141292do_test join-5.1 {
290*c5c4113dSnw141292  execsql {
291*c5c4113dSnw141292    BEGIN;
292*c5c4113dSnw141292    create table centros (id integer primary key, centro);
293*c5c4113dSnw141292    INSERT INTO centros VALUES(1,'xxx');
294*c5c4113dSnw141292    create table usuarios (id integer primary key, nombre, apellidos,
295*c5c4113dSnw141292    idcentro integer);
296*c5c4113dSnw141292    INSERT INTO usuarios VALUES(1,'a','aa',1);
297*c5c4113dSnw141292    INSERT INTO usuarios VALUES(2,'b','bb',1);
298*c5c4113dSnw141292    INSERT INTO usuarios VALUES(3,'c','cc',NULL);
299*c5c4113dSnw141292    create index idcentro on usuarios (idcentro);
300*c5c4113dSnw141292    END;
301*c5c4113dSnw141292    select usuarios.id, usuarios.nombre, centros.centro from
302*c5c4113dSnw141292    usuarios left outer join centros on usuarios.idcentro = centros.id;
303*c5c4113dSnw141292  }
304*c5c4113dSnw141292} {1 a xxx 2 b xxx 3 c {}}
305*c5c4113dSnw141292
306*c5c4113dSnw141292# A test for ticket #247.
307*c5c4113dSnw141292#
308*c5c4113dSnw141292do_test join-7.1 {
309*c5c4113dSnw141292  execsql {
310*c5c4113dSnw141292    CREATE TABLE t7 (x, y);
311*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa1", 1);
312*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa2", NULL);
313*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa3", NULL);
314*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa4", 2);
315*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa30", 131);
316*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa31", 130);
317*c5c4113dSnw141292    INSERT INTO t7 VALUES ("pa28", NULL);
318*c5c4113dSnw141292
319*c5c4113dSnw141292    CREATE TABLE t8 (a integer primary key, b);
320*c5c4113dSnw141292    INSERT INTO t8 VALUES (1, "pa1");
321*c5c4113dSnw141292    INSERT INTO t8 VALUES (2, "pa4");
322*c5c4113dSnw141292    INSERT INTO t8 VALUES (3, NULL);
323*c5c4113dSnw141292    INSERT INTO t8 VALUES (4, NULL);
324*c5c4113dSnw141292    INSERT INTO t8 VALUES (130, "pa31");
325*c5c4113dSnw141292    INSERT INTO t8 VALUES (131, "pa30");
326*c5c4113dSnw141292
327*c5c4113dSnw141292    SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
328*c5c4113dSnw141292  }
329*c5c4113dSnw141292} {1 999 999 2 131 130 999}
330*c5c4113dSnw141292
331*c5c4113dSnw141292# Make sure a left join where the right table is really a view that
332*c5c4113dSnw141292# is itself a join works right.  Ticket #306.
333*c5c4113dSnw141292#
334*c5c4113dSnw141292do_test join-8.1 {
335*c5c4113dSnw141292  execsql {
336*c5c4113dSnw141292    BEGIN;
337*c5c4113dSnw141292    CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
338*c5c4113dSnw141292    INSERT INTO t9 VALUES(1,11);
339*c5c4113dSnw141292    INSERT INTO t9 VALUES(2,22);
340*c5c4113dSnw141292    CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
341*c5c4113dSnw141292    INSERT INTO t10 VALUES(1,2);
342*c5c4113dSnw141292    INSERT INTO t10 VALUES(3,3);
343*c5c4113dSnw141292    CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
344*c5c4113dSnw141292    INSERT INTO t11 VALUES(2,111);
345*c5c4113dSnw141292    INSERT INTO t11 VALUES(3,333);
346*c5c4113dSnw141292    CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
347*c5c4113dSnw141292    COMMIT;
348*c5c4113dSnw141292    SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
349*c5c4113dSnw141292  }
350*c5c4113dSnw141292} {1 11 1 111 2 22 {} {}}
351*c5c4113dSnw141292do_test join-8.2 {
352*c5c4113dSnw141292  execsql {
353*c5c4113dSnw141292    SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
354*c5c4113dSnw141292         ON( a=x);
355*c5c4113dSnw141292  }
356*c5c4113dSnw141292} {1 11 1 111 2 22 {} {}}
357*c5c4113dSnw141292do_test join-8.3 {
358*c5c4113dSnw141292  execsql {
359*c5c4113dSnw141292    SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
360*c5c4113dSnw141292  }
361*c5c4113dSnw141292} {1 111 1 11 3 333 {} {}}
362*c5c4113dSnw141292
363*c5c4113dSnw141292# Ticket #350 describes a scenario where LEFT OUTER JOIN does not
364*c5c4113dSnw141292# function correctly if the right table in the join is really
365*c5c4113dSnw141292# subquery.
366*c5c4113dSnw141292#
367*c5c4113dSnw141292# To test the problem, we generate the same LEFT OUTER JOIN in two
368*c5c4113dSnw141292# separate selects but with on using a subquery and the other calling
369*c5c4113dSnw141292# the table directly.  Then connect the two SELECTs using an EXCEPT.
370*c5c4113dSnw141292# Both queries should generate the same results so the answer should
371*c5c4113dSnw141292# be an empty set.
372*c5c4113dSnw141292#
373*c5c4113dSnw141292do_test join-9.1 {
374*c5c4113dSnw141292  execsql {
375*c5c4113dSnw141292    BEGIN;
376*c5c4113dSnw141292    CREATE TABLE t12(a,b);
377*c5c4113dSnw141292    INSERT INTO t12 VALUES(1,11);
378*c5c4113dSnw141292    INSERT INTO t12 VALUES(2,22);
379*c5c4113dSnw141292    CREATE TABLE t13(b,c);
380*c5c4113dSnw141292    INSERT INTO t13 VALUES(22,222);
381*c5c4113dSnw141292    COMMIT;
382*c5c4113dSnw141292    SELECT * FROM t12 NATURAL LEFT JOIN t13
383*c5c4113dSnw141292      EXCEPT
384*c5c4113dSnw141292      SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
385*c5c4113dSnw141292  }
386*c5c4113dSnw141292} {}
387*c5c4113dSnw141292do_test join-9.2 {
388*c5c4113dSnw141292  execsql {
389*c5c4113dSnw141292    CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
390*c5c4113dSnw141292    SELECT * FROM t12 NATURAL LEFT JOIN t13
391*c5c4113dSnw141292      EXCEPT
392*c5c4113dSnw141292      SELECT * FROM t12 NATURAL LEFT JOIN v13;
393*c5c4113dSnw141292  }
394*c5c4113dSnw141292} {}
395*c5c4113dSnw141292
396*c5c4113dSnw141292finish_test
397