Skip to content

Commit 5caac29

Browse files
committed
wip(knowledge-turso): a narrow record-only scope goes straight to the exact scan
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016WV4cwPhGV4g9p1BGQaZbP
1 parent e42520c commit 5caac29

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

‎packages/plugins/knowledge-turso/src/turso-adapter.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ const VECTOR_MIN_POOL = 8;
148148
*/
149149
const VECTOR_RETRY_POOL = 10;
150150

151+
/**
152+
* How many times the floor ({@link VECTOR_MIN_POOL} × topK) the first
153+
* approximate pass must be expected to keep in scope for a scope of record
154+
* terms alone to take that pass: the `topK × vectorIndexOverfetch`
155+
* candidates times the share of the passages in scope, which a count of the
156+
* rows in scope, served by the record index and stopped at the cut-over,
157+
* reads. A narrower scope goes straight to the exact scan, which the record
158+
* index serves from the rows in scope: the one-bit pass, which reads every
159+
* row of the table, would mostly leave a pool under the floor and be followed
160+
* by that scan anyway. PLACEHOLDER — placed by the bench.
161+
*/
162+
const VECTOR_RECORD_SCOPE_POOL = 1.5;
163+
151164
/** How many passages one batch of a vector index's copy writes: each batch is a transaction of its own, so a search waits at most one batch. */
152165
const VECTOR_BACKFILL_BATCH = 2048;
153166

@@ -811,6 +824,22 @@ export class TursoKnowledgeAdapter implements IKnowledgeAdapter {
811824
if (terms.length > 0) await this.ensureKeywordIndex(tableName);
812825
const instance = now.active.createdAt;
813826
if (vectorLeg && (await this.approximateSearchable(tableName, instance))) {
827+
const candidates = topK * this.vectorIndexOverfetch;
828+
// A scope of record terms alone too narrow for the first pass to be
829+
// expected to clear the floor (VECTOR_RECORD_SCOPE_POOL) goes straight
830+
// to the exact scan, which the record index serves from the rows in
831+
// scope. The count reads those rows' index entries, at most up to the cut-over.
832+
if (recordScope) {
833+
const cut = (VECTOR_RECORD_SCOPE_POOL * VECTOR_MIN_POOL) / this.vectorIndexOverfetch;
834+
const counted = await client.execute({
835+
sql: `WITH total AS (SELECT count(*) AS n FROM ${table})
836+
SELECT n, (SELECT count(*) FROM (SELECT 1 FROM ${table} WHERE ${inScope}
837+
LIMIT (SELECT CAST(n * ?${args.length + 1} AS INTEGER) + 1 FROM total))) AS k FROM total`,
838+
args: [...args, cut] as InArgs,
839+
});
840+
const { n, k } = counted.rows[0];
841+
if (Number(k) < Number(n) * cut) return client.execute(statement());
842+
}
814843
// A failed approximate statement (its index gone under this process's
815844
// memory of it) forgets that memory and is answered by the exact scan.
816845
const approximate = (candidates: number) =>
@@ -826,7 +855,6 @@ export class TursoKnowledgeAdapter implements IKnowledgeAdapter {
826855
const poolOf = (near: ResultSet) => Number(near.rows[0]?.pool ?? 0);
827856
const stands = (near: ResultSet) =>
828857
poolOf(near) >= VECTOR_MIN_POOL * topK && near.rows.filter((r) => r.vpos !== null).length >= topK;
829-
const candidates = topK * this.vectorIndexOverfetch;
830858
const near = await approximate(candidates);
831859
if (near === undefined) return client.execute(statement());
832860
if (stands(near)) return near;

0 commit comments

Comments
 (0)