2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library.
2N/A# This file implements tests for joins, including outer joins.
2N/Aset testdir [file dirname $argv0]
2N/A CREATE TABLE t1(a,b,c);
2N/A INSERT INTO t1 VALUES(1,2,3);
2N/A INSERT INTO t1 VALUES(2,3,4);
2N/A INSERT INTO t1 VALUES(3,4,5);
2N/A} {1 2 3 2 3 4 3 4 5}
2N/A CREATE TABLE t2(b,c,d);
2N/A INSERT INTO t2 VALUES(1,2,3);
2N/A INSERT INTO t2 VALUES(2,3,4);
2N/A INSERT INTO t2 VALUES(3,4,5);
2N/A} {1 2 3 2 3 4 3 4 5}
2N/A SELECT * FROM t1 NATURAL JOIN t2;
2N/A SELECT * FROM t2 NATURAL JOIN t1;
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b,c);
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b);
2N/A SELECT * FROM t1 INNER JOIN t2 USING(c);
2N/A SELECT * FROM t1 INNER JOIN t2 USING(c,b);
2N/A SELECT * FROM t1 NATURAL CROSS JOIN t2;
2N/A SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
2N/A SELECT * FROM t1 NATURAL INNER JOIN t2;
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b,c);
2N/A SELECT * FROM t1 natural inner join t2;
2N/A SELECT * FROM t1 NATURAL JOIN
2N/A (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
2N/A SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
2N/A CREATE TABLE t3(c,d,e);
2N/A INSERT INTO t3 VALUES(2,3,4);
2N/A INSERT INTO t3 VALUES(3,4,5);
2N/A INSERT INTO t3 VALUES(4,5,6);
2N/A} {2 3 4 3 4 5 4 5 6}
2N/A SELECT * FROM t1 natural join t2 natural join t3;
2N/A} {1 2 3 4 5 2 3 4 5 6}
2N/A SELECT * FROM t1 natural join t2 natural join t3;
2N/A CREATE TABLE t4(d,e,f);
2N/A INSERT INTO t4 VALUES(2,3,4);
2N/A INSERT INTO t4 VALUES(3,4,5);
2N/A INSERT INTO t4 VALUES(4,5,6);
2N/A} {2 3 4 3 4 5 4 5 6}
2N/A SELECT * FROM t1 natural join t2 natural join t4;
2N/A SELECT * FROM t1 natural join t2 natural join t4;
2N/A SELECT * FROM t1 natural join t2 natural join t3 WHERE
t1.a=1
2N/A SELECT * FROM t1 NATURAL LEFT JOIN t2;
2N/A} {1 2 3 4 2 3 4 5 3 4 5 {}}
2N/A SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
2N/A} {1 2 3 {} 2 3 4 1 3 4 5 2}
2N/A SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
2N/A} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
2N/A} {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
2N/A} {2 3 4 {} {} {} 3 4 5 1 2 3}
2N/A} {1 2 3 {} {} {} 2 3 4 {} {} {}}
2N/A} {1 {a NATURAL join may not have an ON or USING clause}}
2N/A SELECT * FROM t1 NATURAL JOIN t2 USING(b);
2N/A} {1 {a NATURAL join may not have an ON or USING clause}}
2N/A} {1 {cannot have both ON and USING clauses in the same join}}
2N/A SELECT * FROM t1 JOIN t2 USING(a);
2N/A} {1 {cannot join using column a - column not present in both tables}}
2N/A SELECT * FROM t1 USING(a);
2N/A} {0 {1 2 3 2 3 4 3 4 5}}
2N/A SELECT * FROM t1 INNER OUTER JOIN t2;
2N/A} {1 {unknown or unsupported join type: INNER OUTER}}
2N/A SELECT * FROM t1 LEFT BOGUS JOIN t2;
2N/A} {1 {unknown or unsupported join type: LEFT BOGUS}}
2N/A CREATE TABLE t5(a INTEGER PRIMARY KEY);
2N/A CREATE TABLE t6(a INTEGER);
2N/A INSERT INTO t6 VALUES(NULL);
2N/A INSERT INTO t6 VALUES(NULL);
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A INSERT INTO t6 SELECT * FROM t6;
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A UPDATE t6 SET a='xyz';
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A create table centros (id integer primary key, centro);
2N/A INSERT INTO centros VALUES(1,'xxx');
2N/A create table usuarios (id integer primary key, nombre, apellidos,
2N/A INSERT INTO usuarios VALUES(1,'a','aa',1);
2N/A INSERT INTO usuarios VALUES(2,'b','bb',1);
2N/A INSERT INTO usuarios VALUES(3,'c','cc',NULL);
2N/A create index idcentro on usuarios (idcentro);
2N/A} {1 a xxx 2 b xxx 3 c {}}
2N/A# A test for ticket #247.
2N/A CREATE TABLE t7 (x, y);
2N/A INSERT INTO t7 VALUES ("pa1", 1);
2N/A INSERT INTO t7 VALUES ("pa2", NULL);
2N/A INSERT INTO t7 VALUES ("pa3", NULL);
2N/A INSERT INTO t7 VALUES ("pa4", 2);
2N/A INSERT INTO t7 VALUES ("pa30", 131);
2N/A INSERT INTO t7 VALUES ("pa31", 130);
2N/A INSERT INTO t7 VALUES ("pa28", NULL);
2N/A CREATE TABLE t8 (a integer primary key, b);
2N/A INSERT INTO t8 VALUES (1, "pa1");
2N/A INSERT INTO t8 VALUES (2, "pa4");
2N/A INSERT INTO t8 VALUES (3, NULL);
2N/A INSERT INTO t8 VALUES (4, NULL);
2N/A INSERT INTO t8 VALUES (130, "pa31");
2N/A INSERT INTO t8 VALUES (131, "pa30");
2N/A SELECT coalesce(
t8.a,999) from t7 LEFT JOIN t8 on y=a;
2N/A} {1 999 999 2 131 130 999}
2N/A# Make sure a left join where the right table is really a view that
2N/A# is itself a join works right. Ticket #306.
2N/A CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
2N/A INSERT INTO t9 VALUES(1,11);
2N/A INSERT INTO t9 VALUES(2,22);
2N/A CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
2N/A INSERT INTO t10 VALUES(1,2);
2N/A INSERT INTO t10 VALUES(3,3);
2N/A CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
2N/A INSERT INTO t11 VALUES(2,111);
2N/A INSERT INTO t11 VALUES(3,333);
2N/A CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE
t10.y=
t11.p;
2N/A SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
2N/A} {1 11 1 111 2 22 {} {}}
2N/A SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE
t10.y=
t11.p)
2N/A} {1 11 1 111 2 22 {} {}}
2N/A SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
2N/A} {1 111 1 11 3 333 {} {}}
2N/A# Ticket #350 describes a scenario where LEFT OUTER JOIN does not
2N/A# function correctly if the right table in the join is really
2N/A# To test the problem, we generate the same LEFT OUTER JOIN in two
2N/A# separate selects but with on using a subquery and the other calling
2N/A# the table directly. Then connect the two SELECTs using an EXCEPT.
2N/A# Both queries should generate the same results so the answer should
2N/A CREATE TABLE t12(a,b);
2N/A INSERT INTO t12 VALUES(1,11);
2N/A INSERT INTO t12 VALUES(2,22);
2N/A CREATE TABLE t13(b,c);
2N/A INSERT INTO t13 VALUES(22,222);
2N/A SELECT * FROM t12 NATURAL LEFT JOIN t13
2N/A SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
2N/A CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
2N/A SELECT * FROM t12 NATURAL LEFT JOIN t13
2N/A SELECT * FROM t12 NATURAL LEFT JOIN v13;