First public contribution.
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.47 2008/09/01 15:52:11 drh 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}
72 count {SELECT x, y, w AS abc FROM t1 WHERE abc=10}
78 count {SELECT x, y, w FROM t1 WHERE w=11}
81 count {SELECT x, y, w AS abc FROM t1 WHERE abc=11}
84 count {SELECT x, y, w AS abc FROM t1 WHERE 11=w}
87 count {SELECT x, y, w AS abc FROM t1 WHERE 11=abc}
90 count {SELECT w, x, y FROM t1 WHERE 11=w AND x>2}
96 count {SELECT w AS a, x AS b, y FROM t1 WHERE 11=a AND b>2}
102 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
104 do_test where-1.5.2 {
105 set sqlite_query_plan
108 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
111 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
114 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
116 do_test where-1.8.2 {
117 set sqlite_query_plan
119 do_test where-1.8.3 {
120 count {SELECT x, y FROM t1 WHERE y=144 AND x=3}
121 set sqlite_query_plan
124 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
127 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
130 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
133 # New for SQLite version 2.1: Verify that that inequality constraints
134 # are used correctly.
137 count {SELECT w FROM t1 WHERE x=3 AND y<100}
140 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
143 count {SELECT w FROM t1 WHERE 3=x AND y<100}
146 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
149 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
152 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
155 count {SELECT w FROM t1 WHERE x=3 AND y>225}
158 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
161 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
164 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
167 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
170 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
173 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
176 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
179 # Need to work on optimizing the BETWEEN operator.
181 # do_test where-1.26 {
182 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
186 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
190 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
193 count {SELECT w FROM t1 WHERE y==121}
198 count {SELECT w FROM t1 WHERE w>97}
201 count {SELECT w FROM t1 WHERE w>=97}
204 count {SELECT w FROM t1 WHERE w==97}
206 do_test where-1.33.1 {
207 count {SELECT w FROM t1 WHERE w<=97 AND w==97}
209 do_test where-1.33.2 {
210 count {SELECT w FROM t1 WHERE w<98 AND w==97}
212 do_test where-1.33.3 {
213 count {SELECT w FROM t1 WHERE w>=97 AND w==97}
215 do_test where-1.33.4 {
216 count {SELECT w FROM t1 WHERE w>96 AND w==97}
218 do_test where-1.33.5 {
219 count {SELECT w FROM t1 WHERE w==97 AND w==97}
222 count {SELECT w FROM t1 WHERE w+1==98}
225 count {SELECT w FROM t1 WHERE w<3}
228 count {SELECT w FROM t1 WHERE w<=3}
231 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
235 count {SELECT (w) FROM t1 WHERE (w)>(97)}
238 count {SELECT (w) FROM t1 WHERE (w)>=(97)}
241 count {SELECT (w) FROM t1 WHERE (w)==(97)}
244 count {SELECT (w) FROM t1 WHERE ((w)+(1))==(98)}
248 # Do the same kind of thing except use a join as the data source.
252 SELECT w, p FROM t2, t1
253 WHERE x=q AND y=s AND r=8977
258 SELECT w, p FROM t2, t1
259 WHERE x=q AND s=y AND r=8977
264 SELECT w, p FROM t2, t1
265 WHERE x=q AND s=y AND r=8977 AND w>10
270 SELECT w, p FROM t2, t1
271 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
276 SELECT w, p FROM t2, t1
277 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
282 SELECT w, p FROM t2, t1
283 WHERE x=q AND p=77 AND s=y AND w>5
288 SELECT w, p FROM t1, t2
289 WHERE x=q AND p>77 AND s=y AND w=5
293 # Lets do a 3-way join.
297 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
298 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
303 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
304 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
309 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
310 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
314 # Test to see that the special case of a constant WHERE clause is
319 SELECT * FROM t1 WHERE 0
324 SELECT * FROM t1 WHERE 1 LIMIT 1
349 SELECT count(*) FROM t1 WHERE t1.w
353 # Verify that IN operators in a WHERE clause are handled correctly.
354 # Omit these tests if the build is not capable of sub-queries.
359 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
361 } {1 0 4 2 1 9 3 1 16 4}
364 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
366 } {1 0 4 2 1 9 3 1 16 102}
369 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
371 } {1 0 4 2 1 9 3 1 16 14}
374 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
376 } {1 0 4 2 1 9 3 1 16 102}
379 SELECT * FROM t1 WHERE rowid IN
380 (select rowid from t1 where rowid IN (-1,2,4))
386 SELECT * FROM t1 WHERE rowid+0 IN
387 (select rowid from t1 where rowid IN (-1,2,4))
393 SELECT * FROM t1 WHERE w IN
394 (select rowid from t1 where rowid IN (-1,2,4))
400 SELECT * FROM t1 WHERE w+0 IN
401 (select rowid from t1 where rowid IN (-1,2,4))
407 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
412 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
417 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
419 } {79 6 6400 89 6 8100 199}
422 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
424 } {79 6 6400 89 6 8100 7}
427 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
432 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
437 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,16) ORDER BY 1;
442 # This procedure executes the SQL. Then it checks to see if the OP_Sort
443 # opcode was executed. If an OP_Sort did occur, then "sort" is appended
444 # to the result. If no OP_Sort happened, then "nosort" is appended.
446 # This procedure is used to check to make sure sorting is or is not
447 # occurring as expected.
450 set ::sqlite_sort_count 0
451 set data [execsql $sql]
452 if {$::sqlite_sort_count} {set x sort} {set x nosort}
456 # Check out the logic that attempts to implement the ORDER BY clause
457 # using an index rather than by sorting.
461 CREATE TABLE t3(a,b,c);
462 CREATE INDEX t3a ON t3(a);
463 CREATE INDEX t3bc ON t3(b,c);
464 CREATE INDEX t3acb ON t3(a,c,b);
465 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
466 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
468 } {100 5050 5050 348550}
471 SELECT * FROM t3 ORDER BY a LIMIT 3
473 } {1 100 4 2 99 9 3 98 16 nosort}
476 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
478 } {1 100 4 2 99 9 3 98 16 sort}
481 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
483 } {1 100 4 2 99 9 3 98 16 nosort}
486 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
488 } {1 100 4 2 99 9 3 98 16 nosort}
491 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
493 } {1 100 4 2 99 9 3 98 16 nosort}
496 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
498 } {1 100 4 2 99 9 3 98 16 nosort}
502 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
504 } {1 100 4 2 99 9 3 98 16 sort}
506 do_test where-6.9.1 {
508 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
511 do_test where-6.9.1.1 {
513 SELECT * FROM t3 WHERE a>=1 AND a=1 AND c>0 ORDER BY a LIMIT 3
516 do_test where-6.9.1.2 {
518 SELECT * FROM t3 WHERE a<2 AND a=1 AND c>0 ORDER BY a LIMIT 3
521 do_test where-6.9.2 {
523 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
526 do_test where-6.9.3 {
528 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c LIMIT 3
531 do_test where-6.9.4 {
533 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC LIMIT 3
536 do_test where-6.9.5 {
538 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c DESC LIMIT 3
541 do_test where-6.9.6 {
543 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c DESC LIMIT 3
546 do_test where-6.9.7 {
548 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY c,a LIMIT 3
551 do_test where-6.9.8 {
553 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a DESC, c ASC LIMIT 3
556 do_test where-6.9.9 {
558 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a ASC, c DESC LIMIT 3
563 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
568 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
573 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
578 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
580 } {100 1 10201 99 2 10000 98 3 9801 nosort}
581 do_test where-6.13.1 {
583 SELECT * FROM t3 WHERE a>0 ORDER BY -a LIMIT 3
585 } {100 1 10201 99 2 10000 98 3 9801 sort}
588 SELECT * FROM t3 ORDER BY b LIMIT 3
590 } {100 1 10201 99 2 10000 98 3 9801 nosort}
593 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
595 } {1 0 2 1 3 1 nosort}
598 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
603 SELECT y FROM t1 ORDER BY w LIMIT 3;
608 SELECT y FROM t1 ORDER BY rowid LIMIT 3;
613 SELECT y FROM t1 ORDER BY rowid, y LIMIT 3;
618 SELECT y FROM t1 ORDER BY rowid, y DESC LIMIT 3;
623 SELECT y FROM t1 WHERE y>4 ORDER BY rowid, w, x LIMIT 3;
628 SELECT y FROM t1 WHERE y>=9 ORDER BY rowid, x DESC, w LIMIT 3;
633 SELECT y FROM t1 WHERE y>4 AND y<25 ORDER BY rowid;
638 SELECT y FROM t1 WHERE y>=4 AND y<=25 ORDER BY oid;
643 SELECT y FROM t1 WHERE y<=25 ORDER BY _rowid_, w+y;
648 # Tests for reverse-order sorting.
652 SELECT w FROM t1 WHERE x=3 ORDER BY y;
654 } {8 9 10 11 12 13 14 15 nosort}
657 SELECT w FROM t1 WHERE x=3 ORDER BY y DESC;
659 } {15 14 13 12 11 10 9 8 nosort}
662 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y LIMIT 3;
667 SELECT w FROM t1 WHERE x=3 AND y>100 ORDER BY y DESC LIMIT 3;
672 SELECT w FROM t1 WHERE x=3 AND y>121 ORDER BY y DESC;
674 } {15 14 13 12 11 nosort}
677 SELECT w FROM t1 WHERE x=3 AND y>=121 ORDER BY y DESC;
679 } {15 14 13 12 11 10 nosort}
682 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y DESC;
687 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y DESC;
689 } {13 12 11 10 nosort}
692 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y DESC;
697 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y DESC;
702 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<196 ORDER BY y;
707 SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196 ORDER BY y;
709 } {10 11 12 13 nosort}
712 SELECT w FROM t1 WHERE x=3 AND y>121 AND y<=196 ORDER BY y;
717 SELECT w FROM t1 WHERE x=3 AND y>100 AND y<196 ORDER BY y;
722 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y;
727 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y;
732 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y;
737 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y;
742 SELECT w FROM t1 WHERE x=3 AND y<81 ORDER BY y DESC;
747 SELECT w FROM t1 WHERE x=3 AND y<=81 ORDER BY y DESC;
752 SELECT w FROM t1 WHERE x=3 AND y>256 ORDER BY y DESC;
757 SELECT w FROM t1 WHERE x=3 AND y>=256 ORDER BY y DESC;
762 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y;
767 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y;
772 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y;
777 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y;
782 SELECT w FROM t1 WHERE x=0 AND y<4 ORDER BY y DESC;
787 SELECT w FROM t1 WHERE x=0 AND y<=4 ORDER BY y DESC;
792 SELECT w FROM t1 WHERE x=6 AND y>10201 ORDER BY y DESC;
797 SELECT w FROM t1 WHERE x=6 AND y>=10201 ORDER BY y DESC;
802 SELECT y FROM t1 ORDER BY rowid DESC LIMIT 3
804 } {10201 10000 9801 nosort}
807 SELECT y FROM t1 WHERE y<25 ORDER BY rowid DESC
812 SELECT y FROM t1 WHERE y<=25 ORDER BY rowid DESC
817 SELECT y FROM t1 WHERE y<25 AND y>4 ORDER BY rowid DESC, y DESC
822 SELECT y FROM t1 WHERE y<25 AND y>=4 ORDER BY rowid DESC
828 CREATE TABLE t4 AS SELECT * FROM t1;
829 CREATE INDEX i4xy ON t4(x,y);
832 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
840 SELECT w FROM t4 WHERE x=4 and y<1000 ORDER BY y DESC limit 3;
844 # Make sure searches with an index work with an empty table.
848 CREATE TABLE t5(x PRIMARY KEY);
849 SELECT * FROM t5 WHERE x<10;
854 SELECT * FROM t5 WHERE x<10 ORDER BY x DESC;
859 SELECT * FROM t5 WHERE x=10;
865 SELECT 1 WHERE abs(random())<0
869 proc tclvar_func {vname} {return [set ::$vname]}
870 db function tclvar tclvar_func
873 SELECT count(*) FROM t1 WHERE tclvar('v1');
879 SELECT count(*) FROM t1 WHERE tclvar('v1');
884 proc tclvar_func {vname} {
890 SELECT count(*) FROM t1 WHERE tclvar('v1');
894 # Ticket #1376. The query below was causing a segfault.
895 # The problem was the age-old error of calling realloc() on an
896 # array while there are still pointers to individual elements of
901 CREATE TABLE t99(Dte INT, X INT);
902 DELETE FROM t99 WHERE (Dte = 2451337) OR (Dte = 2451339) OR
903 (Dte BETWEEN 2451345 AND 2451347) OR (Dte = 2451351) OR
904 (Dte BETWEEN 2451355 AND 2451356) OR (Dte = 2451358) OR
905 (Dte = 2451362) OR (Dte = 2451365) OR (Dte = 2451367) OR
906 (Dte BETWEEN 2451372 AND 2451376) OR (Dte BETWEEN 2451382 AND 2451384) OR
907 (Dte = 2451387) OR (Dte BETWEEN 2451389 AND 2451391) OR
908 (Dte BETWEEN 2451393 AND 2451395) OR (Dte = 2451400) OR
909 (Dte = 2451402) OR (Dte = 2451404) OR (Dte BETWEEN 2451416 AND 2451418) OR
910 (Dte = 2451422) OR (Dte = 2451426) OR (Dte BETWEEN 2451445 AND 2451446) OR
911 (Dte = 2451456) OR (Dte = 2451458) OR (Dte BETWEEN 2451465 AND 2451467) OR
912 (Dte BETWEEN 2451469 AND 2451471) OR (Dte = 2451474) OR
913 (Dte BETWEEN 2451477 AND 2451501) OR (Dte BETWEEN 2451503 AND 2451509) OR
914 (Dte BETWEEN 2451511 AND 2451514) OR (Dte BETWEEN 2451518 AND 2451521) OR
915 (Dte BETWEEN 2451523 AND 2451531) OR (Dte BETWEEN 2451533 AND 2451537) OR
916 (Dte BETWEEN 2451539 AND 2451544) OR (Dte BETWEEN 2451546 AND 2451551) OR
917 (Dte BETWEEN 2451553 AND 2451555) OR (Dte = 2451557) OR
918 (Dte BETWEEN 2451559 AND 2451561) OR (Dte = 2451563) OR
919 (Dte BETWEEN 2451565 AND 2451566) OR (Dte BETWEEN 2451569 AND 2451571) OR
920 (Dte = 2451573) OR (Dte = 2451575) OR (Dte = 2451577) OR (Dte = 2451581) OR
921 (Dte BETWEEN 2451583 AND 2451586) OR (Dte BETWEEN 2451588 AND 2451592) OR
922 (Dte BETWEEN 2451596 AND 2451598) OR (Dte = 2451600) OR
923 (Dte BETWEEN 2451602 AND 2451603) OR (Dte = 2451606) OR (Dte = 2451611);
927 # Ticket #2116: Make sure sorting by index works well with nn INTEGER PRIMARY
932 CREATE TABLE t6(a INTEGER PRIMARY KEY, b TEXT);
933 INSERT INTO t6 VALUES(1,'one');
934 INSERT INTO t6 VALUES(4,'four');
935 CREATE INDEX t6i1 ON t6(b);
938 SELECT * FROM t6 ORDER BY b;
940 } {4 four 1 one nosort}
943 SELECT * FROM t6 ORDER BY b, a;
945 } {4 four 1 one nosort}
948 SELECT * FROM t6 ORDER BY a;
950 } {1 one 4 four nosort}
953 SELECT * FROM t6 ORDER BY a, b;
955 } {1 one 4 four nosort}
958 SELECT * FROM t6 ORDER BY b DESC;
960 } {1 one 4 four nosort}
963 SELECT * FROM t6 ORDER BY b DESC, a DESC;
965 } {1 one 4 four nosort}
968 SELECT * FROM t6 ORDER BY b DESC, a ASC;
970 } {1 one 4 four sort}
973 SELECT * FROM t6 ORDER BY b ASC, a DESC;
975 } {4 four 1 one sort}
978 SELECT * FROM t6 ORDER BY a DESC;
980 } {4 four 1 one nosort}
981 do_test where-12.10 {
983 SELECT * FROM t6 ORDER BY a DESC, b DESC;
985 } {4 four 1 one nosort}
986 do_test where-12.11 {
988 SELECT * FROM t6 ORDER BY a DESC, b ASC;
990 } {4 four 1 one nosort}
991 do_test where-12.12 {
993 SELECT * FROM t6 ORDER BY a ASC, b DESC;
995 } {1 one 4 four nosort}
998 CREATE TABLE t7(a INTEGER PRIMARY KEY, b TEXT);
999 INSERT INTO t7 VALUES(1,'one');
1000 INSERT INTO t7 VALUES(4,'four');
1001 CREATE INDEX t7i1 ON t7(b);
1004 SELECT * FROM t7 ORDER BY b;
1006 } {4 four 1 one nosort}
1007 do_test where-13.2 {
1009 SELECT * FROM t7 ORDER BY b, a;
1011 } {4 four 1 one nosort}
1012 do_test where-13.3 {
1014 SELECT * FROM t7 ORDER BY a;
1016 } {1 one 4 four nosort}
1017 do_test where-13.4 {
1019 SELECT * FROM t7 ORDER BY a, b;
1021 } {1 one 4 four nosort}
1022 do_test where-13.5 {
1024 SELECT * FROM t7 ORDER BY b DESC;
1026 } {1 one 4 four nosort}
1027 do_test where-13.6 {
1029 SELECT * FROM t7 ORDER BY b DESC, a DESC;
1031 } {1 one 4 four nosort}
1032 do_test where-13.7 {
1034 SELECT * FROM t7 ORDER BY b DESC, a ASC;
1036 } {1 one 4 four sort}
1037 do_test where-13.8 {
1039 SELECT * FROM t7 ORDER BY b ASC, a DESC;
1041 } {4 four 1 one sort}
1042 do_test where-13.9 {
1044 SELECT * FROM t7 ORDER BY a DESC;
1046 } {4 four 1 one nosort}
1047 do_test where-13.10 {
1049 SELECT * FROM t7 ORDER BY a DESC, b DESC;
1051 } {4 four 1 one nosort}
1052 do_test where-13.11 {
1054 SELECT * FROM t7 ORDER BY a DESC, b ASC;
1056 } {4 four 1 one nosort}
1057 do_test where-13.12 {
1059 SELECT * FROM t7 ORDER BY a ASC, b DESC;
1061 } {1 one 4 four nosort}
1065 # When optimizing out ORDER BY clauses, make sure that trailing terms
1066 # of the ORDER BY clause do not reference other tables in a join.
1068 do_test where-14.1 {
1070 CREATE TABLE t8(a INTEGER PRIMARY KEY, b TEXT UNIQUE);
1071 INSERT INTO t8 VALUES(1,'one');
1072 INSERT INTO t8 VALUES(4,'four');
1075 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b
1077 } {1/4 1/1 4/4 4/1 sort}
1078 do_test where-14.2 {
1080 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
1082 } {1/1 1/4 4/1 4/4 sort}
1083 do_test where-14.3 {
1085 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
1087 } {1/1 1/4 4/1 4/4 nosort}
1088 do_test where-14.4 {
1090 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
1092 } {1/1 1/4 4/1 4/4 nosort}
1093 do_test where-14.5 {
1095 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
1097 } {4/1 4/4 1/1 1/4 nosort}
1098 do_test where-14.6 {
1100 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
1102 } {4/1 4/4 1/1 1/4 nosort}
1103 do_test where-14.7 {
1105 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
1107 } {4/1 4/4 1/1 1/4 sort}
1108 do_test where-14.7.1 {
1110 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
1112 } {4/1 4/4 1/1 1/4 sort}
1113 do_test where-14.7.2 {
1115 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
1117 } {4/1 4/4 1/1 1/4 nosort}
1118 do_test where-14.8 {
1120 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
1122 } {4/4 4/1 1/4 1/1 sort}
1123 do_test where-14.9 {
1125 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b
1127 } {4/4 4/1 1/4 1/1 sort}
1128 do_test where-14.10 {
1130 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||y.b DESC
1132 } {4/1 4/4 1/1 1/4 sort}
1133 do_test where-14.11 {
1135 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b
1137 } {4/1 4/4 1/1 1/4 sort}
1138 do_test where-14.12 {
1140 SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||x.b DESC
1142 } {4/4 4/1 1/4 1/1 sort}
1146 # There was a crash that could occur when a where clause contains an
1147 # alias for an expression in the result set, and that expression retrieves
1148 # a column of the second or subsequent table in a join.
1150 do_test where-15.1 {
1152 CREATE TEMP TABLE t1 (a, b, c, d, e);
1153 CREATE TEMP TABLE t2 (f);
1154 SELECT t1.e AS alias FROM t2, t1 WHERE alias = 1 ;
1158 integrity_check {where-99.0}