sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / vec1 / test / vec1bucket.test
| 1 | # 2026-10-10 |
| 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 for vectors that lie (almost) exactly on the boundary between two |
| 13 | # IVF buckets. The bucket each such vector is assigned to depends only on |
| 14 | # rounding in the L2 distance calculation. The 'rebuild' command, integrity |
| 15 | # check and DELETE must all agree on which bucket each vector is in. |
| 16 | # |
| 17 | # A real PQ model is trained, then its centroids are overwritten with known |
| 18 | # values. Centroids 0 and 1 are random, the others are far away from |
| 19 | # everything. All vectors are inserted near the midpoint of centroids |
| 20 | # 0 and 1. Using 8 centroids matters - with fewer than 8 the AVX2 rebuild |
| 21 | # code falls back to the same routine used by integrity-check and DELETE. |
| 22 | # |
| 23 | |
| 24 | source [file join [file dirname [info script]] vec1_common.tcl] |
| 25 | set testprefix vec1bucket |
| 26 | vec1_load db |
| 27 | |
| 28 | set nElem 128 |
| 29 | set nVec 2000 |
| 30 | set noise 1e-6 |
| 31 | |
| 32 | proc rlist {n} { |
| 33 | set v {} |
| 34 | for {set i 0} {$i<$n} {incr i} { lappend v [expr {rand()*2.0-1.0}] } |
| 35 | set v |
| 36 | } |
| 37 | |
| 38 | # Train a model with 8 buckets, plain PQ, no rotation and no residuals. |
| 39 | do_test 1.0 { |
| 40 | db eval { CREATE TEMP TABLE tr(v) } |
| 41 | for {set i 0} {$i<600} {incr i} { |
| 42 | db eval { INSERT INTO tr VALUES( random_vector($nElem) ) } |
| 43 | } |
| 44 | set T [db one { |
| 45 | SELECT vec1_train(v, '{codesize:8, nbucket:8, quantizer:"pq", residual:0}') |
| 46 | FROM tr |
| 47 | }] |
| 48 | binary scan $T I6 hdr |
| 49 | lassign $hdr iVersion flags nE nCodebook nBucket eDist |
| 50 | list [expr {$flags & 0x16}] $nBucket |
| 51 | } {0 8} |
| 52 | |
| 53 | # Replace the centroids in the model blob. |
| 54 | set nCodeElem [expr {($nElem + $nCodebook - 1) / $nCodebook}] |
| 55 | set iCentOff [expr {24 + $nCodebook*$nCodeElem*256*4}] |
| 56 | set c0 [rlist $nElem] |
| 57 | set c1 [rlist $nElem] |
| 58 | set aCent [concat $c0 $c1] |
| 59 | for {set i 2} {$i<$nBucket} {incr i} { |
| 60 | foreach x [rlist $nElem] { lappend aCent [expr {$x + 100.0}] } |
| 61 | } |
| 62 | set T [string replace $T $iCentOff [expr {$iCentOff+$nBucket*$nElem*4-1}] \ |
| 63 | [binary format f* $aCent]] |
| 64 | |
| 65 | # Insert vectors near the midpoint of c0 and c1, plus a little noise. |
| 66 | set mid {} |
| 67 | foreach a $c0 b $c1 { lappend mid [expr {($a+$b)/2.0}] } |
| 68 | do_test 1.1 { |
| 69 | db eval { CREATE VIRTUAL TABLE v1 USING vec1 } |
| 70 | db transaction { |
| 71 | for {set i 1} {$i<=$nVec} {incr i} { |
| 72 | set v {} |
| 73 | foreach m $mid { lappend v [expr {$m + (rand()-0.5)*$noise}] } |
| 74 | set blob [binary format f* $v] |
| 75 | db eval { INSERT INTO v1(rowid, vector) VALUES($i, $blob) } |
| 76 | } |
| 77 | } |
| 78 | db one { SELECT count(*) FROM v1 } |
| 79 | } $nVec |
| 80 | |
| 81 | # Rebuild (batched N-vector path), then integrity-check (per-vector path). |
| 82 | do_execsql_test 1.2 { INSERT INTO v1(cmd, vector) VALUES('rebuild', $T) } |
| 83 | do_execsql_test 1.3 { PRAGMA integrity_check } ok |
| 84 | |
| 85 | # Delete rows with rowids from iFirst to nVec, stepping by 2. Each DELETE |
| 86 | # recomputes the bucket using the per-vector path, so may compute a |
| 87 | # different bucket to the one used by 'rebuild'. Return the number of rows |
| 88 | # that could not be deleted. |
| 89 | proc delete_rows {tbl iFirst} { |
| 90 | global nVec |
| 91 | set nFail 0 |
| 92 | for {set i $iFirst} {$i<=$nVec} {incr i 2} { |
| 93 | if {[catch { db eval "DELETE FROM $tbl WHERE rowid=$i" }]} { |
| 94 | incr nFail |
| 95 | } |
| 96 | } |
| 97 | set nFail |
| 98 | } |
| 99 | |
| 100 | # Delete every row, checking the index is still consistent half way. |
| 101 | # Deleting rows causes lists to be rewritten once they contain enough |
| 102 | # tombstones, which must be done using the bucket the row was actually |
| 103 | # found in, not the one calculated for it. |
| 104 | do_test 1.4 { delete_rows v1 1 } 0 |
| 105 | do_execsql_test 1.5 { PRAGMA integrity_check } ok |
| 106 | do_test 1.6 { delete_rows v1 2 } 0 |
| 107 | do_execsql_test 1.7 { |
| 108 | SELECT count(*) FROM v1; |
| 109 | PRAGMA integrity_check; |
| 110 | } {0 ok} |
| 111 | |
| 112 | #------------------------------------------------------------------------- |
| 113 | # Same again, but this time move all of bucket 1's lists into bucket 7, |
| 114 | # which is nowhere near any of the vectors. Integrity-check reports this. |
| 115 | # But DELETE should still be able to find and delete the rows. |
| 116 | # |
| 117 | do_test 2.0 { |
| 118 | db eval { CREATE VIRTUAL TABLE v2 USING vec1 } |
| 119 | db transaction { |
| 120 | for {set i 1} {$i<=$nVec} {incr i} { |
| 121 | set v {} |
| 122 | foreach m $mid { lappend v [expr {$m + (rand()-0.5)*$noise}] } |
| 123 | set blob [binary format f* $v] |
| 124 | db eval { INSERT INTO v2(rowid, vector) VALUES($i, $blob) } |
| 125 | } |
| 126 | } |
| 127 | db eval { INSERT INTO v2(cmd, vector) VALUES('rebuild', $T) } |
| 128 | db one { SELECT count(*)>0 FROM v2_idx WHERE bucket=1 } |
| 129 | } 1 |
| 130 | |
| 131 | do_execsql_test 2.1 { |
| 132 | UPDATE v2_idx SET bucket=7 WHERE bucket=1; |
| 133 | } |
| 134 | do_test 2.2 { |
| 135 | string match "*is in wrong bucket*" [db one { PRAGMA integrity_check }] |
| 136 | } 1 |
| 137 | do_test 2.3 { delete_rows v2 1 } 0 |
| 138 | do_test 2.4 { delete_rows v2 2 } 0 |
| 139 | do_execsql_test 2.5 { SELECT count(*) FROM v2 } 0 |
| 140 | |
| 141 | #------------------------------------------------------------------------- |
| 142 | # Create a list in bucket 1 containing rows that belong in bucket 0 as |
| 143 | # well as rows that belong in bucket 1. Then delete all the bucket 0 rows. |
| 144 | # DELETE finds them by searching all buckets. When a list is rewritten to |
| 145 | # remove tombstones, it must stay in bucket 1, the bucket it was found in. |
| 146 | # If it were moved to bucket 0, the bucket 1 rows left in it would be in |
| 147 | # the wrong bucket. |
| 148 | # |
| 149 | # The list is created by rebuilding using model $TA, in which centroid 0 |
| 150 | # is far away from everything, so all rows go to bucket 1. The model is |
| 151 | # then replaced by $T, in which centroid 0 is c0 again. |
| 152 | # |
| 153 | set TA [string replace $T $iCentOff [expr {$iCentOff+$nElem*4-1}] \ |
| 154 | [binary format f* [lmap x $c0 {expr {$x + 200.0}}]]] |
| 155 | |
| 156 | do_test 3.0 { |
| 157 | db eval { CREATE VIRTUAL TABLE v3 USING vec1 } |
| 158 | db transaction { |
| 159 | for {set i 1} {$i<=$nVec} {incr i} { |
| 160 | set c [expr {$i<=$nVec/2 ? $c0 : $c1}] |
| 161 | set v [lmap x $c {expr {$x + (rand()-0.5)*0.001}}] |
| 162 | set blob [binary format f* $v] |
| 163 | db eval { INSERT INTO v3(rowid, vector) VALUES($i, $blob) } |
| 164 | } |
| 165 | } |
| 166 | db eval { INSERT INTO v3(cmd, vector) VALUES('rebuild', $TA) } |
| 167 | db eval { SELECT DISTINCT bucket FROM v3_idx } |
| 168 | } 1 |
| 169 | |
| 170 | do_test 3.1 { |
| 171 | db eval { UPDATE v3_model SET val = $T WHERE id=1 } |
| 172 | db close |
| 173 | sqlite3 db test.db |
| 174 | vec1_load db |
| 175 | string match "*is in wrong bucket*" [db one { PRAGMA integrity_check }] |
| 176 | } 1 |
| 177 | |
| 178 | proc delete_range {tbl iFirst iLast} { |
| 179 | set nFail 0 |
| 180 | for {set i $iFirst} {$i<=$iLast} {incr i} { |
| 181 | if {[catch { db eval "DELETE FROM $tbl WHERE rowid=$i" }]} { |
| 182 | incr nFail |
| 183 | } |
| 184 | } |
| 185 | set nFail |
| 186 | } |
| 187 | |
| 188 | do_test 3.2 { delete_range v3 1 [expr {$nVec/2}] } 0 |
| 189 | do_execsql_test 3.3 { |
| 190 | SELECT count(*) FROM v3; |
| 191 | PRAGMA integrity_check; |
| 192 | } [list [expr {$nVec/2}] ok] |
| 193 | |
| 194 | finish_test |