@minnowdb/core 0.7.0 → 0.7.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.
Files changed (95) hide show
  1. package/dist/block-format/block.js +254 -325
  2. package/dist/block-format/checksum.js +30 -52
  3. package/dist/block-format/codecs.js +112 -125
  4. package/dist/block-format/column.js +187 -205
  5. package/dist/block-format/index.js +5 -1
  6. package/dist/block-format/physical.js +371 -387
  7. package/dist/block-format/types.js +4 -1
  8. package/dist/block-format/unicode.js +30 -39
  9. package/dist/date-value.js +67 -78
  10. package/dist/engine/artifact-cache.js +106 -116
  11. package/dist/engine/batch.js +36 -44
  12. package/dist/engine/buffered-writer.js +156 -158
  13. package/dist/engine/byte-estimates.js +20 -22
  14. package/dist/engine/cache-limits.js +24 -25
  15. package/dist/engine/cancellation.js +5 -3
  16. package/dist/engine/catalog.js +64 -73
  17. package/dist/engine/client.js +715 -780
  18. package/dist/engine/database.js +15941 -19313
  19. package/dist/engine/defaults.js +104 -111
  20. package/dist/engine/errors.js +113 -128
  21. package/dist/engine/fts.js +233 -280
  22. package/dist/engine/group-index.js +287 -321
  23. package/dist/engine/index.js +23 -12
  24. package/dist/engine/join-index.js +177 -186
  25. package/dist/engine/keyed-live.js +168 -173
  26. package/dist/engine/live-api.js +0 -1
  27. package/dist/engine/live-equal.js +32 -37
  28. package/dist/engine/live.js +603 -647
  29. package/dist/engine/memory.js +109 -111
  30. package/dist/engine/optimizer.d.ts +19 -1
  31. package/dist/engine/optimizer.js +2633 -2934
  32. package/dist/engine/point-read.js +122 -182
  33. package/dist/engine/query-api.js +12 -3
  34. package/dist/engine/query-cache.js +61 -69
  35. package/dist/engine/query.d.ts +6 -0
  36. package/dist/engine/query.js +8503 -9980
  37. package/dist/engine/result-wire.js +221 -252
  38. package/dist/engine/schema-wire.js +97 -112
  39. package/dist/engine/schema.js +1099 -1254
  40. package/dist/engine/sort-keys.js +315 -389
  41. package/dist/engine/sql-domains.js +646 -769
  42. package/dist/engine/sql-driver.js +0 -1
  43. package/dist/engine/sql-functions.js +1031 -1125
  44. package/dist/engine/sql-json.js +199 -218
  45. package/dist/engine/sql-semantics.js +572 -639
  46. package/dist/engine/typed-live.js +271 -293
  47. package/dist/engine/vector.js +4484 -5145
  48. package/dist/engine/worker-host.d.ts +8 -63
  49. package/dist/engine/worker-host.js +28 -1130
  50. package/dist/engine/worker-indexeddb.d.ts +1 -0
  51. package/dist/engine/worker-indexeddb.js +3 -0
  52. package/dist/engine/worker-memory.d.ts +1 -0
  53. package/dist/engine/worker-memory.js +3 -0
  54. package/dist/engine/worker-opfs.d.ts +1 -0
  55. package/dist/engine/worker-opfs.js +3 -0
  56. package/dist/engine/worker-server.d.ts +93 -0
  57. package/dist/engine/worker-server.js +999 -0
  58. package/dist/engine/worker-store-indexeddb.d.ts +2 -0
  59. package/dist/engine/worker-store-indexeddb.js +11 -0
  60. package/dist/engine/worker-store-memory.d.ts +2 -0
  61. package/dist/engine/worker-store-memory.js +6 -0
  62. package/dist/engine/worker-store-opfs.d.ts +2 -0
  63. package/dist/engine/worker-store-opfs.js +9 -0
  64. package/dist/engine/worker.js +0 -12
  65. package/dist/engine/write-block-planner.js +93 -100
  66. package/dist/plan/index.js +15 -16
  67. package/dist/plan/model.d.ts +6 -0
  68. package/dist/plan/model.js +20 -27
  69. package/dist/storage/index.js +0 -12
  70. package/dist/storage/indexeddb.js +11776 -13171
  71. package/dist/storage/memory.js +1198 -1241
  72. package/dist/storage/opfs/files.js +238 -295
  73. package/dist/storage/opfs/index.js +9 -3
  74. package/dist/storage/opfs/leader.js +4386 -4856
  75. package/dist/storage/opfs/rpc.js +223 -259
  76. package/dist/storage/opfs/snapshot-ledger.js +219 -260
  77. package/dist/storage/opfs/store.js +884 -1003
  78. package/dist/storage/persistence.js +59 -74
  79. package/dist/storage/snapshot-stream.js +788 -826
  80. package/dist/storage/snapshot.js +36 -37
  81. package/dist/storage/toolkit/extents.js +444 -520
  82. package/dist/storage/toolkit/index.js +28 -29
  83. package/dist/storage/toolkit/record-core.js +5792 -6377
  84. package/dist/storage/toolkit/sync-file.js +34 -42
  85. package/dist/storage/toolkit/wal.js +92 -127
  86. package/dist/storage/toolkit/wire.js +232 -256
  87. package/dist/storage/types.js +2948 -3250
  88. package/dist/testing/block-store-conformance.js +1556 -1629
  89. package/dist/testing/index.js +310 -291
  90. package/dist/testing/opfs-shim.js +280 -312
  91. package/dist/testing/simulator.js +496 -534
  92. package/dist/testing/sqllogictest.js +425 -365
  93. package/dist/transactions/index.js +1464 -1712
  94. package/dist/worker-protocol/index.js +70 -69
  95. package/package.json +20 -2
