interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / vec1 / test / vec1binary2.test

95 lines · 1965 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
14source [file join [file dirname [info script]] vec1_common.tcl]
15set testprefix vec1binary2
16
17vec1_load db
18expr srand(0)
19
20proc random_item {L} {
21 set ii [expr {int(abs(rand()) * [llength $L])}]
22 lindex $L $ii
23}
24
25proc random_binvec {n {opts {1 -1}}} {
26 set lRet [list]
27 for {set ii 0} {$ii < $n} {incr ii} {
28 lappend lRet [random_item $opts]
29 }
30
31 binary format f$n $lRet
32}
33
34db func random_binvec random_binvec
35
36set nQuery 100
37
38foreach {tn N opts nbucket} {
39
40 1 8 {1 -1} 0
41 2 16 {1 -1} 0
42 3 24 {1 -1} 0
43 4 29 {1 -1} 0
44 5 9 {1 -1} 0
45 6 201 {1 -1} 0
46
47 7 8 {2 3} 1
48 8 33 {-400 -155} 1
49} {
50
51 do_execsql_test 1.$tn.0 {
52 DROP TABLE IF EXISTS v1;
53
54 CREATE VIRTUAL TABLE v1 USING vec1(v);
55 WITH s(i) AS (
56 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<5
57 )
58 INSERT INTO v1(v) SELECT random_binvec($N, $opts) FROM s;
59 }
60
61 set spec "{quantizer:\"bq\", nbucket:$nbucket, wht:0}"
62 do_execsql_test 1.$tn.1 {
63 INSERT INTO v1(cmd, arg) VALUES('rebuild', (
64 SELECT vec1_train(v, $spec) FROM v1
65 ));
66 } {}
67
68 do_execsql_test 1.$tn.2 {
69 PRAGMA integrity_check
70 } {ok}
71
72 for {set j 0} {$j < $nQuery} {incr j} {
73 set q [random_binvec $N $opts]
74 set expect [db one {
75 SELECT r FROM (
76 SELECT group_concat(rowid, ' ') AS r, vec1_l2_distance($q, v) FROM v1
77 GROUP BY 2
78 ORDER BY 2 ASC
79 LIMIT 1
80 )
81 }]
82
83 set nExpect [llength $expect]
84
85 do_execsql_test 1.$tn.2.$j. {
86 SELECT r FROM (
87 SELECT rowid AS r FROM v1($q, $nExpect)
88 ) ORDER BY 1
89 } $expect
90 }
91
92}
93
94finish_test
95