PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/test/sort.test

https://bitbucket.org/aware/sqlite
Unknown | 467 lines | 448 code | 19 blank | 0 comment | 0 complexity | b8bcce48b269f19c27f38b56c039a83a MD5 | raw file
  1. # 2001 September 15.
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the CREATE TABLE statement.
  13. #
  14. # $Id:$
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create a bunch of data to sort against
  18. #
  19. do_test sort-1.0 {
  20. execsql {
  21. CREATE TABLE t1(
  22. n int,
  23. v varchar(10),
  24. log int,
  25. roman varchar(10),
  26. flt real
  27. );
  28. INSERT INTO t1 VALUES(1,'one',0,'I',3.141592653);
  29. INSERT INTO t1 VALUES(2,'two',1,'II',2.15);
  30. INSERT INTO t1 VALUES(3,'three',1,'III',4221.0);
  31. INSERT INTO t1 VALUES(4,'four',2,'IV',-0.0013442);
  32. INSERT INTO t1 VALUES(5,'five',2,'V',-11);
  33. INSERT INTO t1 VALUES(6,'six',2,'VI',0.123);
  34. INSERT INTO t1 VALUES(7,'seven',2,'VII',123.0);
  35. INSERT INTO t1 VALUES(8,'eight',3,'VIII',-1.6);
  36. }
  37. execsql {SELECT count(*) FROM t1}
  38. } {8}
  39. do_test sort-1.1 {
  40. execsql {SELECT n FROM t1 ORDER BY n}
  41. } {1 2 3 4 5 6 7 8}
  42. do_test sort-1.1.1 {
  43. execsql {SELECT n FROM t1 ORDER BY n ASC}
  44. } {1 2 3 4 5 6 7 8}
  45. do_test sort-1.1.1 {
  46. execsql {SELECT ALL n FROM t1 ORDER BY n ASC}
  47. } {1 2 3 4 5 6 7 8}
  48. do_test sort-1.2 {
  49. execsql {SELECT n FROM t1 ORDER BY n DESC}
  50. } {8 7 6 5 4 3 2 1}
  51. do_test sort-1.3a {
  52. execsql {SELECT v FROM t1 ORDER BY v}
  53. } {eight five four one seven six three two}
  54. do_test sort-1.3b {
  55. execsql {SELECT n FROM t1 ORDER BY v}
  56. } {8 5 4 1 7 6 3 2}
  57. do_test sort-1.4 {
  58. execsql {SELECT n FROM t1 ORDER BY v DESC}
  59. } {2 3 6 7 1 4 5 8}
  60. do_test sort-1.5 {
  61. execsql {SELECT flt FROM t1 ORDER BY flt}
  62. } {-11.0 -1.6 -0.0013442 0.123 2.15 3.141592653 123.0 4221.0}
  63. do_test sort-1.6 {
  64. execsql {SELECT flt FROM t1 ORDER BY flt DESC}
  65. } {4221.0 123.0 3.141592653 2.15 0.123 -0.0013442 -1.6 -11.0}
  66. do_test sort-1.7 {
  67. execsql {SELECT roman FROM t1 ORDER BY roman}
  68. } {I II III IV V VI VII VIII}
  69. do_test sort-1.8 {
  70. execsql {SELECT n FROM t1 ORDER BY log, flt}
  71. } {1 2 3 5 4 6 7 8}
  72. do_test sort-1.8.1 {
  73. execsql {SELECT n FROM t1 ORDER BY log asc, flt}
  74. } {1 2 3 5 4 6 7 8}
  75. do_test sort-1.8.2 {
  76. execsql {SELECT n FROM t1 ORDER BY log, flt ASC}
  77. } {1 2 3 5 4 6 7 8}
  78. do_test sort-1.8.3 {
  79. execsql {SELECT n FROM t1 ORDER BY log ASC, flt asc}
  80. } {1 2 3 5 4 6 7 8}
  81. do_test sort-1.9 {
  82. execsql {SELECT n FROM t1 ORDER BY log, flt DESC}
  83. } {1 3 2 7 6 4 5 8}
  84. do_test sort-1.9.1 {
  85. execsql {SELECT n FROM t1 ORDER BY log ASC, flt DESC}
  86. } {1 3 2 7 6 4 5 8}
  87. do_test sort-1.10 {
  88. execsql {SELECT n FROM t1 ORDER BY log DESC, flt}
  89. } {8 5 4 6 7 2 3 1}
  90. do_test sort-1.11 {
  91. execsql {SELECT n FROM t1 ORDER BY log DESC, flt DESC}
  92. } {8 7 6 4 5 3 2 1}
  93. # These tests are designed to reach some hard-to-reach places
  94. # inside the string comparison routines.
  95. #
  96. # (Later) The sorting behavior changed in 2.7.0. But we will
  97. # keep these tests. You can never have too many test cases!
  98. #
  99. do_test sort-2.1.1 {
  100. execsql {
  101. UPDATE t1 SET v='x' || -flt;
  102. UPDATE t1 SET v='x-2b' where v=='x-0.123';
  103. SELECT v FROM t1 ORDER BY v;
  104. }
  105. } {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
  106. do_test sort-2.1.2 {
  107. execsql {
  108. SELECT v FROM t1 ORDER BY substr(v,2,999);
  109. }
  110. } {x-123.0 x-2.15 x-2b x-3.141592653 x-4221.0 x0.0013442 x1.6 x11.0}
  111. do_test sort-2.1.3 {
  112. execsql {
  113. SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0;
  114. }
  115. } {x-4221.0 x-123.0 x-3.141592653 x-2.15 x-2b x0.0013442 x1.6 x11.0}
  116. do_test sort-2.1.4 {
  117. execsql {
  118. SELECT v FROM t1 ORDER BY substr(v,2,999) DESC;
  119. }
  120. } {x11.0 x1.6 x0.0013442 x-4221.0 x-3.141592653 x-2b x-2.15 x-123.0}
  121. do_test sort-2.1.5 {
  122. execsql {
  123. SELECT v FROM t1 ORDER BY substr(v,2,999)+0.0 DESC;
  124. }
  125. } {x11.0 x1.6 x0.0013442 x-2b x-2.15 x-3.141592653 x-123.0 x-4221.0}
  126. # This is a bug fix for 2.2.4.
  127. # Strings are normally mapped to upper-case for a caseless comparison.
  128. # But this can cause problems for characters in between 'Z' and 'a'.
  129. #
  130. do_test sort-3.1 {
  131. execsql {
  132. CREATE TABLE t2(a,b);
  133. INSERT INTO t2 VALUES('AGLIENTU',1);
  134. INSERT INTO t2 VALUES('AGLIE`',2);
  135. INSERT INTO t2 VALUES('AGNA',3);
  136. SELECT a, b FROM t2 ORDER BY a;
  137. }
  138. } {AGLIENTU 1 AGLIE` 2 AGNA 3}
  139. do_test sort-3.2 {
  140. execsql {
  141. SELECT a, b FROM t2 ORDER BY a DESC;
  142. }
  143. } {AGNA 3 AGLIE` 2 AGLIENTU 1}
  144. do_test sort-3.3 {
  145. execsql {
  146. DELETE FROM t2;
  147. INSERT INTO t2 VALUES('aglientu',1);
  148. INSERT INTO t2 VALUES('aglie`',2);
  149. INSERT INTO t2 VALUES('agna',3);
  150. SELECT a, b FROM t2 ORDER BY a;
  151. }
  152. } {aglie` 2 aglientu 1 agna 3}
  153. do_test sort-3.4 {
  154. execsql {
  155. SELECT a, b FROM t2 ORDER BY a DESC;
  156. }
  157. } {agna 3 aglientu 1 aglie` 2}
  158. # Version 2.7.0 testing.
  159. #
  160. do_test sort-4.1 {
  161. execsql {
  162. INSERT INTO t1 VALUES(9,'x2.7',3,'IX',4.0e5);
  163. INSERT INTO t1 VALUES(10,'x5.0e10',3,'X',-4.0e5);
  164. INSERT INTO t1 VALUES(11,'x-4.0e9',3,'XI',4.1e4);
  165. INSERT INTO t1 VALUES(12,'x01234567890123456789',3,'XII',-4.2e3);
  166. SELECT n FROM t1 ORDER BY n;
  167. }
  168. } {1 2 3 4 5 6 7 8 9 10 11 12}
  169. do_test sort-4.2 {
  170. execsql {
  171. SELECT n||'' FROM t1 ORDER BY 1;
  172. }
  173. } {1 10 11 12 2 3 4 5 6 7 8 9}
  174. do_test sort-4.3 {
  175. execsql {
  176. SELECT n+0 FROM t1 ORDER BY 1;
  177. }
  178. } {1 2 3 4 5 6 7 8 9 10 11 12}
  179. do_test sort-4.4 {
  180. execsql {
  181. SELECT n||'' FROM t1 ORDER BY 1 DESC;
  182. }
  183. } {9 8 7 6 5 4 3 2 12 11 10 1}
  184. do_test sort-4.5 {
  185. execsql {
  186. SELECT n+0 FROM t1 ORDER BY 1 DESC;
  187. }
  188. } {12 11 10 9 8 7 6 5 4 3 2 1}
  189. do_test sort-4.6 {
  190. execsql {
  191. SELECT v FROM t1 ORDER BY 1;
  192. }
  193. } {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}
  194. do_test sort-4.7 {
  195. execsql {
  196. SELECT v FROM t1 ORDER BY 1 DESC;
  197. }
  198. } {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}
  199. do_test sort-4.8 {
  200. execsql {
  201. SELECT substr(v,2,99) FROM t1 ORDER BY 1;
  202. }
  203. } {-123.0 -2.15 -2b -3.141592653 -4.0e9 -4221.0 0.0013442 01234567890123456789 1.6 11.0 2.7 5.0e10}
  204. #do_test sort-4.9 {
  205. # execsql {
  206. # SELECT substr(v,2,99)+0.0 FROM t1 ORDER BY 1;
  207. # }
  208. #} {-4000000000 -4221 -123 -3.141592653 -2.15 -2 0.0013442 1.6 2.7 11 50000000000 1.23456789012346e+18}
  209. do_test sort-5.1 {
  210. execsql {
  211. create table t3(a,b);
  212. insert into t3 values(5,NULL);
  213. insert into t3 values(6,NULL);
  214. insert into t3 values(3,NULL);
  215. insert into t3 values(4,'cd');
  216. insert into t3 values(1,'ab');
  217. insert into t3 values(2,NULL);
  218. select a from t3 order by b, a;
  219. }
  220. } {2 3 5 6 1 4}
  221. do_test sort-5.2 {
  222. execsql {
  223. select a from t3 order by b, a desc;
  224. }
  225. } {6 5 3 2 1 4}
  226. do_test sort-5.3 {
  227. execsql {
  228. select a from t3 order by b desc, a;
  229. }
  230. } {4 1 2 3 5 6}
  231. do_test sort-5.4 {
  232. execsql {
  233. select a from t3 order by b desc, a desc;
  234. }
  235. } {4 1 6 5 3 2}
  236. do_test sort-6.1 {
  237. execsql {
  238. create index i3 on t3(b,a);
  239. select a from t3 order by b, a;
  240. }
  241. } {2 3 5 6 1 4}
  242. do_test sort-6.2 {
  243. execsql {
  244. select a from t3 order by b, a desc;
  245. }
  246. } {6 5 3 2 1 4}
  247. do_test sort-6.3 {
  248. execsql {
  249. select a from t3 order by b desc, a;
  250. }
  251. } {4 1 2 3 5 6}
  252. do_test sort-6.4 {
  253. execsql {
  254. select a from t3 order by b desc, a desc;
  255. }
  256. } {4 1 6 5 3 2}
  257. do_test sort-7.1 {
  258. execsql {
  259. CREATE TABLE t4(
  260. a INTEGER,
  261. b VARCHAR(30)
  262. );
  263. INSERT INTO t4 VALUES(1,1);
  264. INSERT INTO t4 VALUES(2,2);
  265. INSERT INTO t4 VALUES(11,11);
  266. INSERT INTO t4 VALUES(12,12);
  267. SELECT a FROM t4 ORDER BY 1;
  268. }
  269. } {1 2 11 12}
  270. do_test sort-7.2 {
  271. execsql {
  272. SELECT b FROM t4 ORDER BY 1
  273. }
  274. } {1 11 12 2}
  275. # Omit tests sort-7.3 to sort-7.8 if view support was disabled at
  276. # compilatation time.
  277. ifcapable view {
  278. do_test sort-7.3 {
  279. execsql {
  280. CREATE VIEW v4 AS SELECT * FROM t4;
  281. SELECT a FROM v4 ORDER BY 1;
  282. }
  283. } {1 2 11 12}
  284. do_test sort-7.4 {
  285. execsql {
  286. SELECT b FROM v4 ORDER BY 1;
  287. }
  288. } {1 11 12 2}
  289. ifcapable compound {
  290. do_test sort-7.5 {
  291. execsql {
  292. SELECT a FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
  293. }
  294. } {1 2 11 12}
  295. do_test sort-7.6 {
  296. execsql {
  297. SELECT b FROM t4 UNION SELECT a FROM v4 ORDER BY 1;
  298. }
  299. } {1 2 11 12 1 11 12 2} ;# text from t4.b and numeric from v4.a
  300. do_test sort-7.7 {
  301. execsql {
  302. SELECT a FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
  303. }
  304. } {1 2 11 12 1 11 12 2} ;# numeric from t4.a and text from v4.b
  305. do_test sort-7.8 {
  306. execsql {
  307. SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1;
  308. }
  309. } {1 11 12 2}
  310. } ;# ifcapable compound
  311. } ;# ifcapable view
  312. #### Version 3 works differently here:
  313. #do_test sort-7.9 {
  314. # execsql {
  315. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE numeric;
  316. # }
  317. #} {1 2 11 12}
  318. #do_test sort-7.10 {
  319. # execsql {
  320. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE integer;
  321. # }
  322. #} {1 2 11 12}
  323. #do_test sort-7.11 {
  324. # execsql {
  325. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE text;
  326. # }
  327. #} {1 11 12 2}
  328. #do_test sort-7.12 {
  329. # execsql {
  330. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE blob;
  331. # }
  332. #} {1 11 12 2}
  333. #do_test sort-7.13 {
  334. # execsql {
  335. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE clob;
  336. # }
  337. #} {1 11 12 2}
  338. #do_test sort-7.14 {
  339. # execsql {
  340. # SELECT b FROM t4 UNION SELECT b FROM v4 ORDER BY 1 COLLATE varchar;
  341. # }
  342. #} {1 11 12 2}
  343. # Ticket #297
  344. #
  345. do_test sort-8.1 {
  346. execsql {
  347. CREATE TABLE t5(a real, b text);
  348. INSERT INTO t5 VALUES(100,'A1');
  349. INSERT INTO t5 VALUES(100.0,'A2');
  350. SELECT * FROM t5 ORDER BY a, b;
  351. }
  352. } {100.0 A1 100.0 A2}
  353. ifcapable {bloblit} {
  354. # BLOBs should sort after TEXT
  355. #
  356. do_test sort-9.1 {
  357. execsql {
  358. CREATE TABLE t6(x, y);
  359. INSERT INTO t6 VALUES(1,1);
  360. INSERT INTO t6 VALUES(2,'1');
  361. INSERT INTO t6 VALUES(3,x'31');
  362. INSERT INTO t6 VALUES(4,NULL);
  363. SELECT x FROM t6 ORDER BY y;
  364. }
  365. } {4 1 2 3}
  366. do_test sort-9.2 {
  367. execsql {
  368. SELECT x FROM t6 ORDER BY y DESC;
  369. }
  370. } {3 2 1 4}
  371. do_test sort-9.3 {
  372. execsql {
  373. SELECT x FROM t6 WHERE y<1
  374. }
  375. } {}
  376. do_test sort-9.4 {
  377. execsql {
  378. SELECT x FROM t6 WHERE y<'1'
  379. }
  380. } {1}
  381. do_test sort-9.5 {
  382. execsql {
  383. SELECT x FROM t6 WHERE y<x'31'
  384. }
  385. } {1 2}
  386. do_test sort-9.6 {
  387. execsql {
  388. SELECT x FROM t6 WHERE y>1
  389. }
  390. } {2 3}
  391. do_test sort-9.7 {
  392. execsql {
  393. SELECT x FROM t6 WHERE y>'1'
  394. }
  395. } {3}
  396. } ;# endif bloblit
  397. # Ticket #1092 - ORDER BY on rowid fields.
  398. do_test sort-10.1 {
  399. execsql {
  400. CREATE TABLE t7(c INTEGER PRIMARY KEY);
  401. INSERT INTO t7 VALUES(1);
  402. INSERT INTO t7 VALUES(2);
  403. INSERT INTO t7 VALUES(3);
  404. INSERT INTO t7 VALUES(4);
  405. }
  406. } {}
  407. do_test sort-10.2 {
  408. execsql {
  409. SELECT c FROM t7 WHERE c<=3 ORDER BY c DESC;
  410. }
  411. } {3 2 1}
  412. do_test sort-10.3 {
  413. execsql {
  414. SELECT c FROM t7 WHERE c<3 ORDER BY c DESC;
  415. }
  416. } {2 1}
  417. # ticket #1358. Just because one table in a join gives a unique
  418. # result does not mean they all do. We cannot disable sorting unless
  419. # all tables in the join give unique results.
  420. #
  421. do_test sort-11.1 {
  422. execsql {
  423. create table t8(a unique, b, c);
  424. insert into t8 values(1,2,3);
  425. insert into t8 values(2,3,4);
  426. create table t9(x,y);
  427. insert into t9 values(2,4);
  428. insert into t9 values(2,3);
  429. select y from t8, t9 where a=1 order by a, y;
  430. }
  431. } {3 4}
  432. # Trouble reported on the mailing list. Check for overly aggressive
  433. # (which is to say, incorrect) optimization of order-by with a rowid
  434. # in a join.
  435. #
  436. do_test sort-12.1 {
  437. execsql {
  438. create table a (id integer primary key);
  439. create table b (id integer primary key, aId integer, text);
  440. insert into a values (1);
  441. insert into b values (2, 1, 'xxx');
  442. insert into b values (1, 1, 'zzz');
  443. insert into b values (3, 1, 'yyy');
  444. select a.id, b.id, b.text from a join b on (a.id = b.aId)
  445. order by a.id, b.text;
  446. }
  447. } {1 2 xxx 1 3 yyy 1 1 zzz}
  448. finish_test