xref: /sqlite-3.40.0/test/sort.test (revision be7721d1)
10202b29eSdanielk1977# 2001 September 15.
259692544Sdrh#
3b19a2bc6Sdrh# The author disclaims copyright to this source code.  In place of
4b19a2bc6Sdrh# a legal notice, here is a blessing:
559692544Sdrh#
6b19a2bc6Sdrh#    May you do good and not evil.
7b19a2bc6Sdrh#    May you find forgiveness for yourself and forgive others.
8b19a2bc6Sdrh#    May you share freely, never taking more than you give.
959692544Sdrh#
1059692544Sdrh#***********************************************************************
1159692544Sdrh#
120d51def2Sdan# This file implements regression tests for SQLite library.  The
130d51def2Sdan# focus of this file is testing the sorter (code in vdbesort.c).
140d51def2Sdan#
1559692544Sdrh
1659692544Sdrhset testdir [file dirname $argv0]
1759692544Sdrhsource $testdir/tester.tcl
1892c2e0daSmistachkinset testprefix sort
19*3bd1791dSdrhdb close
20*3bd1791dSdrhsqlite3_shutdown
21*3bd1791dSdrhsqlite3_config_pmasz 10
22*3bd1791dSdrhsqlite3_initialize
23*3bd1791dSdrhsqlite3 db test.db
2459692544Sdrh
2559692544Sdrh# Create a bunch of data to sort against
2659692544Sdrh#
2759692544Sdrhdo_test sort-1.0 {
2859692544Sdrh  execsql {
2959692544Sdrh    CREATE TABLE t1(
3059692544Sdrh       n int,
3159692544Sdrh       v varchar(10),
3259692544Sdrh       log int,
3359692544Sdrh       roman varchar(10),
3459692544Sdrh       flt real
3559692544Sdrh    );
365f3b4ab5Sdrh    INSERT INTO t1 VALUES(1,'one',0,'I',3.141592653);
375f3b4ab5Sdrh    INSERT INTO t1 VALUES(2,'two',1,'II',2.15);
385f3b4ab5Sdrh    INSERT INTO t1 VALUES(3,'three',1,'III',4221.0);
395f3b4ab5Sdrh    INSERT INTO t1 VALUES(4,'four',2,'IV',-0.0013442);
405f3b4ab5Sdrh    INSERT INTO t1 VALUES(5,'five',2,'V',-11);
415f3b4ab5Sdrh    INSERT INTO t1 VALUES(6,'six',2,'VI',0.123);
425f3b4ab5Sdrh    INSERT INTO t1 VALUES(7,'seven',2,'VII',123.0);
435f3b4ab5Sdrh    INSERT INTO t1 VALUES(8,'eight',3,'VIII',-1.6);
4459692544Sdrh  }
4559692544Sdrh  execsql {SELECT count(*) FROM t1}
4659692544Sdrh} {8}
4759692544Sdrh
4859692544Sdrhdo_test sort-1.1 {
4959692544Sdrh  execsql {SELECT n FROM t1 ORDER BY n}
5059692544Sdrh} {1 2 3 4 5 6 7 8}
51c4a3c779Sdrhdo_test sort-1.1.1 {
52c4a3c779Sdrh  execsql {SELECT n FROM t1 ORDER BY n ASC}
53c4a3c779Sdrh} {1 2 3 4 5 6 7 8}
54c4a3c779Sdrhdo_test sort-1.1.1 {
55c4a3c779Sdrh  execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
56c4a3c779Sdrh} {1 2 3 4 5 6 7 8}
5759692544Sdrhdo_test sort-1.2 {
5859692544Sdrh  execsql {SELECT n FROM t1 ORDER BY n DESC}
5959692544Sdrh} {8 7 6 5 4 3 2 1}
6059692544Sdrhdo_test sort-1.3a {
6159692544Sdrh  execsql {SELECT v FROM t1 ORDER BY v}
6259692544Sdrh} {eight five four one seven six three two}
6359692544Sdrhdo_test sort-1.3b {
6459692544Sdrh  execsql {SELECT n FROM t1 ORDER BY v}
6559692544Sdrh} {8 5 4 1 7 6 3 2}
6659692544Sdrhdo_test sort-1.4 {
6759692544Sdrh  execsql {SELECT n FROM t1 ORDER BY v DESC}
6859692544Sdrh} {2 3 6 7 1 4 5 8}
6959692544Sdrhdo_test sort-1.5 {
7059692544Sdrh  execsql {SELECT flt FROM t1 ORDER BY flt}
718a51256cSdrh} {-11.0 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
7259692544Sdrhdo_test sort-1.6 {
7359692544Sdrh  execsql {SELECT flt FROM t1 ORDER BY flt DESC}
748a51256cSdrh} {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11.0}
7559692544Sdrhdo_test sort-1.7 {
7659692544Sdrh  execsql {SELECT roman FROM t1 ORDER BY roman}
7759692544Sdrh} {I II III IV V VI VII VIII}
7859692544Sdrhdo_test sort-1.8 {
7959692544Sdrh  execsql {SELECT n FROM t1 ORDER BY log, flt}
8059692544Sdrh} {1 2 3 5 4 6 7 8}
81c4a3c779Sdrhdo_test sort-1.8.1 {
82c4a3c779Sdrh  execsql {SELECT n FROM t1 ORDER BY log asc, flt}
83c4a3c779Sdrh} {1 2 3 5 4 6 7 8}
84c4a3c779Sdrhdo_test sort-1.8.2 {
85c4a3c779Sdrh  execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
86c4a3c779Sdrh} {1 2 3 5 4 6 7 8}
87c4a3c779Sdrhdo_test sort-1.8.3 {
88c4a3c779Sdrh  execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
89c4a3c779Sdrh} {1 2 3 5 4 6 7 8}
9059692544Sdrhdo_test sort-1.9 {
9159692544Sdrh  execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
9259692544Sdrh} {1 3 2 7 6 4 5 8}
93c4a3c779Sdrhdo_test sort-1.9.1 {
94c4a3c779Sdrh  execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
95c4a3c779Sdrh} {1 3 2 7 6 4 5 8}
9659692544Sdrhdo_test sort-1.10 {
9759692544Sdrh  execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
9859692544Sdrh} {8 5 4 6 7 2 3 1}
9959692544Sdrhdo_test sort-1.11 {
10059692544Sdrh  execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
10159692544Sdrh} {8 7 6 4 5 3 2 1}
10259692544Sdrh
103c4a3c779Sdrh# These tests are designed to reach some hard-to-reach places
104c4a3c779Sdrh# inside the string comparison routines.
105c4a3c779Sdrh#
106a9e99aeeSdrh# (Later) The sorting behavior changed in 2.7.0.  But we will
107a9e99aeeSdrh# keep these tests.  You can never have too many test cases!
108a9e99aeeSdrh#
109a9e99aeeSdrhdo_test sort-2.1.1 {
110c4a3c779Sdrh  execsql {
111c4a3c779Sdrh    UPDATE t1 SET v='x' || -flt;
112c4a3c779Sdrh    UPDATE t1 SET v='x-2b' where v=='x-0.123';
113c4a3c779Sdrh    SELECT v FROM t1 ORDER BY v;
114c4a3c779Sdrh  }
1158a51256cSdrh} {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
116a9e99aeeSdrhdo_test sort-2.1.2 {
117c4a3c779Sdrh  execsql {
118a9e99aeeSdrh    SELECT v FROM t1 ORDER BY substr(v,2,999);
119c4a3c779Sdrh  }
1208a51256cSdrh} {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
121a9e99aeeSdrhdo_test sort-2.1.3 {
122c4a3c779Sdrh  execsql {
123a9e99aeeSdrh    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
124c4a3c779Sdrh  }
1258a51256cSdrh} {x-4221.0 x-123.0 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11.0}
126a9e99aeeSdrhdo_test sort-2.1.4 {
127a9e99aeeSdrh  execsql {
128a9e99aeeSdrh    SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
129a9e99aeeSdrh  }
1308a51256cSdrh} {x11.0 x1.6 x0.0013442 x-4221.0 x-3.141592653 x-2b x-2.15 x-123.0}
131a9e99aeeSdrhdo_test sort-2.1.5 {
132a9e99aeeSdrh  execsql {
133a9e99aeeSdrh    SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
134a9e99aeeSdrh  }
1358a51256cSdrh} {x11.0 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123.0 x-4221.0}
136c4a3c779Sdrh
1379208643dSdrh# This is a bug fix for 2.2.4.
1389208643dSdrh# Strings are normally mapped to upper-case for a caseless comparison.
1399208643dSdrh# But this can cause problems for characters in between 'Z' and 'a'.
1409208643dSdrh#
1419208643dSdrhdo_test sort-3.1 {
1429208643dSdrh  execsql {
1439208643dSdrh    CREATE TABLE t2(a,b);
1449208643dSdrh    INSERT INTO t2 VALUES('AGLIENTU',1);
1459208643dSdrh    INSERT INTO t2 VALUES('AGLIE`',2);
1469208643dSdrh    INSERT INTO t2 VALUES('AGNA',3);
1479208643dSdrh    SELECT a, b FROM t2 ORDER BY a;
1489208643dSdrh  }
1499208643dSdrh} {AGLIENTU 1 AGLIE` 2 AGNA 3}
1509208643dSdrhdo_test sort-3.2 {
1519208643dSdrh  execsql {
1529208643dSdrh    SELECT a, b FROM t2 ORDER BY a DESC;
1539208643dSdrh  }
1549208643dSdrh} {AGNA 3 AGLIE` 2 AGLIENTU 1}
1559208643dSdrhdo_test sort-3.3 {
1569208643dSdrh  execsql {
1579208643dSdrh    DELETE FROM t2;
1589208643dSdrh    INSERT INTO t2 VALUES('aglientu',1);
1599208643dSdrh    INSERT INTO t2 VALUES('aglie`',2);
1609208643dSdrh    INSERT INTO t2 VALUES('agna',3);
1619208643dSdrh    SELECT a, b FROM t2 ORDER BY a;
1629208643dSdrh  }
1639208643dSdrh} {aglie` 2 aglientu 1 agna 3}
1649208643dSdrhdo_test sort-3.4 {
1659208643dSdrh  execsql {
1669208643dSdrh    SELECT a, b FROM t2 ORDER BY a DESC;
1679208643dSdrh  }
1689208643dSdrh} {agna 3 aglientu 1 aglie` 2}
1699208643dSdrh
170a9e99aeeSdrh# Version 2.7.0 testing.
171a9e99aeeSdrh#
172a9e99aeeSdrhdo_test sort-4.1 {
173a9e99aeeSdrh  execsql {
174a9e99aeeSdrh    INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
175a9e99aeeSdrh    INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
176a9e99aeeSdrh    INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
177a9e99aeeSdrh    INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
178a9e99aeeSdrh    SELECT n FROM t1 ORDER BY n;
179a9e99aeeSdrh  }
180a9e99aeeSdrh} {1 2 3 4 5 6 7 8 9 10 11 12}
181a9e99aeeSdrhdo_test sort-4.2 {
182a9e99aeeSdrh  execsql {
183a9e99aeeSdrh    SELECT n||'' FROM t1 ORDER BY 1;
184a9e99aeeSdrh  }
185a9e99aeeSdrh} {1 10 11 12 2 3 4 5 6 7 8 9}
186a9e99aeeSdrhdo_test sort-4.3 {
187a9e99aeeSdrh  execsql {
188a9e99aeeSdrh    SELECT n+0 FROM t1 ORDER BY 1;
189a9e99aeeSdrh  }
190a9e99aeeSdrh} {1 2 3 4 5 6 7 8 9 10 11 12}
191a9e99aeeSdrhdo_test sort-4.4 {
192a9e99aeeSdrh  execsql {
193a9e99aeeSdrh    SELECT n||'' FROM t1 ORDER BY 1 DESC;
194a9e99aeeSdrh  }
195a9e99aeeSdrh} {9 8 7 6 5 4 3 2 12 11 10 1}
196a9e99aeeSdrhdo_test sort-4.5 {
197a9e99aeeSdrh  execsql {
198a9e99aeeSdrh    SELECT n+0 FROM t1 ORDER BY 1 DESC;
199a9e99aeeSdrh  }
200a9e99aeeSdrh} {12 11 10 9 8 7 6 5 4 3 2 1}
201a9e99aeeSdrhdo_test sort-4.6 {
202a9e99aeeSdrh  execsql {
203a9e99aeeSdrh    SELECT v FROM t1 ORDER BY 1;
204a9e99aeeSdrh  }
2058a51256cSdrh} {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}
206a9e99aeeSdrhdo_test sort-4.7 {
207a9e99aeeSdrh  execsql {
208a9e99aeeSdrh    SELECT v FROM t1 ORDER BY 1 DESC;
209a9e99aeeSdrh  }
2108a51256cSdrh} {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}
211a9e99aeeSdrhdo_test sort-4.8 {
212a9e99aeeSdrh  execsql {
213a9e99aeeSdrh    SELECT substr(v,2,99) FROM t1 ORDER BY 1;
214a9e99aeeSdrh  }
2158a51256cSdrh} {-123.0 -2.15 -2b -3.141592653 -4.0e9 -4221.0 0.0013442 01234567890123456789 1.6 11.0 2.7 5.0e10}
216d1efac52Sdrh#do_test sort-4.9 {
217d1efac52Sdrh#  execsql {
218d1efac52Sdrh#    SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
219d1efac52Sdrh#  }
220d1efac52Sdrh#} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
221a9e99aeeSdrh
222da30d369Sdrhdo_test sort-5.1 {
223da30d369Sdrh  execsql {
224da30d369Sdrh    create table t3(a,b);
225da30d369Sdrh    insert into t3 values(5,NULL);
226da30d369Sdrh    insert into t3 values(6,NULL);
227da30d369Sdrh    insert into t3 values(3,NULL);
228da30d369Sdrh    insert into t3 values(4,'cd');
229da30d369Sdrh    insert into t3 values(1,'ab');
230da30d369Sdrh    insert into t3 values(2,NULL);
231da30d369Sdrh    select a from t3 order by b, a;
232da30d369Sdrh  }
233da30d369Sdrh} {2 3 5 6 1 4}
234da30d369Sdrhdo_test sort-5.2 {
235da30d369Sdrh  execsql {
236da30d369Sdrh    select a from t3 order by b, a desc;
237da30d369Sdrh  }
238da30d369Sdrh} {6 5 3 2 1 4}
239da30d369Sdrhdo_test sort-5.3 {
240da30d369Sdrh  execsql {
241da30d369Sdrh    select a from t3 order by b desc, a;
242da30d369Sdrh  }
243da30d369Sdrh} {4 1 2 3 5 6}
244da30d369Sdrhdo_test sort-5.4 {
245da30d369Sdrh  execsql {
246da30d369Sdrh    select a from t3 order by b desc, a desc;
247da30d369Sdrh  }
248da30d369Sdrh} {4 1 6 5 3 2}
249a9e99aeeSdrh
250da30d369Sdrhdo_test sort-6.1 {
251da30d369Sdrh  execsql {
252da30d369Sdrh    create index i3 on t3(b,a);
253da30d369Sdrh    select a from t3 order by b, a;
254da30d369Sdrh  }
255da30d369Sdrh} {2 3 5 6 1 4}
256da30d369Sdrhdo_test sort-6.2 {
257da30d369Sdrh  execsql {
258da30d369Sdrh    select a from t3 order by b, a desc;
259da30d369Sdrh  }
260da30d369Sdrh} {6 5 3 2 1 4}
261da30d369Sdrhdo_test sort-6.3 {
262da30d369Sdrh  execsql {
263da30d369Sdrh    select a from t3 order by b desc, a;
264da30d369Sdrh  }
265da30d369Sdrh} {4 1 2 3 5 6}
266da30d369Sdrhdo_test sort-6.4 {
267da30d369Sdrh  execsql {
268da30d369Sdrh    select a from t3 order by b desc, a desc;
269da30d369Sdrh  }
270da30d369Sdrh} {4 1 6 5 3 2}
2719208643dSdrh
272fcb78a49Sdrhdo_test sort-7.1 {
273fcb78a49Sdrh  execsql {
274fcb78a49Sdrh    CREATE TABLE t4(
275fcb78a49Sdrh      a INTEGER,
276fcb78a49Sdrh      b VARCHAR(30)
277fcb78a49Sdrh    );
278fcb78a49Sdrh    INSERT INTO t4 VALUES(1,1);
279fcb78a49Sdrh    INSERT INTO t4 VALUES(2,2);
280fcb78a49Sdrh    INSERT INTO t4 VALUES(11,11);
281fcb78a49Sdrh    INSERT INTO t4 VALUES(12,12);
282fcb78a49Sdrh    SELECT a FROM t4 ORDER BY 1;
283fcb78a49Sdrh  }
284fcb78a49Sdrh} {1 2 11 12}
285fcb78a49Sdrhdo_test sort-7.2 {
286fcb78a49Sdrh  execsql {
287fcb78a49Sdrh    SELECT b FROM t4 ORDER BY 1
288fcb78a49Sdrh  }
289fcb78a49Sdrh} {1 11 12 2}
2900fa8ddbdSdanielk1977
2910fa8ddbdSdanielk1977# Omit tests sort-7.3 to sort-7.8 if view support was disabled at
2920fa8ddbdSdanielk1977# compilatation time.
2930fa8ddbdSdanielk1977ifcapable view {
294fcb78a49Sdrhdo_test sort-7.3 {
295fcb78a49Sdrh  execsql {
296fcb78a49Sdrh    CREATE VIEW v4 AS SELECT * FROM t4;
297fcb78a49Sdrh    SELECT a FROM v4 ORDER BY 1;
298fcb78a49Sdrh  }
299fcb78a49Sdrh} {1 2 11 12}
300fcb78a49Sdrhdo_test sort-7.4 {
301fcb78a49Sdrh  execsql {
302fcb78a49Sdrh    SELECT b FROM v4 ORDER BY 1;
303fcb78a49Sdrh  }
304fcb78a49Sdrh} {1 11 12 2}
30527c77438Sdanielk1977
30627c77438Sdanielk1977ifcapable compound {
307fcb78a49Sdrhdo_test sort-7.5 {
308fcb78a49Sdrh  execsql {
309fcb78a49Sdrh    SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
310fcb78a49Sdrh  }
311fcb78a49Sdrh} {1 2 11 12}
312fcb78a49Sdrhdo_test sort-7.6 {
313fcb78a49Sdrh  execsql {
314fcb78a49Sdrh    SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
315fcb78a49Sdrh  }
316736c22b8Sdrh} {1 2 11 12 1 11 12 2}  ;# text from t4.b and numeric from v4.a
317fcb78a49Sdrhdo_test sort-7.7 {
318fcb78a49Sdrh  execsql {
319fcb78a49Sdrh    SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
320fcb78a49Sdrh  }
321736c22b8Sdrh} {1 2 11 12 1 11 12 2} ;# numeric from t4.a and text from v4.b
322fcb78a49Sdrhdo_test sort-7.8 {
323fcb78a49Sdrh  execsql {
324fcb78a49Sdrh    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
325fcb78a49Sdrh  }
326fcb78a49Sdrh} {1 11 12 2}
32727c77438Sdanielk1977} ;# ifcapable compound
3280fa8ddbdSdanielk1977} ;# ifcapable view
3290fa8ddbdSdanielk1977
330736c22b8Sdrh#### Version 3 works differently here:
331736c22b8Sdrh#do_test sort-7.9 {
332736c22b8Sdrh#  execsql {
333736c22b8Sdrh#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
334736c22b8Sdrh#  }
335736c22b8Sdrh#} {1 2 11 12}
336736c22b8Sdrh#do_test sort-7.10 {
337736c22b8Sdrh#  execsql {
338736c22b8Sdrh#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
339736c22b8Sdrh#  }
340736c22b8Sdrh#} {1 2 11 12}
3410202b29eSdanielk1977#do_test sort-7.11 {
3420202b29eSdanielk1977#  execsql {
3430202b29eSdanielk1977#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
3440202b29eSdanielk1977#  }
3450202b29eSdanielk1977#} {1 11 12 2}
3460202b29eSdanielk1977#do_test sort-7.12 {
3470202b29eSdanielk1977#  execsql {
3480202b29eSdanielk1977#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
3490202b29eSdanielk1977#  }
3500202b29eSdanielk1977#} {1 11 12 2}
3510202b29eSdanielk1977#do_test sort-7.13 {
3520202b29eSdanielk1977#  execsql {
3530202b29eSdanielk1977#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
3540202b29eSdanielk1977#  }
3550202b29eSdanielk1977#} {1 11 12 2}
3560202b29eSdanielk1977#do_test sort-7.14 {
3570202b29eSdanielk1977#  execsql {
3580202b29eSdanielk1977#    SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
3590202b29eSdanielk1977#  }
3600202b29eSdanielk1977#} {1 11 12 2}
361fcb78a49Sdrh
362cab20050Sdrh# Ticket #297
363cab20050Sdrh#
364cab20050Sdrhdo_test sort-8.1 {
365cab20050Sdrh  execsql {
366cab20050Sdrh    CREATE TABLE t5(a real, b text);
367cab20050Sdrh    INSERT INTO t5 VALUES(100,'A1');
368cab20050Sdrh    INSERT INTO t5 VALUES(100.0,'A2');
369cab20050Sdrh    SELECT * FROM t5 ORDER BY a, b;
370cab20050Sdrh  }
3718a51256cSdrh} {100.0 A1 100.0 A2}
372cab20050Sdrh
373a71aa001Sdrh
374a71aa001Sdrhifcapable {bloblit} {
3756a179ea7Sdrh# BLOBs should sort after TEXT
3766a179ea7Sdrh#
3776a179ea7Sdrhdo_test sort-9.1 {
3786a179ea7Sdrh  execsql {
3796a179ea7Sdrh    CREATE TABLE t6(x, y);
3806a179ea7Sdrh    INSERT INTO t6 VALUES(1,1);
3816a179ea7Sdrh    INSERT INTO t6 VALUES(2,'1');
3826a179ea7Sdrh    INSERT INTO t6 VALUES(3,x'31');
3836a179ea7Sdrh    INSERT INTO t6 VALUES(4,NULL);
3846a179ea7Sdrh    SELECT x FROM t6 ORDER BY y;
3856a179ea7Sdrh  }
3866a179ea7Sdrh} {4 1 2 3}
3876a179ea7Sdrhdo_test sort-9.2 {
3886a179ea7Sdrh  execsql {
3896a179ea7Sdrh    SELECT x FROM t6 ORDER BY y DESC;
3906a179ea7Sdrh  }
3916a179ea7Sdrh} {3 2 1 4}
3926a179ea7Sdrhdo_test sort-9.3 {
3936a179ea7Sdrh  execsql {
3946a179ea7Sdrh    SELECT x FROM t6 WHERE y<1
3956a179ea7Sdrh  }
3966a179ea7Sdrh} {}
3976a179ea7Sdrhdo_test sort-9.4 {
3986a179ea7Sdrh  execsql {
3996a179ea7Sdrh    SELECT x FROM t6 WHERE y<'1'
4006a179ea7Sdrh  }
4016a179ea7Sdrh} {1}
4026a179ea7Sdrhdo_test sort-9.5 {
4036a179ea7Sdrh  execsql {
4046a179ea7Sdrh    SELECT x FROM t6 WHERE y<x'31'
4056a179ea7Sdrh  }
4066a179ea7Sdrh} {1 2}
4076a179ea7Sdrhdo_test sort-9.6 {
4086a179ea7Sdrh  execsql {
4096a179ea7Sdrh    SELECT x FROM t6 WHERE y>1
4106a179ea7Sdrh  }
4116a179ea7Sdrh} {2 3}
4126a179ea7Sdrhdo_test sort-9.7 {
4136a179ea7Sdrh  execsql {
4146a179ea7Sdrh    SELECT x FROM t6 WHERE y>'1'
4156a179ea7Sdrh  }
4166a179ea7Sdrh} {3}
417a71aa001Sdrh} ;# endif bloblit
4186a179ea7Sdrh
419d0a6932fSdanielk1977# Ticket #1092 - ORDER BY on rowid fields.
420d0a6932fSdanielk1977do_test sort-10.1 {
421d0a6932fSdanielk1977  execsql {
422d0a6932fSdanielk1977    CREATE TABLE t7(c INTEGER PRIMARY KEY);
423d0a6932fSdanielk1977    INSERT INTO t7 VALUES(1);
424d0a6932fSdanielk1977    INSERT INTO t7 VALUES(2);
425d0a6932fSdanielk1977    INSERT INTO t7 VALUES(3);
426d0a6932fSdanielk1977    INSERT INTO t7 VALUES(4);
427d0a6932fSdanielk1977  }
428d0a6932fSdanielk1977} {}
429d0a6932fSdanielk1977do_test sort-10.2 {
430d0a6932fSdanielk1977  execsql {
431d0a6932fSdanielk1977    SELECT c FROM t7 WHERE c<=3 ORDER BY c DESC;
432d0a6932fSdanielk1977  }
433d0a6932fSdanielk1977} {3 2 1}
434d0a6932fSdanielk1977do_test sort-10.3 {
435d0a6932fSdanielk1977  execsql {
436d0a6932fSdanielk1977    SELECT c FROM t7 WHERE c<3 ORDER BY c DESC;
437d0a6932fSdanielk1977  }
438d0a6932fSdanielk1977} {2 1}
439d0a6932fSdanielk1977
4408718f526Sdrh# ticket #1358.  Just because one table in a join gives a unique
4418718f526Sdrh# result does not mean they all do.  We cannot disable sorting unless
4428718f526Sdrh# all tables in the join give unique results.
4438718f526Sdrh#
4448718f526Sdrhdo_test sort-11.1 {
4458718f526Sdrh  execsql {
4468718f526Sdrh    create table t8(a unique, b, c);
4478718f526Sdrh    insert into t8 values(1,2,3);
4488718f526Sdrh    insert into t8 values(2,3,4);
4498718f526Sdrh    create table t9(x,y);
4508718f526Sdrh    insert into t9 values(2,4);
4518718f526Sdrh    insert into t9 values(2,3);
4528718f526Sdrh    select y from t8, t9 where a=1 order by a, y;
4538718f526Sdrh  }
4548718f526Sdrh} {3 4}
4558718f526Sdrh
456a21c8495Sdrh# Trouble reported on the mailing list.  Check for overly aggressive
457a21c8495Sdrh# (which is to say, incorrect) optimization of order-by with a rowid
458a21c8495Sdrh# in a join.
459a21c8495Sdrh#
460a21c8495Sdrhdo_test sort-12.1 {
461a21c8495Sdrh  execsql {
462a21c8495Sdrh    create table a (id integer primary key);
463a21c8495Sdrh    create table b (id integer primary key, aId integer, text);
464a21c8495Sdrh    insert into a values (1);
465a21c8495Sdrh    insert into b values (2, 1, 'xxx');
466a21c8495Sdrh    insert into b values (1, 1, 'zzz');
467a21c8495Sdrh    insert into b values (3, 1, 'yyy');
468a21c8495Sdrh    select a.id, b.id, b.text from a join b on (a.id = b.aId)
469a21c8495Sdrh      order by a.id, b.text;
470a21c8495Sdrh  }
471a21c8495Sdrh} {1 2 xxx 1 3 yyy 1 1 zzz}
472a21c8495Sdrh
473ab1dcc1aSdan#-------------------------------------------------------------------------
474ab1dcc1aSdan# Check that the sorter in vdbesort.c sorts in a stable fashion.
475ab1dcc1aSdan#
476ab1dcc1aSdando_execsql_test sort-13.0 {
477ab1dcc1aSdan  CREATE TABLE t10(a, b);
478ab1dcc1aSdan}
479ab1dcc1aSdando_test sort-13.1 {
480ab1dcc1aSdan  db transaction {
481ab1dcc1aSdan    for {set i 0} {$i < 100000} {incr i} {
482ab1dcc1aSdan      execsql { INSERT INTO t10 VALUES( $i/10, $i%10 ) }
483ab1dcc1aSdan    }
484ab1dcc1aSdan  }
485ab1dcc1aSdan} {}
486ab1dcc1aSdando_execsql_test sort-13.2 {
487ab1dcc1aSdan  SELECT a, b FROM t10 ORDER BY a;
488ab1dcc1aSdan} [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
489ab1dcc1aSdando_execsql_test sort-13.3 {
490ab1dcc1aSdan  PRAGMA cache_size = 5;
491ab1dcc1aSdan  SELECT a, b FROM t10 ORDER BY a;
492ab1dcc1aSdan} [db eval {SELECT a, b FROM t10 ORDER BY a, b}]
493ab1dcc1aSdan
4940d51def2Sdan#-------------------------------------------------------------------------
4950d51def2Sdan#
496d94d4ee7Sdanforeach {tn mmap_limit nWorker tmpstore coremutex fakeheap softheaplimit} {
497d94d4ee7Sdan          1          0       3     file      true    false             0
498d94d4ee7Sdan          2          0       3     file      true     true             0
499d94d4ee7Sdan          3          0       0     file      true    false             0
500d94d4ee7Sdan          4    1000000       3     file      true    false             0
501d94d4ee7Sdan          5          0       0   memory     false     true             0
502d94d4ee7Sdan          6          0       0     file     false     true       1000000
503d94d4ee7Sdan          7          0       0     file     false     true         10000
5040d51def2Sdan} {
5050d51def2Sdan  db close
5060d51def2Sdan  sqlite3_shutdown
5070d51def2Sdan  if {$coremutex} {
5080d51def2Sdan    sqlite3_config multithread
5090d51def2Sdan  } else {
5100d51def2Sdan    sqlite3_config singlethread
5110d51def2Sdan  }
5120d51def2Sdan  sqlite3_initialize
5130d51def2Sdan  sorter_test_fakeheap $fakeheap
514d94d4ee7Sdan  sqlite3_soft_heap_limit $softheaplimit
5150d51def2Sdan
5160d51def2Sdan  reset_db
5170d51def2Sdan  sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $mmap_limit
518028696c4Sdrh  execsql "PRAGMA temp_store = $tmpstore; PRAGMA threads = $nWorker"
519028696c4Sdrh
5200d51def2Sdan
5210d51def2Sdan  set ten [string repeat X 10300]
5220d51def2Sdan  set one [string repeat y   200]
5230d51def2Sdan
524d94d4ee7Sdan  if {$softheaplimit} {
525d94d4ee7Sdan    execsql { PRAGMA cache_size = 20 };
526d94d4ee7Sdan  } else {
527d94d4ee7Sdan    execsql { PRAGMA cache_size = 5 };
528d94d4ee7Sdan  }
529d94d4ee7Sdan
5300d51def2Sdan  do_execsql_test 15.$tn.1 {
5310d51def2Sdan    WITH rr AS (
5320d51def2Sdan      SELECT 4, $ten UNION ALL
5330d51def2Sdan      SELECT 2, $one UNION ALL
5340d51def2Sdan      SELECT 1, $ten UNION ALL
5350d51def2Sdan      SELECT 3, $one
5360d51def2Sdan    )
5370d51def2Sdan    SELECT * FROM rr ORDER BY 1;
5380d51def2Sdan  } [list 1 $ten 2 $one 3 $one 4 $ten]
5390d51def2Sdan
5400d51def2Sdan  do_execsql_test 15.$tn.2 {
5410d51def2Sdan    CREATE TABLE t1(a);
5420d51def2Sdan    INSERT INTO t1 VALUES(4);
5430d51def2Sdan    INSERT INTO t1 VALUES(5);
5440d51def2Sdan    INSERT INTO t1 VALUES(3);
5450d51def2Sdan    INSERT INTO t1 VALUES(2);
5460d51def2Sdan    INSERT INTO t1 VALUES(6);
5470d51def2Sdan    INSERT INTO t1 VALUES(1);
5480d51def2Sdan    CREATE INDEX i1 ON t1(a);
5490d51def2Sdan    SELECT * FROM t1 ORDER BY a;
5500d51def2Sdan  } {1 2 3 4 5 6}
5510d51def2Sdan
5520d51def2Sdan  do_execsql_test 15.$tn.3 {
5530d51def2Sdan    WITH rr AS (
5540d51def2Sdan      SELECT 4, $ten UNION ALL
5550d51def2Sdan      SELECT 2, $one
5560d51def2Sdan    )
5570d51def2Sdan    SELECT * FROM rr ORDER BY 1;
5580d51def2Sdan  } [list 2 $one 4 $ten]
5590d51def2Sdan
5600d51def2Sdan  sorter_test_fakeheap 0
5610d51def2Sdan}
5620d51def2Sdan
5630d51def2Sdandb close
5640d51def2Sdansqlite3_shutdown
5650d51def2Sdanset t(0) singlethread
5660d51def2Sdanset t(1) multithread
5670d51def2Sdanset t(2) serialized
5680d51def2Sdansqlite3_config $t($sqlite_options(threadsafe))
5690d51def2Sdansqlite3_initialize
570d94d4ee7Sdansqlite3_soft_heap_limit 0
571d94d4ee7Sdan
572d94d4ee7Sdanreset_db
573d94d4ee7Sdando_catchsql_test 16.1 {
574d94d4ee7Sdan  CREATE TABLE t1(a, b, c);
575d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, 3);
576d94d4ee7Sdan  INSERT INTO t1 VALUES(1, NULL, 3);
577d94d4ee7Sdan  INSERT INTO t1 VALUES(NULL, 2, 3);
578d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, NULL);
579d94d4ee7Sdan  INSERT INTO t1 VALUES(4, 5, 6);
580d94d4ee7Sdan  CREATE UNIQUE INDEX i1 ON t1(b, a, c);
581d94d4ee7Sdan} {0 {}}
582d94d4ee7Sdanreset_db
583d94d4ee7Sdando_catchsql_test 16.2 {
584d94d4ee7Sdan  CREATE TABLE t1(a, b, c);
585d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, 3);
586d94d4ee7Sdan  INSERT INTO t1 VALUES(1, NULL, 3);
587d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, 3);
588d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, NULL);
589d94d4ee7Sdan  INSERT INTO t1 VALUES(4, 5, 6);
590d94d4ee7Sdan  CREATE UNIQUE INDEX i1 ON t1(b, a, c);
591d94d4ee7Sdan} {1 {UNIQUE constraint failed: t1.b, t1.a, t1.c}}
592d94d4ee7Sdan
593d94d4ee7Sdanreset_db
594d94d4ee7Sdando_execsql_test 17.1 {
595d94d4ee7Sdan  SELECT * FROM sqlite_master ORDER BY sql;
596d94d4ee7Sdan} {}
597ab1dcc1aSdan
59859692544Sdrhfinish_test
599