fts5content.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # 2014 Dec 20
  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. #
  12. # This file contains tests for the content= and content_rowid= options.
  13. #
  14. source [file join [file dirname [info script]] fts5_common.tcl]
  15. set testprefix fts5content
  16. # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
  17. ifcapable !fts5 {
  18. finish_test
  19. return
  20. }
  21. #-------------------------------------------------------------------------
  22. # Contentless tables
  23. #
  24. do_execsql_test 1.1 {
  25. CREATE VIRTUAL TABLE f1 USING fts5(a, b, content='');
  26. INSERT INTO f1(rowid, a, b) VALUES(1, 'one', 'o n e');
  27. INSERT INTO f1(rowid, a, b) VALUES(2, 'two', 't w o');
  28. INSERT INTO f1(rowid, a, b) VALUES(3, 'three', 't h r e e');
  29. }
  30. do_execsql_test 1.2 {
  31. SELECT rowid FROM f1 WHERE f1 MATCH 'o';
  32. } {1 2}
  33. do_execsql_test 1.3 {
  34. INSERT INTO f1(a, b) VALUES('four', 'f o u r');
  35. SELECT rowid FROM f1 WHERE f1 MATCH 'o';
  36. } {1 2 4}
  37. do_execsql_test 1.4 {
  38. SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o';
  39. } {1 {} {} 2 {} {} 4 {} {}}
  40. do_execsql_test 1.5 {
  41. SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o';
  42. } {1 {} 2 {} 4 {}}
  43. do_execsql_test 1.6 {
  44. SELECT rowid, highlight(f1, 0, '[', ']') IS NULL FROM f1 WHERE f1 MATCH 'o';
  45. } {1 1 2 1 4 1}
  46. do_execsql_test 1.7 {
  47. SELECT rowid, snippet(f1, -1, '[', ']', '...', 5) IS NULL
  48. FROM f1 WHERE f1 MATCH 'o';
  49. } {1 1 2 1 4 1}
  50. do_execsql_test 1.8 {
  51. SELECT rowid, snippet(f1, 1, '[', ']', '...', 5) IS NULL
  52. FROM f1 WHERE f1 MATCH 'o';
  53. } {1 1 2 1 4 1}
  54. do_execsql_test 1.9 {
  55. SELECT rowid FROM f1;
  56. } {1 2 3 4}
  57. do_execsql_test 1.10 {
  58. SELECT * FROM f1;
  59. } {{} {} {} {} {} {} {} {}}
  60. do_execsql_test 1.11 {
  61. SELECT rowid, a, b FROM f1 ORDER BY rowid ASC;
  62. } {1 {} {} 2 {} {} 3 {} {} 4 {} {}}
  63. do_execsql_test 1.12 {
  64. SELECT a IS NULL FROM f1;
  65. } {1 1 1 1}
  66. do_catchsql_test 1.13 {
  67. DELETE FROM f1 WHERE rowid = 2;
  68. } {1 {cannot DELETE from contentless fts5 table: f1}}
  69. do_catchsql_test 1.14 {
  70. UPDATE f1 SET a = 'a b c' WHERE rowid = 2;
  71. } {1 {cannot UPDATE contentless fts5 table: f1}}
  72. do_execsql_test 1.15 {
  73. INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o');
  74. } {}
  75. do_execsql_test 1.16 {
  76. SELECT rowid FROM f1 WHERE f1 MATCH 'o';
  77. } {1 4}
  78. do_execsql_test 1.17 {
  79. SELECT rowid FROM f1;
  80. } {1 3 4}
  81. #-------------------------------------------------------------------------
  82. # External content tables
  83. #
  84. reset_db
  85. do_execsql_test 2.1 {
  86. -- Create a table. And an external content fts5 table to index it.
  87. CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
  88. CREATE VIRTUAL TABLE fts_idx USING fts5(b, c, content='tbl', content_rowid='a');
  89. -- Triggers to keep the FTS index up to date.
  90. CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
  91. INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  92. END;
  93. CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
  94. INSERT INTO fts_idx(fts_idx, rowid, b, c)
  95. VALUES('delete', old.a, old.b, old.c);
  96. END;
  97. CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
  98. INSERT INTO fts_idx(fts_idx, rowid, b, c)
  99. VALUES('delete', old.a, old.b, old.c);
  100. INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
  101. END;
  102. }
  103. do_execsql_test 2.2 {
  104. INSERT INTO tbl VALUES(1, 'one', 'o n e');
  105. INSERT INTO tbl VALUES(NULL, 'two', 't w o');
  106. INSERT INTO tbl VALUES(3, 'three', 't h r e e');
  107. }
  108. do_execsql_test 2.3 {
  109. INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
  110. }
  111. do_execsql_test 2.4 {
  112. DELETE FROM tbl WHERE rowid=2;
  113. INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
  114. }
  115. do_execsql_test 2.5 {
  116. UPDATE tbl SET c = c || ' x y z';
  117. INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
  118. }
  119. do_execsql_test 2.6 {
  120. SELECT * FROM fts_idx WHERE fts_idx MATCH 't AND x';
  121. } {three {t h r e e x y z}}
  122. do_execsql_test 2.7 {
  123. SELECT highlight(fts_idx, 1, '[', ']') FROM fts_idx
  124. WHERE fts_idx MATCH 't AND x';
  125. } {{[t] h r e e [x] y z}}
  126. #-------------------------------------------------------------------------
  127. # Quick tests of the 'delete-all' command.
  128. #
  129. do_execsql_test 3.1 {
  130. CREATE VIRTUAL TABLE t3 USING fts5(x, content='');
  131. INSERT INTO t3 VALUES('a b c');
  132. INSERT INTO t3 VALUES('d e f');
  133. }
  134. do_execsql_test 3.2 {
  135. SELECT count(*) FROM t3_docsize;
  136. SELECT count(*) FROM t3_data;
  137. } {2 4}
  138. do_execsql_test 3.3 {
  139. INSERT INTO t3(t3) VALUES('delete-all');
  140. SELECT count(*) FROM t3_docsize;
  141. SELECT count(*) FROM t3_data;
  142. } {0 2}
  143. do_execsql_test 3.4 {
  144. INSERT INTO t3 VALUES('a b c');
  145. INSERT INTO t3 VALUES('d e f');
  146. SELECT rowid FROM t3 WHERE t3 MATCH 'e';
  147. } {2}
  148. do_execsql_test 3.5 {
  149. SELECT rowid FROM t3 WHERE t3 MATCH 'c';
  150. } {1}
  151. do_execsql_test 3.6 {
  152. SELECT count(*) FROM t3_docsize;
  153. SELECT count(*) FROM t3_data;
  154. } {2 4}
  155. do_execsql_test 3.7 {
  156. CREATE VIRTUAL TABLE t4 USING fts5(x);
  157. } {}
  158. do_catchsql_test 3.8 {
  159. INSERT INTO t4(t4) VALUES('delete-all');
  160. } {1 {'delete-all' may only be used with a contentless or external content fts5 table}}
  161. #-------------------------------------------------------------------------
  162. # Test an external content table with a more interesting schema.
  163. #
  164. do_execsql_test 4.1 {
  165. CREATE TABLE x2(a, "key col" PRIMARY KEY, b, c) WITHOUT ROWID;
  166. INSERT INTO x2 VALUES('a b', 1, 'c d' , 'e f');
  167. INSERT INTO x2 VALUES('x y', -40, 'z z' , 'y x');
  168. CREATE VIRTUAL TABLE t2 USING fts5(a, c, content=x2, content_rowid='key col');
  169. INSERT INTO t2(t2) VALUES('rebuild');
  170. }
  171. do_execsql_test 4.2 { SELECT rowid FROM t2 } {-40 1}
  172. do_execsql_test 4.3 { SELECT rowid FROM t2 WHERE t2 MATCH 'c'} {}
  173. do_execsql_test 4.4 { SELECT rowid FROM t2 WHERE t2 MATCH 'a'} {1}
  174. do_execsql_test 4.5 { SELECT rowid FROM t2 WHERE t2 MATCH 'x'} {-40}
  175. do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {}
  176. do_execsql_test 4.7 {
  177. DELETE FROM x2 WHERE "key col" = 1;
  178. INSERT INTO t2(t2, rowid, a, c) VALUES('delete', 1, 'a b', 'e f');
  179. INSERT INTO t2(t2) VALUES('integrity-check');
  180. }
  181. do_execsql_test 4.8 { SELECT rowid FROM t2 WHERE t2 MATCH 'b'} {}
  182. do_execsql_test 4.9 { SELECT rowid FROM t2 WHERE t2 MATCH 'y'} {-40}
  183. #-------------------------------------------------------------------------
  184. # Test that if the 'rowid' field of a 'delete' is not an integer, no
  185. # changes are made to the FTS index.
  186. #
  187. do_execsql_test 5.0 {
  188. CREATE VIRTUAL TABLE t5 USING fts5(a, b, content=);
  189. INSERT INTO t5(rowid, a, b) VALUES(-1, 'one', 'two');
  190. INSERT INTO t5(rowid, a, b) VALUES( 0, 'three', 'four');
  191. INSERT INTO t5(rowid, a, b) VALUES( 1, 'five', 'six');
  192. }
  193. set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}]
  194. do_execsql_test 5.1 {
  195. INSERT INTO t5(t5, rowid, a, b) VALUES('delete', NULL, 'three', 'four');
  196. SELECT md5sum(id, block) FROM t5_data;
  197. } $::checksum
  198. #-------------------------------------------------------------------------
  199. # Check that a contentless table can be dropped.
  200. #
  201. reset_db
  202. do_execsql_test 6.1 {
  203. CREATE VIRTUAL TABLE xx USING fts5(x, y, content="");
  204. SELECT name FROM sqlite_master;
  205. } {xx xx_data xx_idx xx_docsize xx_config}
  206. do_execsql_test 6.2 {
  207. DROP TABLE xx;
  208. SELECT name FROM sqlite_master;
  209. } {}
  210. #---------------------------------------------------------------------------
  211. # Check that an fts5 table cannot be its own content table.
  212. #
  213. reset_db
  214. do_execsql_test 7.1.1 {
  215. CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 );
  216. INSERT INTO t1( a ) VALUES('abc');
  217. }
  218. do_catchsql_test 7.1.2 {
  219. SELECT * FROM t1;
  220. } {1 {recursively defined fts5 content table}}
  221. do_catchsql_test 7.1.3 {
  222. SELECT * FROM t1('abc');
  223. } {1 {recursively defined fts5 content table}}
  224. do_catchsql_test 7.1.4 {
  225. SELECT count(*) FROM t1;
  226. } {1 {recursively defined fts5 content table}}
  227. do_catchsql_test 7.1.5 {
  228. SELECT * FROM t1('abc') ORDER BY rank;
  229. } {1 {recursively defined fts5 content table}}
  230. reset_db
  231. do_execsql_test 7.2.1 {
  232. CREATE VIRTUAL TABLE t1 USING fts5(a, c=t2 );
  233. CREATE VIRTUAL TABLE t2 USING fts5(a, c=t1 );
  234. INSERT INTO t1( a ) VALUES('abc');
  235. }
  236. do_catchsql_test 7.2.2 {
  237. SELECT * FROM t1;
  238. } {1 {recursively defined fts5 content table}}
  239. do_catchsql_test 7.2.3 {
  240. SELECT * FROM t1('abc');
  241. } {1 {recursively defined fts5 content table}}
  242. do_catchsql_test 7.2.4 {
  243. SELECT count(*) FROM t1;
  244. } {1 {recursively defined fts5 content table}}
  245. do_catchsql_test 7.2.5 {
  246. SELECT * FROM t1('abc') ORDER BY rank;
  247. } {1 {recursively defined fts5 content table}}
  248. #---------------------------------------------------------------------------
  249. # Check that if the content table is a view, and that view contains an
  250. # error, a reasonable error message is returned if the user tries to
  251. # read from the view via the fts5 table.
  252. #
  253. reset_db
  254. do_execsql_test 8.1 {
  255. CREATE VIEW a1 AS
  256. SELECT 1 AS r, text_value(1) AS t
  257. UNION ALL
  258. SELECT 2 AS r, text_value(2) AS t;
  259. CREATE VIRTUAL TABLE t1 USING fts5(t, content='a1', content_rowid='r');
  260. }
  261. foreach {tn sql} {
  262. 1 "SELECT * FROM t1"
  263. 2 "INSERT INTO t1(t1) VALUES('rebuild')"
  264. 3 "SELECT * FROM t1 WHERE rowid=1"
  265. } {
  266. do_catchsql_test 8.2.$tn $sql {1 {no such function: text_value}}
  267. }
  268. proc text_value {i} {
  269. if {$i==1} { return "one" }
  270. if {$i==2} { return "two" }
  271. return "many"
  272. }
  273. db func text_value text_value
  274. do_execsql_test 8.3.1 { SELECT * FROM t1 } {one two}
  275. do_execsql_test 8.3.2 { INSERT INTO t1(t1) VALUES('rebuild') }
  276. do_execsql_test 8.3.3 { SELECT * FROM t1 WHERE rowid=1 } {one}
  277. do_execsql_test 8.3.4 { SELECT rowid FROM t1('two') } {2}
  278. #-------------------------------------------------------------------------
  279. reset_db
  280. do_execsql_test 9.1 {
  281. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  282. INSERT INTO t1 VALUES(1, 'one two three');
  283. INSERT INTO t1 VALUES(2, 'one two three');
  284. CREATE VIRTUAL TABLE ft USING fts5(b, content=t1, content_rowid=a);
  285. INSERT INTO ft(ft) VALUES('rebuild');
  286. }
  287. do_execsql_test 9.2 {
  288. SELECT rowid, b FROM ft('two');
  289. } {
  290. 1 {one two three}
  291. 2 {one two three}
  292. }
  293. do_execsql_test 9.3 {
  294. DELETE FROM t1 WHERE a=2;
  295. }
  296. do_catchsql_test 9.4 {
  297. SELECT rowid FROM ft('two');
  298. } {0 {1 2}}
  299. do_catchsql_test 9.5 {
  300. SELECT * FROM ft('two');
  301. } {1 {fts5: missing row 2 from content table 'main'.'t1'}}
  302. fts5_aux_test_functions db
  303. do_catchsql_test 9.6 {
  304. SELECT rowid, fts5_columntext(ft, 0) FROM ft('two');
  305. } {1 SQLITE_CORRUPT_VTAB}
  306. finish_test