fts5ae.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # 2014 June 17
  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 testing the FTS5 module.
  13. #
  14. #
  15. source [file join [file dirname [info script]] fts5_common.tcl]
  16. set testprefix fts5ae
  17. # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
  18. ifcapable !fts5 {
  19. finish_test
  20. return
  21. }
  22. foreach_detail_mode $testprefix {
  23. do_execsql_test 1.0 {
  24. CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=%DETAIL%);
  25. INSERT INTO t1(t1, rank) VALUES('pgsz', 32);
  26. }
  27. do_execsql_test 1.1 {
  28. INSERT INTO t1 VALUES('hello', 'world');
  29. SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC;
  30. } {1}
  31. do_execsql_test 1.2 {
  32. INSERT INTO t1 VALUES('world', 'hello');
  33. SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC;
  34. } {1 2}
  35. do_execsql_test 1.3 {
  36. INSERT INTO t1 VALUES('world', 'world');
  37. SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC;
  38. } {1 2}
  39. do_execsql_test 1.4.1 {
  40. INSERT INTO t1 VALUES('hello', 'hello');
  41. }
  42. do_execsql_test 1.4.2 {
  43. SELECT rowid FROM t1 WHERE t1 MATCH 'hello' ORDER BY rowid ASC;
  44. } {1 2 4}
  45. fts5_aux_test_functions db
  46. #-------------------------------------------------------------------------
  47. #
  48. do_execsql_test 2.0 {
  49. CREATE VIRTUAL TABLE t2 USING fts5(x, y, detail=%DETAIL%);
  50. INSERT INTO t2 VALUES('u t l w w m s', 'm f m o l t k o p e');
  51. INSERT INTO t2 VALUES('f g q e l n d m z x q', 'z s i i i m f w w f n g p');
  52. }
  53. do_execsql_test 2.1 {
  54. SELECT rowid, fts5_test_poslist(t2) FROM t2
  55. WHERE t2 MATCH 'm' ORDER BY rowid;
  56. } {
  57. 1 {0.0.5 0.1.0 0.1.2}
  58. 2 {0.0.7 0.1.5}
  59. }
  60. do_execsql_test 2.2 {
  61. SELECT rowid, fts5_test_poslist(t2) FROM t2
  62. WHERE t2 MATCH 'u OR q' ORDER BY rowid;
  63. } {
  64. 1 {0.0.0}
  65. 2 {1.0.2 1.0.10}
  66. }
  67. if {[detail_is_full]} {
  68. do_execsql_test 2.3 {
  69. SELECT rowid, fts5_test_poslist(t2) FROM t2
  70. WHERE t2 MATCH 'y:o' ORDER BY rowid;
  71. } {
  72. 1 {0.1.3 0.1.7}
  73. }
  74. }
  75. #-------------------------------------------------------------------------
  76. #
  77. do_execsql_test 3.0 {
  78. CREATE VIRTUAL TABLE t3 USING fts5(x, y, detail=%DETAIL%);
  79. INSERT INTO t3 VALUES( 'j f h o x x a z g b a f a m i b', 'j z c z y x w t');
  80. INSERT INTO t3 VALUES( 'r c', '');
  81. }
  82. if {[detail_is_full]} {
  83. do_execsql_test 3.1 {
  84. SELECT rowid, fts5_test_poslist(t3) FROM t3 WHERE t3 MATCH 'NEAR(a b)';
  85. } {
  86. 1 {0.0.6 1.0.9 0.0.10 0.0.12 1.0.15}
  87. }
  88. do_execsql_test 3.2 {
  89. SELECT rowid, fts5_test_poslist(t3) FROM t3 WHERE t3 MATCH 'NEAR(r c)';
  90. } {
  91. 2 {0.0.0 1.0.1}
  92. }
  93. }
  94. do_execsql_test 3.3 {
  95. INSERT INTO t3
  96. VALUES('k x j r m a d o i z j', 'r t t t f e b r x i v j v g o');
  97. SELECT rowid, fts5_test_poslist(t3)
  98. FROM t3 WHERE t3 MATCH 'a OR b AND c';
  99. } {
  100. 1 {0.0.6 1.0.9 0.0.10 0.0.12 1.0.15 2.1.2}
  101. 3 0.0.5
  102. }
  103. #-------------------------------------------------------------------------
  104. #
  105. do_execsql_test 4.0 {
  106. CREATE VIRTUAL TABLE t4 USING fts5(x, y, detail=%DETAIL%);
  107. INSERT INTO t4
  108. VALUES('k x j r m a d o i z j', 'r t t t f e b r x i v j v g o');
  109. }
  110. do_execsql_test 4.1 {
  111. SELECT rowid, fts5_test_poslist(t4) FROM t4 WHERE t4 MATCH 'a OR b AND c';
  112. } {
  113. 1 0.0.5
  114. }
  115. #-------------------------------------------------------------------------
  116. # Test that the xColumnSize() and xColumnAvgsize() APIs work.
  117. #
  118. reset_db
  119. fts5_aux_test_functions db
  120. do_execsql_test 5.1 {
  121. CREATE VIRTUAL TABLE t5 USING fts5(x, y, detail=%DETAIL%);
  122. INSERT INTO t5 VALUES('a b c d', 'e f g h i j');
  123. INSERT INTO t5 VALUES('', 'a');
  124. INSERT INTO t5 VALUES('a', '');
  125. }
  126. do_execsql_test 5.2 {
  127. SELECT rowid, fts5_test_columnsize(t5) FROM t5 WHERE t5 MATCH 'a'
  128. ORDER BY rowid DESC;
  129. } {
  130. 3 {1 0}
  131. 2 {0 1}
  132. 1 {4 6}
  133. }
  134. do_execsql_test 5.3 {
  135. SELECT rowid, fts5_test_columntext(t5) FROM t5 WHERE t5 MATCH 'a'
  136. ORDER BY rowid DESC;
  137. } {
  138. 3 {a {}}
  139. 2 {{} a}
  140. 1 {{a b c d} {e f g h i j}}
  141. }
  142. do_execsql_test 5.4 {
  143. SELECT rowid, fts5_test_columntotalsize(t5) FROM t5 WHERE t5 MATCH 'a'
  144. ORDER BY rowid DESC;
  145. } {
  146. 3 {5 7}
  147. 2 {5 7}
  148. 1 {5 7}
  149. }
  150. do_execsql_test 5.5 {
  151. INSERT INTO t5 VALUES('x y z', 'v w x y z');
  152. SELECT rowid, fts5_test_columntotalsize(t5) FROM t5 WHERE t5 MATCH 'a'
  153. ORDER BY rowid DESC;
  154. } {
  155. 3 {8 12}
  156. 2 {8 12}
  157. 1 {8 12}
  158. }
  159. #-------------------------------------------------------------------------
  160. # Test the xTokenize() API
  161. #
  162. reset_db
  163. fts5_aux_test_functions db
  164. do_execsql_test 6.1 {
  165. CREATE VIRTUAL TABLE t6 USING fts5(x, y, detail=%DETAIL%);
  166. INSERT INTO t6 VALUES('There are more', 'things in heaven and earth');
  167. INSERT INTO t6 VALUES(', Horatio, Than are', 'dreamt of in your philosophy.');
  168. }
  169. do_execsql_test 6.2 {
  170. SELECT rowid, fts5_test_tokenize(t6) FROM t6 WHERE t6 MATCH 't*'
  171. } {
  172. 1 {{there are more} {things in heaven and earth}}
  173. 2 {{horatio than are} {dreamt of in your philosophy}}
  174. }
  175. #-------------------------------------------------------------------------
  176. # Test the xQueryPhrase() API
  177. #
  178. reset_db
  179. fts5_aux_test_functions db
  180. do_execsql_test 7.1 {
  181. CREATE VIRTUAL TABLE t7 USING fts5(x, y, detail=%DETAIL%);
  182. }
  183. do_test 7.2 {
  184. foreach {x y} {
  185. {q i b w s a a e l o} {i b z a l f p t e u}
  186. {b a z t a l o x d i} {b p a d b f h d w y}
  187. {z m h n p p u i e g} {v h d v b x j j c z}
  188. {a g i m v a u c b i} {p k s o t l r t b m}
  189. {v v c j o d a s c p} {f f v o k p o f o g}
  190. } {
  191. execsql {INSERT INTO t7 VALUES($x, $y)}
  192. }
  193. execsql { SELECT count(*) FROM t7 }
  194. } {5}
  195. foreach {tn q res} {
  196. 1 a {{4 2}}
  197. 2 b {{3 4}}
  198. 3 c {{2 1}}
  199. 4 d {{2 2}}
  200. 5 {a AND b} {{4 2} {3 4}}
  201. 6 {a OR b OR c OR d} {{4 2} {3 4} {2 1} {2 2}}
  202. } {
  203. do_execsql_test 7.3.$tn {
  204. SELECT fts5_test_queryphrase(t7) FROM t7 WHERE t7 MATCH $q LIMIT 1
  205. } [list $res]
  206. }
  207. do_execsql_test 7.4 {
  208. SELECT fts5_test_rowcount(t7) FROM t7 WHERE t7 MATCH 'a';
  209. } {5 5 5 5}
  210. #do_execsql_test 7.4 {
  211. # SELECT rowid, bm25debug(t7) FROM t7 WHERE t7 MATCH 'a';
  212. #} {5 5 5 5}
  213. #
  214. #-------------------------------------------------------------------------
  215. #
  216. do_test 8.1 {
  217. execsql { CREATE VIRTUAL TABLE t8 USING fts5(x, y, detail=%DETAIL%) }
  218. foreach {rowid x y} {
  219. 0 {A o} {o o o C o o o o o o o o}
  220. 1 {o o B} {o o o C C o o o o o o o}
  221. 2 {A o o} {o o o o D D o o o o o o}
  222. 3 {o B} {o o o o o D o o o o o o}
  223. 4 {E o G} {H o o o o o o o o o o o}
  224. 5 {F o G} {I o J o o o o o o o o o}
  225. 6 {E o o} {H o J o o o o o o o o o}
  226. 7 {o o o} {o o o o o o o o o o o o}
  227. 9 {o o o} {o o o o o o o o o o o o}
  228. } {
  229. execsql { INSERT INTO t8(rowid, x, y) VALUES($rowid, $x, $y) }
  230. }
  231. } {}
  232. foreach {tn q res} {
  233. 1 {a} {0 2}
  234. 2 {b} {3 1}
  235. 3 {c} {1 0}
  236. 4 {d} {2 3}
  237. 5 {g AND (e OR f)} {5 4}
  238. 6 {j AND (h OR i)} {5 6}
  239. } {
  240. do_execsql_test 8.2.$tn.1 {
  241. SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY bm25(t8);
  242. } $res
  243. do_execsql_test 8.2.$tn.2 {
  244. SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY +rank;
  245. } $res
  246. do_execsql_test 8.2.$tn.3 {
  247. SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY rank;
  248. } $res
  249. }
  250. #-------------------------------------------------------------------------
  251. # Test xPhraseCount() for some different queries.
  252. #
  253. do_test 9.1 {
  254. execsql { CREATE VIRTUAL TABLE t9 USING fts5(x) }
  255. foreach x {
  256. "a b c" "d e f"
  257. } {
  258. execsql { INSERT INTO t9 VALUES($x) }
  259. }
  260. } {}
  261. foreach {tn q cnt} {
  262. 1 {a AND b} 2
  263. 2 {a OR b} 2
  264. 3 {a OR b OR c} 3
  265. 4 {NEAR(a b)} 2
  266. } {
  267. do_execsql_test 9.2.$tn {
  268. SELECT fts5_test_phrasecount(t9) FROM t9 WHERE t9 MATCH $q LIMIT 1
  269. } $cnt
  270. }
  271. }
  272. finish_test