2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2002 May 24
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
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#***********************************************************************
2N/A# This file implements regression tests for SQLite library.
2N/A#
2N/A# This file implements tests for joins, including outer joins.
2N/A#
2N/A# $Id: join.test,v 1.11 2003/09/27 13:39:40 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/Ado_test join-1.1 {
2N/A execsql {
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 SELECT * FROM t1;
2N/A }
2N/A} {1 2 3 2 3 4 3 4 5}
2N/Ado_test join-1.2 {
2N/A execsql {
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 SELECT * FROM t2;
2N/A }
2N/A} {1 2 3 2 3 4 3 4 5}
2N/A
2N/Ado_test join-1.3 {
2N/A execsql2 {
2N/A SELECT * FROM t1 NATURAL JOIN t2;
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
2N/Ado_test join-1.3.1 {
2N/A execsql2 {
2N/A SELECT * FROM t2 NATURAL JOIN t1;
2N/A }
2N/A} {t2.b 2 t2.c 3 t2.d 4 t1.a 1 t2.b 3 t2.c 4 t2.d 5 t1.a 2}
2N/Ado_test join-1.4 {
2N/A execsql2 {
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b,c);
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
2N/Ado_test join-1.5 {
2N/A execsql2 {
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b);
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.c 4 t2.d 5}
2N/Ado_test join-1.6 {
2N/A execsql2 {
2N/A SELECT * FROM t1 INNER JOIN t2 USING(c);
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.b 2 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.b 3 t2.d 5}
2N/Ado_test join-1.7 {
2N/A execsql2 {
2N/A SELECT * FROM t1 INNER JOIN t2 USING(c,b);
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
2N/A
2N/Ado_test join-1.8 {
2N/A execsql {
2N/A SELECT * FROM t1 NATURAL CROSS JOIN t2;
2N/A }
2N/A} {1 2 3 4 2 3 4 5}
2N/Ado_test join-1.9 {
2N/A execsql {
2N/A SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
2N/A }
2N/A} {1 2 3 4 2 3 4 5}
2N/Ado_test join-1.10 {
2N/A execsql {
2N/A SELECT * FROM t1 NATURAL INNER JOIN t2;
2N/A }
2N/A} {1 2 3 4 2 3 4 5}
2N/Ado_test join-1.11 {
2N/A execsql {
2N/A SELECT * FROM t1 INNER JOIN t2 USING(b,c);
2N/A }
2N/A} {1 2 3 4 2 3 4 5}
2N/Ado_test join-1.12 {
2N/A execsql {
2N/A SELECT * FROM t1 natural inner join t2;
2N/A }
2N/A} {1 2 3 4 2 3 4 5}
2N/Ado_test join-1.13 {
2N/A execsql2 {
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 }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t3.d 4 t3.e 5}
2N/Ado_test join-1.14 {
2N/A execsql2 {
2N/A SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
2N/A NATURAL JOIN t1
2N/A }
2N/A} {tx.c 3 tx.d 4 tx.e 5 t1.a 1 t1.b 2}
2N/A
2N/Ado_test join-1.15 {
2N/A execsql {
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 SELECT * FROM t3;
2N/A }
2N/A} {2 3 4 3 4 5 4 5 6}
2N/Ado_test join-1.16 {
2N/A execsql {
2N/A SELECT * FROM t1 natural join t2 natural join t3;
2N/A }
2N/A} {1 2 3 4 5 2 3 4 5 6}
2N/Ado_test join-1.17 {
2N/A execsql2 {
2N/A SELECT * FROM t1 natural join t2 natural join t3;
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t3.e 5 t1.a 2 t1.b 3 t1.c 4 t2.d 5 t3.e 6}
2N/Ado_test join-1.18 {
2N/A execsql {
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 SELECT * FROM t4;
2N/A }
2N/A} {2 3 4 3 4 5 4 5 6}
2N/Ado_test join-1.19 {
2N/A execsql {
2N/A SELECT * FROM t1 natural join t2 natural join t4;
2N/A }
2N/A} {1 2 3 4 5 6}
2N/Ado_test join-1.19 {
2N/A execsql2 {
2N/A SELECT * FROM t1 natural join t2 natural join t4;
2N/A }
2N/A} {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t4.e 5 t4.f 6}
2N/Ado_test join-1.20 {
2N/A execsql {
2N/A SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
2N/A }
2N/A} {1 2 3 4 5}
2N/A
2N/Ado_test join-2.1 {
2N/A execsql {
2N/A SELECT * FROM t1 NATURAL LEFT JOIN t2;
2N/A }
2N/A} {1 2 3 4 2 3 4 5 3 4 5 {}}
2N/Ado_test join-2.2 {
2N/A execsql {
2N/A SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
2N/A }
2N/A} {1 2 3 {} 2 3 4 1 3 4 5 2}
2N/Ado_test join-2.3 {
2N/A catchsql {
2N/A SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
2N/A }
2N/A} {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
2N/Ado_test join-2.4 {
2N/A execsql {
2N/A SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
2N/A }
2N/A} {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
2N/Ado_test join-2.5 {
2N/A execsql {
2N/A SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
2N/A }
2N/A} {2 3 4 {} {} {} 3 4 5 1 2 3}
2N/Ado_test join-2.6 {
2N/A execsql {
2N/A SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
2N/A }
2N/A} {1 2 3 {} {} {} 2 3 4 {} {} {}}
2N/A
2N/Ado_test join-3.1 {
2N/A catchsql {
2N/A SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
2N/A }
2N/A} {1 {a NATURAL join may not have an ON or USING clause}}
2N/Ado_test join-3.2 {
2N/A catchsql {
2N/A SELECT * FROM t1 NATURAL JOIN t2 USING(b);
2N/A }
2N/A} {1 {a NATURAL join may not have an ON or USING clause}}
2N/Ado_test join-3.3 {
2N/A catchsql {
2N/A SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
2N/A }
2N/A} {1 {cannot have both ON and USING clauses in the same join}}
2N/Ado_test join-3.4 {
2N/A catchsql {
2N/A SELECT * FROM t1 JOIN t2 USING(a);
2N/A }
2N/A} {1 {cannot join using column a - column not present in both tables}}
2N/Ado_test join-3.5 {
2N/A catchsql {
2N/A SELECT * FROM t1 USING(a);
2N/A }
2N/A} {0 {1 2 3 2 3 4 3 4 5}}
2N/Ado_test join-3.6 {
2N/A catchsql {
2N/A SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
2N/A }
2N/A} {1 {no such column: t3.a}}
2N/Ado_test join-3.7 {
2N/A catchsql {
2N/A SELECT * FROM t1 INNER OUTER JOIN t2;
2N/A }
2N/A} {1 {unknown or unsupported join type: INNER OUTER}}
2N/Ado_test join-3.7 {
2N/A catchsql {
2N/A SELECT * FROM t1 LEFT BOGUS JOIN t2;
2N/A }
2N/A} {1 {unknown or unsupported join type: LEFT BOGUS}}
2N/A
2N/Ado_test join-4.1 {
2N/A execsql {
2N/A BEGIN;
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 COMMIT;
2N/A }
2N/A execsql {
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A }
2N/A} {}
2N/Ado_test join-4.2 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a<t5.a;
2N/A }
2N/A} {}
2N/Ado_test join-4.3 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a>t5.a;
2N/A }
2N/A} {}
2N/Ado_test join-4.4 {
2N/A execsql {
2N/A UPDATE t6 SET a='xyz';
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A }
2N/A} {}
2N/Ado_test join-4.6 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a<t5.a;
2N/A }
2N/A} {}
2N/Ado_test join-4.7 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a>t5.a;
2N/A }
2N/A} {}
2N/Ado_test join-4.8 {
2N/A execsql {
2N/A UPDATE t6 SET a=1;
2N/A SELECT * FROM t6 NATURAL JOIN t5;
2N/A }
2N/A} {}
2N/Ado_test join-4.9 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a<t5.a;
2N/A }
2N/A} {}
2N/Ado_test join-4.10 {
2N/A execsql {
2N/A SELECT * FROM t6, t5 WHERE t6.a>t5.a;
2N/A }
2N/A} {}
2N/A
2N/Ado_test join-5.1 {
2N/A execsql {
2N/A BEGIN;
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 idcentro integer);
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 END;
2N/A select usuarios.id, usuarios.nombre, centros.centro from
2N/A usuarios left outer join centros on usuarios.idcentro = centros.id;
2N/A }
2N/A} {1 a xxx 2 b xxx 3 c {}}
2N/A
2N/A# A test for ticket #247.
2N/A#
2N/Ado_test join-7.1 {
2N/A execsql {
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
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
2N/A SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
2N/A }
2N/A} {1 999 999 2 131 130 999}
2N/A
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#
2N/Ado_test join-8.1 {
2N/A execsql {
2N/A BEGIN;
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 COMMIT;
2N/A SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
2N/A }
2N/A} {1 11 1 111 2 22 {} {}}
2N/Ado_test join-8.2 {
2N/A execsql {
2N/A SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
2N/A ON( a=x);
2N/A }
2N/A} {1 11 1 111 2 22 {} {}}
2N/Ado_test join-8.3 {
2N/A execsql {
2N/A SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
2N/A }
2N/A} {1 111 1 11 3 333 {} {}}
2N/A
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# subquery.
2N/A#
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# be an empty set.
2N/A#
2N/Ado_test join-9.1 {
2N/A execsql {
2N/A BEGIN;
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 COMMIT;
2N/A SELECT * FROM t12 NATURAL LEFT JOIN t13
2N/A EXCEPT
2N/A SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
2N/A }
2N/A} {}
2N/Ado_test join-9.2 {
2N/A execsql {
2N/A CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
2N/A SELECT * FROM t12 NATURAL LEFT JOIN t13
2N/A EXCEPT
2N/A SELECT * FROM t12 NATURAL LEFT JOIN v13;
2N/A }
2N/A} {}
2N/A
2N/Afinish_test