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.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
19 CREATE TABLE t1(a INT);
20 CREATE TABLE t2(b INT);
21 INSERT INTO t2(b) VALUES(NULL);
27 SELECT DISTINCT a FROM t1 FULL JOIN t2 ON true WHERE (b ISNULL);
30 SELECT a FROM t1 FULL JOIN t2 ON true;
33 SELECT a FROM t1 FULL JOIN t2 ON true WHERE (b ISNULL);
36 SELECT DISTINCT a FROM t1 FULL JOIN t2 ON true;
39 #-----------------------------------------------------------
44 CREATE TABLE r4(y INTEGER PRIMARY KEY);
45 INSERT INTO r4 VALUES(55);
49 SELECT 'value!' FROM r3 FULL JOIN r4 ON (y=x);
53 SELECT 'value!' FROM r3 FULL JOIN r4 ON (y=x) WHERE +y=55;
56 #-----------------------------------------------------------
61 CREATE TABLE t2 (c0 , c1 , c2 , UNIQUE (c0), UNIQUE (c2 DESC));
62 INSERT INTO t2 VALUES ('x', 'y', 'z');
64 CREATE VIEW v0(c0) AS SELECT FALSE;
67 do_catchsql_test 3.2 {
68 SELECT * FROM t0 LEFT OUTER JOIN t1 ON v0.c0 INNER JOIN v0 INNER JOIN t2 ON (t2.c2 NOT NULL);
69 } {1 {ON clause references tables to its right}}
71 #-------------------------------------------------------------
75 CREATE TABLE t1(a,b,c,d,e,f,g,h,PRIMARY KEY(a,b,c)) WITHOUT ROWID;
76 CREATE TABLE t2(i, j);
77 INSERT INTO t2 VALUES(10, 20);
81 SELECT (d IS NULL) FROM t1 RIGHT JOIN t2 ON (j=33);
85 CREATE INDEX i1 ON t1( (d IS NULL), d );
89 SELECT (d IS NULL) FROM t1 RIGHT JOIN t2 ON (j=33);
92 #-------------------------------------------------------------------------
100 INSERT INTO t3 VALUES('t3val');
103 do_execsql_test 5.1 {
104 SELECT * FROM t1 INNER JOIN t2 ON (0) RIGHT OUTER JOIN t3;
107 do_execsql_test 5.2 {
108 SELECT * FROM t1 INNER JOIN t2 ON (0) FULL OUTER JOIN t3;
111 do_execsql_test 5.3 {
112 SELECT * FROM t3 LEFT JOIN t2 ON (0);
115 do_execsql_test 5.4 {
116 SELECT * FROM t0 RIGHT JOIN t1 INNER JOIN t2 ON (0) RIGHT JOIN t3
119 do_execsql_test 5.5 {
120 SELECT * FROM t0 RIGHT JOIN t1 INNER JOIN t2 ON (0)
126 do_execsql_test 6.0 {
127 CREATE TABLE t1(a INT);
128 CREATE TABLE t2(b INT);
129 INSERT INTO t1 VALUES(3);
130 SELECT CASE WHEN t2.b THEN 0 ELSE 1 END FROM t1 LEFT JOIN t2 ON true;
132 do_execsql_test 6.1 {
133 SELECT * FROM t1 LEFT JOIN t2 ON true WHERE CASE WHEN t2.b THEN 0 ELSE 1 END;
136 #-------------------------------------------------------------------------
138 do_execsql_test 7.0 {
139 CREATE TABLE t1(a, b);
143 INSERT INTO t1 VALUES ('a', 'a');
144 INSERT INTO t2 VALUES ('ddd');
145 INSERT INTO t3 VALUES(1234);
148 do_execsql_test 7.1 {
149 SELECT t2.rowid FROM t1 JOIN (t2 JOIN t3);
152 do_execsql_test 7.1 {
153 UPDATE t1 SET b = t2.rowid FROM t2, t3;
156 do_execsql_test 7.2 {
160 #-------------------------------------------------------------------------
162 do_execsql_test 8.0 {
163 CREATE TABLE x1(a INTEGER PRIMARY KEY, b);
164 CREATE TABLE x2(c, d);
165 CREATE TABLE x3(rowid, _rowid_);
167 CREATE TABLE x4(rowid, _rowid_, oid);
169 INSERT INTO x1 VALUES(1000, 'thousand');
170 INSERT INTO x2 VALUES('c', 'd');
171 INSERT INTO x3(oid, rowid, _rowid_) VALUES(43, 'hello', 'world');
172 INSERT INTO x4(oid, rowid, _rowid_) VALUES('forty three', 'hello', 'world');
175 do_execsql_test 8.1 {
176 SELECT x3.oid FROM x1 JOIN (x2 JOIN x3 ON c='c')
180 do_execsql_test 8.2 {
181 SELECT x3.rowid FROM x1 JOIN (x2 JOIN x3 ON c='c')
184 do_execsql_test 8.3 {
185 SELECT x4.oid FROM x1 JOIN (x2 JOIN x4 ON c='c')
189 #---------------------------------------------------------------------
192 do_execsql_test 9.0 {
197 CREATE TABLE wo1(a PRIMARY KEY, b) WITHOUT ROWID;
198 CREATE TABLE wo2(a PRIMARY KEY, rowid) WITHOUT ROWID;
199 CREATE TABLE wo3(a PRIMARY KEY, b) WITHOUT ROWID;
202 do_catchsql_test 9.1 {
203 SELECT rowid FROM wo1, x1, x2;
204 } {1 {ambiguous column name: rowid}}
205 do_catchsql_test 9.2 {
206 SELECT rowid FROM wo1, (x1, x2);
207 } {1 {ambiguous column name: rowid}}
208 do_catchsql_test 9.3 {
209 SELECT rowid FROM wo1 JOIN (x1 JOIN x2);
210 } {1 {ambiguous column name: rowid}}
211 do_catchsql_test 9.4 {
212 SELECT a FROM wo1, x1, x2;
213 } {1 {ambiguous column name: a}}
216 # It is not possible to use "rowid" in a USING clause.
218 do_catchsql_test 9.5 {
219 SELECT * FROM x1 JOIN x2 USING (rowid);
220 } {1 {cannot join using column rowid - column not present in both tables}}
221 do_catchsql_test 9.6 {
222 SELECT * FROM wo2 JOIN x2 USING (rowid);
223 } {1 {cannot join using column rowid - column not present in both tables}}
225 # "rowid" columns are not matched by NATURAL JOIN. If they were, then
226 # the SELECT below would return zero rows.
227 do_execsql_test 9.7 {
228 INSERT INTO x1(rowid, a) VALUES(101, 'A');
229 INSERT INTO x2(rowid, b) VALUES(55, 'B');
230 SELECT * FROM x1 NATURAL JOIN x2;
233 do_execsql_test 9.8 {
234 INSERT INTO wo1(a, b) VALUES('mya', 'myb');
235 INSERT INTO wo2(a, rowid) VALUES('mypk', 'myrowid');
236 INSERT INTO wo3(a, b) VALUES('MYA', 'MYB');
237 INSERT INTO x3(rowid, c) VALUES(99, 'x3B');
240 do_catchsql_test 9.8 {
241 SELECT rowid FROM x1 JOIN (x2 JOIN wo2);
243 do_catchsql_test 9.9 {
244 SELECT _rowid_ FROM wo1 JOIN (wo3 JOIN x3)
246 do_catchsql_test 9.10 {
247 SELECT oid FROM wo1 JOIN (wo3 JOIN x3)
249 do_catchsql_test 9.11 {
250 SELECT oid FROM wo2 JOIN (wo3 JOIN x3)
254 do_execsql_test 10.0 {
255 CREATE TABLE rt0 (c0 INTEGER, c1 INTEGER, c2 INTEGER, c3 INTEGER, c4 INTEGER);
256 CREATE TABLE rt3 (c3 INTEGER);
258 INSERT INTO rt0(c3, c1) VALUES (x'', '1');
259 INSERT INTO rt0(c3, c1) VALUES ('-1', -1e500);
260 INSERT INTO rt0(c3, c1) VALUES (1, x'');
262 CREATE VIEW v6(c0, c1, c2) AS SELECT 0, 0, 0;
265 do_execsql_test 10.1 {
266 SELECT COUNT(*) FROM rt0 LEFT JOIN rt3 JOIN v6 ON ((CASE v6.c0 WHEN rt0.c4 THEN rt3.c3 END) NOT BETWEEN (rt0.c4) AND (NULL)) WHERE (rt0.c1); -- 2
269 do_execsql_test 10.2 {
270 SELECT COUNT(*) FROM rt0 LEFT JOIN rt3 RIGHT OUTER JOIN v6 ON ((CASE v6.c0 WHEN rt0.c4 THEN rt3.c3 END) NOT BETWEEN (rt0.c4) AND (NULL)) WHERE (rt0.c1); -- 2
273 #-------------------------------------------------------------------------
275 do_execsql_test 11.1 {
276 CREATE TABLE t1(a, b);
277 CREATE TABLE t2(c, d);
278 CREATE TABLE t3(e, f);
280 INSERT INTO t1 VALUES(1, 1);
281 INSERT INTO t2 VALUES(2, 2);
282 INSERT INTO t3 VALUES(3, 3);
285 do_execsql_test 11.2 {
286 SELECT * FROM t1 LEFT JOIN t2 RIGHT JOIN t3 ON (t2.c=10)
289 do_execsql_test 11.3 {
290 SELECT * FROM t1 LEFT JOIN t2 RIGHT JOIN t3 ON (t2.c=10) WHERE t1.a=1
293 #-------------------------------------------------------------------------
296 do_execsql_test 12.1 {
297 CREATE TABLE t1(a1 INT, b1 TEXT);
298 INSERT INTO t1 VALUES(88,'');
299 CREATE TABLE t2(c2 INT, d2 TEXT);
300 INSERT INTO t2 VALUES(88,'');
301 CREATE TABLE t3(e3 TEXT PRIMARY KEY);
302 INSERT INTO t3 VALUES('');
305 do_execsql_test 12.2 {
306 SELECT * FROM t1 LEFT JOIN t2 ON true RIGHT JOIN t3 ON d2=e3 WHERE c2 BETWEEN NULL AND a1;
308 do_execsql_test 12.3 {
309 SELECT * FROM t1 LEFT JOIN t2 ON true RIGHT JOIN t3 ON d2=e3 WHERE c2 BETWEEN NULL AND a1;
312 #-------------------------------------------------------------------------
313 # 2024-04-05 dbsqlfuzz b9e65e2f110df998f1306571fae7af6c01e4d92b
315 do_execsql_test 13.1 {
316 CREATE TABLE t1(a INT AS (b), b INT);
317 INSERT INTO t1(b) VALUES(123);
318 CREATE TABLE t2(a INT, c INT);
319 SELECT a FROM t2 NATURAL RIGHT JOIN t1;
321 do_execsql_test 13.2 {
322 CREATE INDEX t1a ON t1(a);
323 SELECT a FROM t2 NATURAL RIGHT JOIN t1;
325 # Further tests of the same logic (indexes on expressions
326 # used by RIGHT JOIN) from check-in ffe23af73fcb324d and
327 # forum post https://sqlite.org/forum/forumpost/9b491e1debf0b67a.
329 do_execsql_test 13.3 {
330 CREATE TABLE t3(a INT, b INT);
331 CREATE UNIQUE INDEX t3x ON t3(a, a+b);
332 INSERT INTO t3(a,b) VALUES(1,2),(4,8),(16,32),(4,80),(1,-300);
333 CREATE TABLE t4(x INT, y INT);
334 INSERT INTO t4(x,y) SELECT a, b FROM t3;
335 INSERT INTO t4(x,y) VALUES(99,99);
336 SELECT a1.a, sum( a1.a+a1.b ) FROM t3 AS a1 RIGHT JOIN t4 ON a=x
337 GROUP BY a1.a ORDER BY 1;
338 } {NULL NULL 1 -592 4 192 16 48}
339 do_execsql_test 13.4 {
340 SELECT sum( a1.a+a1.b ) FROM t3 AS a1 RIGHT JOIN t3 ON true
341 GROUP BY a1.a ORDER BY 1;