OLD | NEW |
| (Empty) |
1 # 2014 Dec 20 | |
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 # Tests of the scalar fts5_rowid() and fts5_decode() functions. | |
13 # | |
14 | |
15 source [file join [file dirname [info script]] fts5_common.tcl] | |
16 set testprefix fts5rowid | |
17 | |
18 # If SQLITE_ENABLE_FTS5 is defined, omit this file. | |
19 ifcapable !fts5 { | |
20 finish_test | |
21 return | |
22 } | |
23 | |
24 do_catchsql_test 1.1 { | |
25 SELECT fts5_rowid() | |
26 } {1 {should be: fts5_rowid(subject, ....)}} | |
27 | |
28 do_catchsql_test 1.2 { | |
29 SELECT fts5_rowid('segment') | |
30 } {1 {should be: fts5_rowid('segment', segid, pgno))}} | |
31 | |
32 do_execsql_test 1.3 { | |
33 SELECT fts5_rowid('segment', 1, 1) | |
34 } {137438953473} | |
35 | |
36 do_catchsql_test 1.4 { | |
37 SELECT fts5_rowid('nosucharg'); | |
38 } {1 {first arg to fts5_rowid() must be 'segment'}} | |
39 | |
40 | |
41 #------------------------------------------------------------------------- | |
42 # Tests of the fts5_decode() function. | |
43 # | |
44 reset_db | |
45 do_execsql_test 2.1 { | |
46 CREATE VIRTUAL TABLE x1 USING fts5(a, b); | |
47 INSERT INTO x1(x1, rank) VALUES('pgsz', 32); | |
48 } {} | |
49 | |
50 proc rnddoc {n} { | |
51 set map [list 0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j] | |
52 set doc [list] | |
53 for {set i 0} {$i < $n} {incr i} { | |
54 lappend doc [string map $map [format %.3d [expr int(rand()*100)]]] | |
55 } | |
56 set doc | |
57 } | |
58 db func rnddoc rnddoc | |
59 | |
60 do_execsql_test 2.2 { | |
61 WITH r(a, b) AS ( | |
62 SELECT rnddoc(6), rnddoc(6) UNION ALL | |
63 SELECT rnddoc(6), rnddoc(6) FROM r | |
64 ) | |
65 INSERT INTO x1 SELECT * FROM r LIMIT 10000; | |
66 } | |
67 | |
68 set res [db one {SELECT count(*) FROM x1_data}] | |
69 do_execsql_test 2.3 { | |
70 SELECT count(fts5_decode(rowid, block)) FROM x1_data; | |
71 } $res | |
72 do_execsql_test 2.4 { | |
73 UPDATE x1_data SET block = X''; | |
74 -- SELECT count(fts5_decode(rowid, block)) FROM x1_data; | |
75 SELECT count(*) FROM x1_data; | |
76 } $res | |
77 | |
78 do_execsql_test 2.5 { | |
79 INSERT INTO x1(x1, rank) VALUES('pgsz', 1024); | |
80 INSERT INTO x1(x1) VALUES('rebuild'); | |
81 } | |
82 | |
83 set res [db one {SELECT count(*) FROM x1_data}] | |
84 do_execsql_test 2.6 { | |
85 SELECT count(fts5_decode(rowid, block)) FROM x1_data; | |
86 } $res | |
87 | |
88 # This is really a corruption test... | |
89 #do_execsql_test 2.7 { | |
90 # UPDATE x1_data SET block = X''; | |
91 # SELECT count(fts5_decode(rowid, block)) FROM x1_data; | |
92 #} $res | |
93 | |
94 do_execsql_test 2.8 { | |
95 SELECT fts5_decode(fts5_rowid('segment', 1000, 1), X'AB') | |
96 } {corrupt} | |
97 | |
98 #------------------------------------------------------------------------- | |
99 # Tests with very large tokens. | |
100 # | |
101 set strlist [list \ | |
102 "[string repeat x 400]" \ | |
103 "[string repeat x 300][string repeat w 100]" \ | |
104 "[string repeat x 300][string repeat y 100]" \ | |
105 "[string repeat x 300][string repeat z 600]" \ | |
106 ] | |
107 do_test 3.0 { | |
108 execsql { | |
109 BEGIN; | |
110 CREATE VIRTUAL TABLE x2 USING fts5(a); | |
111 } | |
112 foreach str $strlist { execsql { INSERT INTO x2 VALUES($str) } } | |
113 execsql COMMIT | |
114 } {} | |
115 | |
116 for {set tn 0} {$tn<[llength $strlist]} {incr tn} { | |
117 set str [lindex $strlist $tn] | |
118 do_execsql_test 3.1.$tn { | |
119 SELECT rowid FROM x2 WHERE x2 MATCH $str | |
120 } [expr $tn+1] | |
121 } | |
122 | |
123 set res [db one {SELECT count(*) FROM x2_data}] | |
124 do_execsql_test 3.2 { | |
125 SELECT count(fts5_decode(rowid, block)) FROM x2_data; | |
126 } $res | |
127 | |
128 #------------------------------------------------------------------------- | |
129 # Leaf pages with no terms or rowids at all. | |
130 # | |
131 set strlist [list \ | |
132 "[string repeat {w } 400]" \ | |
133 "[string repeat {x } 400]" \ | |
134 "[string repeat {y } 400]" \ | |
135 "[string repeat {z } 400]" \ | |
136 ] | |
137 do_test 4.0 { | |
138 execsql { | |
139 BEGIN; | |
140 CREATE VIRTUAL TABLE x3 USING fts5(a); | |
141 INSERT INTO x3(x3, rank) VALUES('pgsz', 32); | |
142 } | |
143 foreach str $strlist { execsql { INSERT INTO x3 VALUES($str) } } | |
144 execsql COMMIT | |
145 } {} | |
146 | |
147 for {set tn 0} {$tn<[llength $strlist]} {incr tn} { | |
148 set str [lindex $strlist $tn] | |
149 do_execsql_test 4.1.$tn { | |
150 SELECT rowid FROM x3 WHERE x3 MATCH $str | |
151 } [expr $tn+1] | |
152 } | |
153 | |
154 set res [db one {SELECT count(*) FROM x3_data}] | |
155 do_execsql_test 4.2 { | |
156 SELECT count(fts5_decode(rowid, block)) FROM x3_data; | |
157 } $res | |
158 | |
159 #------------------------------------------------------------------------- | |
160 # Position lists with large values. | |
161 # | |
162 set strlist [list \ | |
163 "[string repeat {w } 400]a" \ | |
164 "[string repeat {x } 400]a" \ | |
165 "[string repeat {y } 400]a" \ | |
166 "[string repeat {z } 400]a" \ | |
167 ] | |
168 do_test 5.0 { | |
169 execsql { | |
170 BEGIN; | |
171 CREATE VIRTUAL TABLE x4 USING fts5(a); | |
172 INSERT INTO x4(x4, rank) VALUES('pgsz', 32); | |
173 } | |
174 foreach str $strlist { execsql { INSERT INTO x4 VALUES($str) } } | |
175 execsql COMMIT | |
176 } {} | |
177 | |
178 do_execsql_test 5.1 { | |
179 SELECT rowid FROM x4 WHERE x4 MATCH 'a' | |
180 } {1 2 3 4} | |
181 | |
182 set res [db one {SELECT count(*) FROM x4_data}] | |
183 do_execsql_test 5.2 { | |
184 SELECT count(fts5_decode(rowid, block)) FROM x4_data; | |
185 } $res | |
186 | |
187 finish_test | |
188 | |
OLD | NEW |