blob: e94047564cfb9eb10478a13695f5496038fdd9f7 [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 {
drhedb04ed2015-09-04 12:54:01 +000045 CREATE INDEX i1w ON t1("w"); -- Verify quoted identifier names
46 CREATE INDEX i1xy ON t1(`x`,'y' ASC); -- Old MySQL compatibility
drh6de4f4c2000-06-12 12:20:48 +000047 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}
drhe0cc3c22015-05-13 17:54:08 +000068do_test where-1.1.1b {
69 count {SELECT x, y, w FROM t1 WHERE w IS 10}
70} {3 121 10 3}
drh6fa978d2013-05-30 19:29:19 +000071do_eqp_test where-1.1.2 {
72 SELECT x, y, w FROM t1 WHERE w=10
drh8a4380d2013-06-11 02:32:50 +000073} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drhe0cc3c22015-05-13 17:54:08 +000074do_eqp_test where-1.1.2b {
75 SELECT x, y, w FROM t1 WHERE w IS 10
76} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drh4f07e5f2007-05-14 11:34:46 +000077do_test where-1.1.3 {
drhd1d38482008-10-07 23:46:38 +000078 db status step
79} {0}
80do_test where-1.1.4 {
81 db eval {SELECT x, y, w FROM t1 WHERE +w=10}
82} {3 121 10}
83do_test where-1.1.5 {
84 db status step
85} {99}
drh6fa978d2013-05-30 19:29:19 +000086do_eqp_test where-1.1.6 {
87 SELECT x, y, w FROM t1 WHERE +w=10
drh8a4380d2013-06-11 02:32:50 +000088} {*SCAN TABLE t1*}
drhd1d38482008-10-07 23:46:38 +000089do_test where-1.1.7 {
drh4f07e5f2007-05-14 11:34:46 +000090 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
91} {3 121 10 3}
drh6fa978d2013-05-30 19:29:19 +000092do_eqp_test where-1.1.8 {
93 SELECT x, y, w AS abc FROM t1 WHERE abc=10
drh8a4380d2013-06-11 02:32:50 +000094} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drhd1d38482008-10-07 23:46:38 +000095do_test where-1.1.9 {
96 db status step
97} {0}
drh4f07e5f2007-05-14 11:34:46 +000098do_test where-1.2.1 {
99 count {SELECT x, y, w FROM t1 WHERE w=11}
100} {3 144 11 3}
101do_test where-1.2.2 {
102 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
103} {3 144 11 3}
104do_test where-1.3.1 {
105 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
106} {3 144 11 3}
107do_test where-1.3.2 {
108 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
109} {3 144 11 3}
drhe0cc3c22015-05-13 17:54:08 +0000110do_test where-1.3.3 {
111 count {SELECT x, y, w AS abc FROM t1 WHERE 11 IS abc}
112} {3 144 11 3}
drh4f07e5f2007-05-14 11:34:46 +0000113do_test where-1.4.1 {
114 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
115} {11 3 144 3}
drhe0cc3c22015-05-13 17:54:08 +0000116do_test where-1.4.1b {
117 count {SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2}
118} {11 3 144 3}
drh6fa978d2013-05-30 19:29:19 +0000119do_eqp_test where-1.4.2 {
120 SELECT w, x, y FROM t1 WHERE 11=w AND x>2
drh8a4380d2013-06-11 02:32:50 +0000121} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drhe0cc3c22015-05-13 17:54:08 +0000122do_eqp_test where-1.4.2b {
123 SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2
124} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drh4f07e5f2007-05-14 11:34:46 +0000125do_test where-1.4.3 {
126 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
127} {11 3 144 3}
drh6fa978d2013-05-30 19:29:19 +0000128do_eqp_test where-1.4.4 {
129 SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2
drh8a4380d2013-06-11 02:32:50 +0000130} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drh6de4f4c2000-06-12 12:20:48 +0000131do_test where-1.5 {
drh487ab3c2001-11-08 00:45:21 +0000132 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
133} {3 144 3}
drh6fa978d2013-05-30 19:29:19 +0000134do_eqp_test where-1.5.2 {
135 SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2
drh8a4380d2013-06-11 02:32:50 +0000136} {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
drh6de4f4c2000-06-12 12:20:48 +0000137do_test where-1.6 {
drh487ab3c2001-11-08 00:45:21 +0000138 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
139} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000140do_test where-1.7 {
drh487ab3c2001-11-08 00:45:21 +0000141 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
142} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000143do_test where-1.8 {
drh487ab3c2001-11-08 00:45:21 +0000144 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
145} {3 144 3}
drh6fa978d2013-05-30 19:29:19 +0000146do_eqp_test where-1.8.2 {
147 SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3
drh8a4380d2013-06-11 02:32:50 +0000148} {*SEARCH TABLE t1 USING INDEX i1xy (x=? AND y=?)*}
drh6fa978d2013-05-30 19:29:19 +0000149do_eqp_test where-1.8.3 {
150 SELECT x, y FROM t1 WHERE y=144 AND x=3
drh8a4380d2013-06-11 02:32:50 +0000151} {*SEARCH TABLE t1 USING COVERING INDEX i1xy (x=? AND y=?)*}
drh6de4f4c2000-06-12 12:20:48 +0000152do_test where-1.9 {
drh487ab3c2001-11-08 00:45:21 +0000153 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
154} {3 144 3}
drh6de4f4c2000-06-12 12:20:48 +0000155do_test where-1.10 {
drh487ab3c2001-11-08 00:45:21 +0000156 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
157} {3 121 3}
drh6de4f4c2000-06-12 12:20:48 +0000158do_test where-1.11 {
drh487ab3c2001-11-08 00:45:21 +0000159 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
160} {3 100 3}
drhe0cc3c22015-05-13 17:54:08 +0000161do_test where-1.11b {
162 count {SELECT x, y FROM t1 WHERE x IS 3 AND y IS 100 AND w<10}
163} {3 100 3}
drh487ab3c2001-11-08 00:45:21 +0000164
165# New for SQLite version 2.1: Verify that that inequality constraints
166# are used correctly.
167#
168do_test where-1.12 {
169 count {SELECT w FROM t1 WHERE x=3 AND y<100}
170} {8 3}
drhe0cc3c22015-05-13 17:54:08 +0000171do_test where-1.12b {
172 count {SELECT w FROM t1 WHERE x IS 3 AND y<100}
173} {8 3}
drh487ab3c2001-11-08 00:45:21 +0000174do_test where-1.13 {
175 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
176} {8 3}
177do_test where-1.14 {
178 count {SELECT w FROM t1 WHERE 3=x AND y<100}
179} {8 3}
drhe0cc3c22015-05-13 17:54:08 +0000180do_test where-1.14b {
181 count {SELECT w FROM t1 WHERE 3 IS x AND y<100}
182} {8 3}
drh487ab3c2001-11-08 00:45:21 +0000183do_test where-1.15 {
184 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
185} {8 3}
186do_test where-1.16 {
187 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
188} {8 9 5}
189do_test where-1.17 {
190 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
191} {8 9 5}
192do_test where-1.18 {
193 count {SELECT w FROM t1 WHERE x=3 AND y>225}
194} {15 3}
drhe0cc3c22015-05-13 17:54:08 +0000195do_test where-1.18b {
196 count {SELECT w FROM t1 WHERE x IS 3 AND y>225}
197} {15 3}
drh487ab3c2001-11-08 00:45:21 +0000198do_test where-1.19 {
199 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
200} {15 3}
201do_test where-1.20 {
202 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
203} {14 15 5}
204do_test where-1.21 {
205 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
206} {14 15 5}
207do_test where-1.22 {
208 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
209} {11 12 5}
drhe0cc3c22015-05-13 17:54:08 +0000210do_test where-1.22b {
211 count {SELECT w FROM t1 WHERE x IS 3 AND y>121 AND y<196}
212} {11 12 5}
drh487ab3c2001-11-08 00:45:21 +0000213do_test where-1.23 {
214 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
215} {10 11 12 13 9}
216do_test where-1.24 {
217 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
218} {11 12 5}
219do_test where-1.25 {
220 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
221} {10 11 12 13 9}
222
223# Need to work on optimizing the BETWEEN operator.
224#
225# do_test where-1.26 {
226# count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
227# } {10 11 12 13 9}
228
229do_test where-1.27 {
230 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
danielk197721de2e72007-11-29 17:43:27 +0000231} {10 10}
drh7cf6e4d2004-05-19 14:56:55 +0000232
drh487ab3c2001-11-08 00:45:21 +0000233do_test where-1.28 {
234 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
235} {10 99}
236do_test where-1.29 {
237 count {SELECT w FROM t1 WHERE y==121}
238} {10 99}
239
240
241do_test where-1.30 {
242 count {SELECT w FROM t1 WHERE w>97}
drh9012bcb2004-12-19 00:11:35 +0000243} {98 99 100 3}
drh487ab3c2001-11-08 00:45:21 +0000244do_test where-1.31 {
245 count {SELECT w FROM t1 WHERE w>=97}
drh9012bcb2004-12-19 00:11:35 +0000246} {97 98 99 100 4}
drh487ab3c2001-11-08 00:45:21 +0000247do_test where-1.33 {
248 count {SELECT w FROM t1 WHERE w==97}
drh9012bcb2004-12-19 00:11:35 +0000249} {97 2}
drhac931eb2005-01-11 18:13:56 +0000250do_test where-1.33.1 {
251 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
252} {97 2}
253do_test where-1.33.2 {
254 count {SELECT w FROM t1 WHERE w<98 AND w==97}
255} {97 2}
256do_test where-1.33.3 {
257 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
258} {97 2}
259do_test where-1.33.4 {
260 count {SELECT w FROM t1 WHERE w>96 AND w==97}
261} {97 2}
262do_test where-1.33.5 {
263 count {SELECT w FROM t1 WHERE w==97 AND w==97}
264} {97 2}
drh487ab3c2001-11-08 00:45:21 +0000265do_test where-1.34 {
266 count {SELECT w FROM t1 WHERE w+1==98}
267} {97 99}
268do_test where-1.35 {
269 count {SELECT w FROM t1 WHERE w<3}
drhcfc6ca42014-02-14 23:49:13 +0000270} {1 2 3}
drh487ab3c2001-11-08 00:45:21 +0000271do_test where-1.36 {
272 count {SELECT w FROM t1 WHERE w<=3}
drhcfc6ca42014-02-14 23:49:13 +0000273} {1 2 3 4}
drh487ab3c2001-11-08 00:45:21 +0000274do_test where-1.37 {
drh8aff1012001-12-22 14:49:24 +0000275 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
drh9012bcb2004-12-19 00:11:35 +0000276} {1 2 3 99}
drh487ab3c2001-11-08 00:45:21 +0000277
drh6977fea2002-10-22 23:38:04 +0000278do_test where-1.38 {
279 count {SELECT (w) FROM t1 WHERE (w)>(97)}
drh9012bcb2004-12-19 00:11:35 +0000280} {98 99 100 3}
drh6977fea2002-10-22 23:38:04 +0000281do_test where-1.39 {
282 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
drh9012bcb2004-12-19 00:11:35 +0000283} {97 98 99 100 4}
drh6977fea2002-10-22 23:38:04 +0000284do_test where-1.40 {
285 count {SELECT (w) FROM t1 WHERE (w)==(97)}
drh9012bcb2004-12-19 00:11:35 +0000286} {97 2}
drh6977fea2002-10-22 23:38:04 +0000287do_test where-1.41 {
288 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
289} {97 99}
290
drh6de4f4c2000-06-12 12:20:48 +0000291
292# Do the same kind of thing except use a join as the data source.
293#
294do_test where-2.1 {
drh487ab3c2001-11-08 00:45:21 +0000295 count {
296 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000297 WHERE x=q AND y=s AND r=8977
298 }
drh487ab3c2001-11-08 00:45:21 +0000299} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000300do_test where-2.2 {
drh487ab3c2001-11-08 00:45:21 +0000301 count {
302 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000303 WHERE x=q AND s=y AND r=8977
304 }
drh487ab3c2001-11-08 00:45:21 +0000305} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000306do_test where-2.3 {
drh487ab3c2001-11-08 00:45:21 +0000307 count {
308 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000309 WHERE x=q AND s=y AND r=8977 AND w>10
310 }
drh487ab3c2001-11-08 00:45:21 +0000311} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000312do_test where-2.4 {
drh487ab3c2001-11-08 00:45:21 +0000313 count {
314 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000315 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
316 }
drh487ab3c2001-11-08 00:45:21 +0000317} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000318do_test where-2.5 {
drh487ab3c2001-11-08 00:45:21 +0000319 count {
320 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000321 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
322 }
drh487ab3c2001-11-08 00:45:21 +0000323} {34 67 6}
drh6de4f4c2000-06-12 12:20:48 +0000324do_test where-2.6 {
drh487ab3c2001-11-08 00:45:21 +0000325 count {
326 SELECT w, p FROM t2, t1
drh6de4f4c2000-06-12 12:20:48 +0000327 WHERE x=q AND p=77 AND s=y AND w>5
328 }
drh487ab3c2001-11-08 00:45:21 +0000329} {24 77 6}
drh6de4f4c2000-06-12 12:20:48 +0000330do_test where-2.7 {
drh487ab3c2001-11-08 00:45:21 +0000331 count {
332 SELECT w, p FROM t1, t2
drh6de4f4c2000-06-12 12:20:48 +0000333 WHERE x=q AND p>77 AND s=y AND w=5
334 }
drh487ab3c2001-11-08 00:45:21 +0000335} {5 96 6}
drh6de4f4c2000-06-12 12:20:48 +0000336
337# Lets do a 3-way join.
338#
339do_test where-3.1 {
drh487ab3c2001-11-08 00:45:21 +0000340 count {
341 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000342 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
343 }
drh9012bcb2004-12-19 00:11:35 +0000344} {11 90 11 8}
drh6de4f4c2000-06-12 12:20:48 +0000345do_test where-3.2 {
drh487ab3c2001-11-08 00:45:21 +0000346 count {
347 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000348 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
349 }
drh9012bcb2004-12-19 00:11:35 +0000350} {12 89 12 8}
drh6de4f4c2000-06-12 12:20:48 +0000351do_test where-3.3 {
drh487ab3c2001-11-08 00:45:21 +0000352 count {
353 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
drh6de4f4c2000-06-12 12:20:48 +0000354 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
355 }
drh9012bcb2004-12-19 00:11:35 +0000356} {15 86 86 8}
drh6de4f4c2000-06-12 12:20:48 +0000357
drh08192d52002-04-30 19:20:28 +0000358# Test to see that the special case of a constant WHERE clause is
359# handled.
360#
361do_test where-4.1 {
362 count {
363 SELECT * FROM t1 WHERE 0
364 }
365} {0}
366do_test where-4.2 {
367 count {
368 SELECT * FROM t1 WHERE 1 LIMIT 1
369 }
drhec7429a2005-10-06 16:53:14 +0000370} {1 0 4 0}
drh08192d52002-04-30 19:20:28 +0000371do_test where-4.3 {
372 execsql {
373 SELECT 99 WHERE 0
374 }
375} {}
376do_test where-4.4 {
377 execsql {
378 SELECT 99 WHERE 1
379 }
380} {99}
drhba0232a2005-06-06 17:27:19 +0000381do_test where-4.5 {
382 execsql {
383 SELECT 99 WHERE 0.1
384 }
385} {99}
386do_test where-4.6 {
387 execsql {
388 SELECT 99 WHERE 0.0
389 }
390} {}
drh85e9e222008-07-15 00:27:34 +0000391do_test where-4.7 {
392 execsql {
393 SELECT count(*) FROM t1 WHERE t1.w
394 }
395} {100}
drh08192d52002-04-30 19:20:28 +0000396
drh48185c12002-06-09 01:55:20 +0000397# Verify that IN operators in a WHERE clause are handled correctly.
danielk19773e8c37e2005-01-21 03:12:14 +0000398# Omit these tests if the build is not capable of sub-queries.
drh48185c12002-06-09 01:55:20 +0000399#
danielk19773e8c37e2005-01-21 03:12:14 +0000400ifcapable subquery {
401 do_test where-5.1 {
402 count {
403 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
404 }
drh4db38a72005-09-01 12:16:28 +0000405 } {1 0 4 2 1 9 3 1 16 4}
danielk19773e8c37e2005-01-21 03:12:14 +0000406 do_test where-5.2 {
407 count {
408 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
409 }
danielk197721de2e72007-11-29 17:43:27 +0000410 } {1 0 4 2 1 9 3 1 16 102}
drh1b8fc652013-02-07 21:15:14 +0000411 do_test where-5.3a {
danielk19773e8c37e2005-01-21 03:12:14 +0000412 count {
413 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
414 }
drh1b8fc652013-02-07 21:15:14 +0000415 } {1 0 4 2 1 9 3 1 16 13}
416 do_test where-5.3b {
417 count {
418 SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
419 }
420 } {1 0 4 2 1 9 3 1 16 13}
421 do_test where-5.3c {
422 count {
423 SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
424 }
425 } {1 0 4 2 1 9 3 1 16 13}
426 do_test where-5.3d {
427 count {
428 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
429 }
drh2d96b932013-02-08 18:48:23 +0000430 } {3 1 16 2 1 9 1 0 4 12}
danielk19773e8c37e2005-01-21 03:12:14 +0000431 do_test where-5.4 {
432 count {
433 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
434 }
danielk197721de2e72007-11-29 17:43:27 +0000435 } {1 0 4 2 1 9 3 1 16 102}
danielk19773e8c37e2005-01-21 03:12:14 +0000436 do_test where-5.5 {
437 count {
438 SELECT * FROM t1 WHERE rowid IN
439 (select rowid from t1 where rowid IN (-1,2,4))
440 ORDER BY 1;
441 }
442 } {2 1 9 4 2 25 3}
443 do_test where-5.6 {
444 count {
445 SELECT * FROM t1 WHERE rowid+0 IN
446 (select rowid from t1 where rowid IN (-1,2,4))
447 ORDER BY 1;
448 }
danielk197721de2e72007-11-29 17:43:27 +0000449 } {2 1 9 4 2 25 103}
danielk19773e8c37e2005-01-21 03:12:14 +0000450 do_test where-5.7 {
451 count {
452 SELECT * FROM t1 WHERE w IN
453 (select rowid from t1 where rowid IN (-1,2,4))
454 ORDER BY 1;
455 }
456 } {2 1 9 4 2 25 9}
457 do_test where-5.8 {
458 count {
459 SELECT * FROM t1 WHERE w+0 IN
460 (select rowid from t1 where rowid IN (-1,2,4))
461 ORDER BY 1;
462 }
danielk197721de2e72007-11-29 17:43:27 +0000463 } {2 1 9 4 2 25 103}
danielk19773e8c37e2005-01-21 03:12:14 +0000464 do_test where-5.9 {
465 count {
466 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
467 }
468 } {2 1 9 3 1 16 7}
469 do_test where-5.10 {
470 count {
471 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
472 }
473 } {2 1 9 3 1 16 199}
474 do_test where-5.11 {
475 count {
476 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
477 }
478 } {79 6 6400 89 6 8100 199}
479 do_test where-5.12 {
480 count {
481 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
482 }
drh3adc9ce2005-07-28 16:51:51 +0000483 } {79 6 6400 89 6 8100 7}
danielk19773e8c37e2005-01-21 03:12:14 +0000484 do_test where-5.13 {
485 count {
486 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
487 }
488 } {2 1 9 3 1 16 7}
489 do_test where-5.14 {
490 count {
491 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
492 }
drh4db38a72005-09-01 12:16:28 +0000493 } {2 1 9 8}
drh3adc9ce2005-07-28 16:51:51 +0000494 do_test where-5.15 {
495 count {
496 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
497 }
498 } {2 1 9 3 1 16 11}
drh2d96b932013-02-08 18:48:23 +0000499 do_test where-5.100 {
500 db eval {
501 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
502 ORDER BY x, y
503 }
504 } {2 1 9 54 5 3025 62 5 3969}
505 do_test where-5.101 {
506 db eval {
507 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
508 ORDER BY x DESC, y DESC
509 }
510 } {62 5 3969 54 5 3025 2 1 9}
511 do_test where-5.102 {
512 db eval {
513 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
514 ORDER BY x DESC, y
515 }
516 } {54 5 3025 62 5 3969 2 1 9}
517 do_test where-5.103 {
518 db eval {
519 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
520 ORDER BY x, y DESC
521 }
522 } {2 1 9 62 5 3969 54 5 3025}
danielk19773e8c37e2005-01-21 03:12:14 +0000523}
drh48185c12002-06-09 01:55:20 +0000524
drh6bf89572004-11-03 16:27:01 +0000525# This procedure executes the SQL. Then it checks to see if the OP_Sort
526# opcode was executed. If an OP_Sort did occur, then "sort" is appended
527# to the result. If no OP_Sort happened, then "nosort" is appended.
528#
529# This procedure is used to check to make sure sorting is or is not
530# occurring as expected.
drhe3184742002-06-19 14:27:05 +0000531#
532proc cksort {sql} {
533 set data [execsql $sql]
drhd1d38482008-10-07 23:46:38 +0000534 if {[db status sort]} {set x sort} {set x nosort}
drhe3184742002-06-19 14:27:05 +0000535 lappend data $x
536 return $data
537}
538# Check out the logic that attempts to implement the ORDER BY clause
539# using an index rather than by sorting.
540#
541do_test where-6.1 {
542 execsql {
543 CREATE TABLE t3(a,b,c);
544 CREATE INDEX t3a ON t3(a);
545 CREATE INDEX t3bc ON t3(b,c);
546 CREATE INDEX t3acb ON t3(a,c,b);
547 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
548 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
549 }
drh3d1d95e2005-09-08 10:37:01 +0000550} {100 5050 5050 348550}
drhe3184742002-06-19 14:27:05 +0000551do_test where-6.2 {
552 cksort {
553 SELECT * FROM t3 ORDER BY a LIMIT 3
554 }
555} {1 100 4 2 99 9 3 98 16 nosort}
556do_test where-6.3 {
557 cksort {
558 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
559 }
560} {1 100 4 2 99 9 3 98 16 sort}
561do_test where-6.4 {
562 cksort {
563 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
564 }
565} {1 100 4 2 99 9 3 98 16 nosort}
566do_test where-6.5 {
567 cksort {
568 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
569 }
570} {1 100 4 2 99 9 3 98 16 nosort}
571do_test where-6.6 {
572 cksort {
573 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
574 }
575} {1 100 4 2 99 9 3 98 16 nosort}
576do_test where-6.7 {
577 cksort {
578 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
579 }
drh3adc9ce2005-07-28 16:51:51 +0000580} {1 100 4 2 99 9 3 98 16 nosort}
danielk19773e8c37e2005-01-21 03:12:14 +0000581ifcapable subquery {
drh1b8fc652013-02-07 21:15:14 +0000582 do_test where-6.8a {
danielk19773e8c37e2005-01-21 03:12:14 +0000583 cksort {
584 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
585 }
drh1b8fc652013-02-07 21:15:14 +0000586 } {1 100 4 2 99 9 3 98 16 nosort}
587 do_test where-6.8b {
588 cksort {
589 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a DESC LIMIT 3
590 }
drh2d96b932013-02-08 18:48:23 +0000591 } {9 92 100 7 94 64 5 96 36 nosort}
danielk19773e8c37e2005-01-21 03:12:14 +0000592}
drhdd4852c2002-12-04 21:50:16 +0000593do_test where-6.9.1 {
drhe3184742002-06-19 14:27:05 +0000594 cksort {
595 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
596 }
597} {1 100 4 nosort}
drhac931eb2005-01-11 18:13:56 +0000598do_test where-6.9.1.1 {
599 cksort {
600 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
601 }
602} {1 100 4 nosort}
603do_test where-6.9.1.2 {
604 cksort {
605 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
606 }
607} {1 100 4 nosort}
drhdd4852c2002-12-04 21:50:16 +0000608do_test where-6.9.2 {
609 cksort {
610 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
611 }
612} {1 100 4 nosort}
613do_test where-6.9.3 {
614 cksort {
615 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
616 }
617} {1 100 4 nosort}
618do_test where-6.9.4 {
619 cksort {
620 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
621 }
622} {1 100 4 nosort}
623do_test where-6.9.5 {
624 cksort {
625 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
626 }
627} {1 100 4 nosort}
628do_test where-6.9.6 {
629 cksort {
630 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
631 }
632} {1 100 4 nosort}
633do_test where-6.9.7 {
634 cksort {
635 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
636 }
drh4fe425a2013-06-12 17:08:06 +0000637} {1 100 4 nosort}
drhdd4852c2002-12-04 21:50:16 +0000638do_test where-6.9.8 {
639 cksort {
640 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
641 }
drh9012bcb2004-12-19 00:11:35 +0000642} {1 100 4 nosort}
drhdd4852c2002-12-04 21:50:16 +0000643do_test where-6.9.9 {
644 cksort {
645 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
646 }
drh9012bcb2004-12-19 00:11:35 +0000647} {1 100 4 nosort}
drhe3184742002-06-19 14:27:05 +0000648do_test where-6.10 {
649 cksort {
650 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
651 }
652} {1 100 4 nosort}
653do_test where-6.11 {
654 cksort {
655 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
656 }
657} {1 100 4 nosort}
658do_test where-6.12 {
659 cksort {
660 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
661 }
662} {1 100 4 nosort}
663do_test where-6.13 {
664 cksort {
665 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
666 }
drhdd4852c2002-12-04 21:50:16 +0000667} {100 1 10201 99 2 10000 98 3 9801 nosort}
668do_test where-6.13.1 {
669 cksort {
670 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
671 }
drhe3184742002-06-19 14:27:05 +0000672} {100 1 10201 99 2 10000 98 3 9801 sort}
673do_test where-6.14 {
674 cksort {
675 SELECT * FROM t3 ORDER BY b LIMIT 3
676 }
677} {100 1 10201 99 2 10000 98 3 9801 nosort}
678do_test where-6.15 {
679 cksort {
680 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
681 }
682} {1 0 2 1 3 1 nosort}
683do_test where-6.16 {
684 cksort {
685 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
686 }
687} {1 0 2 1 3 1 sort}
drhc330af12002-08-14 03:03:57 +0000688do_test where-6.19 {
689 cksort {
690 SELECT y FROM t1 ORDER BY w LIMIT 3;
691 }
692} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000693do_test where-6.20 {
694 cksort {
695 SELECT y FROM t1 ORDER BY rowid LIMIT 3;
696 }
697} {4 9 16 nosort}
698do_test where-6.21 {
699 cksort {
700 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
701 }
drhcc192542006-12-20 03:24:19 +0000702} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000703do_test where-6.22 {
704 cksort {
705 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
706 }
drhcc192542006-12-20 03:24:19 +0000707} {4 9 16 nosort}
drhb6c29892004-11-22 19:12:19 +0000708do_test where-6.23 {
709 cksort {
710 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
711 }
drhcc192542006-12-20 03:24:19 +0000712} {9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000713do_test where-6.24 {
714 cksort {
715 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
716 }
drhcc192542006-12-20 03:24:19 +0000717} {9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000718do_test where-6.25 {
719 cksort {
720 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
721 }
722} {9 16 nosort}
723do_test where-6.26 {
724 cksort {
725 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
726 }
727} {4 9 16 25 nosort}
728do_test where-6.27 {
729 cksort {
730 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
731 }
drhcc192542006-12-20 03:24:19 +0000732} {4 9 16 25 nosort}
drhb6c29892004-11-22 19:12:19 +0000733
drhc330af12002-08-14 03:03:57 +0000734
drhdd4852c2002-12-04 21:50:16 +0000735# Tests for reverse-order sorting.
736#
737do_test where-7.1 {
738 cksort {
739 SELECT w FROM t1 WHERE x=3 ORDER BY y;
740 }
741} {8 9 10 11 12 13 14 15 nosort}
742do_test where-7.2 {
743 cksort {
744 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
745 }
746} {15 14 13 12 11 10 9 8 nosort}
747do_test where-7.3 {
748 cksort {
749 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
750 }
751} {10 11 12 nosort}
752do_test where-7.4 {
753 cksort {
754 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
755 }
756} {15 14 13 nosort}
757do_test where-7.5 {
758 cksort {
759 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
760 }
761} {15 14 13 12 11 nosort}
762do_test where-7.6 {
763 cksort {
764 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
765 }
766} {15 14 13 12 11 10 nosort}
767do_test where-7.7 {
768 cksort {
769 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
770 }
771} {12 11 10 nosort}
772do_test where-7.8 {
773 cksort {
774 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
775 }
776} {13 12 11 10 nosort}
777do_test where-7.9 {
778 cksort {
779 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
780 }
781} {13 12 11 nosort}
782do_test where-7.10 {
783 cksort {
784 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
785 }
786} {12 11 10 nosort}
787do_test where-7.11 {
788 cksort {
789 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
790 }
791} {10 11 12 nosort}
792do_test where-7.12 {
793 cksort {
794 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
795 }
796} {10 11 12 13 nosort}
797do_test where-7.13 {
798 cksort {
799 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
800 }
801} {11 12 13 nosort}
802do_test where-7.14 {
803 cksort {
804 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
805 }
806} {10 11 12 nosort}
drh1a844c32002-12-04 22:29:28 +0000807do_test where-7.15 {
808 cksort {
809 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
810 }
811} {nosort}
812do_test where-7.16 {
813 cksort {
814 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
815 }
816} {8 nosort}
817do_test where-7.17 {
818 cksort {
819 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
820 }
821} {nosort}
822do_test where-7.18 {
823 cksort {
824 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
825 }
826} {15 nosort}
827do_test where-7.19 {
828 cksort {
829 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
830 }
831} {nosort}
832do_test where-7.20 {
833 cksort {
834 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
835 }
836} {8 nosort}
837do_test where-7.21 {
838 cksort {
839 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
840 }
841} {nosort}
842do_test where-7.22 {
843 cksort {
844 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
845 }
846} {15 nosort}
847do_test where-7.23 {
848 cksort {
849 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
850 }
851} {nosort}
852do_test where-7.24 {
853 cksort {
854 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
855 }
856} {1 nosort}
857do_test where-7.25 {
858 cksort {
859 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
860 }
861} {nosort}
862do_test where-7.26 {
863 cksort {
864 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
865 }
866} {100 nosort}
867do_test where-7.27 {
868 cksort {
869 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
870 }
871} {nosort}
872do_test where-7.28 {
873 cksort {
874 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
875 }
876} {1 nosort}
877do_test where-7.29 {
878 cksort {
879 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
880 }
881} {nosort}
882do_test where-7.30 {
883 cksort {
884 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
885 }
886} {100 nosort}
drhb6c29892004-11-22 19:12:19 +0000887do_test where-7.31 {
888 cksort {
889 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
890 }
891} {10201 10000 9801 nosort}
892do_test where-7.32 {
893 cksort {
drha21c8492005-09-01 17:47:51 +0000894 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
drhb6c29892004-11-22 19:12:19 +0000895 }
896} {16 9 4 nosort}
897do_test where-7.33 {
898 cksort {
drha21c8492005-09-01 17:47:51 +0000899 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
drhb6c29892004-11-22 19:12:19 +0000900 }
901} {25 16 9 4 nosort}
902do_test where-7.34 {
903 cksort {
904 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
905 }
drhcc192542006-12-20 03:24:19 +0000906} {16 9 nosort}
drhb6c29892004-11-22 19:12:19 +0000907do_test where-7.35 {
908 cksort {
909 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
910 }
911} {16 9 4 nosort}
drh1a844c32002-12-04 22:29:28 +0000912
913do_test where-8.1 {
914 execsql {
915 CREATE TABLE t4 AS SELECT * FROM t1;
916 CREATE INDEX i4xy ON t4(x,y);
917 }
918 cksort {
919 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
920 }
921} {30 29 28 nosort}
922do_test where-8.2 {
923 execsql {
924 DELETE FROM t4;
925 }
926 cksort {
927 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
928 }
929} {nosort}
930
drhd4d595f2003-04-17 12:44:23 +0000931# Make sure searches with an index work with an empty table.
932#
933do_test where-9.1 {
934 execsql {
935 CREATE TABLE t5(x PRIMARY KEY);
936 SELECT * FROM t5 WHERE x<10;
937 }
938} {}
939do_test where-9.2 {
940 execsql {
941 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
942 }
943} {}
944do_test where-9.3 {
945 execsql {
946 SELECT * FROM t5 WHERE x=10;
947 }
948} {}
949
drh37ea94b2003-04-19 16:34:04 +0000950do_test where-10.1 {
951 execsql {
952 SELECT 1 WHERE abs(random())<0
953 }
954} {}
drh7bdc0c12003-04-19 17:27:24 +0000955do_test where-10.2 {
956 proc tclvar_func {vname} {return [set ::$vname]}
957 db function tclvar tclvar_func
958 set ::v1 0
959 execsql {
960 SELECT count(*) FROM t1 WHERE tclvar('v1');
961 }
962} {0}
963do_test where-10.3 {
964 set ::v1 1
965 execsql {
966 SELECT count(*) FROM t1 WHERE tclvar('v1');
967 }
968} {100}
969do_test where-10.4 {
970 set ::v1 1
971 proc tclvar_func {vname} {
972 upvar #0 $vname v
973 set v [expr {!$v}]
974 return $v
975 }
976 execsql {
977 SELECT count(*) FROM t1 WHERE tclvar('v1');
978 }
979} {50}
980
drh9eb20282005-08-24 03:52:18 +0000981# Ticket #1376. The query below was causing a segfault.
982# The problem was the age-old error of calling realloc() on an
983# array while there are still pointers to individual elements of
984# that array.
985#
986do_test where-11.1 {
drh9eb20282005-08-24 03:52:18 +0000987 execsql {
988 CREATE TABLE t99(Dte INT, X INT);
989 DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
990 (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
991 (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
992 (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
993 (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
994 (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
995 (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
996 (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
997 (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
998 (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
999 (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
1000 (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
1001 (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
1002 (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
1003 (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
1004 (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
1005 (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
1006 (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
1007 (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
1008 (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
1009 (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
1010 (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
1011 }
1012} {}
1013
drhcc192542006-12-20 03:24:19 +00001014# Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
1015# KEY.
1016#
1017do_test where-12.1 {
1018 execsql {
1019 CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
1020 INSERT INTO t6 VALUES(1,'one');
1021 INSERT INTO t6 VALUES(4,'four');
1022 CREATE INDEX t6i1 ON t6(b);
1023 }
1024 cksort {
1025 SELECT * FROM t6 ORDER BY b;
1026 }
1027} {4 four 1 one nosort}
1028do_test where-12.2 {
1029 cksort {
1030 SELECT * FROM t6 ORDER BY b, a;
1031 }
1032} {4 four 1 one nosort}
1033do_test where-12.3 {
1034 cksort {
1035 SELECT * FROM t6 ORDER BY a;
1036 }
1037} {1 one 4 four nosort}
1038do_test where-12.4 {
1039 cksort {
1040 SELECT * FROM t6 ORDER BY a, b;
1041 }
1042} {1 one 4 four nosort}
1043do_test where-12.5 {
1044 cksort {
1045 SELECT * FROM t6 ORDER BY b DESC;
1046 }
1047} {1 one 4 four nosort}
1048do_test where-12.6 {
1049 cksort {
1050 SELECT * FROM t6 ORDER BY b DESC, a DESC;
1051 }
1052} {1 one 4 four nosort}
1053do_test where-12.7 {
1054 cksort {
1055 SELECT * FROM t6 ORDER BY b DESC, a ASC;
1056 }
1057} {1 one 4 four sort}
1058do_test where-12.8 {
1059 cksort {
1060 SELECT * FROM t6 ORDER BY b ASC, a DESC;
1061 }
1062} {4 four 1 one sort}
1063do_test where-12.9 {
1064 cksort {
1065 SELECT * FROM t6 ORDER BY a DESC;
1066 }
1067} {4 four 1 one nosort}
1068do_test where-12.10 {
1069 cksort {
1070 SELECT * FROM t6 ORDER BY a DESC, b DESC;
1071 }
1072} {4 four 1 one nosort}
1073do_test where-12.11 {
1074 cksort {
1075 SELECT * FROM t6 ORDER BY a DESC, b ASC;
1076 }
1077} {4 four 1 one nosort}
1078do_test where-12.12 {
1079 cksort {
1080 SELECT * FROM t6 ORDER BY a ASC, b DESC;
1081 }
1082} {1 one 4 four nosort}
1083do_test where-13.1 {
1084 execsql {
1085 CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
1086 INSERT INTO t7 VALUES(1,'one');
1087 INSERT INTO t7 VALUES(4,'four');
1088 CREATE INDEX t7i1 ON t7(b);
1089 }
1090 cksort {
1091 SELECT * FROM t7 ORDER BY b;
1092 }
1093} {4 four 1 one nosort}
1094do_test where-13.2 {
1095 cksort {
1096 SELECT * FROM t7 ORDER BY b, a;
1097 }
1098} {4 four 1 one nosort}
1099do_test where-13.3 {
1100 cksort {
1101 SELECT * FROM t7 ORDER BY a;
1102 }
1103} {1 one 4 four nosort}
1104do_test where-13.4 {
1105 cksort {
1106 SELECT * FROM t7 ORDER BY a, b;
1107 }
1108} {1 one 4 four nosort}
1109do_test where-13.5 {
1110 cksort {
1111 SELECT * FROM t7 ORDER BY b DESC;
1112 }
1113} {1 one 4 four nosort}
1114do_test where-13.6 {
1115 cksort {
1116 SELECT * FROM t7 ORDER BY b DESC, a DESC;
1117 }
1118} {1 one 4 four nosort}
1119do_test where-13.7 {
1120 cksort {
1121 SELECT * FROM t7 ORDER BY b DESC, a ASC;
1122 }
1123} {1 one 4 four sort}
1124do_test where-13.8 {
1125 cksort {
1126 SELECT * FROM t7 ORDER BY b ASC, a DESC;
1127 }
1128} {4 four 1 one sort}
1129do_test where-13.9 {
1130 cksort {
1131 SELECT * FROM t7 ORDER BY a DESC;
1132 }
1133} {4 four 1 one nosort}
1134do_test where-13.10 {
1135 cksort {
1136 SELECT * FROM t7 ORDER BY a DESC, b DESC;
1137 }
1138} {4 four 1 one nosort}
1139do_test where-13.11 {
1140 cksort {
1141 SELECT * FROM t7 ORDER BY a DESC, b ASC;
1142 }
1143} {4 four 1 one nosort}
1144do_test where-13.12 {
1145 cksort {
1146 SELECT * FROM t7 ORDER BY a ASC, b DESC;
1147 }
1148} {1 one 4 four nosort}
1149
drh7b4fc6a2007-02-06 13:26:32 +00001150# Ticket #2211.
1151#
1152# When optimizing out ORDER BY clauses, make sure that trailing terms
1153# of the ORDER BY clause do not reference other tables in a join.
1154#
dane8258312012-12-05 19:04:32 +00001155if {[permutation] != "no_optimization"} {
drh7b4fc6a2007-02-06 13:26:32 +00001156do_test where-14.1 {
1157 execsql {
drh165674d2013-10-04 15:58:59 +00001158 CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE, c CHAR(100));
1159 INSERT INTO t8(a,b) VALUES(1,'one');
1160 INSERT INTO t8(a,b) VALUES(4,'four');
drh7b4fc6a2007-02-06 13:26:32 +00001161 }
1162 cksort {
1163 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
1164 }
drh6f0e4002012-10-03 12:38:19 +00001165} {1/4 1/1 4/4 4/1 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001166do_test where-14.2 {
1167 cksort {
1168 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
1169 }
drh6f0e4002012-10-03 12:38:19 +00001170} {1/1 1/4 4/1 4/4 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001171do_test where-14.3 {
1172 cksort {
1173 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
1174 }
drh3f4d1d12012-09-15 18:45:54 +00001175} {1/4 1/1 4/4 4/1 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001176do_test where-14.4 {
1177 cksort {
1178 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
1179 }
drh3f4d1d12012-09-15 18:45:54 +00001180} {1/4 1/1 4/4 4/1 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001181do_test where-14.5 {
dan99f8fb62012-04-20 15:24:53 +00001182 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
drh7b4fc6a2007-02-06 13:26:32 +00001183 cksort {
1184 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
1185 }
drh5343b2d2012-09-27 19:53:38 +00001186} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
drh7b4fc6a2007-02-06 13:26:32 +00001187do_test where-14.6 {
dan99f8fb62012-04-20 15:24:53 +00001188 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
drh7b4fc6a2007-02-06 13:26:32 +00001189 cksort {
1190 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
1191 }
drh5343b2d2012-09-27 19:53:38 +00001192} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
drh7b4fc6a2007-02-06 13:26:32 +00001193do_test where-14.7 {
1194 cksort {
1195 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
1196 }
1197} {4/1 4/4 1/1 1/4 sort}
drh32ffdb72007-02-06 23:41:34 +00001198do_test where-14.7.1 {
1199 cksort {
1200 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
1201 }
1202} {4/1 4/4 1/1 1/4 sort}
1203do_test where-14.7.2 {
1204 cksort {
1205 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
1206 }
drh3f4d1d12012-09-15 18:45:54 +00001207} {4/4 4/1 1/4 1/1 nosort}
drh7b4fc6a2007-02-06 13:26:32 +00001208do_test where-14.8 {
1209 cksort {
1210 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
1211 }
1212} {4/4 4/1 1/4 1/1 sort}
1213do_test where-14.9 {
1214 cksort {
1215 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
1216 }
1217} {4/4 4/1 1/4 1/1 sort}
1218do_test where-14.10 {
1219 cksort {
1220 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
1221 }
1222} {4/1 4/4 1/1 1/4 sort}
1223do_test where-14.11 {
1224 cksort {
1225 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
1226 }
1227} {4/1 4/4 1/1 1/4 sort}
1228do_test where-14.12 {
1229 cksort {
1230 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
1231 }
1232} {4/4 4/1 1/4 1/1 sort}
dane8258312012-12-05 19:04:32 +00001233} ;# {permutation != "no_optimization"}
drhcc192542006-12-20 03:24:19 +00001234
danielk1977c9cf6e32007-06-25 16:29:33 +00001235# Ticket #2445.
1236#
1237# There was a crash that could occur when a where clause contains an
1238# alias for an expression in the result set, and that expression retrieves
1239# a column of the second or subsequent table in a join.
1240#
1241do_test where-15.1 {
1242 execsql {
1243 CREATE TEMP TABLE t1 (a, b, c, d, e);
1244 CREATE TEMP TABLE t2 (f);
1245 SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
1246 }
1247} {}
drh9eb20282005-08-24 03:52:18 +00001248
danielk19772d605492008-10-01 08:43:03 +00001249# Ticket #3408.
1250#
1251# The branch of code in where.c that generated rowid lookups was
1252# incorrectly deallocating a constant register, meaning that if the
1253# vdbe code ran more than once, the second time around the constant
1254# value may have been clobbered by some other value.
1255#
1256do_test where-16.1 {
1257 execsql {
1258 CREATE TABLE a1(id INTEGER PRIMARY KEY, v);
1259 CREATE TABLE a2(id INTEGER PRIMARY KEY, v);
1260 INSERT INTO a1 VALUES(1, 'one');
1261 INSERT INTO a1 VALUES(2, 'two');
1262 INSERT INTO a2 VALUES(1, 'one');
1263 INSERT INTO a2 VALUES(2, 'two');
1264 }
1265} {}
1266do_test where-16.2 {
1267 execsql {
1268 SELECT * FROM a2 CROSS JOIN a1 WHERE a1.id=1 AND a1.v='one';
1269 }
1270} {1 one 1 one 2 two 1 one}
1271
1272# The actual problem reported in #3408.
1273do_test where-16.3 {
1274 execsql {
1275 CREATE TEMP TABLE foo(idx INTEGER);
1276 INSERT INTO foo VALUES(1);
1277 INSERT INTO foo VALUES(1);
1278 INSERT INTO foo VALUES(1);
1279 INSERT INTO foo VALUES(2);
1280 INSERT INTO foo VALUES(2);
1281 CREATE TEMP TABLE bar(stuff INTEGER);
1282 INSERT INTO bar VALUES(100);
1283 INSERT INTO bar VALUES(200);
1284 INSERT INTO bar VALUES(300);
1285 }
1286} {}
1287do_test where-16.4 {
1288 execsql {
1289 SELECT bar.RowID id FROM foo, bar WHERE foo.idx = bar.RowID AND id = 2;
1290 }
1291} {2 2}
1292
drhed717fe2003-06-15 23:42:24 +00001293integrity_check {where-99.0}
drh08192d52002-04-30 19:20:28 +00001294
danielk19773072c5e2008-11-03 09:06:05 +00001295#---------------------------------------------------------------------
1296# These tests test that a bug surrounding the use of ForceInt has been
1297# fixed in where.c.
1298#
1299do_test where-17.1 {
1300 execsql {
1301 CREATE TABLE tbooking (
1302 id INTEGER PRIMARY KEY,
1303 eventtype INTEGER NOT NULL
1304 );
1305 INSERT INTO tbooking VALUES(42, 3);
1306 INSERT INTO tbooking VALUES(43, 4);
1307 }
1308} {}
1309do_test where-17.2 {
1310 execsql {
1311 SELECT a.id
1312 FROM tbooking AS a
1313 WHERE a.eventtype=3;
1314 }
1315} {42}
1316do_test where-17.3 {
1317 execsql {
1318 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1319 FROM tbooking AS a
1320 WHERE a.eventtype=3;
1321 }
1322} {42 43}
1323do_test where-17.4 {
1324 execsql {
1325 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1326 FROM (SELECT 1.5 AS id) AS a
1327 }
1328} {1.5 42}
1329do_test where-17.5 {
1330 execsql {
1331 CREATE TABLE tother(a, b);
1332 INSERT INTO tother VALUES(1, 3.7);
1333 SELECT id, a FROM tbooking, tother WHERE id>a;
1334 }
1335} {42 1 43 1}
1336
drh67a5ec72013-09-03 14:03:47 +00001337# Ticket [be84e357c035d068135f20bcfe82761bbf95006b] 2013-09-03
1338# Segfault during query involving LEFT JOIN column in the ORDER BY clause.
1339#
1340do_execsql_test where-18.1 {
1341 CREATE TABLE t181(a);
1342 CREATE TABLE t182(b,c);
1343 INSERT INTO t181 VALUES(1);
1344 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL;
1345} {1}
1346do_execsql_test where-18.2 {
1347 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1348} {1}
1349do_execsql_test where-18.3 {
1350 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c;
1351} {1}
1352do_execsql_test where-18.4 {
1353 INSERT INTO t181 VALUES(1),(1),(1),(1);
1354 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1355} {1}
1356do_execsql_test where-18.5 {
1357 INSERT INTO t181 VALUES(2);
1358 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL, +a;
1359} {1 2}
1360do_execsql_test where-18.6 {
1361 INSERT INTO t181 VALUES(2);
1362 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +a, +c IS NULL;
1363} {1 2}
1364
1365
drh6de4f4c2000-06-12 12:20:48 +00001366finish_test