summaryrefslogtreecommitdiffstats
path: root/ext/fts5/test/fts5contentless.test
blob: 48cfd10ffa74ed5a8e99c08f26d69d24d926d258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# 2014 Dec 20
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests for the content= and content_rowid= options.
#

source [file join [file dirname [info script]] fts5_common.tcl]
set testprefix fts5contentless

# If SQLITE_ENABLE_FTS5 is defined, omit this file.
ifcapable !fts5 {
  finish_test
  return
}

# Check that it is not possible to specify "contentless_delete=1" for 
# anything other than a contentless table.
#
set res(0) {0 {}}
set res(1) {1 {contentless_delete=1 requires a contentless table}}
foreach {tn sql bError} {
  1 "(a, b, contentless_delete=1)"              1
  2 "(a, b, contentless_delete=1, content=abc)" 1
  3 "(a, b, contentless_delete=1, content=)"    0
  4 "(content=, contentless_delete=1, a)"       0
  5 "(content='', contentless_delete=1, hello)" 0
} {
  execsql { BEGIN }
  do_catchsql_test 1.$tn "CREATE VIRTUAL TABLE t1 USING fts5 $sql" $res($bError)
  execsql { ROLLBACK }
}

# Check that it is not possible to specify "contentless_delete=1" 
# along with columnsize=1. 
#
set res(0) {0 {}}
set res(1) {1 {contentless_delete=1 is incompatible with columnsize=0}}
foreach {tn sql bError} {
  2 "(a, b, content='', contentless_delete=1, columnsize=0)" 1 
} {
  execsql { BEGIN }
  do_catchsql_test 1.$tn "CREATE VIRTUAL TABLE t1 USING fts5 $sql" $res($bError)
  execsql { ROLLBACK }
}

# Check that if contentless_delete=1 is specified, then the "origin" 
# column is added to the %_docsize table.
reset_db
do_execsql_test 3.0 {
  CREATE VIRTUAL TABLE x1 USING fts5(c, content='');
  CREATE VIRTUAL TABLE x2 USING fts5(c, content='', contentless_delete=1);
}
do_execsql_test 3.1 {
  SELECT sql FROM sqlite_schema WHERE name IN ('x1_docsize', 'x2_docsize');
} {
  {CREATE TABLE 'x1_docsize'(id INTEGER PRIMARY KEY, sz BLOB)} 
  {CREATE TABLE 'x2_docsize'(id INTEGER PRIMARY KEY, sz BLOB, origin INTEGER)}
}

do_execsql_test 3.2.1 {
  SELECT hex(block) FROM x1_data WHERE id=10
} {00000000000000}
do_execsql_test 3.2.2 {
  SELECT hex(block) FROM x2_data WHERE id=10
} {00000000FF000001000000}

do_execsql_test 3.3 {
  INSERT INTO x2 VALUES('first text');
  INSERT INTO x2 VALUES('second text');
}
do_execsql_test 3.4 {
  SELECT id, origin FROM x2_docsize
} {1 1 2 2}
do_execsql_test 3.5 {
  SELECT level, segment, loc1, loc2 FROM fts5_structure(
    (SELECT block FROM x2_data WHERE id=10)
  )
} {
  0 0   1 1
  0 1   2 2
}
do_execsql_test 3.6 {
  INSERT INTO x2(x2) VALUES('optimize');
}
do_execsql_test 3.7 {
  SELECT level, segment, loc1, loc2 FROM fts5_structure(
    (SELECT block FROM x2_data WHERE id=10)
  )
} {
  1 0   1 2
}

do_execsql_test 3.8 {
  DELETE FROM x2 WHERE rowid=2;
}

do_execsql_test 3.9 {
  SELECT rowid FROM x2('text')
} {1}

#--------------------------------------------------------------------------
reset_db
proc document {n} {
  set vocab [list A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
  set ret [list]
  for {set ii 0} {$ii < $n} {incr ii} {
    lappend ret [lindex $vocab [expr int(rand()*[llength $vocab])]]
  }
  set ret
}

set nRow 1000

do_execsql_test 4.0 {
  CREATE TABLE t1(x);
  CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1);
  INSERT INTO ft(ft, rank) VALUES('pgsz', 100);
}
do_test 4.1 {
  for {set ii 0} {$ii < $nRow} {incr ii} {
    set doc [document 6]
    execsql {
      INSERT INTO t1 VALUES($doc);
      INSERT INTO ft VALUES($doc);
    }
  }
} {}

foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} {
  set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}]
  set L2 [execsql {SELECT rowid FROM ft($v)}]
  do_test 4.2.$v { set L1 } $L2
}

do_test 4.3 {
  for {set ii 1} {$ii < $nRow} {incr ii 2} { 
    execsql {
       DELETE FROM ft WHERE rowid=$ii;
       DELETE FROM t1 WHERE rowid=$ii;
    }
  }
} {}

foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} {
  set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}]
  set L2 [execsql {SELECT rowid FROM ft($v)}]
  do_test 4.4.$v { set L1 } $L2
}

do_execsql_test 4.5 {
  INSERT INTO ft(ft) VALUES('optimize');
} {}

foreach v {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z} {
  set L1 [execsql {SELECT rowid FROM t1 WHERE x LIKE '%'||$v||'%'}]
  set L2 [execsql {SELECT rowid FROM ft($v)}]
  do_test 4.6.$v { set L1 } $L2
}

#execsql_pp { SELECT fts5_decode(id, block) FROM ft_data }

#-------------------------------------------------------------------------
reset_db
do_execsql_test 5.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1);
  INSERT INTO ft(rowid, x) VALUES(1, 'one two three');
  INSERT INTO ft(rowid, x) VALUES(2, 'one two four');
  INSERT INTO ft(rowid, x) VALUES(3, 'one two five');
  INSERT INTO ft(rowid, x) VALUES(4, 'one two seven');
  INSERT INTO ft(rowid, x) VALUES(5, 'one two eight');
}

do_execsql_test 5.1 {
  DELETE FROM ft WHERE rowid=2
}

do_execsql_test 5.2 {
  SELECT rowid FROM ft
} {1 3 4 5}

do_catchsql_test 5.3 {
  UPDATE ft SET x='four six' WHERE rowid=3
} {0 {}}

do_execsql_test 5.4 {
  SELECT rowid FROM ft('one');
} {1 4 5}

do_execsql_test 5.5 {
  REPLACE INTO ft(rowid, x) VALUES(3, 'four six');
  SELECT rowid FROM ft('one');
} {1 4 5}

do_execsql_test 5.6 {
  REPLACE INTO ft(rowid, x) VALUES(6, 'one two eleven');
  SELECT rowid FROM ft('one');
} {1 4 5 6}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 6.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1);
  INSERT INTO ft(rowid, x) VALUES(1, 'one two three');
  INSERT INTO ft(rowid, x) VALUES(2, 'one two four');
}

do_test 6.1 {
  db eval { SELECT rowid FROM ft('one two') } {
    if {$rowid==1} {
      db eval { INSERT INTO ft(rowid, x) VALUES(3, 'one two four') }
    }
  }
} {}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 7.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1);
}

set lRowid [list -450 0 1 2 42]

do_test 7.1 {
  execsql BEGIN
  foreach r $lRowid {
    execsql { INSERT INTO ft(rowid, x) VALUES($r, 'one one one'); }
  }
  execsql COMMIT
} {}

do_test 7.2 {
  execsql BEGIN
  foreach r $lRowid {
    execsql { REPLACE INTO ft(rowid, x) VALUES($r, 'two two two'); }
  }
  execsql COMMIT
} {}

do_execsql_test 7.3 { SELECT rowid FROM ft('one'); } {}
do_execsql_test 7.4 { SELECT rowid FROM ft('two'); } $lRowid

#-------------------------------------------------------------------------
reset_db
do_execsql_test 8.0 {
  CREATE VIRTUAL TABLE ft USING fts5(x, content='', contentless_delete=1);
  INSERT INTO ft VALUES('hello world');
  INSERT INTO ft VALUES('one two three');
}

do_catchsql_test 8.1 {
  INSERT INTO ft(ft, rowid, x) VALUES('delete', 1, 'hello world');
} {1 {'delete' may not be used with a contentless_delete=1 table}}

do_execsql_test 8.2 {
  BEGIN;
    INSERT INTO ft(rowid, x) VALUES(3, 'four four four');
    DELETE FROM ft WHERE rowid=3;
  COMMIT;
  SELECT rowid FROM ft('four');
} {}

finish_test