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