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');
94 do_execsql_test 2.2.2 {
95 ALTER TABLE t0 RENAME TO t1;
97 do_execsql_test 2.2.3 {
98 INSERT INTO vt0(vt0) VALUES('integrity-check');
100 do_execsql_test 2.2.4 {
101 INSERT INTO vt0(c0) VALUES ('abc');
104 do_execsql_test 2.2.5 {
105 INSERT INTO vt0(vt0) VALUES('integrity-check');
108 #-------------------------------------------------------------------------
110 do_execsql_test 3.0 {
111 CREATE VIRTUAL TABLE vt0 USING fts5(a);
112 PRAGMA reverse_unordered_selects = true;
113 INSERT INTO vt0 VALUES('365062398'), (0), (0);
114 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '38');
116 do_execsql_test 3.1 {
117 UPDATE vt0 SET a = 399905135; -- unexpected: database disk image is malformed
119 do_execsql_test 3.2 {
120 INSERT INTO vt0(vt0) VALUES('integrity-check');
123 #-------------------------------------------------------------------------
125 do_execsql_test 4.0 {
126 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
127 INSERT INTO vt0(c0) VALUES ('xyz');
130 do_execsql_test 4.1 {
132 INSERT INTO vt0(c0) VALUES ('abc');
133 INSERT INTO vt0(vt0) VALUES('rebuild');
137 do_execsql_test 4.2 {
138 INSERT INTO vt0(vt0) VALUES('integrity-check');
141 do_execsql_test 4.3 {
143 INSERT INTO vt0(vt0) VALUES('rebuild');
144 INSERT INTO vt0(vt0) VALUES('rebuild');
148 do_execsql_test 4.4 {
149 INSERT INTO vt0(vt0) VALUES('integrity-check');
152 #-------------------------------------------------------------------------
156 do_execsql_test 5.0 {
157 CREATE VIRTUAL TABLE vt0 USING fts5(c0, c1);
158 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '65536');
160 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1236
162 INSERT INTO vt0(c0) SELECT '0' FROM s;
165 do_execsql_test 5.1 {
166 UPDATE vt0 SET c1 = 'T,D&p^y/7#3*v<b<4j7|f';
169 do_execsql_test 5.2 {
170 INSERT INTO vt0(vt0) VALUES('integrity-check');
173 do_catchsql_test 5.3 {
174 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '65537');
175 } {1 {SQL logic error}}
177 #-------------------------------------------------------------------------
181 do_execsql_test 6.0 {
182 CREATE VIRTUAL TABLE vt0 USING fts5(c0);
184 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
186 INSERT INTO vt0(c0) SELECT '0' FROM s;
187 INSERT INTO vt0(vt0, rank) VALUES('crisismerge', 2000);
188 INSERT INTO vt0(vt0, rank) VALUES('automerge', 0);
191 do_execsql_test 6.1 {
192 INSERT INTO vt0(vt0) VALUES('rebuild');
195 #-------------------------------------------------------------------------
198 do_execsql_test 7.0 {
199 CREATE VIRTUAL TABLE t1 USING fts5(x);
200 INSERT INTO t1(rowid, x) VALUES(1, 'hello world');
201 INSERT INTO t1(rowid, x) VALUES(2, 'well said');
202 INSERT INTO t1(rowid, x) VALUES(3, 'hello said');
203 INSERT INTO t1(rowid, x) VALUES(4, 'well world');
205 CREATE TABLE t2 (a, b);
206 INSERT INTO t2 VALUES(1, 'hello');
207 INSERT INTO t2 VALUES(2, 'world');
208 INSERT INTO t2 VALUES(3, 'said');
209 INSERT INTO t2 VALUES(4, 'hello');
212 do_execsql_test 7.1 {
213 SELECT rowid FROM t1 WHERE (rowid, x) IN (SELECT a, b FROM t2);
216 do_execsql_test 7.2 {
217 SELECT rowid FROM t1 WHERE rowid=2 AND t1 = 'hello';
220 #-------------------------------------------------------------------------
223 do_execsql_test 8.0 {
224 CREATE VIRTUAL TABLE vt0 USING fts5(c0, tokenize = "ascii", prefix = 1);
225 INSERT INTO vt0(c0) VALUES (x'd1');
228 do_execsql_test 8.1 {
229 INSERT INTO vt0(vt0) VALUES('integrity-check');
232 #-------------------------------------------------------------------------
235 do_execsql_test 9.0 {
236 CREATE VIRTUAL TABLE t1 using FTS5(mailcontent);
237 insert into t1(rowid, mailcontent) values
238 (-4764623217061966105, 'we are going to upgrade'),
239 (8324454597464624651, 'we are going to upgrade');
242 do_execsql_test 9.1 {
243 INSERT INTO t1(t1) VALUES('integrity-check');
246 do_execsql_test 9.2 {
247 SELECT rowid FROM t1('upgrade');
249 -4764623217061966105 8324454597464624651
252 #-------------------------------------------------------------------------
255 do_execsql_test 10.0 {
256 CREATE VIRTUAL TABLE vt1 USING fts5(c1, c2, prefix = 1, tokenize = "ascii");
257 INSERT INTO vt1 VALUES (x'e4', '䔬');
260 do_execsql_test 10.1 {
261 SELECT quote(CAST(c1 AS blob)), quote(CAST(c2 AS blob)) FROM vt1
264 do_execsql_test 10.2 {
265 INSERT INTO vt1(vt1) VALUES('integrity-check');
268 #-------------------------------------------------------------------------
271 do_execsql_test 11.0 {
272 CREATE VIRTUAL TABLE vt0 USING fts5(
273 c0, prefix = 71, tokenize = "porter ascii", prefix = 9
276 do_execsql_test 11.1 {
278 INSERT INTO vt0(c0) VALUES (x'e8');
280 do_execsql_test 11.2 {
281 INSERT INTO vt0(vt0) VALUES('integrity-check');
284 #-------------------------------------------------------------------------
288 do_execsql_test 11.0 {
289 PRAGMA encoding = 'UTF-16';
290 CREATE VIRTUAL TABLE vt0 USING fts5(c0, c1);
291 INSERT INTO vt0(vt0, rank) VALUES('pgsz', '37');
292 INSERT INTO vt0(c0, c1) VALUES (0.66077, 1957391816);
294 do_execsql_test 11.1 {
295 INSERT INTO vt0(vt0) VALUES('integrity-check');
298 #-------------------------------------------------------------------------
301 do_execsql_test 12.0 {
302 CREATE TABLE t1(a, b, rank);
303 INSERT INTO t1 VALUES('a', 'hello', '');
304 INSERT INTO t1 VALUES('b', 'world', '');
306 CREATE VIRTUAL TABLE ft USING fts5(a);
307 INSERT INTO ft VALUES('b');
308 INSERT INTO ft VALUES('y');
310 CREATE TABLE t2(x, y, ft);
311 INSERT INTO t2 VALUES(1, 2, 'x');
312 INSERT INTO t2 VALUES(3, 4, 'b');
315 do_execsql_test 12.1 {
316 SELECT * FROM t1 NATURAL JOIN ft WHERE ft MATCH('b')
318 do_execsql_test 12.2 {
319 SELECT * FROM ft NATURAL JOIN t1 WHERE ft MATCH('b')
321 do_execsql_test 12.3 {
322 SELECT * FROM t2 JOIN ft USING (ft)
325 #-------------------------------------------------------------------------
326 # Forum post https://sqlite.org/forum/forumpost/21127c1160
329 sqlite3_db_config db DEFENSIVE 1
331 do_execsql_test 13.1.0 {
332 CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
333 CREATE VIRTUAL TABLE b USING fts5(name);
334 CREATE TRIGGER a_trigger AFTER INSERT ON a BEGIN
335 INSERT INTO b (name) VALUES ('foo');
341 sqlite3_prepare db "INSERT INTO a VALUES (1, 'foo') RETURNING id;" -1 dummy
347 sqlite3_finalize $::STMT
355 sqlite3_db_config db DEFENSIVE 1
356 do_execsql_test 13.2.0 {
358 CREATE TABLE a (id INTEGER PRIMARY KEY, name TEXT);
359 CREATE VIRTUAL TABLE b USING fts5(name);
360 CREATE TRIGGER a_trigger AFTER INSERT ON a BEGIN
361 INSERT INTO b (name) VALUES ('foo');
367 sqlite3_prepare db "INSERT INTO a VALUES (1, 'foo') RETURNING id;" -1 dummy
373 sqlite3_finalize $::STMT
380 #-------------------------------------------------------------------------
383 sqlite3 db test.db -uri 1
385 do_execsql_test 14.0 {
386 PRAGMA locking_mode=EXCLUSIVE;
388 ATTACH 'file:/one?vfs=memdb' AS aux1;
389 ATTACH 'file:/one?vfs=memdb' AS aux2;
390 CREATE VIRTUAL TABLE t1 USING fts5(x);
392 do_catchsql_test 14.1 {
394 } {1 {database is locked}}
395 do_catchsql_test 14.2 {
397 } {1 {database is locked}}
398 do_catchsql_test 14.3 {
400 } {1 {database is locked}}
401 do_catchsql_test 14.4 {
405 #-------------------------------------------------------------------------
409 do_execsql_test 15.0 {
410 CREATE TABLE t1(a, b);
415 do_execsql_test -db db2 15.1 {
417 CREATE VIRTUAL TABLE x1 USING fts5(y);
420 list [catch { db2 eval COMMIT } msg] $msg
421 } {1 {database is locked}}
422 do_execsql_test -db db2 15.3 {
425 do_execsql_test 15.4 END
427 list [catch { db2 eval COMMIT } msg] $msg
432 #-------------------------------------------------------------------------
436 do_execsql_test 16.0 {
438 ATTACH 'test.db2' AS aux;
439 CREATE TABLE aux.t2(x,y);
440 INSERT INTO t2 VALUES(1, 2);
441 CREATE VIRTUAL TABLE x1 USING fts5(a);
443 INSERT INTO x1 VALUES('abc');
444 INSERT INTO t2 VALUES(3, 4);
447 do_execsql_test -db db2 16.1 {
448 ATTACH 'test.db2' AS aux;
453 do_catchsql_test 16.2 {
455 } {1 {database is locked}}
457 do_execsql_test 16.3 {
458 INSERT INTO x1 VALUES('def');
461 do_execsql_test -db db2 16.4 {
465 do_execsql_test 16.5 {
469 do_execsql_test -db db2 16.6 {
475 #-------------------------------------------------------------------------
477 do_execsql_test 17.1 {
478 CREATE VIRTUAL TABLE ft USING fts5(x, tokenize="unicode61 separators 'X'");
480 do_execsql_test 17.2 {
481 SELECT 0 FROM ft WHERE ft MATCH 'X' AND ft MATCH 'X'
483 do_execsql_test 17.3 {
484 SELECT 0 FROM ft('X')
487 do_execsql_test 17.4 {
488 CREATE VIRTUAL TABLE t0 USING fts5(c0, t="trigram");
489 INSERT INTO t0 VALUES('assertionfaultproblem');
491 do_execsql_test 17.5 {
492 SELECT 0 FROM t0(0) WHERE c0 GLOB 0;
495 do_execsql_test 17.5 {
496 SELECT c0 FROM t0 WHERE c0 GLOB '*f*';
497 } {assertionfaultproblem}
498 do_execsql_test 17.5 {
499 SELECT c0 FROM t0 WHERE c0 GLOB '*faul*';
500 } {assertionfaultproblem}
502 #-------------------------------------------------------------------------
504 do_execsql_test 18.0 {
506 CREATE VIRTUAL TABLE t1 USING fts5(text);
507 ALTER TABLE t1 RENAME TO t2;
510 do_execsql_test 18.1 {
514 do_execsql_test 18.2 {
518 #-------------------------------------------------------------------------
520 do_execsql_test 19.0 {
521 CREATE VIRTUAL TABLE t1 USING fts5(text);
522 CREATE TABLE t2(text);
524 INSERT INTO t1 VALUES('one');
525 INSERT INTO t1 VALUES('two');
526 INSERT INTO t1 VALUES('three');
527 INSERT INTO t1 VALUES('one');
528 INSERT INTO t1 VALUES('two');
529 INSERT INTO t1 VALUES('three');
531 INSERT INTO t2 VALUES('one');
532 INSERT INTO t2 VALUES('two');
533 INSERT INTO t2 VALUES('three');