@@ -1,300 +1,253 @@
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
1
  import { assertWellFormedString } from "../block-format/unicode.js";
15
2
  import { MAX_CACHEABLE_TEXT_CHARACTERS, MAX_SQL_PATTERN_CHARACTERS } from "./cache-limits.js";
16
3
  import { externalSqlDomainValue } from "./sql-domains.js";
17
- import { MAX_FTS_QUERY_TERMS, MAX_FTS_TOKENS_PER_DOCUMENT, MAX_INDEXED_STRING_CHARACTERS, } from "../storage/types.js";
18
- export const FTS_TOKENIZER_VERSION = 1;
19
- /** BM25 constants — the standard defaults. */
4
+ import { MAX_FTS_QUERY_TERMS, MAX_FTS_TOKENS_PER_DOCUMENT, MAX_INDEXED_STRING_CHARACTERS } from "../storage/types.js";
5
+ const FTS_TOKENIZER_VERSION = 1;
20
6
  const BM25_K1 = 1.2;
21
7
  const BM25_B = 0.75;
22
8
  const MAX_TOKEN_LENGTH = 32;
23
9
  const TOKEN_RUN = /[\p{L}\p{N}]+/gu;
24
10
  const QUERY_WHITESPACE = /\s/u;
25
- // Script_Extensions rather than Script: shared marks like the katakana prolonged sound mark
26
- // (U+30FC, script Common) must stay inside the kana run they extend.
27
11
  const CJK = /[\p{Script_Extensions=Han}\p{Script_Extensions=Hiragana}\p{Script_Extensions=Katakana}]/u;
28
12
  function pushToken(tokens, token) {
29
- if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
30
- return;
31
- tokens.push(token.length > MAX_TOKEN_LENGTH ? token.slice(0, MAX_TOKEN_LENGTH) : token);
13
+ if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
14
+ return;
15
+ tokens.push(token.length > MAX_TOKEN_LENGTH ? token.slice(0, MAX_TOKEN_LENGTH) : token);
32
16
  }
