fts5unicode2.test 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. # 2012 May 25
  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. # The tests in this file focus on testing the "unicode" FTS tokenizer.
  13. #
  14. # This is a modified copy of FTS4 test file "fts4_unicode.test".
  15. #
  16. source [file join [file dirname [info script]] fts5_common.tcl]
  17. set testprefix fts5unicode2
  18. # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
  19. ifcapable !fts5 {
  20. finish_test
  21. return
  22. }
  23. proc do_unicode_token_test {tn input res} {
  24. uplevel [list do_test $tn [list \
  25. sqlite3_fts5_tokenize -subst db "unicode61 remove_diacritics 0" $input
  26. ] [list {*}$res]]
  27. }
  28. proc do_unicode_token_test2 {tn input res} {
  29. uplevel [list do_test $tn [list \
  30. sqlite3_fts5_tokenize -subst db "unicode61" $input
  31. ] [list {*}$res]]
  32. }
  33. proc do_unicode_token_test3 {tn args} {
  34. set tokenizer [concat unicode61 {*}[lrange $args 0 end-2]]
  35. set input [lindex $args end-1]
  36. set res [lindex $args end]
  37. uplevel [list do_test $tn [list \
  38. sqlite3_fts5_tokenize -subst db $tokenizer $input
  39. ] [list {*}$res]]
  40. }
  41. do_unicode_token_test 1.0 {a B c D} {a a b B c c d D}
  42. do_unicode_token_test 1.1 "\uC4 \uD6 \uDC" \
  43. "\uE4 \uC4 \uF6 \uD6 \uFC \uDC"
  44. do_unicode_token_test 1.2 "x\uC4x x\uD6x x\uDCx" \
  45. "x\uE4x x\uC4x x\uF6x x\uD6x x\uFCx x\uDCx"
  46. # 0x00DF is a small "sharp s". 0x1E9E is a capital sharp s.
  47. do_unicode_token_test 1.3 "\uDF" "\uDF \uDF"
  48. do_unicode_token_test 1.4 "\u1E9E" "\uDF \u1E9E"
  49. do_unicode_token_test 1.5 "The quick brown fox" {
  50. the The quick quick brown brown fox fox
  51. }
  52. do_unicode_token_test 1.6 "The\u00bfquick\u224ebrown\u2263fox" {
  53. the The quick quick brown brown fox fox
  54. }
  55. do_unicode_token_test2 1.7 {a B c D} {a a b B c c d D}
  56. do_unicode_token_test2 1.8 "\uC4 \uD6 \uDC" "a \uC4 o \uD6 u \uDC"
  57. do_unicode_token_test2 1.9 "x\uC4x x\uD6x x\uDCx" \
  58. "xax x\uC4x xox x\uD6x xux x\uDCx"
  59. # Check that diacritics are removed if remove_diacritics=1 is specified.
  60. # And that they do not break tokens.
  61. do_unicode_token_test2 1.10 "xx\u0301xx" "xxxx xx\u301xx"
  62. # Title-case mappings work
  63. do_unicode_token_test 1.11 "\u01c5" "\u01c6 \u01c5"
  64. do_unicode_token_test 1.12 "\u00C1abc\u00C2 \u00D1def\u00C3" \
  65. "\u00E1abc\u00E2 \u00C1abc\u00C2 \u00F1def\u00E3 \u00D1def\u00C3"
  66. do_unicode_token_test 1.13 "\u00A2abc\u00A3 \u00A4def\u00A5" \
  67. "abc abc def def"
  68. #-------------------------------------------------------------------------
  69. #
  70. set docs [list {
  71. Enhance the INSERT syntax to allow multiple rows to be inserted via the
  72. VALUES clause.
  73. } {
  74. Enhance the CREATE VIRTUAL TABLE command to support the IF NOT EXISTS clause.
  75. } {
  76. Added the sqlite3_stricmp() interface as a counterpart to sqlite3_strnicmp().
  77. } {
  78. Added the sqlite3_db_readonly() interface.
  79. } {
  80. Added the SQLITE_FCNTL_PRAGMA file control, giving VFS implementations the
  81. ability to add new PRAGMA statements or to override built-in PRAGMAs.
  82. } {
  83. Queries of the form: "SELECT max(x), y FROM table" returns the value of y on
  84. the same row that contains the maximum x value.
  85. } {
  86. Added support for the FTS4 languageid option.
  87. } {
  88. Documented support for the FTS4 content option. This feature has actually
  89. been in the code since version 3.7.9 but is only now considered to be
  90. officially supported.
  91. } {
  92. Pending statements no longer block ROLLBACK. Instead, the pending statement
  93. will return SQLITE_ABORT upon next access after the ROLLBACK.
  94. } {
  95. Improvements to the handling of CSV inputs in the command-line shell
  96. } {
  97. Fix a bug introduced in version 3.7.10 that might cause a LEFT JOIN to be
  98. incorrectly converted into an INNER JOIN if the WHERE clause indexable terms
  99. connected by OR.
  100. }]
  101. unset -nocomplain map
  102. set map(a) [list "\u00C4" "\u00E4"] ; # LATIN LETTER A WITH DIAERESIS
  103. set map(e) [list "\u00CB" "\u00EB"] ; # LATIN LETTER E WITH DIAERESIS
  104. set map(i) [list "\u00CF" "\u00EF"] ; # LATIN LETTER I WITH DIAERESIS
  105. set map(o) [list "\u00D6" "\u00F6"] ; # LATIN LETTER O WITH DIAERESIS
  106. set map(u) [list "\u00DC" "\u00FC"] ; # LATIN LETTER U WITH DIAERESIS
  107. set map(y) [list "\u0178" "\u00FF"] ; # LATIN LETTER Y WITH DIAERESIS
  108. set map(h) [list "\u1E26" "\u1E27"] ; # LATIN LETTER H WITH DIAERESIS
  109. set map(w) [list "\u1E84" "\u1E85"] ; # LATIN LETTER W WITH DIAERESIS
  110. set map(x) [list "\u1E8C" "\u1E8D"] ; # LATIN LETTER X WITH DIAERESIS
  111. foreach k [array names map] {
  112. lappend mappings [string toupper $k] [lindex $map($k) 0]
  113. lappend mappings $k [lindex $map($k) 1]
  114. }
  115. proc mapdoc {doc} {
  116. set doc [regsub -all {[[:space:]]+} $doc " "]
  117. string map $::mappings [string trim $doc]
  118. }
  119. do_test 2.0 {
  120. execsql { CREATE VIRTUAL TABLE t2 USING fts5(tokenize=unicode61, x); }
  121. foreach doc $docs {
  122. set d [mapdoc $doc]
  123. execsql { INSERT INTO t2 VALUES($d) }
  124. }
  125. } {}
  126. do_test 2.1 {
  127. set q [mapdoc "row"]
  128. execsql { SELECT * FROM t2 WHERE t2 MATCH $q }
  129. } [list [mapdoc {
  130. Queries of the form: "SELECT max(x), y FROM table" returns the value of y on
  131. the same row that contains the maximum x value.
  132. }]]
  133. foreach {tn query snippet} {
  134. 2 "row" {
  135. ...returns the value of y on the same [row] that contains
  136. the maximum x value.
  137. }
  138. 3 "ROW" {
  139. ...returns the value of y on the same [row] that contains
  140. the maximum x value.
  141. }
  142. 4 "rollback" {
  143. Pending statements no longer block [ROLLBACK]. Instead, the pending
  144. statement will return SQLITE_ABORT upon...
  145. }
  146. 5 "rOllback" {
  147. Pending statements no longer block [ROLLBACK]. Instead, the pending
  148. statement will return SQLITE_ABORT upon...
  149. }
  150. 6 "lang*" {
  151. Added support for the FTS4 [languageid] option.
  152. }
  153. } {
  154. do_test 2.$tn {
  155. set q [mapdoc $query]
  156. execsql {
  157. SELECT snippet(t2, -1, '[', ']', '...', 15) FROM t2 WHERE t2 MATCH $q
  158. }
  159. } [list [mapdoc $snippet]]
  160. }
  161. #-------------------------------------------------------------------------
  162. # Make sure the unicode61 tokenizer does not crash if it is passed a
  163. # NULL pointer.
  164. reset_db
  165. do_execsql_test 3.1 {
  166. CREATE VIRTUAL TABLE t1 USING fts5(tokenize=unicode61, x, y);
  167. INSERT INTO t1 VALUES(NULL, 'a b c');
  168. }
  169. do_execsql_test 3.2 {
  170. SELECT snippet(t1, -1, '[', ']', '...', 15) FROM t1 WHERE t1 MATCH 'b'
  171. } {{a [b] c}}
  172. do_execsql_test 3.3 {
  173. BEGIN;
  174. DELETE FROM t1;
  175. INSERT INTO t1 VALUES('b b b b b b b b b b b', 'b b b b b b b b b b b b b');
  176. INSERT INTO t1 SELECT * FROM t1;
  177. INSERT INTO t1 SELECT * FROM t1;
  178. INSERT INTO t1 SELECT * FROM t1;
  179. INSERT INTO t1 SELECT * FROM t1;
  180. INSERT INTO t1 SELECT * FROM t1;
  181. INSERT INTO t1 SELECT * FROM t1;
  182. INSERT INTO t1 SELECT * FROM t1;
  183. INSERT INTO t1 SELECT * FROM t1;
  184. INSERT INTO t1 SELECT * FROM t1;
  185. INSERT INTO t1 SELECT * FROM t1;
  186. INSERT INTO t1 SELECT * FROM t1;
  187. INSERT INTO t1 SELECT * FROM t1;
  188. INSERT INTO t1 SELECT * FROM t1;
  189. INSERT INTO t1 SELECT * FROM t1;
  190. INSERT INTO t1 SELECT * FROM t1;
  191. INSERT INTO t1 SELECT * FROM t1;
  192. INSERT INTO t1 VALUES('a b c', NULL);
  193. INSERT INTO t1 VALUES('a x c', NULL);
  194. COMMIT;
  195. }
  196. do_execsql_test 3.4 {
  197. SELECT * FROM t1 WHERE t1 MATCH 'a b';
  198. } {{a b c} {}}
  199. #-------------------------------------------------------------------------
  200. #
  201. reset_db
  202. do_test 4.1 {
  203. set a "abc\uFFFEdef"
  204. set b "abc\uD800def"
  205. set c "\uFFFEdef"
  206. set d "\uD800def"
  207. execsql {
  208. CREATE VIRTUAL TABLE t1 USING fts5(tokenize=unicode61, x);
  209. INSERT INTO t1 VALUES($a);
  210. INSERT INTO t1 VALUES($b);
  211. INSERT INTO t1 VALUES($c);
  212. INSERT INTO t1 VALUES($d);
  213. }
  214. execsql "CREATE VIRTUAL TABLE t8 USING fts5(
  215. a, b, tokenize=\"unicode61 separators '\uFFFE\uD800\u00BF'\"
  216. )"
  217. } {}
  218. do_test 4.2 {
  219. set a [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0x62}]
  220. set b [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0x62}]
  221. set c [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0xBF 0x62}]
  222. set d [binary format c* {0x61 0xF7 0xBF 0xBF 0xBF 0xBF 0xBF 0xBF 0x62}]
  223. execsql {
  224. INSERT INTO t1 VALUES($a);
  225. INSERT INTO t1 VALUES($b);
  226. INSERT INTO t1 VALUES($c);
  227. INSERT INTO t1 VALUES($d);
  228. }
  229. } {}
  230. do_test 4.3 {
  231. set a [binary format c* {0xF7 0xBF 0xBF 0xBF}]
  232. set b [binary format c* {0xF7 0xBF 0xBF 0xBF 0xBF}]
  233. set c [binary format c* {0xF7 0xBF 0xBF 0xBF 0xBF 0xBF}]
  234. set d [binary format c* {0xF7 0xBF 0xBF 0xBF 0xBF 0xBF 0xBF}]
  235. execsql {
  236. INSERT INTO t1 VALUES($a);
  237. INSERT INTO t1 VALUES($b);
  238. INSERT INTO t1 VALUES($c);
  239. INSERT INTO t1 VALUES($d);
  240. }
  241. } {}
  242. do_test 4.4 {
  243. sqlite3_exec_hex db {
  244. CREATE VIRTUAL TABLE t9 USING fts5(a, b,
  245. tokenize="unicode61 separators '%C09004'"
  246. );
  247. INSERT INTO t9(a) VALUES('abc%88def %89ghi%90');
  248. }
  249. } {0 {}}
  250. #-------------------------------------------------------------------------
  251. do_unicode_token_test3 5.1 {tokenchars {}} {
  252. sqlite3_reset sqlite3_column_int
  253. } {
  254. sqlite3 sqlite3
  255. reset reset
  256. sqlite3 sqlite3
  257. column column
  258. int int
  259. }
  260. do_unicode_token_test3 5.2 {tokenchars _} {
  261. sqlite3_reset sqlite3_column_int
  262. } {
  263. sqlite3_reset sqlite3_reset
  264. sqlite3_column_int sqlite3_column_int
  265. }
  266. do_unicode_token_test3 5.3 {separators xyz} {
  267. Laotianxhorseyrunszfast
  268. } {
  269. laotian Laotian
  270. horse horse
  271. runs runs
  272. fast fast
  273. }
  274. do_unicode_token_test3 5.4 {tokenchars xyz} {
  275. Laotianxhorseyrunszfast
  276. } {
  277. laotianxhorseyrunszfast Laotianxhorseyrunszfast
  278. }
  279. do_unicode_token_test3 5.5 {tokenchars _} {separators zyx} {
  280. sqlite3_resetxsqlite3_column_intyhonda_phantom
  281. } {
  282. sqlite3_reset sqlite3_reset
  283. sqlite3_column_int sqlite3_column_int
  284. honda_phantom honda_phantom
  285. }
  286. do_unicode_token_test3 5.6 "separators \u05D1" "abc\u05D1def" {
  287. abc abc def def
  288. }
  289. do_unicode_token_test3 5.7 \
  290. "tokenchars \u2444\u2445" \
  291. "separators \u05D0\u05D1\u05D2" \
  292. "\u2444fre\u2445sh\u05D0water\u05D2fish.\u2445timer" \
  293. [list \
  294. \u2444fre\u2445sh \u2444fre\u2445sh \
  295. water water \
  296. fish fish \
  297. \u2445timer \u2445timer \
  298. ]
  299. # Check that it is not possible to add a standalone diacritic codepoint
  300. # to either separators or tokenchars.
  301. do_unicode_token_test3 5.8 "separators \u0301" \
  302. "hello\u0301world \u0301helloworld" \
  303. "helloworld hello\u0301world helloworld helloworld"
  304. do_unicode_token_test3 5.9 "tokenchars \u0301" \
  305. "hello\u0301world \u0301helloworld" \
  306. "helloworld hello\u0301world helloworld helloworld"
  307. do_unicode_token_test3 5.10 "separators \u0301" \
  308. "remove_diacritics 0" \
  309. "hello\u0301world \u0301helloworld" \
  310. "hello\u0301world hello\u0301world helloworld helloworld"
  311. do_unicode_token_test3 5.11 "tokenchars \u0301" \
  312. "remove_diacritics 0" \
  313. "hello\u0301world \u0301helloworld" \
  314. "hello\u0301world hello\u0301world helloworld helloworld"
  315. #-------------------------------------------------------------------------
  316. proc do_tokenize {tokenizer txt} {
  317. set res [list]
  318. foreach {b c} [sqlite3_fts5_tokenize -subst db $tokenizer $txt] {
  319. lappend res $b
  320. }
  321. set res
  322. }
  323. # Argument $lCodepoint must be a list of codepoints (integers) that
  324. # correspond to whitespace characters. This command creates a string
  325. # $W from the codepoints, then tokenizes "${W}hello{$W}world${W}"
  326. # using tokenizer $tokenizer. The test passes if the tokenizer successfully
  327. # extracts the two 5 character tokens.
  328. #
  329. proc do_isspace_test {tn tokenizer lCp} {
  330. set whitespace [format [string repeat %c [llength $lCp]] {*}$lCp]
  331. set txt "${whitespace}hello${whitespace}world${whitespace}"
  332. uplevel [list do_test $tn [list do_tokenize $tokenizer $txt] {hello world}]
  333. }
  334. set tokenizers [list unicode61]
  335. #ifcapable icu { lappend tokenizers icu }
  336. # Some tests to check that the tokenizers can both identify white-space
  337. # codepoints. All codepoints tested below are of type "Zs" in the
  338. # UnicodeData.txt file.
  339. foreach T $tokenizers {
  340. do_isspace_test 6.$T.1 $T 32
  341. do_isspace_test 6.$T.2 $T 160
  342. do_isspace_test 6.$T.3 $T 5760
  343. do_isspace_test 6.$T.4 $T 6158
  344. do_isspace_test 6.$T.5 $T 8192
  345. do_isspace_test 6.$T.6 $T 8193
  346. do_isspace_test 6.$T.7 $T 8194
  347. do_isspace_test 6.$T.8 $T 8195
  348. do_isspace_test 6.$T.9 $T 8196
  349. do_isspace_test 6.$T.10 $T 8197
  350. do_isspace_test 6.$T.11 $T 8198
  351. do_isspace_test 6.$T.12 $T 8199
  352. do_isspace_test 6.$T.13 $T 8200
  353. do_isspace_test 6.$T.14 $T 8201
  354. do_isspace_test 6.$T.15 $T 8202
  355. do_isspace_test 6.$T.16 $T 8239
  356. do_isspace_test 6.$T.17 $T 8287
  357. do_isspace_test 6.$T.18 $T 12288
  358. do_isspace_test 6.$T.19 $T {32 160 5760 6158}
  359. do_isspace_test 6.$T.20 $T {8192 8193 8194 8195}
  360. do_isspace_test 6.$T.21 $T {8196 8197 8198 8199}
  361. do_isspace_test 6.$T.22 $T {8200 8201 8202 8239}
  362. do_isspace_test 6.$T.23 $T {8287 12288}
  363. }
  364. #-------------------------------------------------------------------------
  365. # Test that the private use ranges are treated as alphanumeric.
  366. #
  367. foreach {tn1 c} {
  368. 1 \ue000 2 \ue001 3 \uf000 4 \uf8fe 5 \uf8ff
  369. } {
  370. foreach {tn2 config res} {
  371. 1 "" "hello*world hello*world"
  372. 2 "separators *" "hello hello world world"
  373. } {
  374. set config [string map [list * $c] $config]
  375. set input [string map [list * $c] "hello*world"]
  376. set output [string map [list * $c] $res]
  377. do_unicode_token_test3 7.$tn1.$tn2 {*}$config $input $output
  378. }
  379. }
  380. #-------------------------------------------------------------------------
  381. # Cursory test of remove_diacritics=0.
  382. #
  383. # 00C4;LATIN CAPITAL LETTER A WITH DIAERESIS
  384. # 00D6;LATIN CAPITAL LETTER O WITH DIAERESIS
  385. # 00E4;LATIN SMALL LETTER A WITH DIAERESIS
  386. # 00F6;LATIN SMALL LETTER O WITH DIAERESIS
  387. #
  388. do_execsql_test 8.1.1 "
  389. CREATE VIRTUAL TABLE t3 USING fts5(
  390. content, tokenize='unicode61 remove_diacritics 1'
  391. );
  392. INSERT INTO t3 VALUES('o');
  393. INSERT INTO t3 VALUES('a');
  394. INSERT INTO t3 VALUES('O');
  395. INSERT INTO t3 VALUES('A');
  396. INSERT INTO t3 VALUES('\xD6');
  397. INSERT INTO t3 VALUES('\xC4');
  398. INSERT INTO t3 VALUES('\xF6');
  399. INSERT INTO t3 VALUES('\xE4');
  400. "
  401. do_execsql_test 8.1.2 {
  402. SELECT rowid FROM t3 WHERE t3 MATCH 'o' ORDER BY rowid ASC;
  403. } {1 3 5 7}
  404. do_execsql_test 8.1.3 {
  405. SELECT rowid FROM t3 WHERE t3 MATCH 'a' ORDER BY rowid ASC;
  406. } {2 4 6 8}
  407. do_execsql_test 8.2.1 {
  408. CREATE VIRTUAL TABLE t4 USING fts5(
  409. content, tokenize='unicode61 remove_diacritics 0'
  410. );
  411. INSERT INTO t4 SELECT * FROM t3 ORDER BY rowid ASC;
  412. }
  413. do_execsql_test 8.2.2 {
  414. SELECT rowid FROM t4 WHERE t4 MATCH 'o' ORDER BY rowid ASC;
  415. } {1 3}
  416. do_execsql_test 8.2.3 {
  417. SELECT rowid FROM t4 WHERE t4 MATCH 'a' ORDER BY rowid ASC;
  418. } {2 4}
  419. #-------------------------------------------------------------------------
  420. foreach {tn val bErr} {
  421. 1 0 0
  422. 2 1 0
  423. 3 2 0
  424. 4 3 1
  425. 5 11 1
  426. } {
  427. reset_db
  428. set aRes(0) {0 {}}
  429. set aRes(1) {1 {error in tokenizer constructor}}
  430. set res $aRes($bErr)
  431. do_catchsql_test 9.1.$tn "
  432. CREATE VIRTUAL TABLE bl USING fts5(
  433. s, tokenize='trigram remove_diacritics $val'
  434. );
  435. " $res
  436. }
  437. finish_test