Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/vec1/test/vec1cat.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / vec1 / test / vec1cat.test` 247 lines ยท 6267 bytes Raw Blame History 1 # 2026-01-17 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 # 13 14 source [file join [file dirname [info script]] vec1_common.tcl] 15 set testprefix vec1cat 16 vec1_load db 17 18 do_execsql_test 1.0 { 19 PRAGMA table_info = vec1cat 20 } { 21 0 database {} 0 {} 0 22 1 name {} 0 {} 0 23 2 model {} 0 {} 0 24 3 qprofile {} 0 {} 0 25 } 26 27 do_execsql_test 1.1 { 28 CREATE VIRTUAL TABLE x1 USING vec1(x); 29 SELECT database, name FROM vec1cat; 30 } { 31 main x1 32 } 33 34 db close 35 sqlite3 db test.db 36 vec1_load db 37 38 do_execsql_test 1.2 { 39 SELECT database, name, model FROM vec1cat; 40 } { 41 main x1 {{"index": "none", "distance": "l2"}} 42 } 43 44 do_execsql_test 1.3 { 45 INSERT INTO x1(cmd, arg) VALUES( 'rebuild', '{index:"flat", distance:"cos"}'); 46 } 47 48 do_execsql_test 1.4 { 49 SELECT database, name, model FROM vec1cat; 50 } { 51 main x1 {{"index": "flat", "distance": "cos"}} 52 } 53 54 db close 55 sqlite3 db test.db 56 vec1_load db 57 58 do_execsql_test 1.5 { 59 SELECT database, name, model FROM vec1cat; 60 } { 61 main x1 {{"index": "flat", "distance": "cos"}} 62 } 63 64 do_execsql_test 1.6 { 65 INSERT INTO x1(cmd, arg) VALUES('rebuild', ( 66 WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000) 67 SELECT vec1_train(random_vector(16), '{nbucket:8, codesize:8}') FROM s 68 )); 69 SELECT database, name, model FROM vec1cat; 70 } { 71 main x1 72 {{"index": "trained", "distance": "l2", "nbucket": 8, "quantizer": "pq", "codesize": 8, "residual": true, "wht": false}} 73 } 74 75 do_execsql_test 1.7 { 76 CREATE VIRTUAL TABLE temp.x2 USING vec1(x); 77 SELECT rowid, database, name, model FROM vec1cat; 78 } { 79 1 temp x2 80 {{"index": "none", "distance": "l2"}} 81 2 main x1 82 {{"index": "trained", "distance": "l2", "nbucket": 8, "quantizer": "pq", "codesize": 8, "residual": true, "wht": false}} 83 } 84 85 foreach {tn spec res} { 86 87 1 {nbucket:0, codesize:8} { 88 "index": "trained", "distance": "l2", "nbucket": 0, 89 "quantizer": "pq", "codesize": 8, 90 "residual": false, "wht": false 91 } 92 93 2 {nbucket:8, codesize:0} { 94 "index": "trained", "distance": "l2", "nbucket": 8, 95 "quantizer": "none", "codesize": 0, 96 "residual": false, "wht": false 97 } 98 99 3 {nbucket:0, codesize:8, opq:1} { 100 "index": "trained", "distance": "l2", "nbucket": 0, 101 "quantizer": "opq", "codesize": 8, 102 "residual": false, "wht": false 103 } 104 105 4 {codesize:2, quantizer: "bq", distance: "cos"} { 106 "index": "trained", "distance": "cos", "nbucket": 0, 107 "quantizer": "bq", "codesize": 2, 108 "residual": false, "wht": true 109 } 110 111 5 {codesize:1, nbucket: 16, quantizer: "bq", distance: "l2", wht: 0} { 112 "index": "trained", "distance": "l2", "nbucket": 16, 113 "quantizer": "bq", "codesize": 1, 114 "residual": true, "wht": false 115 } 116 117 6 {codesize:0, nbucket: 0} { 118 "index":"flat", "distance":"l2" 119 } 120 121 7 {} { 122 "index":"flat", "distance":"l2" 123 } 124 125 } { 126 127 set model [db one { 128 WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000) 129 SELECT vec1_train(random_vector(16), '{'||$spec||'}') FROM s 130 }] 131 132 set output [db one { SELECT json('{' || $res || '}') }] 133 134 do_execsql_test 1.8.$tn.1 { 135 INSERT INTO x1(cmd, arg) VALUES( 'rebuild', $model ); 136 SELECT json(model) FROM vec1cat WHERE name='x1'; 137 } [list $output] 138 139 if {[db one {SELECT json_extract($output, '$.index')=='trained'}]} { 140 set model [db one { 141 WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000) 142 SELECT vec1_train(random_vector(16), $output) FROM s 143 }] 144 } else { 145 set model $output 146 } 147 do_execsql_test 1.8.$tn.2 { 148 INSERT INTO x1(cmd, arg) VALUES( 'rebuild', $model ); 149 SELECT json(model) FROM vec1cat WHERE name='x1'; 150 } [list $output] 151 } 152 153 #------------------------------------------------------------------------- 154 reset_db 155 vec1_load db 156 157 do_execsql_test 2.0 { 158 CREATE VIRTUAL TABLE x1 USING vec1(x); 159 CREATE VIRTUAL TABLE x2 USING vec1(y); 160 CREATE VIRTUAL TABLE x3 USING vec1(z); 161 } 162 163 do_execsql_test 2.1 { 164 SELECT rowid, name FROM vec1cat 165 } { 166 1 x3 2 x2 3 x1 167 } 168 169 do_test 2.2 { 170 list [catch { 171 db eval { 172 SELECT rowid, name FROM vec1cat 173 } { 174 if {$name=="x2"} { 175 db eval { 176 DROP TABLE x2; 177 DROP TABLE x1; 178 } 179 } 180 } 181 } msg] $msg 182 } {1 {database table is locked}} 183 184 do_execsql_test 2.3 { 185 SELECT qprofile FROM vec1cat WHERE name='x1'; 186 } {{{ 187 "total": {"cnt": 0, "cycle": 0}, 188 "init": {"cnt": 0, "cycle": 0}, 189 "coarse": {"cnt": 0, "cycle": 0}, 190 "idxread": {"cnt": 0, "cycle": 0}, 191 "metaread": {"cnt": 0, "cycle": 0}, 192 "metascan": {"cnt": 0, "cycle": 0}, 193 "lut": {"cnt": 0, "cycle": 0}, 194 "pqscan": {"cnt": 0, "cycle": 0}, 195 "finalsort": {"cnt": 0, "cycle": 0} 196 }}} 197 198 #------------------------------------------------------------------------- 199 reset_db 200 vec1_load db 201 do_execsql_test 3.0 { 202 CREATE VIRTUAL TABLE x2 USING vec1(y); 203 INSERT INTO x2 VALUES( random_vector(13) ); 204 INSERT INTO x2 VALUES( random_vector(13) ); 205 INSERT INTO x2 VALUES( random_vector(13) ); 206 INSERT INTO x2 VALUES( random_vector(13) ); 207 INSERT INTO x2 VALUES( random_vector(13) ); 208 209 INSERT INTO x2(cmd, arg) VALUES('rebuild', ( 210 WITH s(i) AS ( VALUES (1) UNION ALL SELECT i+1 FROM s WHERE i<512 ) 211 SELECT vec1_train( random_vector(13), '{nbucket:8}' ) FROM s 212 )) 213 } 214 215 do_execsql_test 3.1 { 216 SELECT count(distance) FROM x2; 217 } 5 218 219 #------------------------------------------------------------------------- 220 reset_db 221 vec1_load db 222 do_execsql_test 4.0 { 223 CREATE VIRTUAL TABLE x1 USING vec1(y, a); 224 225 INSERT INTO x1(cmd, arg) VALUES('rebuild', ( 226 WITH s(i) AS ( VALUES (1) UNION ALL SELECT i+1 FROM s WHERE i<512 ) 227 SELECT vec1_train( 228 random_vector(32), '{nbucket:8, codesize:8, distance:"cos"}' 229 ) FROM s 230 )) 231 } 232 233 do_execsql_test 4.1 { 234 INSERT INTO x1 (rowid, y, a) VALUES(10, random_vector(32), 4556); 235 } 236 237 do_execsql_test 4.2 { 238 SELECT key FROM json_each( (SELECT distance FROM x1) ) 239 } {bucket coarse_error reconstruction_error code} 240 241 do_execsql_test 4.3 { 242 SELECT typeof(distance) FROM x1(random_vector(32), '{nprobe:8, K:5}') 243 } {real} 244 245 246 finish_test 247