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 involving
14 # virtual tables. The test cases in this file are copied from the file
15 # join.test, and some of the comments still reflect that.
17 # $Id: vtab6.test,v 1.5 2009/07/01 16:12:08 danielk1977 Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
27 register_echo_module [sqlite3_connection_pointer db]
30 CREATE TABLE real_t1(a,b,c);
31 CREATE TABLE real_t2(b,c,d);
32 CREATE TABLE real_t3(c,d,e);
33 CREATE TABLE real_t4(d,e,f);
34 CREATE TABLE real_t5(a INTEGER PRIMARY KEY);
35 CREATE TABLE real_t6(a INTEGER);
36 CREATE TABLE real_t7 (x, y);
37 CREATE TABLE real_t8 (a integer primary key, b);
38 CREATE TABLE real_t9(a INTEGER PRIMARY KEY, b);
39 CREATE TABLE real_t10(x INTEGER PRIMARY KEY, y);
40 CREATE TABLE real_t11(p INTEGER PRIMARY KEY, q);
41 CREATE TABLE real_t12(a,b);
42 CREATE TABLE real_t13(b,c);
43 CREATE TABLE real_t21(a,b,c);
44 CREATE TABLE real_t22(p,q);
46 foreach t [list t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t21 t22] {
47 execsql "CREATE VIRTUAL TABLE $t USING echo(real_$t)"
52 INSERT INTO t1 VALUES(1,2,3);
53 INSERT INTO t1 VALUES(2,3,4);
54 INSERT INTO t1 VALUES(3,4,5);
60 INSERT INTO t2 VALUES(1,2,3);
61 INSERT INTO t2 VALUES(2,3,4);
62 INSERT INTO t2 VALUES(3,4,5);
69 SELECT * FROM t1 NATURAL JOIN t2;
71 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
74 SELECT * FROM t2 NATURAL JOIN t1;
76 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
79 SELECT * FROM t2 AS x NATURAL JOIN t1;
81 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
84 SELECT * FROM t2 NATURAL JOIN t1 AS y;
86 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
89 SELECT b FROM t1 NATURAL JOIN t2;
94 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
96 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
99 SELECT * FROM t1 AS x INNER JOIN t2 USING(b,c);
101 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
102 do_test vtab6-1.4.3 {
104 SELECT * FROM t1 INNER JOIN t2 AS y USING(b,c);
106 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
107 do_test vtab6-1.4.4 {
109 SELECT * FROM t1 AS x INNER JOIN t2 AS y USING(b,c);
111 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
112 do_test vtab6-1.4.5 {
114 SELECT b FROM t1 JOIN t2 USING(b);
119 SELECT * FROM t1 INNER JOIN t2 USING(b);
121 } {a 1 b 2 c 3 c 3 d 4 a 2 b 3 c 4 c 4 d 5}
124 SELECT * FROM t1 INNER JOIN t2 USING(c);
126 } {a 1 b 2 c 3 b 2 d 4 a 2 b 3 c 4 b 3 d 5}
129 SELECT * FROM t1 INNER JOIN t2 USING(c,b);
131 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
135 SELECT * FROM t1 NATURAL CROSS JOIN t2;
140 SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
145 SELECT * FROM t1 NATURAL INNER JOIN t2;
150 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
155 SELECT * FROM t1 natural inner join t2;
162 SELECT * FROM t1 NATURAL JOIN
163 (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
165 } {a 1 b 2 c 3 d 4 e 5}
168 SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
171 } {c 3 d 4 e 5 a 1 b 2}
176 INSERT INTO t3 VALUES(2,3,4);
177 INSERT INTO t3 VALUES(3,4,5);
178 INSERT INTO t3 VALUES(4,5,6);
181 } {2 3 4 3 4 5 4 5 6}
184 SELECT * FROM t1 natural join t2 natural join t3;
186 } {1 2 3 4 5 2 3 4 5 6}
189 SELECT * FROM t1 natural join t2 natural join t3;
191 } {a 1 b 2 c 3 d 4 e 5 a 2 b 3 c 4 d 5 e 6}
194 INSERT INTO t4 VALUES(2,3,4);
195 INSERT INTO t4 VALUES(3,4,5);
196 INSERT INTO t4 VALUES(4,5,6);
199 } {2 3 4 3 4 5 4 5 6}
200 do_test vtab6-1.19.1 {
202 SELECT * FROM t1 natural join t2 natural join t4;
205 do_test vtab6-1.19.2 {
207 SELECT * FROM t1 natural join t2 natural join t4;
209 } {a 1 b 2 c 3 d 4 e 5 f 6}
212 SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
218 SELECT * FROM t1 NATURAL LEFT JOIN t2;
220 } {1 2 3 4 2 3 4 5 3 4 5 {}}
223 SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
225 } {1 2 3 {} 2 3 4 1 3 4 5 2}
228 # SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
230 #} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
233 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
235 } {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
236 do_test vtab6-2.4.1 {
238 SELECT * FROM t1 LEFT JOIN t2 ON t1.a IS t2.d
240 } {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
243 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
245 } {2 3 4 {} {} {} 3 4 5 1 2 3}
248 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
250 } {1 2 3 {} {} {} 2 3 4 {} {} {}}
254 SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
256 } {1 {a NATURAL join may not have an ON or USING clause}}
259 SELECT * FROM t1 NATURAL JOIN t2 USING(b);
261 } {1 {a NATURAL join may not have an ON or USING clause}}
264 SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
266 } {1 {near "USING": syntax error}}
269 SELECT * FROM t1 JOIN t2 USING(a);
271 } {1 {cannot join using column a - column not present in both tables}}
273 catchsql { SELECT * FROM t1 USING(a) }
274 } {1 {a JOIN clause is required before USING}}
277 SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
279 } {1 {no such column: t3.a}}
281 # EVIDENCE-OF: R-47973-48020 you cannot say "INNER OUTER JOIN", because
282 # that would be contradictory.
285 SELECT * FROM t1 INNER OUTER JOIN t2;
287 } {1 {unknown join type: INNER OUTER}}
291 SELECT * FROM t1 LEFT BOGUS JOIN t2;
293 } {1 {unknown join type: LEFT BOGUS}}
298 INSERT INTO t6 VALUES(NULL);
299 INSERT INTO t6 VALUES(NULL);
300 INSERT INTO t6 SELECT * FROM t6;
301 INSERT INTO t6 SELECT * FROM t6;
302 INSERT INTO t6 SELECT * FROM t6;
303 INSERT INTO t6 SELECT * FROM t6;
304 INSERT INTO t6 SELECT * FROM t6;
305 INSERT INTO t6 SELECT * FROM t6;
309 SELECT * FROM t6 NATURAL JOIN t5;
314 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
319 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
324 UPDATE t6 SET a='xyz';
325 SELECT * FROM t6 NATURAL JOIN t5;
330 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
335 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
341 SELECT * FROM t6 NATURAL JOIN t5;
346 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
351 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
355 # A test for ticket #247.
358 sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1
360 INSERT INTO t7 VALUES ("pa1", 1);
361 INSERT INTO t7 VALUES ("pa2", NULL);
362 INSERT INTO t7 VALUES ("pa3", NULL);
363 INSERT INTO t7 VALUES ("pa4", 2);
364 INSERT INTO t7 VALUES ("pa30", 131);
365 INSERT INTO t7 VALUES ("pa31", 130);
366 INSERT INTO t7 VALUES ("pa28", NULL);
368 INSERT INTO t8 VALUES (1, "pa1");
369 INSERT INTO t8 VALUES (2, "pa4");
370 INSERT INTO t8 VALUES (3, NULL);
371 INSERT INTO t8 VALUES (4, NULL);
372 INSERT INTO t8 VALUES (130, "pa31");
373 INSERT INTO t8 VALUES (131, "pa30");
375 SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
377 } {1 999 999 2 131 130 999}
379 # Make sure a left join where the right table is really a view that
380 # is itself a join works right. Ticket #306.
386 INSERT INTO t9 VALUES(1,11);
387 INSERT INTO t9 VALUES(2,22);
388 INSERT INTO t10 VALUES(1,2);
389 INSERT INTO t10 VALUES(3,3);
390 INSERT INTO t11 VALUES(2,111);
391 INSERT INTO t11 VALUES(3,333);
392 CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
394 SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
396 } {1 11 1 111 2 22 {} {}}
400 SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
403 } {1 11 1 111 2 22 {} {}}
407 SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
409 } {1 111 1 11 3 333 {} {}}
412 # Ticket #350 describes a scenario where LEFT OUTER JOIN does not
413 # function correctly if the right table in the join is really
416 # To test the problem, we generate the same LEFT OUTER JOIN in two
417 # separate selects but with on using a subquery and the other calling
418 # the table directly. Then connect the two SELECTs using an EXCEPT.
419 # Both queries should generate the same results so the answer should
426 INSERT INTO t12 VALUES(1,11);
427 INSERT INTO t12 VALUES(2,22);
428 INSERT INTO t13 VALUES(22,222);
434 do_test vtab6-9.1.1 {
436 SELECT * FROM t12 NATURAL LEFT JOIN t13
438 SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
445 CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
446 SELECT * FROM t12 NATURAL LEFT JOIN t13
448 SELECT * FROM t12 NATURAL LEFT JOIN v13;
452 } ;# ifcapable compound
457 CREATE INDEX i22 ON real_t22(q);
458 SELECT a FROM t21 LEFT JOIN t22 ON b=p WHERE q=
459 (SELECT max(m.q) FROM t22 m JOIN t21 n ON n.b=m.p WHERE n.c=1);
462 } ;# ifcapable subquery
464 do_test vtab6-11.1.0 {
466 CREATE TABLE ab_r(a, b);
467 CREATE TABLE bc_r(b, c);
469 CREATE VIRTUAL TABLE ab USING echo(ab_r);
470 CREATE VIRTUAL TABLE bc USING echo(bc_r);
472 INSERT INTO ab VALUES(1, 2);
473 INSERT INTO bc VALUES(2, 3);
477 do_test vtab6-11.1.1 {
479 SELECT a, b, c FROM ab NATURAL JOIN bc;
482 do_test vtab6-11.1.2 {
484 SELECT a, b, c FROM bc NATURAL JOIN ab;
488 set ::echo_module_cost 1.0
490 do_test vtab6-11.1.3 {
492 SELECT a, b, c FROM ab NATURAL JOIN bc;
495 do_test vtab6-11.1.4 {
497 SELECT a, b, c FROM bc NATURAL JOIN ab;
502 do_test vtab6-11.2.0 {
504 CREATE INDEX ab_i ON ab_r(b);
505 CREATE INDEX bc_i ON bc_r(b);
509 unset ::echo_module_cost
511 do_test vtab6-11.2.1 {
513 SELECT a, b, c FROM ab NATURAL JOIN bc;
516 do_test vtab6-11.2.2 {
518 SELECT a, b, c FROM bc NATURAL JOIN ab;
522 set ::echo_module_cost 1.0
524 do_test vtab6-11.2.3 {
526 SELECT a, b, c FROM ab NATURAL JOIN bc;
529 do_test vtab6-11.2.4 {
531 SELECT a, b, c FROM bc NATURAL JOIN ab;
535 unset ::echo_module_cost
538 register_echo_module [sqlite3_connection_pointer db]
540 do_test vtab6-11.3.1 {
542 SELECT a, b, c FROM ab NATURAL JOIN bc;
546 do_test vtab6-11.3.2 {
548 SELECT a, b, c FROM bc NATURAL JOIN ab;
552 set ::echo_module_cost 1.0
554 do_test vtab6-11.3.3 {
556 SELECT a, b, c FROM ab NATURAL JOIN bc;
559 do_test vtab6-11.3.4 {
561 SELECT a, b, c FROM bc NATURAL JOIN ab;
565 unset ::echo_module_cost
567 set ::echo_module_ignore_usable 1
570 do_test vtab6-11.4.1 {
572 SELECT a, b, c FROM ab NATURAL JOIN bc;
574 } {1 {ab.xBestIndex malfunction}}
575 do_test vtab6-11.4.2 {
577 SELECT a, b, c FROM bc NATURAL JOIN ab;
579 } {1 {bc.xBestIndex malfunction}}
581 unset ::echo_module_ignore_usable