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 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this file is testing the sorter (code in vdbesort.c).
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
21 sqlite3_config_pmasz 10
25 # Create a bunch of data to sort against
36 INSERT INTO t1 VALUES(1,'one',0,'I',3.141592653);
37 INSERT INTO t1 VALUES(2,'two',1,'II',2.15);
38 INSERT INTO t1 VALUES(3,'three',1,'III',4221.0);
39 INSERT INTO t1 VALUES(4,'four',2,'IV',-0.0013442);
40 INSERT INTO t1 VALUES(5,'five',2,'V',-11);
41 INSERT INTO t1 VALUES(6,'six',2,'VI',0.123);
42 INSERT INTO t1 VALUES(7,'seven',2,'VII',123.0);
43 INSERT INTO t1 VALUES(8,'eight',3,'VIII',-1.6);
45 execsql {SELECT count(*) FROM t1}
49 execsql {SELECT n FROM t1 ORDER BY n}
52 execsql {SELECT n FROM t1 ORDER BY n ASC}
55 execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
58 execsql {SELECT n FROM t1 ORDER BY n DESC}
61 execsql {SELECT v FROM t1 ORDER BY v}
62 } {eight five four one seven six three two}
64 execsql {SELECT n FROM t1 ORDER BY v}
67 execsql {SELECT n FROM t1 ORDER BY v DESC}
70 execsql {SELECT flt FROM t1 ORDER BY flt}
71 } {-11.0 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
73 execsql {SELECT flt FROM t1 ORDER BY flt DESC}
74 } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11.0}
76 execsql {SELECT roman FROM t1 ORDER BY roman}
77 } {I II III IV V VI VII VIII}
79 execsql {SELECT n FROM t1 ORDER BY log, flt}
82 execsql {SELECT n FROM t1 ORDER BY log asc, flt}
85 execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
88 execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
91 execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
94 execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
97 execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
100 execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
103 # These tests are designed to reach some hard-to-reach places
104 # inside the string comparison routines.
106 # (Later) The sorting behavior changed in 2.7.0. But we will
107 # keep these tests. You can never have too many test cases!
111 UPDATE t1 SET v='x' || -flt;
112 UPDATE t1 SET v='x-2b' where v=='x-0.123';
113 SELECT v FROM t1 ORDER BY v;
115 } {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
118 SELECT v FROM t1 ORDER BY substr(v,2,999);
120 } {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
123 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
125 } {x-4221.0 x-123.0 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11.0}
128 SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
130 } {x11.0 x1.6 x0.0013442 x-4221.0 x-3.141592653 x-2b x-2.15 x-123.0}
133 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
135 } {x11.0 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123.0 x-4221.0}
137 # This is a bug fix for 2.2.4.
138 # Strings are normally mapped to upper-case for a caseless comparison.
139 # But this can cause problems for characters in between 'Z' and 'a'.
143 CREATE TABLE t2(a,b);
144 INSERT INTO t2 VALUES('AGLIENTU',1);
145 INSERT INTO t2 VALUES('AGLIE`',2);
146 INSERT INTO t2 VALUES('AGNA',3);
147 SELECT a, b FROM t2 ORDER BY a;
149 } {AGLIENTU 1 AGLIE` 2 AGNA 3}
152 SELECT a, b FROM t2 ORDER BY a DESC;
154 } {AGNA 3 AGLIE` 2 AGLIENTU 1}
158 INSERT INTO t2 VALUES('aglientu',1);
159 INSERT INTO t2 VALUES('aglie`',2);
160 INSERT INTO t2 VALUES('agna',3);
161 SELECT a, b FROM t2 ORDER BY a;
163 } {aglie` 2 aglientu 1 agna 3}
166 SELECT a, b FROM t2 ORDER BY a DESC;
168 } {agna 3 aglientu 1 aglie` 2}
170 # Version 2.7.0 testing.
174 INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
175 INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
176 INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
177 INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
178 SELECT n FROM t1 ORDER BY n;
180 } {1 2 3 4 5 6 7 8 9 10 11 12}
183 SELECT n||'' FROM t1 ORDER BY 1;
185 } {1 10 11 12 2 3 4 5 6 7 8 9}
188 SELECT n+0 FROM t1 ORDER BY 1;
190 } {1 2 3 4 5 6 7 8 9 10 11 12}
193 SELECT n||'' FROM t1 ORDER BY 1 DESC;
195 } {9 8 7 6 5 4 3 2 12 11 10 1}
198 SELECT n+0 FROM t1 ORDER BY 1 DESC;
200 } {12 11 10 9 8 7 6 5 4 3 2 1}
203 SELECT v FROM t1 ORDER BY 1;
205 } {x-123.0 x-2.15 x-2b x-3.141592653 x-4.0e9 x-4221.0 x0.0013442 x01234567890123456789 x1.6 x11.0 x2.7 x5.0e10}
208 SELECT v FROM t1 ORDER BY 1 DESC;
210 } {x5.0e10 x2.7 x11.0 x1.6 x01234567890123456789 x0.0013442 x-4221.0 x-4.0e9 x-3.141592653 x-2b x-2.15 x-123.0}
213 SELECT substr(v,2,99) FROM t1 ORDER BY 1;
215 } {-123.0 -2.15 -2b -3.141592653 -4.0e9 -4221.0 0.0013442 01234567890123456789 1.6 11.0 2.7 5.0e10}
218 # SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
220 #} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
224 create table t3(a,b);
225 insert into t3 values(5,NULL);
226 insert into t3 values(6,NULL);
227 insert into t3 values(3,NULL);
228 insert into t3 values(4,'cd');
229 insert into t3 values(1,'ab');
230 insert into t3 values(2,NULL);
231 select a from t3 order by b, a;
236 select a from t3 order by b, a desc;
241 select a from t3 order by b desc, a;
246 select a from t3 order by b desc, a desc;
252 create index i3 on t3(b,a);
253 select a from t3 order by b, a;
258 select a from t3 order by b, a desc;
263 select a from t3 order by b desc, a;
268 select a from t3 order by b desc, a desc;
278 INSERT INTO t4 VALUES(1,1);
279 INSERT INTO t4 VALUES(2,2);
280 INSERT INTO t4 VALUES(11,11);
281 INSERT INTO t4 VALUES(12,12);
282 SELECT a FROM t4 ORDER BY 1;
287 SELECT b FROM t4 ORDER BY 1
291 # Omit tests sort-7.3 to sort-7.8 if view support was disabled at
292 # compilatation time.
296 CREATE VIEW v4 AS SELECT * FROM t4;
297 SELECT a FROM v4 ORDER BY 1;
302 SELECT b FROM v4 ORDER BY 1;
309 SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
314 SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
316 } {1 2 11 12 1 11 12 2} ;# text from t4.b and numeric from v4.a
319 SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
321 } {1 2 11 12 1 11 12 2} ;# numeric from t4.a and text from v4.b
324 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
327 } ;# ifcapable compound
330 #### Version 3 works differently here:
333 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
338 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
343 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
348 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
353 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
358 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
366 CREATE TABLE t5(a real, b text);
367 INSERT INTO t5 VALUES(100,'A1');
368 INSERT INTO t5 VALUES(100.0,'A2');
369 SELECT * FROM t5 ORDER BY a, b;
371 } {100.0 A1 100.0 A2}
374 ifcapable {bloblit} {
375 # BLOBs should sort after TEXT
379 CREATE TABLE t6(x, y);
380 INSERT INTO t6 VALUES(1,1);
381 INSERT INTO t6 VALUES(2,'1');
382 INSERT INTO t6 VALUES(3,x'31');
383 INSERT INTO t6 VALUES(4,NULL);
384 SELECT x FROM t6 ORDER BY y;
389 SELECT x FROM t6 ORDER BY y DESC;
394 SELECT x FROM t6 WHERE y<1
399 SELECT x FROM t6 WHERE y<'1'
404 SELECT x FROM t6 WHERE y<x'31'
409 SELECT x FROM t6 WHERE y>1
414 SELECT x FROM t6 WHERE y>'1'
419 # Ticket #1092 - ORDER BY on rowid fields.
422 CREATE TABLE t7(c INTEGER PRIMARY KEY);
423 INSERT INTO t7 VALUES(1);
424 INSERT INTO t7 VALUES(2);
425 INSERT INTO t7 VALUES(3);
426 INSERT INTO t7 VALUES(4);
431 SELECT c FROM t7 WHERE c<=3 ORDER BY c DESC;
436 SELECT c FROM t7 WHERE c<3 ORDER BY c DESC;
440 # ticket #1358. Just because one table in a join gives a unique
441 # result does not mean they all do. We cannot disable sorting unless
442 # all tables in the join give unique results.
446 create table t8(a unique, b, c);
447 insert into t8 values(1,2,3);
448 insert into t8 values(2,3,4);
449 create table t9(x,y);
450 insert into t9 values(2,4);
451 insert into t9 values(2,3);
452 select y from t8, t9 where a=1 order by a, y;
456 # Trouble reported on the mailing list. Check for overly aggressive
457 # (which is to say, incorrect) optimization of order-by with a rowid
462 create table a (id integer primary key);
463 create table b (id integer primary key, aId integer, text);
464 insert into a values (1);
465 insert into b values (2, 1, 'xxx');
466 insert into b values (1, 1, 'zzz');
467 insert into b values (3, 1, 'yyy');
468 select a.id, b.id, b.text from a join b on (a.id = b.aId)
469 order by a.id, b.text;
471 } {1 2 xxx 1 3 yyy 1 1 zzz}
473 #-------------------------------------------------------------------------
474 # Check that the sorter in vdbesort.c sorts in a stable fashion.
476 do_execsql_test sort-13.0 {
477 CREATE TABLE t10(a, b);
481 for {set i 0} {$i < 100000} {incr i} {
482 execsql { INSERT INTO t10 VALUES( $i/10, $i%10 ) }
486 do_execsql_test sort-13.2 {
487 SELECT a, b FROM t10 ORDER BY a;
488 } [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
489 do_execsql_test sort-13.3 {
490 PRAGMA cache_size = 5;
491 SELECT a, b FROM t10 ORDER BY a;
492 } [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
494 #-------------------------------------------------------------------------
496 foreach {tn mmap_limit nWorker tmpstore coremutex fakeheap softheaplimit} {
497 1 0 3 file true false 0
498 2 0 3 file true true 0
499 3 0 0 file true false 0
500 4 1000000 3 file true false 0
501 5 0 0 memory false true 0
502 6 0 0 file false true 1000000
503 7 0 0 file false true 10000
508 sqlite3_config multithread
510 sqlite3_config singlethread
513 sorter_test_fakeheap $fakeheap
514 sqlite3_soft_heap_limit $softheaplimit
517 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $mmap_limit
518 execsql "PRAGMA temp_store = $tmpstore; PRAGMA threads = $nWorker"
521 set ten [string repeat X 10300]
522 set one [string repeat y 200]
524 if {$softheaplimit} {
525 execsql { PRAGMA cache_size = 20 };
527 execsql { PRAGMA cache_size = 5 };
530 do_execsql_test 15.$tn.1 {
532 SELECT 4, $ten UNION ALL
533 SELECT 2, $one UNION ALL
534 SELECT 1, $ten UNION ALL
537 SELECT * FROM rr ORDER BY 1;
538 } [list 1 $ten 2 $one 3 $one 4 $ten]
540 do_execsql_test 15.$tn.2 {
542 INSERT INTO t1 VALUES(4);
543 INSERT INTO t1 VALUES(5);
544 INSERT INTO t1 VALUES(3);
545 INSERT INTO t1 VALUES(2);
546 INSERT INTO t1 VALUES(6);
547 INSERT INTO t1 VALUES(1);
548 CREATE INDEX i1 ON t1(a);
549 SELECT * FROM t1 ORDER BY a;
552 do_execsql_test 15.$tn.3 {
554 SELECT 4, $ten UNION ALL
557 SELECT * FROM rr ORDER BY 1;
558 } [list 2 $one 4 $ten]
560 sorter_test_fakeheap 0
565 set t(0) singlethread
568 sqlite3_config $t($sqlite_options(threadsafe))
570 sqlite3_soft_heap_limit 0
573 do_catchsql_test 16.1 {
574 CREATE TABLE t1(a, b, c);
575 INSERT INTO t1 VALUES(1, 2, 3);
576 INSERT INTO t1 VALUES(1, NULL, 3);
577 INSERT INTO t1 VALUES(NULL, 2, 3);
578 INSERT INTO t1 VALUES(1, 2, NULL);
579 INSERT INTO t1 VALUES(4, 5, 6);
580 CREATE UNIQUE INDEX i1 ON t1(b, a, c);
583 do_catchsql_test 16.2 {
584 CREATE TABLE t1(a, b, c);
585 INSERT INTO t1 VALUES(1, 2, 3);
586 INSERT INTO t1 VALUES(1, NULL, 3);
587 INSERT INTO t1 VALUES(1, 2, 3);
588 INSERT INTO t1 VALUES(1, 2, NULL);
589 INSERT INTO t1 VALUES(4, 5, 6);
590 CREATE UNIQUE INDEX i1 ON t1(b, a, c);
591 } {1 {UNIQUE constraint failed: t1.b, t1.a, t1.c}}
594 do_execsql_test 17.1 {
595 SELECT * FROM sqlite_master ORDER BY sql;