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/vec1ac.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 / vec1ac.test` 100 lines ยท 2343 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 vec1ac 16 vec1_load db 17 18 db func normalize normalize 19 proc normalize {vec} { 20 binary scan $vec f* aVal 21 set sum 0.0 22 foreach v $aVal { 23 set sum [expr $sum + ($v*$v)] 24 } 25 set sz [expr sqrt($sum)] 26 set aNew [list] 27 foreach v $aVal { 28 lappend aNew [expr $v/$sz] 29 } 30 binary format f* $aNew 31 } 32 33 db func rescale rescale 34 proc rescale {vec mul} { 35 binary scan $vec f* aVal 36 set aNew [list] 37 foreach v $aVal { 38 lappend aNew [expr $v*$mul] 39 } 40 binary format f* $aNew 41 } 42 43 do_execsql_test 1.0 { 44 CREATE TABLE t1(k INTEGER PRIMARY KEY, v VECTOR); 45 WITH s(i) AS ( 46 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000 47 ) 48 INSERT INTO t1 SELECT NULL, random_vector2(128) FROM s; 49 50 CREATE VIRTUAL TABLE v1 USING vec1; 51 INSERT INTO v1(rowid, vector) SELECT k, v FROM t1; 52 } 53 54 foreach {tn cfg} { 55 1 { {nbucket:0, codesize:0, distance:"cos"} } 56 2 { {nbucket:0, codesize:32, distance:"cos"} } 57 3 { {nbucket:32, codesize:32, distance:"cos"} } 58 } { 59 60 do_execsql_test 1.$tn.1 { 61 INSERT INTO v1(cmd, vector) VALUES('rebuild', ( 62 SELECT vec1_train(vector, $cfg) FROM v1 63 )); 64 } {} 65 66 do_execsql_test 1.$tn.2 { 67 SELECT sum( 68 rowid IN ( SELECT rowid FROM v1( normalize( o.vector ), '{K:10}' ) ) 69 ) 70 FROM v1 AS o WHERE rowid BETWEEN 600 AND 700; 71 } {101} 72 73 do_execsql_test 1.$tn.3 { 74 SELECT sum( 75 rowid IN ( SELECT rowid FROM v1( rescale( o.vector, 0.01 ), '{K:10}' ) ) 76 ) 77 FROM v1 AS o WHERE rowid BETWEEN 600 AND 700; 78 } {101} 79 } 80 81 do_execsql_test 2.0 { 82 CREATE VIRTUAL TABLE v2 USING vec1; 83 INSERT INTO v2(rowid, vector) SELECT k, v FROM t1; 84 INSERT INTO v2(cmd, vector) VALUES('rebuild', ( 85 SELECT vec1_train(vector, '{nbucket:32, codesize:32, distance:"cos"}') 86 FROM v2 87 )); 88 } 89 90 do_execsql_test 2.1 { 91 PRAGMA integrity_check; 92 } {ok} 93 94 do_execsql_test 2.2 { 95 DELETE FROM v2 WHERE (rowid % 45)==0; 96 PRAGMA integrity_check; 97 } {ok} 98 99 100 finish_test