Same fix as r45172 for classes/iconimage:
[AROS-Contrib.git] / sqlite3 / test / subselect.test
blob58f68b11605c0234af9671c5c4e9d3d839930999
1 # 2001 September 15
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
13 # expressions.
15 # $Id: subselect.test,v 1.12 2005/01/21 03:12:16 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.
21 ifcapable !subquery {
22   finish_test
23   return
26 # Basic sanity checking.  Try a simple subselect.
28 do_test subselect-1.1 {
29   execsql {
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);
34   }
35   execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
36 } {3 4}
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]
42   lappend v $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)}
49 } {2}
50 do_test subselect-1.3b {
51   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
52 } {4}
53 do_test subselect-1.3c {
54   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
55 } {6}
56 do_test subselect-1.3c {
57   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
58 } {}
60 # What if the subselect doesn't return any value.  We should get
61 # NULL as the result.  Check it out.
63 do_test subselect-1.4 {
64   execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
65 } {2}
67 # Try multiple subselects within a single expression.
69 do_test subselect-1.5 {
70   execsql {
71     CREATE TABLE t2(x int, y int);
72     INSERT INTO t2 VALUES(1,2);
73     INSERT INTO t2 VALUES(2,4);
74     INSERT INTO t2 VALUES(3,8);
75     INSERT INTO t2 VALUES(4,16);
76   }
77   execsql {
78     SELECT y from t2 
79     WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
80   }
81 } {8}
83 # Try something useful.  Delete every entry from t2 where the
84 # x value is less than half of the maximum.
86 do_test subselect-1.6 {
87   execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
88   execsql {SELECT x FROM t2 ORDER BY x}
89 } {2 3 4}
91 # Make sure sorting works for SELECTs there used as a scalar expression.
93 do_test subselect-2.1 {
94   execsql {
95     SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
96   }
97 } {1 5}
98 do_test subselect-2.2 {
99   execsql {
100     SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
101   }
102 } {1}
103 do_test subselect-2.3 {
104   execsql {
105     SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
106   }
107 } {0}
109 # Verify that the ORDER BY clause is honored in a subquery.
111 ifcapable compound {
112 do_test subselect-3.1 {
113   execsql {
114     CREATE TABLE t3(x int);
115     INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
116     SELECT * FROM t3 ORDER BY x;
117   }
118 } {1 2 3 4 5 6}
119 } ;# ifcapable compound
120 ifcapable !compound {
121 do_test subselect-3.1 {
122   execsql {
123     CREATE TABLE t3(x int);
124     INSERT INTO t3 SELECT a FROM t1; 
125     INSERT INTO t3 SELECT b FROM t1;
126     SELECT * FROM t3 ORDER BY x;
127   }
128 } {1 2 3 4 5 6}
129 } ;# ifcapable !compound
131 do_test subselect-3.2 {
132   execsql {
133     SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
134   }
135 } {3.0}
136 do_test subselect-3.3 {
137   execsql {
138     SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
139   }
140 } {11.0}
141 do_test subselect-3.4 {
142   execsql {
143     SELECT (SELECT x FROM t3 ORDER BY x);
144   }
145 } {1}
146 do_test subselect-3.5 {
147   execsql {
148     SELECT (SELECT x FROM t3 ORDER BY x DESC);
149   }
150 } {6}
151 do_test subselect-3.6 {
152   execsql {
153     SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
154   }
155 } {1}
156 do_test subselect-3.7 {
157   execsql {
158     SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
159   }
160 } {6}
161 do_test subselect-3.8 {
162   execsql {
163     SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
164   }
165 } {3}
166 do_test subselect-3.9 {
167   execsql {
168     SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
169   }
170 } {4}
171 do_test subselect-3.10 {
172   execsql {
173     SELECT x FROM t3 WHERE x IN
174        (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
175   }
176 } {4}
178 finish_test