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
20 # Create a bunch of data to sort against
31 INSERT INTO t1 VALUES(1,'one',0,'I',3.141592653);
32 INSERT INTO t1 VALUES(2,'two',1,'II',2.15);
33 INSERT INTO t1 VALUES(3,'three',1,'III',4221.0);
34 INSERT INTO t1 VALUES(4,'four',2,'IV',-0.0013442);
35 INSERT INTO t1 VALUES(5,'five',2,'V',-11);
36 INSERT INTO t1 VALUES(6,'six',2,'VI',0.123);
37 INSERT INTO t1 VALUES(7,'seven',2,'VII',123.0);
38 INSERT INTO t1 VALUES(8,'eight',3,'VIII',-1.6);
40 execsql {SELECT count(*) FROM t1}
44 execsql {SELECT n FROM t1 ORDER BY n}
47 execsql {SELECT n FROM t1 ORDER BY n ASC}
50 execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
53 execsql {SELECT n FROM t1 ORDER BY n DESC}
56 execsql {SELECT v FROM t1 ORDER BY v}
57 } {eight five four one seven six three two}
59 execsql {SELECT n FROM t1 ORDER BY v}
62 execsql {SELECT n FROM t1 ORDER BY v DESC}
65 execsql {SELECT flt FROM t1 ORDER BY flt}
66 } {-11.0 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
68 execsql {SELECT flt FROM t1 ORDER BY flt DESC}
69 } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11.0}
71 execsql {SELECT roman FROM t1 ORDER BY roman}
72 } {I II III IV V VI VII VIII}
74 execsql {SELECT n FROM t1 ORDER BY log, flt}
77 execsql {SELECT n FROM t1 ORDER BY log asc, flt}
80 execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
83 execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
86 execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
89 execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
92 execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
95 execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
98 # These tests are designed to reach some hard-to-reach places
99 # inside the string comparison routines.
101 # (Later) The sorting behavior changed in 2.7.0. But we will
102 # keep these tests. You can never have too many test cases!
106 UPDATE t1 SET v='x' || -flt;
107 UPDATE t1 SET v='x-2b' where v=='x-0.123';
108 SELECT v FROM t1 ORDER BY v;
110 } {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
113 SELECT v FROM t1 ORDER BY substr(v,2,999);
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)+0.0;
120 } {x-4221.0 x-123.0 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11.0}
123 SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
125 } {x11.0 x1.6 x0.0013442 x-4221.0 x-3.141592653 x-2b x-2.15 x-123.0}
128 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
130 } {x11.0 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123.0 x-4221.0}
132 # This is a bug fix for 2.2.4.
133 # Strings are normally mapped to upper-case for a caseless comparison.
134 # But this can cause problems for characters in between 'Z' and 'a'.
138 CREATE TABLE t2(a,b);
139 INSERT INTO t2 VALUES('AGLIENTU',1);
140 INSERT INTO t2 VALUES('AGLIE`',2);
141 INSERT INTO t2 VALUES('AGNA',3);
142 SELECT a, b FROM t2 ORDER BY a;
144 } {AGLIENTU 1 AGLIE` 2 AGNA 3}
147 SELECT a, b FROM t2 ORDER BY a DESC;
149 } {AGNA 3 AGLIE` 2 AGLIENTU 1}
153 INSERT INTO t2 VALUES('aglientu',1);
154 INSERT INTO t2 VALUES('aglie`',2);
155 INSERT INTO t2 VALUES('agna',3);
156 SELECT a, b FROM t2 ORDER BY a;
158 } {aglie` 2 aglientu 1 agna 3}
161 SELECT a, b FROM t2 ORDER BY a DESC;
163 } {agna 3 aglientu 1 aglie` 2}
165 # Version 2.7.0 testing.
169 INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
170 INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
171 INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
172 INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
173 SELECT n FROM t1 ORDER BY n;
175 } {1 2 3 4 5 6 7 8 9 10 11 12}
178 SELECT n||'' FROM t1 ORDER BY 1;
180 } {1 10 11 12 2 3 4 5 6 7 8 9}
183 SELECT n+0 FROM t1 ORDER BY 1;
185 } {1 2 3 4 5 6 7 8 9 10 11 12}
188 SELECT n||'' FROM t1 ORDER BY 1 DESC;
190 } {9 8 7 6 5 4 3 2 12 11 10 1}
193 SELECT n+0 FROM t1 ORDER BY 1 DESC;
195 } {12 11 10 9 8 7 6 5 4 3 2 1}
198 SELECT v FROM t1 ORDER BY 1;
200 } {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}
203 SELECT v FROM t1 ORDER BY 1 DESC;
205 } {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}
208 SELECT substr(v,2,99) FROM t1 ORDER BY 1;
210 } {-123.0 -2.15 -2b -3.141592653 -4.0e9 -4221.0 0.0013442 01234567890123456789 1.6 11.0 2.7 5.0e10}
213 # SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
215 #} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
219 create table t3(a,b);
220 insert into t3 values(5,NULL);
221 insert into t3 values(6,NULL);
222 insert into t3 values(3,NULL);
223 insert into t3 values(4,'cd');
224 insert into t3 values(1,'ab');
225 insert into t3 values(2,NULL);
226 select a from t3 order by b, a;
231 select a from t3 order by b, a desc;
236 select a from t3 order by b desc, a;
241 select a from t3 order by b desc, a desc;
247 create index i3 on t3(b,a);
248 select a from t3 order by b, a;
253 select a from t3 order by b, a desc;
258 select a from t3 order by b desc, a;
263 select a from t3 order by b desc, a desc;
273 INSERT INTO t4 VALUES(1,1);
274 INSERT INTO t4 VALUES(2,2);
275 INSERT INTO t4 VALUES(11,11);
276 INSERT INTO t4 VALUES(12,12);
277 SELECT a FROM t4 ORDER BY 1;
282 SELECT b FROM t4 ORDER BY 1
286 # Omit tests sort-7.3 to sort-7.8 if view support was disabled at
287 # compilatation time.
291 CREATE VIEW v4 AS SELECT * FROM t4;
292 SELECT a FROM v4 ORDER BY 1;
297 SELECT b FROM v4 ORDER BY 1;
304 SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
309 SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
311 } {1 2 11 12 1 11 12 2} ;# text from t4.b and numeric from v4.a
314 SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
316 } {1 2 11 12 1 11 12 2} ;# numeric from t4.a and text from v4.b
319 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
322 } ;# ifcapable compound
325 #### Version 3 works differently here:
328 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
333 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
338 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
343 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
348 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
353 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
361 CREATE TABLE t5(a real, b text);
362 INSERT INTO t5 VALUES(100,'A1');
363 INSERT INTO t5 VALUES(100.0,'A2');
364 SELECT * FROM t5 ORDER BY a, b;
366 } {100.0 A1 100.0 A2}
369 ifcapable {bloblit} {
370 # BLOBs should sort after TEXT
374 CREATE TABLE t6(x, y);
375 INSERT INTO t6 VALUES(1,1);
376 INSERT INTO t6 VALUES(2,'1');
377 INSERT INTO t6 VALUES(3,x'31');
378 INSERT INTO t6 VALUES(4,NULL);
379 SELECT x FROM t6 ORDER BY y;
384 SELECT x FROM t6 ORDER BY y DESC;
389 SELECT x FROM t6 WHERE y<1
394 SELECT x FROM t6 WHERE y<'1'
399 SELECT x FROM t6 WHERE y<x'31'
404 SELECT x FROM t6 WHERE y>1
409 SELECT x FROM t6 WHERE y>'1'
414 # Ticket #1092 - ORDER BY on rowid fields.
417 CREATE TABLE t7(c INTEGER PRIMARY KEY);
418 INSERT INTO t7 VALUES(1);
419 INSERT INTO t7 VALUES(2);
420 INSERT INTO t7 VALUES(3);
421 INSERT INTO t7 VALUES(4);
426 SELECT c FROM t7 WHERE c<=3 ORDER BY c DESC;
431 SELECT c FROM t7 WHERE c<3 ORDER BY c DESC;
435 # ticket #1358. Just because one table in a join gives a unique
436 # result does not mean they all do. We cannot disable sorting unless
437 # all tables in the join give unique results.
441 create table t8(a unique, b, c);
442 insert into t8 values(1,2,3);
443 insert into t8 values(2,3,4);
444 create table t9(x,y);
445 insert into t9 values(2,4);
446 insert into t9 values(2,3);
447 select y from t8, t9 where a=1 order by a, y;
451 # Trouble reported on the mailing list. Check for overly aggressive
452 # (which is to say, incorrect) optimization of order-by with a rowid
457 create table a (id integer primary key);
458 create table b (id integer primary key, aId integer, text);
459 insert into a values (1);
460 insert into b values (2, 1, 'xxx');
461 insert into b values (1, 1, 'zzz');
462 insert into b values (3, 1, 'yyy');
463 select a.id, b.id, b.text from a join b on (a.id = b.aId)
464 order by a.id, b.text;
466 } {1 2 xxx 1 3 yyy 1 1 zzz}
468 #-------------------------------------------------------------------------
469 # Check that the sorter in vdbesort.c sorts in a stable fashion.
471 do_execsql_test sort-13.0 {
472 CREATE TABLE t10(a, b);
476 for {set i 0} {$i < 100000} {incr i} {
477 execsql { INSERT INTO t10 VALUES( $i/10, $i%10 ) }
481 do_execsql_test sort-13.2 {
482 SELECT a, b FROM t10 ORDER BY a;
483 } [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
484 do_execsql_test sort-13.3 {
485 PRAGMA cache_size = 5;
486 SELECT a, b FROM t10 ORDER BY a;
487 } [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
489 #-------------------------------------------------------------------------
490 # Sort some large ( > 4KiB) records.
497 set i1 [expr (($i2<<3) + $a) & 0x7FFFFFFF]
498 set i2 [expr (($i1<<3) + $b) & 0x7FFFFFFF]
504 do_execsql_test sort-14.0 {
505 PRAGMA cache_size = 5;
506 CREATE TABLE t11(a, b);
507 INSERT INTO t11 VALUES(randomblob(5000), NULL);
508 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --2
509 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --3
510 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --4
511 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --5
512 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --6
513 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --7
514 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --8
515 INSERT INTO t11 SELECT randomblob(5000), NULL FROM t11; --9
516 UPDATE t11 SET b = cksum(a);
519 foreach {tn mmap_limit} {
523 do_test sort-14.$tn {
524 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $mmap_limit
526 db eval { SELECT * FROM t11 ORDER BY b } {
527 if {$b != [cksum $a]} {error "checksum failed"}
528 if {[string compare $b $prev] < 0} {error "sort failed"}
535 #-------------------------------------------------------------------------
537 foreach {tn mmap_limit nWorker tmpstore coremutex fakeheap softheaplimit} {
538 1 0 3 file true false 0
539 2 0 3 file true true 0
540 3 0 0 file true false 0
541 4 1000000 3 file true false 0
542 5 0 0 memory false true 0
543 6 0 0 file false true 1000000
544 7 0 0 file false true 10000
549 sqlite3_config multithread
551 sqlite3_config singlethread
554 sorter_test_fakeheap $fakeheap
555 sqlite3_soft_heap_limit $softheaplimit
558 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $mmap_limit
559 execsql "PRAGMA temp_store = $tmpstore; PRAGMA threads = $nWorker"
562 set ten [string repeat X 10300]
563 set one [string repeat y 200]
565 if {$softheaplimit} {
566 execsql { PRAGMA cache_size = 20 };
568 execsql { PRAGMA cache_size = 5 };
571 do_execsql_test 15.$tn.1 {
573 SELECT 4, $ten UNION ALL
574 SELECT 2, $one UNION ALL
575 SELECT 1, $ten UNION ALL
578 SELECT * FROM rr ORDER BY 1;
579 } [list 1 $ten 2 $one 3 $one 4 $ten]
581 do_execsql_test 15.$tn.2 {
583 INSERT INTO t1 VALUES(4);
584 INSERT INTO t1 VALUES(5);
585 INSERT INTO t1 VALUES(3);
586 INSERT INTO t1 VALUES(2);
587 INSERT INTO t1 VALUES(6);
588 INSERT INTO t1 VALUES(1);
589 CREATE INDEX i1 ON t1(a);
590 SELECT * FROM t1 ORDER BY a;
593 do_execsql_test 15.$tn.3 {
595 SELECT 4, $ten UNION ALL
598 SELECT * FROM rr ORDER BY 1;
599 } [list 2 $one 4 $ten]
601 sorter_test_fakeheap 0
606 set t(0) singlethread
609 sqlite3_config $t($sqlite_options(threadsafe))
611 sqlite3_soft_heap_limit 0
614 do_catchsql_test 16.1 {
615 CREATE TABLE t1(a, b, c);
616 INSERT INTO t1 VALUES(1, 2, 3);
617 INSERT INTO t1 VALUES(1, NULL, 3);
618 INSERT INTO t1 VALUES(NULL, 2, 3);
619 INSERT INTO t1 VALUES(1, 2, NULL);
620 INSERT INTO t1 VALUES(4, 5, 6);
621 CREATE UNIQUE INDEX i1 ON t1(b, a, c);
624 do_catchsql_test 16.2 {
625 CREATE TABLE t1(a, b, c);
626 INSERT INTO t1 VALUES(1, 2, 3);
627 INSERT INTO t1 VALUES(1, NULL, 3);
628 INSERT INTO t1 VALUES(1, 2, 3);
629 INSERT INTO t1 VALUES(1, 2, NULL);
630 INSERT INTO t1 VALUES(4, 5, 6);
631 CREATE UNIQUE INDEX i1 ON t1(b, a, c);
632 } {1 {UNIQUE constraint failed: t1.b, t1.a, t1.c}}
635 do_execsql_test 17.1 {
636 SELECT * FROM sqlite_master ORDER BY sql;