@sixb/sqlite 0.1.15 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-limit-storage.d.ts.map +1 -1
- package/dist/ai-usage-storage.d.ts.map +1 -1
- package/dist/execution-storage.d.ts +28 -0
- package/dist/execution-storage.d.ts.map +1 -1
- package/dist/file-download-grant-storage.d.ts +11 -0
- package/dist/file-download-grant-storage.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1428 -599
- package/dist/index.js.map +29 -27
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/query-compiler.d.ts +3 -0
- package/dist/objects/query-compiler.d.ts.map +1 -1
- package/dist/objects/read-scope.d.ts +2 -0
- package/dist/objects/read-scope.d.ts.map +1 -1
- package/dist/objects/reader.d.ts +1 -0
- package/dist/objects/reader.d.ts.map +1 -1
- package/dist/objects/rows.d.ts +1 -0
- package/dist/objects/rows.d.ts.map +1 -1
- package/dist/objects/storage.d.ts.map +1 -1
- package/dist/ontology-storage/index.d.ts +2 -0
- package/dist/ontology-storage/index.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +17 -37
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-state.d.ts +3 -26
- package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts +3 -6
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/ontology-storage/outbox.d.ts.map +1 -1
- package/dist/ontology-storage/replacement-plans.d.ts +88 -0
- package/dist/ontology-storage/replacement-plans.d.ts.map +1 -0
- package/dist/ontology-storage/shared.d.ts +8 -5
- package/dist/ontology-storage/shared.d.ts.map +1 -1
- package/dist/ontology-storage/source-cleanup.d.ts.map +1 -1
- package/dist/ontology-storage/source-roots.d.ts +15 -2
- package/dist/ontology-storage/source-roots.d.ts.map +1 -1
- package/dist/ontology-storage/sources.d.ts +1 -2
- package/dist/ontology-storage/sources.d.ts.map +1 -1
- package/dist/projection-run-storage.d.ts.map +1 -1
- package/dist/rules-storage.d.ts.map +1 -1
- package/dist/testing.d.ts +4 -1
- package/dist/testing.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/ai-cost-storage.ts +10 -0
- package/src/ai-limit-storage.ts +13 -2
- package/src/ai-usage-storage.ts +9 -1
- package/src/execution-storage.ts +7 -2
- package/src/file-download-grant-storage.ts +76 -0
- package/src/index.ts +10 -3
- package/src/migrations/053-replacement-plans.sql +73 -0
- package/src/migrations/054-slim-ontology-outbox.sql +21 -0
- package/src/migrations/055-commit-touches.sql +31 -0
- package/src/migrations/056-audio-transcription.sql +10 -0
- package/src/migrations/057-reranking-model-kind.sql +16 -0
- package/src/migrations/058-file-download-grants.sql +21 -0
- package/src/migrations/059-drained-source-versions.sql +11 -0
- package/src/migrations.ts +115 -0
- package/src/objects/README.md +19 -0
- package/src/objects/query-compiler.ts +49 -7
- package/src/objects/read-scope.ts +10 -8
- package/src/objects/reader.ts +44 -8
- package/src/objects/rows.ts +1 -0
- package/src/objects/storage.ts +5 -11
- package/src/ontology-storage/index.ts +6 -1
- package/src/ontology-storage/materialization-session.ts +120 -211
- package/src/ontology-storage/materialization-state.ts +5 -261
- package/src/ontology-storage/materialization-writer.ts +11 -13
- package/src/ontology-storage/materializations.ts +133 -210
- package/src/ontology-storage/outbox.ts +50 -16
- package/src/ontology-storage/replacement-plans.ts +1064 -0
- package/src/ontology-storage/shared.ts +17 -12
- package/src/ontology-storage/source-cleanup.ts +24 -9
- package/src/ontology-storage/source-roots.ts +38 -14
- package/src/ontology-storage/sources.ts +0 -26
- package/src/projection-run-storage.ts +7 -1
- package/src/rules-storage.ts +9 -14
- package/src/testing.ts +6 -4
package/src/migrations.ts
CHANGED
|
@@ -121,6 +121,11 @@ import projectionRunSupersessionSql from "./migrations/051-projection-run-supers
|
|
|
121
121
|
import compactSourceStorageSql from "./migrations/052-compact-source-storage.sql" with {
|
|
122
122
|
type: "text",
|
|
123
123
|
}
|
|
124
|
+
import replacementPlansSql from "./migrations/053-replacement-plans.sql" with { type: "text" }
|
|
125
|
+
import slimOntologyOutboxSql from "./migrations/054-slim-ontology-outbox.sql" with { type: "text" }
|
|
126
|
+
import commitTouchesSql from "./migrations/055-commit-touches.sql" with { type: "text" }
|
|
127
|
+
import audioTranscriptionSql from "./migrations/056-audio-transcription.sql" with { type: "text" }
|
|
128
|
+
import rerankingModelKindSql from "./migrations/057-reranking-model-kind.sql" with { type: "text" }
|
|
124
129
|
|
|
125
130
|
const MIGRATIONS_TABLE_SQL = `
|
|
126
131
|
CREATE TABLE IF NOT EXISTS sixb_migrations (
|
|
@@ -246,6 +251,26 @@ export const sqliteStorageMigrations = defineMigrations({
|
|
|
246
251
|
},
|
|
247
252
|
{ checksum: checksum(compactSourceStorageSql) }
|
|
248
253
|
),
|
|
254
|
+
sqliteSql("053-replacement-plans", replacementPlansSql),
|
|
255
|
+
sqliteStep(
|
|
256
|
+
"054-slim-ontology-outbox",
|
|
257
|
+
(db) => {
|
|
258
|
+
// SQLite has no procedural block to refuse the conversion in SQL: check before it.
|
|
259
|
+
const mismatched = unrebuildableOutboxEvents(db)
|
|
260
|
+
if (mismatched > 0) {
|
|
261
|
+
throw new Error(
|
|
262
|
+
`[SixbSqliteStorage] ${mismatched} outbox events would not rebuild from their commit.`
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
db.run(slimOntologyOutboxSql)
|
|
266
|
+
},
|
|
267
|
+
{ checksum: checksum(slimOntologyOutboxSql) }
|
|
268
|
+
),
|
|
269
|
+
sqliteSql("055-commit-touches", commitTouchesSql),
|
|
270
|
+
sqliteSql("056-audio-transcription", audioTranscriptionSql),
|
|
271
|
+
sqliteSql("057-reranking-model-kind", rerankingModelKindSql),
|
|
272
|
+
sqliteSql("058-file-download-grants", fileDownloadGrantsSql),
|
|
273
|
+
sqliteSql("059-drained-source-versions", drainedSourceVersionsSql),
|
|
249
274
|
],
|
|
250
275
|
})
|
|
251
276
|
|
|
@@ -416,6 +441,92 @@ function sourceStorageCounts(db: Database): {
|
|
|
416
441
|
.get() as { readonly versions: number; readonly roots: number; readonly rows: number }
|
|
417
442
|
}
|
|
418
443
|
|
|
444
|
+
/**
|
|
445
|
+
* Stored events that would not rebuild exactly from their commit and execution. Who asked and what
|
|
446
|
+
* wrote are derived from the execution exactly as 047 derived the commit's copies; both sides were
|
|
447
|
+
* written as canonical JSON, so equal JSON compares as equal text.
|
|
448
|
+
*/
|
|
449
|
+
function unrebuildableOutboxEvents(db: Database): number {
|
|
450
|
+
const row = db
|
|
451
|
+
.query(
|
|
452
|
+
`SELECT COUNT(*) AS mismatched
|
|
453
|
+
FROM ontology_outbox AS outbox
|
|
454
|
+
LEFT JOIN ontology_commits AS commits
|
|
455
|
+
ON commits.project_id = outbox.project_id AND commits.id = outbox.commit_id
|
|
456
|
+
LEFT JOIN executions
|
|
457
|
+
ON executions.project_id = commits.project_id AND executions.id = commits.execution_id
|
|
458
|
+
WHERE executions.id IS NULL
|
|
459
|
+
OR outbox.envelope ->> '$.id' IS NOT outbox.id
|
|
460
|
+
OR outbox.envelope ->> '$.commitOrdinal' IS NOT outbox.commit_ordinal
|
|
461
|
+
OR outbox.envelope ->> '$.commitId' IS NOT commits.id
|
|
462
|
+
OR outbox.envelope ->> '$.projectId' IS NOT commits.project_id
|
|
463
|
+
OR outbox.envelope ->> '$.schemaVersion' IS NOT 1
|
|
464
|
+
OR outbox.envelope ->> '$.occurredAt' IS NOT commits.committed_at
|
|
465
|
+
OR outbox.envelope -> '$.origin' IS NOT json(commits.origin)
|
|
466
|
+
OR outbox.envelope ->> '$.correlationId' IS NOT executions.correlation_id
|
|
467
|
+
OR outbox.envelope -> '$.requestedBy' IS NOT CASE
|
|
468
|
+
WHEN executions.requested_by_user_id IS NOT NULL
|
|
469
|
+
THEN json_object('id', executions.requested_by_user_id, 'type', 'user')
|
|
470
|
+
WHEN executions.requested_by_service_account_id IS NOT NULL
|
|
471
|
+
THEN json_object('id', executions.requested_by_service_account_id, 'type', 'serviceAccount')
|
|
472
|
+
END
|
|
473
|
+
OR outbox.envelope -> '$.executor' IS NOT CASE executions.executor_kind
|
|
474
|
+
WHEN 'request' THEN json_object('requestId', executions.executor_id, 'type', 'request')
|
|
475
|
+
WHEN 'agent' THEN json_object('runId', executions.executor_id, 'type', 'agent')
|
|
476
|
+
WHEN 'kernel' THEN json_object(
|
|
477
|
+
'operation',
|
|
478
|
+
CASE executions.authority_kernel_operation
|
|
479
|
+
WHEN 'ontology.recover'
|
|
480
|
+
THEN json_object('recoveryId', executions.executor_id, 'type', 'ontology.recover')
|
|
481
|
+
ELSE json_object('indexingId', executions.executor_id, 'type', 'ontology.indexVectors')
|
|
482
|
+
END,
|
|
483
|
+
'type', 'kernel'
|
|
484
|
+
)
|
|
485
|
+
ELSE json_object(
|
|
486
|
+
'id', executions.authority_primitive_id,
|
|
487
|
+
'kind', executions.executor_kind,
|
|
488
|
+
'runId', executions.executor_id,
|
|
489
|
+
'type', 'primitive'
|
|
490
|
+
)
|
|
491
|
+
END
|
|
492
|
+
OR outbox.envelope ->> '$.topic' IS NOT CASE substr(outbox.envelope ->> '$.type', 1, 4)
|
|
493
|
+
WHEN 'obje' THEN 'objects'
|
|
494
|
+
WHEN 'link' THEN 'links'
|
|
495
|
+
WHEN 'tele' THEN 'telemetry'
|
|
496
|
+
END
|
|
497
|
+
OR outbox.envelope ->> '$.partitionKey' IS NOT CASE outbox.envelope ->> '$.topic'
|
|
498
|
+
WHEN 'objects' THEN (outbox.envelope ->> '$.payload.objectTypeId') || ':' ||
|
|
499
|
+
(outbox.envelope ->> '$.payload.primaryId')
|
|
500
|
+
WHEN 'links' THEN (outbox.envelope ->> '$.payload.sourceTypeId') || ':' ||
|
|
501
|
+
(outbox.envelope ->> '$.payload.sourceId') || ':' ||
|
|
502
|
+
(outbox.envelope ->> '$.payload.linkId')
|
|
503
|
+
WHEN 'telemetry' THEN (outbox.envelope ->> '$.payload.objectTypeId') || ':' ||
|
|
504
|
+
(outbox.envelope ->> '$.payload.objectId') || ':' ||
|
|
505
|
+
(outbox.envelope ->> '$.payload.propertyId')
|
|
506
|
+
END
|
|
507
|
+
OR (
|
|
508
|
+
outbox.envelope ->> '$.type' IN ('object.created', 'link.created')
|
|
509
|
+
AND (
|
|
510
|
+
(SELECT COUNT(*) FROM json_each(outbox.envelope, '$.payload.propertyChanges'))
|
|
511
|
+
IS NOT (SELECT COUNT(*) FROM json_each(outbox.envelope, '$.payload.properties'))
|
|
512
|
+
OR EXISTS (
|
|
513
|
+
SELECT 1
|
|
514
|
+
FROM json_each(outbox.envelope, '$.payload.propertyChanges') AS change
|
|
515
|
+
LEFT JOIN json_each(outbox.envelope, '$.payload.properties') AS property
|
|
516
|
+
ON property.key = change.key
|
|
517
|
+
WHERE property.key IS NULL
|
|
518
|
+
OR change.value ->> '$.operation' IS NOT 'created'
|
|
519
|
+
OR json_type(change.value, '$.after') IS NOT property.type
|
|
520
|
+
OR change.value ->> '$.after' IS NOT property.value
|
|
521
|
+
OR (SELECT COUNT(*) FROM json_each(change.value)) IS NOT 2
|
|
522
|
+
)
|
|
523
|
+
)
|
|
524
|
+
)`
|
|
525
|
+
)
|
|
526
|
+
.get() as { readonly mismatched: number }
|
|
527
|
+
return row.mismatched
|
|
528
|
+
}
|
|
529
|
+
|
|
419
530
|
function checksum(value: string): string {
|
|
420
531
|
return createHash("sha256").update(value).digest("hex")
|
|
421
532
|
}
|
|
@@ -482,3 +593,7 @@ import connectorOptionalPkceSql from "./migrations/039-connector-optional-pkce.s
|
|
|
482
593
|
type: "text",
|
|
483
594
|
}
|
|
484
595
|
import fileUploadSessionsSql from "./migrations/048-file-upload-sessions.sql" with { type: "text" }
|
|
596
|
+
import fileDownloadGrantsSql from "./migrations/058-file-download-grants.sql" with { type: "text" }
|
|
597
|
+
import drainedSourceVersionsSql from "./migrations/059-drained-source-versions.sql" with {
|
|
598
|
+
type: "text",
|
|
599
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Object reads
|
|
2
|
+
|
|
3
|
+
`read-scope.ts` derives visible objects, links and property grants from the selected roots and
|
|
4
|
+
paths. The selected reader holds its traversal-budget probe and terminal statements on one WAL
|
|
5
|
+
snapshot. SQL must filter by those relations before ranking or limiting results.
|
|
6
|
+
|
|
7
|
+
SQLite flattens CTEs. Expose identity columns from the grant/visible relation and put authorized
|
|
8
|
+
parents before indexed lookups with `CROSS JOIN`; exposing stored identities can cause a full
|
|
9
|
+
grant scan per traversed edge. The query-plan regression test checks those lookups.
|
|
10
|
+
|
|
11
|
+
Vector `list()` materializes at most the candidate budget plus one identity/vector pair per
|
|
12
|
+
eligible object. Its count controls distance evaluation and travels with the ranked rows, so
|
|
13
|
+
admission, total and ranking share one scan. Returned objects are hydrated after top-k; candidate
|
|
14
|
+
filters and traversals may still evaluate masked properties while deciding eligibility.
|
|
15
|
+
Overflow fails before exposing rows; it never silently ranks a truncated candidate set.
|
|
16
|
+
|
|
17
|
+
An outer zero limit can suppress the count row; that case uses the separate bounded probe.
|
|
18
|
+
Count, existence and facet terminals also retain that probe. Totals count the top-k set before
|
|
19
|
+
the outer limit, and all candidates must grant every profile source property.
|
|
@@ -24,7 +24,14 @@ export interface SqliteObjectQueryPageRow {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export interface CompiledObjectQuery {
|
|
27
|
-
vectorProbe?: {
|
|
27
|
+
vectorProbe?: {
|
|
28
|
+
sql: string
|
|
29
|
+
args: SqliteValue[]
|
|
30
|
+
limit: number
|
|
31
|
+
/** Maximum returned rows and total before the outermost limit, respectively. */
|
|
32
|
+
rowLimit: number
|
|
33
|
+
totalLimit: number
|
|
34
|
+
}
|
|
28
35
|
sql: string
|
|
29
36
|
args: SqliteValue[]
|
|
30
37
|
totalSql: string
|
|
@@ -98,7 +105,7 @@ export function compileObjectQuery(
|
|
|
98
105
|
if (hasVectorProfile(query) && !isVectorProfileQuery(query)) {
|
|
99
106
|
throw new ObjectQueryExecutionError(
|
|
100
107
|
"unsupported_vector_composition",
|
|
101
|
-
"Vector search supports one profile with filters before ranking and limit/project after."
|
|
108
|
+
"Vector search supports one profile with filters, traversals or sets before ranking and limit/project after."
|
|
102
109
|
)
|
|
103
110
|
}
|
|
104
111
|
const source = options.source ?? DEFAULT_OBJECT_QUERY_SOURCE
|
|
@@ -139,6 +146,11 @@ function compileObjectQueryInternal(
|
|
|
139
146
|
ctx: CompileContext
|
|
140
147
|
): CompiledObjectQuery {
|
|
141
148
|
switch (query.kind) {
|
|
149
|
+
case "rerank":
|
|
150
|
+
throw new ObjectQueryExecutionError(
|
|
151
|
+
"reranking_requires_runtime",
|
|
152
|
+
"Reranking must execute in the model runtime, not storage."
|
|
153
|
+
)
|
|
142
154
|
case "start":
|
|
143
155
|
return compileStart(projectId, query, ctx)
|
|
144
156
|
case "refs":
|
|
@@ -231,9 +243,31 @@ function compileVector(
|
|
|
231
243
|
{ kind: "column", column: "_vector_score", direction: "desc" },
|
|
232
244
|
...identityOrderFields().map((field) => ({ ...field, binary: true })),
|
|
233
245
|
])
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
246
|
+
const rankedOrder = compileOrder(order.fields, "ranked")
|
|
247
|
+
// Materialize only bounded, authorized candidates once. CASE prevents distance evaluation
|
|
248
|
+
// above the envelope; the reader rejects the count before exposing any rows or scores.
|
|
249
|
+
const sql = `SELECT stored.project_id, stored.object_type_id, stored.primary_id,
|
|
250
|
+
stored.properties, stored.properties AS _cursor_properties,
|
|
251
|
+
stored.created_at, stored.updated_at, stored.version, stored.last_commit_id,
|
|
252
|
+
ranked._vector_candidate_count, ranked._vector_score
|
|
253
|
+
FROM (
|
|
254
|
+
WITH _sixb_vector_candidates AS MATERIALIZED (
|
|
255
|
+
SELECT input.project_id, input.object_type_id, input.primary_id,
|
|
256
|
+
vectors.embedding AS _vector_embedding ${candidates} LIMIT ?
|
|
257
|
+
), _sixb_vector_budget AS (
|
|
258
|
+
SELECT COUNT(*) AS _vector_candidate_count FROM _sixb_vector_candidates
|
|
259
|
+
)
|
|
260
|
+
SELECT input.project_id, input.object_type_id, input.primary_id, budget._vector_candidate_count,
|
|
261
|
+
CASE WHEN budget._vector_candidate_count <= ?
|
|
262
|
+
THEN max(-1, min(1, 1 - vec_distance_cosine(input._vector_embedding, ?)))
|
|
263
|
+
END AS _vector_score
|
|
264
|
+
FROM _sixb_vector_candidates AS input CROSS JOIN _sixb_vector_budget AS budget
|
|
265
|
+
ORDER BY ${order.sql} LIMIT ?
|
|
266
|
+
) AS ranked CROSS JOIN ${ctx.source.objectsTable} AS stored
|
|
267
|
+
ON stored.project_id = ranked.project_id AND stored.object_type_id = ranked.object_type_id
|
|
268
|
+
AND stored.primary_id = ranked.primary_id
|
|
269
|
+
ORDER BY ${rankedOrder.sql}`
|
|
270
|
+
const args = [...candidateArgs, limit + 1, limit, encodeSqliteVector(values), query.k]
|
|
237
271
|
return {
|
|
238
272
|
sql,
|
|
239
273
|
args,
|
|
@@ -245,6 +279,8 @@ function compileVector(
|
|
|
245
279
|
sql: `SELECT COUNT(*) AS total FROM (SELECT 1 ${candidates} LIMIT ?) AS candidates`,
|
|
246
280
|
args: [...candidateArgs, limit + 1],
|
|
247
281
|
limit,
|
|
282
|
+
rowLimit: query.k,
|
|
283
|
+
totalLimit: query.k,
|
|
248
284
|
},
|
|
249
285
|
hasMore: () => false,
|
|
250
286
|
trimRows: identityRows,
|
|
@@ -451,7 +487,13 @@ function compileLimit(
|
|
|
451
487
|
FROM (${input.sql}) AS input
|
|
452
488
|
`,
|
|
453
489
|
totalArgs: input.args,
|
|
454
|
-
vectorProbe: input.vectorProbe
|
|
490
|
+
vectorProbe: input.vectorProbe
|
|
491
|
+
? {
|
|
492
|
+
...input.vectorProbe,
|
|
493
|
+
rowLimit: Math.min(input.vectorProbe.rowLimit, rowLimit),
|
|
494
|
+
totalLimit: input.vectorProbe.rowLimit,
|
|
495
|
+
}
|
|
496
|
+
: undefined,
|
|
455
497
|
order: input.order,
|
|
456
498
|
hasMore: (rowCount, total) =>
|
|
457
499
|
total === undefined ? ctx.probeLimit && rowCount > limit : limit < total,
|
|
@@ -852,7 +894,7 @@ function compileProject(
|
|
|
852
894
|
input.created_at,
|
|
853
895
|
input.updated_at,
|
|
854
896
|
input.version,
|
|
855
|
-
input.last_commit_id${input.vectorProbe ? ", input._vector_score" : ""}
|
|
897
|
+
input.last_commit_id${input.vectorProbe ? ", input._vector_score, input._vector_candidate_count" : ""}
|
|
856
898
|
FROM (${input.sql}) AS input
|
|
857
899
|
ORDER BY ${inputOrder.sql}
|
|
858
900
|
`,
|
|
@@ -22,6 +22,8 @@ export interface SqliteSelectedObjectReadSource extends SqliteObjectQuerySource
|
|
|
22
22
|
* CROSS JOIN keeps authorized parents/grants before indexed edge/object lookups, even when
|
|
23
23
|
* statistics would otherwise make SQLite rescan all grants for every stored object.
|
|
24
24
|
* Property grants are grouped only after path-sensitive reachability has been resolved.
|
|
25
|
+
* Expose identities from grants/visible, so flattened joins can seek those relations by key
|
|
26
|
+
* before loading stored values. Exposing stored identities makes traversal rescan grants per edge.
|
|
25
27
|
*/
|
|
26
28
|
export function compileSqliteSelectedObjectReadSource(
|
|
27
29
|
projectId: string,
|
|
@@ -105,7 +107,7 @@ function compileSelectedReadScopeCte(scopeJson: string, projectId: string): Comp
|
|
|
105
107
|
_sixb_scope_root_facts(project_id, root_id, node_id, object_type_id, primary_id) AS (
|
|
106
108
|
SELECT anchor.project_id, root.root_id, root.node_id, root.object_type_id, root.primary_id
|
|
107
109
|
FROM _sixb_scope_roots AS root
|
|
108
|
-
JOIN objects AS anchor
|
|
110
|
+
CROSS JOIN objects AS anchor
|
|
109
111
|
ON anchor.project_id = ?
|
|
110
112
|
AND anchor.object_type_id = root.object_type_id
|
|
111
113
|
AND anchor.primary_id = root.primary_id
|
|
@@ -187,7 +189,7 @@ function compileSelectedReadScopeCte(scopeJson: string, projectId: string): Comp
|
|
|
187
189
|
GROUP BY reachable.project_id, reachable.object_type_id, reachable.primary_id
|
|
188
190
|
),
|
|
189
191
|
_sixb_scope_objects AS (
|
|
190
|
-
SELECT
|
|
192
|
+
SELECT grants.project_id, grants.object_type_id, grants.primary_id,
|
|
191
193
|
COALESCE((
|
|
192
194
|
SELECT json_group_object(property.key, ${sqliteJsonEachValue("property")})
|
|
193
195
|
FROM json_each(stored.properties) AS property
|
|
@@ -277,12 +279,12 @@ function compileSelectedReadScopeCte(scopeJson: string, projectId: string): Comp
|
|
|
277
279
|
),
|
|
278
280
|
_sixb_scope_links AS (
|
|
279
281
|
SELECT
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
282
|
+
visible.project_id,
|
|
283
|
+
visible.source_type_id,
|
|
284
|
+
visible.source_id,
|
|
285
|
+
visible.link_id,
|
|
286
|
+
visible.target_type_id,
|
|
287
|
+
visible.target_id,
|
|
286
288
|
CASE
|
|
287
289
|
WHEN stored.properties IS NULL THEN NULL
|
|
288
290
|
ELSE NULLIF(
|
package/src/objects/reader.ts
CHANGED
|
@@ -54,23 +54,23 @@ export class SqliteObjectReader {
|
|
|
54
54
|
ensureSqliteVectorSearch(this.db)
|
|
55
55
|
const probe = compiled.vectorProbe
|
|
56
56
|
const row = this.db.query<{ total: number }, SqliteValue[]>(probe.sql).get(...probe.args)
|
|
57
|
-
|
|
58
|
-
throw new ObjectQueryExecutionError(
|
|
59
|
-
"vector_work_limit_exceeded",
|
|
60
|
-
`Exact vector search supports at most ${probe.limit} eligible vectors at this dimension. Narrow the query filters.`
|
|
61
|
-
)
|
|
62
|
-
}
|
|
57
|
+
assertVectorCandidateCount(row?.total, probe.limit)
|
|
63
58
|
}
|
|
64
59
|
|
|
65
60
|
queryObjects(params: QueryObjectsInput): QueryObjectsResult {
|
|
66
|
-
this.assertVectorSearch(params)
|
|
67
61
|
const source = this.source
|
|
68
62
|
const compiled = compileObjectQuery(params.projectId, params.query, {
|
|
69
63
|
includeTotal: params.includeTotal,
|
|
70
64
|
source,
|
|
71
65
|
})
|
|
72
|
-
|
|
66
|
+
if (compiled.vectorProbe) ensureSqliteVectorSearch(this.db)
|
|
67
|
+
const ordinaryTotal =
|
|
68
|
+
params.includeTotal === false || compiled.vectorProbe
|
|
69
|
+
? undefined
|
|
70
|
+
: readTotal(this.db, compiled)
|
|
73
71
|
const rawRows = this.db.query(compiled.sql).all(...compiled.args) as ObjectQueryDatabaseRow[]
|
|
72
|
+
const vectorTotal = this.vectorResultTotal(compiled, rawRows)
|
|
73
|
+
const total = params.includeTotal === false ? undefined : (vectorTotal ?? ordinaryTotal)
|
|
74
74
|
const rows = compiled.trimRows(rawRows) as readonly ObjectQueryDatabaseRow[]
|
|
75
75
|
const hasMore =
|
|
76
76
|
total === undefined && compiled.hasMoreProbe
|
|
@@ -87,6 +87,26 @@ export class SqliteObjectReader {
|
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
private vectorResultTotal(
|
|
91
|
+
compiled: CompiledObjectQuery,
|
|
92
|
+
rows: readonly ObjectQueryDatabaseRow[]
|
|
93
|
+
): number | undefined {
|
|
94
|
+
const probe = compiled.vectorProbe
|
|
95
|
+
if (!probe) return undefined
|
|
96
|
+
|
|
97
|
+
// A zero outer limit can hide the count row. Keep admission and total semantics for that
|
|
98
|
+
// uncommon case, without rescanning candidates for ordinary vector queries.
|
|
99
|
+
let count = rows[0]?._vector_candidate_count
|
|
100
|
+
if (rows.length === 0) {
|
|
101
|
+
count =
|
|
102
|
+
probe.rowLimit === 0
|
|
103
|
+
? this.db.query<{ total: number }, SqliteValue[]>(probe.sql).get(...probe.args)?.total
|
|
104
|
+
: 0
|
|
105
|
+
}
|
|
106
|
+
assertVectorCandidateCount(count, probe.limit)
|
|
107
|
+
return Math.min(count, probe.totalLimit)
|
|
108
|
+
}
|
|
109
|
+
|
|
90
110
|
countObjects(params: CountObjectsInput): CountObjectsResult {
|
|
91
111
|
this.assertVectorSearch(params)
|
|
92
112
|
const source = this.source
|
|
@@ -116,6 +136,7 @@ export class SqliteObjectReader {
|
|
|
116
136
|
source,
|
|
117
137
|
})
|
|
118
138
|
return {
|
|
139
|
+
total: readTotal(this.db, compiled),
|
|
119
140
|
facets: params.facets.map((facet) => ({
|
|
120
141
|
propertyId: facet.propertyId,
|
|
121
142
|
buckets: readFacetBuckets(this.db, compiled, facet),
|
|
@@ -519,6 +540,21 @@ function sqliteFacetValue(valueType: string | null, value: unknown): unknown {
|
|
|
519
540
|
return value
|
|
520
541
|
}
|
|
521
542
|
|
|
543
|
+
function assertVectorCandidateCount(count: unknown, limit: number): asserts count is number {
|
|
544
|
+
if (typeof count !== "number" || !Number.isSafeInteger(count) || count < 0) {
|
|
545
|
+
throw new ObjectQueryExecutionError(
|
|
546
|
+
"invalid_vector_candidate_count",
|
|
547
|
+
"Storage did not return a valid vector candidate count."
|
|
548
|
+
)
|
|
549
|
+
}
|
|
550
|
+
if (count > limit) {
|
|
551
|
+
throw new ObjectQueryExecutionError(
|
|
552
|
+
"vector_work_limit_exceeded",
|
|
553
|
+
`Exact vector search supports at most ${limit} eligible vectors at this dimension. Narrow the query filters.`
|
|
554
|
+
)
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
522
558
|
function existsProbeQuery(query: ObjectQuery): ObjectQuery {
|
|
523
559
|
return { kind: "limit", limit: 1, input: stripOuterRowShape(query) }
|
|
524
560
|
}
|
package/src/objects/rows.ts
CHANGED
|
@@ -107,6 +107,7 @@ export interface DatabaseRow {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
export interface ObjectQueryDatabaseRow extends DatabaseRow {
|
|
110
|
+
_vector_candidate_count?: number
|
|
110
111
|
_vector_score?: number
|
|
111
112
|
_cursor_properties?: string
|
|
112
113
|
/** `json_object(linkId, value, ...)` serialized text from an `expand` pushdown; absent otherwise. */
|
package/src/objects/storage.ts
CHANGED
|
@@ -161,9 +161,7 @@ export class SqliteObjectStorage implements ObjectStorage {
|
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
async facetObjects(params: FacetObjectsInput): Promise<FacetObjectsResult> {
|
|
164
|
-
|
|
165
|
-
return runDeferredReadTransaction(this.db, () => this.reader.facetObjects(params))
|
|
166
|
-
return this.reader.facetObjects(params)
|
|
164
|
+
return runDeferredReadTransaction(this.db, () => this.reader.facetObjects(params))
|
|
167
165
|
}
|
|
168
166
|
|
|
169
167
|
async getByPrimaryId(params: {
|
|
@@ -252,23 +250,19 @@ export class SqliteObjectStorage implements ObjectStorage {
|
|
|
252
250
|
limit: number
|
|
253
251
|
}): Promise<{ objects: readonly ObjectRow[]; nextPrimaryId?: string }> {
|
|
254
252
|
assertReconciliationPageLimit(params.limit)
|
|
253
|
+
// A cursor written as `? IS NULL OR …` cannot seek, so every page would rescan from the start.
|
|
254
|
+
const cursor = params.afterPrimaryId === undefined ? [] : [params.afterPrimaryId]
|
|
255
255
|
const rows = this.db
|
|
256
256
|
.query(
|
|
257
257
|
`
|
|
258
258
|
SELECT * FROM objects
|
|
259
259
|
WHERE project_id = ? AND object_type_id = ?
|
|
260
|
-
|
|
260
|
+
${cursor.length > 0 ? "AND primary_id > ?" : ""}
|
|
261
261
|
ORDER BY primary_id ASC
|
|
262
262
|
LIMIT ?
|
|
263
263
|
`
|
|
264
264
|
)
|
|
265
|
-
.all(
|
|
266
|
-
params.projectId,
|
|
267
|
-
params.objectTypeId,
|
|
268
|
-
params.afterPrimaryId ?? null,
|
|
269
|
-
params.afterPrimaryId ?? null,
|
|
270
|
-
params.limit + 1
|
|
271
|
-
) as DatabaseRow[]
|
|
265
|
+
.all(params.projectId, params.objectTypeId, ...cursor, params.limit + 1) as DatabaseRow[]
|
|
272
266
|
const hasMore = rows.length > params.limit
|
|
273
267
|
const objects = rows.slice(0, params.limit).map((row) => rowToObject(row))
|
|
274
268
|
const last = objects.at(-1)
|
|
@@ -4,9 +4,9 @@ import { SqliteOntologyCommitStorage } from "./commits"
|
|
|
4
4
|
import type { SqliteOntologyTransactionContext } from "./materialization-session"
|
|
5
5
|
import { SqliteOntologyMaterializationStorage } from "./materializations"
|
|
6
6
|
import { SqliteOntologyOutboxStorage } from "./outbox"
|
|
7
|
+
import { SqliteOntologyReplacementPlanStorage } from "./replacement-plans"
|
|
7
8
|
import type { SqliteRootOperation } from "./shared"
|
|
8
9
|
import { SqliteOntologySourceStorage } from "./sources"
|
|
9
|
-
|
|
10
10
|
import { SqliteVectorIndexingStorage } from "./vector-indexing"
|
|
11
11
|
import { SqliteOntologyVectorStorage } from "./vectors"
|
|
12
12
|
|
|
@@ -14,6 +14,7 @@ export class SqliteOntologyStorage implements OntologyStorage {
|
|
|
14
14
|
readonly commits: SqliteOntologyCommitStorage
|
|
15
15
|
readonly sources: SqliteOntologySourceStorage
|
|
16
16
|
readonly materializations: SqliteOntologyMaterializationStorage
|
|
17
|
+
readonly replacementPlans: SqliteOntologyReplacementPlanStorage
|
|
17
18
|
readonly vectorIndexing: SqliteVectorIndexingStorage
|
|
18
19
|
readonly vectors: SqliteOntologyVectorStorage
|
|
19
20
|
readonly outbox: SqliteOntologyOutboxStorage
|
|
@@ -29,6 +30,10 @@ export class SqliteOntologyStorage implements OntologyStorage {
|
|
|
29
30
|
input.db,
|
|
30
31
|
input.transactionContext
|
|
31
32
|
)
|
|
33
|
+
this.replacementPlans = new SqliteOntologyReplacementPlanStorage(
|
|
34
|
+
input.db,
|
|
35
|
+
input.runRootOperation
|
|
36
|
+
)
|
|
32
37
|
this.vectorIndexing = new SqliteVectorIndexingStorage(
|
|
33
38
|
input.db,
|
|
34
39
|
input.runRootOperation,
|