8030 libmvect: uninitialized variable
[unleashed.git] / usr / src / lib / libsqlite / test / subselect.test
blob85b3911935558d313a21d4e68dd20601f6da73e2
2 #pragma ident   "%Z%%M% %I%     %E% SMI"
4 # 2001 September 15
6 # The author disclaims copyright to this source code.  In place of
7 # a legal notice, here is a blessing:
9 #    May you do good and not evil.
10 #    May you find forgiveness for yourself and forgive others.
11 #    May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.  The
15 # focus of this file is testing SELECT statements that are part of
16 # expressions.
18 # $Id: subselect.test,v 1.7 2002/07/15 18:55:26 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Basic sanity checking.  Try a simple subselect.
25 do_test subselect-1.1 {
26   execsql {
27     CREATE TABLE t1(a int, b int);
28     INSERT INTO t1 VALUES(1,2);
29     INSERT INTO t1 VALUES(3,4);
30     INSERT INTO t1 VALUES(5,6);
31   }
32   execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
33 } {3 4}
35 # Try a select with more than one result column.
37 do_test subselect-1.2 {
38   set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
39   lappend v $msg
40 } {1 {only a single result allowed for a SELECT that is part of an expression}}
42 # A subselect without an aggregate.
44 do_test subselect-1.3a {
45   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
46 } {2}
47 do_test subselect-1.3b {
48   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
49 } {4}
50 do_test subselect-1.3c {
51   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
52 } {6}
53 do_test subselect-1.3c {
54   execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
55 } {}
57 # What if the subselect doesn't return any value.  We should get
58 # NULL as the result.  Check it out.
60 do_test subselect-1.4 {
61   execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
62 } {2}
64 # Try multiple subselects within a single expression.
66 do_test subselect-1.5 {
67   execsql {
68     CREATE TABLE t2(x int, y int);
69     INSERT INTO t2 VALUES(1,2);
70     INSERT INTO t2 VALUES(2,4);
71     INSERT INTO t2 VALUES(3,8);
72     INSERT INTO t2 VALUES(4,16);
73   }
74   execsql {
75     SELECT y from t2 
76     WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
77   }
78 } {8}
80 # Try something useful.  Delete every entry from t2 where the
81 # x value is less than half of the maximum.
83 do_test subselect-1.6 {
84   execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
85   execsql {SELECT x FROM t2 ORDER BY x}
86 } {2 3 4}
88 # Make sure sorting works for SELECTs there used as a scalar expression.
90 do_test subselect-2.1 {
91   execsql {
92     SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
93   }
94 } {1 5}
95 do_test subselect-2.2 {
96   execsql {
97     SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
98   }
99 } {1}
100 do_test subselect-2.3 {
101   execsql {
102     SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
103   }
104 } {0}
106 # Verify that the ORDER BY clause is honored in a subquery.
108 do_test subselect-3.1 {
109   execsql {
110     CREATE TABLE t3(x int);
111     INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
112     SELECT * FROM t3 ORDER BY x;
113   }
114 } {1 2 3 4 5 6}
115 do_test subselect-3.2 {
116   execsql {
117     SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
118   }
119 } {3}
120 do_test subselect-3.3 {
121   execsql {
122     SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
123   }
124 } {11}
125 do_test subselect-3.4 {
126   execsql {
127     SELECT (SELECT x FROM t3 ORDER BY x);
128   }
129 } {1}
130 do_test subselect-3.5 {
131   execsql {
132     SELECT (SELECT x FROM t3 ORDER BY x DESC);
133   }
134 } {6}
135 do_test subselect-3.6 {
136   execsql {
137     SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
138   }
139 } {1}
140 do_test subselect-3.7 {
141   execsql {
142     SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
143   }
144 } {6}
145 do_test subselect-3.8 {
146   execsql {
147     SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
148   }
149 } {3}
150 do_test subselect-3.9 {
151   execsql {
152     SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);
153   }
154 } {4}
158 finish_test