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 CREATE TABLE statement.
14 # $Id: sort.test,v 1.19 2005/02/02 01:10:45 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Create a bunch of data to sort against
30 INSERT INTO t1 VALUES(1,'one',0,'I',3.141592653);
31 INSERT INTO t1 VALUES(2,'two',1,'II',2.15);
32 INSERT INTO t1 VALUES(3,'three',1,'III',4221.0);
33 INSERT INTO t1 VALUES(4,'four',2,'IV',-0.0013442);
34 INSERT INTO t1 VALUES(5,'five',2,'V',-11);
35 INSERT INTO t1 VALUES(6,'six',2,'VI',0.123);
36 INSERT INTO t1 VALUES(7,'seven',2,'VII',123.0);
37 INSERT INTO t1 VALUES(8,'eight',3,'VIII',-1.6);
39 execsql {SELECT count(*) FROM t1}
43 execsql {SELECT n FROM t1 ORDER BY n}
46 execsql {SELECT n FROM t1 ORDER BY n ASC}
49 execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
52 execsql {SELECT n FROM t1 ORDER BY n DESC}
55 execsql {SELECT v FROM t1 ORDER BY v}
56 } {eight five four one seven six three two}
58 execsql {SELECT n FROM t1 ORDER BY v}
61 execsql {SELECT n FROM t1 ORDER BY v DESC}
64 execsql {SELECT flt FROM t1 ORDER BY flt}
65 } {-11 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
67 execsql {SELECT flt FROM t1 ORDER BY flt DESC}
68 } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11}
70 execsql {SELECT roman FROM t1 ORDER BY roman}
71 } {I II III IV V VI VII VIII}
73 execsql {SELECT n FROM t1 ORDER BY log, flt}
76 execsql {SELECT n FROM t1 ORDER BY log asc, flt}
79 execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
82 execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
85 execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
88 execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
91 execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
94 execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
97 # These tests are designed to reach some hard-to-reach places
98 # inside the string comparison routines.
100 # (Later) The sorting behavior changed in 2.7.0. But we will
101 # keep these tests. You can never have too many test cases!
105 UPDATE t1 SET v='x' || -flt;
106 UPDATE t1 SET v='x-2b' where v=='x-0.123';
107 SELECT v FROM t1 ORDER BY v;
109 } {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
112 SELECT v FROM t1 ORDER BY substr(v,2,999);
114 } {x-123 x-2.15 x-2b x-3.141592653 x-4221 x0.0013442 x1.6 x11}
117 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
119 } {x-4221 x-123 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11}
122 SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
124 } {x11 x1.6 x0.0013442 x-4221 x-3.141592653 x-2b x-2.15 x-123}
127 SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
129 } {x11 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123 x-4221}
131 # This is a bug fix for 2.2.4.
132 # Strings are normally mapped to upper-case for a caseless comparison.
133 # But this can cause problems for characters in between 'Z' and 'a'.
137 CREATE TABLE t2(a,b);
138 INSERT INTO t2 VALUES('AGLIENTU',1);
139 INSERT INTO t2 VALUES('AGLIE`',2);
140 INSERT INTO t2 VALUES('AGNA',3);
141 SELECT a, b FROM t2 ORDER BY a;
143 } {AGLIENTU 1 AGLIE` 2 AGNA 3}
146 SELECT a, b FROM t2 ORDER BY a DESC;
148 } {AGNA 3 AGLIE` 2 AGLIENTU 1}
152 INSERT INTO t2 VALUES('aglientu',1);
153 INSERT INTO t2 VALUES('aglie`',2);
154 INSERT INTO t2 VALUES('agna',3);
155 SELECT a, b FROM t2 ORDER BY a;
157 } {aglie` 2 aglientu 1 agna 3}
160 SELECT a, b FROM t2 ORDER BY a DESC;
162 } {agna 3 aglientu 1 aglie` 2}
164 # Version 2.7.0 testing.
168 INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
169 INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
170 INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
171 INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
172 SELECT n FROM t1 ORDER BY n;
174 } {1 2 3 4 5 6 7 8 9 10 11 12}
177 SELECT n||'' FROM t1 ORDER BY 1;
179 } {1 10 11 12 2 3 4 5 6 7 8 9}
182 SELECT n+0 FROM t1 ORDER BY 1;
184 } {1 2 3 4 5 6 7 8 9 10 11 12}
187 SELECT n||'' FROM t1 ORDER BY 1 DESC;
189 } {9 8 7 6 5 4 3 2 12 11 10 1}
192 SELECT n+0 FROM t1 ORDER BY 1 DESC;
194 } {12 11 10 9 8 7 6 5 4 3 2 1}
197 SELECT v FROM t1 ORDER BY 1;
199 } {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}
202 SELECT v FROM t1 ORDER BY 1 DESC;
204 } {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}
207 SELECT substr(v,2,99) FROM t1 ORDER BY 1;
209 } {-123 -2.15 -2b -3.141592653 -4.0e9 -4221 0.0013442 01234567890123456789 1.6 11 2.7 5.0e10}
212 # SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
214 #} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
218 create table t3(a,b);
219 insert into t3 values(5,NULL);
220 insert into t3 values(6,NULL);
221 insert into t3 values(3,NULL);
222 insert into t3 values(4,'cd');
223 insert into t3 values(1,'ab');
224 insert into t3 values(2,NULL);
225 select a from t3 order by b, a;
230 select a from t3 order by b, a desc;
235 select a from t3 order by b desc, a;
240 select a from t3 order by b desc, a desc;
246 create index i3 on t3(b,a);
247 select a from t3 order by b, a;
252 select a from t3 order by b, a desc;
257 select a from t3 order by b desc, a;
262 select a from t3 order by b desc, a desc;
272 INSERT INTO t4 VALUES(1,1);
273 INSERT INTO t4 VALUES(2,2);
274 INSERT INTO t4 VALUES(11,11);
275 INSERT INTO t4 VALUES(12,12);
276 SELECT a FROM t4 ORDER BY 1;
281 SELECT b FROM t4 ORDER BY 1
285 # Omit tests sort-7.3 to sort-7.8 if view support was disabled at
286 # compilatation time.
290 CREATE VIEW v4 AS SELECT * FROM t4;
291 SELECT a FROM v4 ORDER BY 1;
296 SELECT b FROM v4 ORDER BY 1;
303 SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
308 SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
310 } {1 2 11 12 1 11 12 2} ;# text from t4.b and numeric from v4.a
313 SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
315 } {1 2 11 12 1 11 12 2} ;# numeric from t4.a and text from v4.b
318 SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
321 } ;# ifcapable compound
324 #### Version 3 works differently here:
327 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
332 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
337 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
342 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
347 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
352 # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
360 CREATE TABLE t5(a real, b text);
361 INSERT INTO t5 VALUES(100,'A1');
362 INSERT INTO t5 VALUES(100.0,'A2');
363 SELECT * FROM t5 ORDER BY a, b;
368 ifcapable {bloblit} {
369 # BLOBs should sort after TEXT
373 CREATE TABLE t6(x, y);
374 INSERT INTO t6 VALUES(1,1);
375 INSERT INTO t6 VALUES(2,'1');
376 INSERT INTO t6 VALUES(3,x'31');
377 INSERT INTO t6 VALUES(4,NULL);
378 SELECT x FROM t6 ORDER BY y;
383 SELECT x FROM t6 ORDER BY y DESC;
388 SELECT x FROM t6 WHERE y<1
393 SELECT x FROM t6 WHERE y<'1'
398 SELECT x FROM t6 WHERE y<x'31'
403 SELECT x FROM t6 WHERE y>1
408 SELECT x FROM t6 WHERE y>'1'
413 # Ticket #1092 - ORDER BY on rowid fields.
416 CREATE TABLE t7(c INTEGER PRIMARY KEY);
417 INSERT INTO t7 VALUES(1);
418 INSERT INTO t7 VALUES(2);
419 INSERT INTO t7 VALUES(3);
420 INSERT INTO t7 VALUES(4);
425 SELECT c FROM t7 WHERE c<=3 ORDER BY c DESC;
430 SELECT c FROM t7 WHERE c<3 ORDER BY c DESC;