Skip to content

Commit b7fc459

Browse files
asg017claude
andcommitted
Enable auxiliary columns for rescore, IVF, and DiskANN indexes
The constructor previously rejected auxiliary columns (+col) for all non-flat index types. Analysis confirms all code paths already handle aux columns correctly — aux data lives in _auxiliary shadow table, independent of the vector index structures. Remove the three auxiliary column guards. Metadata and partition key guards remain in place (separate analysis needed). Adds 8 snapshot-based tests covering shadow table creation, insert+KNN returning aux values, aux UPDATE, aux DELETE cleanup, and DROP TABLE for both rescore and DiskANN. IVF aux verified with IVF-enabled build. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 01b4b2a commit b7fc459

6 files changed

Lines changed: 597 additions & 37 deletions

File tree

sqlite-vec.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5149,11 +5149,6 @@ static int vec0_init(sqlite3 *db, void *pAux, int argc, const char *const *argv,
51495149
}
51505150
}
51515151
if (hasRescore) {
5152-
if (numAuxiliaryColumns > 0) {
5153-
*pzErr = sqlite3_mprintf(VEC_CONSTRUCTOR_ERROR
5154-
"Auxiliary columns are not supported with rescore indexes");
5155-
goto error;
5156-
}
51575152
if (numMetadataColumns > 0) {
51585153
*pzErr = sqlite3_mprintf(VEC_CONSTRUCTOR_ERROR
51595154
"Metadata columns are not supported with rescore indexes");
@@ -5183,11 +5178,6 @@ static int vec0_init(sqlite3 *db, void *pAux, int argc, const char *const *argv,
51835178
"partition key columns are not supported with IVF indexes");
51845179
goto error;
51855180
}
5186-
if (numAuxiliaryColumns > 0) {
5187-
*pzErr = sqlite3_mprintf(VEC_CONSTRUCTOR_ERROR
5188-
"auxiliary columns are not supported with IVF indexes");
5189-
goto error;
5190-
}
51915181
if (numMetadataColumns > 0) {
51925182
*pzErr = sqlite3_mprintf(VEC_CONSTRUCTOR_ERROR
51935183
"metadata columns are not supported with IVF indexes");
@@ -5199,12 +5189,6 @@ static int vec0_init(sqlite3 *db, void *pAux, int argc, const char *const *argv,
51995189
// DiskANN columns cannot coexist with aux/metadata/partition columns
52005190
for (int i = 0; i < numVectorColumns; i++) {
52015191
if (pNew->vector_columns[i].index_type == VEC0_INDEX_TYPE_DISKANN) {
5202-
if (numAuxiliaryColumns > 0) {
5203-
*pzErr = sqlite3_mprintf(
5204-
VEC_CONSTRUCTOR_ERROR
5205-
"Auxiliary columns are not supported with DiskANN-indexed vector columns");
5206-
goto error;
5207-
}
52085192
if (numMetadataColumns > 0) {
52095193
*pzErr = sqlite3_mprintf(
52105194
VEC_CONSTRUCTOR_ERROR

tests/__snapshots__/test-auxiliary.ambr

Lines changed: 371 additions & 0 deletions
Large diffs are not rendered by default.

tests/test-auxiliary.py

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sqlite3
2-
from helpers import exec, vec0_shadow_table_contents
2+
import struct
3+
import pytest
4+
from helpers import exec, vec0_shadow_table_contents, _f32
35

46

57
def test_constructor_limit(db, snapshot):
@@ -126,3 +128,198 @@ def test_knn(db, snapshot):
126128
) == snapshot(name="illegal KNN w/ aux")
127129

128130

131+
# ======================================================================
132+
# Auxiliary columns with non-flat indexes
133+
# ======================================================================
134+
135+
136+
def test_rescore_aux_shadow_tables(db, snapshot):
137+
"""Rescore + aux column: verify shadow tables are created correctly."""
138+
db.execute(
139+
"CREATE VIRTUAL TABLE t USING vec0("
140+
" emb float[128] indexed by rescore(quantizer=bit),"
141+
" +label text,"
142+
" +score float"
143+
")"
144+
)
145+
assert exec(db, "SELECT name, sql FROM sqlite_master WHERE type='table' AND name LIKE 't_%' ORDER BY name") == snapshot(
146+
name="rescore aux shadow tables"
147+
)
148+
149+
150+
def test_rescore_aux_insert_knn(db, snapshot):
151+
"""Insert with aux data, KNN should return aux column values."""
152+
db.execute(
153+
"CREATE VIRTUAL TABLE t USING vec0("
154+
" emb float[128] indexed by rescore(quantizer=bit),"
155+
" +label text"
156+
")"
157+
)
158+
import random
159+
random.seed(77)
160+
data = [
161+
("alpha", [random.gauss(0, 1) for _ in range(128)]),
162+
("beta", [random.gauss(0, 1) for _ in range(128)]),
163+
("gamma", [random.gauss(0, 1) for _ in range(128)]),
164+
]
165+
for label, vec in data:
166+
db.execute(
167+
"INSERT INTO t(emb, label) VALUES (?, ?)",
168+
[_f32(vec), label],
169+
)
170+
171+
assert exec(db, "SELECT rowid, label FROM t ORDER BY rowid") == snapshot(
172+
name="rescore aux select all"
173+
)
174+
assert vec0_shadow_table_contents(db, "t", skip_info=True) == snapshot(
175+
name="rescore aux shadow contents"
176+
)
177+
178+
# KNN should include aux column, "alpha" closest to its own vector
179+
rows = db.execute(
180+
"SELECT label, distance FROM t WHERE emb MATCH ? ORDER BY distance LIMIT 3",
181+
[_f32(data[0][1])],
182+
).fetchall()
183+
assert len(rows) == 3
184+
assert rows[0][0] == "alpha"
185+
186+
187+
def test_rescore_aux_update(db):
188+
"""UPDATE aux column on rescore table should work without affecting vectors."""
189+
db.execute(
190+
"CREATE VIRTUAL TABLE t USING vec0("
191+
" emb float[128] indexed by rescore(quantizer=bit),"
192+
" +label text"
193+
")"
194+
)
195+
import random
196+
random.seed(88)
197+
vec = [random.gauss(0, 1) for _ in range(128)]
198+
db.execute("INSERT INTO t(rowid, emb, label) VALUES (1, ?, 'original')", [_f32(vec)])
199+
db.execute("UPDATE t SET label = 'updated' WHERE rowid = 1")
200+
201+
assert db.execute("SELECT label FROM t WHERE rowid = 1").fetchone()[0] == "updated"
202+
203+
# KNN still works with updated aux
204+
rows = db.execute(
205+
"SELECT rowid, label FROM t WHERE emb MATCH ? ORDER BY distance LIMIT 1",
206+
[_f32(vec)],
207+
).fetchall()
208+
assert rows[0][0] == 1
209+
assert rows[0][1] == "updated"
210+
211+
212+
def test_rescore_aux_delete(db, snapshot):
213+
"""DELETE should remove aux data from shadow table."""
214+
db.execute(
215+
"CREATE VIRTUAL TABLE t USING vec0("
216+
" emb float[128] indexed by rescore(quantizer=bit),"
217+
" +label text"
218+
")"
219+
)
220+
import random
221+
random.seed(99)
222+
for i in range(5):
223+
db.execute(
224+
"INSERT INTO t(rowid, emb, label) VALUES (?, ?, ?)",
225+
[i + 1, _f32([random.gauss(0, 1) for _ in range(128)]), f"item-{i+1}"],
226+
)
227+
228+
db.execute("DELETE FROM t WHERE rowid = 3")
229+
230+
assert exec(db, "SELECT rowid, label FROM t ORDER BY rowid") == snapshot(
231+
name="rescore aux after delete"
232+
)
233+
assert exec(db, "SELECT rowid, value00 FROM t_auxiliary ORDER BY rowid") == snapshot(
234+
name="rescore aux shadow after delete"
235+
)
236+
237+
238+
def test_diskann_aux_shadow_tables(db, snapshot):
239+
"""DiskANN + aux column: verify shadow tables are created correctly."""
240+
db.execute("""
241+
CREATE VIRTUAL TABLE t USING vec0(
242+
emb float[8] INDEXED BY diskann(neighbor_quantizer=binary, n_neighbors=8),
243+
+label text,
244+
+score float
245+
)
246+
""")
247+
assert exec(db, "SELECT name, sql FROM sqlite_master WHERE type='table' AND name LIKE 't_%' ORDER BY name") == snapshot(
248+
name="diskann aux shadow tables"
249+
)
250+
251+
252+
def test_diskann_aux_insert_knn(db, snapshot):
253+
"""DiskANN + aux: insert, KNN, verify aux values returned."""
254+
db.execute("""
255+
CREATE VIRTUAL TABLE t USING vec0(
256+
emb float[8] INDEXED BY diskann(neighbor_quantizer=binary, n_neighbors=8),
257+
+label text
258+
)
259+
""")
260+
data = [
261+
("red", [1, 0, 0, 0, 0, 0, 0, 0]),
262+
("green", [0, 1, 0, 0, 0, 0, 0, 0]),
263+
("blue", [0, 0, 1, 0, 0, 0, 0, 0]),
264+
]
265+
for label, vec in data:
266+
db.execute("INSERT INTO t(emb, label) VALUES (?, ?)", [_f32(vec), label])
267+
268+
assert exec(db, "SELECT rowid, label FROM t ORDER BY rowid") == snapshot(
269+
name="diskann aux select all"
270+
)
271+
assert vec0_shadow_table_contents(db, "t", skip_info=True) == snapshot(
272+
name="diskann aux shadow contents"
273+
)
274+
275+
rows = db.execute(
276+
"SELECT label, distance FROM t WHERE emb MATCH ? AND k = 3",
277+
[_f32([1, 0, 0, 0, 0, 0, 0, 0])],
278+
).fetchall()
279+
assert len(rows) >= 1
280+
assert rows[0][0] == "red"
281+
282+
283+
def test_diskann_aux_update_and_delete(db, snapshot):
284+
"""DiskANN + aux: update aux column, delete row, verify cleanup."""
285+
db.execute("""
286+
CREATE VIRTUAL TABLE t USING vec0(
287+
emb float[8] INDEXED BY diskann(neighbor_quantizer=binary, n_neighbors=8),
288+
+label text
289+
)
290+
""")
291+
for i in range(5):
292+
vec = [0.0] * 8
293+
vec[i % 8] = 1.0
294+
db.execute(
295+
"INSERT INTO t(rowid, emb, label) VALUES (?, ?, ?)",
296+
[i + 1, _f32(vec), f"item-{i+1}"],
297+
)
298+
299+
db.execute("UPDATE t SET label = 'UPDATED' WHERE rowid = 2")
300+
db.execute("DELETE FROM t WHERE rowid = 3")
301+
302+
assert exec(db, "SELECT rowid, label FROM t ORDER BY rowid") == snapshot(
303+
name="diskann aux after update+delete"
304+
)
305+
assert exec(db, "SELECT rowid, value00 FROM t_auxiliary ORDER BY rowid") == snapshot(
306+
name="diskann aux shadow after update+delete"
307+
)
308+
309+
310+
def test_diskann_aux_drop_cleans_all(db):
311+
"""DROP TABLE should remove aux shadow table too."""
312+
db.execute("""
313+
CREATE VIRTUAL TABLE t USING vec0(
314+
emb float[8] INDEXED BY diskann(neighbor_quantizer=binary, n_neighbors=8),
315+
+label text
316+
)
317+
""")
318+
db.execute("INSERT INTO t(emb, label) VALUES (?, 'test')", [_f32([1]*8)])
319+
db.execute("DROP TABLE t")
320+
321+
tables = [r[0] for r in db.execute(
322+
"SELECT name FROM sqlite_master WHERE name LIKE 't_%'"
323+
).fetchall()]
324+
assert "t_auxiliary" not in tables
325+

tests/test-diskann.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,16 +630,19 @@ def test_diskann_command_search_list_size_error(db):
630630
# Error cases: DiskANN + auxiliary/metadata/partition columns
631631
# ======================================================================
632632

633-
def test_diskann_create_error_with_auxiliary_column(db):
634-
"""DiskANN tables should not support auxiliary columns."""
635-
result = exec(db, """
633+
def test_diskann_create_with_auxiliary_column(db):
634+
"""DiskANN tables should support auxiliary columns."""
635+
db.execute("""
636636
CREATE VIRTUAL TABLE t USING vec0(
637637
emb float[64] INDEXED BY diskann(neighbor_quantizer=binary),
638638
+extra text
639639
)
640640
""")
641-
assert "error" in result
642-
assert "auxiliary" in result["message"].lower() or "Auxiliary" in result["message"]
641+
# Auxiliary shadow table should exist
642+
tables = [r[0] for r in db.execute(
643+
"SELECT name FROM sqlite_master WHERE name LIKE 't_%' ORDER BY 1"
644+
).fetchall()]
645+
assert "t_auxiliary" in tables
643646

644647

645648
def test_diskann_create_error_with_metadata_column(db):

tests/test-ivf-mutations.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ def test_update_vector_via_delete_insert(db):
203203
# ============================================================================
204204

205205

206-
def test_error_ivf_with_auxiliary_column(db):
207-
result = exec(
208-
db,
209-
"CREATE VIRTUAL TABLE t USING vec0(v float[4] indexed by ivf(), +extra text)",
206+
def test_ivf_with_auxiliary_column(db):
207+
"""IVF should support auxiliary columns."""
208+
db.execute(
209+
"CREATE VIRTUAL TABLE t USING vec0(v float[4] indexed by ivf(), +extra text)"
210210
)
211-
assert "error" in result
212-
assert "auxiliary" in result.get("message", "").lower()
211+
tables = [r[0] for r in db.execute(
212+
"SELECT name FROM sqlite_master WHERE name LIKE 't_%' ORDER BY 1"
213+
).fetchall()]
214+
assert "t_auxiliary" in tables
213215

214216

215217
def test_error_ivf_with_metadata_column(db):

tests/test-rescore-mutations.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ def unpack_float_vec(blob):
3232
# ============================================================================
3333

3434

35-
def test_create_error_with_aux_column(db):
36-
"""Rescore should reject auxiliary columns."""
37-
with pytest.raises(sqlite3.OperationalError, match="Auxiliary columns"):
38-
db.execute(
39-
"CREATE VIRTUAL TABLE t USING vec0("
40-
" embedding float[8] indexed by rescore(quantizer=bit),"
41-
" +extra text"
42-
")"
43-
)
35+
def test_create_with_aux_column(db):
36+
"""Rescore should support auxiliary columns."""
37+
db.execute(
38+
"CREATE VIRTUAL TABLE t USING vec0("
39+
" embedding float[128] indexed by rescore(quantizer=bit),"
40+
" +extra text"
41+
")"
42+
)
43+
tables = [r[0] for r in db.execute(
44+
"SELECT name FROM sqlite_master WHERE name LIKE 't_%' ORDER BY 1"
45+
).fetchall()]
46+
assert "t_auxiliary" in tables
4447

4548

4649
def test_create_error_with_metadata_column(db):

0 commit comments

Comments
 (0)