blob: 9145bcc7535ef6882f5ab60fbc40f096f1a6236a [file] [log] [blame]
drh588a9a12008-09-01 15:52:10 +00001# 2001 September 15
drh6de4f4c2000-06-12 12:20:48 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh6de4f4c2000-06-12 12:20:48 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh6de4f4c2000-06-12 12:20:48 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the use of indices in WHERE clases.
13#
danielk19773072c5e2008-11-03 09:06:05 +000014# $Id: where.test,v 1.50 2008/11/03 09:06:06 danielk1977 Exp $
drh6de4f4c2000-06-12 12:20:48 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Build some test data
20#
21do_test where-1.0 {
22 execsql {
23 CREATE TABLE t1(w int, x int, y int);
24 CREATE TABLE t2(p int, q int, r int, s int);
25 }
26 for {set i 1} {$i<=100} {incr i} {
27 set w $i
28 set x [expr {int(log($i)/log(2))}]
29 set y [expr {$i*$i + 2*$i + 1}]
30 execsql "INSERT INTO t1 VALUES($w,$x,$y)"
31 }
danielk19773e8c37e2005-01-21 03:12:14 +000032
33 ifcapable subquery {
34 execsql {
35 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
36 }
37 } else {
38 set maxy [execsql {select max(y) from t1}]
39 execsql "
40 INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1;
41 "
42 }
43
drh6de4f4c2000-06-12 12:20:48 +000044 execsql {
drh6de4f4c2000-06-12 12:20:48 +000045 CREATE INDEX i1w ON t1(w);
46 CREATE INDEX i1xy ON t1(x,y);
47 CREATE INDEX i2p ON t2(p);
48 CREATE INDEX i2r ON t2(r);
49 CREATE INDEX i2qs ON t2(q, s);
50 }
51} {}
52
drh487ab3c2001-11-08 00:45:21 +000053# Do an SQL statement. Append the search count to the end of the result.
54#
55proc count sql {
56 set ::sqlite_search_count 0
57 return [concat [execsql $sql] $::sqlite_search_count]
58}
59
60# Verify that queries use an index. We are using the special variable
61# "sqlite_search_count" which tallys the number of executions of MoveTo
62# and Next operators in the VDBE. By verifing that the search count is
63# small we can be assured that indices are being used properly.
drh6de4f4c2000-06-12 12:20:48 +000064#
drh4f07e5f2007-05-14 11:34:46 +000065do_test where-1.1.1 {
66 count {SELECT x, y, w FROM t1 WHERE w=10}
67} {3 121 10 3}
drh9042f392005-07-15 23:24:23 +000068do_test where-1.1.2 {
69 set sqlite_query_plan
70} {t1 i1w}
drh4f07e5f2007-05-14 11:34:46 +000071do_test where-1.1.3 {
drhd1d38482008-10-07 23:46:38 +000072 db status step
73} {0}
74do_test where-1.1.4 {
75 db eval {SELECT x, y, w FROM t1 WHERE +w=10}
76} {3 121 10}
77do_test where-1.1.5 {
78 db status step
79} {99}
80do_test where-1.1.6 {
81 set sqlite_query_plan
82} {t1 {}}
83do_test where-1.1.7 {
drh4f07e5f2007-05-14 11:34:46 +000084 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
85} {3 121 10 3}
drhd1d38482008-10-07 23:46:38 +000086do_test where-1.1.8 {
drh4f07e5f2007-05-14 11:34:46 +000087 set sqlite_query_plan
88} {t1 i1w}
drhd1d38482008-10-07 23:46:38 +000089do_test where-1.1.9 {
90 db status step
91} {0}
drh4f07e5f2007-05-14 11:34:46 +000092do_test where-1.2.1 {
93 count {SELECT x, y, w FROM t1 WHERE w=11}
94} {3 144 11 3}
95do_test where-1.2.2 {
96 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
97} {3 144 11 3}
98do_test where-1.3.1 {
99 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
100} {3 144 11 3}
101do_test where-1.3.2 {
102 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
103} {3 144 11 3}
104do_test where-1.4.1 {
105 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
106} {11 3 144 3}
drh9042f392005-07-15 23:24:23 +0000107do_test where-1.4.2 {
drh7ec764a2005-07-21 03:48:20 +0000108 set sqlite_query_plan
drh9042f392005-07-15 23:24:23 +0000109} {t1 i1w}
drh4f07e5f2007-05-14 11:34:46 +0000110do_test where-1.4.3 {
111 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
112} {11 3 144 3}
113do_test where-1.4.4 {
114 set sqlite_query_plan
115} {t1 i1w}
drh6de4f4c2000-06-12 12:20:48 +0000116do_test where-1.5 {
drh487ab3c2001-11-08 00:45:21 +0000117 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
118} {3 144 3}
drh9042f392005-07-15 23:24:23 +0000119do_test where-1.5.2 {
120 set sqlite_query_plan
121} {t1 i1w}
drh6de4f4c2000-06-12 12:20:48 +0000122do_test where-1.6 {
drh487ab3c2001-11-08 00:45:21 +0000123 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
124} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000125do_test where-1.7 {
drh487ab3c2001-11-08 00:45:21 +0000126 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
127} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000128do_test where-1.8 {
drh487ab3c2001-11-08 00:45:21 +0000129 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
130} {3 144 3}
drh9042f392005-07-15 23:24:23 +0000131do_test where-1.8.2 {
132 set sqlite_query_plan
133} {t1 i1xy}
134do_test where-1.8.3 {
135 count {SELECT x, y FROM t1 WHERE y=144 AND x=3}
136 set sqlite_query_plan
137} {{} i1xy}
drh6de4f4c2000-06-12 12:20:48 +0000138do_test where-1.9 {
drh487ab3c2001-11-08 00:45:21 +0000139 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
140} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000141do_test where-1.10 {
drh487ab3c2001-11-08 00:45:21 +0000142 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
143} {3 121 3}
drh6de4f4c2000-06-12 12:20:48 +0000144do_test where-1.11 {
drh487ab3c2001-11-08 00:45:21 +0000145 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
146} {3 100 3}
147
148# New for SQLite version 2.1: Verify that that inequality constraints
149# are used correctly.
150#
151do_test where-1.12 {
152 count {SELECT w FROM t1 WHERE x=3 AND y<100}
153} {8 3}
154do_test where-1.13 {
155 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
156} {8 3}
157do_test where-1.14 {
158 count {SELECT w FROM t1 WHERE 3=x AND y<100}
159} {8 3}
160do_test where-1.15 {
161 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
162} {8 3}
163do_test where-1.16 {
164 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
165} {8 9 5}
166do_test where-1.17 {
167 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
168} {8 9 5}
169do_test where-1.18 {
170 count {SELECT w FROM t1 WHERE x=3 AND y>225}
171} {15 3}
172do_test where-1.19 {
173 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
174} {15 3}
175do_test where-1.20 {
176 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
177} {14 15 5}
178do_test where-1.21 {
179 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
180} {14 15 5}
181do_test where-1.22 {
182 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
183} {11 12 5}
184do_test where-1.23 {
185 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
186} {10 11 12 13 9}
187do_test where-1.24 {
188 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
189} {11 12 5}
190do_test where-1.25 {
191 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
192} {10 11 12 13 9}
193
194# Need to work on optimizing the BETWEEN operator.
195#
196# do_test where-1.26 {
197# count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
198# } {10 11 12 13 9}
199
200do_test where-1.27 {
201 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
danielk197721de2e72007-11-29 17:43:27 +0000202} {10 10}
drh7cf6e4d2004-05-19 14:56:55 +0000203
drh487ab3c2001-11-08 00:45:21 +0000204do_test where-1.28 {
205 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
206} {10 99}
207do_test where-1.29 {
208 count {SELECT w FROM t1 WHERE y==121}
209} {10 99}
210
211
212do_test where-1.30 {
213 count {SELECT w FROM t1 WHERE w>97}
drh9012bcb2004-12-19 00:11:35 +0000214} {98 99 100 3}
drh487ab3c2001-11-08 00:45:21 +0000215do_test where-1.31 {
216 count {SELECT w FROM t1 WHERE w>=97}
drh9012bcb2004-12-19 00:11:35 +0000217} {97 98 99 100 4}
drh487ab3c2001-11-08 00:45:21 +0000218do_test where-1.33 {
219 count {SELECT w FROM t1 WHERE w==97}
drh9012bcb2004-12-19 00:11:35 +0000220} {97 2}
drhac931eb2005-01-11 18:13:56 +0000221do_test where-1.33.1 {
222 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
223} {97 2}
224do_test where-1.33.2 {
225 count {SELECT w FROM t1 WHERE w<98 AND w==97}
226} {97 2}
227do_test where-1.33.3 {
228 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
229} {97 2}
230do_test where-1.33.4 {
231 count {SELECT w FROM t1 WHERE w>96 AND w==97}
232} {97 2}
233do_test where-1.33.5 {
234 count {SELECT w FROM t1 WHERE w==97 AND w==97}
235} {97 2}
drh487ab3c2001-11-08 00:45:21 +0000236do_test where-1.34 {
237 count {SELECT w FROM t1 WHERE w+1==98}
238} {97 99}
239do_test where-1.35 {
240 count {SELECT w FROM t1 WHERE w<3}
drh9012bcb2004-12-19 00:11:35 +0000241} {1 2 2}
drh487ab3c2001-11-08 00:45:21 +0000242do_test where-1.36 {
243 count {SELECT w FROM t1 WHERE w<=3}
drh9012bcb2004-12-19 00:11:35 +0000244} {1 2 3 3}
drh487ab3c2001-11-08 00:45:21 +0000245do_test where-1.37 {
drh8aff1012001-12-22 14:49:24 +0000246 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
drh9012bcb2004-12-19 00:11:35 +0000247} {1 2 3 99}
drh487ab3c2001-11-08 00:45:21 +0000248
drh6977fea2002-10-22 23:38:04 +0000249do_test where-1.38 {
250 count {SELECT (w) FROM t1 WHERE (w)>(97)}
drh9012bcb2004-12-19 00:11:35 +0000251} {98 99 100 3}
drh6977fea2002-10-22 23:38:04 +0000252do_test where-1.39 {
253 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
drh9012bcb2004-12-19 00:11:35 +0000254} {97 98 99 100 4}
drh6977fea2002-10-22 23:38:04 +0000255do_test where-1.40 {
256 count {SELECT (w) FROM t1 WHERE (w)==(97)}
drh9012bcb2004-12-19 00:11:35 +0000257} {97 2}
drh6977fea2002-10-22 23:38:04 +0000258do_test where-1.41 {
259 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
260} {97 99}
261
drh6de4f4c2000-06-12 12:20:48 +0000262
263# Do the same kind of thing except use a join as the data source.
264#
265do_test where-2.1 {
drh487ab3c2001-11-08 00:45:21 +0000266 count {
267 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000268 WHERE x=q AND y=s AND r=8977
269 }
drh487ab3c2001-11-08 00:45:21 +0000270} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000271do_test where-2.2 {
drh487ab3c2001-11-08 00:45:21 +0000272 count {
273 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000274 WHERE x=q AND s=y AND r=8977
275 }
drh487ab3c2001-11-08 00:45:21 +0000276} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000277do_test where-2.3 {
drh487ab3c2001-11-08 00:45:21 +0000278 count {
279 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000280 WHERE x=q AND s=y AND r=8977 AND w>10
281 }
drh487ab3c2001-11-08 00:45:21 +0000282} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000283do_test where-2.4 {
drh487ab3c2001-11-08 00:45:21 +0000284 count {
285 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000286 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
287 }
drh487ab3c2001-11-08 00:45:21 +0000288} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000289do_test where-2.5 {
drh487ab3c2001-11-08 00:45:21 +0000290 count {
291 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000292 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
293 }
drh487ab3c2001-11-08 00:45:21 +0000294} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000295do_test where-2.6 {
drh487ab3c2001-11-08 00:45:21 +0000296 count {
297 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000298 WHERE x=q AND p=77 AND s=y AND w>5
299 }
drh487ab3c2001-11-08 00:45:21 +0000300} {24 77 6}
drh6de4f4c2000-06-12 12:20:48 +0000301do_test where-2.7 {
drh487ab3c2001-11-08 00:45:21 +0000302 count {
303 SELECT w, p FROM t1, t2
drh6de4f4c2000-06-12 12:20:48 +0000304 WHERE x=q AND p>77 AND s=y AND w=5
305 }
drh487ab3c2001-11-08 00:45:21 +0000306} {5 96 6}
drh6de4f4c2000-06-12 12:20:48 +0000307
308# Lets do a 3-way join.
309#
310do_test where-3.1 {
drh487ab3c2001-11-08 00:45:21 +0000311 count {
312 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000313 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
314 }
drh9012bcb2004-12-19 00:11:35 +0000315} {11 90 11 8}
drh6de4f4c2000-06-12 12:20:48 +0000316do_test where-3.2 {
drh487ab3c2001-11-08 00:45:21 +0000317 count {
318 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000319 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
320 }
drh9012bcb2004-12-19 00:11:35 +0000321} {12 89 12 8}
drh6de4f4c2000-06-12 12:20:48 +0000322do_test where-3.3 {
drh487ab3c2001-11-08 00:45:21 +0000323 count {
324 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000325 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
326 }
drh9012bcb2004-12-19 00:11:35 +0000327} {15 86 86 8}
drh6de4f4c2000-06-12 12:20:48 +0000328
drh08192d52002-04-30 19:20:28 +0000329# Test to see that the special case of a constant WHERE clause is
330# handled.
331#
332do_test where-4.1 {
333 count {
334 SELECT * FROM t1 WHERE 0
335 }
336} {0}
337do_test where-4.2 {
338 count {
339 SELECT * FROM t1 WHERE 1 LIMIT 1
340 }
drhec7429a2005-10-06 16:53:14 +0000341} {1 0 4 0}
drh08192d52002-04-30 19:20:28 +0000342do_test where-4.3 {
343 execsql {
344 SELECT 99 WHERE 0
345 }
346} {}
347do_test where-4.4 {
348 execsql {
349 SELECT 99 WHERE 1
350 }
351} {99}
drhba0232a2005-06-06 17:27:19 +0000352do_test where-4.5 {
353 execsql {
354 SELECT 99 WHERE 0.1
355 }
356} {99}
357do_test where-4.6 {
358 execsql {
359 SELECT 99 WHERE 0.0
360 }
361} {}
drh85e9e222008-07-15 00:27:34 +0000362do_test where-4.7 {
363 execsql {
364 SELECT count(*) FROM t1 WHERE t1.w
365 }
366} {100}
drh08192d52002-04-30 19:20:28 +0000367
drh48185c12002-06-09 01:55:20 +0000368# Verify that IN operators in a WHERE clause are handled correctly.
danielk19773e8c37e2005-01-21 03:12:14 +0000369# Omit these tests if the build is not capable of sub-queries.
drh48185c12002-06-09 01:55:20 +0000370#
danielk19773e8c37e2005-01-21 03:12:14 +0000371ifcapable subquery {
372 do_test where-5.1 {
373 count {
374 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
375 }
drh4db38a72005-09-01 12:16:28 +0000376 } {1 0 4 2 1 9 3 1 16 4}
danielk19773e8c37e2005-01-21 03:12:14 +0000377 do_test where-5.2 {
378 count {
379 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
380 }
danielk197721de2e72007-11-29 17:43:27 +0000381 } {1 0 4 2 1 9 3 1 16 102}
danielk19773e8c37e2005-01-21 03:12:14 +0000382 do_test where-5.3 {
383 count {
384 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
385 }
drh4db38a72005-09-01 12:16:28 +0000386 } {1 0 4 2 1 9 3 1 16 14}
danielk19773e8c37e2005-01-21 03:12:14 +0000387 do_test where-5.4 {
388 count {
389 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
390 }
danielk197721de2e72007-11-29 17:43:27 +0000391 } {1 0 4 2 1 9 3 1 16 102}
danielk19773e8c37e2005-01-21 03:12:14 +0000392 do_test where-5.5 {
393 count {
394 SELECT * FROM t1 WHERE rowid IN
395 (select rowid from t1 where rowid IN (-1,2,4))
396 ORDER BY 1;
397 }
398 } {2 1 9 4 2 25 3}
399 do_test where-5.6 {
400 count {
401 SELECT * FROM t1 WHERE rowid+0 IN
402 (select rowid from t1 where rowid IN (-1,2,4))
403 ORDER BY 1;
404 }
danielk197721de2e72007-11-29 17:43:27 +0000405 } {2 1 9 4 2 25 103}
danielk19773e8c37e2005-01-21 03:12:14 +0000406 do_test where-5.7 {
407 count {
408 SELECT * FROM t1 WHERE w IN
409 (select rowid from t1 where rowid IN (-1,2,4))
410 ORDER BY 1;
411 }
412 } {2 1 9 4 2 25 9}
413 do_test where-5.8 {
414 count {
415 SELECT * FROM t1 WHERE w+0 IN
416 (select rowid from t1 where rowid IN (-1,2,4))
417 ORDER BY 1;
418 }
danielk197721de2e72007-11-29 17:43:27 +0000419 } {2 1 9 4 2 25 103}
danielk19773e8c37e2005-01-21 03:12:14 +0000420 do_test where-5.9 {
421 count {
422 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
423 }
424 } {2 1 9 3 1 16 7}
425 do_test where-5.10 {
426 count {
427 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
428 }
429 } {2 1 9 3 1 16 199}
430 do_test where-5.11 {
431 count {
432 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
433 }
434 } {79 6 6400 89 6 8100 199}
435 do_test where-5.12 {
436 count {
437 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
438 }
drh3adc9ce2005-07-28 16:51:51 +0000439 } {79 6 6400 89 6 8100 7}
danielk19773e8c37e2005-01-21 03:12:14 +0000440 do_test where-5.13 {
441 count {
442 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
443 }
444 } {2 1 9 3 1 16 7}
445 do_test where-5.14 {
446 count {
447 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
448 }
drh4db38a72005-09-01 12:16:28 +0000449 } {2 1 9 8}
drh3adc9ce2005-07-28 16:51:51 +0000450 do_test where-5.15 {
451 count {
452 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
453 }
454 } {2 1 9 3 1 16 11}
danielk19773e8c37e2005-01-21 03:12:14 +0000455}
drh48185c12002-06-09 01:55:20 +0000456
drh6bf89572004-11-03 16:27:01 +0000457# This procedure executes the SQL. Then it checks to see if the OP_Sort
458# opcode was executed. If an OP_Sort did occur, then "sort" is appended
459# to the result. If no OP_Sort happened, then "nosort" is appended.
460#
461# This procedure is used to check to make sure sorting is or is not
462# occurring as expected.
drhe3184742002-06-19 14:27:05 +0000463#
464proc cksort {sql} {
465 set data [execsql $sql]
drhd1d38482008-10-07 23:46:38 +0000466 if {[db status sort]} {set x sort} {set x nosort}
drhe3184742002-06-19 14:27:05 +0000467 lappend data $x
468 return $data
469}
470# Check out the logic that attempts to implement the ORDER BY clause
471# using an index rather than by sorting.
472#
473do_test where-6.1 {
474 execsql {
475 CREATE TABLE t3(a,b,c);
476 CREATE INDEX t3a ON t3(a);
477 CREATE INDEX t3bc ON t3(b,c);
478 CREATE INDEX t3acb ON t3(a,c,b);
479 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
480 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
481 }
drh3d1d95e2005-09-08 10:37:01 +0000482} {100 5050 5050 348550}
drhe3184742002-06-19 14:27:05 +0000483do_test where-6.2 {
484 cksort {
485 SELECT * FROM t3 ORDER BY a LIMIT 3
486 }
487} {1 100 4 2 99 9 3 98 16 nosort}
488do_test where-6.3 {
489 cksort {
490 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
491 }
492} {1 100 4 2 99 9 3 98 16 sort}
493do_test where-6.4 {
494 cksort {
495 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
496 }
497} {1 100 4 2 99 9 3 98 16 nosort}
498do_test where-6.5 {
499 cksort {
500 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
501 }
502} {1 100 4 2 99 9 3 98 16 nosort}
503do_test where-6.6 {
504 cksort {
505 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
506 }
507} {1 100 4 2 99 9 3 98 16 nosort}
508do_test where-6.7 {
509 cksort {
510 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
511 }
drh3adc9ce2005-07-28 16:51:51 +0000512} {1 100 4 2 99 9 3 98 16 nosort}
danielk19773e8c37e2005-01-21 03:12:14 +0000513ifcapable subquery {
514 do_test where-6.8 {
515 cksort {
516 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
517 }
518 } {1 100 4 2 99 9 3 98 16 sort}
519}
drhdd4852c2002-12-04 21:50:16 +0000520do_test where-6.9.1 {
drhe3184742002-06-19 14:27:05 +0000521 cksort {
522 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
523 }
524} {1 100 4 nosort}
drhac931eb2005-01-11 18:13:56 +0000525do_test where-6.9.1.1 {
526 cksort {
527 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
528 }
529} {1 100 4 nosort}
530do_test where-6.9.1.2 {
531 cksort {
532 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
533 }
534} {1 100 4 nosort}
drhdd4852c2002-12-04 21:50:16 +0000535do_test where-6.9.2 {
536 cksort {
537 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
538 }
539} {1 100 4 nosort}
540do_test where-6.9.3 {
541 cksort {
542 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
543 }
544} {1 100 4 nosort}
545do_test where-6.9.4 {
546 cksort {
547 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
548 }
549} {1 100 4 nosort}
550do_test where-6.9.5 {
551 cksort {
552 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
553 }
554} {1 100 4 nosort}
555do_test where-6.9.6 {
556 cksort {
557 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
558 }
559} {1 100 4 nosort}
560do_test where-6.9.7 {
561 cksort {
562 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
563 }
564} {1 100 4 sort}
565do_test where-6.9.8 {
566 cksort {
567 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
568 }
drh9012bcb2004-12-19 00:11:35 +0000569} {1 100 4 nosort}
drhdd4852c2002-12-04 21:50:16 +0000570do_test where-6.9.9 {
571 cksort {
572 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
573 }
drh9012bcb2004-12-19 00:11:35 +0000574} {1 100 4 nosort}
drhe3184742002-06-19 14:27:05 +0000575do_test where-6.10 {
576 cksort {
577 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
578 }
579} {1 100 4 nosort}
580do_test where-6.11 {
581 cksort {
582 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
583 }
584} {1 100 4 nosort}
585do_test where-6.12 {
586 cksort {
587 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
588 }
589} {1 100 4 nosort}
590do_test where-6.13 {
591 cksort {
592 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
593 }
drhdd4852c2002-12-04 21:50:16 +0000594} {100 1 10201 99 2 10000 98 3 9801 nosort}
595do_test where-6.13.1 {
596 cksort {
597 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
598 }
drhe3184742002-06-19 14:27:05 +0000599} {100 1 10201 99 2 10000 98 3 9801 sort}
600do_test where-6.14 {
601 cksort {
602 SELECT * FROM t3 ORDER BY b LIMIT 3
603 }
604} {100 1 10201 99 2 10000 98 3 9801 nosort}
605do_test where-6.15 {
606 cksort {
607 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
608 }
609} {1 0 2 1 3 1 nosort}
610do_test where-6.16 {
611 cksort {
612 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
613 }
614} {1 0 2 1 3 1 sort}
drhc330af12002-08-14 03:03:57 +0000615do_test where-6.19 {
616 cksort {
617 SELECT y FROM t1 ORDER BY w LIMIT 3;
618 }
619} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000620do_test where-6.20 {
621 cksort {
622 SELECT y FROM t1 ORDER BY rowid LIMIT 3;
623 }
624} {4 9 16 nosort}
625do_test where-6.21 {
626 cksort {
627 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
628 }
drhcc192542006-12-20 03:24:19 +0000629} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000630do_test where-6.22 {
631 cksort {
632 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
633 }
drhcc192542006-12-20 03:24:19 +0000634} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000635do_test where-6.23 {
636 cksort {
637 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
638 }
drhcc192542006-12-20 03:24:19 +0000639} {9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000640do_test where-6.24 {
641 cksort {
642 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
643 }
drhcc192542006-12-20 03:24:19 +0000644} {9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000645do_test where-6.25 {
646 cksort {
647 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
648 }
649} {9 16 nosort}
650do_test where-6.26 {
651 cksort {
652 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
653 }
654} {4 9 16 25 nosort}
655do_test where-6.27 {
656 cksort {
657 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
658 }
drhcc192542006-12-20 03:24:19 +0000659} {4 9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000660
drhc330af12002-08-14 03:03:57 +0000661
drhdd4852c2002-12-04 21:50:16 +0000662# Tests for reverse-order sorting.
663#
664do_test where-7.1 {
665 cksort {
666 SELECT w FROM t1 WHERE x=3 ORDER BY y;
667 }
668} {8 9 10 11 12 13 14 15 nosort}
669do_test where-7.2 {
670 cksort {
671 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
672 }
673} {15 14 13 12 11 10 9 8 nosort}
674do_test where-7.3 {
675 cksort {
676 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
677 }
678} {10 11 12 nosort}
679do_test where-7.4 {
680 cksort {
681 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
682 }
683} {15 14 13 nosort}
684do_test where-7.5 {
685 cksort {
686 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
687 }
688} {15 14 13 12 11 nosort}
689do_test where-7.6 {
690 cksort {
691 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
692 }
693} {15 14 13 12 11 10 nosort}
694do_test where-7.7 {
695 cksort {
696 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
697 }
698} {12 11 10 nosort}
699do_test where-7.8 {
700 cksort {
701 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
702 }
703} {13 12 11 10 nosort}
704do_test where-7.9 {
705 cksort {
706 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
707 }
708} {13 12 11 nosort}
709do_test where-7.10 {
710 cksort {
711 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
712 }
713} {12 11 10 nosort}
714do_test where-7.11 {
715 cksort {
716 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
717 }
718} {10 11 12 nosort}
719do_test where-7.12 {
720 cksort {
721 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
722 }
723} {10 11 12 13 nosort}
724do_test where-7.13 {
725 cksort {
726 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
727 }
728} {11 12 13 nosort}
729do_test where-7.14 {
730 cksort {
731 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
732 }
733} {10 11 12 nosort}
drh1a844c32002-12-04 22:29:28 +0000734do_test where-7.15 {
735 cksort {
736 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
737 }
738} {nosort}
739do_test where-7.16 {
740 cksort {
741 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
742 }
743} {8 nosort}
744do_test where-7.17 {
745 cksort {
746 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
747 }
748} {nosort}
749do_test where-7.18 {
750 cksort {
751 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
752 }
753} {15 nosort}
754do_test where-7.19 {
755 cksort {
756 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
757 }
758} {nosort}
759do_test where-7.20 {
760 cksort {
761 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
762 }
763} {8 nosort}
764do_test where-7.21 {
765 cksort {
766 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
767 }
768} {nosort}
769do_test where-7.22 {
770 cksort {
771 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
772 }
773} {15 nosort}
774do_test where-7.23 {
775 cksort {
776 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
777 }
778} {nosort}
779do_test where-7.24 {
780 cksort {
781 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
782 }
783} {1 nosort}
784do_test where-7.25 {
785 cksort {
786 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
787 }
788} {nosort}
789do_test where-7.26 {
790 cksort {
791 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
792 }
793} {100 nosort}
794do_test where-7.27 {
795 cksort {
796 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
797 }
798} {nosort}
799do_test where-7.28 {
800 cksort {
801 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
802 }
803} {1 nosort}
804do_test where-7.29 {
805 cksort {
806 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
807 }
808} {nosort}
809do_test where-7.30 {
810 cksort {
811 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
812 }
813} {100 nosort}
drhb6c29892004-11-22 19:12:19 +0000814do_test where-7.31 {
815 cksort {
816 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
817 }
818} {10201 10000 9801 nosort}
819do_test where-7.32 {
820 cksort {
drha21c8492005-09-01 17:47:51 +0000821 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
drhb6c29892004-11-22 19:12:19 +0000822 }
823} {16 9 4 nosort}
824do_test where-7.33 {
825 cksort {
drha21c8492005-09-01 17:47:51 +0000826 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
drhb6c29892004-11-22 19:12:19 +0000827 }
828} {25 16 9 4 nosort}
829do_test where-7.34 {
830 cksort {
831 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
832 }
drhcc192542006-12-20 03:24:19 +0000833} {16 9 nosort}
drhb6c29892004-11-22 19:12:19 +0000834do_test where-7.35 {
835 cksort {
836 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
837 }
838} {16 9 4 nosort}
drh1a844c32002-12-04 22:29:28 +0000839
840do_test where-8.1 {
841 execsql {
842 CREATE TABLE t4 AS SELECT * FROM t1;
843 CREATE INDEX i4xy ON t4(x,y);
844 }
845 cksort {
846 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
847 }
848} {30 29 28 nosort}
849do_test where-8.2 {
850 execsql {
851 DELETE FROM t4;
852 }
853 cksort {
854 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
855 }
856} {nosort}
857
drhd4d595f2003-04-17 12:44:23 +0000858# Make sure searches with an index work with an empty table.
859#
860do_test where-9.1 {
861 execsql {
862 CREATE TABLE t5(x PRIMARY KEY);
863 SELECT * FROM t5 WHERE x<10;
864 }
865} {}
866do_test where-9.2 {
867 execsql {
868 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
869 }
870} {}
871do_test where-9.3 {
872 execsql {
873 SELECT * FROM t5 WHERE x=10;
874 }
875} {}
876
drh37ea94b2003-04-19 16:34:04 +0000877do_test where-10.1 {
878 execsql {
879 SELECT 1 WHERE abs(random())<0
880 }
881} {}
drh7bdc0c12003-04-19 17:27:24 +0000882do_test where-10.2 {
883 proc tclvar_func {vname} {return [set ::$vname]}
884 db function tclvar tclvar_func
885 set ::v1 0
886 execsql {
887 SELECT count(*) FROM t1 WHERE tclvar('v1');
888 }
889} {0}
890do_test where-10.3 {
891 set ::v1 1
892 execsql {
893 SELECT count(*) FROM t1 WHERE tclvar('v1');
894 }
895} {100}
896do_test where-10.4 {
897 set ::v1 1
898 proc tclvar_func {vname} {
899 upvar #0 $vname v
900 set v [expr {!$v}]
901 return $v
902 }
903 execsql {
904 SELECT count(*) FROM t1 WHERE tclvar('v1');
905 }
906} {50}
907
drh9eb20282005-08-24 03:52:18 +0000908# Ticket #1376. The query below was causing a segfault.
909# The problem was the age-old error of calling realloc() on an
910# array while there are still pointers to individual elements of
911# that array.
912#
913do_test where-11.1 {
drh9eb20282005-08-24 03:52:18 +0000914 execsql {
915 CREATE TABLE t99(Dte INT, X INT);
916 DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
917 (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
918 (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
919 (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
920 (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
921 (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
922 (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
923 (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
924 (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
925 (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
926 (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
927 (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
928 (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
929 (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
930 (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
931 (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
932 (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
933 (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
934 (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
935 (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
936 (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
937 (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
938 }
939} {}
940
drhcc192542006-12-20 03:24:19 +0000941# Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
942# KEY.
943#
944do_test where-12.1 {
945 execsql {
946 CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
947 INSERT INTO t6 VALUES(1,'one');
948 INSERT INTO t6 VALUES(4,'four');
949 CREATE INDEX t6i1 ON t6(b);
950 }
951 cksort {
952 SELECT * FROM t6 ORDER BY b;
953 }
954} {4 four 1 one nosort}
955do_test where-12.2 {
956 cksort {
957 SELECT * FROM t6 ORDER BY b, a;
958 }
959} {4 four 1 one nosort}
960do_test where-12.3 {
961 cksort {
962 SELECT * FROM t6 ORDER BY a;
963 }
964} {1 one 4 four nosort}
965do_test where-12.4 {
966 cksort {
967 SELECT * FROM t6 ORDER BY a, b;
968 }
969} {1 one 4 four nosort}
970do_test where-12.5 {
971 cksort {
972 SELECT * FROM t6 ORDER BY b DESC;
973 }
974} {1 one 4 four nosort}
975do_test where-12.6 {
976 cksort {
977 SELECT * FROM t6 ORDER BY b DESC, a DESC;
978 }
979} {1 one 4 four nosort}
980do_test where-12.7 {
981 cksort {
982 SELECT * FROM t6 ORDER BY b DESC, a ASC;
983 }
984} {1 one 4 four sort}
985do_test where-12.8 {
986 cksort {
987 SELECT * FROM t6 ORDER BY b ASC, a DESC;
988 }
989} {4 four 1 one sort}
990do_test where-12.9 {
991 cksort {
992 SELECT * FROM t6 ORDER BY a DESC;
993 }
994} {4 four 1 one nosort}
995do_test where-12.10 {
996 cksort {
997 SELECT * FROM t6 ORDER BY a DESC, b DESC;
998 }
999} {4 four 1 one nosort}
1000do_test where-12.11 {
1001 cksort {
1002 SELECT * FROM t6 ORDER BY a DESC, b ASC;
1003 }
1004} {4 four 1 one nosort}
1005do_test where-12.12 {
1006 cksort {
1007 SELECT * FROM t6 ORDER BY a ASC, b DESC;
1008 }
1009} {1 one 4 four nosort}
1010do_test where-13.1 {
1011 execsql {
1012 CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
1013 INSERT INTO t7 VALUES(1,'one');
1014 INSERT INTO t7 VALUES(4,'four');
1015 CREATE INDEX t7i1 ON t7(b);
1016 }
1017 cksort {
1018 SELECT * FROM t7 ORDER BY b;
1019 }
1020} {4 four 1 one nosort}
1021do_test where-13.2 {
1022 cksort {
1023 SELECT * FROM t7 ORDER BY b, a;
1024 }
1025} {4 four 1 one nosort}
1026do_test where-13.3 {
1027 cksort {
1028 SELECT * FROM t7 ORDER BY a;
1029 }
1030} {1 one 4 four nosort}
1031do_test where-13.4 {
1032 cksort {
1033 SELECT * FROM t7 ORDER BY a, b;
1034 }
1035} {1 one 4 four nosort}
1036do_test where-13.5 {
1037 cksort {
1038 SELECT * FROM t7 ORDER BY b DESC;
1039 }
1040} {1 one 4 four nosort}
1041do_test where-13.6 {
1042 cksort {
1043 SELECT * FROM t7 ORDER BY b DESC, a DESC;
1044 }
1045} {1 one 4 four nosort}
1046do_test where-13.7 {
1047 cksort {
1048 SELECT * FROM t7 ORDER BY b DESC, a ASC;
1049 }
1050} {1 one 4 four sort}
1051do_test where-13.8 {
1052 cksort {
1053 SELECT * FROM t7 ORDER BY b ASC, a DESC;
1054 }
1055} {4 four 1 one sort}
1056do_test where-13.9 {
1057 cksort {
1058 SELECT * FROM t7 ORDER BY a DESC;
1059 }
1060} {4 four 1 one nosort}
1061do_test where-13.10 {
1062 cksort {
1063 SELECT * FROM t7 ORDER BY a DESC, b DESC;
1064 }
1065} {4 four 1 one nosort}
1066do_test where-13.11 {
1067 cksort {
1068 SELECT * FROM t7 ORDER BY a DESC, b ASC;
1069 }
1070} {4 four 1 one nosort}
1071do_test where-13.12 {
1072 cksort {
1073 SELECT * FROM t7 ORDER BY a ASC, b DESC;
1074 }
1075} {1 one 4 four nosort}
1076
drh7b4fc6a2007-02-06 13:26:32 +00001077# Ticket #2211.
1078#
1079# When optimizing out ORDER BY clauses, make sure that trailing terms
1080# of the ORDER BY clause do not reference other tables in a join.
1081#
1082do_test where-14.1 {
1083 execsql {
1084 CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE);
1085 INSERT INTO t8 VALUES(1,'one');
1086 INSERT INTO t8 VALUES(4,'four');
1087 }
1088 cksort {
1089 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
1090 }
1091} {1/4 1/1 4/4 4/1 sort}
1092do_test where-14.2 {
1093 cksort {
1094 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
1095 }
1096} {1/1 1/4 4/1 4/4 sort}
1097do_test where-14.3 {
1098 cksort {
1099 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
1100 }
1101} {1/1 1/4 4/1 4/4 nosort}
1102do_test where-14.4 {
1103 cksort {
1104 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
1105 }
1106} {1/1 1/4 4/1 4/4 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001107do_test where-14.5 {
1108 cksort {
1109 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
1110 }
1111} {4/1 4/4 1/1 1/4 nosort}
1112do_test where-14.6 {
1113 cksort {
1114 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
1115 }
1116} {4/1 4/4 1/1 1/4 nosort}
1117do_test where-14.7 {
1118 cksort {
1119 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
1120 }
1121} {4/1 4/4 1/1 1/4 sort}
drh32ffdb72007-02-06 23:41:34 +00001122do_test where-14.7.1 {
1123 cksort {
1124 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
1125 }
1126} {4/1 4/4 1/1 1/4 sort}
1127do_test where-14.7.2 {
1128 cksort {
1129 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
1130 }
1131} {4/1 4/4 1/1 1/4 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001132do_test where-14.8 {
1133 cksort {
1134 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
1135 }
1136} {4/4 4/1 1/4 1/1 sort}
1137do_test where-14.9 {
1138 cksort {
1139 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
1140 }
1141} {4/4 4/1 1/4 1/1 sort}
1142do_test where-14.10 {
1143 cksort {
1144 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
1145 }
1146} {4/1 4/4 1/1 1/4 sort}
1147do_test where-14.11 {
1148 cksort {
1149 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
1150 }
1151} {4/1 4/4 1/1 1/4 sort}
1152do_test where-14.12 {
1153 cksort {
1154 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
1155 }
1156} {4/4 4/1 1/4 1/1 sort}
drhcc192542006-12-20 03:24:19 +00001157
danielk1977c9cf6e32007-06-25 16:29:33 +00001158# Ticket #2445.
1159#
1160# There was a crash that could occur when a where clause contains an
1161# alias for an expression in the result set, and that expression retrieves
1162# a column of the second or subsequent table in a join.
1163#
1164do_test where-15.1 {
1165 execsql {
1166 CREATE TEMP TABLE t1 (a, b, c, d, e);
1167 CREATE TEMP TABLE t2 (f);
1168 SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
1169 }
1170} {}
drh9eb20282005-08-24 03:52:18 +00001171
danielk19772d605492008-10-01 08:43:03 +00001172# Ticket #3408.
1173#
1174# The branch of code in where.c that generated rowid lookups was
1175# incorrectly deallocating a constant register, meaning that if the
1176# vdbe code ran more than once, the second time around the constant
1177# value may have been clobbered by some other value.
1178#
1179do_test where-16.1 {
1180 execsql {
1181 CREATE TABLE a1(id INTEGER PRIMARY KEY, v);
1182 CREATE TABLE a2(id INTEGER PRIMARY KEY, v);
1183 INSERT INTO a1 VALUES(1, 'one');
1184 INSERT INTO a1 VALUES(2, 'two');
1185 INSERT INTO a2 VALUES(1, 'one');
1186 INSERT INTO a2 VALUES(2, 'two');
1187 }
1188} {}
1189do_test where-16.2 {
1190 execsql {
1191 SELECT * FROM a2 CROSS JOIN a1 WHERE a1.id=1 AND a1.v='one';
1192 }
1193} {1 one 1 one 2 two 1 one}
1194
1195# The actual problem reported in #3408.
1196do_test where-16.3 {
1197 execsql {
1198 CREATE TEMP TABLE foo(idx INTEGER);
1199 INSERT INTO foo VALUES(1);
1200 INSERT INTO foo VALUES(1);
1201 INSERT INTO foo VALUES(1);
1202 INSERT INTO foo VALUES(2);
1203 INSERT INTO foo VALUES(2);
1204 CREATE TEMP TABLE bar(stuff INTEGER);
1205 INSERT INTO bar VALUES(100);
1206 INSERT INTO bar VALUES(200);
1207 INSERT INTO bar VALUES(300);
1208 }
1209} {}
1210do_test where-16.4 {
1211 execsql {
1212 SELECT bar.RowID id FROM foo, bar WHERE foo.idx = bar.RowID AND id = 2;
1213 }
1214} {2 2}
1215
drhed717fe2003-06-15 23:42:24 +00001216integrity_check {where-99.0}
drh08192d52002-04-30 19:20:28 +00001217
danielk19773072c5e2008-11-03 09:06:05 +00001218#---------------------------------------------------------------------
1219# These tests test that a bug surrounding the use of ForceInt has been
1220# fixed in where.c.
1221#
1222do_test where-17.1 {
1223 execsql {
1224 CREATE TABLE tbooking (
1225 id INTEGER PRIMARY KEY,
1226 eventtype INTEGER NOT NULL
1227 );
1228 INSERT INTO tbooking VALUES(42, 3);
1229 INSERT INTO tbooking VALUES(43, 4);
1230 }
1231} {}
1232do_test where-17.2 {
1233 execsql {
1234 SELECT a.id
1235 FROM tbooking AS a
1236 WHERE a.eventtype=3;
1237 }
1238} {42}
1239do_test where-17.3 {
1240 execsql {
1241 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1242 FROM tbooking AS a
1243 WHERE a.eventtype=3;
1244 }
1245} {42 43}
1246do_test where-17.4 {
1247 execsql {
1248 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1249 FROM (SELECT 1.5 AS id) AS a
1250 }
1251} {1.5 42}
1252do_test where-17.5 {
1253 execsql {
1254 CREATE TABLE tother(a, b);
1255 INSERT INTO tother VALUES(1, 3.7);
1256 SELECT id, a FROM tbooking, tother WHERE id>a;
1257 }
1258} {42 1 43 1}
1259
drh6de4f4c2000-06-12 12:20:48 +00001260finish_test