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 #***********************************************************************
13 if {![info exists testdir
]} {
14 set testdir
[file join [file dirname
[info script
]] .. .. .. test
]
16 source $testdir/tester.tcl
19 proc return_if_no_fts5
{} {
25 proc return_if_no_fts5
{} {}
29 sqlite3_fts5_may_be_corrupt
0
33 proc fts5_test_poslist
{cmd
} {
35 for {set i
0} {$i < [$cmd xInstCount
]} {incr i
} {
36 lappend res
[string map
{{ } .
} [$cmd xInst
$i]]
41 proc fts5_test_poslist2
{cmd
} {
44 for {set i
0} {$i < [$cmd xPhraseCount
]} {incr i
} {
45 $cmd xPhraseForeach
$i c o
{
54 proc fts5_test_collist
{cmd
} {
57 for {set i
0} {$i < [$cmd xPhraseCount
]} {incr i
} {
58 $cmd xPhraseColumnForeach
$i c
{ lappend res
$i.
$c }
64 proc fts5_test_columnsize
{cmd
} {
66 for {set i
0} {$i < [$cmd xColumnCount
]} {incr i
} {
67 lappend res
[$cmd xColumnSize
$i]
72 proc fts5_test_columntext
{cmd
} {
74 for {set i
0} {$i < [$cmd xColumnCount
]} {incr i
} {
75 lappend res
[$cmd xColumnText
$i]
80 proc fts5_test_columntotalsize
{cmd
} {
82 for {set i
0} {$i < [$cmd xColumnCount
]} {incr i
} {
83 lappend res
[$cmd xColumnTotalSize
$i]
88 proc test_append_token
{varname token iStart iEnd
} {
93 proc fts5_test_tokenize
{cmd
} {
95 for {set i
0} {$i < [$cmd xColumnCount
]} {incr i
} {
97 $cmd xTokenize
[$cmd xColumnText
$i] [list test_append_token tokens
]
103 proc fts5_test_rowcount
{cmd
} {
107 proc test_queryphrase_cb
{cnt cmd
} {
109 for {set i
0} {$i < [$cmd xInstCount
]} {incr i
} {
110 foreach {ip ic io
} [$cmd xInst
$i] break
113 foreach ic
[array names A
] {
114 lset L
$ic [expr {[lindex $L $ic] + 1}]
117 proc fts5_test_queryphrase
{cmd
} {
119 for {set i
0} {$i < [$cmd xPhraseCount
]} {incr i
} {
121 for {set j
0} {$j < [$cmd xColumnCount
]} {incr j
} { lappend cnt
0 }
122 $cmd xQueryPhrase
$i [list test_queryphrase_cb cnt
]
128 proc fts5_test_phrasecount
{cmd
} {
132 proc fts5_test_all
{cmd
} {
134 lappend res columnsize
[fts5_test_columnsize
$cmd]
135 lappend res columntext
[fts5_test_columntext
$cmd]
136 lappend res columntotalsize
[fts5_test_columntotalsize
$cmd]
137 lappend res poslist
[fts5_test_poslist
$cmd]
138 lappend res tokenize
[fts5_test_tokenize
$cmd]
139 lappend res rowcount
[fts5_test_rowcount
$cmd]
143 proc fts5_aux_test_functions
{db
} {
147 fts5_test_columntotalsize
155 fts5_test_queryphrase
156 fts5_test_phrasecount
158 sqlite3_fts5_create_function
$db $f $f
162 proc fts5_segcount
{tbl
} {
164 foreach n
[fts5_level_segs
$tbl] { incr N
$n }
168 proc fts5_level_segs
{tbl
} {
169 set sql
"SELECT fts5_decode(rowid,block) aS r FROM ${tbl}_data WHERE rowid=10"
171 foreach L
[lrange [db one
$sql] 1 end
] {
172 lappend ret
[expr [llength $L] - 3]
177 proc fts5_level_segids
{tbl
} {
178 set sql
"SELECT fts5_decode(rowid,block) aS r FROM ${tbl}_data WHERE rowid=10"
180 foreach L
[lrange [db one
$sql] 1 end
] {
182 foreach S
[lrange $L 3 end
] {
183 regexp {id
=([1234567890]*)} $S -> segid
191 proc fts5_rnddoc
{n
} {
192 set map
[list 0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
8 i
9 j
]
194 for {set i
0} {$i < $n} {incr i
} {
195 lappend doc
"x[string map $map [format %.3d [expr int(rand()*1000)]]]"
200 #-------------------------------------------------------------------------
203 # nearset aCol ?-pc VARNAME? ?-near N? ?-col C? -- phrase1 phrase2...
205 # This command is used to test if a document (set of column values) matches
206 # the logical equivalent of a single FTS5 NEAR() clump and, if so, return
207 # the equivalent of an FTS5 position list.
209 # Parameter $aCol is passed a list of the column values for the document
210 # to test. Parameters $phrase1 and so on are the phrases.
212 # The result is a list of phrase hits. Each phrase hit is formatted as
213 # three integers separated by "." characters, in the following format:
215 # <phrase number> . <column number> . <token offset>
219 # -near N (NEAR distance. Default 10)
220 # -col C (List of column indexes to match against)
221 # -pc VARNAME (variable in caller frame to use for phrase numbering)
222 # -dict VARNAME (array in caller frame to use for synonyms)
224 proc nearset
{aCol args
} {
226 # Process the command line options.
233 set nOpt
[lsearch -exact $args --]
234 if {$nOpt<0} { error "no -- option" }
236 # Set $lPhrase to be a list of phrases. $nPhrase its length.
237 set lPhrase
[lrange $args [expr $nOpt+1] end
]
238 set nPhrase
[llength $lPhrase]
240 foreach {k v
} [lrange $args 0 [expr $nOpt-1]] {
241 if {[info exists O
($k)]==0} { error "unrecognized option $k" }
248 upvar $O(-pc) counter
251 if {$O(-dict)!=""} { upvar $O(-dict) aDict
}
253 for {set j
0} {$j < [llength $aCol]} {incr j
} {
254 for {set i
0} {$i < $nPhrase} {incr i
} {
259 # Loop through each column of the current row.
260 for {set iCol
0} {$iCol < [llength $aCol]} {incr iCol
} {
262 # If there is a column filter, test whether this column is excluded. If
263 # so, skip to the next iteration of this loop. Otherwise, set zCol to the
264 # column value and nToken to the number of tokens that comprise it.
265 if {$O(-col)!="" && [lsearch $O(-col) $iCol]<0} continue
266 set zCol
[lindex $aCol $iCol]
267 set nToken
[llength $zCol]
269 # Each iteration of the following loop searches a substring of the
270 # column value for phrase matches. The last token of the substring
271 # is token $iLast of the column value. The first token is:
273 # iFirst = ($iLast - $O(-near) - 1)
275 # where $sz is the length of the phrase being searched for. A phrase
276 # counts as matching the substring if its first token lies on or before
277 # $iLast and its last token on or after $iFirst.
279 # For example, if the query is "NEAR(a+b c, 2)" and the column value:
281 # "x x x x A B x x C x"
282 # 0 1 2 3 4 5 6 7 8 9"
284 # when (iLast==8 && iFirst=5) the range will contain both phrases and
285 # so both instances can be added to the output poslists.
287 set iLast
[expr $O(-near) >= $nToken ?
$nToken - 1 : $O(-near)]
288 for { } {$iLast < $nToken} {incr iLast
} {
290 catch { array unset B
}
292 for {set iPhrase
0} {$iPhrase<$nPhrase} {incr iPhrase
} {
293 set p
[lindex $lPhrase $iPhrase]
294 set nPm1
[expr {[llength $p] - 1}]
295 set iFirst
[expr $iLast - $O(-near) - [llength $p]]
297 for {set i
$iFirst} {$i <= $iLast} {incr i
} {
298 set lCand
[lrange $zCol $i [expr $i+$nPm1]]
300 foreach tok
$p term
$lCand {
301 if {[nearset_match aDict
$tok $term]==0} { set bMatch
0 ; break }
303 if {$bMatch} { lappend B
($iPhrase) $i }
306 if {![info exists B
($iPhrase)]} break
309 if {$iPhrase==$nPhrase} {
310 for {set iPhrase
0} {$iPhrase<$nPhrase} {incr iPhrase
} {
311 set A
($iCol,$iPhrase) [concat $A($iCol,$iPhrase) $B($iPhrase)]
312 set A
($iCol,$iPhrase) [lsort -integer -uniq $A($iCol,$iPhrase)]
319 #puts [array names A]
321 for {set iPhrase
0} {$iPhrase<$nPhrase} {incr iPhrase
} {
322 for {set iCol
0} {$iCol < [llength $aCol]} {incr iCol
} {
323 foreach a
$A($iCol,$iPhrase) {
324 lappend res
"$counter.$iCol.$a"
330 #puts "$aCol -> $res"
334 proc nearset_match
{aDictVar tok term
} {
335 if {[string match
$tok $term]} { return 1 }
337 upvar $aDictVar aDict
338 if {[info exists aDict
($tok)]} {
339 foreach s
$aDict($tok) {
340 if {[string match
$s $term]} { return 1 }
346 #-------------------------------------------------------------------------
351 # Sort a position list of the type returned by command [nearset]
353 proc sort_poslist
{L
} {
354 lsort -command instcompare
$L
356 proc instcompare
{lhs rhs
} {
357 foreach {p1 c1 o1
} [split $lhs .
] {}
358 foreach {p2 c2 o2
} [split $rhs .
] {}
360 set res
[expr $c1 - $c2]
361 if {$res==0} { set res
[expr $o1 - $o2] }
362 if {$res==0} { set res
[expr $p1 - $p2] }
367 #-------------------------------------------------------------------------
368 # Logical operators used by the commands returned by fts5_tcl_expr().
372 if {[llength $a]==0} { return [list] }
374 sort_poslist
[concat {*}$args]
377 sort_poslist
[concat {*}$args]
380 if {[llength $b]>0} { return [list] }
384 #-------------------------------------------------------------------------
385 # This command is similar to [split], except that it also provides the
386 # start and end offsets of each token. For example:
388 # [fts5_tokenize_split "abc d ef"] -> {abc 0 3 d 4 5 ef 6 8}
391 proc gobble_whitespace
{textvar
} {
393 regexp {([ ]*)(.
*)} $t -> space t
394 return [string length
$space]
397 proc gobble_text
{textvar wordvar
} {
400 regexp {([^
]*)(.
*)} $t -> w t
401 return [string length
$w]
404 proc fts5_tokenize_split
{text} {
407 set iOff
[gobble_whitespace
text]
408 while {[set nToken
[gobble_text
text word
]]} {
409 lappend ret
$word $iOff [expr $iOff+$nToken]
411 incr iOff
[gobble_whitespace
text]
417 #-------------------------------------------------------------------------
419 proc foreach_detail_mode
{prefix script
} {
420 set saved
$::testprefix
421 foreach d
[list full col none
] {
422 set s
[string map
[list %DETAIL
% $d] $script]
424 set ::testprefix "$prefix-$d"
429 set ::testprefix $saved
432 proc detail_check
{} {
433 if {$::detail != "none" && $::detail!="full" && $::detail!="col"} {
434 error "not in foreach_detail_mode {...} block"
437 proc detail_is_none
{} { detail_check
; expr {$::detail == "none"} }
438 proc detail_is_col
{} { detail_check
; expr {$::detail == "col" } }
439 proc detail_is_full
{} { detail_check
; expr {$::detail == "full"} }
442 #-------------------------------------------------------------------------
443 # Convert a poslist of the type returned by fts5_test_poslist() to a
444 # collist as returned by fts5_test_collist().
446 proc fts5_poslist2collist
{poslist
} {
449 regexp {(.
*)\.
[1234567890]+} $h -> cand
452 set res
[lsort -command fts5_collist_elem_compare
-unique $res]
456 # Comparison function used by fts5_poslist2collist to sort collist entries.
457 proc fts5_collist_elem_compare
{a b
} {
458 foreach {a1 a2
} [split $a .
] {}
459 foreach {b1 b2
} [split $b .
] {}
461 if {$a1==$b1} { return [expr $a2 - $b2] }
462 return [expr $a1 - $b1]
466 #--------------------------------------------------------------------------
467 # Construct and return a tcl list equivalent to that returned by the SQL
468 # query executed against database handle [db]:
472 # fts5_test_poslist($tbl),
473 # fts5_test_collist($tbl)
475 # ORDER BY rowid $order;
477 proc fts5_query_data
{expr tbl
{order ASC
} {aDictVar
""}} {
479 # Figure out the set of columns in the FTS5 table. This routine does
480 # not handle tables with UNINDEXED columns, but if it did, it would
482 db
eval "PRAGMA table_info = $tbl" x
{ lappend lCols
$x(name
) }
485 if {$aDictVar != ""} {
486 upvar $aDictVar aDict
491 foreach e
$lCols { append cols
", '$e'" }
492 set tclexpr
[db one
[subst -novar {
493 SELECT fts5_expr_tcl
( $expr, 'nearset
$cols -dict $d -pc ::pc'
[set cols
] )
497 db
eval "SELECT rowid, * FROM $tbl ORDER BY rowid $order" x
{
499 foreach col
$lCols { lappend cols
$x($col) }
502 set rowdata
[eval $tclexpr]
503 if {$rowdata != ""} {
504 lappend res
$x(rowid
) $rowdata [fts5_poslist2collist
$rowdata]
511 #-------------------------------------------------------------------------
512 # Similar to [fts5_query_data], but omit the collist field.
514 proc fts5_poslist_data
{expr tbl
{order ASC
} {aDictVar
""}} {
518 upvar $aDictVar aDict
524 foreach {rowid poslist collist
} [fts5_query_data
$expr $tbl $order $dict] {
525 lappend res
$rowid $poslist
530 proc fts5_collist_data
{expr tbl
{order ASC
} {aDictVar
""}} {
534 upvar $aDictVar aDict
540 foreach {rowid poslist collist
} [fts5_query_data
$expr $tbl $order $dict] {
541 lappend res
$rowid $collist
546 #-------------------------------------------------------------------------
549 # This command will only work inside a [foreach_detail_mode] block. It tests
550 # whether or not expression $expr run on FTS5 table $tbl is supported by
551 # the current mode. If so, 1 is returned. If not, 0.
553 # detail=full (all queries supported)
554 # detail=col (all but phrase queries and NEAR queries)
555 # detail=none (all but phrase queries, NEAR queries, and column filters)
557 proc fts5_expr_ok
{expr tbl
} {
559 if {![detail_is_full
]} {
560 set nearset
"nearset_rc"
561 if {[detail_is_col
]} { set nearset
"nearset_rf" }
564 db
eval "PRAGMA table_info = $tbl" x
{ lappend lCols
$x(name
) }
567 foreach e
$lCols { append cols
", '$e'" }
569 set tclexpr
[db one
[subst -novar {
570 SELECT fts5_expr_tcl
( $expr, '
[set nearset
] $cols -pc ::pc'
[set cols
] )
573 if {$::expr_not_ok} { return 0 }
579 # Helper for [fts5_expr_ok]
580 proc nearset_rf
{aCol args
} {
581 set idx
[lsearch -exact $args --]
582 if {$idx != [llength $args]-2 ||
[llength [lindex $args end
]]!=1} {
588 # Helper for [fts5_expr_ok]
589 proc nearset_rc
{aCol args
} {
590 nearset_rf
$aCol {*}$args
591 if {[lsearch $args -col]>=0} {
598 #-------------------------------------------------------------------------
599 # Code for a simple Tcl tokenizer that supports synonyms at query time.
601 proc tclnum_tokenize
{mode tflags
text} {
602 foreach {w iStart iEnd
} [fts5_tokenize_split
$text] {
603 sqlite3_fts5_token
$w $iStart $iEnd
604 if {$tflags == $mode && [info exists
::tclnum_syn($w)]} {
605 foreach s
$::tclnum_syn($w) { sqlite3_fts5_token
-colo $s $iStart $iEnd }
610 proc tclnum_create
{args
} {
612 if {[llength $args]} {
613 set mode
[lindex $args 0]
615 if {$mode != "query" && $mode != "document"} { error "bad mode: $mode" }
616 return [list tclnum_tokenize
$mode]
619 proc fts5_tclnum_register
{db
} {
632 {a1 a2 a3 a4 a5 a6 a7 a8 a9
}
633 {b1 b2 b3 b4 b5 b6 b7 b8 b9
}
634 {c1 c2 c3 c4 c5 c6 c7 c8 c9
}
638 foreach x
$SYNDICT {if {$x!=$s} {lappend o
$x}}
639 set ::tclnum_syn($s) $o
642 sqlite3_fts5_create_tokenizer db tclnum tclnum_create
645 # End of tokenizer code.
646 #-------------------------------------------------------------------------