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
70 t1 NATURAL LEFT OUTER JOIN (t2 NATURAL JOIN t3)
72 } {1 11 111 1111 2 22 {} {} 3 33 {} {}}
75 #-------------------------------------------------------------------------
76 # Check that ticket [25e335f802ddc] has been resolved. It should be an
77 # error for the ON clause of a LEFT JOIN to refer to a table to its right.
83 INSERT INTO aa VALUES('one');
84 INSERT INTO bb VALUES('one');
85 INSERT INTO cc VALUES('one');
88 do_catchsql_test 2.1 {
89 SELECT * FROM aa LEFT JOIN cc ON (a=b) JOIN bb ON (b=coalesce(c,1));
90 } {1 {ON clause references tables to its right}}
91 do_catchsql_test 2.2 {
92 SELECT * FROM aa JOIN cc ON (a=b) JOIN bb ON (b=c);
95 #-------------------------------------------------------------------------
96 # Test that a problem causing where.c to overlook opportunities to
97 # omit unnecessary tables from a LEFT JOIN when UNIQUE, NOT NULL column
98 # that makes this possible happens to be the leftmost in its table.
101 do_execsql_test 3.0 {
102 CREATE TABLE t1(k1 INTEGER PRIMARY KEY, k2, k3);
103 CREATE TABLE t2(k2 INTEGER PRIMARY KEY, v2);
105 -- Prior to this problem being fixed, table t3_2 would be omitted from
106 -- the join queries below, but if t3_1 were used in its place it would
108 CREATE TABLE t3_1(k3 PRIMARY KEY, v3) WITHOUT ROWID;
109 CREATE TABLE t3_2(v3, k3 PRIMARY KEY) WITHOUT ROWID;
113 SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_1 USING (k3);
117 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?)
121 SELECT v2 FROM t1 LEFT JOIN t2 USING (k2) LEFT JOIN t3_2 USING (k3);
125 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?)
128 #-------------------------------------------------------------------------
129 # Test that tables other than the rightmost can be omitted from a
132 do_execsql_test 4.0 {
133 CREATE TABLE c1(k INTEGER PRIMARY KEY, v1);
134 CREATE TABLE c2(k INTEGER PRIMARY KEY, v2);
135 CREATE TABLE c3(k INTEGER PRIMARY KEY, v3);
137 INSERT INTO c1 VALUES(1, 2);
138 INSERT INTO c2 VALUES(2, 3);
139 INSERT INTO c3 VALUES(3, 'v3');
141 INSERT INTO c1 VALUES(111, 1112);
142 INSERT INTO c2 VALUES(112, 1113);
143 INSERT INTO c3 VALUES(113, 'v1113');
145 do_execsql_test 4.1.1 {
146 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
148 do_execsql_test 4.1.2 {
149 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
152 do_execsql_test 4.1.3 {
153 SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
156 do_execsql_test 4.1.4 {
157 SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
158 } {2 v3 2 v3 1112 {} 1112 {}}
161 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
165 |--SEARCH c2 USING INTEGER PRIMARY KEY (rowid=?)
166 `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?)
169 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
173 `--SEARCH c3 USING INTEGER PRIMARY KEY (rowid=?)
176 do_execsql_test 4.2.0 {
180 CREATE TABLE c1(k UNIQUE, v1);
181 CREATE TABLE c2(k UNIQUE, v2);
182 CREATE TABLE c3(k UNIQUE, v3);
184 INSERT INTO c1 VALUES(1, 2);
185 INSERT INTO c2 VALUES(2, 3);
186 INSERT INTO c3 VALUES(3, 'v3');
188 INSERT INTO c1 VALUES(111, 1112);
189 INSERT INTO c2 VALUES(112, 1113);
190 INSERT INTO c3 VALUES(113, 'v1113');
192 do_execsql_test 4.2.1 {
193 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
195 do_execsql_test 4.2.2 {
196 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
199 do_execsql_test 4.2.3 {
200 SELECT DISTINCT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
203 do_execsql_test 4.2.4 {
204 SELECT v1, v3 FROM c1 LEFT JOIN c2 LEFT JOIN c3 ON (c3.k=v1+1);
205 } {2 v3 2 v3 1112 {} 1112 {}}
208 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v2);
212 |--SEARCH c2 USING INDEX sqlite_autoindex_c2_1 (k=?)
213 `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?)
216 SELECT v1, v3 FROM c1 LEFT JOIN c2 ON (c2.k=v1) LEFT JOIN c3 ON (c3.k=v1+1);
220 `--SEARCH c3 USING INDEX sqlite_autoindex_c3_1 (k=?)
223 # 2017-11-23 (Thanksgiving day)
224 # OSSFuzz found an assertion fault in the new LEFT JOIN eliminator code.
226 do_execsql_test 4.3.0 {
227 DROP TABLE IF EXISTS t1;
228 DROP TABLE IF EXISTS t2;
229 CREATE TABLE t1(x PRIMARY KEY) WITHOUT ROWID;
233 LEFT JOIN t1 AS b ON (a.x=b.x)
234 LEFT JOIN t2 AS c ON (a.x=c.x);
236 do_execsql_test 4.3.1 {
237 WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10)
238 INSERT INTO t1(x) SELECT x FROM c;
239 INSERT INTO t2(x) SELECT x+9 FROM t1;
242 LEFT JOIN t1 AS b ON (a.x=b.x)
243 LEFT JOIN t2 AS c ON (a.x=c.x);
244 } {1 {} 2 {} 3 {} 4 {} 5 {} 6 {} 7 {} 8 {} 9 {} 10 10}
246 do_execsql_test 5.0 {
247 CREATE TABLE s1 (a INTEGER PRIMARY KEY);
248 CREATE TABLE s2 (a INTEGER PRIMARY KEY);
249 CREATE TABLE s3 (a INTEGER);
250 CREATE UNIQUE INDEX ndx on s3(a);
253 SELECT s1.a FROM s1 left join s2 using (a);
257 SELECT s1.a FROM s1 left join s3 using (a);
260 do_execsql_test 6.0 {
261 CREATE TABLE u1(a INTEGER PRIMARY KEY, b, c);
262 CREATE TABLE u2(a INTEGER PRIMARY KEY, b, c);
263 CREATE INDEX u1ab ON u1(b, c);
266 SELECT u2.* FROM u2 LEFT JOIN u1 ON( u1.a=u2.a AND u1.b=u2.b AND u1.c=u2.c );
271 do_execsql_test 7.0 {
272 CREATE TABLE t1(a,b); INSERT INTO t1 VALUES(1,2),(3,4),(5,6);
273 CREATE TABLE t2(c,d); INSERT INTO t2 VALUES(2,4),(3,6);
274 CREATE TABLE t3(x); INSERT INTO t3 VALUES(9);
277 FROM t1 LEFT JOIN (SELECT * FROM t2, t3) ON (c=b AND x=9)
280 } {3 4 {} {} {} x 5 6 {} {} {} x}
282 #-------------------------------------------------------------------------
286 do_execsql_test 8.0 {
291 do_execsql_test 8.1 {
292 SELECT * FROM t0 LEFT JOIN t1
293 WHERE (t1.c0 BETWEEN 0 AND 0) > ('' AND t0.c0);
296 #-------------------------------------------------------------------------
297 # Ticket [45f4bf4eb] reported by Manuel Rigger (2020-04-25)
299 # Follow up error reported by Eric Speckman on the SQLite forum
300 # https://sqlite.org/forum/info/c49496d24d35bd7c (2020-08-19)
303 do_execsql_test 9.0 {
304 CREATE TABLE t0(c0 INT);
305 CREATE VIEW v0(c0) AS SELECT CAST(t0.c0 AS INTEGER) FROM t0;
306 INSERT INTO t0(c0) VALUES (0);
309 do_execsql_test 9.1 {
310 SELECT typeof(c0), c0 FROM v0 WHERE c0>='0'
313 do_execsql_test 9.2 {
314 SELECT * FROM t0, v0 WHERE v0.c0 >= '0';
317 do_execsql_test 9.3 {
318 SELECT * FROM t0 LEFT JOIN v0 WHERE v0.c0 >= '0';
321 do_execsql_test 9.4 {
322 SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0';
325 do_execsql_test 9.5 {
326 SELECT * FROM t0 LEFT JOIN v0 ON v0.c0 >= '0' WHERE TRUE
327 UNION SELECT 0,0 WHERE 0;
330 do_execsql_test 9.10 {
331 CREATE TABLE t1 (aaa);
332 INSERT INTO t1 VALUES(23456);
333 CREATE TABLE t2(bbb);
334 CREATE VIEW v2(ccc) AS SELECT bbb IS 1234 FROM t2;
335 SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2;
337 optimization_control db query-flattener 0
338 do_execsql_test 9.11 {
339 SELECT ccc, ccc IS NULL AS ddd FROM t1 LEFT JOIN v2;