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"); -- Verify quoted identifier names
46 CREATE INDEX i1xy ON t1(`x`,'y' ASC); -- Old MySQL compatibility
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_test where-1.1.1b {
69 count {SELECT x, y, w FROM t1 WHERE w IS 10}
71 do_eqp_test where-1.1.2 {
72 SELECT x, y, w FROM t1 WHERE w=10
73 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
74 do_eqp_test where-1.1.2b {
75 SELECT x, y, w FROM t1 WHERE w IS 10
76 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
81 db eval {SELECT x, y, w FROM t1 WHERE +w=10}
86 do_eqp_test where-1.1.6 {
87 SELECT x, y, w FROM t1 WHERE +w=10
90 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
92 do_eqp_test where-1.1.8 {
93 SELECT x, y, w AS abc FROM t1 WHERE abc=10
94 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
99 count {SELECT x, y, w FROM t1 WHERE w=11}
101 do_test where-1.2.2 {
102 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
104 do_test where-1.3.1 {
105 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
107 do_test where-1.3.2 {
108 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
110 do_test where-1.3.3 {
111 count {SELECT x, y, w AS abc FROM t1 WHERE 11 IS abc}
113 do_test where-1.4.1 {
114 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
116 do_test where-1.4.1b {
117 count {SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2}
119 do_eqp_test where-1.4.2 {
120 SELECT w, x, y FROM t1 WHERE 11=w AND x>2
121 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
122 do_eqp_test where-1.4.2b {
123 SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2
124 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
125 do_test where-1.4.3 {
126 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
128 do_eqp_test where-1.4.4 {
129 SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2
130 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
132 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
134 do_eqp_test where-1.5.2 {
135 SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2
136 } {*SEARCH TABLE t1 USING INDEX i1w (w=?)*}
138 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
141 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
144 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
146 do_eqp_test where-1.8.2 {
147 SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3
148 } {*SEARCH TABLE t1 USING INDEX i1xy (x=? AND y=?)*}
149 do_eqp_test where-1.8.3 {
150 SELECT x, y FROM t1 WHERE y=144 AND x=3
151 } {*SEARCH TABLE t1 USING COVERING INDEX i1xy (x=? AND y=?)*}
153 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
156 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
159 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
161 do_test where-1.11b {
162 count {SELECT x, y FROM t1 WHERE x IS 3 AND y IS 100 AND w<10}
165 # New for SQLite version 2.1: Verify that that inequality constraints
166 # are used correctly.
169 count {SELECT w FROM t1 WHERE x=3 AND y<100}
171 do_test where-1.12b {
172 count {SELECT w FROM t1 WHERE x IS 3 AND y<100}
175 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
178 count {SELECT w FROM t1 WHERE 3=x AND y<100}
180 do_test where-1.14b {
181 count {SELECT w FROM t1 WHERE 3 IS x AND y<100}
184 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
187 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
190 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
193 count {SELECT w FROM t1 WHERE x=3 AND y>225}
195 do_test where-1.18b {
196 count {SELECT w FROM t1 WHERE x IS 3 AND y>225}
199 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
202 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
205 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
208 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
210 do_test where-1.22b {
211 count {SELECT w FROM t1 WHERE x IS 3 AND y>121 AND y<196}
214 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
217 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
220 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
223 # Need to work on optimizing the BETWEEN operator.
225 # do_test where-1.26 {
226 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
230 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
234 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
237 count {SELECT w FROM t1 WHERE y==121}
242 count {SELECT w FROM t1 WHERE w>97}
245 count {SELECT w FROM t1 WHERE w>=97}
248 count {SELECT w FROM t1 WHERE w==97}
250 do_test where-1.33.1 {
251 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
253 do_test where-1.33.2 {
254 count {SELECT w FROM t1 WHERE w<98 AND w==97}
256 do_test where-1.33.3 {
257 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
259 do_test where-1.33.4 {
260 count {SELECT w FROM t1 WHERE w>96 AND w==97}
262 do_test where-1.33.5 {
263 count {SELECT w FROM t1 WHERE w==97 AND w==97}
266 count {SELECT w FROM t1 WHERE w+1==98}
269 count {SELECT w FROM t1 WHERE w<3}
272 count {SELECT w FROM t1 WHERE w<=3}
275 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
279 count {SELECT (w) FROM t1 WHERE (w)>(97)}
282 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
285 count {SELECT (w) FROM t1 WHERE (w)==(97)}
288 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
292 # Do the same kind of thing except use a join as the data source.
296 SELECT w, p FROM t2, t1
297 WHERE x=q AND y=s AND r=8977
302 SELECT w, p FROM t2, t1
303 WHERE x=q AND s=y AND r=8977
308 SELECT w, p FROM t2, t1
309 WHERE x=q AND s=y AND r=8977 AND w>10
314 SELECT w, p FROM t2, t1
315 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
320 SELECT w, p FROM t2, t1
321 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
326 SELECT w, p FROM t2, t1
327 WHERE x=q AND p=77 AND s=y AND w>5
332 SELECT w, p FROM t1, t2
333 WHERE x=q AND p>77 AND s=y AND w=5
337 # Lets do a 3-way join.
341 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
342 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
347 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
348 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
353 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
354 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
358 # Test to see that the special case of a constant WHERE clause is
363 SELECT * FROM t1 WHERE 0
368 SELECT * FROM t1 WHERE 1 LIMIT 1
393 SELECT count(*) FROM t1 WHERE t1.w
397 # Verify that IN operators in a WHERE clause are handled correctly.
398 # Omit these tests if the build is not capable of sub-queries.
403 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
405 } {1 0 4 2 1 9 3 1 16 4}
408 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
410 } {1 0 4 2 1 9 3 1 16 102}
413 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
415 } {1 0 4 2 1 9 3 1 16 12}
418 SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
420 } {1 0 4 2 1 9 3 1 16 12}
423 SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
425 } {1 0 4 2 1 9 3 1 16 12}
428 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
430 } {3 1 16 2 1 9 1 0 4 11}
433 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
435 } {1 0 4 2 1 9 3 1 16 102}
438 SELECT * FROM t1 WHERE rowid IN
439 (select rowid from t1 where rowid IN (-1,2,4))
445 SELECT * FROM t1 WHERE rowid+0 IN
446 (select rowid from t1 where rowid IN (-1,2,4))
452 SELECT * FROM t1 WHERE w IN
453 (select rowid from t1 where rowid IN (-1,2,4))
459 SELECT * FROM t1 WHERE w+0 IN
460 (select rowid from t1 where rowid IN (-1,2,4))
466 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
471 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
476 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
478 } {79 6 6400 89 6 8100 199}
481 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
483 } {79 6 6400 89 6 8100 7}
486 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
491 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
496 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
499 do_test where-5.100 {
501 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
504 } {2 1 9 54 5 3025 62 5 3969}
505 do_test where-5.101 {
507 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
508 ORDER BY x DESC, y DESC
510 } {62 5 3969 54 5 3025 2 1 9}
511 do_test where-5.102 {
513 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
516 } {54 5 3025 62 5 3969 2 1 9}
517 do_test where-5.103 {
519 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
522 } {2 1 9 62 5 3969 54 5 3025}
525 # This procedure executes the SQL. Then it checks to see if the OP_Sort
526 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
527 # to the result. If no OP_Sort happened, then "nosort" is appended.
529 # This procedure is used to check to make sure sorting is or is not
530 # occurring as expected.
533 set data [execsql $sql]
534 if {[db status sort]} {set x sort} {set x nosort}
538 # Check out the logic that attempts to implement the ORDER BY clause
539 # using an index rather than by sorting.
543 CREATE TABLE t3(a,b,c);
544 CREATE INDEX t3a ON t3(a);
545 CREATE INDEX t3bc ON t3(b,c);
546 CREATE INDEX t3acb ON t3(a,c,b);
547 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
548 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
550 } {100 5050 5050 348550}
553 SELECT * FROM t3 ORDER BY a LIMIT 3
555 } {1 100 4 2 99 9 3 98 16 nosort}
558 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
560 } {1 100 4 2 99 9 3 98 16 sort}
563 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
565 } {1 100 4 2 99 9 3 98 16 nosort}
568 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
570 } {1 100 4 2 99 9 3 98 16 nosort}
573 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
575 } {1 100 4 2 99 9 3 98 16 nosort}
576 do_test where-6.7.1 {
578 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 10
580 } {/1 100 4 2 99 9 3 98 16 .* nosort/}
581 do_test where-6.7.2 {
583 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 1
589 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
591 } {1 100 4 2 99 9 3 98 16 nosort}
594 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a DESC LIMIT 3
596 } {9 92 100 7 94 64 5 96 36 nosort}
598 do_test where-6.9.1 {
600 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
603 do_test where-6.9.1.1 {
605 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
608 do_test where-6.9.1.2 {
610 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
613 do_test where-6.9.2 {
615 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
618 do_test where-6.9.3 {
620 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
623 do_test where-6.9.4 {
625 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
628 do_test where-6.9.5 {
630 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
633 do_test where-6.9.6 {
635 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
638 do_test where-6.9.7 {
640 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
643 do_test where-6.9.8 {
645 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
648 do_test where-6.9.9 {
650 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
655 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
660 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
665 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
670 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
672 } {100 1 10201 99 2 10000 98 3 9801 nosort}
673 do_test where-6.13.1 {
675 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
677 } {100 1 10201 99 2 10000 98 3 9801 sort}
680 SELECT * FROM t3 ORDER BY b LIMIT 3
682 } {100 1 10201 99 2 10000 98 3 9801 nosort}
685 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
687 } {1 0 2 1 3 1 nosort}
690 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
695 SELECT y FROM t1 ORDER BY w LIMIT 3;
700 SELECT y FROM t1 ORDER BY rowid LIMIT 3;
705 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
710 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
715 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
720 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
725 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
730 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
735 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
740 # Tests for reverse-order sorting.
744 SELECT w FROM t1 WHERE x=3 ORDER BY y;
746 } {8 9 10 11 12 13 14 15 nosort}
749 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
751 } {15 14 13 12 11 10 9 8 nosort}
754 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
759 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
764 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
766 } {15 14 13 12 11 nosort}
769 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
771 } {15 14 13 12 11 10 nosort}
774 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
779 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
781 } {13 12 11 10 nosort}
784 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
789 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
794 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
799 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
801 } {10 11 12 13 nosort}
804 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
809 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
814 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
819 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
824 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
829 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
834 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
839 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
844 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
849 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
854 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
859 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
864 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
869 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
874 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
879 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
884 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
889 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
894 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
896 } {10201 10000 9801 nosort}
899 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
904 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
909 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
914 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
920 CREATE TABLE t4 AS SELECT * FROM t1;
921 CREATE INDEX i4xy ON t4(x,y);
924 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
932 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
936 # Make sure searches with an index work with an empty table.
940 CREATE TABLE t5(x PRIMARY KEY);
941 SELECT * FROM t5 WHERE x<10;
946 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
951 SELECT * FROM t5 WHERE x=10;
957 SELECT 1 WHERE abs(random())<0
961 proc tclvar_func {vname} {return [set ::$vname]}
962 db function tclvar tclvar_func
965 SELECT count(*) FROM t1 WHERE tclvar('v1');
971 SELECT count(*) FROM t1 WHERE tclvar('v1');
976 proc tclvar_func {vname} {
982 SELECT count(*) FROM t1 WHERE tclvar('v1');
986 # Ticket #1376. The query below was causing a segfault.
987 # The problem was the age-old error of calling realloc() on an
988 # array while there are still pointers to individual elements of
993 CREATE TABLE t99(Dte INT, X INT);
994 DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
995 (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
996 (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
997 (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
998 (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
999 (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
1000 (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
1001 (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
1002 (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
1003 (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
1004 (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
1005 (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
1006 (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
1007 (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
1008 (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
1009 (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
1010 (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
1011 (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
1012 (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
1013 (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
1014 (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
1015 (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
1019 # Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
1022 do_test where-12.1 {
1024 CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
1025 INSERT INTO t6 VALUES(1,'one');
1026 INSERT INTO t6 VALUES(4,'four');
1027 CREATE INDEX t6i1 ON t6(b);
1030 SELECT * FROM t6 ORDER BY b;
1032 } {4 four 1 one nosort}
1033 do_test where-12.2 {
1035 SELECT * FROM t6 ORDER BY b, a;
1037 } {4 four 1 one nosort}
1038 do_test where-12.3 {
1040 SELECT * FROM t6 ORDER BY a;
1042 } {1 one 4 four nosort}
1043 do_test where-12.4 {
1045 SELECT * FROM t6 ORDER BY a, b;
1047 } {1 one 4 four nosort}
1048 do_test where-12.5 {
1050 SELECT * FROM t6 ORDER BY b DESC;
1052 } {1 one 4 four nosort}
1053 do_test where-12.6 {
1055 SELECT * FROM t6 ORDER BY b DESC, a DESC;
1057 } {1 one 4 four nosort}
1058 do_test where-12.7 {
1060 SELECT * FROM t6 ORDER BY b DESC, a ASC;
1062 } {1 one 4 four sort}
1063 do_test where-12.8 {
1065 SELECT * FROM t6 ORDER BY b ASC, a DESC;
1067 } {4 four 1 one sort}
1068 do_test where-12.9 {
1070 SELECT * FROM t6 ORDER BY a DESC;
1072 } {4 four 1 one nosort}
1073 do_test where-12.10 {
1075 SELECT * FROM t6 ORDER BY a DESC, b DESC;
1077 } {4 four 1 one nosort}
1078 do_test where-12.11 {
1080 SELECT * FROM t6 ORDER BY a DESC, b ASC;
1082 } {4 four 1 one nosort}
1083 do_test where-12.12 {
1085 SELECT * FROM t6 ORDER BY a ASC, b DESC;
1087 } {1 one 4 four nosort}
1088 do_test where-13.1 {
1090 CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
1091 INSERT INTO t7 VALUES(1,'one');
1092 INSERT INTO t7 VALUES(4,'four');
1093 CREATE INDEX t7i1 ON t7(b);
1096 SELECT * FROM t7 ORDER BY b;
1098 } {4 four 1 one nosort}
1099 do_test where-13.2 {
1101 SELECT * FROM t7 ORDER BY b, a;
1103 } {4 four 1 one nosort}
1104 do_test where-13.3 {
1106 SELECT * FROM t7 ORDER BY a;
1108 } {1 one 4 four nosort}
1109 do_test where-13.4 {
1111 SELECT * FROM t7 ORDER BY a, b;
1113 } {1 one 4 four nosort}
1114 do_test where-13.5 {
1116 SELECT * FROM t7 ORDER BY b DESC;
1118 } {1 one 4 four nosort}
1119 do_test where-13.6 {
1121 SELECT * FROM t7 ORDER BY b DESC, a DESC;
1123 } {1 one 4 four nosort}
1124 do_test where-13.7 {
1126 SELECT * FROM t7 ORDER BY b DESC, a ASC;
1128 } {1 one 4 four sort}
1129 do_test where-13.8 {
1131 SELECT * FROM t7 ORDER BY b ASC, a DESC;
1133 } {4 four 1 one sort}
1134 do_test where-13.9 {
1136 SELECT * FROM t7 ORDER BY a DESC;
1138 } {4 four 1 one nosort}
1139 do_test where-13.10 {
1141 SELECT * FROM t7 ORDER BY a DESC, b DESC;
1143 } {4 four 1 one nosort}
1144 do_test where-13.11 {
1146 SELECT * FROM t7 ORDER BY a DESC, b ASC;
1148 } {4 four 1 one nosort}
1149 do_test where-13.12 {
1151 SELECT * FROM t7 ORDER BY a ASC, b DESC;
1153 } {1 one 4 four nosort}
1157 # When optimizing out ORDER BY clauses, make sure that trailing terms
1158 # of the ORDER BY clause do not reference other tables in a join.
1160 if {[permutation] != "no_optimization"} {
1161 do_test where-14.1 {
1163 CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE, c CHAR(100));
1164 INSERT INTO t8(a,b) VALUES(1,'one');
1165 INSERT INTO t8(a,b) VALUES(4,'four');
1168 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
1170 } {1/4 1/1 4/4 4/1 nosort}
1171 do_test where-14.2 {
1173 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
1175 } {1/1 1/4 4/1 4/4 nosort}
1176 do_test where-14.3 {
1178 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
1180 } {1/4 1/1 4/4 4/1 nosort}
1181 do_test where-14.4 {
1183 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
1185 } {1/4 1/1 4/4 4/1 nosort}
1186 do_test where-14.5 {
1187 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
1189 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
1191 } {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
1192 do_test where-14.6 {
1193 # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
1195 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
1197 } {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
1198 do_test where-14.7 {
1200 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
1202 } {4/1 4/4 1/1 1/4 sort}
1203 do_test where-14.7.1 {
1205 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
1207 } {4/1 4/4 1/1 1/4 sort}
1208 do_test where-14.7.2 {
1210 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
1212 } {4/4 4/1 1/4 1/1 nosort}
1213 do_test where-14.8 {
1215 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
1217 } {4/4 4/1 1/4 1/1 sort}
1218 do_test where-14.9 {
1220 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
1222 } {4/4 4/1 1/4 1/1 sort}
1223 do_test where-14.10 {
1225 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
1227 } {4/1 4/4 1/1 1/4 sort}
1228 do_test where-14.11 {
1230 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
1232 } {4/1 4/4 1/1 1/4 sort}
1233 do_test where-14.12 {
1235 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
1237 } {4/4 4/1 1/4 1/1 sort}
1238 } ;# {permutation != "no_optimization"}
1242 # There was a crash that could occur when a where clause contains an
1243 # alias for an expression in the result set, and that expression retrieves
1244 # a column of the second or subsequent table in a join.
1246 do_test where-15.1 {
1248 CREATE TEMP TABLE t1 (a, b, c, d, e);
1249 CREATE TEMP TABLE t2 (f);
1250 SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
1256 # The branch of code in where.c that generated rowid lookups was
1257 # incorrectly deallocating a constant register, meaning that if the
1258 # vdbe code ran more than once, the second time around the constant
1259 # value may have been clobbered by some other value.
1261 do_test where-16.1 {
1263 CREATE TABLE a1(id INTEGER PRIMARY KEY, v);
1264 CREATE TABLE a2(id INTEGER PRIMARY KEY, v);
1265 INSERT INTO a1 VALUES(1, 'one');
1266 INSERT INTO a1 VALUES(2, 'two');
1267 INSERT INTO a2 VALUES(1, 'one');
1268 INSERT INTO a2 VALUES(2, 'two');
1271 do_test where-16.2 {
1273 SELECT * FROM a2 CROSS JOIN a1 WHERE a1.id=1 AND a1.v='one';
1275 } {1 one 1 one 2 two 1 one}
1277 # The actual problem reported in #3408.
1278 do_test where-16.3 {
1280 CREATE TEMP TABLE foo(idx INTEGER);
1281 INSERT INTO foo VALUES(1);
1282 INSERT INTO foo VALUES(1);
1283 INSERT INTO foo VALUES(1);
1284 INSERT INTO foo VALUES(2);
1285 INSERT INTO foo VALUES(2);
1286 CREATE TEMP TABLE bar(stuff INTEGER);
1287 INSERT INTO bar VALUES(100);
1288 INSERT INTO bar VALUES(200);
1289 INSERT INTO bar VALUES(300);
1292 do_test where-16.4 {
1294 SELECT bar.RowID id FROM foo, bar WHERE foo.idx = bar.RowID AND id = 2;
1298 integrity_check {where-99.0}
1300 #---------------------------------------------------------------------
1301 # These tests test that a bug surrounding the use of ForceInt has been
1304 do_test where-17.1 {
1306 CREATE TABLE tbooking (
1307 id INTEGER PRIMARY KEY,
1308 eventtype INTEGER NOT NULL
1310 INSERT INTO tbooking VALUES(42, 3);
1311 INSERT INTO tbooking VALUES(43, 4);
1314 do_test where-17.2 {
1318 WHERE a.eventtype=3;
1321 do_test where-17.3 {
1323 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1325 WHERE a.eventtype=3;
1328 do_test where-17.4 {
1330 SELECT a.id, (SELECT b.id FROM tbooking AS b WHERE b.id>a.id)
1331 FROM (SELECT 1.5 AS id) AS a
1334 do_test where-17.5 {
1336 CREATE TABLE tother(a, b);
1337 INSERT INTO tother VALUES(1, 3.7);
1338 SELECT id, a FROM tbooking, tother WHERE id>a;
1342 # Ticket [be84e357c035d068135f20bcfe82761bbf95006b] 2013-09-03
1343 # Segfault during query involving LEFT JOIN column in the ORDER BY clause.
1345 do_execsql_test where-18.1 {
1346 CREATE TABLE t181(a);
1347 CREATE TABLE t182(b,c);
1348 INSERT INTO t181 VALUES(1);
1349 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL;
1351 do_execsql_test where-18.2 {
1352 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1354 do_execsql_test where-18.3 {
1355 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c;
1357 do_execsql_test where-18.4 {
1358 INSERT INTO t181 VALUES(1),(1),(1),(1);
1359 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1361 do_execsql_test where-18.5 {
1362 INSERT INTO t181 VALUES(2);
1363 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL, +a;
1365 do_execsql_test where-18.6 {
1366 INSERT INTO t181 VALUES(2);
1367 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +a, +c IS NULL;
1370 # Make sure the OR optimization works on a JOIN
1372 do_execsql_test where-19.0 {
1373 CREATE TABLE t191(a INT UNIQUE NOT NULL, b INT UNIQUE NOT NULL,c,d);
1374 CREATE INDEX t191a ON t1(a);
1375 CREATE INDEX t191b ON t1(b);
1376 CREATE TABLE t192(x INTEGER PRIMARY KEY,y INT, z INT);
1379 SELECT t191.rowid FROM t192, t191 WHERE (a=y OR b=y) AND x=?1;
1380 } {/.* sqlite_autoindex_t191_1 .* sqlite_autoindex_t191_2 .*/}
1382 # 2018-04-24 ticket [https://www.sqlite.org/src/info/4ba5abf65c5b0f9a]
1383 # Index on expressions leads to an incorrect answer for a LEFT JOIN
1385 do_execsql_test where-20.0 {
1386 CREATE TABLE t201(x);
1387 CREATE TABLE t202(y, z);
1388 INSERT INTO t201 VALUES('key');
1389 INSERT INTO t202 VALUES('key', -1);
1390 CREATE INDEX t202i ON t202(y, ifnull(z, 0));
1391 SELECT count(*) FROM t201 LEFT JOIN t202 ON (x=y) WHERE ifnull(z, 0) >=0;
1394 do_execsql_test where-21.0 {
1395 CREATE TABLE t12(a, b, c);
1396 CREATE TABLE t13(x);
1397 CREATE INDEX t12ab ON t12(b, a);
1398 CREATE INDEX t12ac ON t12(c, a);
1400 INSERT INTO t12 VALUES(4, 0, 1);
1401 INSERT INTO t12 VALUES(4, 1, 0);
1402 INSERT INTO t12 VALUES(5, 0, 1);
1403 INSERT INTO t12 VALUES(5, 1, 0);
1405 INSERT INTO t13 VALUES(1), (2), (3), (4);
1407 do_execsql_test where-21.1 {
1408 SELECT * FROM t12 WHERE
1409 a = (SELECT * FROM (SELECT count(*) FROM t13 LIMIT 5) ORDER BY 1 LIMIT 10)
1416 # 2018-11-05: ticket [https://www.sqlite.org/src/tktview/65eb38f6e46de8c75e188a]
1417 # Incorrect result in LEFT JOIN when STAT4 is enabled.
1420 do_execsql_test where-22.1 {
1421 CREATE TABLE t1(a INT);
1422 CREATE INDEX t1a ON t1(a);
1423 INSERT INTO t1(a) VALUES(NULL),(NULL),(42),(NULL),(NULL);
1424 CREATE TABLE t2(dummy INT);
1425 SELECT count(*) FROM t1 LEFT JOIN t2 ON a IS NOT NULL;