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 joins, including outer joins.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
23 INSERT INTO t1 VALUES(1,11);
24 INSERT INTO t1 VALUES(2,22);
25 INSERT INTO t1 VALUES(3,33);
32 INSERT INTO t2 VALUES(11,111);
33 INSERT INTO t2 VALUES(33,333);
34 INSERT INTO t2 VALUES(44,444);
37 } {11 111 33 333 44 444};
41 INSERT INTO t3 VALUES(111,1111);
42 INSERT INTO t3 VALUES(444,4444);
43 INSERT INTO t3 VALUES(555,5555);
46 } {111 1111 444 4444 555 5555}
51 t1 NATURAL JOIN t2 NATURAL JOIN t3
57 t1 NATURAL JOIN t2 NATURAL LEFT OUTER JOIN t3
59 } {1 11 111 1111 3 33 333 {}}
63 t1 NATURAL LEFT OUTER JOIN t2 NATURAL JOIN t3
66 do_test join2-1.6-rj {
69 t2 NATURAL RIGHT OUTER JOIN t1 NATURAL JOIN t3
76 t1 NATURAL LEFT OUTER JOIN (t2 NATURAL JOIN t3)
78 } {1 11 111 1111 2 22 {} {} 3 33 {} {}}
79 do_test join2-1.7-rj {
81 SELECT a, b, c, d FROM
82 t2 NATURAL JOIN t3 NATURAL RIGHT JOIN t1
84 } {1 11 111 1111 2 22 {} {} 3 33 {} {}}
87 #-------------------------------------------------------------------------
88 # Check that ticket [25e335f802ddc] has been resolved. It should be an
89 # error for the ON clause of a LEFT JOIN to refer to a table to its right.
95 INSERT INTO aa VALUES('one');
96 INSERT INTO bb VALUES('one');
97 INSERT INTO cc VALUES('one');
100 do_catchsql_test 2.1 {
101 SELECT * FROM aa LEFT JOIN cc ON (a=b) JOIN bb ON (b=coalesce(c,1));
102 } {1 {ON clause references tables to its right}}
103 do_catchsql_test 2.1b {
104 SELECT * FROM aa RIGHT JOIN cc ON (a=b) JOIN bb ON (b=coalesce(c,1));
105 } {1 {ON clause references tables to its right}}
106 do_catchsql_test 2.2 {
107 SELECT * FROM aa JOIN cc ON (a=b) JOIN bb ON (b=c);
110 #-------------------------------------------------------------------------
111 # Test that a problem causing where.c to overlook opportunities to
112 # omit unnecessary tables from a LEFT JOIN when UNIQUE, NOT NULL column
113 # that makes this possible happens to be the leftmost in its table.
116 do_execsql_test 3.0 {
117 CREATE TABLE t1(k1 INTEGER PRIMARY KEY, k2, k3);
118 CREATE TABLE t2(k2 INTEGER PRIMARY KEY, v2);
120 -- Prior to this problem being fixed, table t3_2 would be omitted from
121 -- the join queries below, but if t3_1 were used in its place it would
123 CREATE TABLE t3_1(k3 PRIMARY KEY, v3) WITHOUT ROWID;
124 CREATE TABLE t3_2(v3, k3 PRIMARY KEY) WITHOUT ROWID;
128 SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_1 USING (k3);
132 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN
136 SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_2 USING (k3);
140 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN
143 #-------------------------------------------------------------------------
144 # Test that tables other than the rightmost can be omitted from a
147 do_execsql_test 4.0 {
148 CREATE TABLE c1(k INTEGER PRIMARY KEY, v1);
149 CREATE TABLE c2(k INTEGER PRIMARY KEY, v2);
150 CREATE TABLE c3(k INTEGER PRIMARY KEY, v3);
152 INSERT INTO c1 VALUES(1, 2);
153 INSERT INTO c2 VALUES(2, 3);
154 INSERT INTO c3 VALUES(3, 'v3');
156 INSERT INTO c1 VALUES(111, 1112);
157 INSERT INTO c2 VALUES(112, 1113);
158 INSERT INTO c3 VALUES(113, 'v1113');
160 do_execsql_test 4.1.1 {
161 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
163 do_execsql_test 4.1.2 {
164 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
167 do_execsql_test 4.1.3 {
168 SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
171 do_execsql_test 4.1.4 {
172 SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
173 } {2 v3 2 v3 1112 {} 1112 {}}
176 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
180 |--SEARCH c2 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN
181 `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN
184 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
188 `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?) LEFT-JOIN
191 do_execsql_test 4.2.0 {
195 CREATE TABLE c1(k UNIQUE, v1);
196 CREATE TABLE c2(k UNIQUE, v2);
197 CREATE TABLE c3(k UNIQUE, v3);
199 INSERT INTO c1 VALUES(1, 2);
200 INSERT INTO c2 VALUES(2, 3);
201 INSERT INTO c3 VALUES(3, 'v3');
203 INSERT INTO c1 VALUES(111, 1112);
204 INSERT INTO c2 VALUES(112, 1113);
205 INSERT INTO c3 VALUES(113, 'v1113');
207 do_execsql_test 4.2.1 {
208 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
210 do_execsql_test 4.2.2 {
211 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
214 do_execsql_test 4.2.3 {
215 SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
218 do_execsql_test 4.2.4 {
219 SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
220 } {2 v3 2 v3 1112 {} 1112 {}}
223 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
227 |--SEARCH c2 USING INDEX sqlite_autoindex_c2_1 (k=?) LEFT-JOIN
228 `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?) LEFT-JOIN
231 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
235 `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?) LEFT-JOIN
238 # 2017-11-23 (Thanksgiving day)
239 # OSSFuzz found an assertion fault in the new LEFT JOIN eliminator code.
241 do_execsql_test 4.3.0 {
242 DROP TABLE IF EXISTS t1;
243 DROP TABLE IF EXISTS t2;
244 CREATE TABLE t1(x PRIMARY KEY) WITHOUT ROWID;
248 LEFT JOIN t1 AS b ON (a.x=b.x)
249 LEFT JOIN t2 AS c ON (a.x=c.x);
251 do_execsql_test 4.3.1 {
252 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10)
253 INSERT INTO t1(x) SELECT x FROM c;
254 INSERT INTO t2(x) SELECT x+9 FROM t1;
257 LEFT JOIN t1 AS b ON (a.x=b.x)
258 LEFT JOIN t2 AS c ON (a.x=c.x);
259 } {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 10}
261 do_execsql_test 5.0 {
262 CREATE TABLE s1 (a INTEGER PRIMARY KEY);
263 CREATE TABLE s2 (a INTEGER PRIMARY KEY);
264 CREATE TABLE s3 (a INTEGER);
265 CREATE UNIQUE INDEX ndx on s3(a);
268 SELECT s1.a FROM s1 left join s2 using (a);
272 SELECT s1.a FROM s1 left join s3 using (a);
275 do_execsql_test 6.0 {
276 CREATE TABLE u1(a INTEGER PRIMARY KEY, b, c);
277 CREATE TABLE u2(a INTEGER PRIMARY KEY, b, c);
278 CREATE INDEX u1ab ON u1(b, c);
281 SELECT u2.* FROM u2 LEFT JOIN u1 ON( u1.a=u2.a AND u1.b=u2.b AND u1.c=u2.c );
286 do_execsql_test 7.0 {
287 CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2),(3,4),(5,6);
288 CREATE TABLE t2(c,d); INSERT INTO t2 VALUES(2,4),(3,6);
289 CREATE TABLE t3(x); INSERT INTO t3 VALUES(9);
292 FROM t1 LEFT JOIN (SELECT * FROM t2, t3) ON (c=b AND x=9)
295 } {3 4 {} {} {} x 5 6 {} {} {} x}
297 #-------------------------------------------------------------------------
301 do_execsql_test 8.0 {
306 do_execsql_test 8.1 {
307 SELECT * FROM t0 LEFT JOIN t1
308 WHERE (t1.c0 BETWEEN 0 AND 0) > ('' AND t0.c0);
311 #-------------------------------------------------------------------------
312 # Ticket [45f4bf4eb] reported by Manuel Rigger (2020-04-25)
314 # Follow up error reported by Eric Speckman on the SQLite forum
315 # https://sqlite.org/forum/info/c49496d24d35bd7c (2020-08-19)
318 do_execsql_test 9.0 {
319 CREATE TABLE t0(c0 INT);
320 CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0;
321 INSERT INTO t0(c0) VALUES (0);
324 do_execsql_test 9.1 {
325 SELECT typeof(c0), c0 FROM v0 WHERE c0>='0'
328 do_execsql_test 9.2 {
329 SELECT * FROM t0, v0 WHERE v0.c0 >= '0';
332 do_execsql_test 9.3 {
333 SELECT * FROM t0 LEFT JOIN v0 WHERE v0.c0 >= '0';
336 do_execsql_test 9.4 {
337 SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0';
340 do_execsql_test 9.5 {
341 SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0' WHERE TRUE
342 UNION SELECT 0,0 WHERE 0;
345 do_execsql_test 9.10 {
346 CREATE TABLE t1 (aaa);
347 INSERT INTO t1 VALUES(23456);
348 CREATE TABLE t2(bbb);
349 CREATE VIEW v2(ccc) AS SELECT bbb IS 1234 FROM t2;
350 SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2;
352 optimization_control db query-flattener 0
353 do_execsql_test 9.11 {
354 SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2;