@minnowdb/core 0.0.1 → 0.1.1
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/LICENSE +21 -0
- package/README.md +38 -1
- package/dist/block-format/block.d.ts +46 -0
- package/dist/block-format/block.d.ts.map +1 -0
- package/dist/block-format/block.js +244 -0
- package/dist/block-format/block.js.map +1 -0
- package/dist/block-format/checksum.d.ts +7 -0
- package/dist/block-format/checksum.d.ts.map +1 -0
- package/dist/block-format/checksum.js +56 -0
- package/dist/block-format/checksum.js.map +1 -0
- package/dist/block-format/codecs.d.ts +24 -0
- package/dist/block-format/codecs.d.ts.map +1 -0
- package/dist/block-format/codecs.js +102 -0
- package/dist/block-format/codecs.js.map +1 -0
- package/dist/block-format/column.d.ts +8 -0
- package/dist/block-format/column.d.ts.map +1 -0
- package/dist/block-format/column.js +166 -0
- package/dist/block-format/column.js.map +1 -0
- package/dist/block-format/index.d.ts +7 -0
- package/dist/block-format/index.d.ts.map +1 -0
- package/dist/block-format/index.js +7 -0
- package/dist/block-format/index.js.map +1 -0
- package/dist/block-format/physical.d.ts +20 -0
- package/dist/block-format/physical.d.ts.map +1 -0
- package/dist/block-format/physical.js +439 -0
- package/dist/block-format/physical.js.map +1 -0
- package/dist/block-format/types.d.ts +93 -0
- package/dist/block-format/types.d.ts.map +1 -0
- package/dist/block-format/types.js +2 -0
- package/dist/block-format/types.js.map +1 -0
- package/dist/engine/artifact-cache.d.ts +22 -0
- package/dist/engine/artifact-cache.d.ts.map +1 -0
- package/dist/engine/artifact-cache.js +68 -0
- package/dist/engine/artifact-cache.js.map +1 -0
- package/dist/engine/batch.d.ts +28 -0
- package/dist/engine/batch.d.ts.map +1 -0
- package/dist/engine/batch.js +38 -0
- package/dist/engine/batch.js.map +1 -0
- package/dist/engine/buffered-writer.d.ts +43 -0
- package/dist/engine/buffered-writer.d.ts.map +1 -0
- package/dist/engine/buffered-writer.js +158 -0
- package/dist/engine/buffered-writer.js.map +1 -0
- package/dist/engine/catalog.d.ts +82 -0
- package/dist/engine/catalog.d.ts.map +1 -0
- package/dist/engine/catalog.js +47 -0
- package/dist/engine/catalog.js.map +1 -0
- package/dist/engine/client.d.ts +211 -0
- package/dist/engine/client.d.ts.map +1 -0
- package/dist/engine/client.js +538 -0
- package/dist/engine/client.js.map +1 -0
- package/dist/engine/coordinator.d.ts +17 -0
- package/dist/engine/coordinator.d.ts.map +1 -0
- package/dist/engine/coordinator.js +60 -0
- package/dist/engine/coordinator.js.map +1 -0
- package/dist/engine/database.d.ts +661 -0
- package/dist/engine/database.d.ts.map +1 -0
- package/dist/engine/database.js +10313 -0
- package/dist/engine/database.js.map +1 -0
- package/dist/engine/defaults.d.ts +33 -0
- package/dist/engine/defaults.d.ts.map +1 -0
- package/dist/engine/defaults.js +87 -0
- package/dist/engine/defaults.js.map +1 -0
- package/dist/engine/errors.d.ts +55 -0
- package/dist/engine/errors.d.ts.map +1 -0
- package/dist/engine/errors.js +81 -0
- package/dist/engine/errors.js.map +1 -0
- package/dist/engine/fts.d.ts +93 -0
- package/dist/engine/fts.d.ts.map +1 -0
- package/dist/engine/fts.js +273 -0
- package/dist/engine/fts.js.map +1 -0
- package/dist/engine/group-index.d.ts +32 -0
- package/dist/engine/group-index.d.ts.map +1 -0
- package/dist/engine/group-index.js +366 -0
- package/dist/engine/group-index.js.map +1 -0
- package/dist/engine/index.d.ts +13 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +12 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/join-index.d.ts +12 -0
- package/dist/engine/join-index.d.ts.map +1 -0
- package/dist/engine/join-index.js +217 -0
- package/dist/engine/join-index.js.map +1 -0
- package/dist/engine/live.d.ts +86 -0
- package/dist/engine/live.d.ts.map +1 -0
- package/dist/engine/live.js +289 -0
- package/dist/engine/live.js.map +1 -0
- package/dist/engine/memory.d.ts +37 -0
- package/dist/engine/memory.d.ts.map +1 -0
- package/dist/engine/memory.js +119 -0
- package/dist/engine/memory.js.map +1 -0
- package/dist/engine/optimizer.d.ts +20 -0
- package/dist/engine/optimizer.d.ts.map +1 -0
- package/dist/engine/optimizer.js +1170 -0
- package/dist/engine/optimizer.js.map +1 -0
- package/dist/engine/query-cache.d.ts +10 -0
- package/dist/engine/query-cache.d.ts.map +1 -0
- package/dist/engine/query-cache.js +58 -0
- package/dist/engine/query-cache.js.map +1 -0
- package/dist/engine/query.d.ts +844 -0
- package/dist/engine/query.d.ts.map +1 -0
- package/dist/engine/query.js +7450 -0
- package/dist/engine/query.js.map +1 -0
- package/dist/engine/schema-wire.d.ts +92 -0
- package/dist/engine/schema-wire.d.ts.map +1 -0
- package/dist/engine/schema-wire.js +115 -0
- package/dist/engine/schema-wire.js.map +1 -0
- package/dist/engine/schema.d.ts +394 -0
- package/dist/engine/schema.d.ts.map +1 -0
- package/dist/engine/schema.js +709 -0
- package/dist/engine/schema.js.map +1 -0
- package/dist/engine/sort-keys.d.ts +36 -0
- package/dist/engine/sort-keys.d.ts.map +1 -0
- package/dist/engine/sort-keys.js +60 -0
- package/dist/engine/sort-keys.js.map +1 -0
- package/dist/engine/sql-json.d.ts +30 -0
- package/dist/engine/sql-json.d.ts.map +1 -0
- package/dist/engine/sql-json.js +120 -0
- package/dist/engine/sql-json.js.map +1 -0
- package/dist/engine/sql-semantics.d.ts +31 -0
- package/dist/engine/sql-semantics.d.ts.map +1 -0
- package/dist/engine/sql-semantics.js +121 -0
- package/dist/engine/sql-semantics.js.map +1 -0
- package/dist/engine/vector.d.ts +87 -0
- package/dist/engine/vector.d.ts.map +1 -0
- package/dist/engine/vector.js +4227 -0
- package/dist/engine/vector.js.map +1 -0
- package/dist/engine/worker-host.d.ts +48 -0
- package/dist/engine/worker-host.d.ts.map +1 -0
- package/dist/engine/worker-host.js +501 -0
- package/dist/engine/worker-host.js.map +1 -0
- package/dist/engine/worker.d.ts +2 -0
- package/dist/engine/worker.d.ts.map +1 -0
- package/dist/engine/worker.js +14 -0
- package/dist/engine/worker.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/plan/index.d.ts +17 -0
- package/dist/plan/index.d.ts.map +1 -0
- package/dist/plan/index.js +17 -0
- package/dist/plan/index.js.map +1 -0
- package/dist/storage/fixture-shape.d.ts +42 -0
- package/dist/storage/fixture-shape.d.ts.map +1 -0
- package/dist/storage/fixture-shape.js +146 -0
- package/dist/storage/fixture-shape.js.map +1 -0
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +5 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/indexeddb.d.ts +139 -0
- package/dist/storage/indexeddb.d.ts.map +1 -0
- package/dist/storage/indexeddb.js +3161 -0
- package/dist/storage/indexeddb.js.map +1 -0
- package/dist/storage/memory.d.ts +121 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +1455 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/snapshot.d.ts +108 -0
- package/dist/storage/snapshot.d.ts.map +1 -0
- package/dist/storage/snapshot.js +276 -0
- package/dist/storage/snapshot.js.map +1 -0
- package/dist/storage/types.d.ts +895 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +1549 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/testing/index.d.ts +76 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +233 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/seeds.d.ts +11 -0
- package/dist/testing/seeds.d.ts.map +1 -0
- package/dist/testing/seeds.js +50 -0
- package/dist/testing/seeds.js.map +1 -0
- package/dist/transactions/index.d.ts +148 -0
- package/dist/transactions/index.d.ts.map +1 -0
- package/dist/transactions/index.js +662 -0
- package/dist/transactions/index.js.map +1 -0
- package/dist/worker-protocol/index.d.ts +84 -0
- package/dist/worker-protocol/index.d.ts.map +1 -0
- package/dist/worker-protocol/index.js +117 -0
- package/dist/worker-protocol/index.js.map +1 -0
- package/package.json +66 -16
- package/sql-feature-matrix.json +1275 -0
- package/index.d.ts +0 -1
- package/index.js +0 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write-time default filling. For a default-bearing column (always non-nullable), a null or
|
|
3
|
+
* absent slot means "generate" and an explicit value passes through untouched. Pure defaults
|
|
4
|
+
* (now, literals) are filled here before validation; auto-increment slots stay null until the
|
|
5
|
+
* write path reserves a range from storage, keeping generation atomic across concurrent
|
|
6
|
+
* writers. Function defaults never reach the engine — the typed facade fills them before the
|
|
7
|
+
* batch crosses the boundary.
|
|
8
|
+
*/
|
|
9
|
+
import type { RowIdRange, TableColumnRecord, TableRecord } from "../storage/types.js";
|
|
10
|
+
import type { BatchValue, ColumnarBatch } from "./batch.js";
|
|
11
|
+
/** The deferred part of a fill: slots that need storage-reserved auto-increment values. */
|
|
12
|
+
export interface AutoIncrementFill {
|
|
13
|
+
readonly column: TableColumnRecord;
|
|
14
|
+
/** Row indexes whose value must come from the reserved range, in row order. */
|
|
15
|
+
readonly missingIndexes: readonly number[];
|
|
16
|
+
/** One past the largest explicit value in the batch, so generated keys never collide with it. */
|
|
17
|
+
readonly atLeast: bigint;
|
|
18
|
+
}
|
|
19
|
+
export interface FilledBatch {
|
|
20
|
+
readonly batch: ColumnarBatch;
|
|
21
|
+
/** Full written vectors, input row order, for columns where at least one slot was generated. */
|
|
22
|
+
readonly generated: Map<string, BatchValue[]>;
|
|
23
|
+
/** Present whenever the table has an auto-increment column, even if every value is explicit —
|
|
24
|
+
* the counter still bumps past the explicit maximum in the same atomic step. */
|
|
25
|
+
readonly autoIncrement?: AutoIncrementFill;
|
|
26
|
+
}
|
|
27
|
+
export declare function fillColumnDefaults(table: TableRecord, input: ColumnarBatch, now: () => Date, knownRowCount?: number): FilledBatch;
|
|
28
|
+
/**
|
|
29
|
+
* Writes the reserved range into the batch's null slots. The vector is the fresh array
|
|
30
|
+
* `fillColumnDefaults` created for this fill, never a caller-owned one.
|
|
31
|
+
*/
|
|
32
|
+
export declare function patchAutoIncrementValues(input: ColumnarBatch, fill: AutoIncrementFill, range: RowIdRange): void;
|
|
33
|
+
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../src/engine/defaults.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AACtF,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D,2FAA2F;AAC3F,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC;IACnC,+EAA+E;IAC/E,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;IAC3C,iGAAiG;IACjG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,gGAAgG;IAChG,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC;IAC9C;oFACgF;IAChF,QAAQ,CAAC,aAAa,CAAC,EAAE,iBAAiB,CAAC;CAC5C;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,WAAW,EAClB,KAAK,EAAE,aAAa,EACpB,GAAG,EAAE,MAAM,IAAI,EACf,aAAa,CAAC,EAAE,MAAM,GACrB,WAAW,CA8Db;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,aAAa,EACpB,IAAI,EAAE,iBAAiB,EACvB,KAAK,EAAE,UAAU,GAChB,IAAI,CASN"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write-time default filling. For a default-bearing column (always non-nullable), a null or
|
|
3
|
+
* absent slot means "generate" and an explicit value passes through untouched. Pure defaults
|
|
4
|
+
* (now, literals) are filled here before validation; auto-increment slots stay null until the
|
|
5
|
+
* write path reserves a range from storage, keeping generation atomic across concurrent
|
|
6
|
+
* writers. Function defaults never reach the engine — the typed facade fills them before the
|
|
7
|
+
* batch crosses the boundary.
|
|
8
|
+
*/
|
|
9
|
+
export function fillColumnDefaults(table, input, now, knownRowCount) {
|
|
10
|
+
const defaultColumns = table.columns.filter((column) => column.defaultValue !== undefined);
|
|
11
|
+
if (defaultColumns.length === 0)
|
|
12
|
+
return { batch: input, generated: new Map() };
|
|
13
|
+
const rowCount = knownRowCount ?? Object.values(input.columns).find((values) => values.length > 0)?.length ?? 0;
|
|
14
|
+
if (rowCount === 0)
|
|
15
|
+
return { batch: input, generated: new Map() };
|
|
16
|
+
const columns = { ...input.columns };
|
|
17
|
+
const generated = new Map();
|
|
18
|
+
let autoIncrement;
|
|
19
|
+
let nowValue;
|
|
20
|
+
for (const column of defaultColumns) {
|
|
21
|
+
const defaultValue = column.defaultValue;
|
|
22
|
+
if (defaultValue === undefined)
|
|
23
|
+
continue;
|
|
24
|
+
const provided = input.columns[column.name];
|
|
25
|
+
if (defaultValue.kind === "autoincrement") {
|
|
26
|
+
const missingIndexes = [];
|
|
27
|
+
let maxExplicit = 0n;
|
|
28
|
+
for (let index = 0; index < rowCount; index += 1) {
|
|
29
|
+
const value = provided?.[index] ?? null;
|
|
30
|
+
if (value === null) {
|
|
31
|
+
missingIndexes.push(index);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) {
|
|
35
|
+
throw new TypeError(`${column.name}[${String(index)}] must be a whole number in the safe integer range`);
|
|
36
|
+
}
|
|
37
|
+
if (BigInt(value) > maxExplicit)
|
|
38
|
+
maxExplicit = BigInt(value);
|
|
39
|
+
}
|
|
40
|
+
if (missingIndexes.length > 0) {
|
|
41
|
+
columns[column.name] =
|
|
42
|
+
provided === undefined ? new Array(rowCount).fill(null) : [...provided];
|
|
43
|
+
}
|
|
44
|
+
autoIncrement = { column, missingIndexes, atLeast: maxExplicit + 1n };
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const values = provided === undefined ? new Array(rowCount).fill(null) : [...provided];
|
|
48
|
+
let filled = false;
|
|
49
|
+
for (let index = 0; index < rowCount; index += 1) {
|
|
50
|
+
if ((values[index] ?? null) !== null)
|
|
51
|
+
continue;
|
|
52
|
+
filled = true;
|
|
53
|
+
switch (defaultValue.kind) {
|
|
54
|
+
case "now":
|
|
55
|
+
values[index] = nowValue ??= now();
|
|
56
|
+
break;
|
|
57
|
+
case "literal":
|
|
58
|
+
values[index] = defaultValue.value;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (filled) {
|
|
63
|
+
columns[column.name] = values;
|
|
64
|
+
generated.set(column.name, values);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
batch: { columns },
|
|
69
|
+
generated,
|
|
70
|
+
...(autoIncrement === undefined ? {} : { autoIncrement }),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Writes the reserved range into the batch's null slots. The vector is the fresh array
|
|
75
|
+
* `fillColumnDefaults` created for this fill, never a caller-owned one.
|
|
76
|
+
*/
|
|
77
|
+
export function patchAutoIncrementValues(input, fill, range) {
|
|
78
|
+
const vector = input.columns[fill.column.name];
|
|
79
|
+
fill.missingIndexes.forEach((rowIndex, offset) => {
|
|
80
|
+
const value = range.start + BigInt(offset);
|
|
81
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
82
|
+
throw new RangeError(`Auto-increment exhausted the safe integer range: ${fill.column.name}`);
|
|
83
|
+
}
|
|
84
|
+
vector[rowIndex] = Number(value);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
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QAC3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,UAAU,CAAC,oDAAoD,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAC/F,CAAC;QACD,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed engine errors, kept free of engine imports so the main-thread worker client can rehydrate
|
|
3
|
+
* them (name, fields, instanceof) without bundling the executor.
|
|
4
|
+
*/
|
|
5
|
+
type ErrorValue = boolean | number | string | Date;
|
|
6
|
+
export declare class UniqueConstraintError extends Error {
|
|
7
|
+
readonly tableName: string;
|
|
8
|
+
readonly columnName: string;
|
|
9
|
+
readonly value: ErrorValue;
|
|
10
|
+
readonly name = "UniqueConstraintError";
|
|
11
|
+
constructor(tableName: string, columnName: string, value: ErrorValue);
|
|
12
|
+
}
|
|
13
|
+
export declare class MissingKeyError extends Error {
|
|
14
|
+
readonly tableName: string;
|
|
15
|
+
readonly columnName: string;
|
|
16
|
+
readonly value: ErrorValue;
|
|
17
|
+
readonly name = "MissingKeyError";
|
|
18
|
+
constructor(tableName: string, columnName: string, value: ErrorValue);
|
|
19
|
+
}
|
|
20
|
+
export declare class CompactionMemoryBudgetError extends Error {
|
|
21
|
+
readonly budgetBytes: number;
|
|
22
|
+
readonly minimumBytes: number;
|
|
23
|
+
readonly name = "CompactionMemoryBudgetError";
|
|
24
|
+
constructor(budgetBytes: number, minimumBytes: number);
|
|
25
|
+
}
|
|
26
|
+
export declare class CompactionWriteAmplificationError extends Error {
|
|
27
|
+
readonly outputBytes: number;
|
|
28
|
+
readonly maximumOutputBytes: number;
|
|
29
|
+
readonly name = "CompactionWriteAmplificationError";
|
|
30
|
+
constructor(outputBytes: number, maximumOutputBytes: number);
|
|
31
|
+
}
|
|
32
|
+
export declare class CompactionJobCancelledError extends Error {
|
|
33
|
+
readonly jobId: string;
|
|
34
|
+
readonly name = "CompactionJobCancelledError";
|
|
35
|
+
constructor(jobId: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* SQL that failed to compile, located in the text the caller passed. `offset` and `length` are
|
|
39
|
+
* character positions into that exact string — leading whitespace included — so an editor can
|
|
40
|
+
* underline the token that failed without re-deriving the position from the message.
|
|
41
|
+
*
|
|
42
|
+
* `length` is 0 where the failure has no width: an empty statement, or a query that ended before
|
|
43
|
+
* the parser expected it to. Errors raised after parsing succeeds (plan optimization, execution)
|
|
44
|
+
* carry no position and stay plain `TypeError`s.
|
|
45
|
+
*
|
|
46
|
+
* It extends `TypeError` because that is what compilation failures threw before positions existed.
|
|
47
|
+
*/
|
|
48
|
+
export declare class SqlCompileError extends TypeError {
|
|
49
|
+
readonly offset: number;
|
|
50
|
+
readonly length: number;
|
|
51
|
+
readonly name = "SqlCompileError";
|
|
52
|
+
constructor(message: string, offset: number, length: number);
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/engine/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,KAAK,UAAU,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAMnD,qBAAa,qBAAsB,SAAQ,KAAK;IAI5C,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM;IAC3B,QAAQ,CAAC,KAAK,EAAE,UAAU;IAL5B,SAAkB,IAAI,2BAA2B;gBAGtC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,UAAU;CAI7B;AAED,qBAAa,eAAgB,SAAQ,KAAK;IAItC,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,UAAU,EAAE,MAAM;IAC3B,QAAQ,CAAC,KAAK,EAAE,UAAU;IAL5B,SAAkB,IAAI,qBAAqB;gBAGhC,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,UAAU;CAI7B;AAED,qBAAa,2BAA4B,SAAQ,KAAK;IAIlD,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,QAAQ,CAAC,YAAY,EAAE,MAAM;IAJ/B,SAAkB,IAAI,iCAAiC;gBAG5C,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM;CAMhC;AAED,qBAAa,iCAAkC,SAAQ,KAAK;IAIxD,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,QAAQ,CAAC,kBAAkB,EAAE,MAAM;IAJrC,SAAkB,IAAI,uCAAuC;gBAGlD,WAAW,EAAE,MAAM,EACnB,kBAAkB,EAAE,MAAM;CAMtC;AAED,qBAAa,2BAA4B,SAAQ,KAAK;IAGxC,QAAQ,CAAC,KAAK,EAAE,MAAM;IAFlC,SAAkB,IAAI,iCAAiC;gBAElC,KAAK,EAAE,MAAM;CAGnC;AAED;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,SAAQ,SAAS;IAK1C,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM;IALzB,SAAkB,IAAI,qBAAqB;gBAGzC,OAAO,EAAE,MAAM,EACN,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM;CAI1B"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed engine errors, kept free of engine imports so the main-thread worker client can rehydrate
|
|
3
|
+
* them (name, fields, instanceof) without bundling the executor.
|
|
4
|
+
*/
|
|
5
|
+
function formatValue(value) {
|
|
6
|
+
return value instanceof Date ? value.toISOString() : String(value);
|
|
7
|
+
}
|
|
8
|
+
export class UniqueConstraintError extends Error {
|
|
9
|
+
tableName;
|
|
10
|
+
columnName;
|
|
11
|
+
value;
|
|
12
|
+
name = "UniqueConstraintError";
|
|
13
|
+
constructor(tableName, columnName, value) {
|
|
14
|
+
super(`Duplicate value for ${tableName}.${columnName}: ${formatValue(value)}`);
|
|
15
|
+
this.tableName = tableName;
|
|
16
|
+
this.columnName = columnName;
|
|
17
|
+
this.value = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class MissingKeyError extends Error {
|
|
21
|
+
tableName;
|
|
22
|
+
columnName;
|
|
23
|
+
value;
|
|
24
|
+
name = "MissingKeyError";
|
|
25
|
+
constructor(tableName, columnName, value) {
|
|
26
|
+
super(`Missing value for ${tableName}.${columnName}: ${formatValue(value)}`);
|
|
27
|
+
this.tableName = tableName;
|
|
28
|
+
this.columnName = columnName;
|
|
29
|
+
this.value = value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class CompactionMemoryBudgetError extends Error {
|
|
33
|
+
budgetBytes;
|
|
34
|
+
minimumBytes;
|
|
35
|
+
name = "CompactionMemoryBudgetError";
|
|
36
|
+
constructor(budgetBytes, minimumBytes) {
|
|
37
|
+
super(`Compaction needs at least ${String(minimumBytes)} bytes of working memory; budget is ${String(budgetBytes)} bytes`);
|
|
38
|
+
this.budgetBytes = budgetBytes;
|
|
39
|
+
this.minimumBytes = minimumBytes;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class CompactionWriteAmplificationError extends Error {
|
|
43
|
+
outputBytes;
|
|
44
|
+
maximumOutputBytes;
|
|
45
|
+
name = "CompactionWriteAmplificationError";
|
|
46
|
+
constructor(outputBytes, maximumOutputBytes) {
|
|
47
|
+
super(`Compaction output would use ${String(outputBytes)} stored bytes; limit is ${String(maximumOutputBytes)} bytes`);
|
|
48
|
+
this.outputBytes = outputBytes;
|
|
49
|
+
this.maximumOutputBytes = maximumOutputBytes;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export class CompactionJobCancelledError extends Error {
|
|
53
|
+
jobId;
|
|
54
|
+
name = "CompactionJobCancelledError";
|
|
55
|
+
constructor(jobId) {
|
|
56
|
+
super(`Compaction job cancelled: ${jobId}`);
|
|
57
|
+
this.jobId = jobId;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* SQL that failed to compile, located in the text the caller passed. `offset` and `length` are
|
|
62
|
+
* character positions into that exact string — leading whitespace included — so an editor can
|
|
63
|
+
* underline the token that failed without re-deriving the position from the message.
|
|
64
|
+
*
|
|
65
|
+
* `length` is 0 where the failure has no width: an empty statement, or a query that ended before
|
|
66
|
+
* the parser expected it to. Errors raised after parsing succeeds (plan optimization, execution)
|
|
67
|
+
* carry no position and stay plain `TypeError`s.
|
|
68
|
+
*
|
|
69
|
+
* It extends `TypeError` because that is what compilation failures threw before positions existed.
|
|
70
|
+
*/
|
|
71
|
+
export class SqlCompileError extends TypeError {
|
|
72
|
+
offset;
|
|
73
|
+
length;
|
|
74
|
+
name = "SqlCompileError";
|
|
75
|
+
constructor(message, offset, length) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.offset = offset;
|
|
78
|
+
this.length = length;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/engine/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,SAAS,WAAW,CAAC,KAAiB;IACpC,OAAO,KAAK,YAAY,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACrE,CAAC;AAED,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAInC;IACA;IACA;IALO,IAAI,GAAG,uBAAuB,CAAC;IAEjD,YACW,SAAiB,EACjB,UAAkB,EAClB,KAAiB;QAE1B,KAAK,CAAC,uBAAuB,SAAS,IA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6BAA6B,KAAK,EAAE,CAAC,CAAC;QADzB,UAAK,GAAL,KAAK,CAAQ;IAElC,CAAC;CACF;AAED;;;;;;;;;;GAUG;AACH,MAAM,OAAO,eAAgB,SAAQ,SAAS;IAKjC;IACA;IALO,IAAI,GAAG,iBAAiB,CAAC;IAE3C,YACE,OAAe,EACN,MAAc,EACd,MAAc;QAEvB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAQ;IAGzB,CAAC;CACF"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Full-text search primitives: the tokenizer, the query grammar, and the BM25 scorer.
|
|
3
|
+
*
|
|
4
|
+
* The tokenizer is deliberately hand-rolled rather than `Intl.Segmenter`: segmentation output
|
|
5
|
+
* must be identical across browsers, ICU versions, and time, because phase-2 persists tokens in
|
|
6
|
+
* a multi-tab shared index — an environment-varying tokenizer would silently split one logical
|
|
7
|
+
* index into two vocabularies. Any change to tokenization must bump `FTS_TOKENIZER_VERSION`;
|
|
8
|
+
* a persisted index stamped with another version reads as absent and rebuilds.
|
|
9
|
+
*
|
|
10
|
+
* The scorer takes only integers (term frequency, document length, document frequency, document
|
|
11
|
+
* count, total token count), so the row and columnar executors produce bit-identical scores no
|
|
12
|
+
* matter what order they accumulate in. Keep it that way: never feed it a running float.
|
|
13
|
+
*/
|
|
14
|
+
export declare const FTS_TOKENIZER_VERSION = 1;
|
|
15
|
+
/** One parsed query term; `prefix` terms match any token they prefix. */
|
|
16
|
+
export interface FtsQueryTerm {
|
|
17
|
+
readonly term: string;
|
|
18
|
+
readonly prefix: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Corpus statistics for one (columns, query) signature — O(#query terms) integers. Every row of
|
|
22
|
+
* the corpus counts as a document (an all-null document has length 0): this definition is what
|
|
23
|
+
* lets a persisted index serve exact statistics without a scan, since the document count is
|
|
24
|
+
* just the table's row count. All producers — the row executor, the columnar scan, and the
|
|
25
|
+
* postings index — must share it, or BM25 scores drift between paths.
|
|
26
|
+
*/
|
|
27
|
+
export interface FtsStats {
|
|
28
|
+
/** Every row of the corpus, including all-null documents. */
|
|
29
|
+
docCount: number;
|
|
30
|
+
/** Total tokens across all documents. */
|
|
31
|
+
totalTokens: number;
|
|
32
|
+
/** Documents containing each query term, aligned with the query's term order. */
|
|
33
|
+
dfByTerm: number[];
|
|
34
|
+
}
|
|
35
|
+
/** Tokenizes one cell's text: NFKC, locale-independent lowercase, letter/number runs. */
|
|
36
|
+
export declare function tokenize(text: string): string[];
|
|
37
|
+
/**
|
|
38
|
+
* Parses a query: whitespace-separated chunks, each tokenized with the document pipeline; a
|
|
39
|
+
* chunk ending in `*` marks its last produced token as a prefix term. Zero terms means the
|
|
40
|
+
* query matches nothing.
|
|
41
|
+
*/
|
|
42
|
+
export declare function tokenizeQuery(query: string): FtsQueryTerm[];
|
|
43
|
+
export declare function termMatches(token: string, term: FtsQueryTerm): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Compile-time cap on query terms: the columnar fast path packs per-term presence into one
|
|
46
|
+
* 32-bit mask per dictionary entry.
|
|
47
|
+
*/
|
|
48
|
+
export declare const FTS_MAX_QUERY_TERMS = 32;
|
|
49
|
+
/** Tokenized query terms with the same bounded caching scheme as the LIKE pattern cache. */
|
|
50
|
+
export declare function cachedQueryTerms(query: string): FtsQueryTerm[];
|
|
51
|
+
/** Compile-time validation shared by the SQL parser and the DSL expression builder. */
|
|
52
|
+
export declare function validateFtsQuery(query: string): void;
|
|
53
|
+
/** Bitmask of query terms present in a token list: bit i set means terms[i] matched. */
|
|
54
|
+
export declare function termsMask(tokens: readonly string[], terms: readonly FtsQueryTerm[]): number;
|
|
55
|
+
/** The all-terms-present mask; terms.length is capped at 32 so the shift stays in range. */
|
|
56
|
+
export declare function fullTermsMask(termCount: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* Document-level MATCH truth over one row's rendered column values: every term must appear in
|
|
59
|
+
* some column (AND across terms, OR across columns). All columns null → SQL unknown (null);
|
|
60
|
+
* an empty query matches nothing.
|
|
61
|
+
*/
|
|
62
|
+
export declare function ftsMatchTruth(terms: readonly FtsQueryTerm[], values: Iterable<unknown>): boolean | null;
|
|
63
|
+
/**
|
|
64
|
+
* Per-term frequencies of the query's terms in one token list, aligned with the term order.
|
|
65
|
+
* A document matches when every slot is non-zero.
|
|
66
|
+
*/
|
|
67
|
+
export declare function termFrequencies(tokens: readonly string[], terms: readonly FtsQueryTerm[]): number[];
|
|
68
|
+
/**
|
|
69
|
+
* One term's BM25 contribution. Integer inputs only — see the module comment. `df` of zero
|
|
70
|
+
* contributes nothing (the term appears in no document, so no row scores on it).
|
|
71
|
+
*/
|
|
72
|
+
export declare function bm25Score(tf: number, docLength: number, df: number, docCount: number, totalTokens: number): number;
|
|
73
|
+
/** Sums the contributions of every query term for one document. */
|
|
74
|
+
export declare function bm25DocumentScore(frequencies: readonly number[], docLength: number, stats: FtsStats): number;
|
|
75
|
+
/** Accumulates corpus statistics one document at a time; every producer feeds this. */
|
|
76
|
+
export declare class FtsStatsAccumulator {
|
|
77
|
+
#private;
|
|
78
|
+
constructor(terms: readonly FtsQueryTerm[]);
|
|
79
|
+
/** Adds one document; an all-null row contributes a document of length 0. */
|
|
80
|
+
addDocument(tokens: readonly string[]): void;
|
|
81
|
+
/** Adds one document from pre-aggregated counts (the dictionary-table producers). */
|
|
82
|
+
addDocumentCounts(termMask: number, length: number): void;
|
|
83
|
+
get stats(): FtsStats;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Document-level BM25 for one row's rendered column values: term frequencies and document
|
|
87
|
+
* length sum across columns (the document is the concatenation of its fields). All columns
|
|
88
|
+
* null → SQL null; a document containing no query term scores 0.
|
|
89
|
+
*/
|
|
90
|
+
export declare function ftsBm25Row(terms: readonly FtsQueryTerm[], values: Iterable<unknown>, stats: FtsStats): number | null;
|
|
91
|
+
/** Renders one cell for the document, per column type. Booleans are excluded from documents. */
|
|
92
|
+
export declare function renderDocumentValue(value: unknown): string | undefined;
|
|
93
|
+
//# sourceMappingURL=fts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fts.d.ts","sourceRoot":"","sources":["../../src/engine/fts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAevC,yEAAyE;AACzE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,QAAQ;IACvB,6DAA6D;IAC7D,QAAQ,EAAE,MAAM,CAAC;IACjB,yCAAyC;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,iFAAiF;IACjF,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAmCD,yFAAyF;AACzF,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAQ/C;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,EAAE,CAW3D;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAEtE;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAItC,4FAA4F;AAC5F,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,YAAY,EAAE,CAO9D;AAED,uFAAuF;AACvF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAIpD;AAED,wFAAwF;AACxF,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,EAAE,SAAS,YAAY,EAAE,GAAG,MAAM,CAW3F;AAED,4FAA4F;AAC5F,wBAAgB,aAAa,CAAC,SA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-text search primitives: the tokenizer, the query grammar, and the BM25 scorer.
|
|
3
|
+
*
|
|
4
|
+
* The tokenizer is deliberately hand-rolled rather than `Intl.Segmenter`: segmentation output
|
|
5
|
+
* must be identical across browsers, ICU versions, and time, because phase-2 persists tokens in
|
|
6
|
+
* a multi-tab shared index — an environment-varying tokenizer would silently split one logical
|
|
7
|
+
* index into two vocabularies. Any change to tokenization must bump `FTS_TOKENIZER_VERSION`;
|
|
8
|
+
* a persisted index stamped with another version reads as absent and rebuilds.
|
|
9
|
+
*
|
|
10
|
+
* The scorer takes only integers (term frequency, document length, document frequency, document
|
|
11
|
+
* count, total token count), so the row and columnar executors produce bit-identical scores no
|
|
12
|
+
* matter what order they accumulate in. Keep it that way: never feed it a running float.
|
|
13
|
+
*/
|
|
14
|
+
export const FTS_TOKENIZER_VERSION = 1;
|
|
15
|
+
/** BM25 constants — the standard defaults. */
|
|
16
|
+
const BM25_K1 = 1.2;
|
|
17
|
+
const BM25_B = 0.75;
|
|
18
|
+
const MAX_TOKEN_LENGTH = 32;
|
|
19
|
+
const MAX_TOKENS_PER_DOCUMENT = 4096;
|
|
20
|
+
const TOKEN_RUN = /[\p{L}\p{N}]+/gu;
|
|
21
|
+
// Script_Extensions rather than Script: shared marks like the katakana prolonged sound mark
|
|
22
|
+
// (U+30FC, script Common) must stay inside the kana run they extend.
|
|
23
|
+
const CJK = /[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}]/u;
|
|
24
|
+
function pushToken(tokens, token) {
|
|
25
|
+
if (tokens.length >= MAX_TOKENS_PER_DOCUMENT)
|
|
26
|
+
return;
|
|
27
|
+
tokens.push(token.length > MAX_TOKEN_LENGTH ? token.slice(0, MAX_TOKEN_LENGTH) : token);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Splits one normalized run into tokens: contiguous CJK subsequences emit character bigrams
|
|
31
|
+
* (a single character when alone), everything else stays one token. Bigrams give usable CJK
|
|
32
|
+
* search without a dictionary while staying fully deterministic.
|
|
33
|
+
*/
|
|
34
|
+
function emitRun(tokens, run) {
|
|
35
|
+
if (!CJK.test(run)) {
|
|
36
|
+
pushToken(tokens, run);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const characters = Array.from(run);
|
|
40
|
+
let start = 0;
|
|
41
|
+
while (start < characters.length) {
|
|
42
|
+
const isCjk = CJK.test(characters[start] ?? "");
|
|
43
|
+
let end = start + 1;
|
|
44
|
+
while (end < characters.length && CJK.test(characters[end] ?? "") === isCjk)
|
|
45
|
+
end += 1;
|
|
46
|
+
if (isCjk) {
|
|
47
|
+
if (end - start === 1)
|
|
48
|
+
pushToken(tokens, characters[start] ?? "");
|
|
49
|
+
for (let index = start; index < end - 1; index += 1) {
|
|
50
|
+
pushToken(tokens, (characters[index] ?? "") + (characters[index + 1] ?? ""));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
pushToken(tokens, characters.slice(start, end).join(""));
|
|
55
|
+
}
|
|
56
|
+
start = end;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Tokenizes one cell's text: NFKC, locale-independent lowercase, letter/number runs. */
|
|
60
|
+
export function tokenize(text) {
|
|
61
|
+
const tokens = [];
|
|
62
|
+
const normalized = text.normalize("NFKC").toLowerCase();
|
|
63
|
+
for (const match of normalized.matchAll(TOKEN_RUN)) {
|
|
64
|
+
emitRun(tokens, match[0]);
|
|
65
|
+
if (tokens.length >= MAX_TOKENS_PER_DOCUMENT)
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
return tokens;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Parses a query: whitespace-separated chunks, each tokenized with the document pipeline; a
|
|
72
|
+
* chunk ending in `*` marks its last produced token as a prefix term. Zero terms means the
|
|
73
|
+
* query matches nothing.
|
|
74
|
+
*/
|
|
75
|
+
export function tokenizeQuery(query) {
|
|
76
|
+
const terms = [];
|
|
77
|
+
for (const chunk of query.split(/\s+/)) {
|
|
78
|
+
if (chunk.length === 0)
|
|
79
|
+
continue;
|
|
80
|
+
const prefix = chunk.endsWith("*");
|
|
81
|
+
const tokens = tokenize(prefix ? chunk.slice(0, -1) : chunk);
|
|
82
|
+
tokens.forEach((term, index) => {
|
|
83
|
+
terms.push({ term, prefix: prefix && index === tokens.length - 1 });
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return terms;
|
|
87
|
+
}
|
|
88
|
+
export function termMatches(token, term) {
|
|
89
|
+
return term.prefix ? token.startsWith(term.term) : token === term.term;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Compile-time cap on query terms: the columnar fast path packs per-term presence into one
|
|
93
|
+
* 32-bit mask per dictionary entry.
|
|
94
|
+
*/
|
|
95
|
+
export const FTS_MAX_QUERY_TERMS = 32;
|
|
96
|
+
const queryTermsCache = new Map();
|
|
97
|
+
/** Tokenized query terms with the same bounded caching scheme as the LIKE pattern cache. */
|
|
98
|
+
export function cachedQueryTerms(query) {
|
|
99
|
+
const cached = queryTermsCache.get(query);
|
|
100
|
+
if (cached !== undefined)
|
|
101
|
+
return cached;
|
|
102
|
+
const terms = tokenizeQuery(query);
|
|
103
|
+
if (queryTermsCache.size >= 128)
|
|
104
|
+
queryTermsCache.clear();
|
|
105
|
+
queryTermsCache.set(query, terms);
|
|
106
|
+
return terms;
|
|
107
|
+
}
|
|
108
|
+
/** Compile-time validation shared by the SQL parser and the DSL expression builder. */
|
|
109
|
+
export function validateFtsQuery(query) {
|
|
110
|
+
if (cachedQueryTerms(query).length > FTS_MAX_QUERY_TERMS) {
|
|
111
|
+
throw new TypeError(`Full-text queries support at most ${String(FTS_MAX_QUERY_TERMS)} terms`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Bitmask of query terms present in a token list: bit i set means terms[i] matched. */
|
|
115
|
+
export function termsMask(tokens, terms) {
|
|
116
|
+
let mask = 0;
|
|
117
|
+
for (const token of tokens) {
|
|
118
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
119
|
+
const term = terms[index];
|
|
120
|
+
if (term !== undefined && (mask & (1 << index)) === 0 && termMatches(token, term)) {
|
|
121
|
+
mask |= 1 << index;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return mask;
|
|
126
|
+
}
|
|
127
|
+
/** The all-terms-present mask; terms.length is capped at 32 so the shift stays in range. */
|
|
128
|
+
export function fullTermsMask(termCount) {
|
|
129
|
+
return termCount >= 32 ? -1 : (1 << termCount) - 1;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Document-level MATCH truth over one row's rendered column values: every term must appear in
|
|
133
|
+
* some column (AND across terms, OR across columns). All columns null → SQL unknown (null);
|
|
134
|
+
* an empty query matches nothing.
|
|
135
|
+
*/
|
|
136
|
+
export function ftsMatchTruth(terms, values) {
|
|
137
|
+
let anyPresent = false;
|
|
138
|
+
const present = new Array(terms.length).fill(false);
|
|
139
|
+
let satisfied = 0;
|
|
140
|
+
for (const value of values) {
|
|
141
|
+
const rendered = renderDocumentValue(value);
|
|
142
|
+
if (rendered === undefined)
|
|
143
|
+
continue;
|
|
144
|
+
anyPresent = true;
|
|
145
|
+
if (terms.length === 0 || satisfied === terms.length)
|
|
146
|
+
continue;
|
|
147
|
+
for (const token of tokenize(rendered)) {
|
|
148
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
149
|
+
const term = terms[index];
|
|
150
|
+
if (term !== undefined && present[index] !== true && termMatches(token, term)) {
|
|
151
|
+
present[index] = true;
|
|
152
|
+
satisfied += 1;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (satisfied === terms.length)
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (!anyPresent)
|
|
160
|
+
return null;
|
|
161
|
+
return terms.length > 0 && satisfied === terms.length;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Per-term frequencies of the query's terms in one token list, aligned with the term order.
|
|
165
|
+
* A document matches when every slot is non-zero.
|
|
166
|
+
*/
|
|
167
|
+
export function termFrequencies(tokens, terms) {
|
|
168
|
+
const frequencies = new Array(terms.length).fill(0);
|
|
169
|
+
for (const token of tokens) {
|
|
170
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
171
|
+
const term = terms[index];
|
|
172
|
+
if (term !== undefined && termMatches(token, term)) {
|
|
173
|
+
frequencies[index] = (frequencies[index] ?? 0) + 1;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return frequencies;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* One term's BM25 contribution. Integer inputs only — see the module comment. `df` of zero
|
|
181
|
+
* contributes nothing (the term appears in no document, so no row scores on it).
|
|
182
|
+
*/
|
|
183
|
+
export function bm25Score(tf, docLength, df, docCount, totalTokens) {
|
|
184
|
+
if (tf === 0 || df === 0 || docCount === 0)
|
|
185
|
+
return 0;
|
|
186
|
+
const idf = Math.log(1 + (docCount - df + 0.5) / (df + 0.5));
|
|
187
|
+
const averageLength = totalTokens / docCount;
|
|
188
|
+
const normalized = (tf * (BM25_K1 + 1)) /
|
|
189
|
+
(tf +
|
|
190
|
+
BM25_K1 * (1 - BM25_B + (BM25_B * docLength) / (averageLength === 0 ? 1 : averageLength)));
|
|
191
|
+
return idf * normalized;
|
|
192
|
+
}
|
|
193
|
+
/** Sums the contributions of every query term for one document. */
|
|
194
|
+
export function bm25DocumentScore(frequencies, docLength, stats) {
|
|
195
|
+
let score = 0;
|
|
196
|
+
for (let index = 0; index < frequencies.length; index += 1) {
|
|
197
|
+
score += bm25Score(frequencies[index] ?? 0, docLength, stats.dfByTerm[index] ?? 0, stats.docCount, stats.totalTokens);
|
|
198
|
+
}
|
|
199
|
+
return score;
|
|
200
|
+
}
|
|
201
|
+
/** Accumulates corpus statistics one document at a time; every producer feeds this. */
|
|
202
|
+
export class FtsStatsAccumulator {
|
|
203
|
+
#terms;
|
|
204
|
+
#stats;
|
|
205
|
+
constructor(terms) {
|
|
206
|
+
this.#terms = terms;
|
|
207
|
+
this.#stats = {
|
|
208
|
+
docCount: 0,
|
|
209
|
+
totalTokens: 0,
|
|
210
|
+
dfByTerm: new Array(terms.length).fill(0),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** Adds one document; an all-null row contributes a document of length 0. */
|
|
214
|
+
addDocument(tokens) {
|
|
215
|
+
this.#stats.docCount += 1;
|
|
216
|
+
this.#stats.totalTokens += tokens.length;
|
|
217
|
+
const frequencies = termFrequencies(tokens, this.#terms);
|
|
218
|
+
for (let index = 0; index < frequencies.length; index += 1) {
|
|
219
|
+
if ((frequencies[index] ?? 0) > 0) {
|
|
220
|
+
this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/** Adds one document from pre-aggregated counts (the dictionary-table producers). */
|
|
225
|
+
addDocumentCounts(termMask, length) {
|
|
226
|
+
this.#stats.docCount += 1;
|
|
227
|
+
this.#stats.totalTokens += length;
|
|
228
|
+
for (let index = 0; index < this.#terms.length; index += 1) {
|
|
229
|
+
if ((termMask & (1 << index)) !== 0) {
|
|
230
|
+
this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
get stats() {
|
|
235
|
+
return this.#stats;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Document-level BM25 for one row's rendered column values: term frequencies and document
|
|
240
|
+
* length sum across columns (the document is the concatenation of its fields). All columns
|
|
241
|
+
* null → SQL null; a document containing no query term scores 0.
|
|
242
|
+
*/
|
|
243
|
+
export function ftsBm25Row(terms, values, stats) {
|
|
244
|
+
let anyPresent = false;
|
|
245
|
+
let docLength = 0;
|
|
246
|
+
const frequencies = new Array(terms.length).fill(0);
|
|
247
|
+
for (const value of values) {
|
|
248
|
+
const rendered = renderDocumentValue(value);
|
|
249
|
+
if (rendered === undefined)
|
|
250
|
+
continue;
|
|
251
|
+
anyPresent = true;
|
|
252
|
+
const tokens = tokenize(rendered);
|
|
253
|
+
docLength += tokens.length;
|
|
254
|
+
const partial = termFrequencies(tokens, terms);
|
|
255
|
+
for (let index = 0; index < terms.length; index += 1) {
|
|
256
|
+
frequencies[index] = (frequencies[index] ?? 0) + (partial[index] ?? 0);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (!anyPresent)
|
|
260
|
+
return null;
|
|
261
|
+
return bm25DocumentScore(frequencies, docLength, stats);
|
|
262
|
+
}
|
|
263
|
+
/** Renders one cell for the document, per column type. Booleans are excluded from documents. */
|
|
264
|
+
export function renderDocumentValue(value) {
|
|
265
|
+
if (value === null || value === undefined || typeof value === "boolean")
|
|
266
|
+
return undefined;
|
|
267
|
+
if (value instanceof Date)
|
|
268
|
+
return value.toISOString();
|
|
269
|
+
if (typeof value === "number")
|
|
270
|
+
return String(value);
|
|
271
|
+
return typeof value === "string" ? value : undefined;
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=fts.js.map
|