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.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Build some test data
22 CREATE TABLE t1(w int, x int, y int);
23 CREATE TABLE t2(p int, q int, r int, s int);
25 for {set i 1} {$i<=100} {incr i} {
27 set x [expr {int(log($i)/log(2))}]
28 set y [expr {$i*$i + 2*$i + 1}]
29 execsql "INSERT INTO t1 VALUES($w,$x,$y)"
34 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
37 set maxy [execsql {select max(y) from t1}]
39 INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1;
44 CREATE INDEX i1w ON t1("w"); -- Verify quoted identifier names
45 CREATE INDEX i1xy ON t1(`x`,'y' ASC); -- Old MySQL compatibility
46 CREATE INDEX i2p ON t2(p);
47 CREATE INDEX i2r ON t2(r);
48 CREATE INDEX i2qs ON t2(q, s);
52 # Do an SQL statement. Append the search count to the end of the result.
55 set ::sqlite_search_count 0
56 return [concat [execsql $sql] $::sqlite_search_count]
59 # Verify that queries use an index. We are using the special variable
60 # "sqlite_search_count" which tallys the number of executions of MoveTo
61 # and Next operators in the VDBE. By verifing that the search count is
62 # small we can be assured that indices are being used properly.
65 count {SELECT x, y, w FROM t1 WHERE w=10}
67 do_test where-1.1.1b {
68 count {SELECT x, y, w FROM t1 WHERE w IS 10}
70 do_eqp_test where-1.1.2 {
71 SELECT x, y, w FROM t1 WHERE w=10
72 } {*SEARCH t1 USING INDEX i1w (w=?)*}
73 do_eqp_test where-1.1.2b {
74 SELECT x, y, w FROM t1 WHERE w IS 10
75 } {*SEARCH t1 USING INDEX i1w (w=?)*}
80 db eval {SELECT x, y, w FROM t1 WHERE +w=10}
85 do_eqp_test where-1.1.6 {
86 SELECT x, y, w FROM t1 WHERE +w=10
89 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
91 do_eqp_test where-1.1.8 {
92 SELECT x, y, w AS abc FROM t1 WHERE abc=10
93 } {*SEARCH t1 USING INDEX i1w (w=?)*}
98 count {SELECT x, y, w FROM t1 WHERE w=11}
100 do_test where-1.2.2 {
101 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
103 do_test where-1.3.1 {
104 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
106 do_test where-1.3.2 {
107 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
109 do_test where-1.3.3 {
110 count {SELECT x, y, w AS abc FROM t1 WHERE 11 IS abc}
112 do_test where-1.4.1 {
113 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
115 do_test where-1.4.1b {
116 count {SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2}
118 do_eqp_test where-1.4.2 {
119 SELECT w, x, y FROM t1 WHERE 11=w AND x>2
120 } {*SEARCH t1 USING INDEX i1w (w=?)*}
121 do_eqp_test where-1.4.2b {
122 SELECT w, x, y FROM t1 WHERE 11 IS w AND x>2
123 } {*SEARCH t1 USING INDEX i1w (w=?)*}
124 do_test where-1.4.3 {
125 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
127 do_eqp_test where-1.4.4 {
128 SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2
129 } {*SEARCH t1 USING INDEX i1w (w=?)*}
131 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
133 do_eqp_test where-1.5.2 {
134 SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2
135 } {*SEARCH t1 USING INDEX i1w (w=?)*}
137 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
140 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
143 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
145 do_eqp_test where-1.8.2 {
146 SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3
147 } {*SEARCH t1 USING INDEX i1xy (x=? AND y=?)*}
148 do_eqp_test where-1.8.3 {
149 SELECT x, y FROM t1 WHERE y=144 AND x=3
150 } {*SEARCH t1 USING COVERING INDEX i1xy (x=? AND y=?)*}
152 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
155 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
158 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
160 do_test where-1.11b {
161 count {SELECT x, y FROM t1 WHERE x IS 3 AND y IS 100 AND w<10}
164 # New for SQLite version 2.1: Verify that that inequality constraints
165 # are used correctly.
168 count {SELECT w FROM t1 WHERE x=3 AND y<100}
170 do_test where-1.12b {
171 count {SELECT w FROM t1 WHERE x IS 3 AND y<100}
174 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
177 count {SELECT w FROM t1 WHERE 3=x AND y<100}
179 do_test where-1.14b {
180 count {SELECT w FROM t1 WHERE 3 IS x AND y<100}
183 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
186 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
189 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
192 count {SELECT w FROM t1 WHERE x=3 AND y>225}
194 do_test where-1.18b {
195 count {SELECT w FROM t1 WHERE x IS 3 AND y>225}
198 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
201 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
204 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
207 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
209 do_test where-1.22b {
210 count {SELECT w FROM t1 WHERE x IS 3 AND y>121 AND y<196}
213 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
216 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
219 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
222 # Need to work on optimizing the BETWEEN operator.
224 # do_test where-1.26 {
225 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
229 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
233 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
236 count {SELECT w FROM t1 WHERE y==121}
241 count {SELECT w FROM t1 WHERE w>97}
244 count {SELECT w FROM t1 WHERE w>=97}
247 count {SELECT w FROM t1 WHERE w==97}
249 do_test where-1.33.1 {
250 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
252 do_test where-1.33.2 {
253 count {SELECT w FROM t1 WHERE w<98 AND w==97}
255 do_test where-1.33.3 {
256 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
258 do_test where-1.33.4 {
259 count {SELECT w FROM t1 WHERE w>96 AND w==97}
261 do_test where-1.33.5 {
262 count {SELECT w FROM t1 WHERE w==97 AND w==97}
265 count {SELECT w FROM t1 WHERE w+1==98}
268 count {SELECT w FROM t1 WHERE w<3}
271 count {SELECT w FROM t1 WHERE w<=3}
274 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
278 count {SELECT (w) FROM t1 WHERE (w)>(97)}
281 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
284 count {SELECT (w) FROM t1 WHERE (w)==(97)}
287 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
291 # Do the same kind of thing except use a join as the data source.
295 SELECT w, p FROM t2, t1
296 WHERE x=q AND y=s AND r=8977
301 SELECT w, p FROM t2, t1
302 WHERE x=q AND s=y AND r=8977
307 SELECT w, p FROM t2, t1
308 WHERE x=q AND s=y AND r=8977 AND w>10
313 SELECT w, p FROM t2, t1
314 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
319 SELECT w, p FROM t2, t1
320 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
325 SELECT w, p FROM t2, t1
326 WHERE x=q AND p=77 AND s=y AND w>5
331 SELECT w, p FROM t1, t2
332 WHERE x=q AND p>77 AND s=y AND w=5
336 # Lets do a 3-way join.
340 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
341 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
346 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
347 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
352 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
353 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
357 # Test to see that the special case of a constant WHERE clause is
362 SELECT * FROM t1 WHERE 0
367 SELECT * FROM t1 WHERE 1 LIMIT 1
392 SELECT count(*) FROM t1 WHERE t1.w
396 # Verify that IN operators in a WHERE clause are handled correctly.
397 # Omit these tests if the build is not capable of sub-queries.
402 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
404 } {1 0 4 2 1 9 3 1 16 4}
407 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
409 } {1 0 4 2 1 9 3 1 16 102}
412 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
414 } {1 0 4 2 1 9 3 1 16 12}
417 SELECT * FROM t1 WHERE w IN (3,-1,1,2) order by 1;
419 } {1 0 4 2 1 9 3 1 16 12}
422 SELECT * FROM t1 WHERE w IN (3,2,-1,1,2) order by 1;
424 } {1 0 4 2 1 9 3 1 16 12}
427 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1 DESC;
429 } {3 1 16 2 1 9 1 0 4 11}
432 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
434 } {1 0 4 2 1 9 3 1 16 102}
437 SELECT * FROM t1 WHERE rowid IN
438 (select rowid from t1 where rowid IN (-1,2,4))
444 SELECT * FROM t1 WHERE rowid+0 IN
445 (select rowid from t1 where rowid IN (-1,2,4))
451 SELECT * FROM t1 WHERE w IN
452 (select rowid from t1 where rowid IN (-1,2,4))
458 SELECT * FROM t1 WHERE w+0 IN
459 (select rowid from t1 where rowid IN (-1,2,4))
465 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
470 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
475 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
477 } {79 6 6400 89 6 8100 199}
480 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
482 } {79 6 6400 89 6 8100 7}
485 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
490 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
495 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
498 do_test where-5.100 {
500 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
503 } {2 1 9 54 5 3025 62 5 3969}
504 do_test where-5.101 {
506 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
507 ORDER BY x DESC, y DESC
509 } {62 5 3969 54 5 3025 2 1 9}
510 do_test where-5.102 {
512 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
515 } {54 5 3025 62 5 3969 2 1 9}
516 do_test where-5.103 {
518 SELECT w, x, y FROM t1 WHERE x IN (1,5) AND y IN (9,8,3025,1000,3969)
521 } {2 1 9 62 5 3969 54 5 3025}
524 # This procedure executes the SQL. Then it checks to see if the OP_Sort
525 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
526 # to the result. If no OP_Sort happened, then "nosort" is appended.
528 # This procedure is used to check to make sure sorting is or is not
529 # occurring as expected.
532 set data [execsql $sql]
533 if {[db status sort]} {set x sort} {set x nosort}
537 # Check out the logic that attempts to implement the ORDER BY clause
538 # using an index rather than by sorting.
542 CREATE TABLE t3(a,b,c);
543 CREATE INDEX t3a ON t3(a);
544 CREATE INDEX t3bc ON t3(b,c);
545 CREATE INDEX t3acb ON t3(a,c,b);
546 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
547 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.1rj {
1352 SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY c IS NULL;
1354 do_execsql_test where-18.2 {
1355 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1357 do_execsql_test where-18.3 {
1358 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c;
1360 do_execsql_test where-18.3rj {
1361 SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY c;
1363 do_execsql_test where-18.4 {
1364 INSERT INTO t181 VALUES(1),(1),(1),(1);
1365 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +c;
1367 do_execsql_test where-18.4rj {
1368 SELECT DISTINCT a FROM t182 RIGHT JOIN t181 ON a=b ORDER BY +c;
1370 do_execsql_test where-18.5 {
1371 INSERT INTO t181 VALUES(2);
1372 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY c IS NULL, +a;
1374 do_execsql_test where-18.6 {
1375 INSERT INTO t181 VALUES(2);
1376 SELECT DISTINCT a FROM t181 LEFT JOIN t182 ON a=b ORDER BY +a, +c IS NULL;
1379 # Make sure the OR optimization works on a JOIN
1381 do_execsql_test where-19.0 {
1382 CREATE TABLE t191(a INT UNIQUE NOT NULL, b INT UNIQUE NOT NULL,c,d);
1383 CREATE INDEX t191a ON t1(a);
1384 CREATE INDEX t191b ON t1(b);
1385 CREATE TABLE t192(x INTEGER PRIMARY KEY,y INT, z INT);
1388 SELECT t191.rowid FROM t192, t191 WHERE (a=y OR b=y) AND x=?1;
1389 } {/.* sqlite_autoindex_t191_1 .* sqlite_autoindex_t191_2 .*/}
1391 # 2018-04-24 ticket [https://www.sqlite.org/src/info/4ba5abf65c5b0f9a]
1392 # Index on expressions leads to an incorrect answer for a LEFT JOIN
1394 do_execsql_test where-20.0 {
1395 CREATE TABLE t201(x);
1396 CREATE TABLE t202(y, z);
1397 INSERT INTO t201 VALUES('key');
1398 INSERT INTO t202 VALUES('key', -1);
1399 CREATE INDEX t202i ON t202(y, ifnull(z, 0));
1400 SELECT count(*) FROM t201 LEFT JOIN t202 ON (x=y) WHERE ifnull(z, 0) >=0;
1403 do_execsql_test where-21.0 {
1404 CREATE TABLE t12(a, b, c);
1405 CREATE TABLE t13(x);
1406 CREATE INDEX t12ab ON t12(b, a);
1407 CREATE INDEX t12ac ON t12(c, a);
1409 INSERT INTO t12 VALUES(4, 0, 1);
1410 INSERT INTO t12 VALUES(4, 1, 0);
1411 INSERT INTO t12 VALUES(5, 0, 1);
1412 INSERT INTO t12 VALUES(5, 1, 0);
1414 INSERT INTO t13 VALUES(1), (2), (3), (4);
1416 do_execsql_test where-21.1 {
1417 SELECT * FROM t12 WHERE
1418 a = (SELECT * FROM (SELECT count(*) FROM t13 LIMIT 5) ORDER BY 1 LIMIT 10)
1425 # 2018-11-05: ticket [https://www.sqlite.org/src/tktview/65eb38f6e46de8c75e188a]
1426 # Incorrect result in LEFT JOIN when STAT4 is enabled.
1429 do_execsql_test where-22.1 {
1430 CREATE TABLE t1(a INT);
1431 CREATE INDEX t1a ON t1(a);
1432 INSERT INTO t1(a) VALUES(NULL),(NULL),(42),(NULL),(NULL);
1433 CREATE TABLE t2(dummy INT);
1434 SELECT count(*) FROM t1 LEFT JOIN t2 ON a IS NOT NULL;
1437 # 20190-02-22: A bug introduced by checkin
1438 # https://www.sqlite.org/src/info/fa792714ae62fa98.
1440 do_execsql_test where-23.0 {
1441 DROP TABLE IF EXISTS t1;
1442 DROP TABLE IF EXISTS t2;
1443 CREATE TABLE t1(a INTEGER PRIMARY KEY);
1444 INSERT INTO t1(a) VALUES(1),(2),(3);
1445 CREATE TABLE t2(x INTEGER PRIMARY KEY, y INT);
1446 INSERT INTO t2(y) VALUES(2),(3);
1447 SELECT * FROM t1, t2 WHERE a=y AND y=3;
1450 #-------------------------------------------------------------------------
1453 do_execsql_test where-24.0 {
1454 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
1455 INSERT INTO t1 VALUES(1, 'one');
1456 INSERT INTO t1 VALUES(2, 'two');
1457 INSERT INTO t1 VALUES(3, 'three');
1458 INSERT INTO t1 VALUES(4, 'four');
1461 foreach {tn sql res} {
1462 1 "SELECT b FROM t1" {one two three four}
1463 2 "SELECT b FROM t1 WHERE a<4" {one two three}
1464 3 "SELECT b FROM t1 WHERE a>1" {two three four}
1465 4 "SELECT b FROM t1 WHERE a>1 AND a<4" {two three}
1467 5 "SELECT b FROM t1 WHERE a>? AND a<4" {}
1468 6 "SELECT b FROM t1 WHERE a>1 AND a<?" {}
1469 7 "SELECT b FROM t1 WHERE a>? AND a<?" {}
1471 7 "SELECT b FROM t1 WHERE a>=? AND a<=4" {}
1472 8 "SELECT b FROM t1 WHERE a>=1 AND a<=?" {}
1473 9 "SELECT b FROM t1 WHERE a>=? AND a<=?" {}
1476 foreach r $res { set rev [concat $r $rev] }
1478 do_execsql_test where-24.$tn.1 "$sql" $res
1479 do_execsql_test where-24.$tn.2 "$sql ORDER BY rowid" $res
1480 do_execsql_test where-24.$tn.3 "$sql ORDER BY rowid DESC" $rev
1482 do_execsql_test where-24-$tn.4 "
1486 $sql ORDER BY rowid;
1487 $sql ORDER BY rowid DESC;
1492 #-------------------------------------------------------------------------
1495 do_execsql_test where-25.0 {
1496 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
1497 CREATE UNIQUE INDEX i1 ON t1(c);
1498 INSERT INTO t1 VALUES(1, 'one', 'i');
1499 INSERT INTO t1 VALUES(2, 'two', 'ii');
1501 CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c);
1502 CREATE UNIQUE INDEX i2 ON t2(c);
1503 INSERT INTO t2 VALUES(1, 'one', 'i');
1504 INSERT INTO t2 VALUES(2, 'two', 'ii');
1505 INSERT INTO t2 VALUES(3, 'three', 'iii');
1507 PRAGMA writable_schema = 1;
1508 UPDATE sqlite_schema SET rootpage = (
1509 SELECT rootpage FROM sqlite_schema WHERE name = 'i2'
1510 ) WHERE name = 'i1';
1514 do_catchsql_test where-25.1 {
1515 DELETE FROM t1 WHERE c='iii'
1516 } {1 {database disk image is malformed}}
1517 do_catchsql_test where-25.2 {
1518 INSERT INTO t1 VALUES(4, 'four', 'iii')
1519 ON CONFLICT(c) DO UPDATE SET b=NULL
1520 } {1 {database disk image is malformed}}
1523 do_execsql_test where-25.3 {
1524 CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID;
1525 CREATE UNIQUE INDEX i1 ON t1(c);
1526 INSERT INTO t1 VALUES(1, 'one', 'i');
1527 INSERT INTO t1 VALUES(2, 'two', 'ii');
1529 CREATE TABLE t2(a INTEGER PRIMARY KEY, b, c);
1530 CREATE UNIQUE INDEX i2 ON t2(c);
1531 INSERT INTO t2 VALUES(1, 'one', 'i');
1532 INSERT INTO t2 VALUES(2, 'two', 'ii');
1533 INSERT INTO t2 VALUES(3, 'three', 'iii');
1535 PRAGMA writable_schema = 1;
1536 UPDATE sqlite_schema SET rootpage = (
1537 SELECT rootpage FROM sqlite_schema WHERE name = 'i2'
1538 ) WHERE name = 'i1';
1542 do_catchsql_test where-25.4 {
1543 SELECT * FROM t1 WHERE c='iii'
1545 do_catchsql_test where-25.5 {
1546 INSERT INTO t1 VALUES(4, 'four', 'iii')
1547 ON CONFLICT(c) DO UPDATE SET b=NULL
1548 } {1 {corrupt database}}
1550 # 2019-08-21 Ticket https://www.sqlite.org/src/info/d9f584e936c7a8d0
1554 do_execsql_test where-26.1 {
1555 CREATE TABLE t0(c0 INTEGER PRIMARY KEY, c1 TEXT);
1556 INSERT INTO t0(c0, c1) VALUES (1, 'a');
1557 CREATE TABLE t1(c0 INT PRIMARY KEY, c1 TEXT);
1558 INSERT INTO t1(c0, c1) VALUES (1, 'a');
1559 SELECT * FROM t0 WHERE '-1' BETWEEN 0 AND t0.c0;
1561 do_execsql_test where-26.2 {
1562 SELECT * FROM t1 WHERE '-1' BETWEEN 0 AND t1.c0;
1564 do_execsql_test where-26.3 {
1565 SELECT * FROM t0 WHERE '-1'>=0 AND '-1'<=t0.c0;
1567 do_execsql_test where-26.4 {
1568 SELECT * FROM t1 WHERE '-1'>=0 AND '-1'<=t1.c0;
1570 do_execsql_test where-26.5 {
1571 SELECT '-1' BETWEEN 0 AND t0.c0 FROM t0;
1573 do_execsql_test where-26.6 {
1574 SELECT '-1' BETWEEN 0 AND t1.c0 FROM t1;
1576 do_execsql_test where-26.7 {
1577 SELECT '-1'>=0 AND '-1'<=t0.c0 FROM t0;
1579 do_execsql_test where-26.8 {
1580 SELECT '-1'>=0 AND '-1'<=t1.c0 FROM t1;
1583 # 2021-07-19 https://sqlite.org/forum/forumpost/2bdb86a068
1584 # Lose of precision when doing comparisons between integer and
1585 # floating point values that are near 9223372036854775807 in the
1586 # OP_SeekGE opcode (and similar).
1588 # Valgrind documentation acknowledges that under valgrind, FP calculations
1589 # may not be as accurate as on x86/amd64 hardware. This seems to be causing
1590 # these tests to fail.
1592 # https://valgrind.org/docs/manual/manual-core.html#manual-core.limits
1594 if {[permutation]!="valgrind"} {
1596 do_execsql_test where-27.1 {
1597 CREATE TABLE t1(a INTEGER PRIMARY KEY);
1598 INSERT INTO t1(a) VALUES(9223372036854775807);
1599 SELECT 1 FROM t1 WHERE a>=(9223372036854775807+1);
1601 do_execsql_test where-27.2 {
1602 SELECT a>=9223372036854775807+1 FROM t1;
1606 # 2022-05-10 dbsqlfuzz 4c5e3e89bc251d28378be88233f531b84ec66901
1609 do_execsql_test where-28.1 {
1610 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT);
1611 CREATE INDEX t1b ON t1(b,b,b,b,b,b,b,b,b,b,b,b,b);
1612 INSERT INTO t1(a,b) VALUES(1,1),(15,2),(19,5);
1613 UPDATE t1 SET b=999 WHERE a IN (SELECT 15) AND b IN (1,2);
1621 # 2022-12-07 Yong Heng [https://sqlite.org/forum/forumpost/dfe8084751]
1624 do_execsql_test where-29.1 {
1625 SELECT DISTINCT 'xyz' FROM pragma_cache_size
1626 WHERE rowid OR abs(0)
1628 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1629 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1630 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1631 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1632 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1633 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1639 # Tests case for the query planner performance issue reported by
1640 # https://sqlite.org/forum/forumpost/1d571c0296
1642 # The fix was to adjust the cost of computing an automatic index for
1643 # ephemeral tables, to help ensure that they are generated if they are
1644 # needed. The test case below only looks at the query plan. But 12x
1645 # improved performance has been verified by populating the "raw" table
1646 # with 100K rows of random data and running actual speed tests.
1648 do_test where-30.1 {
1649 unset -nocomplain res
1651 db eval {CREATE TABLE raw(country,date,total,delta, UNIQUE(country,date));}
1655 -- Find the country and min/max date
1656 init(country, date, fin) AS (SELECT country, min(date), max(date)
1657 FROM raw WHERE total > 0 GROUP BY country),
1659 -- Generate the date stream for each country
1660 src(country, date) AS (SELECT raw.country, raw.date
1661 FROM raw JOIN init i on raw.country = i.country AND raw.date > i.date
1662 ORDER BY raw.country, raw.date),
1664 -- Generate the x & y for each entry in the country/date stream
1665 vals(country, date, x, y) AS (SELECT src.country, src.date,
1666 julianday(raw.date) - julianday(src.date), log(delta+1)
1667 FROM src JOIN raw on raw.country = src.country
1668 AND raw.date > date(src.date,'-7 days')
1669 AND raw.date <= src.date AND delta >= 0),
1671 -- Accumulate the data we need
1672 sums(country, date, x2, x, n, xy, y) AS (SELECT country, date,
1673 sum(x*x*1.0), sum(x*1.0), sum(1.0), sum(x*y*1.0), sum(y*1.0)
1674 FROM vals GROUP BY 1, 2),
1676 -- use these to calculate to divisor for the inverse matrix
1677 mult(country, date, m) AS (SELECT country, date, 1.0/(x2 * n - x * x)
1680 -- Build the inverse matrix
1681 inv(country, date, a,b,c,d) AS (SELECT mult.country, mult.date, n * m,
1682 -x * m, -x * m, x2 * m
1683 FROM mult JOIN sums on sums.country=mult.country
1684 AND mult.date=sums.date),
1686 -- Calculate the coefficients for the least squares fit
1687 fit(country, date, a, b) AS (SELECT inv.country, inv.date,
1688 a * xy + b * y, c * xy + d * y
1690 JOIN mult on mult.country = inv.country AND mult.date = inv.date
1691 JOIN sums on sums.country = mult.country AND sums.date = mult.date
1693 SELECT *, nFin/nPrev - 1 AS growth, log(2)/log(nFin/nPrev) AS doubling
1694 FROM (SELECT f.*, exp(b) - 1 AS nFin, exp(a* (-1) + b) - 1 AS nPrev
1695 FROM fit f JOIN init i on i.country = f.country
1696 AND f.date <= date(i.fin,'-3 days'))
1697 WHERE nPrev > 0 AND nFin > 0;
1699 if {$parent!=0} continue
1700 if {![string match SCAN* $detail]} continue
1705 # ^^^^^^-- there should only be one top-level table scan in the query plan.