2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the the library is able to correctly
16 # handle file-format 3 (version 2.6.x) databases.
18 # $Id: format3.test,v 1.4 2003/12/23 02:17:35 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Create a bunch of data to sort against
26 set fd [open data.txt w]
27 puts $fd "1\tone\t0\tI\t3.141592653"
28 puts $fd "2\ttwo\t1\tII\t2.15"
29 puts $fd "3\tthree\t1\tIII\t4221.0"
30 puts $fd "4\tfour\t2\tIV\t-0.0013442"
31 puts $fd "5\tfive\t2\tV\t-11"
32 puts $fd "6\tsix\t2\tVI\t0.123"
33 puts $fd "7\tseven\t2\tVII\t123.0"
34 puts $fd "8\teight\t3\tVIII\t-1.6"
44 COPY t1 FROM 'data.txt'
48 set ::bt [btree_open test.db]
49 btree_begin_transaction $::bt
50 set m [btree_get_meta $::bt]
51 set m [lreplace $m 2 2 3]
52 eval btree_update_meta $::bt $m
56 execsql {SELECT count(*) FROM t1}
60 execsql {SELECT n FROM t1 ORDER BY n}
62 do_test format3-1.1.1 {
63 execsql {SELECT n FROM t1 ORDER BY n ASC}
65 do_test format3-1.1.1 {
66 execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
69 execsql {SELECT n FROM t1 ORDER BY n DESC}
71 do_test format3-1.3a {
72 execsql {SELECT v FROM t1 ORDER BY v}
73 } {eight five four one seven six three two}
74 do_test format3-1.3b {
75 execsql {SELECT n FROM t1 ORDER BY v}
78 execsql {SELECT n FROM t1 ORDER BY v DESC}
81 execsql {SELECT flt FROM t1 ORDER BY flt}
82 } {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
84 execsql {SELECT flt FROM t1 ORDER BY flt DESC}
85 } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
87 execsql {SELECT roman FROM t1 ORDER BY roman}
88 } {I II III IV V VI VII VIII}
90 execsql {SELECT n FROM t1 ORDER BY log, flt}
92 do_test format3-1.8.1 {
93 execsql {SELECT n FROM t1 ORDER BY log asc, flt}
95 do_test format3-1.8.2 {
96 execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
98 do_test format3-1.8.3 {
99 execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
101 do_test format3-1.9 {
102 execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
104 do_test format3-1.9.1 {
105 execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
107 do_test format3-1.10 {
108 execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
110 do_test format3-1.11 {
111 execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
114 # These tests are designed to reach some hard-to-reach places
115 # inside the string comparison routines.
117 # (Later) The sorting behavior changed in 2.7.0. But we will
118 # keep these tests. You can never have too many test cases!
120 do_test format3-2.1.1 {
122 UPDATE t1 SET v='x' || -flt;
123 UPDATE t1 SET v='x-2b' where v=='x-0.123';
124 SELECT v FROM t1 ORDER BY v;
126 } {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
127 do_test format3-2.1.2 {
129 SELECT v FROM t1 ORDER BY substr(v,2,999);
131 } {x-4221 x-123 x-3.141592653 x-2.15 x0.0013442 x1.6 x11 x-2b}
132 do_test format3-2.1.3 {
134 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
136 } {x-4221 x-123 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11}
137 do_test format3-2.1.4 {
139 SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
141 } {x-2b x11 x1.6 x0.0013442 x-2.15 x-3.141592653 x-123 x-4221}
142 do_test format3-2.1.5 {
144 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
146 } {x11 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123 x-4221}
148 # This is a bug fix for 2.2.4.
149 # Strings are normally mapped to upper-case for a caseless comparison.
150 # But this can cause problems for characters in between 'Z' and 'a'.
152 do_test format3-3.1 {
154 CREATE TABLE t2(a,b);
155 INSERT INTO t2 VALUES('AGLIENTU',1);
156 INSERT INTO t2 VALUES('AGLIE`',2);
157 INSERT INTO t2 VALUES('AGNA',3);
158 SELECT a, b FROM t2 ORDER BY a;
160 } {AGLIENTU 1 AGLIE` 2 AGNA 3}
161 do_test format3-3.2 {
163 SELECT a, b FROM t2 ORDER BY a DESC;
165 } {AGNA 3 AGLIE` 2 AGLIENTU 1}
166 do_test format3-3.3 {
169 INSERT INTO t2 VALUES('aglientu',1);
170 INSERT INTO t2 VALUES('aglie`',2);
171 INSERT INTO t2 VALUES('agna',3);
172 SELECT a, b FROM t2 ORDER BY a;
174 } {aglie` 2 aglientu 1 agna 3}
175 do_test format3-3.4 {
177 SELECT a, b FROM t2 ORDER BY a DESC;
179 } {agna 3 aglientu 1 aglie` 2}
181 # Version 2.7.0 testing.
183 do_test format3-4.1 {
185 INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
186 INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
187 INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
188 INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
189 SELECT n FROM t1 ORDER BY n;
191 } {1 2 3 4 5 6 7 8 9 10 11 12}
192 do_test format3-4.2 {
194 SELECT n||'' FROM t1 ORDER BY 1;
196 } {1 2 3 4 5 6 7 8 9 10 11 12}
197 do_test format3-4.3 {
199 SELECT n+0 FROM t1 ORDER BY 1;
201 } {1 2 3 4 5 6 7 8 9 10 11 12}
202 do_test format3-4.4 {
204 SELECT n||'' FROM t1 ORDER BY 1 DESC;
206 } {12 11 10 9 8 7 6 5 4 3 2 1}
207 do_test format3-4.5 {
209 SELECT n+0 FROM t1 ORDER BY 1 DESC;
211 } {12 11 10 9 8 7 6 5 4 3 2 1}
212 do_test format3-4.6 {
214 SELECT v FROM t1 ORDER BY 1;
216 } {x-123 x-2.15 x-2b x-3.141592653 x-4.0e9 x-4221 x0.0013442 x01234567890123456789 x1.6 x11 x2.7 x5.0e10}
217 do_test format3-4.7 {
219 SELECT v FROM t1 ORDER BY 1 DESC;
221 } {x5.0e10 x2.7 x11 x1.6 x01234567890123456789 x0.0013442 x-4221 x-4.0e9 x-3.141592653 x-2b x-2.15 x-123}
222 do_test format3-4.8 {
224 SELECT substr(v,2,99) FROM t1 ORDER BY 1;
226 } {-4.0e9 -4221 -123 -3.141592653 -2.15 0.0013442 1.6 2.7 11 5.0e10 01234567890123456789 -2b}
228 # Build some new test data, this time with indices.
230 do_test format3-5.0 {
233 CREATE TABLE t1(w int, x text, y blob);
235 CREATE TABLE t2(p varchar(1), q clob, r real, s numeric(8));
237 for {set i 1} {$i<=100} {incr i} {
239 set x [expr {int(log($i)/log(2))}]
240 set y [expr {$i*$i + 2*$i + 1}]
241 execsql "INSERT INTO t1 VALUES($w,$x,$y)"
244 INSERT INTO t2 SELECT 101-w, x, (SELECT max(y) FROM t1)+1-y, y FROM t1;
245 CREATE INDEX i1w ON t1(w);
246 CREATE INDEX i1xy ON t1(x,y);
247 CREATE INDEX i2p ON t2(p);
248 CREATE INDEX i2r ON t2(r);
249 CREATE INDEX i2qs ON t2(q, s);
253 # Do an SQL statement. Append the search count to the end of the result.
256 set ::sqlite_search_count 0
257 return [concat [execsql $sql] $::sqlite_search_count]
260 # Verify that queries use an index. We are using the special variable
261 # "sqlite_search_count" which tallys the number of executions of MoveTo
262 # and Next operators in the VDBE. By verifing that the search count is
263 # small we can be assured that indices are being used properly.
265 do_test format3-5.1 {
268 count {SELECT x, y FROM t1 WHERE w=10}
270 do_test format3-5.2 {
271 count {SELECT x, y FROM t1 WHERE w=11}
273 do_test format3-5.3 {
274 count {SELECT x, y FROM t1 WHERE 11=w}
276 do_test format3-5.4 {
277 count {SELECT x, y FROM t1 WHERE 11=w AND x>2}
279 do_test format3-5.5 {
280 count {SELECT x, y FROM t1 WHERE y<200 AND w=11 AND x>2}
282 do_test format3-5.6 {
283 count {SELECT x, y FROM t1 WHERE y<200 AND x>2 AND w=11}
285 do_test format3-5.7 {
286 count {SELECT x, y FROM t1 WHERE w=11 AND y<200 AND x>2}
288 do_test format3-5.8 {
289 count {SELECT x, y FROM t1 WHERE w>10 AND y=144 AND x=3}
291 do_test format3-5.9 {
292 count {SELECT x, y FROM t1 WHERE y=144 AND w>10 AND x=3}
294 do_test format3-5.10 {
295 count {SELECT x, y FROM t1 WHERE x=3 AND w>=10 AND y=121}
297 do_test format3-5.11 {
298 count {SELECT x, y FROM t1 WHERE x=3 AND y=100 AND w<10}
301 # New for SQLite version 2.1: Verify that that inequality constraints
302 # are used correctly.
304 do_test format3-5.12 {
305 count {SELECT w FROM t1 WHERE x=3 AND y<100}
307 do_test format3-5.13 {
308 count {SELECT w FROM t1 WHERE x=3 AND 100>y}
310 do_test format3-5.14 {
311 count {SELECT w FROM t1 WHERE 3=x AND y<100}
313 do_test format3-5.15 {
314 count {SELECT w FROM t1 WHERE 3=x AND 100>y}
316 do_test format3-5.16 {
317 count {SELECT w FROM t1 WHERE x=3 AND y<=100}
319 do_test format3-5.17 {
320 count {SELECT w FROM t1 WHERE x=3 AND 100>=y}
322 do_test format3-5.18 {
323 count {SELECT w FROM t1 WHERE x=3 AND y>225}
325 do_test format3-5.19 {
326 count {SELECT w FROM t1 WHERE x=3 AND 225<y}
328 do_test format3-5.20 {
329 count {SELECT w FROM t1 WHERE x=3 AND y>=225}
331 do_test format3-5.21 {
332 count {SELECT w FROM t1 WHERE x=3 AND 225<=y}
334 do_test format3-5.22 {
335 count {SELECT w FROM t1 WHERE x=3 AND y>121 AND y<196}
337 do_test format3-5.23 {
338 count {SELECT w FROM t1 WHERE x=3 AND y>=121 AND y<=196}
340 do_test format3-5.24 {
341 count {SELECT w FROM t1 WHERE x=3 AND 121<y AND 196>y}
343 do_test format3-5.25 {
344 count {SELECT w FROM t1 WHERE x=3 AND 121<=y AND 196>=y}
347 # Need to work on optimizing the BETWEEN operator.
349 # do_test format3-5.26 {
350 # count {SELECT w FROM t1 WHERE x=3 AND y BETWEEN 121 AND 196}
353 do_test format3-5.27 {
354 count {SELECT w FROM t1 WHERE x=3 AND y+1==122}
356 do_test format3-5.28 {
357 count {SELECT w FROM t1 WHERE x+1=4 AND y+1==122}
359 do_test format3-5.29 {
360 count {SELECT w FROM t1 WHERE y==121}
364 do_test format3-5.30 {
365 count {SELECT w FROM t1 WHERE w>97}
367 do_test format3-5.31 {
368 count {SELECT w FROM t1 WHERE w>=97}
370 do_test format3-5.33 {
371 count {SELECT w FROM t1 WHERE w==97}
373 do_test format3-5.34 {
374 count {SELECT w FROM t1 WHERE w+1==98}
376 do_test format3-5.35 {
377 count {SELECT w FROM t1 WHERE w<3}
379 do_test format3-5.36 {
380 count {SELECT w FROM t1 WHERE w<=3}
382 do_test format3-5.37 {
383 count {SELECT w FROM t1 WHERE w+1<=4 ORDER BY w}
387 # Do the same kind of thing except use a join as the data source.
389 do_test format3-6.1 {
393 SELECT w, p FROM t2, t1
394 WHERE x=q AND y=s AND r=8977
397 do_test format3-6.2 {
399 SELECT w, p FROM t2, t1
400 WHERE x=q AND s=y AND r=8977
403 do_test format3-6.3 {
405 SELECT w, p FROM t2, t1
406 WHERE x=q AND s=y AND r=8977 AND w>10
409 do_test format3-6.4 {
411 SELECT w, p FROM t2, t1
412 WHERE p<80 AND x=q AND s=y AND r=8977 AND w>10
415 do_test format3-6.5 {
417 SELECT w, p FROM t2, t1
418 WHERE p<80 AND x=q AND 8977=r AND s=y AND w>10
421 do_test format3-6.6 {
423 SELECT w, p FROM t2, t1
424 WHERE x=q AND p=77 AND s=y AND w>5
427 do_test format3-6.7 {
429 SELECT w, p FROM t1, t2
430 WHERE x=q AND p>77 AND s=y AND w=5
434 # Lets do a 3-way join.
436 do_test format3-7.1 {
438 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
439 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=11
442 do_test format3-7.2 {
444 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
445 WHERE C.w=101-B.p AND B.r=10202-A.y AND A.w=12
448 do_test format3-7.3 {
450 SELECT A.w, B.p, C.w FROM t1 as A, t2 as B, t1 as C
451 WHERE A.w=15 AND B.p=C.w AND B.r=10202-A.y
455 # Test to see that the special case of a constant WHERE clause is
458 do_test format3-8.1 {
460 SELECT * FROM t1 WHERE 0
463 do_test format3-8.2 {
465 SELECT * FROM t1 WHERE 1 LIMIT 1
468 do_test format3-8.3 {
473 do_test format3-8.4 {
479 # Verify that IN operators in a WHERE clause are handled correctly.
481 do_test format3-9.1 {
483 SELECT * FROM t1 WHERE rowid IN (1,2,3,1234) order by 1;
485 } {1 0 4 2 1 9 3 1 16 0}
486 do_test format3-9.2 {
488 SELECT * FROM t1 WHERE rowid+0 IN (1,2,3,1234) order by 1;
490 } {1 0 4 2 1 9 3 1 16 199}
491 do_test format3-9.3 {
493 SELECT * FROM t1 WHERE w IN (-1,1,2,3) order by 1;
495 } {1 0 4 2 1 9 3 1 16 10}
496 do_test format3-9.4 {
498 SELECT * FROM t1 WHERE w+0 IN (-1,1,2,3) order by 1;
500 } {1 0 4 2 1 9 3 1 16 199}
501 do_test format3-9.5 {
503 SELECT * FROM t1 WHERE rowid IN
504 (select rowid from t1 where rowid IN (-1,2,4))
508 do_test format3-9.6 {
510 SELECT * FROM t1 WHERE rowid+0 IN
511 (select rowid from t1 where rowid IN (-1,2,4))
515 do_test format3-9.7 {
517 SELECT * FROM t1 WHERE w IN
518 (select rowid from t1 where rowid IN (-1,2,4))
522 do_test format3-9.8 {
524 SELECT * FROM t1 WHERE w+0 IN
525 (select rowid from t1 where rowid IN (-1,2,4))
529 do_test format3-9.9 {
531 SELECT * FROM t1 WHERE x IN (1,7) ORDER BY 1;
534 do_test format3-9.10 {
536 SELECT * FROM t1 WHERE x+0 IN (1,7) ORDER BY 1;
539 do_test format3-9.11 {
541 SELECT * FROM t1 WHERE y IN (6400,8100) ORDER BY 1;
543 } {79 6 6400 89 6 8100 199}
544 do_test format3-9.12 {
546 SELECT * FROM t1 WHERE x=6 AND y IN (6400,8100) ORDER BY 1;
548 } {79 6 6400 89 6 8100 74}
549 do_test format3-9.13 {
551 SELECT * FROM t1 WHERE x IN (1,7) AND y NOT IN (6400,8100) ORDER BY 1;
554 do_test format3-9.14 {
556 SELECT * FROM t1 WHERE x IN (1,7) AND y IN (9,10) ORDER BY 1;
560 # This procedure executes the SQL. Then it checks the generated program
561 # for the SQL and appends a "nosort" to the result if the program contains the
562 # SortCallback opcode. If the program does not contain the SortCallback
563 # opcode it appends "sort"
566 set data [execsql $sql]
567 set prog [execsql "EXPLAIN $sql"]
568 if {[regexp SortCallback $prog]} {set x sort} {set x nosort}
572 # Check out the logic that attempts to implement the ORDER BY clause
573 # using an index rather than by sorting.
575 do_test format3-10.1 {
577 CREATE TABLE t3(a,b,c);
578 CREATE INDEX t3a ON t3(a);
579 CREATE INDEX t3bc ON t3(b,c);
580 CREATE INDEX t3acb ON t3(a,c,b);
581 INSERT INTO t3 SELECT w, 101-w, y FROM t1;
582 SELECT count(*), sum(a), sum(b), sum(c) FROM t3;
584 } {100 5050 5050 348550}
585 do_test format3-10.2 {
587 SELECT * FROM t3 ORDER BY a LIMIT 3
589 } {1 100 4 2 99 9 3 98 16 nosort}
590 do_test format3-10.3 {
592 SELECT * FROM t3 ORDER BY a+1 LIMIT 3
594 } {1 100 4 2 99 9 3 98 16 sort}
595 do_test format3-10.4 {
597 SELECT * FROM t3 WHERE a<10 ORDER BY a LIMIT 3
599 } {1 100 4 2 99 9 3 98 16 nosort}
600 do_test format3-10.5 {
602 SELECT * FROM t3 WHERE a>0 AND a<10 ORDER BY a LIMIT 3
604 } {1 100 4 2 99 9 3 98 16 nosort}
605 do_test format3-10.6 {
607 SELECT * FROM t3 WHERE a>0 ORDER BY a LIMIT 3
609 } {1 100 4 2 99 9 3 98 16 nosort}
610 do_test format3-10.7 {
612 SELECT * FROM t3 WHERE b>0 ORDER BY a LIMIT 3
614 } {1 100 4 2 99 9 3 98 16 sort}
615 do_test format3-10.8 {
617 SELECT * FROM t3 WHERE a IN (3,5,7,1,9,4,2) ORDER BY a LIMIT 3
619 } {1 100 4 2 99 9 3 98 16 sort}
620 do_test format3-10.9 {
622 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
625 do_test format3-10.10 {
627 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a LIMIT 3
630 do_test format3-10.11 {
632 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c LIMIT 3
635 do_test format3-10.12 {
637 SELECT * FROM t3 WHERE a=1 AND c>0 ORDER BY a,c,b LIMIT 3
640 do_test format3-10.13 {
642 SELECT * FROM t3 WHERE a>0 ORDER BY a DESC LIMIT 3
644 } {100 1 10201 99 2 10000 98 3 9801 nosort}
645 do_test format3-10.13.1 {
647 SELECT * FROM t3 WHERE a>0 ORDER BY a+1 DESC LIMIT 3
649 } {100 1 10201 99 2 10000 98 3 9801 sort}
650 do_test format3-10.14 {
652 SELECT * FROM t3 ORDER BY b LIMIT 3
654 } {100 1 10201 99 2 10000 98 3 9801 nosort}
655 do_test format3-10.15 {
657 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t3.a LIMIT 3
659 } {1 0 2 1 3 1 nosort}
660 do_test format3-10.16 {
662 SELECT t3.a, t1.x FROM t3, t1 WHERE t3.a=t1.w ORDER BY t1.x, t3.a LIMIT 3
665 do_test format3-10.17 {
667 SELECT y FROM t1 ORDER BY w COLLATE text LIMIT 3;
670 do_test format3-10.18 {
672 SELECT y FROM t1 ORDER BY w COLLATE numeric LIMIT 3;
675 do_test format3-10.19 {
677 SELECT y FROM t1 ORDER BY w LIMIT 3;
681 # Check that all comparisons are numeric. Similar tests in misc1.test
682 # check the same comparisons on a format4+ database and find that some
683 # are numeric and some are text.
685 do_test format3-11.1 {
686 execsql {SELECT '0'=='0.0'}
688 do_test format3-11.2 {
689 execsql {SELECT '0'==0.0}
691 do_test format3-11.3 {
692 execsql {SELECT '123456789012345678901'=='123456789012345678900'}
694 do_test format3-11.4 {
696 CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
697 INSERT INTO t6 VALUES('0','0.0');
701 do_test format3-11.5 {
703 INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
707 do_test format3-11.6 {
709 INSERT OR IGNORE INTO t6 VALUES('y',0);
713 do_test format3-11.7 {
715 CREATE TABLE t7(x INTEGER, y TEXT, z);
716 INSERT INTO t7 VALUES(0,0,1);
717 INSERT INTO t7 VALUES(0.0,0,2);
718 INSERT INTO t7 VALUES(0,0.0,3);
719 INSERT INTO t7 VALUES(0.0,0.0,4);
720 SELECT DISTINCT x, y FROM t7 ORDER BY z;
724 # Make sure attempts to attach a format 3 database fail.
726 do_test format3-12.1 {
727 file delete -force test2.db
730 CREATE TABLE t8(x,y);
731 ATTACH DATABASE 'test.db' AS format3;
733 } {1 {incompatible file format in auxiliary database: format3}}
734 do_test format3-12.2 {
736 ATTACH DATABASE 'test2.db' AS test2;
738 } {1 {cannot attach auxiliary databases to an older format master database}}