fts2q.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # 2008 June 26
  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 focus
  12. # of this script is testing the FTS2 module's optimize() function.
  13. #
  14. # $Id: fts2q.test,v 1.2 2008/07/22 23:49:44 shess Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # If SQLITE_ENABLE_FTS2 is not defined, omit this file.
  19. ifcapable !fts2 {
  20. finish_test
  21. return
  22. }
  23. #*************************************************************************
  24. # Probe to see if support for the FTS2 dump_* functions is compiled in.
  25. # TODO(shess): Change main.mk to do the right thing and remove this test.
  26. db eval {
  27. DROP TABLE IF EXISTS t1;
  28. CREATE VIRTUAL TABLE t1 USING fts2(c);
  29. INSERT INTO t1 (rowid, c) VALUES (1, 'x');
  30. }
  31. set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
  32. set r {1 {unable to use function dump_terms in the requested context}}
  33. if {[catchsql $s]==$r} {
  34. finish_test
  35. return
  36. }
  37. #*************************************************************************
  38. # Utility function to check for the expected terms in the segment
  39. # level/index. _all version does same but for entire index.
  40. proc check_terms {test level index terms} {
  41. # TODO(shess): Figure out why uplevel in do_test can't catch
  42. # $level and $index directly.
  43. set ::level $level
  44. set ::index $index
  45. do_test $test.terms {
  46. execsql {
  47. SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
  48. }
  49. } [list $terms]
  50. }
  51. proc check_terms_all {test terms} {
  52. do_test $test.terms {
  53. execsql {
  54. SELECT dump_terms(t1) FROM t1 LIMIT 1;
  55. }
  56. } [list $terms]
  57. }
  58. # Utility function to check for the expected doclist for the term in
  59. # segment level/index. _all version does same for entire index.
  60. proc check_doclist {test level index term doclist} {
  61. # TODO(shess): Again, why can't the non-:: versions work?
  62. set ::term $term
  63. set ::level $level
  64. set ::index $index
  65. do_test $test {
  66. execsql {
  67. SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
  68. }
  69. } [list $doclist]
  70. }
  71. proc check_doclist_all {test term doclist} {
  72. set ::term $term
  73. do_test $test {
  74. execsql {
  75. SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
  76. }
  77. } [list $doclist]
  78. }
  79. #*************************************************************************
  80. # Test results when all rows are deleted and one is added back.
  81. # Previously older segments would continue to exist, but now the index
  82. # should be dropped when the table is empty. The results should look
  83. # exactly like we never added the earlier rows in the first place.
  84. db eval {
  85. DROP TABLE IF EXISTS t1;
  86. CREATE VIRTUAL TABLE t1 USING fts2(c);
  87. INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
  88. INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
  89. INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
  90. DELETE FROM t1 WHERE 1=1; -- Delete each row rather than dropping table.
  91. INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
  92. }
  93. # Should be a single initial segment.
  94. do_test fts2q-1.segments {
  95. execsql {
  96. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  97. }
  98. } {0 0}
  99. do_test fts2q-1.matches {
  100. execsql {
  101. SELECT OFFSETS(t1) FROM t1
  102. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
  103. }
  104. } {{0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}}
  105. check_terms_all fts2q-1.1 {a is test this}
  106. check_doclist_all fts2q-1.1.1 a {[1 0[2]]}
  107. check_doclist_all fts2q-1.1.2 is {[1 0[1]]}
  108. check_doclist_all fts2q-1.1.3 test {[1 0[3]]}
  109. check_doclist_all fts2q-1.1.4 this {[1 0[0]]}
  110. check_terms fts2q-1.2 0 0 {a is test this}
  111. check_doclist fts2q-1.2.1 0 0 a {[1 0[2]]}
  112. check_doclist fts2q-1.2.2 0 0 is {[1 0[1]]}
  113. check_doclist fts2q-1.2.3 0 0 test {[1 0[3]]}
  114. check_doclist fts2q-1.2.4 0 0 this {[1 0[0]]}
  115. #*************************************************************************
  116. # Test results when everything is optimized manually.
  117. # NOTE(shess): This is a copy of fts2c-1.3. I've pulled a copy here
  118. # because fts2q-2 and fts2q-3 should have identical results.
  119. db eval {
  120. DROP TABLE IF EXISTS t1;
  121. CREATE VIRTUAL TABLE t1 USING fts2(c);
  122. INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
  123. INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
  124. INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
  125. DELETE FROM t1 WHERE rowid IN (1,3);
  126. DROP TABLE IF EXISTS t1old;
  127. ALTER TABLE t1 RENAME TO t1old;
  128. CREATE VIRTUAL TABLE t1 USING fts2(c);
  129. INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old;
  130. DROP TABLE t1old;
  131. }
  132. # Should be a single optimal segment with the same logical results.
  133. do_test fts2q-2.segments {
  134. execsql {
  135. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  136. }
  137. } {0 0}
  138. do_test fts2q-2.matches {
  139. execsql {
  140. SELECT OFFSETS(t1) FROM t1
  141. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
  142. }
  143. } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
  144. check_terms_all fts2q-2.1 {a test that was}
  145. check_doclist_all fts2q-2.1.1 a {[2 0[2]]}
  146. check_doclist_all fts2q-2.1.2 test {[2 0[3]]}
  147. check_doclist_all fts2q-2.1.3 that {[2 0[0]]}
  148. check_doclist_all fts2q-2.1.4 was {[2 0[1]]}
  149. check_terms fts2q-2.2 0 0 {a test that was}
  150. check_doclist fts2q-2.2.1 0 0 a {[2 0[2]]}
  151. check_doclist fts2q-2.2.2 0 0 test {[2 0[3]]}
  152. check_doclist fts2q-2.2.3 0 0 that {[2 0[0]]}
  153. check_doclist fts2q-2.2.4 0 0 was {[2 0[1]]}
  154. #*************************************************************************
  155. # Test results when everything is optimized via optimize().
  156. db eval {
  157. DROP TABLE IF EXISTS t1;
  158. CREATE VIRTUAL TABLE t1 USING fts2(c);
  159. INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
  160. INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
  161. INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
  162. DELETE FROM t1 WHERE rowid IN (1,3);
  163. SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
  164. }
  165. # Should be a single optimal segment with the same logical results.
  166. do_test fts2q-3.segments {
  167. execsql {
  168. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  169. }
  170. } {0 0}
  171. do_test fts2q-3.matches {
  172. execsql {
  173. SELECT OFFSETS(t1) FROM t1
  174. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
  175. }
  176. } {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
  177. check_terms_all fts2q-3.1 {a test that was}
  178. check_doclist_all fts2q-3.1.1 a {[2 0[2]]}
  179. check_doclist_all fts2q-3.1.2 test {[2 0[3]]}
  180. check_doclist_all fts2q-3.1.3 that {[2 0[0]]}
  181. check_doclist_all fts2q-3.1.4 was {[2 0[1]]}
  182. check_terms fts2q-3.2 0 0 {a test that was}
  183. check_doclist fts2q-3.2.1 0 0 a {[2 0[2]]}
  184. check_doclist fts2q-3.2.2 0 0 test {[2 0[3]]}
  185. check_doclist fts2q-3.2.3 0 0 that {[2 0[0]]}
  186. check_doclist fts2q-3.2.4 0 0 was {[2 0[1]]}
  187. #*************************************************************************
  188. # Test optimize() against a table involving segment merges.
  189. # NOTE(shess): Since there's no transaction, each of the INSERT/UPDATE
  190. # statements generates a segment.
  191. db eval {
  192. DROP TABLE IF EXISTS t1;
  193. CREATE VIRTUAL TABLE t1 USING fts2(c);
  194. INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
  195. INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
  196. INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
  197. UPDATE t1 SET c = 'This is a test one' WHERE rowid = 1;
  198. UPDATE t1 SET c = 'That was a test one' WHERE rowid = 2;
  199. UPDATE t1 SET c = 'This is a test one' WHERE rowid = 3;
  200. UPDATE t1 SET c = 'This is a test two' WHERE rowid = 1;
  201. UPDATE t1 SET c = 'That was a test two' WHERE rowid = 2;
  202. UPDATE t1 SET c = 'This is a test two' WHERE rowid = 3;
  203. UPDATE t1 SET c = 'This is a test three' WHERE rowid = 1;
  204. UPDATE t1 SET c = 'That was a test three' WHERE rowid = 2;
  205. UPDATE t1 SET c = 'This is a test three' WHERE rowid = 3;
  206. UPDATE t1 SET c = 'This is a test four' WHERE rowid = 1;
  207. UPDATE t1 SET c = 'That was a test four' WHERE rowid = 2;
  208. UPDATE t1 SET c = 'This is a test four' WHERE rowid = 3;
  209. UPDATE t1 SET c = 'This is a test' WHERE rowid = 1;
  210. UPDATE t1 SET c = 'That was a test' WHERE rowid = 2;
  211. UPDATE t1 SET c = 'This is a test' WHERE rowid = 3;
  212. }
  213. # 2 segments in level 0, 1 in level 1 (18 segments created, 16
  214. # merged).
  215. do_test fts2q-4.segments {
  216. execsql {
  217. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  218. }
  219. } {0 0 0 1 1 0}
  220. do_test fts2q-4.matches {
  221. execsql {
  222. SELECT OFFSETS(t1) FROM t1
  223. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
  224. }
  225. } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
  226. {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
  227. {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
  228. check_terms_all fts2q-4.1 {a four is one test that this three two was}
  229. check_doclist_all fts2q-4.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
  230. check_doclist_all fts2q-4.1.2 four {}
  231. check_doclist_all fts2q-4.1.3 is {[1 0[1]] [3 0[1]]}
  232. check_doclist_all fts2q-4.1.4 one {}
  233. check_doclist_all fts2q-4.1.5 test {[1 0[3]] [2 0[3]] [3 0[3]]}
  234. check_doclist_all fts2q-4.1.6 that {[2 0[0]]}
  235. check_doclist_all fts2q-4.1.7 this {[1 0[0]] [3 0[0]]}
  236. check_doclist_all fts2q-4.1.8 three {}
  237. check_doclist_all fts2q-4.1.9 two {}
  238. check_doclist_all fts2q-4.1.10 was {[2 0[1]]}
  239. check_terms fts2q-4.2 0 0 {a four test that was}
  240. check_doclist fts2q-4.2.1 0 0 a {[2 0[2]]}
  241. check_doclist fts2q-4.2.2 0 0 four {[2]}
  242. check_doclist fts2q-4.2.3 0 0 test {[2 0[3]]}
  243. check_doclist fts2q-4.2.4 0 0 that {[2 0[0]]}
  244. check_doclist fts2q-4.2.5 0 0 was {[2 0[1]]}
  245. check_terms fts2q-4.3 0 1 {a four is test this}
  246. check_doclist fts2q-4.3.1 0 1 a {[3 0[2]]}
  247. check_doclist fts2q-4.3.2 0 1 four {[3]}
  248. check_doclist fts2q-4.3.3 0 1 is {[3 0[1]]}
  249. check_doclist fts2q-4.3.4 0 1 test {[3 0[3]]}
  250. check_doclist fts2q-4.3.5 0 1 this {[3 0[0]]}
  251. check_terms fts2q-4.4 1 0 {a four is one test that this three two was}
  252. check_doclist fts2q-4.4.1 1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]}
  253. check_doclist fts2q-4.4.2 1 0 four {[1] [2 0[4]] [3 0[4]]}
  254. check_doclist fts2q-4.4.3 1 0 is {[1 0[1]] [3 0[1]]}
  255. check_doclist fts2q-4.4.4 1 0 one {[1] [2] [3]}
  256. check_doclist fts2q-4.4.5 1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]}
  257. check_doclist fts2q-4.4.6 1 0 that {[2 0[0]]}
  258. check_doclist fts2q-4.4.7 1 0 this {[1 0[0]] [3 0[0]]}
  259. check_doclist fts2q-4.4.8 1 0 three {[1] [2] [3]}
  260. check_doclist fts2q-4.4.9 1 0 two {[1] [2] [3]}
  261. check_doclist fts2q-4.4.10 1 0 was {[2 0[1]]}
  262. # Optimize should leave the result in the level of the highest-level
  263. # prior segment.
  264. do_test fts2q-4.5 {
  265. execsql {
  266. SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
  267. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  268. }
  269. } {{Index optimized} 1 0}
  270. # Identical to fts2q-4.matches.
  271. do_test fts2q-4.5.matches {
  272. execsql {
  273. SELECT OFFSETS(t1) FROM t1
  274. WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
  275. }
  276. } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
  277. {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
  278. {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
  279. check_terms_all fts2q-4.5.1 {a is test that this was}
  280. check_doclist_all fts2q-4.5.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
  281. check_doclist_all fts2q-4.5.1.2 is {[1 0[1]] [3 0[1]]}
  282. check_doclist_all fts2q-4.5.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
  283. check_doclist_all fts2q-4.5.1.4 that {[2 0[0]]}
  284. check_doclist_all fts2q-4.5.1.5 this {[1 0[0]] [3 0[0]]}
  285. check_doclist_all fts2q-4.5.1.6 was {[2 0[1]]}
  286. check_terms fts2q-4.5.2 1 0 {a is test that this was}
  287. check_doclist fts2q-4.5.2.1 1 0 a {[1 0[2]] [2 0[2]] [3 0[2]]}
  288. check_doclist fts2q-4.5.2.2 1 0 is {[1 0[1]] [3 0[1]]}
  289. check_doclist fts2q-4.5.2.3 1 0 test {[1 0[3]] [2 0[3]] [3 0[3]]}
  290. check_doclist fts2q-4.5.2.4 1 0 that {[2 0[0]]}
  291. check_doclist fts2q-4.5.2.5 1 0 this {[1 0[0]] [3 0[0]]}
  292. check_doclist fts2q-4.5.2.6 1 0 was {[2 0[1]]}
  293. # Re-optimizing does nothing.
  294. do_test fts2q-5.0 {
  295. execsql {
  296. SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
  297. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  298. }
  299. } {{Index already optimal} 1 0}
  300. # Even if we move things around, still does nothing.
  301. do_test fts2q-5.1 {
  302. execsql {
  303. UPDATE t1_segdir SET level = 2 WHERE level = 1 AND idx = 0;
  304. SELECT OPTIMIZE(t1) FROM t1 LIMIT 1;
  305. SELECT level, idx FROM t1_segdir ORDER BY level, idx;
  306. }
  307. } {{Index already optimal} 2 0}
  308. finish_test