3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
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.
14 # $Id: where.test,v 1.50 2008/11/03 09:06:06 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Build some test data
23 CREATE TABLE t1(w int, x int, y int);
24 CREATE TABLE t2(p int, q int, r int, s int);
26 for {set i 1} {$i<=100} {incr 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)"
35 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
38 set maxy [execsql {select max(y) from t1}]
40 INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1;
45 CREATE INDEX i1w ON t1(w);
46 CREATE INDEX i1xy ON t1(x,y);
47 CREATE INDEX i2p ON t2(p);
48 CREATE INDEX i2r ON t2(r);
49 CREATE INDEX i2qs ON t2(q, s);
53 # Do an SQL statement. Append the search count to the end of the result.
56 set ::sqlite_search_count 0
57 return [concat [execsql $sql] $::sqlite_search_count]
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.
66 count {SELECT x, y, w FROM t1 WHERE w=10}
68 do_eqp_test where-1.1.2 {
69 SELECT x, y, w FROM t1 WHERE w=10
70 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
75 db eval {SELECT x, y, w FROM t1 WHERE +w=10}
80 do_eqp_test where-1.1.6 {
81 SELECT x, y, w FROM t1 WHERE +w=10
84 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
86 do_eqp_test where-1.1.8 {
87 SELECT x, y, w AS abc FROM t1 WHERE abc=10
88 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
93 count {SELECT x, y, w FROM t1 WHERE w=11}
96 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
99 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
101 do_test where-1.3.2 {
102 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
104 do_test where-1.4.1 {
105 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
107 do_eqp_test where-1.4.2 {
108 SELECT w, x, y FROM t1 WHERE 11=w AND x>2
109 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
110 do_test where-1.4.3 {
111 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
113 do_eqp_test where-1.4.4 {
114 SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2
115 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
117 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
119 do_eqp_test where-1.5.2 {
120 SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2
121 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
123 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
126 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
129 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
131 do_eqp_test where-1.8.2 {
132 SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3
133 } {*SEARCH TABLE t1 USING INDEX i1xy (x=? AND y=?)*}
134 do_eqp_test where-1.8.3 {
135 SELECT x, y FROM t1 WHERE y=144 AND x=3
136 } {*SEARCH TABLE t1 USING COVERING INDEX i1xy (x=? AND y=?)*}
138 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
141 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
144 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
147 # New for SQLite version 2.1: Verify that that inequality constraints
148 # are used correctly.
151 count {SELECT w FROM t1 WHERE x=3 AND y<100}
154 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
157 count {SELECT w FROM t1 WHERE 3=x AND y<100}
160 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
163 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
166 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
169 count {SELECT w FROM t1 WHERE x=3 AND y>225}
172 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
175 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
178 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
181 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
184 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
187 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
190 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
193 # Need to work on optimizing the BETWEEN operator.
195 # do_test where-1.26 {
196 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
200 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
204 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
207 count {SELECT w FROM t1 WHERE y==121}
212 count {SELECT w FROM t1 WHERE w>97}
215 count {SELECT w FROM t1 WHERE w>=97}
218 count {SELECT w FROM t1 WHERE w==97}
220 do_test where-1.33.1 {
221 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
223 do_test where-1.33.2 {
224 count {SELECT w FROM t1 WHERE w<98 AND w==97}
226 do_test where-1.33.3 {
227 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
229 do_test where-1.33.4 {
230 count {SELECT w FROM t1 WHERE w>96 AND w==97}
232 do_test where-1.33.5 {
233 count {SELECT w FROM t1 WHERE w==97 AND w==97}
236 count {SELECT w FROM t1 WHERE w+1==98}
239 count {SELECT w FROM t1 WHERE w<3}
242 count {SELECT w FROM t1 WHERE w<=3}
245 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
249 count {SELECT (w) FROM t1 WHERE (w)>(97)}
252 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
255 count {SELECT (w) FROM t1 WHERE (w)==(97)}
258 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
262 # Do the same kind of thing except use a join as the data source.
266 SELECT w, p FROM t2, t1
267 WHERE x=q AND y=s AND r=8977
272 SELECT w, p FROM t2, t1
273 WHERE x=q AND s=y AND r=8977
278 SELECT w, p FROM t2, t1
279 WHERE x=q AND s=y AND r=8977 AND w>10
284 SELECT w, p FROM t2, t1
285 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
290 SELECT w, p FROM t2, t1
291 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
296 SELECT w, p FROM t2, t1
297 WHERE x=q AND p=77 AND s=y AND w>5
302 SELECT w, p FROM t1, t2
303 WHERE x=q AND p>77 AND s=y AND w=5
307 # Lets do a 3-way join.
311 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
312 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
317 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
318 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
323 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
324 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
328 # Test to see that the special case of a constant WHERE clause is
333 SELECT * FROM t1 WHERE 0
338 SELECT * FROM t1 WHERE 1 LIMIT 1
363 SELECT count(*) FROM t1 WHERE t1.w
367 # Verify that IN operators in a WHERE clause are handled correctly.
368 # Omit these tests if the build is not capable of sub-queries.
373 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
375 } {1 0 4 2 1 9 3 1 16 4}
378 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
380 } {1 0 4 2 1 9 3 1 16 102}
383 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
385 } {1 0 4 2 1 9 3 1 16 13}
388 SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
390 } {1 0 4 2 1 9 3 1 16 13}
393 SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
395 } {1 0 4 2 1 9 3 1 16 13}
398 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
400 } {3 1 16 2 1 9 1 0 4 12}
403 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
405 } {1 0 4 2 1 9 3 1 16 102}
408 SELECT * FROM t1 WHERE rowid IN
409 (select rowid from t1 where rowid IN (-1,2,4))
415 SELECT * FROM t1 WHERE rowid+0 IN
416 (select rowid from t1 where rowid IN (-1,2,4))
422 SELECT * FROM t1 WHERE w IN
423 (select rowid from t1 where rowid IN (-1,2,4))
429 SELECT * FROM t1 WHERE w+0 IN
430 (select rowid from t1 where rowid IN (-1,2,4))
436 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
441 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
446 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
448 } {79 6 6400 89 6 8100 199}
451 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
453 } {79 6 6400 89 6 8100 7}
456 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
461 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
466 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
469 do_test where-5.100 {
471 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
474 } {2 1 9 54 5 3025 62 5 3969}
475 do_test where-5.101 {
477 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
478 ORDER BY x DESC, y DESC
480 } {62 5 3969 54 5 3025 2 1 9}
481 do_test where-5.102 {
483 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
486 } {54 5 3025 62 5 3969 2 1 9}
487 do_test where-5.103 {
489 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
492 } {2 1 9 62 5 3969 54 5 3025}
495 # This procedure executes the SQL. Then it checks to see if the OP_Sort
496 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
497 # to the result. If no OP_Sort happened, then "nosort" is appended.
499 # This procedure is used to check to make sure sorting is or is not
500 # occurring as expected.
503 set data [execsql $sql]
504 if {[db status sort]} {set x sort} {set x nosort}
508 # Check out the logic that attempts to implement the ORDER BY clause
509 # using an index rather than by sorting.
513 CREATE TABLE t3(a,b,c);
514 CREATE INDEX t3a ON t3(a);
515 CREATE INDEX t3bc ON t3(b,c);
516 CREATE INDEX t3acb ON t3(a,c,b);
517 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
518 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
520 } {100 5050 5050 348550}
523 SELECT * FROM t3 ORDER BY a LIMIT 3
525 } {1 100 4 2 99 9 3 98 16 nosort}
528 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
530 } {1 100 4 2 99 9 3 98 16 sort}
533 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
535 } {1 100 4 2 99 9 3 98 16 nosort}
538 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
540 } {1 100 4 2 99 9 3 98 16 nosort}
543 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
545 } {1 100 4 2 99 9 3 98 16 nosort}
548 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
550 } {1 100 4 2 99 9 3 98 16 nosort}
554 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
556 } {1 100 4 2 99 9 3 98 16 nosort}
559 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a DESC LIMIT 3
561 } {9 92 100 7 94 64 5 96 36 nosort}
563 do_test where-6.9.1 {
565 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
568 do_test where-6.9.1.1 {
570 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
573 do_test where-6.9.1.2 {
575 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
578 do_test where-6.9.2 {
580 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
583 do_test where-6.9.3 {
585 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
588 do_test where-6.9.4 {
590 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
593 do_test where-6.9.5 {
595 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
598 do_test where-6.9.6 {
600 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
603 do_test where-6.9.7 {
605 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
608 do_test where-6.9.8 {
610 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
613 do_test where-6.9.9 {
615 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
620 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
625 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
630 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
635 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
637 } {100 1 10201 99 2 10000 98 3 9801 nosort}
638 do_test where-6.13.1 {
640 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
642 } {100 1 10201 99 2 10000 98 3 9801 sort}
645 SELECT * FROM t3 ORDER BY b LIMIT 3
647 } {100 1 10201 99 2 10000 98 3 9801 nosort}
650 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
652 } {1 0 2 1 3 1 nosort}
655 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
660 SELECT y FROM t1 ORDER BY w LIMIT 3;
665 SELECT y FROM t1 ORDER BY rowid LIMIT 3;
670 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
675 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
680 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
685 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
690 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
695 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
700 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
705 # Tests for reverse-order sorting.
709 SELECT w FROM t1 WHERE x=3 ORDER BY y;
711 } {8 9 10 11 12 13 14 15 nosort}
714 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
716 } {15 14 13 12 11 10 9 8 nosort}
719 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
724 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
729 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
731 } {15 14 13 12 11 nosort}
734 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
736 } {15 14 13 12 11 10 nosort}
739 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
744 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
746 } {13 12 11 10 nosort}
749 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
754 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
759 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
764 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
766 } {10 11 12 13 nosort}
769 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
774 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
779 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
784 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
789 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
794 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
799 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
804 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
809 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
814 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
819 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
824 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
829 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
834 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
839 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
844 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
849 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
854 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
859 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
861 } {10201 10000 9801 nosort}
864 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
869 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
874 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
879 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
885 CREATE TABLE t4 AS SELECT * FROM t1;
886 CREATE INDEX i4xy ON t4(x,y);
889 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
897 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
901 # Make sure searches with an index work with an empty table.
905 CREATE TABLE t5(x PRIMARY KEY);
906 SELECT * FROM t5 WHERE x<10;
911 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
916 SELECT * FROM t5 WHERE x=10;
922 SELECT 1 WHERE abs(random())<0
926 proc tclvar_func {vname} {return [set ::$vname]}
927 db function tclvar tclvar_func
930 SELECT count(*) FROM t1 WHERE tclvar('v1');
936 SELECT count(*) FROM t1 WHERE tclvar('v1');
941 proc tclvar_func {vname} {
947 SELECT count(*) FROM t1 WHERE tclvar('v1');
951 # Ticket #1376. The query below was causing a segfault.
952 # The problem was the age-old error of calling realloc() on an
953 # array while there are still pointers to individual elements of
958 CREATE TABLE t99(Dte INT, X INT);
959 DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
960 (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
961 (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
962 (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
963 (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
964 (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
965 (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
966 (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
967 (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
968 (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
969 (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
970 (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
971 (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
972 (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
973 (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
974 (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
975 (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
976 (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
977 (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
978 (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
979 (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
980 (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
984 # Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
989 CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
990 INSERT INTO t6 VALUES(1,'one');
991 INSERT INTO t6 VALUES(4,'four');
992 CREATE INDEX t6i1 ON t6(b);
995 SELECT * FROM t6 ORDER BY b;
997 } {4 four 1 one nosort}
1000 SELECT * FROM t6 ORDER BY b, a;
1002 } {4 four 1 one nosort}
1003 do_test where-12.3 {
1005 SELECT * FROM t6 ORDER BY a;
1007 } {1 one 4 four nosort}
1008 do_test where-12.4 {
1010 SELECT * FROM t6 ORDER BY a, b;
1012 } {1 one 4 four nosort}
1013 do_test where-12.5 {
1015 SELECT * FROM t6 ORDER BY b DESC;
1017 } {1 one 4 four nosort}
1018 do_test where-12.6 {
1020 SELECT * FROM t6 ORDER BY b DESC, a DESC;
1022 } {1 one 4 four nosort}
1023 do_test where-12.7 {
1025 SELECT * FROM t6 ORDER BY b DESC, a ASC;
1027 } {1 one 4 four sort}
1028 do_test where-12.8 {
1030 SELECT * FROM t6 ORDER BY b ASC, a DESC;
1032 } {4 four 1 one sort}
1033 do_test where-12.9 {
1035 SELECT * FROM t6 ORDER BY a DESC;
1037 } {4 four 1 one nosort}
1038 do_test where-12.10 {
1040 SELECT * FROM t6 ORDER BY a DESC, b DESC;
1042 } {4 four 1 one nosort}
1043 do_test where-12.11 {
1045 SELECT * FROM t6 ORDER BY a DESC, b ASC;
1047 } {4 four 1 one nosort}
1048 do_test where-12.12 {
1050 SELECT * FROM t6 ORDER BY a ASC, b DESC;
1052 } {1 one 4 four nosort}
1053 do_test where-13.1 {
1055 CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
1056 INSERT INTO t7 VALUES(1,'one');
1057 INSERT INTO t7 VALUES(4,'four');
1058 CREATE INDEX t7i1 ON t7(b);
1061 SELECT * FROM t7 ORDER BY b;
1063 } {4 four 1 one nosort}
1064 do_test where-13.2 {
1066 SELECT * FROM t7 ORDER BY b, a;
1068 } {4 four 1 one nosort}
1069 do_test where-13.3 {
1071 SELECT * FROM t7 ORDER BY a;
1073 } {1 one 4 four nosort}
1074 do_test where-13.4 {
1076 SELECT * FROM t7 ORDER BY a, b;
1078 } {1 one 4 four nosort}
1079 do_test where-13.5 {
1081 SELECT * FROM t7 ORDER BY b DESC;
1083 } {1 one 4 four nosort}
1084 do_test where-13.6 {
1086 SELECT * FROM t7 ORDER BY b DESC, a DESC;
1088 } {1 one 4 four nosort}
1089 do_test where-13.7 {
1091 SELECT * FROM t7 ORDER BY b DESC, a ASC;
1093 } {1 one 4 four sort}
1094 do_test where-13.8 {
1096 SELECT * FROM t7 ORDER BY b ASC, a DESC;
1098 } {4 four 1 one sort}
1099 do_test where-13.9 {
1101 SELECT * FROM t7 ORDER BY a DESC;
1103 } {4 four 1 one nosort}
1104 do_test where-13.10 {
1106 SELECT * FROM t7 ORDER BY a DESC, b DESC;
1108 } {4 four 1 one nosort}
1109 do_test where-13.11 {
1111 SELECT * FROM t7 ORDER BY a DESC, b ASC;
1113 } {4 four 1 one nosort}
1114 do_test where-13.12 {
1116 SELECT * FROM t7 ORDER BY a ASC, b DESC;
1118 } {1 one 4 four nosort}
1122 # When optimizing out ORDER BY clauses, make sure that trailing terms
1123 # of the ORDER BY clause do not reference other tables in a join.
1125 if {[permutation] != "no_optimization"} {
1126 do_test where-14.1 {
1128 CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE, c CHAR(100));
1129 INSERT INTO t8(a,b) VALUES(1,'one');
1130 INSERT INTO t8(a,b) VALUES(4,'four');
1133 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
1135 } {1/4 1/1 4/4 4/1 nosort}
1136 do_test where-14.2 {
1138 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
1140 } {1/1 1/4 4/1 4/4 nosort}
1141 do_test where-14.3 {
1143 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
1145 } {1/4 1/1 4/4 4/1 nosort}
1146 do_test where-14.4 {
1148 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
1150 } {1/4 1/1 4/4 4/1 nosort}
1151 do_test where-14.5 {
1152 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
1154 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
1156 } {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
1157 do_test where-14.6 {
1158 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
1160 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
1162 } {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
1163 do_test where-14.7 {
1165 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
1167 } {4/1 4/4 1/1 1/4 sort}
1168 do_test where-14.7.1 {
1170 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
1172 } {4/1 4/4 1/1 1/4 sort}
1173 do_test where-14.7.2 {
1175 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
1177 } {4/4 4/1 1/4 1/1 nosort}
1178 do_test where-14.8 {
1180 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
1182 } {4/4 4/1 1/4 1/1 sort}
1183 do_test where-14.9 {
1185 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
1187 } {4/4 4/1 1/4 1/1 sort}
1188 do_test where-14.10 {
1190 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
1192 } {4/1 4/4 1/1 1/4 sort}
1193 do_test where-14.11 {
1195 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
1197 } {4/1 4/4 1/1 1/4 sort}
1198 do_test where-14.12 {
1200 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
1202 } {4/4 4/1 1/4 1/1 sort}
1203 } ;# {permutation != "no_optimization"}
1207 # There was a crash that could occur when a where clause contains an
1208 # alias for an expression in the result set, and that expression retrieves
1209 # a column of the second or subsequent table in a join.
1211 do_test where-15.1 {
1213 CREATE TEMP TABLE t1 (a, b, c, d, e);
1214 CREATE TEMP TABLE t2 (f);
1215 SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
1221 # The branch of code in where.c that generated rowid lookups was
1222 # incorrectly deallocating a constant register, meaning that if the
1223 # vdbe code ran more than once, the second time around the constant
1224 # value may have been clobbered by some other value.
1226 do_test where-16.1 {
1228 CREATE TABLE a1(id INTEGER PRIMARY KEY, v);
1229 CREATE TABLE a2(id INTEGER PRIMARY KEY, v);
1230 INSERT INTO a1 VALUES(1, 'one');
1231 INSERT INTO a1 VALUES(2, 'two');
1232 INSERT INTO a2 VALUES(1, 'one');
1233 INSERT INTO a2 VALUES(2, 'two');
1236 do_test where-16.2 {
1238 SELECT * FROM a2 CROSS JOIN a1 WHERE a1.id=1 AND a1.v='one';
1240 } {1 one 1 one 2 two 1 one}
1242 # The actual problem reported in #3408.
1243 do_test where-16.3 {
1245 CREATE TEMP TABLE foo(idx INTEGER);
1246 INSERT INTO foo VALUES(1);
1247 INSERT INTO foo VALUES(1);
1248 INSERT INTO foo VALUES(1);
1249 INSERT INTO foo VALUES(2);
1250 INSERT INTO foo VALUES(2);
1251 CREATE TEMP TABLE bar(stuff INTEGER);
1252 INSERT INTO bar VALUES(100);
1253 INSERT INTO bar VALUES(200);
1254 INSERT INTO bar VALUES(300);
1257 do_test where-16.4 {
1259 SELECT bar.RowID id FROM foo, bar WHERE foo.idx = bar.RowID AND id = 2;
1263 integrity_check {where-99.0}
1265 #---------------------------------------------------------------------
1266 # These tests test that a bug surrounding the use of ForceInt has been
1269 do_test where-17.1 {
1271 CREATE TABLE tbooking (
1272 id INTEGER PRIMARY KEY,
1273 eventtype INTEGER NOT NULL
1275 INSERT INTO tbooking VALUES(42, 3);
1276 INSERT INTO tbooking VALUES(43, 4);
1279 do_test where-17.2 {
1283 WHERE a.eventtype=3;
1286 do_test where-17.3 {
1288 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1290 WHERE a.eventtype=3;
1293 do_test where-17.4 {
1295 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1296 FROM (SELECT 1.5 AS id) AS a
1299 do_test where-17.5 {
1301 CREATE TABLE tother(a, b);
1302 INSERT INTO tother VALUES(1, 3.7);
1303 SELECT id, a FROM tbooking, tother WHERE id>a;
1307 # Ticket [be84e357c035d068135f20bcfe82761bbf95006b] 2013-09-03
1308 # Segfault during query involving LEFT JOIN column in the ORDER BY clause.
1310 do_execsql_test where-18.1 {
1311 CREATE TABLE t181(a);
1312 CREATE TABLE t182(b,c);
1313 INSERT INTO t181 VALUES(1);
1314 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL;
1316 do_execsql_test where-18.2 {
1317 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1319 do_execsql_test where-18.3 {
1320 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c;
1322 do_execsql_test where-18.4 {
1323 INSERT INTO t181 VALUES(1),(1),(1),(1);
1324 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1326 do_execsql_test where-18.5 {
1327 INSERT INTO t181 VALUES(2);
1328 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL, +a;
1330 do_execsql_test where-18.6 {
1331 INSERT INTO t181 VALUES(2);
1332 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +a, +c IS NULL;