PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/src/sqlite/test/descidx1.test

#
Unknown | 364 lines | 347 code | 17 blank | 0 comment | 0 complexity | 9e73f63865467dd7316c11e2df9b8f9f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. # 2005 December 21
  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 script is descending indices.
  13. #
  14. # $Id: descidx1.test,v 1.10 2008/03/19 00:21:31 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Do not use a codec for tests in this file, as the database file is
  19. # manipulated directly using tcl scripts (using the [hexio_write] command).
  20. #
  21. do_not_use_codec
  22. db eval {PRAGMA legacy_file_format=OFF}
  23. # This procedure sets the value of the file-format in file 'test.db'
  24. # to $newval. Also, the schema cookie is incremented.
  25. #
  26. proc set_file_format {newval} {
  27. hexio_write test.db 44 [hexio_render_int32 $newval]
  28. set schemacookie [hexio_get_int [hexio_read test.db 40 4]]
  29. incr schemacookie
  30. hexio_write test.db 40 [hexio_render_int32 $schemacookie]
  31. return {}
  32. }
  33. # This procedure returns the value of the file-format in file 'test.db'.
  34. #
  35. proc get_file_format {{fname test.db}} {
  36. return [hexio_get_int [hexio_read $fname 44 4]]
  37. }
  38. # Verify that the file format starts as 4.
  39. #
  40. do_test descidx1-1.1 {
  41. execsql {
  42. CREATE TABLE t1(a,b);
  43. CREATE INDEX i1 ON t1(b ASC);
  44. }
  45. get_file_format
  46. } {4}
  47. do_test descidx1-1.2 {
  48. execsql {
  49. CREATE INDEX i2 ON t1(a DESC);
  50. }
  51. get_file_format
  52. } {4}
  53. # Put some information in the table and verify that the descending
  54. # index actually works.
  55. #
  56. do_test descidx1-2.1 {
  57. execsql {
  58. INSERT INTO t1 VALUES(1,1);
  59. INSERT INTO t1 VALUES(2,2);
  60. INSERT INTO t1 SELECT a+2, a+2 FROM t1;
  61. INSERT INTO t1 SELECT a+4, a+4 FROM t1;
  62. SELECT b FROM t1 WHERE a>3 AND a<7;
  63. }
  64. } {6 5 4}
  65. do_test descidx1-2.2 {
  66. execsql {
  67. SELECT a FROM t1 WHERE b>3 AND b<7;
  68. }
  69. } {4 5 6}
  70. do_test descidx1-2.3 {
  71. execsql {
  72. SELECT b FROM t1 WHERE a>=3 AND a<7;
  73. }
  74. } {6 5 4 3}
  75. do_test descidx1-2.4 {
  76. execsql {
  77. SELECT b FROM t1 WHERE a>3 AND a<=7;
  78. }
  79. } {7 6 5 4}
  80. do_test descidx1-2.5 {
  81. execsql {
  82. SELECT b FROM t1 WHERE a>=3 AND a<=7;
  83. }
  84. } {7 6 5 4 3}
  85. do_test descidx1-2.6 {
  86. execsql {
  87. SELECT a FROM t1 WHERE b>=3 AND b<=7;
  88. }
  89. } {3 4 5 6 7}
  90. # This procedure executes the SQL. Then it checks to see if the OP_Sort
  91. # opcode was executed. If an OP_Sort did occur, then "sort" is appended
  92. # to the result. If no OP_Sort happened, then "nosort" is appended.
  93. #
  94. # This procedure is used to check to make sure sorting is or is not
  95. # occurring as expected.
  96. #
  97. proc cksort {sql} {
  98. set ::sqlite_sort_count 0
  99. set data [execsql $sql]
  100. if {$::sqlite_sort_count} {set x sort} {set x nosort}
  101. lappend data $x
  102. return $data
  103. }
  104. # Test sorting using a descending index.
  105. #
  106. do_test descidx1-3.1 {
  107. cksort {SELECT a FROM t1 ORDER BY a}
  108. } {1 2 3 4 5 6 7 8 nosort}
  109. do_test descidx1-3.2 {
  110. cksort {SELECT a FROM t1 ORDER BY a ASC}
  111. } {1 2 3 4 5 6 7 8 nosort}
  112. do_test descidx1-3.3 {
  113. cksort {SELECT a FROM t1 ORDER BY a DESC}
  114. } {8 7 6 5 4 3 2 1 nosort}
  115. do_test descidx1-3.4 {
  116. cksort {SELECT b FROM t1 ORDER BY a}
  117. } {1 2 3 4 5 6 7 8 nosort}
  118. do_test descidx1-3.5 {
  119. cksort {SELECT b FROM t1 ORDER BY a ASC}
  120. } {1 2 3 4 5 6 7 8 nosort}
  121. do_test descidx1-3.6 {
  122. cksort {SELECT b FROM t1 ORDER BY a DESC}
  123. } {8 7 6 5 4 3 2 1 nosort}
  124. do_test descidx1-3.7 {
  125. cksort {SELECT a FROM t1 ORDER BY b}
  126. } {1 2 3 4 5 6 7 8 nosort}
  127. do_test descidx1-3.8 {
  128. cksort {SELECT a FROM t1 ORDER BY b ASC}
  129. } {1 2 3 4 5 6 7 8 nosort}
  130. do_test descidx1-3.9 {
  131. cksort {SELECT a FROM t1 ORDER BY b DESC}
  132. } {8 7 6 5 4 3 2 1 nosort}
  133. do_test descidx1-3.10 {
  134. cksort {SELECT b FROM t1 ORDER BY b}
  135. } {1 2 3 4 5 6 7 8 nosort}
  136. do_test descidx1-3.11 {
  137. cksort {SELECT b FROM t1 ORDER BY b ASC}
  138. } {1 2 3 4 5 6 7 8 nosort}
  139. do_test descidx1-3.12 {
  140. cksort {SELECT b FROM t1 ORDER BY b DESC}
  141. } {8 7 6 5 4 3 2 1 nosort}
  142. do_test descidx1-3.21 {
  143. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a}
  144. } {4 5 6 7 nosort}
  145. do_test descidx1-3.22 {
  146. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
  147. } {4 5 6 7 nosort}
  148. do_test descidx1-3.23 {
  149. cksort {SELECT a FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
  150. } {7 6 5 4 nosort}
  151. do_test descidx1-3.24 {
  152. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a}
  153. } {4 5 6 7 nosort}
  154. do_test descidx1-3.25 {
  155. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a ASC}
  156. } {4 5 6 7 nosort}
  157. do_test descidx1-3.26 {
  158. cksort {SELECT b FROM t1 WHERE a>3 AND a<8 ORDER BY a DESC}
  159. } {7 6 5 4 nosort}
  160. # Create a table with indices that are descending on some terms and
  161. # ascending on others.
  162. #
  163. ifcapable bloblit {
  164. do_test descidx1-4.1 {
  165. execsql {
  166. CREATE TABLE t2(a INT, b TEXT, c BLOB, d REAL);
  167. CREATE INDEX i3 ON t2(a ASC, b DESC, c ASC);
  168. CREATE INDEX i4 ON t2(b DESC, a ASC, d DESC);
  169. INSERT INTO t2 VALUES(1,'one',x'31',1.0);
  170. INSERT INTO t2 VALUES(2,'two',x'3232',2.0);
  171. INSERT INTO t2 VALUES(3,'three',x'333333',3.0);
  172. INSERT INTO t2 VALUES(4,'four',x'34343434',4.0);
  173. INSERT INTO t2 VALUES(5,'five',x'3535353535',5.0);
  174. INSERT INTO t2 VALUES(6,'six',x'363636363636',6.0);
  175. INSERT INTO t2 VALUES(2,'two',x'323232',2.1);
  176. INSERT INTO t2 VALUES(2,'zwei',x'3232',2.2);
  177. INSERT INTO t2 VALUES(2,NULL,NULL,2.3);
  178. SELECT count(*) FROM t2;
  179. }
  180. } {9}
  181. do_test descidx1-4.2 {
  182. execsql {
  183. SELECT d FROM t2 ORDER BY a;
  184. }
  185. } {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
  186. do_test descidx1-4.3 {
  187. execsql {
  188. SELECT d FROM t2 WHERE a>=2;
  189. }
  190. } {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
  191. do_test descidx1-4.4 {
  192. execsql {
  193. SELECT d FROM t2 WHERE a>2;
  194. }
  195. } {3.0 4.0 5.0 6.0}
  196. do_test descidx1-4.5 {
  197. execsql {
  198. SELECT d FROM t2 WHERE a=2 AND b>'two';
  199. }
  200. } {2.2}
  201. do_test descidx1-4.6 {
  202. execsql {
  203. SELECT d FROM t2 WHERE a=2 AND b>='two';
  204. }
  205. } {2.2 2.0 2.1}
  206. do_test descidx1-4.7 {
  207. execsql {
  208. SELECT d FROM t2 WHERE a=2 AND b<'two';
  209. }
  210. } {}
  211. do_test descidx1-4.8 {
  212. execsql {
  213. SELECT d FROM t2 WHERE a=2 AND b<='two';
  214. }
  215. } {2.0 2.1}
  216. }
  217. do_test descidx1-5.1 {
  218. execsql {
  219. CREATE TABLE t3(a,b,c,d);
  220. CREATE INDEX t3i1 ON t3(a DESC, b ASC, c DESC, d ASC);
  221. INSERT INTO t3 VALUES(0,0,0,0);
  222. INSERT INTO t3 VALUES(0,0,0,1);
  223. INSERT INTO t3 VALUES(0,0,1,0);
  224. INSERT INTO t3 VALUES(0,0,1,1);
  225. INSERT INTO t3 VALUES(0,1,0,0);
  226. INSERT INTO t3 VALUES(0,1,0,1);
  227. INSERT INTO t3 VALUES(0,1,1,0);
  228. INSERT INTO t3 VALUES(0,1,1,1);
  229. INSERT INTO t3 VALUES(1,0,0,0);
  230. INSERT INTO t3 VALUES(1,0,0,1);
  231. INSERT INTO t3 VALUES(1,0,1,0);
  232. INSERT INTO t3 VALUES(1,0,1,1);
  233. INSERT INTO t3 VALUES(1,1,0,0);
  234. INSERT INTO t3 VALUES(1,1,0,1);
  235. INSERT INTO t3 VALUES(1,1,1,0);
  236. INSERT INTO t3 VALUES(1,1,1,1);
  237. SELECT count(*) FROM t3;
  238. }
  239. } {16}
  240. do_test descidx1-5.2 {
  241. cksort {
  242. SELECT a||b||c||d FROM t3 ORDER BY a,b,c,d;
  243. }
  244. } {0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 sort}
  245. do_test descidx1-5.3 {
  246. cksort {
  247. SELECT a||b||c||d FROM t3 ORDER BY a DESC, b ASC, c DESC, d ASC;
  248. }
  249. } {1010 1011 1000 1001 1110 1111 1100 1101 0010 0011 0000 0001 0110 0111 0100 0101 nosort}
  250. do_test descidx1-5.4 {
  251. cksort {
  252. SELECT a||b||c||d FROM t3 ORDER BY a ASC, b DESC, c ASC, d DESC;
  253. }
  254. } {0101 0100 0111 0110 0001 0000 0011 0010 1101 1100 1111 1110 1001 1000 1011 1010 nosort}
  255. do_test descidx1-5.5 {
  256. cksort {
  257. SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b ASC, c DESC
  258. }
  259. } {101 100 111 110 001 000 011 010 nosort}
  260. do_test descidx1-5.6 {
  261. cksort {
  262. SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c ASC
  263. }
  264. } {010 011 000 001 110 111 100 101 nosort}
  265. do_test descidx1-5.7 {
  266. cksort {
  267. SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b DESC, c DESC
  268. }
  269. } {011 010 001 000 111 110 101 100 sort}
  270. do_test descidx1-5.8 {
  271. cksort {
  272. SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a ASC, b ASC, c ASC
  273. }
  274. } {000 001 010 011 100 101 110 111 sort}
  275. do_test descidx1-5.9 {
  276. cksort {
  277. SELECT a||b||c FROM t3 WHERE d=0 ORDER BY a DESC, b DESC, c ASC
  278. }
  279. } {110 111 100 101 010 011 000 001 sort}
  280. # Test the legacy_file_format pragma here because we have access to
  281. # the get_file_format command.
  282. #
  283. ifcapable legacyformat {
  284. do_test descidx1-6.1 {
  285. db close
  286. file delete -force test.db test.db-journal
  287. sqlite3 db test.db
  288. execsql {PRAGMA legacy_file_format}
  289. } {1}
  290. } else {
  291. do_test descidx1-6.1 {
  292. db close
  293. file delete -force test.db test.db-journal
  294. sqlite3 db test.db
  295. execsql {PRAGMA legacy_file_format}
  296. } {0}
  297. }
  298. do_test descidx1-6.2 {
  299. execsql {PRAGMA legacy_file_format=YES}
  300. execsql {PRAGMA legacy_file_format}
  301. } {1}
  302. do_test descidx1-6.3 {
  303. execsql {
  304. CREATE TABLE t1(a,b,c);
  305. }
  306. get_file_format
  307. } {1}
  308. ifcapable vacuum {
  309. # Verify that the file format is preserved across a vacuum.
  310. do_test descidx1-6.3.1 {
  311. execsql {VACUUM}
  312. get_file_format
  313. } {1}
  314. }
  315. do_test descidx1-6.4 {
  316. db close
  317. file delete -force test.db test.db-journal
  318. sqlite3 db test.db
  319. execsql {PRAGMA legacy_file_format=NO}
  320. execsql {PRAGMA legacy_file_format}
  321. } {0}
  322. do_test descidx1-6.5 {
  323. execsql {
  324. CREATE TABLE t1(a,b,c);
  325. CREATE INDEX i1 ON t1(a ASC, b DESC, c ASC);
  326. INSERT INTO t1 VALUES(1,2,3);
  327. INSERT INTO t1 VALUES(1,1,0);
  328. INSERT INTO t1 VALUES(1,2,1);
  329. INSERT INTO t1 VALUES(1,3,4);
  330. }
  331. get_file_format
  332. } {4}
  333. ifcapable vacuum {
  334. # Verify that the file format is preserved across a vacuum.
  335. do_test descidx1-6.6 {
  336. execsql {VACUUM}
  337. get_file_format
  338. } {4}
  339. do_test descidx1-6.7 {
  340. execsql {
  341. PRAGMA legacy_file_format=ON;
  342. VACUUM;
  343. }
  344. get_file_format
  345. } {4}
  346. }
  347. finish_test