xref: /titanic_50/usr/src/lib/libsqlite/test/where.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing the use of indices in WHERE clases.
16*c5c4113dSnw141292#
17*c5c4113dSnw141292# $Id: where.test,v 1.17 2003/06/15 23:42:25 drh Exp $
18*c5c4113dSnw141292
19*c5c4113dSnw141292set testdir [file dirname $argv0]
20*c5c4113dSnw141292source $testdir/tester.tcl
21*c5c4113dSnw141292
22*c5c4113dSnw141292# Build some test data
23*c5c4113dSnw141292#
24*c5c4113dSnw141292do_test where-1.0 {
25*c5c4113dSnw141292  execsql {
26*c5c4113dSnw141292    CREATE TABLE t1(w int, x int, y int);
27*c5c4113dSnw141292    CREATE TABLE t2(p int, q int, r int, s int);
28*c5c4113dSnw141292  }
29*c5c4113dSnw141292  for {set i 1} {$i<=100} {incr i} {
30*c5c4113dSnw141292    set w $i
31*c5c4113dSnw141292    set x [expr {int(log($i)/log(2))}]
32*c5c4113dSnw141292    set y [expr {$i*$i + 2*$i + 1}]
33*c5c4113dSnw141292    execsql "INSERT INTO t1 VALUES($w,$x,$y)"
34*c5c4113dSnw141292  }
35*c5c4113dSnw141292  execsql {
36*c5c4113dSnw141292    INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
37*c5c4113dSnw141292    CREATE INDEX i1w ON t1(w);
38*c5c4113dSnw141292    CREATE INDEX i1xy ON t1(x,y);
39*c5c4113dSnw141292    CREATE INDEX i2p ON t2(p);
40*c5c4113dSnw141292    CREATE INDEX i2r ON t2(r);
41*c5c4113dSnw141292    CREATE INDEX i2qs ON t2(q, s);
42*c5c4113dSnw141292  }
43*c5c4113dSnw141292} {}
44*c5c4113dSnw141292
45*c5c4113dSnw141292# Do an SQL statement.  Append the search count to the end of the result.
46*c5c4113dSnw141292#
47*c5c4113dSnw141292proc count sql {
48*c5c4113dSnw141292  set ::sqlite_search_count 0
49*c5c4113dSnw141292  return [concat [execsql $sql] $::sqlite_search_count]
50*c5c4113dSnw141292}
51*c5c4113dSnw141292
52*c5c4113dSnw141292# Verify that queries use an index.  We are using the special variable
53*c5c4113dSnw141292# "sqlite_search_count" which tallys the number of executions of MoveTo
54*c5c4113dSnw141292# and Next operators in the VDBE.  By verifing that the search count is
55*c5c4113dSnw141292# small we can be assured that indices are being used properly.
56*c5c4113dSnw141292#
57*c5c4113dSnw141292do_test where-1.1 {
58*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE w=10}
59*c5c4113dSnw141292} {3 121 3}
60*c5c4113dSnw141292do_test where-1.2 {
61*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE w=11}
62*c5c4113dSnw141292} {3 144 3}
63*c5c4113dSnw141292do_test where-1.3 {
64*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE 11=w}
65*c5c4113dSnw141292} {3 144 3}
66*c5c4113dSnw141292do_test where-1.4 {
67*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
68*c5c4113dSnw141292} {3 144 3}
69*c5c4113dSnw141292do_test where-1.5 {
70*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
71*c5c4113dSnw141292} {3 144 3}
72*c5c4113dSnw141292do_test where-1.6 {
73*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
74*c5c4113dSnw141292} {3 144 3}
75*c5c4113dSnw141292do_test where-1.7 {
76*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
77*c5c4113dSnw141292} {3 144 3}
78*c5c4113dSnw141292do_test where-1.8 {
79*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
80*c5c4113dSnw141292} {3 144 3}
81*c5c4113dSnw141292do_test where-1.9 {
82*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
83*c5c4113dSnw141292} {3 144 3}
84*c5c4113dSnw141292do_test where-1.10 {
85*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
86*c5c4113dSnw141292} {3 121 3}
87*c5c4113dSnw141292do_test where-1.11 {
88*c5c4113dSnw141292  count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
89*c5c4113dSnw141292} {3 100 3}
90*c5c4113dSnw141292
91*c5c4113dSnw141292# New for SQLite version 2.1: Verify that that inequality constraints
92*c5c4113dSnw141292# are used correctly.
93*c5c4113dSnw141292#
94*c5c4113dSnw141292do_test where-1.12 {
95*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y<100}
96*c5c4113dSnw141292} {8 3}
97*c5c4113dSnw141292do_test where-1.13 {
98*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 100>y}
99*c5c4113dSnw141292} {8 3}
100*c5c4113dSnw141292do_test where-1.14 {
101*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE 3=x AND y<100}
102*c5c4113dSnw141292} {8 3}
103*c5c4113dSnw141292do_test where-1.15 {
104*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE 3=x AND 100>y}
105*c5c4113dSnw141292} {8 3}
106*c5c4113dSnw141292do_test where-1.16 {
107*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y<=100}
108*c5c4113dSnw141292} {8 9 5}
109*c5c4113dSnw141292do_test where-1.17 {
110*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
111*c5c4113dSnw141292} {8 9 5}
112*c5c4113dSnw141292do_test where-1.18 {
113*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y>225}
114*c5c4113dSnw141292} {15 3}
115*c5c4113dSnw141292do_test where-1.19 {
116*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 225<y}
117*c5c4113dSnw141292} {15 3}
118*c5c4113dSnw141292do_test where-1.20 {
119*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y>=225}
120*c5c4113dSnw141292} {14 15 5}
121*c5c4113dSnw141292do_test where-1.21 {
122*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
123*c5c4113dSnw141292} {14 15 5}
124*c5c4113dSnw141292do_test where-1.22 {
125*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
126*c5c4113dSnw141292} {11 12 5}
127*c5c4113dSnw141292do_test where-1.23 {
128*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
129*c5c4113dSnw141292} {10 11 12 13 9}
130*c5c4113dSnw141292do_test where-1.24 {
131*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
132*c5c4113dSnw141292} {11 12 5}
133*c5c4113dSnw141292do_test where-1.25 {
134*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
135*c5c4113dSnw141292} {10 11 12 13 9}
136*c5c4113dSnw141292
137*c5c4113dSnw141292# Need to work on optimizing the BETWEEN operator.
138*c5c4113dSnw141292#
139*c5c4113dSnw141292# do_test where-1.26 {
140*c5c4113dSnw141292#   count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
141*c5c4113dSnw141292# } {10 11 12 13 9}
142*c5c4113dSnw141292
143*c5c4113dSnw141292do_test where-1.27 {
144*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
145*c5c4113dSnw141292} {10 17}
146*c5c4113dSnw141292do_test where-1.28 {
147*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
148*c5c4113dSnw141292} {10 99}
149*c5c4113dSnw141292do_test where-1.29 {
150*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE y==121}
151*c5c4113dSnw141292} {10 99}
152*c5c4113dSnw141292
153*c5c4113dSnw141292
154*c5c4113dSnw141292do_test where-1.30 {
155*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w>97}
156*c5c4113dSnw141292} {98 99 100 6}
157*c5c4113dSnw141292do_test where-1.31 {
158*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w>=97}
159*c5c4113dSnw141292} {97 98 99 100 8}
160*c5c4113dSnw141292do_test where-1.33 {
161*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w==97}
162*c5c4113dSnw141292} {97 3}
163*c5c4113dSnw141292do_test where-1.34 {
164*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w+1==98}
165*c5c4113dSnw141292} {97 99}
166*c5c4113dSnw141292do_test where-1.35 {
167*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w<3}
168*c5c4113dSnw141292} {1 2 4}
169*c5c4113dSnw141292do_test where-1.36 {
170*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w<=3}
171*c5c4113dSnw141292} {1 2 3 6}
172*c5c4113dSnw141292do_test where-1.37 {
173*c5c4113dSnw141292  count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
174*c5c4113dSnw141292} {1 2 3 199}
175*c5c4113dSnw141292
176*c5c4113dSnw141292do_test where-1.38 {
177*c5c4113dSnw141292  count {SELECT (w) FROM t1 WHERE (w)>(97)}
178*c5c4113dSnw141292} {98 99 100 6}
179*c5c4113dSnw141292do_test where-1.39 {
180*c5c4113dSnw141292  count {SELECT (w) FROM t1 WHERE (w)>=(97)}
181*c5c4113dSnw141292} {97 98 99 100 8}
182*c5c4113dSnw141292do_test where-1.40 {
183*c5c4113dSnw141292  count {SELECT (w) FROM t1 WHERE (w)==(97)}
184*c5c4113dSnw141292} {97 3}
185*c5c4113dSnw141292do_test where-1.41 {
186*c5c4113dSnw141292  count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
187*c5c4113dSnw141292} {97 99}
188*c5c4113dSnw141292
189*c5c4113dSnw141292
190*c5c4113dSnw141292# Do the same kind of thing except use a join as the data source.
191*c5c4113dSnw141292#
192*c5c4113dSnw141292do_test where-2.1 {
193*c5c4113dSnw141292  count {
194*c5c4113dSnw141292    SELECT w, p FROM t2, t1
195*c5c4113dSnw141292    WHERE x=q AND y=s AND r=8977
196*c5c4113dSnw141292  }
197*c5c4113dSnw141292} {34 67 6}
198*c5c4113dSnw141292do_test where-2.2 {
199*c5c4113dSnw141292  count {
200*c5c4113dSnw141292    SELECT w, p FROM t2, t1
201*c5c4113dSnw141292    WHERE x=q AND s=y AND r=8977
202*c5c4113dSnw141292  }
203*c5c4113dSnw141292} {34 67 6}
204*c5c4113dSnw141292do_test where-2.3 {
205*c5c4113dSnw141292  count {
206*c5c4113dSnw141292    SELECT w, p FROM t2, t1
207*c5c4113dSnw141292    WHERE x=q AND s=y AND r=8977 AND w>10
208*c5c4113dSnw141292  }
209*c5c4113dSnw141292} {34 67 6}
210*c5c4113dSnw141292do_test where-2.4 {
211*c5c4113dSnw141292  count {
212*c5c4113dSnw141292    SELECT w, p FROM t2, t1
213*c5c4113dSnw141292    WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
214*c5c4113dSnw141292  }
215*c5c4113dSnw141292} {34 67 6}
216*c5c4113dSnw141292do_test where-2.5 {
217*c5c4113dSnw141292  count {
218*c5c4113dSnw141292    SELECT w, p FROM t2, t1
219*c5c4113dSnw141292    WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
220*c5c4113dSnw141292  }
221*c5c4113dSnw141292} {34 67 6}
222*c5c4113dSnw141292do_test where-2.6 {
223*c5c4113dSnw141292  count {
224*c5c4113dSnw141292    SELECT w, p FROM t2, t1
225*c5c4113dSnw141292    WHERE x=q AND p=77 AND s=y AND w>5
226*c5c4113dSnw141292  }
227*c5c4113dSnw141292} {24 77 6}
228*c5c4113dSnw141292do_test where-2.7 {
229*c5c4113dSnw141292  count {
230*c5c4113dSnw141292    SELECT w, p FROM t1, t2
231*c5c4113dSnw141292    WHERE x=q AND p>77 AND s=y AND w=5
232*c5c4113dSnw141292  }
233*c5c4113dSnw141292} {5 96 6}
234*c5c4113dSnw141292
235*c5c4113dSnw141292# Lets do a 3-way join.
236*c5c4113dSnw141292#
237*c5c4113dSnw141292do_test where-3.1 {
238*c5c4113dSnw141292  count {
239*c5c4113dSnw141292    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
240*c5c4113dSnw141292    WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
241*c5c4113dSnw141292  }
242*c5c4113dSnw141292} {11 90 11 9}
243*c5c4113dSnw141292do_test where-3.2 {
244*c5c4113dSnw141292  count {
245*c5c4113dSnw141292    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
246*c5c4113dSnw141292    WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
247*c5c4113dSnw141292  }
248*c5c4113dSnw141292} {12 89 12 9}
249*c5c4113dSnw141292do_test where-3.3 {
250*c5c4113dSnw141292  count {
251*c5c4113dSnw141292    SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
252*c5c4113dSnw141292    WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
253*c5c4113dSnw141292  }
254*c5c4113dSnw141292} {15 86 86 9}
255*c5c4113dSnw141292
256*c5c4113dSnw141292# Test to see that the special case of a constant WHERE clause is
257*c5c4113dSnw141292# handled.
258*c5c4113dSnw141292#
259*c5c4113dSnw141292do_test where-4.1 {
260*c5c4113dSnw141292  count {
261*c5c4113dSnw141292    SELECT * FROM t1 WHERE 0
262*c5c4113dSnw141292  }
263*c5c4113dSnw141292} {0}
264*c5c4113dSnw141292do_test where-4.2 {
265*c5c4113dSnw141292  count {
266*c5c4113dSnw141292    SELECT * FROM t1 WHERE 1 LIMIT 1
267*c5c4113dSnw141292  }
268*c5c4113dSnw141292} {1 0 4 1}
269*c5c4113dSnw141292do_test where-4.3 {
270*c5c4113dSnw141292  execsql {
271*c5c4113dSnw141292    SELECT 99 WHERE 0
272*c5c4113dSnw141292  }
273*c5c4113dSnw141292} {}
274*c5c4113dSnw141292do_test where-4.4 {
275*c5c4113dSnw141292  execsql {
276*c5c4113dSnw141292    SELECT 99 WHERE 1
277*c5c4113dSnw141292  }
278*c5c4113dSnw141292} {99}
279*c5c4113dSnw141292
280*c5c4113dSnw141292# Verify that IN operators in a WHERE clause are handled correctly.
281*c5c4113dSnw141292#
282*c5c4113dSnw141292do_test where-5.1 {
283*c5c4113dSnw141292  count {
284*c5c4113dSnw141292    SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
285*c5c4113dSnw141292  }
286*c5c4113dSnw141292} {1 0 4 2 1 9 3 1 16 0}
287*c5c4113dSnw141292do_test where-5.2 {
288*c5c4113dSnw141292  count {
289*c5c4113dSnw141292    SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
290*c5c4113dSnw141292  }
291*c5c4113dSnw141292} {1 0 4 2 1 9 3 1 16 199}
292*c5c4113dSnw141292do_test where-5.3 {
293*c5c4113dSnw141292  count {
294*c5c4113dSnw141292    SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
295*c5c4113dSnw141292  }
296*c5c4113dSnw141292} {1 0 4 2 1 9 3 1 16 10}
297*c5c4113dSnw141292do_test where-5.4 {
298*c5c4113dSnw141292  count {
299*c5c4113dSnw141292    SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
300*c5c4113dSnw141292  }
301*c5c4113dSnw141292} {1 0 4 2 1 9 3 1 16 199}
302*c5c4113dSnw141292do_test where-5.5 {
303*c5c4113dSnw141292  count {
304*c5c4113dSnw141292    SELECT * FROM t1 WHERE rowid IN
305*c5c4113dSnw141292       (select rowid from t1 where rowid IN (-1,2,4))
306*c5c4113dSnw141292    ORDER BY 1;
307*c5c4113dSnw141292  }
308*c5c4113dSnw141292} {2 1 9 4 2 25 1}
309*c5c4113dSnw141292do_test where-5.6 {
310*c5c4113dSnw141292  count {
311*c5c4113dSnw141292    SELECT * FROM t1 WHERE rowid+0 IN
312*c5c4113dSnw141292       (select rowid from t1 where rowid IN (-1,2,4))
313*c5c4113dSnw141292    ORDER BY 1;
314*c5c4113dSnw141292  }
315*c5c4113dSnw141292} {2 1 9 4 2 25 199}
316*c5c4113dSnw141292do_test where-5.7 {
317*c5c4113dSnw141292  count {
318*c5c4113dSnw141292    SELECT * FROM t1 WHERE w IN
319*c5c4113dSnw141292       (select rowid from t1 where rowid IN (-1,2,4))
320*c5c4113dSnw141292    ORDER BY 1;
321*c5c4113dSnw141292  }
322*c5c4113dSnw141292} {2 1 9 4 2 25 7}
323*c5c4113dSnw141292do_test where-5.8 {
324*c5c4113dSnw141292  count {
325*c5c4113dSnw141292    SELECT * FROM t1 WHERE w+0 IN
326*c5c4113dSnw141292       (select rowid from t1 where rowid IN (-1,2,4))
327*c5c4113dSnw141292    ORDER BY 1;
328*c5c4113dSnw141292  }
329*c5c4113dSnw141292} {2 1 9 4 2 25 199}
330*c5c4113dSnw141292do_test where-5.9 {
331*c5c4113dSnw141292  count {
332*c5c4113dSnw141292    SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
333*c5c4113dSnw141292  }
334*c5c4113dSnw141292} {2 1 9 3 1 16 6}
335*c5c4113dSnw141292do_test where-5.10 {
336*c5c4113dSnw141292  count {
337*c5c4113dSnw141292    SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
338*c5c4113dSnw141292  }
339*c5c4113dSnw141292} {2 1 9 3 1 16 199}
340*c5c4113dSnw141292do_test where-5.11 {
341*c5c4113dSnw141292  count {
342*c5c4113dSnw141292    SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
343*c5c4113dSnw141292  }
344*c5c4113dSnw141292} {79 6 6400 89 6 8100 199}
345*c5c4113dSnw141292do_test where-5.12 {
346*c5c4113dSnw141292  count {
347*c5c4113dSnw141292    SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
348*c5c4113dSnw141292  }
349*c5c4113dSnw141292} {79 6 6400 89 6 8100 74}
350*c5c4113dSnw141292do_test where-5.13 {
351*c5c4113dSnw141292  count {
352*c5c4113dSnw141292    SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
353*c5c4113dSnw141292  }
354*c5c4113dSnw141292} {2 1 9 3 1 16 6}
355*c5c4113dSnw141292do_test where-5.14 {
356*c5c4113dSnw141292  count {
357*c5c4113dSnw141292    SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
358*c5c4113dSnw141292  }
359*c5c4113dSnw141292} {2 1 9 6}
360*c5c4113dSnw141292
361*c5c4113dSnw141292# This procedure executes the SQL.  Then it checks the generated program
362*c5c4113dSnw141292# for the SQL and appends a "nosort" to the result if the program contains the
363*c5c4113dSnw141292# SortCallback opcode.  If the program does not contain the SortCallback
364*c5c4113dSnw141292# opcode it appends "sort"
365*c5c4113dSnw141292#
366*c5c4113dSnw141292proc cksort {sql} {
367*c5c4113dSnw141292  set data [execsql $sql]
368*c5c4113dSnw141292  set prog [execsql "EXPLAIN $sql"]
369*c5c4113dSnw141292  if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
370*c5c4113dSnw141292  lappend data $x
371*c5c4113dSnw141292  return $data
372*c5c4113dSnw141292}
373*c5c4113dSnw141292# Check out the logic that attempts to implement the ORDER BY clause
374*c5c4113dSnw141292# using an index rather than by sorting.
375*c5c4113dSnw141292#
376*c5c4113dSnw141292do_test where-6.1 {
377*c5c4113dSnw141292  execsql {
378*c5c4113dSnw141292    CREATE TABLE t3(a,b,c);
379*c5c4113dSnw141292    CREATE INDEX t3a ON t3(a);
380*c5c4113dSnw141292    CREATE INDEX t3bc ON t3(b,c);
381*c5c4113dSnw141292    CREATE INDEX t3acb ON t3(a,c,b);
382*c5c4113dSnw141292    INSERT INTO t3 SELECT w, 101-w, y FROM t1;
383*c5c4113dSnw141292    SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
384*c5c4113dSnw141292  }
385*c5c4113dSnw141292} {100 5050 5050 348550}
386*c5c4113dSnw141292do_test where-6.2 {
387*c5c4113dSnw141292  cksort {
388*c5c4113dSnw141292    SELECT * FROM t3 ORDER BY a LIMIT 3
389*c5c4113dSnw141292  }
390*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 nosort}
391*c5c4113dSnw141292do_test where-6.3 {
392*c5c4113dSnw141292  cksort {
393*c5c4113dSnw141292    SELECT * FROM t3 ORDER BY a+1 LIMIT 3
394*c5c4113dSnw141292  }
395*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 sort}
396*c5c4113dSnw141292do_test where-6.4 {
397*c5c4113dSnw141292  cksort {
398*c5c4113dSnw141292    SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
399*c5c4113dSnw141292  }
400*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 nosort}
401*c5c4113dSnw141292do_test where-6.5 {
402*c5c4113dSnw141292  cksort {
403*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
404*c5c4113dSnw141292  }
405*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 nosort}
406*c5c4113dSnw141292do_test where-6.6 {
407*c5c4113dSnw141292  cksort {
408*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
409*c5c4113dSnw141292  }
410*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 nosort}
411*c5c4113dSnw141292do_test where-6.7 {
412*c5c4113dSnw141292  cksort {
413*c5c4113dSnw141292    SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
414*c5c4113dSnw141292  }
415*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 sort}
416*c5c4113dSnw141292do_test where-6.8 {
417*c5c4113dSnw141292  cksort {
418*c5c4113dSnw141292    SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
419*c5c4113dSnw141292  }
420*c5c4113dSnw141292} {1 100 4 2 99 9 3 98 16 sort}
421*c5c4113dSnw141292do_test where-6.9.1 {
422*c5c4113dSnw141292  cksort {
423*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
424*c5c4113dSnw141292  }
425*c5c4113dSnw141292} {1 100 4 nosort}
426*c5c4113dSnw141292do_test where-6.9.2 {
427*c5c4113dSnw141292  cksort {
428*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
429*c5c4113dSnw141292  }
430*c5c4113dSnw141292} {1 100 4 nosort}
431*c5c4113dSnw141292do_test where-6.9.3 {
432*c5c4113dSnw141292  cksort {
433*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
434*c5c4113dSnw141292  }
435*c5c4113dSnw141292} {1 100 4 nosort}
436*c5c4113dSnw141292do_test where-6.9.4 {
437*c5c4113dSnw141292  cksort {
438*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
439*c5c4113dSnw141292  }
440*c5c4113dSnw141292} {1 100 4 nosort}
441*c5c4113dSnw141292do_test where-6.9.5 {
442*c5c4113dSnw141292  cksort {
443*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
444*c5c4113dSnw141292  }
445*c5c4113dSnw141292} {1 100 4 nosort}
446*c5c4113dSnw141292do_test where-6.9.6 {
447*c5c4113dSnw141292  cksort {
448*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
449*c5c4113dSnw141292  }
450*c5c4113dSnw141292} {1 100 4 nosort}
451*c5c4113dSnw141292do_test where-6.9.7 {
452*c5c4113dSnw141292  cksort {
453*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
454*c5c4113dSnw141292  }
455*c5c4113dSnw141292} {1 100 4 sort}
456*c5c4113dSnw141292do_test where-6.9.8 {
457*c5c4113dSnw141292  cksort {
458*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
459*c5c4113dSnw141292  }
460*c5c4113dSnw141292} {1 100 4 sort}
461*c5c4113dSnw141292do_test where-6.9.9 {
462*c5c4113dSnw141292  cksort {
463*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
464*c5c4113dSnw141292  }
465*c5c4113dSnw141292} {1 100 4 sort}
466*c5c4113dSnw141292do_test where-6.10 {
467*c5c4113dSnw141292  cksort {
468*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
469*c5c4113dSnw141292  }
470*c5c4113dSnw141292} {1 100 4 nosort}
471*c5c4113dSnw141292do_test where-6.11 {
472*c5c4113dSnw141292  cksort {
473*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
474*c5c4113dSnw141292  }
475*c5c4113dSnw141292} {1 100 4 nosort}
476*c5c4113dSnw141292do_test where-6.12 {
477*c5c4113dSnw141292  cksort {
478*c5c4113dSnw141292    SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
479*c5c4113dSnw141292  }
480*c5c4113dSnw141292} {1 100 4 nosort}
481*c5c4113dSnw141292do_test where-6.13 {
482*c5c4113dSnw141292  cksort {
483*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
484*c5c4113dSnw141292  }
485*c5c4113dSnw141292} {100 1 10201 99 2 10000 98 3 9801 nosort}
486*c5c4113dSnw141292do_test where-6.13.1 {
487*c5c4113dSnw141292  cksort {
488*c5c4113dSnw141292    SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
489*c5c4113dSnw141292  }
490*c5c4113dSnw141292} {100 1 10201 99 2 10000 98 3 9801 sort}
491*c5c4113dSnw141292do_test where-6.14 {
492*c5c4113dSnw141292  cksort {
493*c5c4113dSnw141292    SELECT * FROM t3 ORDER BY b LIMIT 3
494*c5c4113dSnw141292  }
495*c5c4113dSnw141292} {100 1 10201 99 2 10000 98 3 9801 nosort}
496*c5c4113dSnw141292do_test where-6.15 {
497*c5c4113dSnw141292  cksort {
498*c5c4113dSnw141292    SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
499*c5c4113dSnw141292  }
500*c5c4113dSnw141292} {1 0 2 1 3 1 nosort}
501*c5c4113dSnw141292do_test where-6.16 {
502*c5c4113dSnw141292  cksort {
503*c5c4113dSnw141292    SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
504*c5c4113dSnw141292  }
505*c5c4113dSnw141292} {1 0 2 1 3 1 sort}
506*c5c4113dSnw141292do_test where-6.17 {
507*c5c4113dSnw141292  cksort {
508*c5c4113dSnw141292    SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
509*c5c4113dSnw141292  }
510*c5c4113dSnw141292} {4 121 10201 sort}
511*c5c4113dSnw141292do_test where-6.18 {
512*c5c4113dSnw141292  cksort {
513*c5c4113dSnw141292    SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
514*c5c4113dSnw141292  }
515*c5c4113dSnw141292} {4 9 16 sort}
516*c5c4113dSnw141292do_test where-6.19 {
517*c5c4113dSnw141292  cksort {
518*c5c4113dSnw141292    SELECT y FROM t1 ORDER BY w LIMIT 3;
519*c5c4113dSnw141292  }
520*c5c4113dSnw141292} {4 9 16 nosort}
521*c5c4113dSnw141292
522*c5c4113dSnw141292# Tests for reverse-order sorting.
523*c5c4113dSnw141292#
524*c5c4113dSnw141292do_test where-7.1 {
525*c5c4113dSnw141292  cksort {
526*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 ORDER BY y;
527*c5c4113dSnw141292  }
528*c5c4113dSnw141292} {8 9 10 11 12 13 14 15 nosort}
529*c5c4113dSnw141292do_test where-7.2 {
530*c5c4113dSnw141292  cksort {
531*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
532*c5c4113dSnw141292  }
533*c5c4113dSnw141292} {15 14 13 12 11 10 9 8 nosort}
534*c5c4113dSnw141292do_test where-7.3 {
535*c5c4113dSnw141292  cksort {
536*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
537*c5c4113dSnw141292  }
538*c5c4113dSnw141292} {10 11 12 nosort}
539*c5c4113dSnw141292do_test where-7.4 {
540*c5c4113dSnw141292  cksort {
541*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
542*c5c4113dSnw141292  }
543*c5c4113dSnw141292} {15 14 13 nosort}
544*c5c4113dSnw141292do_test where-7.5 {
545*c5c4113dSnw141292  cksort {
546*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
547*c5c4113dSnw141292  }
548*c5c4113dSnw141292} {15 14 13 12 11 nosort}
549*c5c4113dSnw141292do_test where-7.6 {
550*c5c4113dSnw141292  cksort {
551*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
552*c5c4113dSnw141292  }
553*c5c4113dSnw141292} {15 14 13 12 11 10 nosort}
554*c5c4113dSnw141292do_test where-7.7 {
555*c5c4113dSnw141292  cksort {
556*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
557*c5c4113dSnw141292  }
558*c5c4113dSnw141292} {12 11 10 nosort}
559*c5c4113dSnw141292do_test where-7.8 {
560*c5c4113dSnw141292  cksort {
561*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
562*c5c4113dSnw141292  }
563*c5c4113dSnw141292} {13 12 11 10 nosort}
564*c5c4113dSnw141292do_test where-7.9 {
565*c5c4113dSnw141292  cksort {
566*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
567*c5c4113dSnw141292  }
568*c5c4113dSnw141292} {13 12 11 nosort}
569*c5c4113dSnw141292do_test where-7.10 {
570*c5c4113dSnw141292  cksort {
571*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
572*c5c4113dSnw141292  }
573*c5c4113dSnw141292} {12 11 10 nosort}
574*c5c4113dSnw141292do_test where-7.11 {
575*c5c4113dSnw141292  cksort {
576*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
577*c5c4113dSnw141292  }
578*c5c4113dSnw141292} {10 11 12 nosort}
579*c5c4113dSnw141292do_test where-7.12 {
580*c5c4113dSnw141292  cksort {
581*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
582*c5c4113dSnw141292  }
583*c5c4113dSnw141292} {10 11 12 13 nosort}
584*c5c4113dSnw141292do_test where-7.13 {
585*c5c4113dSnw141292  cksort {
586*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
587*c5c4113dSnw141292  }
588*c5c4113dSnw141292} {11 12 13 nosort}
589*c5c4113dSnw141292do_test where-7.14 {
590*c5c4113dSnw141292  cksort {
591*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
592*c5c4113dSnw141292  }
593*c5c4113dSnw141292} {10 11 12 nosort}
594*c5c4113dSnw141292do_test where-7.15 {
595*c5c4113dSnw141292  cksort {
596*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
597*c5c4113dSnw141292  }
598*c5c4113dSnw141292} {nosort}
599*c5c4113dSnw141292do_test where-7.16 {
600*c5c4113dSnw141292  cksort {
601*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
602*c5c4113dSnw141292  }
603*c5c4113dSnw141292} {8 nosort}
604*c5c4113dSnw141292do_test where-7.17 {
605*c5c4113dSnw141292  cksort {
606*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
607*c5c4113dSnw141292  }
608*c5c4113dSnw141292} {nosort}
609*c5c4113dSnw141292do_test where-7.18 {
610*c5c4113dSnw141292  cksort {
611*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
612*c5c4113dSnw141292  }
613*c5c4113dSnw141292} {15 nosort}
614*c5c4113dSnw141292do_test where-7.19 {
615*c5c4113dSnw141292  cksort {
616*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
617*c5c4113dSnw141292  }
618*c5c4113dSnw141292} {nosort}
619*c5c4113dSnw141292do_test where-7.20 {
620*c5c4113dSnw141292  cksort {
621*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
622*c5c4113dSnw141292  }
623*c5c4113dSnw141292} {8 nosort}
624*c5c4113dSnw141292do_test where-7.21 {
625*c5c4113dSnw141292  cksort {
626*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
627*c5c4113dSnw141292  }
628*c5c4113dSnw141292} {nosort}
629*c5c4113dSnw141292do_test where-7.22 {
630*c5c4113dSnw141292  cksort {
631*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
632*c5c4113dSnw141292  }
633*c5c4113dSnw141292} {15 nosort}
634*c5c4113dSnw141292do_test where-7.23 {
635*c5c4113dSnw141292  cksort {
636*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
637*c5c4113dSnw141292  }
638*c5c4113dSnw141292} {nosort}
639*c5c4113dSnw141292do_test where-7.24 {
640*c5c4113dSnw141292  cksort {
641*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
642*c5c4113dSnw141292  }
643*c5c4113dSnw141292} {1 nosort}
644*c5c4113dSnw141292do_test where-7.25 {
645*c5c4113dSnw141292  cksort {
646*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
647*c5c4113dSnw141292  }
648*c5c4113dSnw141292} {nosort}
649*c5c4113dSnw141292do_test where-7.26 {
650*c5c4113dSnw141292  cksort {
651*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
652*c5c4113dSnw141292  }
653*c5c4113dSnw141292} {100 nosort}
654*c5c4113dSnw141292do_test where-7.27 {
655*c5c4113dSnw141292  cksort {
656*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
657*c5c4113dSnw141292  }
658*c5c4113dSnw141292} {nosort}
659*c5c4113dSnw141292do_test where-7.28 {
660*c5c4113dSnw141292  cksort {
661*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
662*c5c4113dSnw141292  }
663*c5c4113dSnw141292} {1 nosort}
664*c5c4113dSnw141292do_test where-7.29 {
665*c5c4113dSnw141292  cksort {
666*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
667*c5c4113dSnw141292  }
668*c5c4113dSnw141292} {nosort}
669*c5c4113dSnw141292do_test where-7.30 {
670*c5c4113dSnw141292  cksort {
671*c5c4113dSnw141292    SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
672*c5c4113dSnw141292  }
673*c5c4113dSnw141292} {100 nosort}
674*c5c4113dSnw141292
675*c5c4113dSnw141292do_test where-8.1 {
676*c5c4113dSnw141292  execsql {
677*c5c4113dSnw141292    CREATE TABLE t4 AS SELECT * FROM t1;
678*c5c4113dSnw141292    CREATE INDEX i4xy ON t4(x,y);
679*c5c4113dSnw141292  }
680*c5c4113dSnw141292  cksort {
681*c5c4113dSnw141292    SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
682*c5c4113dSnw141292  }
683*c5c4113dSnw141292} {30 29 28 nosort}
684*c5c4113dSnw141292do_test where-8.2 {
685*c5c4113dSnw141292  execsql {
686*c5c4113dSnw141292    DELETE FROM t4;
687*c5c4113dSnw141292  }
688*c5c4113dSnw141292  cksort {
689*c5c4113dSnw141292    SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
690*c5c4113dSnw141292  }
691*c5c4113dSnw141292} {nosort}
692*c5c4113dSnw141292
693*c5c4113dSnw141292# Make sure searches with an index work with an empty table.
694*c5c4113dSnw141292#
695*c5c4113dSnw141292do_test where-9.1 {
696*c5c4113dSnw141292  execsql {
697*c5c4113dSnw141292    CREATE TABLE t5(x PRIMARY KEY);
698*c5c4113dSnw141292    SELECT * FROM t5 WHERE x<10;
699*c5c4113dSnw141292  }
700*c5c4113dSnw141292} {}
701*c5c4113dSnw141292do_test where-9.2 {
702*c5c4113dSnw141292  execsql {
703*c5c4113dSnw141292    SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
704*c5c4113dSnw141292  }
705*c5c4113dSnw141292} {}
706*c5c4113dSnw141292do_test where-9.3 {
707*c5c4113dSnw141292  execsql {
708*c5c4113dSnw141292    SELECT * FROM t5 WHERE x=10;
709*c5c4113dSnw141292  }
710*c5c4113dSnw141292} {}
711*c5c4113dSnw141292
712*c5c4113dSnw141292do_test where-10.1 {
713*c5c4113dSnw141292  execsql {
714*c5c4113dSnw141292    SELECT 1 WHERE abs(random())<0
715*c5c4113dSnw141292  }
716*c5c4113dSnw141292} {}
717*c5c4113dSnw141292do_test where-10.2 {
718*c5c4113dSnw141292  proc tclvar_func {vname} {return [set ::$vname]}
719*c5c4113dSnw141292  db function tclvar tclvar_func
720*c5c4113dSnw141292  set ::v1 0
721*c5c4113dSnw141292  execsql {
722*c5c4113dSnw141292    SELECT count(*) FROM t1 WHERE tclvar('v1');
723*c5c4113dSnw141292  }
724*c5c4113dSnw141292} {0}
725*c5c4113dSnw141292do_test where-10.3 {
726*c5c4113dSnw141292  set ::v1 1
727*c5c4113dSnw141292  execsql {
728*c5c4113dSnw141292    SELECT count(*) FROM t1 WHERE tclvar('v1');
729*c5c4113dSnw141292  }
730*c5c4113dSnw141292} {100}
731*c5c4113dSnw141292do_test where-10.4 {
732*c5c4113dSnw141292  set ::v1 1
733*c5c4113dSnw141292  proc tclvar_func {vname} {
734*c5c4113dSnw141292    upvar #0 $vname v
735*c5c4113dSnw141292    set v [expr {!$v}]
736*c5c4113dSnw141292    return $v
737*c5c4113dSnw141292  }
738*c5c4113dSnw141292  execsql {
739*c5c4113dSnw141292    SELECT count(*) FROM t1 WHERE tclvar('v1');
740*c5c4113dSnw141292  }
741*c5c4113dSnw141292} {50}
742*c5c4113dSnw141292
743*c5c4113dSnw141292integrity_check {where-99.0}
744*c5c4113dSnw141292
745*c5c4113dSnw141292finish_test
746