collate1.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #
  2. # 2001 September 15
  3. #
  4. # The author disclaims copyright to this source code. In place of
  5. # a legal notice, here is a blessing:
  6. #
  7. # May you do good and not evil.
  8. # May you find forgiveness for yourself and forgive others.
  9. # May you share freely, never taking more than you give.
  10. #
  11. #***********************************************************************
  12. # This file implements regression tests for SQLite library. The
  13. # focus of this script is page cache subsystem.
  14. #
  15. # $Id: collate1.test,v 1.5 2007/02/01 23:02:46 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. #
  19. # Tests are roughly organised as follows:
  20. #
  21. # collate1-1.* - Single-field ORDER BY with an explicit COLLATE clause.
  22. # collate1-2.* - Multi-field ORDER BY with an explicit COLLATE clause.
  23. # collate1-3.* - ORDER BY using a default collation type. Also that an
  24. # explict collate type overrides a default collate type.
  25. # collate1-4.* - ORDER BY using a data type.
  26. #
  27. #
  28. # Collation type 'HEX'. If an argument can be interpreted as a hexadecimal
  29. # number, then it is converted to one before the comparison is performed.
  30. # Numbers are less than other strings. If neither argument is a number,
  31. # [string compare] is used.
  32. #
  33. db collate HEX hex_collate
  34. proc hex_collate {lhs rhs} {
  35. set lhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $lhs]
  36. set rhs_ishex [regexp {^(0x|)[1234567890abcdefABCDEF]+$} $rhs]
  37. if {$lhs_ishex && $rhs_ishex} {
  38. set lhsx [scan $lhs %x]
  39. set rhsx [scan $rhs %x]
  40. if {$lhs < $rhs} {return -1}
  41. if {$lhs == $rhs} {return 0}
  42. if {$lhs > $rhs} {return 1}
  43. }
  44. if {$lhs_ishex} {
  45. return -1;
  46. }
  47. if {$rhs_ishex} {
  48. return 1;
  49. }
  50. return [string compare $lhs $rhs]
  51. }
  52. db function hex {format 0x%X}
  53. # Mimic the SQLite 2 collation type NUMERIC.
  54. db collate numeric numeric_collate
  55. proc numeric_collate {lhs rhs} {
  56. if {$lhs == $rhs} {return 0}
  57. return [expr ($lhs>$rhs)?1:-1]
  58. }
  59. do_test collate1-1.0 {
  60. execsql {
  61. CREATE TABLE collate1t1(c1, c2);
  62. INSERT INTO collate1t1 VALUES(45, hex(45));
  63. INSERT INTO collate1t1 VALUES(NULL, NULL);
  64. INSERT INTO collate1t1 VALUES(281, hex(281));
  65. }
  66. } {}
  67. do_test collate1-1.1 {
  68. execsql {
  69. SELECT c2 FROM collate1t1 ORDER BY 1;
  70. }
  71. } {{} 0x119 0x2D}
  72. do_test collate1-1.2 {
  73. breakpoint
  74. execsql {
  75. SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex;
  76. }
  77. } {{} 0x2D 0x119}
  78. do_test collate1-1.3 {
  79. execsql {
  80. SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC;
  81. }
  82. } {0x119 0x2D {}}
  83. do_test collate1-1.4 {
  84. execsql {
  85. SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex ASC;
  86. }
  87. } {{} 0x2D 0x119}
  88. do_test collate1-1.5 {
  89. execsql {
  90. SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1
  91. }
  92. } {{} 0x2D 0x119}
  93. do_test collate1-1.6 {
  94. execsql {
  95. SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 ASC
  96. }
  97. } {{} 0x2D 0x119}
  98. do_test collate1-1.7 {
  99. execsql {
  100. SELECT c2 COLLATE hex FROM collate1t1 ORDER BY 1 DESC
  101. }
  102. } {0x119 0x2D {}}
  103. do_test collate1-1.99 {
  104. execsql {
  105. DROP TABLE collate1t1;
  106. }
  107. } {}
  108. do_test collate1-2.0 {
  109. execsql {
  110. CREATE TABLE collate1t1(c1, c2);
  111. INSERT INTO collate1t1 VALUES('5', '0x11');
  112. INSERT INTO collate1t1 VALUES('5', '0xA');
  113. INSERT INTO collate1t1 VALUES(NULL, NULL);
  114. INSERT INTO collate1t1 VALUES('7', '0xA');
  115. INSERT INTO collate1t1 VALUES('11', '0x11');
  116. INSERT INTO collate1t1 VALUES('11', '0x101');
  117. }
  118. } {}
  119. do_test collate1-2.2 {
  120. execsql {
  121. SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
  122. }
  123. } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
  124. do_test collate1-2.3 {
  125. execsql {
  126. SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary, 2 COLLATE hex;
  127. }
  128. } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
  129. do_test collate1-2.4 {
  130. execsql {
  131. SELECT c1, c2 FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
  132. }
  133. } {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
  134. do_test collate1-2.5 {
  135. execsql {
  136. SELECT c1, c2 FROM collate1t1
  137. ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex DESC;
  138. }
  139. } {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
  140. do_test collate1-2.6 {
  141. execsql {
  142. SELECT c1, c2 FROM collate1t1
  143. ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
  144. }
  145. } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
  146. do_test collate1-2.12.1 {
  147. execsql {
  148. SELECT c1 COLLATE numeric, c2 FROM collate1t1
  149. ORDER BY 1, 2 COLLATE hex;
  150. }
  151. } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
  152. do_test collate1-2.12.2 {
  153. execsql {
  154. SELECT c1 COLLATE hex, c2 FROM collate1t1
  155. ORDER BY 1 COLLATE numeric, 2 COLLATE hex;
  156. }
  157. } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
  158. do_test collate1-2.12.3 {
  159. execsql {
  160. SELECT c1, c2 COLLATE hex FROM collate1t1
  161. ORDER BY 1 COLLATE numeric, 2;
  162. }
  163. } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
  164. do_test collate1-2.12.4 {
  165. execsql {
  166. SELECT c1 COLLATE numeric, c2 COLLATE hex
  167. FROM collate1t1
  168. ORDER BY 1, 2;
  169. }
  170. } {{} {} 5 0xA 5 0x11 7 0xA 11 0x11 11 0x101}
  171. do_test collate1-2.13 {
  172. execsql {
  173. SELECT c1 COLLATE binary, c2 COLLATE hex
  174. FROM collate1t1
  175. ORDER BY 1, 2;
  176. }
  177. } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
  178. do_test collate1-2.14 {
  179. execsql {
  180. SELECT c1, c2
  181. FROM collate1t1 ORDER BY 1 COLLATE binary DESC, 2 COLLATE hex;
  182. }
  183. } {7 0xA 5 0xA 5 0x11 11 0x11 11 0x101 {} {}}
  184. do_test collate1-2.15 {
  185. execsql {
  186. SELECT c1 COLLATE binary, c2 COLLATE hex
  187. FROM collate1t1
  188. ORDER BY 1 DESC, 2 DESC;
  189. }
  190. } {7 0xA 5 0x11 5 0xA 11 0x101 11 0x11 {} {}}
  191. do_test collate1-2.16 {
  192. execsql {
  193. SELECT c1 COLLATE hex, c2 COLLATE binary
  194. FROM collate1t1
  195. ORDER BY 1 COLLATE binary ASC, 2 COLLATE hex ASC;
  196. }
  197. } {{} {} 11 0x11 11 0x101 5 0xA 5 0x11 7 0xA}
  198. do_test collate1-2.99 {
  199. execsql {
  200. DROP TABLE collate1t1;
  201. }
  202. } {}
  203. #
  204. # These tests ensure that the default collation type for a column is used
  205. # by an ORDER BY clause correctly. The focus is all the different ways
  206. # the column can be referenced. i.e. a, collate2t1.a, main.collate2t1.a etc.
  207. #
  208. do_test collate1-3.0 {
  209. execsql {
  210. CREATE TABLE collate1t1(a COLLATE hex, b);
  211. INSERT INTO collate1t1 VALUES( '0x5', 5 );
  212. INSERT INTO collate1t1 VALUES( '1', 1 );
  213. INSERT INTO collate1t1 VALUES( '0x45', 69 );
  214. INSERT INTO collate1t1 VALUES( NULL, NULL );
  215. SELECT * FROM collate1t1 ORDER BY a;
  216. }
  217. } {{} {} 1 1 0x5 5 0x45 69}
  218. do_test collate1-3.1 {
  219. execsql {
  220. SELECT * FROM collate1t1 ORDER BY 1;
  221. }
  222. } {{} {} 1 1 0x5 5 0x45 69}
  223. do_test collate1-3.2 {
  224. execsql {
  225. SELECT * FROM collate1t1 ORDER BY collate1t1.a;
  226. }
  227. } {{} {} 1 1 0x5 5 0x45 69}
  228. do_test collate1-3.3 {
  229. execsql {
  230. SELECT * FROM collate1t1 ORDER BY main.collate1t1.a;
  231. }
  232. } {{} {} 1 1 0x5 5 0x45 69}
  233. do_test collate1-3.4 {
  234. execsql {
  235. SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1;
  236. }
  237. } {{} {} 1 1 0x5 5 0x45 69}
  238. do_test collate1-3.5 {
  239. execsql {
  240. SELECT a as c1, b as c2 FROM collate1t1 ORDER BY c1 COLLATE binary;
  241. }
  242. } {{} {} 0x45 69 0x5 5 1 1}
  243. do_test collate1-3.5.1 {
  244. execsql {
  245. SELECT a COLLATE binary as c1, b as c2
  246. FROM collate1t1 ORDER BY c1;
  247. }
  248. } {{} {} 0x45 69 0x5 5 1 1}
  249. do_test collate1-3.6 {
  250. execsql {
  251. DROP TABLE collate1t1;
  252. }
  253. } {}
  254. # Update for SQLite version 3. The collate1-4.* test cases were written
  255. # before manifest types were introduced. The following test cases still
  256. # work, due to the 'affinity' mechanism, but they don't prove anything
  257. # about collation sequences.
  258. #
  259. do_test collate1-4.0 {
  260. execsql {
  261. CREATE TABLE collate1t1(c1 numeric, c2 text);
  262. INSERT INTO collate1t1 VALUES(1, 1);
  263. INSERT INTO collate1t1 VALUES(12, 12);
  264. INSERT INTO collate1t1 VALUES(NULL, NULL);
  265. INSERT INTO collate1t1 VALUES(101, 101);
  266. }
  267. } {}
  268. do_test collate1-4.1 {
  269. execsql {
  270. SELECT c1 FROM collate1t1 ORDER BY 1;
  271. }
  272. } {{} 1 12 101}
  273. do_test collate1-4.2 {
  274. execsql {
  275. SELECT c2 FROM collate1t1 ORDER BY 1;
  276. }
  277. } {{} 1 101 12}
  278. do_test collate1-4.3 {
  279. execsql {
  280. SELECT c2+0 FROM collate1t1 ORDER BY 1;
  281. }
  282. } {{} 1 12 101}
  283. do_test collate1-4.4 {
  284. execsql {
  285. SELECT c1||'' FROM collate1t1 ORDER BY 1;
  286. }
  287. } {{} 1 101 12}
  288. do_test collate1-4.4.1 {
  289. execsql {
  290. SELECT (c1||'') COLLATE numeric FROM collate1t1 ORDER BY 1;
  291. }
  292. } {{} 1 12 101}
  293. do_test collate1-4.5 {
  294. execsql {
  295. DROP TABLE collate1t1;
  296. }
  297. } {}
  298. # A problem reported on the mailing list: A CREATE TABLE statement
  299. # is allowed to have two or more COLLATE clauses on the same column.
  300. # That probably ought to be an error, but we allow it for backwards
  301. # compatibility. Just make sure it works and doesn't leak memory.
  302. #
  303. do_test collate1-5.1 {
  304. execsql {
  305. CREATE TABLE c5(
  306. id INTEGER PRIMARY KEY,
  307. a TEXT COLLATE binary COLLATE nocase COLLATE rtrim,
  308. b TEXT COLLATE nocase COLLATE binary,
  309. c TEXT COLLATE rtrim COLLATE binary COLLATE rtrim COLLATE nocase
  310. );
  311. INSERT INTO c5 VALUES(1, 'abc','abc','abc');
  312. INSERT INTO c5 VALUES(2, 'abc ','ABC','ABC');
  313. SELECT id FROM c5 WHERE a='abc' ORDER BY id;
  314. }
  315. } {1 2}
  316. do_test collate1-5.2 {
  317. execsql {
  318. SELECT id FROM c5 WHERE b='abc' ORDER BY id;
  319. }
  320. } {1}
  321. do_test collate1-5.3 {
  322. execsql {
  323. SELECT id FROM c5 WHERE c='abc' ORDER BY id;
  324. }
  325. } {1 2}
  326. finish_test