sl@0
|
1 |
# 2005 January 19
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#*************************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
12 |
# focus of this script is testing correlated subqueries
|
sl@0
|
13 |
#
|
sl@0
|
14 |
# $Id: subquery.test,v 1.16 2008/07/10 00:32:42 drh Exp $
|
sl@0
|
15 |
#
|
sl@0
|
16 |
|
sl@0
|
17 |
set testdir [file dirname $argv0]
|
sl@0
|
18 |
source $testdir/tester.tcl
|
sl@0
|
19 |
|
sl@0
|
20 |
ifcapable !subquery {
|
sl@0
|
21 |
finish_test
|
sl@0
|
22 |
return
|
sl@0
|
23 |
}
|
sl@0
|
24 |
|
sl@0
|
25 |
do_test subquery-1.1 {
|
sl@0
|
26 |
execsql {
|
sl@0
|
27 |
BEGIN;
|
sl@0
|
28 |
CREATE TABLE t1(a,b);
|
sl@0
|
29 |
INSERT INTO t1 VALUES(1,2);
|
sl@0
|
30 |
INSERT INTO t1 VALUES(3,4);
|
sl@0
|
31 |
INSERT INTO t1 VALUES(5,6);
|
sl@0
|
32 |
INSERT INTO t1 VALUES(7,8);
|
sl@0
|
33 |
CREATE TABLE t2(x,y);
|
sl@0
|
34 |
INSERT INTO t2 VALUES(1,1);
|
sl@0
|
35 |
INSERT INTO t2 VALUES(3,9);
|
sl@0
|
36 |
INSERT INTO t2 VALUES(5,25);
|
sl@0
|
37 |
INSERT INTO t2 VALUES(7,49);
|
sl@0
|
38 |
COMMIT;
|
sl@0
|
39 |
}
|
sl@0
|
40 |
execsql {
|
sl@0
|
41 |
SELECT a, (SELECT y FROM t2 WHERE x=a) FROM t1 WHERE b<8
|
sl@0
|
42 |
}
|
sl@0
|
43 |
} {1 1 3 9 5 25}
|
sl@0
|
44 |
do_test subquery-1.2 {
|
sl@0
|
45 |
execsql {
|
sl@0
|
46 |
UPDATE t1 SET b=b+(SELECT y FROM t2 WHERE x=a);
|
sl@0
|
47 |
SELECT * FROM t1;
|
sl@0
|
48 |
}
|
sl@0
|
49 |
} {1 3 3 13 5 31 7 57}
|
sl@0
|
50 |
|
sl@0
|
51 |
do_test subquery-1.3 {
|
sl@0
|
52 |
execsql {
|
sl@0
|
53 |
SELECT b FROM t1 WHERE EXISTS(SELECT * FROM t2 WHERE y=a)
|
sl@0
|
54 |
}
|
sl@0
|
55 |
} {3}
|
sl@0
|
56 |
do_test subquery-1.4 {
|
sl@0
|
57 |
execsql {
|
sl@0
|
58 |
SELECT b FROM t1 WHERE NOT EXISTS(SELECT * FROM t2 WHERE y=a)
|
sl@0
|
59 |
}
|
sl@0
|
60 |
} {13 31 57}
|
sl@0
|
61 |
|
sl@0
|
62 |
# Simple tests to make sure correlated subqueries in WHERE clauses
|
sl@0
|
63 |
# are used by the query optimizer correctly.
|
sl@0
|
64 |
do_test subquery-1.5 {
|
sl@0
|
65 |
execsql {
|
sl@0
|
66 |
SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
|
sl@0
|
67 |
}
|
sl@0
|
68 |
} {1 1 3 3 5 5 7 7}
|
sl@0
|
69 |
do_test subquery-1.6 {
|
sl@0
|
70 |
execsql {
|
sl@0
|
71 |
CREATE INDEX i1 ON t1(a);
|
sl@0
|
72 |
SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
|
sl@0
|
73 |
}
|
sl@0
|
74 |
} {1 1 3 3 5 5 7 7}
|
sl@0
|
75 |
do_test subquery-1.7 {
|
sl@0
|
76 |
execsql {
|
sl@0
|
77 |
SELECT a, x FROM t2, t1 WHERE t1.a = (SELECT x);
|
sl@0
|
78 |
}
|
sl@0
|
79 |
} {1 1 3 3 5 5 7 7}
|
sl@0
|
80 |
|
sl@0
|
81 |
# Try an aggregate in both the subquery and the parent query.
|
sl@0
|
82 |
do_test subquery-1.8 {
|
sl@0
|
83 |
execsql {
|
sl@0
|
84 |
SELECT count(*) FROM t1 WHERE a > (SELECT count(*) FROM t2);
|
sl@0
|
85 |
}
|
sl@0
|
86 |
} {2}
|
sl@0
|
87 |
|
sl@0
|
88 |
# Test a correlated subquery disables the "only open the index" optimization.
|
sl@0
|
89 |
do_test subquery-1.9.1 {
|
sl@0
|
90 |
execsql {
|
sl@0
|
91 |
SELECT (y*2)>b FROM t1, t2 WHERE a=x;
|
sl@0
|
92 |
}
|
sl@0
|
93 |
} {0 1 1 1}
|
sl@0
|
94 |
do_test subquery-1.9.2 {
|
sl@0
|
95 |
execsql {
|
sl@0
|
96 |
SELECT a FROM t1 WHERE (SELECT (y*2)>b FROM t2 WHERE a=x);
|
sl@0
|
97 |
}
|
sl@0
|
98 |
} {3 5 7}
|
sl@0
|
99 |
|
sl@0
|
100 |
# Test that the flattening optimization works with subquery expressions.
|
sl@0
|
101 |
do_test subquery-1.10.1 {
|
sl@0
|
102 |
execsql {
|
sl@0
|
103 |
SELECT (SELECT a), b FROM t1;
|
sl@0
|
104 |
}
|
sl@0
|
105 |
} {1 3 3 13 5 31 7 57}
|
sl@0
|
106 |
do_test subquery-1.10.2 {
|
sl@0
|
107 |
execsql {
|
sl@0
|
108 |
SELECT * FROM (SELECT (SELECT a), b FROM t1);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
} {1 3 3 13 5 31 7 57}
|
sl@0
|
111 |
do_test subquery-1.10.3 {
|
sl@0
|
112 |
execsql {
|
sl@0
|
113 |
SELECT * FROM (SELECT (SELECT sum(a) FROM t1));
|
sl@0
|
114 |
}
|
sl@0
|
115 |
} {16}
|
sl@0
|
116 |
do_test subquery-1.10.4 {
|
sl@0
|
117 |
execsql {
|
sl@0
|
118 |
CREATE TABLE t5 (val int, period text PRIMARY KEY);
|
sl@0
|
119 |
INSERT INTO t5 VALUES(5, '2001-3');
|
sl@0
|
120 |
INSERT INTO t5 VALUES(10, '2001-4');
|
sl@0
|
121 |
INSERT INTO t5 VALUES(15, '2002-1');
|
sl@0
|
122 |
INSERT INTO t5 VALUES(5, '2002-2');
|
sl@0
|
123 |
INSERT INTO t5 VALUES(10, '2002-3');
|
sl@0
|
124 |
INSERT INTO t5 VALUES(15, '2002-4');
|
sl@0
|
125 |
INSERT INTO t5 VALUES(10, '2003-1');
|
sl@0
|
126 |
INSERT INTO t5 VALUES(5, '2003-2');
|
sl@0
|
127 |
INSERT INTO t5 VALUES(25, '2003-3');
|
sl@0
|
128 |
INSERT INTO t5 VALUES(5, '2003-4');
|
sl@0
|
129 |
|
sl@0
|
130 |
SELECT period, vsum
|
sl@0
|
131 |
FROM (SELECT
|
sl@0
|
132 |
a.period,
|
sl@0
|
133 |
(select sum(val) from t5 where period between a.period and '2002-4') vsum
|
sl@0
|
134 |
FROM t5 a where a.period between '2002-1' and '2002-4')
|
sl@0
|
135 |
WHERE vsum < 45 ;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
} {2002-2 30 2002-3 25 2002-4 15}
|
sl@0
|
138 |
do_test subquery-1.10.5 {
|
sl@0
|
139 |
execsql {
|
sl@0
|
140 |
SELECT period, vsum from
|
sl@0
|
141 |
(select a.period,
|
sl@0
|
142 |
(select sum(val) from t5 where period between a.period and '2002-4') vsum
|
sl@0
|
143 |
FROM t5 a where a.period between '2002-1' and '2002-4')
|
sl@0
|
144 |
WHERE vsum < 45 ;
|
sl@0
|
145 |
}
|
sl@0
|
146 |
} {2002-2 30 2002-3 25 2002-4 15}
|
sl@0
|
147 |
do_test subquery-1.10.6 {
|
sl@0
|
148 |
execsql {
|
sl@0
|
149 |
DROP TABLE t5;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
} {}
|
sl@0
|
152 |
|
sl@0
|
153 |
|
sl@0
|
154 |
|
sl@0
|
155 |
#------------------------------------------------------------------
|
sl@0
|
156 |
# The following test cases - subquery-2.* - are not logically
|
sl@0
|
157 |
# organized. They're here largely because they were failing during
|
sl@0
|
158 |
# one stage of development of sub-queries.
|
sl@0
|
159 |
#
|
sl@0
|
160 |
do_test subquery-2.1 {
|
sl@0
|
161 |
execsql {
|
sl@0
|
162 |
SELECT (SELECT 10);
|
sl@0
|
163 |
}
|
sl@0
|
164 |
} {10}
|
sl@0
|
165 |
do_test subquery-2.2.1 {
|
sl@0
|
166 |
execsql {
|
sl@0
|
167 |
CREATE TABLE t3(a PRIMARY KEY, b);
|
sl@0
|
168 |
INSERT INTO t3 VALUES(1, 2);
|
sl@0
|
169 |
INSERT INTO t3 VALUES(3, 1);
|
sl@0
|
170 |
}
|
sl@0
|
171 |
} {}
|
sl@0
|
172 |
do_test subquery-2.2.2 {
|
sl@0
|
173 |
execsql {
|
sl@0
|
174 |
SELECT * FROM t3 WHERE a IN (SELECT b FROM t3);
|
sl@0
|
175 |
}
|
sl@0
|
176 |
} {1 2}
|
sl@0
|
177 |
do_test subquery-2.2.3 {
|
sl@0
|
178 |
execsql {
|
sl@0
|
179 |
DROP TABLE t3;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
} {}
|
sl@0
|
182 |
do_test subquery-2.3.1 {
|
sl@0
|
183 |
execsql {
|
sl@0
|
184 |
CREATE TABLE t3(a TEXT);
|
sl@0
|
185 |
INSERT INTO t3 VALUES('10');
|
sl@0
|
186 |
}
|
sl@0
|
187 |
} {}
|
sl@0
|
188 |
do_test subquery-2.3.2 {
|
sl@0
|
189 |
execsql {
|
sl@0
|
190 |
SELECT a IN (10.0, 20) FROM t3;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
} {0}
|
sl@0
|
193 |
do_test subquery-2.3.3 {
|
sl@0
|
194 |
execsql {
|
sl@0
|
195 |
DROP TABLE t3;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
} {}
|
sl@0
|
198 |
do_test subquery-2.4.1 {
|
sl@0
|
199 |
execsql {
|
sl@0
|
200 |
CREATE TABLE t3(a TEXT);
|
sl@0
|
201 |
INSERT INTO t3 VALUES('XX');
|
sl@0
|
202 |
}
|
sl@0
|
203 |
} {}
|
sl@0
|
204 |
do_test subquery-2.4.2 {
|
sl@0
|
205 |
execsql {
|
sl@0
|
206 |
SELECT count(*) FROM t3 WHERE a IN (SELECT 'XX')
|
sl@0
|
207 |
}
|
sl@0
|
208 |
} {1}
|
sl@0
|
209 |
do_test subquery-2.4.3 {
|
sl@0
|
210 |
execsql {
|
sl@0
|
211 |
DROP TABLE t3;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
} {}
|
sl@0
|
214 |
do_test subquery-2.5.1 {
|
sl@0
|
215 |
execsql {
|
sl@0
|
216 |
CREATE TABLE t3(a INTEGER);
|
sl@0
|
217 |
INSERT INTO t3 VALUES(10);
|
sl@0
|
218 |
|
sl@0
|
219 |
CREATE TABLE t4(x TEXT);
|
sl@0
|
220 |
INSERT INTO t4 VALUES('10.0');
|
sl@0
|
221 |
}
|
sl@0
|
222 |
} {}
|
sl@0
|
223 |
do_test subquery-2.5.2 {
|
sl@0
|
224 |
# In the expr "x IN (SELECT a FROM t3)" the RHS of the IN operator
|
sl@0
|
225 |
# has text affinity and the LHS has integer affinity. The rule is
|
sl@0
|
226 |
# that we try to convert both sides to an integer before doing the
|
sl@0
|
227 |
# comparision. Hence, the integer value 10 in t3 will compare equal
|
sl@0
|
228 |
# to the string value '10.0' in t4 because the t4 value will be
|
sl@0
|
229 |
# converted into an integer.
|
sl@0
|
230 |
execsql {
|
sl@0
|
231 |
SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
|
sl@0
|
232 |
}
|
sl@0
|
233 |
} {10.0}
|
sl@0
|
234 |
do_test subquery-2.5.3.1 {
|
sl@0
|
235 |
# The t4i index cannot be used to resolve the "x IN (...)" constraint
|
sl@0
|
236 |
# because the constraint has integer affinity but t4i has text affinity.
|
sl@0
|
237 |
execsql {
|
sl@0
|
238 |
CREATE INDEX t4i ON t4(x);
|
sl@0
|
239 |
SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
|
sl@0
|
240 |
}
|
sl@0
|
241 |
} {10.0}
|
sl@0
|
242 |
do_test subquery-2.5.3.2 {
|
sl@0
|
243 |
# Verify that the t4i index was not used in the previous query
|
sl@0
|
244 |
set ::sqlite_query_plan
|
sl@0
|
245 |
} {t4 {}}
|
sl@0
|
246 |
do_test subquery-2.5.4 {
|
sl@0
|
247 |
execsql {
|
sl@0
|
248 |
DROP TABLE t3;
|
sl@0
|
249 |
DROP TABLE t4;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
} {}
|
sl@0
|
252 |
|
sl@0
|
253 |
#------------------------------------------------------------------
|
sl@0
|
254 |
# The following test cases - subquery-3.* - test tickets that
|
sl@0
|
255 |
# were raised during development of correlated subqueries.
|
sl@0
|
256 |
#
|
sl@0
|
257 |
|
sl@0
|
258 |
# Ticket 1083
|
sl@0
|
259 |
ifcapable view {
|
sl@0
|
260 |
do_test subquery-3.1 {
|
sl@0
|
261 |
catchsql { DROP TABLE t1; }
|
sl@0
|
262 |
catchsql { DROP TABLE t2; }
|
sl@0
|
263 |
execsql {
|
sl@0
|
264 |
CREATE TABLE t1(a,b);
|
sl@0
|
265 |
INSERT INTO t1 VALUES(1,2);
|
sl@0
|
266 |
CREATE VIEW v1 AS SELECT b FROM t1 WHERE a>0;
|
sl@0
|
267 |
CREATE TABLE t2(p,q);
|
sl@0
|
268 |
INSERT INTO t2 VALUES(2,9);
|
sl@0
|
269 |
SELECT * FROM v1 WHERE EXISTS(SELECT * FROM t2 WHERE p=v1.b);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
} {2}
|
sl@0
|
272 |
} else {
|
sl@0
|
273 |
catchsql { DROP TABLE t1; }
|
sl@0
|
274 |
catchsql { DROP TABLE t2; }
|
sl@0
|
275 |
execsql {
|
sl@0
|
276 |
CREATE TABLE t1(a,b);
|
sl@0
|
277 |
INSERT INTO t1 VALUES(1,2);
|
sl@0
|
278 |
CREATE TABLE t2(p,q);
|
sl@0
|
279 |
INSERT INTO t2 VALUES(2,9);
|
sl@0
|
280 |
}
|
sl@0
|
281 |
}
|
sl@0
|
282 |
|
sl@0
|
283 |
# Ticket 1084
|
sl@0
|
284 |
do_test subquery-3.2 {
|
sl@0
|
285 |
catchsql {
|
sl@0
|
286 |
CREATE TABLE t1(a,b);
|
sl@0
|
287 |
INSERT INTO t1 VALUES(1,2);
|
sl@0
|
288 |
}
|
sl@0
|
289 |
execsql {
|
sl@0
|
290 |
SELECT (SELECT t1.a) FROM t1;
|
sl@0
|
291 |
}
|
sl@0
|
292 |
} {1}
|
sl@0
|
293 |
|
sl@0
|
294 |
# Test Cases subquery-3.3.* test correlated subqueries where the
|
sl@0
|
295 |
# parent query is an aggregate query. Ticket #1105 is an example
|
sl@0
|
296 |
# of such a query.
|
sl@0
|
297 |
#
|
sl@0
|
298 |
do_test subquery-3.3.1 {
|
sl@0
|
299 |
execsql {
|
sl@0
|
300 |
SELECT a, (SELECT b) FROM t1 GROUP BY a;
|
sl@0
|
301 |
}
|
sl@0
|
302 |
} {1 2}
|
sl@0
|
303 |
do_test subquery-3.3.2 {
|
sl@0
|
304 |
catchsql {DROP TABLE t2}
|
sl@0
|
305 |
execsql {
|
sl@0
|
306 |
CREATE TABLE t2(c, d);
|
sl@0
|
307 |
INSERT INTO t2 VALUES(1, 'one');
|
sl@0
|
308 |
INSERT INTO t2 VALUES(2, 'two');
|
sl@0
|
309 |
SELECT a, (SELECT d FROM t2 WHERE a=c) FROM t1 GROUP BY a;
|
sl@0
|
310 |
}
|
sl@0
|
311 |
} {1 one}
|
sl@0
|
312 |
do_test subquery-3.3.3 {
|
sl@0
|
313 |
execsql {
|
sl@0
|
314 |
INSERT INTO t1 VALUES(2, 4);
|
sl@0
|
315 |
SELECT max(a), (SELECT d FROM t2 WHERE a=c) FROM t1;
|
sl@0
|
316 |
}
|
sl@0
|
317 |
} {2 two}
|
sl@0
|
318 |
do_test subquery-3.3.4 {
|
sl@0
|
319 |
execsql {
|
sl@0
|
320 |
SELECT a, (SELECT (SELECT d FROM t2 WHERE a=c)) FROM t1 GROUP BY a;
|
sl@0
|
321 |
}
|
sl@0
|
322 |
} {1 one 2 two}
|
sl@0
|
323 |
do_test subquery-3.3.5 {
|
sl@0
|
324 |
execsql {
|
sl@0
|
325 |
SELECT a, (SELECT count(*) FROM t2 WHERE a=c) FROM t1;
|
sl@0
|
326 |
}
|
sl@0
|
327 |
} {1 1 2 1}
|
sl@0
|
328 |
|
sl@0
|
329 |
#------------------------------------------------------------------
|
sl@0
|
330 |
# These tests - subquery-4.* - use the TCL statement cache to try
|
sl@0
|
331 |
# and expose bugs to do with re-using statements that have been
|
sl@0
|
332 |
# passed to sqlite3_reset().
|
sl@0
|
333 |
#
|
sl@0
|
334 |
# One problem was that VDBE memory cells were not being initialised
|
sl@0
|
335 |
# to NULL on the second and subsequent executions.
|
sl@0
|
336 |
#
|
sl@0
|
337 |
do_test subquery-4.1.1 {
|
sl@0
|
338 |
execsql {
|
sl@0
|
339 |
SELECT (SELECT a FROM t1);
|
sl@0
|
340 |
}
|
sl@0
|
341 |
} {1}
|
sl@0
|
342 |
do_test subquery-4.2 {
|
sl@0
|
343 |
execsql {
|
sl@0
|
344 |
DELETE FROM t1;
|
sl@0
|
345 |
SELECT (SELECT a FROM t1);
|
sl@0
|
346 |
}
|
sl@0
|
347 |
} {{}}
|
sl@0
|
348 |
do_test subquery-4.2.1 {
|
sl@0
|
349 |
execsql {
|
sl@0
|
350 |
CREATE TABLE t3(a PRIMARY KEY);
|
sl@0
|
351 |
INSERT INTO t3 VALUES(10);
|
sl@0
|
352 |
}
|
sl@0
|
353 |
execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
|
sl@0
|
354 |
} {}
|
sl@0
|
355 |
do_test subquery-4.2.2 {
|
sl@0
|
356 |
execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
|
sl@0
|
357 |
} {}
|
sl@0
|
358 |
|
sl@0
|
359 |
#------------------------------------------------------------------
|
sl@0
|
360 |
# The subquery-5.* tests make sure string literals in double-quotes
|
sl@0
|
361 |
# are handled efficiently. Double-quote literals are first checked
|
sl@0
|
362 |
# to see if they match any column names. If there is not column name
|
sl@0
|
363 |
# match then those literals are used a string constants. When a
|
sl@0
|
364 |
# double-quoted string appears, we want to make sure that the search
|
sl@0
|
365 |
# for a matching column name did not cause an otherwise static subquery
|
sl@0
|
366 |
# to become a dynamic (correlated) subquery.
|
sl@0
|
367 |
#
|
sl@0
|
368 |
do_test subquery-5.1 {
|
sl@0
|
369 |
proc callcntproc {n} {
|
sl@0
|
370 |
incr ::callcnt
|
sl@0
|
371 |
return $n
|
sl@0
|
372 |
}
|
sl@0
|
373 |
set callcnt 0
|
sl@0
|
374 |
db function callcnt callcntproc
|
sl@0
|
375 |
execsql {
|
sl@0
|
376 |
CREATE TABLE t4(x,y);
|
sl@0
|
377 |
INSERT INTO t4 VALUES('one',1);
|
sl@0
|
378 |
INSERT INTO t4 VALUES('two',2);
|
sl@0
|
379 |
INSERT INTO t4 VALUES('three',3);
|
sl@0
|
380 |
INSERT INTO t4 VALUES('four',4);
|
sl@0
|
381 |
CREATE TABLE t5(a,b);
|
sl@0
|
382 |
INSERT INTO t5 VALUES(1,11);
|
sl@0
|
383 |
INSERT INTO t5 VALUES(2,22);
|
sl@0
|
384 |
INSERT INTO t5 VALUES(3,33);
|
sl@0
|
385 |
INSERT INTO t5 VALUES(4,44);
|
sl@0
|
386 |
SELECT b FROM t5 WHERE a IN
|
sl@0
|
387 |
(SELECT callcnt(y)+0 FROM t4 WHERE x="two")
|
sl@0
|
388 |
}
|
sl@0
|
389 |
} {22}
|
sl@0
|
390 |
do_test subquery-5.2 {
|
sl@0
|
391 |
# This is the key test. The subquery should have only run once. If
|
sl@0
|
392 |
# The double-quoted identifier "two" were causing the subquery to be
|
sl@0
|
393 |
# processed as a correlated subquery, then it would have run 4 times.
|
sl@0
|
394 |
set callcnt
|
sl@0
|
395 |
} {1}
|
sl@0
|
396 |
|
sl@0
|
397 |
|
sl@0
|
398 |
# Ticket #1380. Make sure correlated subqueries on an IN clause work
|
sl@0
|
399 |
# correctly when the left-hand side of the IN operator is constant.
|
sl@0
|
400 |
#
|
sl@0
|
401 |
do_test subquery-6.1 {
|
sl@0
|
402 |
set callcnt 0
|
sl@0
|
403 |
execsql {
|
sl@0
|
404 |
SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=y)
|
sl@0
|
405 |
}
|
sl@0
|
406 |
} {one two three four}
|
sl@0
|
407 |
do_test subquery-6.2 {
|
sl@0
|
408 |
set callcnt
|
sl@0
|
409 |
} {4}
|
sl@0
|
410 |
do_test subquery-6.3 {
|
sl@0
|
411 |
set callcnt 0
|
sl@0
|
412 |
execsql {
|
sl@0
|
413 |
SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=1)
|
sl@0
|
414 |
}
|
sl@0
|
415 |
} {one two three four}
|
sl@0
|
416 |
do_test subquery-6.4 {
|
sl@0
|
417 |
set callcnt
|
sl@0
|
418 |
} {1}
|
sl@0
|
419 |
|
sl@0
|
420 |
if 0 { ############# disable until we get #2652 fixed
|
sl@0
|
421 |
# Ticket #2652. Allow aggregate functions of outer queries inside
|
sl@0
|
422 |
# a non-aggregate subquery.
|
sl@0
|
423 |
#
|
sl@0
|
424 |
do_test subquery-7.1 {
|
sl@0
|
425 |
execsql {
|
sl@0
|
426 |
CREATE TABLE t7(c7);
|
sl@0
|
427 |
INSERT INTO t7 VALUES(1);
|
sl@0
|
428 |
INSERT INTO t7 VALUES(2);
|
sl@0
|
429 |
INSERT INTO t7 VALUES(3);
|
sl@0
|
430 |
CREATE TABLE t8(c8);
|
sl@0
|
431 |
INSERT INTO t8 VALUES(100);
|
sl@0
|
432 |
INSERT INTO t8 VALUES(200);
|
sl@0
|
433 |
INSERT INTO t8 VALUES(300);
|
sl@0
|
434 |
CREATE TABLE t9(c9);
|
sl@0
|
435 |
INSERT INTO t9 VALUES(10000);
|
sl@0
|
436 |
INSERT INTO t9 VALUES(20000);
|
sl@0
|
437 |
INSERT INTO t9 VALUES(30000);
|
sl@0
|
438 |
|
sl@0
|
439 |
SELECT (SELECT c7+c8 FROM t7) FROM t8;
|
sl@0
|
440 |
}
|
sl@0
|
441 |
} {101 201 301}
|
sl@0
|
442 |
do_test subquery-7.2 {
|
sl@0
|
443 |
execsql {
|
sl@0
|
444 |
SELECT (SELECT max(c7)+c8 FROM t7) FROM t8;
|
sl@0
|
445 |
}
|
sl@0
|
446 |
} {103 203 303}
|
sl@0
|
447 |
do_test subquery-7.3 {
|
sl@0
|
448 |
execsql {
|
sl@0
|
449 |
SELECT (SELECT c7+max(c8) FROM t8) FROM t7
|
sl@0
|
450 |
}
|
sl@0
|
451 |
} {301}
|
sl@0
|
452 |
do_test subquery-7.4 {
|
sl@0
|
453 |
execsql {
|
sl@0
|
454 |
SELECT (SELECT max(c7)+max(c8) FROM t8) FROM t7
|
sl@0
|
455 |
}
|
sl@0
|
456 |
} {303}
|
sl@0
|
457 |
do_test subquery-7.5 {
|
sl@0
|
458 |
execsql {
|
sl@0
|
459 |
SELECT (SELECT c8 FROM t8 WHERE rowid=max(c7)) FROM t7
|
sl@0
|
460 |
}
|
sl@0
|
461 |
} {300}
|
sl@0
|
462 |
do_test subquery-7.6 {
|
sl@0
|
463 |
execsql {
|
sl@0
|
464 |
SELECT (SELECT (SELECT max(c7+c8+c9) FROM t9) FROM t8) FROM t7
|
sl@0
|
465 |
}
|
sl@0
|
466 |
} {30101 30102 30103}
|
sl@0
|
467 |
do_test subquery-7.7 {
|
sl@0
|
468 |
execsql {
|
sl@0
|
469 |
SELECT (SELECT (SELECT c7+max(c8+c9) FROM t9) FROM t8) FROM t7
|
sl@0
|
470 |
}
|
sl@0
|
471 |
} {30101 30102 30103}
|
sl@0
|
472 |
do_test subquery-7.8 {
|
sl@0
|
473 |
execsql {
|
sl@0
|
474 |
SELECT (SELECT (SELECT max(c7)+c8+c9 FROM t9) FROM t8) FROM t7
|
sl@0
|
475 |
}
|
sl@0
|
476 |
} {10103}
|
sl@0
|
477 |
do_test subquery-7.9 {
|
sl@0
|
478 |
execsql {
|
sl@0
|
479 |
SELECT (SELECT (SELECT c7+max(c8)+c9 FROM t9) FROM t8) FROM t7
|
sl@0
|
480 |
}
|
sl@0
|
481 |
} {10301 10302 10303}
|
sl@0
|
482 |
do_test subquery-7.10 {
|
sl@0
|
483 |
execsql {
|
sl@0
|
484 |
SELECT (SELECT (SELECT c7+c8+max(c9) FROM t9) FROM t8) FROM t7
|
sl@0
|
485 |
}
|
sl@0
|
486 |
} {30101 30102 30103}
|
sl@0
|
487 |
do_test subquery-7.11 {
|
sl@0
|
488 |
execsql {
|
sl@0
|
489 |
SELECT (SELECT (SELECT max(c7)+max(c8)+max(c9) FROM t9) FROM t8) FROM t7
|
sl@0
|
490 |
}
|
sl@0
|
491 |
} {30303}
|
sl@0
|
492 |
} ;############# Disabled
|
sl@0
|
493 |
|
sl@0
|
494 |
finish_test
|