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/vec1train.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 / vec1train.test` 226 lines ยท 6058 bytes Raw Blame History 1 # 2026-04-01 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 vec1train 16 vec1_load db 17 18 do_execsql_test 1.0 { 19 CREATE TABLE t1(a); 20 WITH s(i) AS ( 21 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 22 ) INSERT INTO t1 SELECT random_vector(16) FROM s; 23 } 24 25 do_catchsql_test 1.1 { 26 SELECT vec1_train(a, '{codesize: "large"}') FROM t1 27 } {1 {vec1: codesize must be set to an integer value between 1 and 256, or 0}} 28 29 do_catchsql_test 1.2 { 30 SELECT vec1_train(a, '{codesize: 16, nthread: -1}') FROM t1 31 } {1 {vec1: nthread must be set to an integer value between 1 and 128}} 32 33 do_catchsql_test 1.3 { 34 SELECT vec1_train(a, '{codesize: 1024}') FROM t1 35 } {1 {vec1: codesize must be set to an integer value between 1 and 256, or 0}} 36 37 do_catchsql_test 1.4 { 38 SELECT vec1_train(a, '{codesize: 8, opq: "yes"}') FROM t1 39 } {1 {vec1: opq must be set to a boolean value}} 40 41 do_catchsql_test 1.5 { 42 SELECT vec1_train(a, '{codesize: 8, opq: 555}') FROM t1 43 } {1 {vec1: opq must be set to a boolean value}} 44 45 do_execsql_test 2.0 { 46 CREATE VIRTUAL TABLE u1 USING vec1(a); 47 } 48 49 do_catchsql_test 2.1 { 50 INSERT INTO u1(cmd, arg) VALUES('rebuild', '{param: 1234}'); 51 } {1 {vec1: unknown parameter: param}} 52 53 do_execsql_test 3.0 { 54 CREATE TABLE t2(x); 55 INSERT INTO t2 VALUES(random_vector(16)); 56 INSERT INTO t2 VALUES(random_vector(16)); 57 INSERT INTO t2 VALUES(random_vector(16)); 58 59 INSERT INTO t2 VALUES(''); 60 } 61 62 do_catchsql_test 3.1 { 63 SELECT vec1_train(x, '{codesize: 8}') FROM t2 64 } {1 {vec1_train: invalid vector size: 0 bytes}} 65 66 do_execsql_test 3.2.0 { 67 UPDATE t2 SET x=randomblob(7) WHERE rowid=4; 68 } 69 do_catchsql_test 3.2.1 { 70 SELECT vec1_train(x, '{codesize: 8}') FROM t2 71 } {1 {vec1_train: invalid vector size: 7 bytes}} 72 73 do_execsql_test 3.3.0 { 74 UPDATE t2 SET x=random_vector(20) WHERE rowid=4; 75 } 76 do_catchsql_test 3.3.1 { 77 SELECT vec1_train(x, '{codesize: 8}') FROM t2 78 } {1 {vec1_train: bad argument}} 79 do_catchsql_test 3.3.2 { 80 SELECT vec1_train(x) FROM t2 81 } {1 {vec1_train: bad argument}} 82 83 do_execsql_test 3.4.0 { 84 UPDATE t2 SET x=random_vector(16) WHERE rowid=4; 85 } 86 #do_catchsql_test 3.4.1 { 87 # SELECT vec1_train(x, '{codesize: 12}') FROM t2 88 #} {1 {vec1_train: vector size (16) must be a multiple of codesize (12)}} 89 90 #------------------------------------------------------------------------- 91 reset_db 92 vec1_load db 93 94 do_execsql_test 4.0 { 95 CREATE TABLE x1(v); 96 } 97 98 do_catchsql_test 4.1 { 99 SELECT vec1_train(v, '{nbucket: 8}') FROM x1 100 } {1 {too few training vectors (have 0, require more than that)}} 101 102 do_execsql_test 4.2 { 103 WITH s(i) AS ( 104 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100 105 ) INSERT INTO x1 SELECT random_vector(16) FROM s; 106 } 107 108 do_catchsql_test 4.3 { 109 SELECT vec1_train(v, '{nbucket: 8, progress: "nofunc"}') FROM x1 110 } {1 {no such function: nofunc}} 111 112 proc errorfunc {args} { 113 error "this is an error!" 114 } 115 db func errorfunc errorfunc 116 117 do_catchsql_test 4.4 { 118 SELECT vec1_train(v, '{nbucket: 8, progress: "errorfunc"}') FROM x1 119 } {1 {this is an error!}} 120 121 #------------------------------------------------------------------------- 122 reset_db 123 vec1_load db 124 125 do_execsql_test 5.0 { 126 CREATE VIRTUAL TABLE v1 USING vec1(a); 127 WITH s(i) AS ( 128 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1000 129 ) INSERT INTO v1 SELECT random_vector(16) FROM s; 130 } 131 132 do_catchsql_test 5.1 { 133 INSERT INTO v1(cmd, arg) VALUES( 134 'rebuild', (SELECT substr( ( 135 SELECT vec1_train(a, '{codesize: 8}') FROM v1 136 ), 1, 2000)) 137 ); 138 } {1 {vec1: model size mismatch: have 2000, expected 16408}} 139 140 #------------------------------------------------------------------------- 141 reset_db 142 vec1_load db 143 do_execsql_test 6.0 { 144 CREATE VIRTUAL TABLE v1 USING vec1(a); 145 WITH s(i) AS ( 146 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<12 147 ) INSERT INTO v1 SELECT random_vector(16) FROM s; 148 } 149 150 proc myprofile {args} {} 151 db func myprofile myprofile 152 153 do_catchsql_test 6.1 { 154 INSERT INTO v1(cmd, arg) VALUES('rebuild', ( 155 SELECT vec1_train(a, '{quantizer:"pq", profile: "myprofile"}') FROM v1 156 )); 157 } {1 {too few training vectors (require 512, have 12)}} 158 159 #------------------------------------------------------------------------- 160 # vec1_train() is SQLITE_DIRECTONLY. It may not be used within a view 161 # or trigger. 162 # 163 reset_db 164 vec1_load db 165 do_execsql_test 7.0 { 166 CREATE TABLE t1(a); 167 WITH s(i) AS ( 168 VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<100 169 ) INSERT INTO t1 SELECT random_vector(16) FROM s; 170 CREATE TABLE log(x); 171 } 172 173 do_execsql_test 7.1.0 { 174 CREATE VIEW v1 AS SELECT vec1_train(a) FROM t1; 175 CREATE VIEW v2 AS SELECT vec1_train(a, '{nbucket: 8}') FROM t1; 176 } 177 do_catchsql_test 7.1.1 { 178 SELECT * FROM v1 179 } {1 {unsafe use of vec1_train()}} 180 do_catchsql_test 7.1.2 { 181 SELECT * FROM v2 182 } {1 {unsafe use of vec1_train()}} 183 184 do_execsql_test 7.2.0 { 185 CREATE TABLE t2(x); 186 CREATE TRIGGER tr1 AFTER INSERT ON t2 BEGIN 187 INSERT INTO log SELECT vec1_train(a, '{nbucket: 8}') FROM t1; 188 END; 189 } 190 do_catchsql_test 7.2.1 { 191 INSERT INTO t2 VALUES(1); 192 } {1 {unsafe use of vec1_train()}} 193 do_execsql_test 7.2.2 { 194 SELECT count(*) FROM t2; 195 SELECT count(*) FROM log; 196 } {0 0} 197 198 #------------------------------------------------------------------------- 199 # 200 reset_db 201 vec1_load db 202 203 do_catchsql_test 8.0 { 204 205 WITH data(i, v) AS ( 206 SELECT 1, random_vector(4097) 207 UNION ALL 208 SELECT i+1, random_vector(4097) FROM data WHERE i<550 209 ) 210 SELECT length( vec1_train(v, '{nbucket: 8}') ) FROM data; 211 212 } {1 {vec1_train: invalid vector size: 16388 bytes}} 213 214 do_execsql_test 8.1 { 215 216 WITH data(i, v) AS ( 217 SELECT 1, random_vector(4096) 218 UNION ALL 219 SELECT i+1, random_vector(4096) FROM data WHERE i<550 220 ) 221 SELECT length( vec1_train(v, '{nbucket: 8}') ) FROM data; 222 223 } [expr 8*4096*4 + 24] 224 225 finish_test 226