OSDN Git Service

Add support for doing FULL JOIN ON FALSE. While this is really a rather
[pg-rex/syncrep.git] / src / test / regress / sql / join.sql
1 --
2 -- JOIN
3 -- Test JOIN clauses
4 --
5
6 CREATE TABLE J1_TBL (
7   i integer,
8   j integer,
9   t text
10 );
11
12 CREATE TABLE J2_TBL (
13   i integer,
14   k integer
15 );
16
17
18 INSERT INTO J1_TBL VALUES (1, 4, 'one');
19 INSERT INTO J1_TBL VALUES (2, 3, 'two');
20 INSERT INTO J1_TBL VALUES (3, 2, 'three');
21 INSERT INTO J1_TBL VALUES (4, 1, 'four');
22 INSERT INTO J1_TBL VALUES (5, 0, 'five');
23 INSERT INTO J1_TBL VALUES (6, 6, 'six');
24 INSERT INTO J1_TBL VALUES (7, 7, 'seven');
25 INSERT INTO J1_TBL VALUES (8, 8, 'eight');
26 INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
27 INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
28 INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
29
30 INSERT INTO J2_TBL VALUES (1, -1);
31 INSERT INTO J2_TBL VALUES (2, 2);
32 INSERT INTO J2_TBL VALUES (3, -3);
33 INSERT INTO J2_TBL VALUES (2, 4);
34 INSERT INTO J2_TBL VALUES (5, -5);
35 INSERT INTO J2_TBL VALUES (5, -5);
36 INSERT INTO J2_TBL VALUES (0, NULL);
37 INSERT INTO J2_TBL VALUES (NULL, NULL);
38 INSERT INTO J2_TBL VALUES (NULL, 0);
39
40 --
41 -- CORRELATION NAMES
42 -- Make sure that table/column aliases are supported
43 -- before diving into more complex join syntax.
44 --
45
46 SELECT '' AS "xxx", *
47   FROM J1_TBL AS tx;
48
49 SELECT '' AS "xxx", *
50   FROM J1_TBL tx;
51
52 SELECT '' AS "xxx", *
53   FROM J1_TBL AS t1 (a, b, c);
54
55 SELECT '' AS "xxx", *
56   FROM J1_TBL t1 (a, b, c);
57
58 SELECT '' AS "xxx", *
59   FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
60
61 SELECT '' AS "xxx", t1.a, t2.e
62   FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
63   WHERE t1.a = t2.d;
64
65
66 --
67 -- CROSS JOIN
68 -- Qualifications are not allowed on cross joins,
69 -- which degenerate into a standard unqualified inner join.
70 --
71
72 SELECT '' AS "xxx", *
73   FROM J1_TBL CROSS JOIN J2_TBL;
74
75 -- ambiguous column
76 SELECT '' AS "xxx", i, k, t
77   FROM J1_TBL CROSS JOIN J2_TBL;
78
79 -- resolve previous ambiguity by specifying the table name
80 SELECT '' AS "xxx", t1.i, k, t
81   FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
82
83 SELECT '' AS "xxx", ii, tt, kk
84   FROM (J1_TBL CROSS JOIN J2_TBL)
85     AS tx (ii, jj, tt, ii2, kk);
86
87 SELECT '' AS "xxx", tx.ii, tx.jj, tx.kk
88   FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
89     AS tx (ii, jj, tt, ii2, kk);
90
91 SELECT '' AS "xxx", *
92   FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
93
94
95 --
96 --
97 -- Inner joins (equi-joins)
98 --
99 --
100
101 --
102 -- Inner joins (equi-joins) with USING clause
103 -- The USING syntax changes the shape of the resulting table
104 -- by including a column in the USING clause only once in the result.
105 --
106
107 -- Inner equi-join on specified column
108 SELECT '' AS "xxx", *
109   FROM J1_TBL INNER JOIN J2_TBL USING (i);
110
111 -- Same as above, slightly different syntax
112 SELECT '' AS "xxx", *
113   FROM J1_TBL JOIN J2_TBL USING (i);
114
115 SELECT '' AS "xxx", *
116   FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
117   ORDER BY a, d;
118
119 SELECT '' AS "xxx", *
120   FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
121   ORDER BY b, t1.a;
122
123
124 --
125 -- NATURAL JOIN
126 -- Inner equi-join on all columns with the same name
127 --
128
129 SELECT '' AS "xxx", *
130   FROM J1_TBL NATURAL JOIN J2_TBL;
131
132 SELECT '' AS "xxx", *
133   FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
134
135 SELECT '' AS "xxx", *
136   FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
137
138 -- mismatch number of columns
139 -- currently, Postgres will fill in with underlying names
140 SELECT '' AS "xxx", *
141   FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
142
143
144 --
145 -- Inner joins (equi-joins)
146 --
147
148 SELECT '' AS "xxx", *
149   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
150
151 SELECT '' AS "xxx", *
152   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
153
154
155 --
156 -- Non-equi-joins
157 --
158
159 SELECT '' AS "xxx", *
160   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
161
162
163 --
164 -- Outer joins
165 -- Note that OUTER is a noise word
166 --
167
168 SELECT '' AS "xxx", *
169   FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i)
170   ORDER BY i, k, t;
171
172 SELECT '' AS "xxx", *
173   FROM J1_TBL LEFT JOIN J2_TBL USING (i)
174   ORDER BY i, k, t;
175
176 SELECT '' AS "xxx", *
177   FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
178
179 SELECT '' AS "xxx", *
180   FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
181
182 SELECT '' AS "xxx", *
183   FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i)
184   ORDER BY i, k, t;
185
186 SELECT '' AS "xxx", *
187   FROM J1_TBL FULL JOIN J2_TBL USING (i)
188   ORDER BY i, k, t;
189
190 SELECT '' AS "xxx", *
191   FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
192
193 SELECT '' AS "xxx", *
194   FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
195
196
197 --
198 -- More complicated constructs
199 --
200
201 --
202 -- Multiway full join
203 --
204
205 CREATE TABLE t1 (name TEXT, n INTEGER);
206 CREATE TABLE t2 (name TEXT, n INTEGER);
207 CREATE TABLE t3 (name TEXT, n INTEGER);
208
209 INSERT INTO t1 VALUES ( 'bb', 11 );
210 INSERT INTO t2 VALUES ( 'bb', 12 );
211 INSERT INTO t2 VALUES ( 'cc', 22 );
212 INSERT INTO t2 VALUES ( 'ee', 42 );
213 INSERT INTO t3 VALUES ( 'bb', 13 );
214 INSERT INTO t3 VALUES ( 'cc', 23 );
215 INSERT INTO t3 VALUES ( 'dd', 33 );
216
217 SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
218
219 --
220 -- Test interactions of join syntax and subqueries
221 --
222
223 -- Basic cases (we expect planner to pull up the subquery here)
224 SELECT * FROM
225 (SELECT * FROM t2) as s2
226 INNER JOIN
227 (SELECT * FROM t3) s3
228 USING (name);
229
230 SELECT * FROM
231 (SELECT * FROM t2) as s2
232 LEFT JOIN
233 (SELECT * FROM t3) s3
234 USING (name);
235
236 SELECT * FROM
237 (SELECT * FROM t2) as s2
238 FULL JOIN
239 (SELECT * FROM t3) s3
240 USING (name);
241
242 -- Cases with non-nullable expressions in subquery results;
243 -- make sure these go to null as expected
244 SELECT * FROM
245 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
246 NATURAL INNER JOIN
247 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
248
249 SELECT * FROM
250 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
251 NATURAL LEFT JOIN
252 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
253
254 SELECT * FROM
255 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
256 NATURAL FULL JOIN
257 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
258
259 SELECT * FROM
260 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
261 NATURAL INNER JOIN
262 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
263 NATURAL INNER JOIN
264 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
265
266 SELECT * FROM
267 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
268 NATURAL FULL JOIN
269 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
270 NATURAL FULL JOIN
271 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
272
273 SELECT * FROM
274 (SELECT name, n as s1_n FROM t1) as s1
275 NATURAL FULL JOIN
276   (SELECT * FROM
277     (SELECT name, n as s2_n FROM t2) as s2
278     NATURAL FULL JOIN
279     (SELECT name, n as s3_n FROM t3) as s3
280   ) ss2;
281
282 SELECT * FROM
283 (SELECT name, n as s1_n FROM t1) as s1
284 NATURAL FULL JOIN
285   (SELECT * FROM
286     (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
287     NATURAL FULL JOIN
288     (SELECT name, n as s3_n FROM t3) as s3
289   ) ss2;
290
291
292 -- Test for propagation of nullability constraints into sub-joins
293
294 create temp table x (x1 int, x2 int);
295 insert into x values (1,11);
296 insert into x values (2,22);
297 insert into x values (3,null);
298 insert into x values (4,44);
299 insert into x values (5,null);
300
301 create temp table y (y1 int, y2 int);
302 insert into y values (1,111);
303 insert into y values (2,222);
304 insert into y values (3,333);
305 insert into y values (4,null);
306
307 select * from x;
308 select * from y;
309
310 select * from x left join y on (x1 = y1 and x2 is not null);
311 select * from x left join y on (x1 = y1 and y2 is not null);
312
313 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
314 on (x1 = xx1);
315 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
316 on (x1 = xx1 and x2 is not null);
317 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
318 on (x1 = xx1 and y2 is not null);
319 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
320 on (x1 = xx1 and xx2 is not null);
321 -- these should NOT give the same answers as above
322 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
323 on (x1 = xx1) where (x2 is not null);
324 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
325 on (x1 = xx1) where (y2 is not null);
326 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
327 on (x1 = xx1) where (xx2 is not null);
328
329 --
330 -- regression test: check for bug with propagation of implied equality
331 -- to outside an IN
332 --
333 select count(*) from tenk1 a where unique1 in
334   (select unique1 from tenk1 b join tenk1 c using (unique1)
335    where b.unique2 = 42);
336
337 --
338 -- regression test: check for failure to generate a plan with multiple
339 -- degenerate IN clauses
340 --
341 select count(*) from tenk1 x where
342   x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
343   x.unique1 = 0 and
344   x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
345
346 -- try that with GEQO too
347 begin;
348 set geqo = on;
349 set geqo_threshold = 2;
350 select count(*) from tenk1 x where
351   x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
352   x.unique1 = 0 and
353   x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
354 rollback;
355
356
357 --
358 -- Clean up
359 --
360
361 DROP TABLE t1;
362 DROP TABLE t2;
363 DROP TABLE t3;
364
365 DROP TABLE J1_TBL;
366 DROP TABLE J2_TBL;
367
368 -- Both DELETE and UPDATE allow the specification of additional tables
369 -- to "join" against to determine which rows should be modified.
370
371 CREATE TEMP TABLE t1 (a int, b int);
372 CREATE TEMP TABLE t2 (a int, b int);
373 CREATE TEMP TABLE t3 (x int, y int);
374
375 INSERT INTO t1 VALUES (5, 10);
376 INSERT INTO t1 VALUES (15, 20);
377 INSERT INTO t1 VALUES (100, 100);
378 INSERT INTO t1 VALUES (200, 1000);
379 INSERT INTO t2 VALUES (200, 2000);
380 INSERT INTO t3 VALUES (5, 20);
381 INSERT INTO t3 VALUES (6, 7);
382 INSERT INTO t3 VALUES (7, 8);
383 INSERT INTO t3 VALUES (500, 100);
384
385 DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
386 SELECT * FROM t3;
387 DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
388 SELECT * FROM t3;
389 DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.x AND t3.y = t3_other.y;
390 SELECT * FROM t3;
391
392 -- Test join against inheritance tree
393
394 create temp table t2a () inherits (t2);
395
396 insert into t2a values (200, 2001);
397
398 select * from t1 left join t2 on (t1.a = t2.a);
399
400 --
401 -- regression test for 8.1 merge right join bug
402 --
403
404 CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
405 INSERT INTO tt1 VALUES (1, 11);
406 INSERT INTO tt1 VALUES (2, NULL);
407
408 CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
409 INSERT INTO tt2 VALUES (21, 11);
410 INSERT INTO tt2 VALUES (22, 11);
411
412 set enable_hashjoin to off;
413 set enable_nestloop to off;
414
415 -- these should give the same results
416
417 select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
418
419 select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
420
421 reset enable_hashjoin;
422 reset enable_nestloop;
423
424 --
425 -- regression test for 8.2 bug with improper re-ordering of left joins
426 --
427
428 create temp table tt3(f1 int, f2 text);
429 insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
430 create index tt3i on tt3(f1);
431 analyze tt3;
432
433 create temp table tt4(f1 int);
434 insert into tt4 values (0),(1),(9999);
435 analyze tt4;
436
437 SELECT a.f1
438 FROM tt4 a
439 LEFT JOIN (
440         SELECT b.f1
441         FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
442         WHERE c.f1 IS NULL
443 ) AS d ON (a.f1 = d.f1)
444 WHERE d.f1 IS NULL;
445
446 --
447 -- regression test for problems of the sort depicted in bug #3494
448 --
449
450 create temp table tt5(f1 int, f2 int);
451 create temp table tt6(f1 int, f2 int);
452
453 insert into tt5 values(1, 10);
454 insert into tt5 values(1, 11);
455
456 insert into tt6 values(1, 9);
457 insert into tt6 values(1, 2);
458 insert into tt6 values(2, 9);
459
460 select * from tt5,tt6 where tt5.f1 = tt6.f1 and tt5.f1 = tt5.f2 - tt6.f2;
461
462 --
463 -- regression test for problems of the sort depicted in bug #3588
464 --
465
466 create temp table xx (pkxx int);
467 create temp table yy (pkyy int, pkxx int);
468
469 insert into xx values (1);
470 insert into xx values (2);
471 insert into xx values (3);
472
473 insert into yy values (101, 1);
474 insert into yy values (201, 2);
475 insert into yy values (301, NULL);
476
477 select yy.pkyy as yy_pkyy, yy.pkxx as yy_pkxx, yya.pkyy as yya_pkyy,
478        xxa.pkxx as xxa_pkxx, xxb.pkxx as xxb_pkxx
479 from yy
480      left join (SELECT * FROM yy where pkyy = 101) as yya ON yy.pkyy = yya.pkyy
481      left join xx xxa on yya.pkxx = xxa.pkxx
482      left join xx xxb on coalesce (xxa.pkxx, 1) = xxb.pkxx;
483
484 --
485 -- regression test for improper pushing of constants across outer-join clauses
486 -- (as seen in early 8.2.x releases)
487 --
488
489 create temp table zt1 (f1 int primary key);
490 create temp table zt2 (f2 int primary key);
491 create temp table zt3 (f3 int primary key);
492 insert into zt1 values(53);
493 insert into zt2 values(53);
494
495 select * from
496   zt2 left join zt3 on (f2 = f3)
497       left join zt1 on (f3 = f1)
498 where f2 = 53;
499
500 create temp view zv1 as select *,'dummy'::text AS junk from zt1;
501
502 select * from
503   zt2 left join zt3 on (f2 = f3)
504       left join zv1 on (f3 = f1)
505 where f2 = 53;
506
507 --
508 -- regression test for improper extraction of OR indexqual conditions
509 -- (as seen in early 8.3.x releases)
510 --
511
512 select a.unique2, a.ten, b.tenthous, b.unique2, b.hundred
513 from tenk1 a left join tenk1 b on a.unique2 = b.tenthous
514 where a.unique1 = 42 and
515       ((b.unique2 is null and a.ten = 2) or b.hundred = 3);
516
517 --
518 -- test proper positioning of one-time quals in EXISTS (8.4devel bug)
519 --
520 prepare foo(bool) as
521   select count(*) from tenk1 a left join tenk1 b
522     on (a.unique2 = b.unique1 and exists
523         (select 1 from tenk1 c where c.thousand = b.unique2 and $1));
524 execute foo(true);
525 execute foo(false);
526
527 --
528 -- test for sane behavior with noncanonical merge clauses, per bug #4926
529 --
530
531 begin;
532
533 set enable_mergejoin = 1;
534 set enable_hashjoin = 0;
535 set enable_nestloop = 0;
536
537 create temp table a (i integer);
538 create temp table b (x integer, y integer);
539
540 select * from a left join b on i = x and i = y and x = i;
541
542 rollback;
543
544 --
545 -- test NULL behavior of whole-row Vars, per bug #5025
546 --
547 select t1.q2, count(t2.*)
548 from int8_tbl t1 left join int8_tbl t2 on (t1.q2 = t2.q1)
549 group by t1.q2 order by 1;
550
551 select t1.q2, count(t2.*)
552 from int8_tbl t1 left join (select * from int8_tbl) t2 on (t1.q2 = t2.q1)
553 group by t1.q2 order by 1;
554
555 select t1.q2, count(t2.*)
556 from int8_tbl t1 left join (select * from int8_tbl offset 0) t2 on (t1.q2 = t2.q1)
557 group by t1.q2 order by 1;
558
559 select t1.q2, count(t2.*)
560 from int8_tbl t1 left join
561   (select q1, case when q2=1 then 1 else q2 end as q2 from int8_tbl) t2
562   on (t1.q2 = t2.q1)
563 group by t1.q2 order by 1;
564
565 --
566 -- test the corner cases FULL JOIN ON TRUE and FULL JOIN ON FALSE
567 --
568 select * from int4_tbl a full join int4_tbl b on true;
569 select * from int4_tbl a full join int4_tbl b on false;