select6.test revision 2
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
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. The
2N/A# focus of this file is testing SELECT statements that contain
2N/A# subqueries in their FROM clause.
2N/A#
2N/A# $Id: select6.test,v 1.11 2004/01/24 20:18:13 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/Ado_test select6-1.0 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t1(x, y);
2N/A INSERT INTO t1 VALUES(1,1);
2N/A INSERT INTO t1 VALUES(2,2);
2N/A INSERT INTO t1 VALUES(3,2);
2N/A INSERT INTO t1 VALUES(4,3);
2N/A INSERT INTO t1 VALUES(5,3);
2N/A INSERT INTO t1 VALUES(6,3);
2N/A INSERT INTO t1 VALUES(7,3);
2N/A INSERT INTO t1 VALUES(8,4);
2N/A INSERT INTO t1 VALUES(9,4);
2N/A INSERT INTO t1 VALUES(10,4);
2N/A INSERT INTO t1 VALUES(11,4);
2N/A INSERT INTO t1 VALUES(12,4);
2N/A INSERT INTO t1 VALUES(13,4);
2N/A INSERT INTO t1 VALUES(14,4);
2N/A INSERT INTO t1 VALUES(15,4);
2N/A INSERT INTO t1 VALUES(16,5);
2N/A INSERT INTO t1 VALUES(17,5);
2N/A INSERT INTO t1 VALUES(18,5);
2N/A INSERT INTO t1 VALUES(19,5);
2N/A INSERT INTO t1 VALUES(20,5);
2N/A COMMIT;
2N/A SELECT DISTINCT y FROM t1 ORDER BY y;
2N/A }
2N/A} {1 2 3 4 5}
2N/A
2N/Ado_test select6-1.1 {
2N/A execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
2N/A} {x 1 y 1}
2N/Ado_test select6-1.2 {
2N/A execsql {SELECT count(*) FROM (SELECT y FROM t1)}
2N/A} {20}
2N/Ado_test select6-1.3 {
2N/A execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
2N/A} {5}
2N/Ado_test select6-1.4 {
2N/A execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
2N/A} {5}
2N/Ado_test select6-1.5 {
2N/A execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
2N/A} {5}
2N/A
2N/Ado_test select6-1.6 {
2N/A execsql {
2N/A SELECT *
2N/A FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
2N/A (SELECT max(x),y FROM t1 GROUP BY y) as b
2N/A WHERE a.y=b.y ORDER BY a.y
2N/A }
2N/A} {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
2N/Ado_test select6-1.7 {
2N/A execsql {
2N/A SELECT a.y, a.[count(*)], [max(x)], [count(*)]
2N/A FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
2N/A (SELECT max(x),y FROM t1 GROUP BY y) as b
2N/A WHERE a.y=b.y ORDER BY a.y
2N/A }
2N/A} {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
2N/Ado_test select6-1.8 {
2N/A execsql {
2N/A SELECT q, p, r
2N/A FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
2N/A (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
2N/A WHERE q=s ORDER BY s
2N/A }
2N/A} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
2N/Ado_test select6-1.9 {
2N/A execsql {
2N/A SELECT q, p, r, b.[min(x)+y]
2N/A FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
2N/A (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
2N/A WHERE q=s ORDER BY s
2N/A }
2N/A} {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
2N/A
2N/Ado_test select6-2.0 {
2N/A execsql {
2N/A CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
2N/A INSERT INTO t2 SELECT * FROM t1;
2N/A SELECT DISTINCT b FROM t2 ORDER BY b;
2N/A }
2N/A} {1 2 3 4 5}
2N/Ado_test select6-2.1 {
2N/A execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
2N/A} {a 1 b 1}
2N/Ado_test select6-2.2 {
2N/A execsql {SELECT count(*) FROM (SELECT b FROM t2)}
2N/A} {20}
2N/Ado_test select6-2.3 {
2N/A execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
2N/A} {5}
2N/Ado_test select6-2.4 {
2N/A execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
2N/A} {5}
2N/Ado_test select6-2.5 {
2N/A execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
2N/A} {5}
2N/A
2N/Ado_test select6-2.6 {
2N/A execsql {
2N/A SELECT *
2N/A FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
2N/A (SELECT max(a),b FROM t2 GROUP BY b) as b
2N/A WHERE a.b=b.b ORDER BY a.b
2N/A }
2N/A} {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
2N/Ado_test select6-2.7 {
2N/A execsql {
2N/A SELECT a.b, a.[count(*)], [max(a)], [count(*)]
2N/A FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
2N/A (SELECT max(a),b FROM t2 GROUP BY b) as b
2N/A WHERE a.b=b.b ORDER BY a.b
2N/A }
2N/A} {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
2N/Ado_test select6-2.8 {
2N/A execsql {
2N/A SELECT q, p, r
2N/A FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
2N/A (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
2N/A WHERE q=s ORDER BY s
2N/A }
2N/A} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
2N/Ado_test select6-2.9 {
2N/A execsql {
2N/A SELECT a.q, a.p, b.r
2N/A FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
2N/A (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
2N/A WHERE a.q=b.s ORDER BY a.q
2N/A }
2N/A} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
2N/A
2N/Ado_test sqlite6-3.1 {
2N/A execsql2 {
2N/A SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
2N/A }
2N/A} {x 3 y 2}
2N/Ado_test sqlite6-3.2 {
2N/A execsql {
2N/A SELECT * FROM
2N/A (SELECT a.q, a.p, b.r
2N/A FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
2N/A (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
2N/A WHERE a.q=b.s ORDER BY a.q)
2N/A ORDER BY q
2N/A }
2N/A} {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
2N/Ado_test select6-3.3 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
2N/A }
2N/A} {10.5 3.7 14.2}
2N/Ado_test select6-3.4 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
2N/A }
2N/A} {11.5 4 15.5}
2N/Ado_test select6-3.5 {
2N/A execsql {
2N/A SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
2N/A }
2N/A} {4 3 7}
2N/Ado_test select6-3.6 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
2N/A WHERE a>10
2N/A }
2N/A} {10.5 3.7 14.2}
2N/Ado_test select6-3.7 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
2N/A WHERE a<10
2N/A }
2N/A} {}
2N/Ado_test select6-3.8 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
2N/A WHERE a>10
2N/A }
2N/A} {11.5 4 15.5}
2N/Ado_test select6-3.9 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
2N/A WHERE a<10
2N/A }
2N/A} {}
2N/Ado_test select6-3.10 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
2N/A ORDER BY a
2N/A }
2N/A} {1 1 2 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
2N/Ado_test select6-3.11 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM
2N/A (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
2N/A WHERE b<4 ORDER BY a
2N/A }
2N/A} {1 1 2 2.5 2 4.5 5.5 3 8.5}
2N/Ado_test select6-3.12 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM
2N/A (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
2N/A WHERE b<4 ORDER BY a
2N/A }
2N/A} {2.5 2 4.5 5.5 3 8.5}
2N/Ado_test select6-3.13 {
2N/A execsql {
2N/A SELECT a,b,a+b FROM
2N/A (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
2N/A ORDER BY a
2N/A }
2N/A} {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
2N/Ado_test select6-3.14 {
2N/A execsql {
2N/A SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
2N/A ORDER BY [count(*)]
2N/A }
2N/A} {1 1 2 2 4 3 5 5 8 4}
2N/Ado_test select6-3.15 {
2N/A execsql {
2N/A SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
2N/A ORDER BY y
2N/A }
2N/A} {1 1 2 2 4 3 8 4 5 5}
2N/A
2N/Ado_test select6-4.1 {
2N/A execsql {
2N/A SELECT a,b,c FROM
2N/A (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
2N/A WHERE a<10 ORDER BY a;
2N/A }
2N/A} {8 4 12 9 4 13}
2N/Ado_test select6-4.2 {
2N/A execsql {
2N/A SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test select6-4.3 {
2N/A execsql {
2N/A SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test select6-4.4 {
2N/A execsql {
2N/A SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
2N/A }
2N/A} {2.5}
2N/Ado_test select6-4.5 {
2N/A execsql {
2N/A SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
2N/A }
2N/A} {2.5}
2N/A
2N/Ado_test select6-5.1 {
2N/A execsql {
2N/A SELECT a,x,b FROM
2N/A (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
2N/A (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
2N/A WHERE a=b
2N/A ORDER BY a
2N/A }
2N/A} {8 5 8 9 6 9 10 7 10}
2N/Ado_test select6-5.2 {
2N/A execsql {
2N/A SELECT a,x,b FROM
2N/A (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
2N/A (SELECT x AS 'b' FROM t1 WHERE y=4)
2N/A WHERE a=b
2N/A ORDER BY a
2N/A }
2N/A} {8 5 8 9 6 9 10 7 10}
2N/A
2N/A# Tests of compound sub-selects
2N/A#
2N/Ado_test select5-6.1 {
2N/A execsql {
2N/A DELETE FROM t1 WHERE x>4;
2N/A SELECT * FROM t1
2N/A }
2N/A} {1 1 2 2 3 2 4 3}
2N/Ado_test select6-6.2 {
2N/A execsql {
2N/A SELECT * FROM (
2N/A SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
2N/A ) ORDER BY a;
2N/A }
2N/A} {1 2 3 4 11 12 13 14}
2N/Ado_test select6-6.3 {
2N/A execsql {
2N/A SELECT * FROM (
2N/A SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
2N/A ) ORDER BY a;
2N/A }
2N/A} {1 2 2 3 3 4 4 5}
2N/Ado_test select6-6.4 {
2N/A execsql {
2N/A SELECT * FROM (
2N/A SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
2N/A ) ORDER BY a;
2N/A }
2N/A} {1 2 3 4 5}
2N/Ado_test select6-6.5 {
2N/A execsql {
2N/A SELECT * FROM (
2N/A SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
2N/A ) ORDER BY a;
2N/A }
2N/A} {2 3 4}
2N/Ado_test select6-6.6 {
2N/A execsql {
2N/A SELECT * FROM (
2N/A SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
2N/A ) ORDER BY a;
2N/A }
2N/A} {1 3}
2N/A
2N/A# Subselects with no FROM clause
2N/A#
2N/Ado_test select6-7.1 {
2N/A execsql {
2N/A SELECT * FROM (SELECT 1)
2N/A }
2N/A} {1}
2N/Ado_test select6-7.2 {
2N/A execsql {
2N/A SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
2N/A }
2N/A} {abc 2 1 1 2 abc}
2N/Ado_test select6-7.3 {
2N/A execsql {
2N/A SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
2N/A }
2N/A} {}
2N/Ado_test select6-7.4 {
2N/A execsql2 {
2N/A SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
2N/A }
2N/A} {c abc b 2 a 1 a 1 b 2 c abc}
2N/A
2N/A# The following procedure compiles the SQL given as an argument and returns
2N/A# TRUE if that SQL uses any transient tables and returns FALSE if no
2N/A# transient tables are used. This is used to make sure that the
2N/A# sqliteFlattenSubquery() routine in select.c is doing its job.
2N/A#
2N/Aproc is_flat {sql} {
2N/A return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenTemp]]
2N/A}
2N/A
2N/A# Check that the flattener works correctly for deeply nested subqueries
2N/A# involving joins.
2N/A#
2N/Ado_test select6-8.1 {
2N/A execsql {
2N/A BEGIN;
2N/A CREATE TABLE t3(p,q);
2N/A INSERT INTO t3 VALUES(1,11);
2N/A INSERT INTO t3 VALUES(2,22);
2N/A CREATE TABLE t4(q,r);
2N/A INSERT INTO t4 VALUES(11,111);
2N/A INSERT INTO t4 VALUES(22,222);
2N/A COMMIT;
2N/A SELECT * FROM t3 NATURAL JOIN t4;
2N/A }
2N/A} {1 11 111 2 22 222}
2N/Ado_test select6-8.2 {
2N/A execsql {
2N/A SELECT y, p, q, r FROM
2N/A (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
2N/A (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
2N/A WHERE y=p
2N/A }
2N/A} {1 1 11 111 2 2 22 222 2 2 22 222}
2N/Ado_test select6-8.3 {
2N/A is_flat {
2N/A SELECT y, p, q, r FROM
2N/A (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
2N/A (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
2N/A WHERE y=p
2N/A }
2N/A} {1}
2N/Ado_test select6-8.4 {
2N/A execsql {
2N/A SELECT DISTINCT y, p, q, r FROM
2N/A (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
2N/A (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
2N/A WHERE y=p
2N/A }
2N/A} {1 1 11 111 2 2 22 222}
2N/Ado_test select6-8.5 {
2N/A execsql {
2N/A SELECT * FROM
2N/A (SELECT y, p, q, r FROM
2N/A (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
2N/A (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
2N/A WHERE y=p) AS e,
2N/A (SELECT r AS z FROM t4 WHERE q=11) AS f
2N/A WHERE e.r=f.z
2N/A }
2N/A} {1 1 11 111 111}
2N/Ado_test select6-8.6 {
2N/A is_flat {
2N/A SELECT * FROM
2N/A (SELECT y, p, q, r FROM
2N/A (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
2N/A (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
2N/A WHERE y=p) AS e,
2N/A (SELECT r AS z FROM t4 WHERE q=11) AS f
2N/A WHERE e.r=f.z
2N/A }
2N/A} {1}
2N/A
2N/A
2N/Afinish_test