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. The
12 # focus of this file is testing SELECT statements that are part of
15 # $Id: subselect.test,v 1.16 2008/08/04 03:51:24 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Omit this whole file if the library is build without subquery support.
26 # Basic sanity checking. Try a simple subselect.
28 do_test subselect-1.1 {
30 CREATE TABLE t1(a int, b int);
31 INSERT INTO t1 VALUES(1,2);
32 INSERT INTO t1 VALUES(3,4);
33 INSERT INTO t1 VALUES(5,6);
35 execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
38 # Try a select with more than one result column.
40 do_test subselect-1.2 {
41 set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
43 } {1 {only a single result allowed for a SELECT that is part of an expression}}
45 # A subselect without an aggregate.
47 do_test subselect-1.3a {
48 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
50 do_test subselect-1.3b {
51 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
53 do_test subselect-1.3c {
54 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
56 do_test subselect-1.3d {
57 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
60 do_test subselect-1.3e {
63 WHERE a = (SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY 1);
68 # What if the subselect doesn't return any value. We should get
69 # NULL as the result. Check it out.
71 do_test subselect-1.4 {
72 execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
75 # Try multiple subselects within a single expression.
77 do_test subselect-1.5 {
79 CREATE TABLE t2(x int, y int);
80 INSERT INTO t2 VALUES(1,2);
81 INSERT INTO t2 VALUES(2,4);
82 INSERT INTO t2 VALUES(3,8);
83 INSERT INTO t2 VALUES(4,16);
87 WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
91 # Try something useful. Delete every entry from t2 where the
92 # x value is less than half of the maximum.
94 do_test subselect-1.6 {
95 execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
96 execsql {SELECT x FROM t2 ORDER BY x}
99 # Make sure sorting works for SELECTs there used as a scalar expression.
101 do_test subselect-2.1 {
103 SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
106 do_test subselect-2.2 {
108 SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
111 do_test subselect-2.3 {
113 SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
117 # Verify that the ORDER BY clause is honored in a subquery.
120 do_test subselect-3.1 {
122 CREATE TABLE t3(x int);
123 INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
124 SELECT * FROM t3 ORDER BY x;
127 } ;# ifcapable compound
128 ifcapable !compound {
129 do_test subselect-3.1 {
131 CREATE TABLE t3(x int);
132 INSERT INTO t3 SELECT a FROM t1;
133 INSERT INTO t3 SELECT b FROM t1;
134 SELECT * FROM t3 ORDER BY x;
137 } ;# ifcapable !compound
139 do_test subselect-3.2 {
141 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
144 do_test subselect-3.3 {
146 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
149 do_test subselect-3.4 {
151 SELECT (SELECT x FROM t3 ORDER BY x);
154 do_test subselect-3.5 {
156 SELECT (SELECT x FROM t3 ORDER BY x DESC);
159 do_test subselect-3.6 {
161 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
164 do_test subselect-3.7 {
166 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
169 do_test subselect-3.8 {
171 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
174 do_test subselect-3.9 {
176 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
179 do_test subselect-3.10 {
181 SELECT x FROM t3 WHERE x IN
182 (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
187 # Make sure type affinities work correctly on subqueries with
188 # an ORDER BY clause.
190 do_test subselect-4.1 {
192 CREATE TABLE t4(a TEXT, b TEXT);
193 INSERT INTO t4 VALUES('a','1');
194 INSERT INTO t4 VALUES('b','2');
195 INSERT INTO t4 VALUES('c','3');
196 SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b);
199 do_test subselect-4.2 {
201 SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b LIMIT 1);
204 do_test subselect-4.3 {
206 SELECT a FROM t4 WHERE b IN (SELECT b FROM t4 ORDER BY b DESC LIMIT 1);