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 # This file implements tests for RIGHT and FULL OUTER JOINs.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
20 CREATE TABLE t3(id INTEGER PRIMARY KEY, w TEXT);
21 CREATE TABLE t4(id INTEGER PRIMARY KEY, x TEXT);
22 CREATE TABLE t5(id INTEGER PRIMARY KEY, y TEXT);
23 CREATE TABLE t6(id INTEGER PRIMARY KEY, z INT);
24 CREATE VIEW dual(dummy) AS VALUES('x');
25 INSERT INTO t3(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven');
26 INSERT INTO t4(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave');
27 INSERT INTO t5(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'),
29 INSERT INTO t6(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999);
32 CREATE TABLE t3(id INT PRIMARY KEY, w TEXT) WITHOUT ROWID;
33 CREATE TABLE t4(id INT PRIMARY KEY, x TEXT) WITHOUT ROWID;
34 CREATE TABLE t5(id INT PRIMARY KEY, y TEXT) WITHOUT ROWID;
35 CREATE TABLE t6(id INT PRIMARY KEY, z INT) WITHOUT ROWID;
36 CREATE TABLE dual(dummy TEXT);
37 INSERT INTO dual(dummy) VALUES('x');
38 INSERT INTO t3(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven');
39 INSERT INTO t4(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave');
40 INSERT INTO t5(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'),
42 INSERT INTO t6(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999);
45 CREATE TABLE t3x(id INTEGER PRIMARY KEY, w TEXT);
46 CREATE TABLE t4x(id INTEGER PRIMARY KEY, x TEXT);
47 CREATE TABLE t5x(id INTEGER PRIMARY KEY, y TEXT);
48 CREATE TABLE t6x(id INTEGER PRIMARY KEY, z INT);
49 CREATE VIEW dual(dummy) AS VALUES('x');
50 INSERT INTO t3x(id,w) VALUES(2,'two'),(3,'three'),(6,'six'),(7,'seven');
51 INSERT INTO t4x(id,x) VALUES(2,'alice'),(4,'bob'),(6,'cindy'),(8,'dave');
52 INSERT INTO t5x(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow'),(4,'green'),
54 INSERT INTO t6x(id,z) VALUES(3,333),(4,444),(5,555),(0,1000),(9,999);
55 CREATE VIEW t3 AS SELECT * FROM t3x LIMIT 1000;
56 CREATE VIEW t4 AS SELECT * FROM t4x LIMIT 1000;
57 CREATE VIEW t5 AS SELECT * FROM t5x LIMIT 1000;
58 CREATE VIEW t6 AS SELECT * FROM t6x LIMIT 1000;
61 CREATE TABLE t3a(id INTEGER PRIMARY KEY, w TEXT);
62 CREATE TABLE t3b(id INTEGER PRIMARY KEY, w TEXT);
63 CREATE TABLE t4a(id INTEGER PRIMARY KEY, x TEXT);
64 CREATE TABLE t4b(id INTEGER PRIMARY KEY, x TEXT);
65 CREATE TABLE t5a(id INTEGER PRIMARY KEY, y TEXT);
66 CREATE TABLE t5b(id INTEGER PRIMARY KEY, y TEXT);
67 CREATE TABLE t6a(id INTEGER PRIMARY KEY, z INT);
68 CREATE TABLE t6b(id INTEGER PRIMARY KEY, z INT);
69 CREATE VIEW dual(dummy) AS VALUES('x');
70 INSERT INTO t3a(id,w) VALUES(2,'two'),(3,'three');
71 INSERT INTO t3b(id,w) VALUES(6,'six'),(7,'seven');
72 INSERT INTO t4a(id,x) VALUES(2,'alice'),(4,'bob');
73 INSERT INTO t4b(id,x) VALUES(6,'cindy'),(8,'dave');
74 INSERT INTO t5a(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow');
75 INSERT INTO t5b(id,y) VALUES(4,'green'),(5,'blue');
76 INSERT INTO t6a(id,z) VALUES(3,333),(4,444);
77 INSERT INTO t6b(id,z) VALUES(5,555),(0,1000),(9,999);
78 CREATE VIEW t3 AS SELECT * FROM t3a UNION ALL SELECT * FROM t3b;
79 CREATE VIEW t4 AS SELECT * FROM t4a UNION ALL SELECT * FROM t4b;
80 CREATE VIEW t5 AS SELECT * FROM t5a UNION ALL SELECT * FROM t5b;
81 CREATE VIEW t6 AS SELECT * FROM t6a UNION ALL SELECT * FROM t6b;
84 CREATE TABLE t3a(id INTEGER PRIMARY KEY, w TEXT) WITHOUT ROWID;
85 CREATE TABLE t3b(id INTEGER PRIMARY KEY, w TEXT);
86 CREATE TABLE t4a(id INTEGER PRIMARY KEY, x TEXT) WITHOUT ROWID;
87 CREATE TABLE t4b(id INTEGER PRIMARY KEY, x TEXT) WITHOUT ROWID;
88 CREATE TABLE t5a(id INTEGER PRIMARY KEY, y TEXT);
89 CREATE TABLE t5b(id INTEGER PRIMARY KEY, y TEXT) WITHOUT ROWID;
90 CREATE TABLE t6a(id INTEGER PRIMARY KEY, z INT);
91 CREATE TABLE t6b(id INTEGER PRIMARY KEY, z INT);
92 CREATE VIEW dual(dummy) AS VALUES('x');
93 INSERT INTO t3a(id,w) VALUES(2,'two'),(3,'three');
94 INSERT INTO t3b(id,w) VALUES(6,'six'),(7,'seven');
95 INSERT INTO t4a(id,x) VALUES(2,'alice'),(4,'bob');
96 INSERT INTO t4b(id,x) VALUES(6,'cindy'),(8,'dave');
97 INSERT INTO t5a(id,y) VALUES(1,'red'),(2,'orange'),(3,'yellow');
98 INSERT INTO t5b(id,y) VALUES(4,'green'),(5,'blue');
99 INSERT INTO t6a(id,z) VALUES(3,333),(4,444);
100 INSERT INTO t6b(id,z) VALUES(5,555),(0,1000),(9,999);
101 CREATE VIEW t3 AS SELECT * FROM t3a UNION ALL SELECT * FROM t3b;
102 CREATE VIEW t4 AS SELECT * FROM t4a UNION ALL SELECT * FROM t4b LIMIT 50;
103 CREATE VIEW t5 AS SELECT * FROM t5a UNION ALL SELECT * FROM t5b LIMIT 100;
104 CREATE VIEW t6 AS SELECT * FROM t6a UNION ALL SELECT * FROM t6b;
109 do_execsql_test join9-$id.setup $schema {}
111 # Verifid by PG-14 for case 1
112 do_execsql_test join9-$id.100 {
113 SELECT *, t4.id, t5.id, t6.id
114 FROM t4 NATURAL LEFT JOIN t5 NATURAL LEFT JOIN t6
117 2 alice orange - 2 2 -
118 4 bob green 444 4 4 4
123 do_execsql_test join9-$id.101 {
124 SELECT *, t4.id, t5.id, t6.id
125 FROM t4 NATURAL LEFT JOIN t5 NATURAL LEFT JOIN t6
128 2 alice orange - 2 2 -
129 4 bob green 444 4 4 4
133 do_execsql_test join9-$id.102 {
134 SELECT *, t4.id, t5.id, t6.id
135 FROM t4 LEFT JOIN t5 USING(id) LEFT JOIN t6 USING(id)
138 2 alice orange - 2 2 -
139 4 bob green 444 4 4 4
144 # Verifid by PG-14 using case 1
145 do_execsql_test join9-$id.200 {
146 SELECT id, x, y, z, t4.id, t5.id, t6.id
147 FROM t5 NATURAL RIGHT JOIN t4 NATURAL LEFT JOIN t6
150 2 alice orange - 2 2 -
151 4 bob green 444 4 4 4
156 do_execsql_test join9-$id.201 {
157 SELECT id, x, y, z, t4.id, t5.id, t6.id
158 FROM t5 NATURAL RIGHT JOIN t4 NATURAL LEFT JOIN t6
161 2 alice orange - 2 2 -
162 4 bob green 444 4 4 4
167 # Verified by PG-14 using case 1
168 do_execsql_test join9-$id.300 {
169 SELECT *, t4.id, t5.id, t6.id
170 FROM t4 NATURAL RIGHT JOIN t5 NATURAL RIGHT JOIN t6
175 4 bob green 444 4 4 4
180 do_execsql_test join9-$id.301 {
181 SELECT *, t4.id, t5.id, t6.id
182 FROM t4 NATURAL RIGHT JOIN t5 NATURAL RIGHT JOIN t6
187 4 bob green 444 4 4 4
192 # Verified by PG-14 for case 1
193 do_execsql_test join9-$id.400 {
194 SELECT *, t4.id, t5.id, t6.id
195 FROM t4 NATURAL FULL JOIN t5 NATURAL FULL JOIN t6
200 2 alice orange - 2 2 -
202 4 bob green 444 4 4 4
209 do_execsql_test join9-$id.401 {
210 SELECT *, t4.id, t5.id, t6.id
211 FROM t4 NATURAL FULL JOIN t5 NATURAL FULL JOIN t6
216 2 alice orange - 2 2 -
218 4 bob green 444 4 4 4
224 do_execsql_test join9-$id.402 {
225 SELECT id, x, y, z, t4.id, t5.id, t6.id
226 FROM t4 NATURAL FULL JOIN t6 NATURAL FULL JOIN t5
231 2 alice orange - 2 2 -
233 4 bob green 444 4 4 4
239 do_execsql_test join9-$id.403 {
240 SELECT id, x, y, z, t4.id, t5.id, t6.id
241 FROM t5 NATURAL FULL JOIN t4 NATURAL FULL JOIN t6
246 2 alice orange - 2 2 -
248 4 bob green 444 4 4 4
254 do_execsql_test join9-$id.404 {
255 SELECT id, x, y, z, t4.id, t5.id, t6.id
256 FROM t5 NATURAL FULL JOIN t6 NATURAL FULL JOIN t4
261 2 alice orange - 2 2 -
263 4 bob green 444 4 4 4
269 do_execsql_test join9-$id.405 {
270 SELECT id, x, y, z, t4.id, t5.id, t6.id
271 FROM t6 NATURAL FULL JOIN t4 NATURAL FULL JOIN t5
276 2 alice orange - 2 2 -
278 4 bob green 444 4 4 4
284 do_execsql_test join9-$id.406 {
285 SELECT id, x, y, z, t4.id, t5.id, t6.id
286 FROM t6 NATURAL FULL JOIN t5 NATURAL FULL JOIN t4
291 2 alice orange - 2 2 -
293 4 bob green 444 4 4 4
300 # Verified by PG-14 using case 1
301 do_execsql_test join9-$id.500 {
302 SELECT id, w, x, y, z
303 FROM t3 FULL JOIN t4 USING(id)
305 FULL JOIN t6 USING(id)
320 # Verified by PG-14 using case 1
321 do_execsql_test join9-$id.600 {
322 SELECT id, w, x, y, z
323 FROM t3 JOIN dual AS d1 ON true
324 FULL JOIN t4 USING(id)
325 JOIN dual AS d2 ON true
327 JOIN dual AS d3 ON true
328 FULL JOIN t6 USING(id)
329 CROSS JOIN dual AS d4
344 # Verified by PG-14 using case 1
345 do_execsql_test join9-$id.700 {
346 SELECT id, w, x, y, z
347 FROM t3 JOIN dual AS d1 ON true
348 FULL JOIN t4 USING(id)
349 JOIN dual AS d2 ON true
351 JOIN dual AS d3 ON true
352 FULL JOIN t6 USING(id)
353 CROSS JOIN dual AS d4
354 WHERE x<>'bob' OR x IS NULL
368 # Verified by PG-14 using case 1
369 do_execsql_test join9-$id.800 {
370 WITH t7(id,a) AS MATERIALIZED (SELECT * FROM t4 WHERE false)
373 JOIN t7 AS t7b USING(id)
374 FULL JOIN t3 USING(id);
383 do_execsql_test join9-$id.900 {
385 FROM (t3 NATURAL FULL JOIN t4)
387 (t5 NATURAL FULL JOIN t6)
401 do_execsql_test join9-$id.910 {
403 FROM t3 NATURAL FULL JOIN
404 (t4 NATURAL FULL JOIN
405 (t5 NATURAL FULL JOIN t6))
419 do_execsql_test join9-$id.920 {
423 t5 FULL JOIN t6 USING (id)
439 do_execsql_test join9-$id.920 {
443 t5 FULL JOIN t6 USING (id)
461 do_execsql_test join9-$id.930 {
465 t5 FULL JOIN t6 USING(id)
467 ) AS j1 ON j1.id=t3.id
468 ORDER BY coalesce(t3.id,j1.id);
472 2 two 2 alice orange -
473 3 three 3 - yellow 333
483 do_execsql_test join9-$id.940 {
487 t5 FULL JOIN t6 USING(id)
489 ) AS j1 ON j1.id=t3.id
490 ORDER BY coalesce(t3.id,j1.id);
494 2 two 2 alice orange -
495 3 three 3 - yellow 333
504 do_execsql_test join9-$id.950 {
508 t5 FULL JOIN t6 USING(id)
510 ) AS j1 ON j1.id=t3.id
511 ORDER BY coalesce(t3.id,j1.id);
513 2 two 2 alice orange -
521 # Restriction (27) in the query flattener
523 do_execsql_test join9-$id.1000 {
524 WITH t56(id,y,z) AS (SELECT * FROM t5 FULL JOIN t6 USING(id) LIMIT 50)
525 SELECT id,x,y,z FROM t4 JOIN t56 USING(id)
533 do_execsql_test join9-$id.1010 {
535 FROM t4 INNER JOIN (t5 FULL JOIN t6 USING(id)) USING(id)
543 do_execsql_test join9-$id.1020 {
545 FROM t4 FULL JOIN t5 USING(id) INNER JOIN t6 USING(id)
554 do_execsql_test join9-$id.1030 {
555 WITH t45(id,x,y) AS (SELECT * FROM t4 FULL JOIN t5 USING(id) LIMIT 50)
556 SELECT id,x,y,z FROM t45 JOIN t6 USING(id)