123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- # 2001 September 15
- #
- # The author disclaims copyright to this source code. In place of
- # a legal notice, here is a blessing:
- #
- # May you do good and not evil.
- # May you find forgiveness for yourself and forgive others.
- # May you share freely, never taking more than you give.
- #
- #***********************************************************************
- # This file implements regression tests for SQLite library. The
- # focus of this file is testing SELECT statements that contain
- # subqueries in their FROM clause.
- #
- # $Id: select6.test,v 1.9 2002/04/30 19:20:29 drh Exp $
- set testdir [file dirname $argv0]
- source $testdir/tester.tcl
- do_test select6-1.0 {
- execsql {
- BEGIN;
- CREATE TABLE t1(x, y);
- INSERT INTO t1 VALUES(1,1);
- INSERT INTO t1 VALUES(2,2);
- INSERT INTO t1 VALUES(3,2);
- INSERT INTO t1 VALUES(4,3);
- INSERT INTO t1 VALUES(5,3);
- INSERT INTO t1 VALUES(6,3);
- INSERT INTO t1 VALUES(7,3);
- INSERT INTO t1 VALUES(8,4);
- INSERT INTO t1 VALUES(9,4);
- INSERT INTO t1 VALUES(10,4);
- INSERT INTO t1 VALUES(11,4);
- INSERT INTO t1 VALUES(12,4);
- INSERT INTO t1 VALUES(13,4);
- INSERT INTO t1 VALUES(14,4);
- INSERT INTO t1 VALUES(15,4);
- INSERT INTO t1 VALUES(16,5);
- INSERT INTO t1 VALUES(17,5);
- INSERT INTO t1 VALUES(18,5);
- INSERT INTO t1 VALUES(19,5);
- INSERT INTO t1 VALUES(20,5);
- COMMIT;
- SELECT DISTINCT y FROM t1 ORDER BY y;
- }
- } {1 2 3 4 5}
- do_test select6-1.1 {
- execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
- } {x 1 y 1}
- do_test select6-1.2 {
- execsql {SELECT count(*) FROM (SELECT y FROM t1)}
- } {20}
- do_test select6-1.3 {
- execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
- } {5}
- do_test select6-1.4 {
- execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
- } {5}
- do_test select6-1.5 {
- execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
- } {5}
- do_test select6-1.6 {
- execsql {
- SELECT *
- FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
- (SELECT max(x),y FROM t1 GROUP BY y) as b
- WHERE a.y=b.y ORDER BY a.y
- }
- } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
- do_test select6-1.7 {
- execsql {
- SELECT a.y, a.[count(*)], [max(x)], [count(*)]
- FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
- (SELECT max(x),y FROM t1 GROUP BY y) as b
- WHERE a.y=b.y ORDER BY a.y
- }
- } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
- do_test select6-1.8 {
- execsql {
- SELECT q, p, r
- FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
- (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
- WHERE q=s ORDER BY s
- }
- } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
- do_test select6-1.9 {
- execsql {
- SELECT q, p, r, b.[min(x)+y]
- FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
- (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
- WHERE q=s ORDER BY s
- }
- } {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
- do_test select6-2.0 {
- execsql {
- CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
- INSERT INTO t2 SELECT * FROM t1;
- SELECT DISTINCT b FROM t2 ORDER BY b;
- }
- } {1 2 3 4 5}
- do_test select6-2.1 {
- execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
- } {a 1 b 1}
- do_test select6-2.2 {
- execsql {SELECT count(*) FROM (SELECT b FROM t2)}
- } {20}
- do_test select6-2.3 {
- execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
- } {5}
- do_test select6-2.4 {
- execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
- } {5}
- do_test select6-2.5 {
- execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
- } {5}
- do_test select6-2.6 {
- execsql {
- SELECT *
- FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
- (SELECT max(a),b FROM t2 GROUP BY b) as b
- WHERE a.b=b.b ORDER BY a.b
- }
- } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
- do_test select6-2.7 {
- execsql {
- SELECT a.b, a.[count(*)], [max(a)], [count(*)]
- FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
- (SELECT max(a),b FROM t2 GROUP BY b) as b
- WHERE a.b=b.b ORDER BY a.b
- }
- } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
- do_test select6-2.8 {
- execsql {
- SELECT q, p, r
- FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
- (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
- WHERE q=s ORDER BY s
- }
- } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
- do_test select6-2.9 {
- execsql {
- SELECT a.q, a.p, b.r
- FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
- (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
- WHERE a.q=b.s ORDER BY a.q
- }
- } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
- do_test sqlite6-3.1 {
- execsql2 {
- SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
- }
- } {x 3 y 2}
- do_test sqlite6-3.2 {
- execsql {
- SELECT * FROM
- (SELECT a.q, a.p, b.r
- FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
- (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
- WHERE a.q=b.s ORDER BY a.q)
- ORDER BY [a.q]
- }
- } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
- do_test select6-3.3 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
- }
- } {10.5 3.7 14.2}
- do_test select6-3.4 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
- }
- } {11.5 4 15.5}
- do_test select6-3.5 {
- execsql {
- SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
- }
- } {4 3 7}
- do_test select6-3.6 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
- WHERE a>10
- }
- } {10.5 3.7 14.2}
- do_test select6-3.7 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
- WHERE a<10
- }
- } {}
- do_test select6-3.8 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
- WHERE a>10
- }
- } {11.5 4 15.5}
- do_test select6-3.9 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
- WHERE a<10
- }
- } {}
- do_test select6-3.10 {
- execsql {
- SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
- ORDER BY a
- }
- } {1 1 2 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
- do_test select6-3.11 {
- execsql {
- SELECT a,b,a+b FROM
- (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
- WHERE b<4 ORDER BY a
- }
- } {1 1 2 2.5 2 4.5 5.5 3 8.5}
- do_test select6-3.12 {
- execsql {
- SELECT a,b,a+b FROM
- (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
- WHERE b<4 ORDER BY a
- }
- } {2.5 2 4.5 5.5 3 8.5}
- do_test select6-3.13 {
- execsql {
- SELECT a,b,a+b FROM
- (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
- ORDER BY a
- }
- } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
- do_test select6-3.14 {
- execsql {
- SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
- ORDER BY [count(*)]
- }
- } {1 1 2 2 4 3 5 5 8 4}
- do_test select6-3.15 {
- execsql {
- SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
- ORDER BY y
- }
- } {1 1 2 2 4 3 8 4 5 5}
- do_test select6-4.1 {
- execsql {
- SELECT a,b,c FROM
- (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
- WHERE a<10 ORDER BY a;
- }
- } {8 4 12 9 4 13}
- do_test select6-4.2 {
- execsql {
- SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
- }
- } {1 2 3 4}
- do_test select6-4.3 {
- execsql {
- SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
- }
- } {1 2 3 4}
- do_test select6-4.4 {
- execsql {
- SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
- }
- } {2.5}
- do_test select6-4.5 {
- execsql {
- SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
- }
- } {2.5}
- do_test select6-5.1 {
- execsql {
- SELECT a,x,b FROM
- (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
- (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
- WHERE a=b
- ORDER BY a
- }
- } {8 5 8 9 6 9 10 7 10}
- do_test select6-5.2 {
- execsql {
- SELECT a,x,b FROM
- (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
- (SELECT x AS 'b' FROM t1 WHERE y=4)
- WHERE a=b
- ORDER BY a
- }
- } {8 5 8 9 6 9 10 7 10}
- # Tests of compound sub-selects
- #
- do_test select5-6.1 {
- execsql {
- DELETE FROM t1 WHERE x>4;
- SELECT * FROM t1
- }
- } {1 1 2 2 3 2 4 3}
- do_test select6-6.2 {
- execsql {
- SELECT * FROM (
- SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
- ) ORDER BY a;
- }
- } {1 2 3 4 11 12 13 14}
- do_test select6-6.3 {
- execsql {
- SELECT * FROM (
- SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
- ) ORDER BY a;
- }
- } {1 2 2 3 3 4 4 5}
- do_test select6-6.4 {
- execsql {
- SELECT * FROM (
- SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
- ) ORDER BY a;
- }
- } {1 2 3 4 5}
- do_test select6-6.5 {
- execsql {
- SELECT * FROM (
- SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
- ) ORDER BY a;
- }
- } {2 3 4}
- do_test select6-6.6 {
- execsql {
- SELECT * FROM (
- SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
- ) ORDER BY a;
- }
- } {1 3}
- # Subselects with no FROM clause
- #
- do_test select6-7.1 {
- execsql {
- SELECT * FROM (SELECT 1)
- }
- } {1}
- do_test select6-7.2 {
- execsql {
- SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
- }
- } {abc 2 1 1 2 abc}
- do_test select6-7.3 {
- execsql {
- SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
- }
- } {}
- do_test select6-7.4 {
- execsql2 {
- SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
- }
- } {c abc b 2 a 1 a 1 b 2 c abc}
- finish_test
|