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/vec1binary.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 / vec1binary.test` 162 lines ยท 3920 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 vec1binary 16 17 18 foreach {tn nElem codesize} { 19 1 16 2 20 2 16 1 21 3 48 6 22 4 48 3 23 } { 24 reset_db 25 vec1_load db 26 expr srand(0) 27 do_execsql_test 1.$tn.0 { 28 CREATE VIRTUAL TABLE v1 USING vec1(v); 29 } 30 31 set nRow [expr 40] 32 set nLimit [expr 40] 33 34 do_execsql_test 1.$tn.1 { 35 WITH s(i) AS ( 36 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$nRow 37 ) 38 INSERT INTO v1 SELECT random_vector($nElem) FROM s; 39 } 40 41 set jarg "{quantizer:\"bq\", nbucket: 8, codesize: $codesize}" 42 do_execsql_test 1.$tn.2 { 43 INSERT INTO v1(cmd, arg) VALUES('rebuild', ( 44 SELECT vec1_train(v, $jarg) FROM v1 45 )); 46 } 47 48 set v [db one {SELECT random_vector($nElem)}] 49 50 set res [db eval { 51 SELECT rowid FROM v1 ORDER by vec1_l2_distance($v, v) LIMIT $nLimit 52 }] 53 54 do_execsql_test 1.$tn.2 { 55 SELECT rowid FROM v1($v, '{K:50, nprobe:8}') 56 ORDER BY vec1_l2_distance(v, $v) 57 LIMIT $nLimit 58 } $res 59 60 do_execsql_test 1.$tn.3 { 61 PRAGMA integrity_check 62 } {ok} 63 } 64 65 #------------------------------------------------------------------------- 66 reset_db 67 vec1_load db 68 69 do_execsql_test 2.1 { 70 CREATE VIRTUAL TABLE v1 USING vec1(v); 71 INSERT INTO v1(cmd, arg) VALUES('rebuild', ( 72 WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$nRow ) 73 SELECT vec1_train(random_vector(8), '{quantizer:"bq", nbucket:8}') FROM s 74 )); 75 76 INSERT INTO v1 VALUES( random_vector(8) ); 77 } 78 79 set v [db one {SELECT random_vector(8)}] 80 81 do_execsql_test 2.2 { 82 SELECT rowid FROM v1($v, '{nprobe:8}') LIMIT $nLimit 83 } {1} 84 85 #------------------------------------------------------------------------- 86 reset_db 87 vec1_load db 88 89 do_execsql_test 2.1 { 90 CREATE VIRTUAL TABLE v1 USING vec1(abc); 91 INSERT INTO v1(cmd, arg) VALUES('rebuild', ( 92 WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<$nRow ) 93 SELECT vec1_train(random_vector(64), '{quantizer:"bq", nbucket:2}') FROM s 94 )); 95 96 WITH s(i) AS ( 97 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 98 ) 99 INSERT INTO v1 SELECT random_vector(64) FROM s; 100 } 101 102 do_execsql_test 2.2 { 103 PRAGMA integrity_check 104 } {ok} 105 106 foreach id [db eval {SELECT rowid FROM v1}] { 107 db eval { DELETE FROM v1 WHERE rowid = $id } 108 if {($id % 1)==0} { 109 do_execsql_test 2.3.$id { 110 PRAGMA integrity_check 111 } {ok} 112 } 113 } 114 115 #------------------------------------------------------------------------- 116 # Now check the version number in model headers. This should be 7 when 117 # using a binary quantizer, or 4 otherwise. 118 # 119 reset_db 120 vec1_load db 121 122 foreach {tn jarg iVersion} { 123 1 "{nbucket:2, codesize:8}" 4 124 2 "{nbucket:2, codesize:8, quantizer:\"bq\"}" 7 125 3 "{nbucket:2, codesize:8, quantizer:\"pq\"}" 4 126 } { 127 do_test 2.$tn { 128 set M [db one { 129 WITH s(i) AS ( 130 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<512 131 ) 132 SELECT vec1_train(random_vector(16), $jarg) FROM s; 133 }] 134 135 binary scan $M I version 136 set version 137 } $iVersion 138 } 139 140 #------------------------------------------------------------------------- 141 reset_db 142 vec1_load db 143 144 do_execsql_test 3.1 { 145 CREATE VIRTUAL TABLE v1 USING vec1(v); 146 WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000 ) 147 INSERT INTO v1 SELECT random_vector(100) FROM s; 148 149 INSERT INTO v1(cmd, arg) VALUES('rebuild', ( 150 SELECT vec1_train(v, '{quantizer:"bq", nbucket:2, codesize:8}') FROM v1 151 )); 152 } 153 154 set v [db one {SELECT random_vector(100)}] 155 do_execsql_test 3.2 { 156 SELECT count(*) FROM ( 157 SELECT rowid FROM v1($v, '{nprobe:2}') LIMIT 5 158 ); 159 } {5} 160 161 finish_test 162