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 / vec1growth.test

114 lines · 2603 bytes Raw Blame History
1# 2026-03-31
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# This file contains tests for vec1. Specifically, it tests that the
13# index does not grow indefinitely if there are lots of inserts/deletes
14# and updates on the table.
15#
16
17source [file join [file dirname [info script]] vec1_common.tcl]
18set testprefix vec1growth
19vec1_load db
20
21foreach {tn nMeta} {
22 1 0
23 2 1
24 3 2
25} {
26
27 set lMeta [list]
28 set cols ""
29 set exprs ""
30 for {set ii 0} {$ii<$nMeta} {incr ii} {
31 lappend lMeta c$ii
32 append cols ", c$ii"
33 append exprs ", i+$ii"
34 }
35
36 do_execsql_test 1.$tn.0 "
37 DROP TABLE IF EXISTS v1;
38 CREATE VIRTUAL TABLE v1 USING vec1(vec $cols);
39 "
40
41 do_execsql_test 1.1 "
42 WITH s(i) AS (
43 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1_000
44 )
45 INSERT INTO v1(rowid, vec $cols) SELECT i, random_vector(32) $exprs FROM s;
46 "
47
48 do_execsql_test 1.2 {
49 INSERT INTO v1(cmd, arg) VALUES('rebuild', (
50 SELECT vec1_train(vec, '{nbucket:16, codesize:0}') FROM v1
51 ));
52 }
53
54 do_execsql_test 1.2.ic {
55 PRAGMA integrity_check
56 } {ok}
57
58 # Get the original number of bytes of index data.
59 #
60 set nOrig [db one { SELECT sum( length(val) ) FROM v1_idx }]
61 set nMax [expr {($nOrig * 6)/5}]
62
63 proc randint {min max} {
64 expr { int(abs(rand()) * (1+$max-$min)) }
65 }
66
67 for {set iIter 0} {$iIter<20} {incr iIter} {
68 do_test 1.$tn.3.$iIter {
69 for {set ii 1} {$ii <= 1000} {incr ii} {
70 set r [randint 1 1000]
71 execsql { UPDATE v1 SET vec=random_vector(32) WHERE rowid=$r }
72 }
73 set nByte [db one { SELECT sum( length(val) ) FROM v1_idx }]
74 expr $nByte<=$nMax
75 } {1}
76
77 do_execsql_test 1.$tn.3.$iIter.ic.(sz=$nByte,max=$nMax) {
78 PRAGMA integrity_check
79 } {ok}
80 }
81
82}
83
84reset_db
85vec1_load db
86
87do_execsql_test 2.0 {
88 CREATE VIRTUAL TABLE vec USING vec1(a, b);
89 INSERT INTO vec(cmd, arg) VALUES('rebuild', '{index:"flat"}');
90 BEGIN;
91}
92do_test 2.1 {
93 for {set ii 1} {$ii<=128} {incr ii} {
94 execsql { INSERT INTO vec(a, b) VALUES( random_vector(16), NULL ) }
95 }
96} {}
97
98do_test 2.1 {
99 for {set ii 128} {$ii>1} {incr ii -1} {
100 execsql { DELETE FROM vec WHERE rowid=$ii }
101 }
102} {}
103
104do_execsql_test 2.2 {
105 COMMIT;
106 PRAGMA integrity_check;
107} {ok}
108
109do_execsql_test 2.3 {
110 SELECT count(*) FROM vec;
111} 1
112
113finish_test
114