3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is testing the FTS5 module.
15 source [file join [file dirname [info script]] fts5_common.tcl]
16 set testprefix fts5misc
18 # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
25 CREATE VIRTUAL TABLE t1 USING fts5(a);
28 do_catchsql_test 1.1.1 {
29 SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*');
30 } {1 {unknown special query: }}
31 do_catchsql_test 1.1.2 {
33 WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*'));
34 } {1 {unknown special query: }}
36 do_catchsql_test 1.2.1 {
37 SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*id');
40 do_catchsql_test 1.2.2 {
42 WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*id'));
45 do_catchsql_test 1.3.1 {
46 SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads');
47 } {1 {no such cursor: 2}}
49 do_catchsql_test 1.3.2 {
51 WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads'));
52 } {1 {no such cursor: 2}}
57 do_catchsql_test 1.3.3 {
59 WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads'));
60 } {1 {no such cursor: 1}}
62 #-------------------------------------------------------------------------
66 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
68 do_execsql_test 2.1.1 {
70 INSERT INTO vt0(c0) VALUES ('xyz');
72 do_execsql_test 2.1.2 {
73 ALTER TABLE t0 ADD COLUMN c5;
75 do_execsql_test 2.1.3 {
76 INSERT INTO vt0(vt0) VALUES('integrity-check');
78 do_execsql_test 2.1.4 {
79 INSERT INTO vt0(c0) VALUES ('abc');
82 do_execsql_test 2.1.5 {
83 INSERT INTO vt0(vt0) VALUES('integrity-check');
87 do_execsql_test 2.2.1 {
89 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
91 INSERT INTO vt0(c0) VALUES ('xyz');
95 do_execsql_test 2.2.2 {
96 ALTER TABLE t0 RENAME TO t1;
98 do_execsql_test 2.2.3 {
99 INSERT INTO vt0(vt0) VALUES('integrity-check');
101 do_execsql_test 2.2.4 {
102 INSERT INTO vt0(c0) VALUES ('abc');
105 do_execsql_test 2.2.5 {
106 INSERT INTO vt0(vt0) VALUES('integrity-check');
109 #-------------------------------------------------------------------------
111 do_execsql_test 3.0 {
112 CREATE VIRTUAL TABLE vt0 USING fts5(a);
113 PRAGMA reverse_unordered_selects = true;
114 INSERT INTO vt0 VALUES('365062398'), (0), (0);
115 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '38');
117 do_execsql_test 3.1 {
118 UPDATE vt0 SET a = 399905135; -- unexpected: database disk image is malformed
120 do_execsql_test 3.2 {
121 INSERT INTO vt0(vt0) VALUES('integrity-check');
124 #-------------------------------------------------------------------------
126 do_execsql_test 4.0 {
127 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
128 INSERT INTO vt0(c0) VALUES ('xyz');
131 do_execsql_test 4.1 {
133 INSERT INTO vt0(c0) VALUES ('abc');
134 INSERT INTO vt0(vt0) VALUES('rebuild');
138 do_execsql_test 4.2 {
139 INSERT INTO vt0(vt0) VALUES('integrity-check');
142 do_execsql_test 4.3 {
144 INSERT INTO vt0(vt0) VALUES('rebuild');
145 INSERT INTO vt0(vt0) VALUES('rebuild');
149 do_execsql_test 4.4 {
150 INSERT INTO vt0(vt0) VALUES('integrity-check');
153 #-------------------------------------------------------------------------
157 do_execsql_test 5.0 {
158 CREATE VIRTUAL TABLE vt0 USING fts5(c0, c1);
159 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '65536');
161 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1236
163 INSERT INTO vt0(c0) SELECT '0' FROM s;
166 do_execsql_test 5.1 {
167 UPDATE vt0 SET c1 = 'T,D&p^y/7#3*v<b<4j7|f';
170 do_execsql_test 5.2 {
171 INSERT INTO vt0(vt0) VALUES('integrity-check');
174 do_catchsql_test 5.3 {
175 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '65537');
176 } {1 {SQL logic error}}
178 #-------------------------------------------------------------------------
182 do_execsql_test 6.0 {
183 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
185 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
187 INSERT INTO vt0(c0) SELECT '0' FROM s;
188 INSERT INTO vt0(vt0, rank) VALUES('crisismerge', 2000);
189 INSERT INTO vt0(vt0, rank) VALUES('automerge', 0);
192 do_execsql_test 6.1 {
193 INSERT INTO vt0(vt0) VALUES('rebuild');
196 #-------------------------------------------------------------------------
199 do_execsql_test 7.0 {
200 CREATE VIRTUAL TABLE t1 USING fts5(x);
201 INSERT INTO t1(rowid, x) VALUES(1, 'hello world');
202 INSERT INTO t1(rowid, x) VALUES(2, 'well said');
203 INSERT INTO t1(rowid, x) VALUES(3, 'hello said');
204 INSERT INTO t1(rowid, x) VALUES(4, 'well world');
206 CREATE TABLE t2 (a, b);
207 INSERT INTO t2 VALUES(1, 'hello');
208 INSERT INTO t2 VALUES(2, 'world');
209 INSERT INTO t2 VALUES(3, 'said');
210 INSERT INTO t2 VALUES(4, 'hello');
213 do_execsql_test 7.1 {
214 SELECT rowid FROM t1 WHERE (rowid, x) IN (SELECT a, b FROM t2);
217 do_execsql_test 7.2 {
218 SELECT rowid FROM t1 WHERE rowid=2 AND t1 = 'hello';
221 #-------------------------------------------------------------------------
224 do_execsql_test 8.0 {
225 CREATE VIRTUAL TABLE vt0 USING fts5(c0, tokenize = "ascii", prefix = 1);
226 INSERT INTO vt0(c0) VALUES (x'd1');
229 do_execsql_test 8.1 {
230 INSERT INTO vt0(vt0) VALUES('integrity-check');
233 #-------------------------------------------------------------------------
236 do_execsql_test 9.0 {
237 CREATE VIRTUAL TABLE t1 using FTS5(mailcontent);
238 insert into t1(rowid, mailcontent) values
239 (-4764623217061966105, 'we are going to upgrade'),
240 (8324454597464624651, 'we are going to upgrade');
243 do_execsql_test 9.1 {
244 INSERT INTO t1(t1) VALUES('integrity-check');
247 do_execsql_test 9.2 {
248 SELECT rowid FROM t1('upgrade');
250 -4764623217061966105 8324454597464624651
253 #-------------------------------------------------------------------------
256 do_execsql_test 10.0 {
257 CREATE VIRTUAL TABLE vt1 USING fts5(c1, c2, prefix = 1, tokenize = "ascii");
258 INSERT INTO vt1 VALUES (x'e4', '䔬');
261 do_execsql_test 10.1 {
262 SELECT quote(CAST(c1 AS blob)), quote(CAST(c2 AS blob)) FROM vt1
265 do_execsql_test 10.2 {
266 INSERT INTO vt1(vt1) VALUES('integrity-check');
269 #-------------------------------------------------------------------------
272 do_execsql_test 11.0 {
273 CREATE VIRTUAL TABLE vt0 USING fts5(
274 c0, prefix = 71, tokenize = "porter ascii", prefix = 9
277 do_execsql_test 11.1 {
279 INSERT INTO vt0(c0) VALUES (x'e8');
281 do_execsql_test 11.2 {
282 INSERT INTO vt0(vt0) VALUES('integrity-check');
285 #-------------------------------------------------------------------------
289 do_execsql_test 11.0 {
290 PRAGMA encoding = 'UTF-16';
291 CREATE VIRTUAL TABLE vt0 USING fts5(c0, c1);
292 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '37');
293 INSERT INTO vt0(c0, c1) VALUES (0.66077, 1957391816);
295 do_execsql_test 11.1 {
296 INSERT INTO vt0(vt0) VALUES('integrity-check');
299 #-------------------------------------------------------------------------
302 do_execsql_test 12.0 {
303 CREATE TABLE t1(a, b, rank);
304 INSERT INTO t1 VALUES('a', 'hello', '');
305 INSERT INTO t1 VALUES('b', 'world', '');
307 CREATE VIRTUAL TABLE ft USING fts5(a);
308 INSERT INTO ft VALUES('b');
309 INSERT INTO ft VALUES('y');
311 CREATE TABLE t2(x, y, ft);
312 INSERT INTO t2 VALUES(1, 2, 'x');
313 INSERT INTO t2 VALUES(3, 4, 'b');
316 do_execsql_test 12.1 {
317 SELECT * FROM t1 NATURAL JOIN ft WHERE ft MATCH('b')
319 do_execsql_test 12.2 {
320 SELECT * FROM ft NATURAL JOIN t1 WHERE ft MATCH('b')
322 do_execsql_test 12.3 {
323 SELECT * FROM t2 JOIN ft USING (ft)
326 #-------------------------------------------------------------------------
327 # Forum post https://sqlite.org/forum/forumpost/21127c1160
330 sqlite3_db_config db DEFENSIVE 1
332 do_execsql_test 13.1.0 {
333 CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
334 CREATE VIRTUAL TABLE b USING fts5(name);
335 CREATE TRIGGER a_trigger AFTER INSERT ON a BEGIN
336 INSERT INTO b (name) VALUES ('foo');
342 sqlite3_prepare db "INSERT INTO a VALUES (1, 'foo') RETURNING id;" -1 dummy
348 sqlite3_finalize $::STMT
356 sqlite3_db_config db DEFENSIVE 1
357 do_execsql_test 13.2.0 {
359 CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
360 CREATE VIRTUAL TABLE b USING fts5(name);
361 CREATE TRIGGER a_trigger AFTER INSERT ON a BEGIN
362 INSERT INTO b (name) VALUES ('foo');
368 sqlite3_prepare db "INSERT INTO a VALUES (1, 'foo') RETURNING id;" -1 dummy
374 sqlite3_finalize $::STMT
381 #-------------------------------------------------------------------------
384 sqlite3 db test.db -uri 1
386 do_execsql_test 14.0 {
387 PRAGMA locking_mode=EXCLUSIVE;
389 ATTACH 'file:/one?vfs=memdb' AS aux1;
390 ATTACH 'file:/one?vfs=memdb' AS aux2;
391 CREATE VIRTUAL TABLE t1 USING fts5(x);
393 do_catchsql_test 14.1 {
395 } {1 {database is locked}}
396 do_catchsql_test 14.2 {
398 } {1 {database is locked}}
399 do_catchsql_test 14.3 {
401 } {1 {database is locked}}
402 do_catchsql_test 14.4 {
406 #-------------------------------------------------------------------------
410 do_execsql_test 15.0 {
411 CREATE TABLE t1(a, b);
416 do_execsql_test -db db2 15.1 {
418 CREATE VIRTUAL TABLE x1 USING fts5(y);
421 list [catch { db2 eval COMMIT } msg] $msg
422 } {1 {database is locked}}
423 do_execsql_test -db db2 15.3 {
426 do_execsql_test 15.4 END
428 list [catch { db2 eval COMMIT } msg] $msg
431 #-------------------------------------------------------------------------
435 do_execsql_test 16.0 {
437 ATTACH 'test.db2' AS aux;
438 CREATE TABLE aux.t2(x,y);
439 INSERT INTO t2 VALUES(1, 2);
440 CREATE VIRTUAL TABLE x1 USING fts5(a);
442 INSERT INTO x1 VALUES('abc');
443 INSERT INTO t2 VALUES(3, 4);
446 do_execsql_test -db db2 16.1 {
447 ATTACH 'test.db2' AS aux;
452 do_catchsql_test 16.2 {
454 } {1 {database is locked}}
456 do_execsql_test 16.3 {
457 INSERT INTO x1 VALUES('def');
460 do_execsql_test -db db2 16.4 {
464 do_execsql_test 16.5 {
468 do_execsql_test -db db2 16.6 {
472 #-------------------------------------------------------------------------
474 do_execsql_test 17.1 {
475 CREATE VIRTUAL TABLE ft USING fts5(x, tokenize="unicode61 separators 'X'");
477 do_execsql_test 17.2 {
478 SELECT 0 FROM ft WHERE ft MATCH 'X' AND ft MATCH 'X'
480 do_execsql_test 17.3 {
481 SELECT 0 FROM ft('X')
484 do_execsql_test 17.4 {
485 CREATE VIRTUAL TABLE t0 USING fts5(c0, t="trigram");
486 INSERT INTO t0 VALUES('assertionfaultproblem');
488 do_execsql_test 17.5 {
489 SELECT 0 FROM t0(0) WHERE c0 GLOB 0;
492 do_execsql_test 17.5 {
493 SELECT c0 FROM t0 WHERE c0 GLOB '*f*';
494 } {assertionfaultproblem}
495 do_execsql_test 17.5 {
496 SELECT c0 FROM t0 WHERE c0 GLOB '*faul*';
497 } {assertionfaultproblem}