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 #***********************************************************************
12 # This file implements tests for JOINs
14 # The test case output is (mostly) all generated by PostgreSQL 14. This
15 # test module was created as follows:
17 # 1. Run a TCL script (included at the bottom of this file) that
18 # generates an input script for "psql" that will run man
19 # diverse tests on joins.
21 # 2. Run the script from step (1) through psql and collect the
24 # 3. Make a few minor global search-and-replace operations to convert
25 # the psql output into a form suitable for this test module.
27 # 4. Add this header, and the script content at the footer.
29 # A few extra tests that were not generated from postgresql output are
32 set testdir [file dirname $argv0]
33 source $testdir/tester.tcl
36 CREATE TABLE t1(x INT);
37 CREATE TABLE t2(y INT);
38 CREATE TABLE t3(z INT);
39 CREATE TABLE t4(w INT);
40 INSERT INTO t1 VALUES(10);
41 INSERT INTO t3 VALUES(20),(30);
42 INSERT INTO t4 VALUES(50);
44 do_execsql_test joinF-1 {
46 FROM t1 INNER JOIN t2 ON true
47 INNER JOIN t3 ON t2.y IS NOT NULL
49 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
52 do_execsql_test joinF-2 {
54 FROM t1 INNER JOIN t2 ON true
55 INNER JOIN t3 ON t2.y IS NOT NULL
57 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
58 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
61 do_execsql_test joinF-3 {
63 FROM t1 INNER JOIN t2 ON true
64 INNER JOIN t3 ON t2.y IS NOT NULL
66 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
69 do_execsql_test joinF-4 {
71 FROM t1 INNER JOIN t2 ON true
72 INNER JOIN t3 ON t2.y IS NOT NULL
74 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
75 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
78 do_execsql_test joinF-5 {
80 FROM t1 INNER JOIN t2 ON true
81 INNER JOIN t3 ON t2.y IS NOT NULL
83 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
87 do_execsql_test joinF-6 {
89 FROM t1 INNER JOIN t2 ON true
90 INNER JOIN t3 ON t2.y IS NOT NULL
92 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
93 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
96 do_execsql_test joinF-7 {
98 FROM t1 INNER JOIN t2 ON true
99 LEFT JOIN t3 ON t2.y IS NOT NULL
100 INNER JOIN t4 ON true
101 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
104 do_execsql_test joinF-8 {
106 FROM t1 INNER JOIN t2 ON true
107 LEFT JOIN t3 ON t2.y IS NOT NULL
108 INNER JOIN t4 ON true
109 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
110 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
113 do_execsql_test joinF-9 {
115 FROM t1 INNER JOIN t2 ON true
116 LEFT JOIN t3 ON t2.y IS NOT NULL
118 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
121 do_execsql_test joinF-10 {
123 FROM t1 INNER JOIN t2 ON true
124 LEFT JOIN t3 ON t2.y IS NOT NULL
126 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
127 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
130 do_execsql_test joinF-11 {
132 FROM t1 INNER JOIN t2 ON true
133 LEFT JOIN t3 ON t2.y IS NOT NULL
134 RIGHT JOIN t4 ON true
135 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
139 do_execsql_test joinF-12 {
141 FROM t1 INNER JOIN t2 ON true
142 LEFT JOIN t3 ON t2.y IS NOT NULL
143 RIGHT JOIN t4 ON true
144 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
145 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
148 do_execsql_test joinF-13 {
150 FROM t1 INNER JOIN t2 ON true
151 RIGHT JOIN t3 ON t2.y IS NOT NULL
152 INNER JOIN t4 ON true
153 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
158 do_execsql_test joinF-14 {
160 FROM t1 INNER JOIN t2 ON true
161 RIGHT JOIN t3 ON t2.y IS NOT NULL
162 INNER JOIN t4 ON true
163 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
164 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
169 do_execsql_test joinF-15 {
171 FROM t1 INNER JOIN t2 ON true
172 RIGHT JOIN t3 ON t2.y IS NOT NULL
174 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
179 do_execsql_test joinF-16 {
181 FROM t1 INNER JOIN t2 ON true
182 RIGHT JOIN t3 ON t2.y IS NOT NULL
184 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
185 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
190 do_execsql_test joinF-17 {
192 FROM t1 INNER JOIN t2 ON true
193 RIGHT JOIN t3 ON t2.y IS NOT NULL
194 RIGHT JOIN t4 ON true
195 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
200 do_execsql_test joinF-18 {
202 FROM t1 INNER JOIN t2 ON true
203 RIGHT JOIN t3 ON t2.y IS NOT NULL
204 RIGHT JOIN t4 ON true
205 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
206 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
211 do_execsql_test joinF-19 {
213 FROM t1 LEFT JOIN t2 ON true
214 INNER JOIN t3 ON t2.y IS NOT NULL
215 INNER JOIN t4 ON true
216 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
219 do_execsql_test joinF-20 {
221 FROM t1 LEFT JOIN t2 ON true
222 INNER JOIN t3 ON t2.y IS NOT NULL
223 INNER JOIN t4 ON true
224 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
225 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
228 do_execsql_test joinF-21 {
230 FROM t1 LEFT JOIN t2 ON true
231 INNER JOIN t3 ON t2.y IS NOT NULL
233 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
236 do_execsql_test joinF-22 {
238 FROM t1 LEFT JOIN t2 ON true
239 INNER JOIN t3 ON t2.y IS NOT NULL
241 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
242 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
245 do_execsql_test joinF-23 {
247 FROM t1 LEFT JOIN t2 ON true
248 INNER JOIN t3 ON t2.y IS NOT NULL
249 RIGHT JOIN t4 ON true
250 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
254 do_execsql_test joinF-24 {
256 FROM t1 LEFT JOIN t2 ON true
257 INNER JOIN t3 ON t2.y IS NOT NULL
258 RIGHT JOIN t4 ON true
259 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
260 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
263 do_execsql_test joinF-25 {
265 FROM t1 LEFT JOIN t2 ON true
266 LEFT JOIN t3 ON t2.y IS NOT NULL
267 INNER JOIN t4 ON true
268 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
272 do_execsql_test joinF-26 {
274 FROM t1 LEFT JOIN t2 ON true
275 LEFT JOIN t3 ON t2.y IS NOT NULL
276 INNER JOIN t4 ON true
277 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
278 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
281 do_execsql_test joinF-27 {
283 FROM t1 LEFT JOIN t2 ON true
284 LEFT JOIN t3 ON t2.y IS NOT NULL
286 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
290 do_execsql_test joinF-28 {
292 FROM t1 LEFT JOIN t2 ON true
293 LEFT JOIN t3 ON t2.y IS NOT NULL
295 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
296 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
299 do_execsql_test joinF-29 {
301 FROM t1 LEFT JOIN t2 ON true
302 LEFT JOIN t3 ON t2.y IS NOT NULL
303 RIGHT JOIN t4 ON true
304 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
308 do_execsql_test joinF-30 {
310 FROM t1 LEFT JOIN t2 ON true
311 LEFT JOIN t3 ON t2.y IS NOT NULL
312 RIGHT JOIN t4 ON true
313 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
314 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
317 do_execsql_test joinF-31 {
319 FROM t1 LEFT JOIN t2 ON true
320 RIGHT JOIN t3 ON t2.y IS NOT NULL
321 INNER JOIN t4 ON true
322 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
327 do_execsql_test joinF-32 {
329 FROM t1 LEFT JOIN t2 ON true
330 RIGHT JOIN t3 ON t2.y IS NOT NULL
331 INNER JOIN t4 ON true
332 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
333 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
338 do_execsql_test joinF-33 {
340 FROM t1 LEFT JOIN t2 ON true
341 RIGHT JOIN t3 ON t2.y IS NOT NULL
343 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
348 do_execsql_test joinF-34 {
350 FROM t1 LEFT JOIN t2 ON true
351 RIGHT JOIN t3 ON t2.y IS NOT NULL
353 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
354 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
359 do_execsql_test joinF-35 {
361 FROM t1 LEFT JOIN t2 ON true
362 RIGHT JOIN t3 ON t2.y IS NOT NULL
363 RIGHT JOIN t4 ON true
364 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
369 do_execsql_test joinF-36 {
371 FROM t1 LEFT JOIN t2 ON true
372 RIGHT JOIN t3 ON t2.y IS NOT NULL
373 RIGHT JOIN t4 ON true
374 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
375 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
380 do_execsql_test joinF-37 {
382 FROM t1 RIGHT JOIN t2 ON true
383 INNER JOIN t3 ON t2.y IS NOT NULL
384 INNER JOIN t4 ON true
385 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
388 do_execsql_test joinF-38 {
390 FROM t1 RIGHT JOIN t2 ON true
391 INNER JOIN t3 ON t2.y IS NOT NULL
392 INNER JOIN t4 ON true
393 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
394 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
397 do_execsql_test joinF-39 {
399 FROM t1 RIGHT JOIN t2 ON true
400 INNER JOIN t3 ON t2.y IS NOT NULL
402 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
405 do_execsql_test joinF-40 {
407 FROM t1 RIGHT JOIN t2 ON true
408 INNER JOIN t3 ON t2.y IS NOT NULL
410 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
411 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
414 do_execsql_test joinF-41 {
416 FROM t1 RIGHT JOIN t2 ON true
417 INNER JOIN t3 ON t2.y IS NOT NULL
418 RIGHT JOIN t4 ON true
419 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
423 do_execsql_test joinF-42 {
425 FROM t1 RIGHT JOIN t2 ON true
426 INNER JOIN t3 ON t2.y IS NOT NULL
427 RIGHT JOIN t4 ON true
428 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
429 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
432 do_execsql_test joinF-43 {
434 FROM t1 RIGHT JOIN t2 ON true
435 LEFT JOIN t3 ON t2.y IS NOT NULL
436 INNER JOIN t4 ON true
437 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
440 do_execsql_test joinF-44 {
442 FROM t1 RIGHT JOIN t2 ON true
443 LEFT JOIN t3 ON t2.y IS NOT NULL
444 INNER JOIN t4 ON true
445 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
446 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
449 do_execsql_test joinF-45 {
451 FROM t1 RIGHT JOIN t2 ON true
452 LEFT JOIN t3 ON t2.y IS NOT NULL
454 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
457 do_execsql_test joinF-46 {
459 FROM t1 RIGHT JOIN t2 ON true
460 LEFT JOIN t3 ON t2.y IS NOT NULL
462 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
463 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
466 do_execsql_test joinF-47 {
468 FROM t1 RIGHT JOIN t2 ON true
469 LEFT JOIN t3 ON t2.y IS NOT NULL
470 RIGHT JOIN t4 ON true
471 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
475 do_execsql_test joinF-48 {
477 FROM t1 RIGHT JOIN t2 ON true
478 LEFT JOIN t3 ON t2.y IS NOT NULL
479 RIGHT JOIN t4 ON true
480 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
481 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
484 do_execsql_test joinF-49 {
486 FROM t1 RIGHT JOIN t2 ON true
487 RIGHT JOIN t3 ON t2.y IS NOT NULL
488 INNER JOIN t4 ON true
489 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
494 do_execsql_test joinF-50 {
496 FROM t1 RIGHT JOIN t2 ON true
497 RIGHT JOIN t3 ON t2.y IS NOT NULL
498 INNER JOIN t4 ON true
499 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
500 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
505 do_execsql_test joinF-51 {
507 FROM t1 RIGHT JOIN t2 ON true
508 RIGHT JOIN t3 ON t2.y IS NOT NULL
510 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
515 do_execsql_test joinF-52 {
517 FROM t1 RIGHT JOIN t2 ON true
518 RIGHT JOIN t3 ON t2.y IS NOT NULL
520 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
521 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
526 do_execsql_test joinF-53 {
528 FROM t1 RIGHT JOIN t2 ON true
529 RIGHT JOIN t3 ON t2.y IS NOT NULL
530 RIGHT JOIN t4 ON true
531 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
536 do_execsql_test joinF-54 {
538 FROM t1 RIGHT JOIN t2 ON true
539 RIGHT JOIN t3 ON t2.y IS NOT NULL
540 RIGHT JOIN t4 ON true
541 WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)
542 ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);
549 ############################################################################
550 # This is the TCL script used to generate the psql script that generated
555 # \\pset tuples_only on
558 # DROP TABLE IF EXISTS t1;
559 # DROP TABLE IF EXISTS t2;
560 # DROP TABLE IF EXISTS t3;
561 # DROP TABLE IF EXISTS t4;
562 # CREATE TABLE t1(x INT);
563 # CREATE TABLE t2(y INT);
564 # CREATE TABLE t3(z INT);
565 # CREATE TABLE t4(w INT);
566 # INSERT INTO t1 VALUES(10);
567 # INSERT INTO t3 VALUES(20),(30);
568 # INSERT INTO t4 VALUES(50);
571 # proc echo {prefix txt} {
572 # regsub -all {\n} $txt \n$prefix txt
577 # foreach j1 {INNER LEFT RIGHT} {
578 # foreach j2 {INNER LEFT RIGHT} {
579 # foreach j3 {INNER LEFT RIGHT} {
583 # append q1 "SELECT *\n"
584 # append q1 " FROM t1 $j1 JOIN t2 ON true\n"
585 # append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n"
586 # append q1 " $j3 JOIN t4 ON true\n"
587 # append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);"
589 # echo "\\qecho " "do_execsql_test joinF-$n \{"
590 # echo "\\qecho X " $q1
591 # echo "\\qecho " "\} \{"
593 # echo "\\qecho " "\}"
597 # append q1 "SELECT *\n"
598 # append q1 " FROM t1 $j1 JOIN t2 ON true\n"
599 # append q1 " $j2 JOIN t3 ON t2.y IS NOT NULL\n"
600 # append q1 " $j3 JOIN t4 ON true\n"
601 # append q1 " WHERE (t3.z!=400 AND t3.z!=500 AND t3.z!=600)\n"
602 # append q1 " ORDER BY coalesce(t1.x,t2.y,t3.z,t4.w,0);"
604 # echo "\\qecho " "do_execsql_test joinF-$n \{"
605 # echo "\\qecho X " $q1
606 # echo "\\qecho " "\} \{"
608 # echo "\\qecho " "\}"