xref: /illumos-gate/usr/src/lib/libsqlite/test/where.test (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing the use of indices in WHERE clases.
14#
15# $Id: where.test,v 1.17 2003/06/15 23:42:25 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22do_test where-1.0 {
23  execsql {
24    CREATE TABLE t1(w int, x int, y int);
25    CREATE TABLE t2(p int, q int, r int, s int);
26  }
27  for {set i 1} {$i<=100} {incr i} {
28    set w $i
29    set x [expr {int(log($i)/log(2))}]
30    set y [expr {$i*$i + 2*$i + 1}]
31    execsql "INSERT INTO t1 VALUES($w,$x,$y)"
32  }
33  execsql {
34    INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
35    CREATE INDEX i1w ON t1(w);
36    CREATE INDEX i1xy ON t1(x,y);
37    CREATE INDEX i2p ON t2(p);
38    CREATE INDEX i2r ON t2(r);
39    CREATE INDEX i2qs ON t2(q, s);
40  }
41} {}
42
43# Do an SQL statement.  Append the search count to the end of the result.
44#
45proc count sql {
46  set ::sqlite_search_count 0
47  return [concat [execsql $sql] $::sqlite_search_count]
48}
49
50# Verify that queries use an index.  We are using the special variable
51# "sqlite_search_count" which tallys the number of executions of MoveTo
52# and Next operators in the VDBE.  By verifing that the search count is
53# small we can be assured that indices are being used properly.
54#
55do_test where-1.1 {
56  count {SELECT x, y FROM t1 WHERE w=10}
57} {3 121 3}
58do_test where-1.2 {
59  count {SELECT x, y FROM t1 WHERE w=11}
60} {3 144 3}
61do_test where-1.3 {
62  count {SELECT x, y FROM t1 WHERE 11=w}
63} {3 144 3}
64do_test where-1.4 {
65  count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
66} {3 144 3}
67do_test where-1.5 {
68  count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
69} {3 144 3}
70do_test where-1.6 {
71  count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
72} {3 144 3}
73do_test where-1.7 {
74  count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
75} {3 144 3}
76do_test where-1.8 {
77  count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
78} {3 144 3}
79do_test where-1.9 {
80  count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
81} {3 144 3}
82do_test where-1.10 {
83  count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
84} {3 121 3}
85do_test where-1.11 {
86  count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
87} {3 100 3}
88
89# New for SQLite version 2.1: Verify that that inequality constraints
90# are used correctly.
91#
92do_test where-1.12 {
93  count {SELECT w FROM t1 WHERE x=3 AND y<100}
94} {8 3}
95do_test where-1.13 {
96  count {SELECT w FROM t1 WHERE x=3 AND 100>y}
97} {8 3}
98do_test where-1.14 {
99  count {SELECT w FROM t1 WHERE 3=x AND y<100}
100} {8 3}
101do_test where-1.15 {
102  count {SELECT w FROM t1 WHERE 3=x AND 100>y}
103} {8 3}
104do_test where-1.16 {
105  count {SELECT w FROM t1 WHERE x=3 AND y<=100}
106} {8 9 5}
107do_test where-1.17 {
108  count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
109} {8 9 5}
110do_test where-1.18 {
111  count {SELECT w FROM t1 WHERE x=3 AND y>225}
112} {15 3}
113do_test where-1.19 {
114  count {SELECT w FROM t1 WHERE x=3 AND 225<y}
115} {15 3}
116do_test where-1.20 {
117  count {SELECT w FROM t1 WHERE x=3 AND y>=225}
118} {14 15 5}
119do_test where-1.21 {
120  count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
121} {14 15 5}
122do_test where-1.22 {
123  count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
124} {11 12 5}
125do_test where-1.23 {
126  count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
127} {10 11 12 13 9}
128do_test where-1.24 {
129  count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
130} {11 12 5}
131do_test where-1.25 {
132  count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
133} {10 11 12 13 9}
134
135# Need to work on optimizing the BETWEEN operator.
136#
137# do_test where-1.26 {
138#   count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
139# } {10 11 12 13 9}
140
141do_test where-1.27 {
142  count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
143} {10 17}
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 0}
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 10}
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 1}
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 199}
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 7}
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 199}
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 6}
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 6}
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 6}
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 SortCallback $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}
504do_test where-6.17 {
505  cksort {
506    SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
507  }
508} {4 121 10201 sort}
509do_test where-6.18 {
510  cksort {
511    SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
512  }
513} {4 9 16 sort}
514do_test where-6.19 {
515  cksort {
516    SELECT y FROM t1 ORDER BY w LIMIT 3;
517  }
518} {4 9 16 nosort}
519
520# Tests for reverse-order sorting.
521#
522do_test where-7.1 {
523  cksort {
524    SELECT w FROM t1 WHERE x=3 ORDER BY y;
525  }
526} {8 9 10 11 12 13 14 15 nosort}
527do_test where-7.2 {
528  cksort {
529    SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
530  }
531} {15 14 13 12 11 10 9 8 nosort}
532do_test where-7.3 {
533  cksort {
534    SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
535  }
536} {10 11 12 nosort}
537do_test where-7.4 {
538  cksort {
539    SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
540  }
541} {15 14 13 nosort}
542do_test where-7.5 {
543  cksort {
544    SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
545  }
546} {15 14 13 12 11 nosort}
547do_test where-7.6 {
548  cksort {
549    SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
550  }
551} {15 14 13 12 11 10 nosort}
552do_test where-7.7 {
553  cksort {
554    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
555  }
556} {12 11 10 nosort}
557do_test where-7.8 {
558  cksort {
559    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
560  }
561} {13 12 11 10 nosort}
562do_test where-7.9 {
563  cksort {
564    SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
565  }
566} {13 12 11 nosort}
567do_test where-7.10 {
568  cksort {
569    SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
570  }
571} {12 11 10 nosort}
572do_test where-7.11 {
573  cksort {
574    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
575  }
576} {10 11 12 nosort}
577do_test where-7.12 {
578  cksort {
579    SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
580  }
581} {10 11 12 13 nosort}
582do_test where-7.13 {
583  cksort {
584    SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
585  }
586} {11 12 13 nosort}
587do_test where-7.14 {
588  cksort {
589    SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
590  }
591} {10 11 12 nosort}
592do_test where-7.15 {
593  cksort {
594    SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
595  }
596} {nosort}
597do_test where-7.16 {
598  cksort {
599    SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
600  }
601} {8 nosort}
602do_test where-7.17 {
603  cksort {
604    SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
605  }
606} {nosort}
607do_test where-7.18 {
608  cksort {
609    SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
610  }
611} {15 nosort}
612do_test where-7.19 {
613  cksort {
614    SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
615  }
616} {nosort}
617do_test where-7.20 {
618  cksort {
619    SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
620  }
621} {8 nosort}
622do_test where-7.21 {
623  cksort {
624    SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
625  }
626} {nosort}
627do_test where-7.22 {
628  cksort {
629    SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
630  }
631} {15 nosort}
632do_test where-7.23 {
633  cksort {
634    SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
635  }
636} {nosort}
637do_test where-7.24 {
638  cksort {
639    SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
640  }
641} {1 nosort}
642do_test where-7.25 {
643  cksort {
644    SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
645  }
646} {nosort}
647do_test where-7.26 {
648  cksort {
649    SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
650  }
651} {100 nosort}
652do_test where-7.27 {
653  cksort {
654    SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
655  }
656} {nosort}
657do_test where-7.28 {
658  cksort {
659    SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
660  }
661} {1 nosort}
662do_test where-7.29 {
663  cksort {
664    SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
665  }
666} {nosort}
667do_test where-7.30 {
668  cksort {
669    SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
670  }
671} {100 nosort}
672
673do_test where-8.1 {
674  execsql {
675    CREATE TABLE t4 AS SELECT * FROM t1;
676    CREATE INDEX i4xy ON t4(x,y);
677  }
678  cksort {
679    SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
680  }
681} {30 29 28 nosort}
682do_test where-8.2 {
683  execsql {
684    DELETE FROM t4;
685  }
686  cksort {
687    SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
688  }
689} {nosort}
690
691# Make sure searches with an index work with an empty table.
692#
693do_test where-9.1 {
694  execsql {
695    CREATE TABLE t5(x PRIMARY KEY);
696    SELECT * FROM t5 WHERE x<10;
697  }
698} {}
699do_test where-9.2 {
700  execsql {
701    SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
702  }
703} {}
704do_test where-9.3 {
705  execsql {
706    SELECT * FROM t5 WHERE x=10;
707  }
708} {}
709
710do_test where-10.1 {
711  execsql {
712    SELECT 1 WHERE abs(random())<0
713  }
714} {}
715do_test where-10.2 {
716  proc tclvar_func {vname} {return [set ::$vname]}
717  db function tclvar tclvar_func
718  set ::v1 0
719  execsql {
720    SELECT count(*) FROM t1 WHERE tclvar('v1');
721  }
722} {0}
723do_test where-10.3 {
724  set ::v1 1
725  execsql {
726    SELECT count(*) FROM t1 WHERE tclvar('v1');
727  }
728} {100}
729do_test where-10.4 {
730  set ::v1 1
731  proc tclvar_func {vname} {
732    upvar #0 $vname v
733    set v [expr {!$v}]
734    return $v
735  }
736  execsql {
737    SELECT count(*) FROM t1 WHERE tclvar('v1');
738  }
739} {50}
740
741integrity_check {where-99.0}
742
743finish_test
744