33
- /**
34
- * Splits one normalized run into tokens: contiguous CJK subsequences emit character bigrams
35
- * (a single character when alone), everything else stays one token. Bigrams give usable CJK
36
- * search without a dictionary while staying fully deterministic.
37
- */
38
17
  function emitRun(tokens, run) {
39
- if (!CJK.test(run)) {
40
- pushToken(tokens, run);
41
- return;
42
- }
43
- const characters = Array.from(run);
44
- let start = 0;
45
- while (start < characters.length) {
46
- const isCjk = CJK.test(characters[start] ?? "");
47
- let end = start + 1;
48
- while (end < characters.length && CJK.test(characters[end] ?? "") === isCjk)
49
- end += 1;
50
- if (isCjk) {
51
- if (end - start === 1)
52
- pushToken(tokens, characters[start] ?? "");
53
- for (let index = start; index < end - 1; index += 1) {
54
- pushToken(tokens, (characters[index] ?? "") + (characters[index + 1] ?? ""));
55
- }
56
- }
57
- else {
58
- pushToken(tokens, characters.slice(start, end).join(""));
59
- }
60
- start = end;
61
- }
62
- }
63
- /** Tokenizes one cell's text: NFKC, locale-independent lowercase, letter/number runs. */
64
- export function tokenize(text) {
65
- if (text.length > MAX_INDEXED_STRING_CHARACTERS) {
66
- throw new RangeError(`Full-text values cannot exceed ${String(MAX_INDEXED_STRING_CHARACTERS)} characters`);
67
- }
68
- assertWellFormedString(text, "Full-text value");
69
- const tokens = [];
70
- const normalized = text.normalize("NFKC").toLowerCase();
71
- for (const match of normalized.matchAll(TOKEN_RUN)) {
72
- emitRun(tokens, match[0]);
73
- if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
74
- break;
75
- }
76
- return tokens;
77
- }
78
- /**
79
- * Parses a query: whitespace-separated chunks, each tokenized with the document pipeline; a
80
- * chunk ending in `*` marks its last produced token as a prefix term. Zero terms means the
81
- * query matches nothing.
82
- */
83
- export function tokenizeQuery(query) {
84
- if (query.length > MAX_SQL_PATTERN_CHARACTERS) {
85
- throw new RangeError(`Full-text queries cannot exceed ${String(MAX_SQL_PATTERN_CHARACTERS)} characters`);
86
- }
87
- assertWellFormedString(query, "Full-text query");
88
- const terms = [];
89
- let cursor = 0;
90
- while (cursor < query.length) {
91
- while (cursor < query.length && QUERY_WHITESPACE.test(query[cursor] ?? ""))
92
- cursor += 1;
93
- const start = cursor;
94
- while (cursor < query.length && !QUERY_WHITESPACE.test(query[cursor] ?? ""))
95
- cursor += 1;
96
- if (cursor === start)
97
- continue;
98
- const prefix = query[cursor - 1] === "*";
99
- const tokens = tokenize(query.slice(start, prefix ? cursor - 1 : cursor));
100
- for (let index = 0; index < tokens.length; index += 1) {
101
- const term = tokens[index];
102
- if (term === undefined)
103
- continue;
104
- terms.push({ term, prefix: prefix && index === tokens.length - 1 });
105
- // Every caller rejects above 32. Stop before adversarial whitespace can allocate a terms
106
- // array proportional to the input; the 33rd item is all validation needs to see.
107
- if (terms.length > MAX_FTS_QUERY_TERMS)
108
- return terms;
109
- }
18
+ if (!CJK.test(run)) {
19
+ pushToken(tokens, run);
20
+ return;
21
+ }
22
+ const characters = Array.from(run);
23
+ let start = 0;
24
+ while (start < characters.length) {
25
+ const isCjk = CJK.test(characters[start] ?? "");
26
+ let end = start + 1;
27
+ while (end < characters.length && CJK.test(characters[end] ?? "") === isCjk)
28
+ end += 1;
29
+ if (isCjk) {
30
+ if (end - start === 1)
31
+ pushToken(tokens, characters[start] ?? "");
32
+ for (let index = start; index < end - 1; index += 1) {
33
+ pushToken(tokens, (characters[index] ?? "") + (characters[index + 1] ?? ""));
34
+ }
35
+ } else {
36
+ pushToken(tokens, characters.slice(start, end).join(""));
37
+ }
38
+ start = end;
39
+ }
40
+ }
41
+ function tokenize(text) {
42
+ if (text.length > MAX_INDEXED_STRING_CHARACTERS) {
43
+ throw new RangeError(`Full-text values cannot exceed ${String(MAX_INDEXED_STRING_CHARACTERS)} characters`);
44
+ }
45
+ assertWellFormedString(text, "Full-text value");
46
+ const tokens = [];
47
+ const normalized = text.normalize("NFKC").toLowerCase();
48
+ for (const match of normalized.matchAll(TOKEN_RUN)) {
49
+ emitRun(tokens, match[0]);
50
+ if (tokens.length >= MAX_FTS_TOKENS_PER_DOCUMENT)
51
+ break;
52
+ }
53
+ return tokens;
54
+ }
55
+ function tokenizeQuery(query) {
56
+ if (query.length > MAX_SQL_PATTERN_CHARACTERS) {
57
+ throw new RangeError(`Full-text queries cannot exceed ${String(MAX_SQL_PATTERN_CHARACTERS)} characters`);
58
+ }
59
+ assertWellFormedString(query, "Full-text query");
60
+ const terms = [];
61
+ let cursor = 0;
62
+ while (cursor < query.length) {
63
+ while (cursor < query.length && QUERY_WHITESPACE.test(query[cursor] ?? ""))
64
+ cursor += 1;
65
+ const start = cursor;
66
+ while (cursor < query.length && !QUERY_WHITESPACE.test(query[cursor] ?? ""))
67
+ cursor += 1;
68
+ if (cursor === start)
69
+ continue;
70
+ const prefix = query[cursor - 1] === "*";
71
+ const tokens = tokenize(query.slice(start, prefix ? cursor - 1 : cursor));
72
+ for (let index = 0; index < tokens.length; index += 1) {
73
+ const term = tokens[index];
74
+ if (term === void 0)
75
+ continue;
76
+ terms.push({ term, prefix: prefix && index === tokens.length - 1 });
77
+ if (terms.length > MAX_FTS_QUERY_TERMS)
78
+ return terms;
110
79
  }
111
- return terms;
80
+ }
81
+ return terms;
112
82
  }
113
83
  function termMatches(token, term) {
114
- return term.prefix ? token.startsWith(term.term) : token === term.term;
115
- }
116
- /**
117
- * Compile-time cap on query terms: the columnar fast path packs per-term presence into one
118
- * 32-bit mask per dictionary entry.
119
- */
120
- const queryTermsCache = new Map();
121
- /** Tokenized query terms with the same bounded caching scheme as the LIKE pattern cache. */
122
- export function cachedQueryTerms(query) {
123
- const cached = queryTermsCache.get(query);
124
- if (cached !== undefined)
125
- return cached;
126
- const terms = tokenizeQuery(query);
127
- if (query.length > MAX_CACHEABLE_TEXT_CHARACTERS)
128
- return terms;
129
- if (queryTermsCache.size >= 128)
130
- queryTermsCache.clear();
131
- queryTermsCache.set(query, terms);
84
+ return term.prefix ? token.startsWith(term.term) : token === term.term;
85
+ }
86
+ const queryTermsCache = /* @__PURE__ */ new Map();
87
+ function cachedQueryTerms(query) {
88
+ const cached = queryTermsCache.get(query);
89
+ if (cached !== void 0)
90
+ return cached;
91
+ const terms = tokenizeQuery(query);
92
+ if (query.length > MAX_CACHEABLE_TEXT_CHARACTERS)
132
93
  return terms;
133
- }
134
- /** Compile-time validation shared by the SQL parser and the DSL expression builder. */
135
- export function validateFtsQuery(query) {
136
- if (cachedQueryTerms(query).length > MAX_FTS_QUERY_TERMS) {
137
- throw new TypeError(`Full-text queries support at most ${String(MAX_FTS_QUERY_TERMS)} terms`);
138
- }
139
- }
140
- /** Bitmask of query terms present in a token list: bit i set means terms[i] matched. */
141
- export function termsMask(tokens, terms) {
142
- let mask = 0;
143
- for (const token of tokens) {
144
- for (let index = 0; index < terms.length; index += 1) {
145
- const term = terms[index];
146
- if (term !== undefined && (mask & (1 << index)) === 0 && termMatches(token, term)) {
147
- mask |= 1 << index;
148
- }
94
+ if (queryTermsCache.size >= 128)
95
+ queryTermsCache.clear();
96
+ queryTermsCache.set(query, terms);
97
+ return terms;
98
+ }
99
+ function validateFtsQuery(query) {
100
+ if (cachedQueryTerms(query).length > MAX_FTS_QUERY_TERMS) {
101
+ throw new TypeError(`Full-text queries support at most ${String(MAX_FTS_QUERY_TERMS)} terms`);
102
+ }
103
+ }
104
+ function termsMask(tokens, terms) {
105
+ let mask = 0;
106
+ for (const token of tokens) {
107
+ for (let index = 0; index < terms.length; index += 1) {
108
+ const term = terms[index];
109
+ if (term !== void 0 && (mask & 1 << index) === 0 && termMatches(token, term)) {
110
+ mask |= 1 << index;
111
+ }
112
+ }
113
+ }
114
+ return mask;
115
+ }
116
+ function fullTermsMask(termCount) {
117
+ return termCount >= 32 ? -1 : (1 << termCount) - 1;
118
+ }
119
+ function ftsMatchTruth(terms, values) {
120
+ let anyPresent = false;
121
+ const present = new Array(terms.length).fill(false);
122
+ let satisfied = 0;
123
+ for (const value of values) {
124
+ const rendered = renderDocumentValue(value);
125
+ if (rendered === void 0)
126
+ continue;
127
+ anyPresent = true;
128
+ if (terms.length === 0 || satisfied === terms.length)
129
+ continue;
130
+ for (const token of tokenize(rendered)) {
131
+ for (let index = 0; index < terms.length; index += 1) {
132
+ const term = terms[index];
133
+ if (term !== void 0 && present[index] !== true && termMatches(token, term)) {
134
+ present[index] = true;
135
+ satisfied += 1;
149
136
  }
150
- }
151
- return mask;
152
- }
153
- /** The all-terms-present mask; terms.length is capped at 32 so the shift stays in range. */
154
- export function fullTermsMask(termCount) {
155
- return termCount >= 32 ? -1 : (1 << termCount) - 1;
156
- }
157
- /**
158
- * Document-level MATCH truth over one row's rendered column values: every term must appear in
159
- * some column (AND across terms, OR across columns). All columns null → SQL unknown (null);
160
- * an empty query matches nothing.
161
- */
162
- export function ftsMatchTruth(terms, values) {
163
- let anyPresent = false;
164
- const present = new Array(terms.length).fill(false);
165
- let satisfied = 0;
166
- for (const value of values) {
167
- const rendered = renderDocumentValue(value);
168
- if (rendered === undefined)
169
- continue;
170
- anyPresent = true;
171
- if (terms.length === 0 || satisfied === terms.length)
172
- continue;
173
- for (const token of tokenize(rendered)) {
174
- for (let index = 0; index < terms.length; index += 1) {
175
- const term = terms[index];
176
- if (term !== undefined && present[index] !== true && termMatches(token, term)) {
177
- present[index] = true;
178
- satisfied += 1;
179
- }
180
- }
181
- if (satisfied === terms.length)
182
- break;
183
- }
184
- }
185
- if (!anyPresent)
186
- return null;
187
- return terms.length > 0 && satisfied === terms.length;
188
- }
189
- /**
190
- * Per-term frequencies of the query's terms in one token list, aligned with the term order.
191
- * A document matches when every slot is non-zero.
192
- */
193
- export function termFrequencies(tokens, terms) {
194
- const frequencies = new Array(terms.length).fill(0);
195
- for (const token of tokens) {
196
- for (let index = 0; index < terms.length; index += 1) {
197
- const term = terms[index];
198
- if (term !== undefined && termMatches(token, term)) {
199
- frequencies[index] = (frequencies[index] ?? 0) + 1;
200
- }
201
- }
202
- }
203
- return frequencies;
204
- }
205
- /**
206
- * One term's BM25 contribution. Integer inputs only — see the module comment. `df` of zero
207
- * contributes nothing (the term appears in no document, so no row scores on it).
208
- */
209
- export function bm25Score(tf, docLength, df, docCount, totalTokens) {
210
- if (tf === 0 || df === 0 || docCount === 0)
211
- return 0;
212
- const idf = Math.log(1 + (docCount - df + 0.5) / (df + 0.5));
213
- const averageLength = totalTokens / docCount;
214
- const normalized = (tf * (BM25_K1 + 1)) /
215
- (tf +
216
- BM25_K1 * (1 - BM25_B + (BM25_B * docLength) / (averageLength === 0 ? 1 : averageLength)));
217
- return idf * normalized;
218
- }
219
- /** Sums the contributions of every query term for one document. */
220
- export function bm25DocumentScore(frequencies, docLength, stats) {
221
- let score = 0;
137
+ }
138
+ if (satisfied === terms.length)
139
+ break;
140
+ }
141
+ }
142
+ if (!anyPresent)
143
+ return null;
144
+ return terms.length > 0 && satisfied === terms.length;
145
+ }
146
+ function termFrequencies(tokens, terms) {
147
+ const frequencies = new Array(terms.length).fill(0);
148
+ for (const token of tokens) {
149
+ for (let index = 0; index < terms.length; index += 1) {
150
+ const term = terms[index];
151
+ if (term !== void 0 && termMatches(token, term)) {
152
+ frequencies[index] = (frequencies[index] ?? 0) + 1;
153
+ }
154
+ }
155
+ }
156
+ return frequencies;
157
+ }
158
+ function bm25Score(tf, docLength, df, docCount, totalTokens) {
159
+ if (tf === 0 || df === 0 || docCount === 0)
160
+ return 0;
161
+ const idf = Math.log(1 + (docCount - df + 0.5) / (df + 0.5));
162
+ const averageLength = totalTokens / docCount;
163
+ const normalized = tf * (BM25_K1 + 1) / (tf + BM25_K1 * (1 - BM25_B + BM25_B * docLength / (averageLength === 0 ? 1 : averageLength)));
164
+ return idf * normalized;
165
+ }
166
+ function bm25DocumentScore(frequencies, docLength, stats) {
167
+ let score = 0;
168
+ for (let index = 0; index < frequencies.length; index += 1) {
169
+ score += bm25Score(frequencies[index] ?? 0, docLength, stats.dfByTerm[index] ?? 0, stats.docCount, stats.totalTokens);
170
+ }
171
+ return score;
172
+ }
173
+ class FtsStatsAccumulator {
174
+ #terms;
175
+ #stats;
176
+ constructor(terms) {
177
+ this.#terms = terms;
178
+ this.#stats = {
179
+ docCount: 0,
180
+ totalTokens: 0,
181
+ dfByTerm: new Array(terms.length).fill(0)
182
+ };
183
+ }
184
+ addDocument(tokens) {
185
+ this.#stats.docCount += 1;
186
+ this.#stats.totalTokens += tokens.length;
187
+ const frequencies = termFrequencies(tokens, this.#terms);
222
188
  for (let index = 0; index < frequencies.length; index += 1) {
223
- score += bm25Score(frequencies[index] ?? 0, docLength, stats.dfByTerm[index] ?? 0, stats.docCount, stats.totalTokens);
224
- }
225
- return score;
226
- }
227
- /** Accumulates corpus statistics one document at a time; every producer feeds this. */
228
- export class FtsStatsAccumulator {
229
- #terms;
230
- #stats;
231
- constructor(terms) {
232
- this.#terms = terms;
233
- this.#stats = {
234
- docCount: 0,
235
- totalTokens: 0,
236
- dfByTerm: new Array(terms.length).fill(0),
237
- };
238
- }
239
- /** Adds one document; an all-null row contributes a document of length 0. */
240
- addDocument(tokens) {
241
- this.#stats.docCount += 1;
242
- this.#stats.totalTokens += tokens.length;
243
- const frequencies = termFrequencies(tokens, this.#terms);
244
- for (let index = 0; index < frequencies.length; index += 1) {
245
- if ((frequencies[index] ?? 0) > 0) {
246
- this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
247
- }
248
- }
249
- }
250
- /** Adds one document from pre-aggregated counts (the dictionary-table producers). */
251
- addDocumentCounts(termMask, length) {
252
- this.#stats.docCount += 1;
253
- this.#stats.totalTokens += length;
254
- for (let index = 0; index < this.#terms.length; index += 1) {
255
- if ((termMask & (1 << index)) !== 0) {
256
- this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
257
- }
258
- }
259
- }
260
- get stats() {
261
- return this.#stats;
262
- }
263
- }
264
- /**
265
- * Document-level BM25 for one row's rendered column values: term frequencies and document
266
- * length sum across columns (the document is the concatenation of its fields). All columns
267
- * null SQL null; a document containing no query term scores 0.
268
- */
269
- export function ftsBm25Row(terms, values, stats) {
270
- let anyPresent = false;
271
- let docLength = 0;
272
- const frequencies = new Array(terms.length).fill(0);
273
- for (const value of values) {
274
- const rendered = renderDocumentValue(value);
275
- if (rendered === undefined)
276
- continue;
277
- anyPresent = true;
278
- const tokens = tokenize(rendered);
279
- docLength += tokens.length;
280
- const partial = termFrequencies(tokens, terms);
281
- for (let index = 0; index < terms.length; index += 1) {
282
- frequencies[index] = (frequencies[index] ?? 0) + (partial[index] ?? 0);
283
- }
284
- }
285
- if (!anyPresent)
286
- return null;
287
- return bm25DocumentScore(frequencies, docLength, stats);
288
- }
289
- /** Renders one cell for the document, per column type. Booleans are excluded from documents. */
290
- export function renderDocumentValue(value) {
291
- value = externalSqlDomainValue(value);
292
- if (value === null || value === undefined || typeof value === "boolean")
293
- return undefined;
294
- if (value instanceof Date)
295
- return dateIsoString(value);
296
- if (typeof value === "number")
297
- return String(value);
298
- return typeof value === "string" ? value : undefined;
189
+ if ((frequencies[index] ?? 0) > 0) {
190
+ this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
191
+ }
192
+ }
193
+ }
194
+ addDocumentCounts(termMask, length) {
195
+ this.#stats.docCount += 1;
196
+ this.#stats.totalTokens += length;
197
+ for (let index = 0; index < this.#terms.length; index += 1) {
198
+ if ((termMask & 1 << index) !== 0) {
199
+ this.#stats.dfByTerm[index] = (this.#stats.dfByTerm[index] ?? 0) + 1;
200
+ }
201
+ }
202
+ }
203
+ get stats() {
204
+ return this.#stats;
205
+ }
206
+ }
207
+ function ftsBm25Row(terms, values, stats) {
208
+ let anyPresent = false;
209
+ let docLength = 0;
210
+ const frequencies = new Array(terms.length).fill(0);
211
+ for (const value of values) {
212
+ const rendered = renderDocumentValue(value);
213
+ if (rendered === void 0)
214
+ continue;
215
+ anyPresent = true;
216
+ const tokens = tokenize(rendered);
217
+ docLength += tokens.length;
218
+ const partial = termFrequencies(tokens, terms);
219
+ for (let index = 0; index < terms.length; index += 1) {
220
+ frequencies[index] = (frequencies[index] ?? 0) + (partial[index] ?? 0);
221
+ }
222
+ }
223
+ if (!anyPresent)
224
+ return null;
225
+ return bm25DocumentScore(frequencies, docLength, stats);
226
+ }
227
+ function renderDocumentValue(value) {
228
+ value = externalSqlDomainValue(value);
229
+ if (value === null || value === void 0 || typeof value === "boolean")
230
+ return void 0;
231
+ if (value instanceof Date)
232
+ return dateIsoString(value);
233
+ if (typeof value === "number")
234
+ return String(value);
235
+ return typeof value === "string" ? value : void 0;
299
236
  }
300
237
  import { dateIsoString } from "../date-value.js";
238
+ export {
239
+ FTS_TOKENIZER_VERSION,
240
+ FtsStatsAccumulator,
241
+ bm25DocumentScore,
242
+ bm25Score,
243
+ cachedQueryTerms,
244
+ ftsBm25Row,
245
+ ftsMatchTruth,
246
+ fullTermsMask,
247
+ renderDocumentValue,
248
+ termFrequencies,
249
+ termsMask,
250
+ tokenize,
251
+ tokenizeQuery,
252
+ validateFtsQuery
253
+ };