datalevin-node 1.0.2 → 1.2.10
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/README.md +470 -31
- package/jars/datalevin-runtime-1.2.10.jar +4 -0
- package/package.json +1 -1
- package/src/client.js +5 -3
- package/src/connection.js +87 -40
- package/src/convert.js +111 -7
- package/src/database.js +13 -0
- package/src/entity.js +4 -0
- package/src/form.js +408 -0
- package/src/index.js +38 -1
- package/src/interop.js +119 -7
- package/src/kv.js +27 -1
- package/src/native.js +172 -0
- package/src/prepared.js +88 -0
- package/src/query.js +1352 -0
- package/src/resource.js +2 -0
- package/src/transaction.js +312 -0
- package/src/udf-value.js +267 -0
- package/src/udf.js +128 -76
- package/jars/datalevin-runtime-1.0.2.jar +0 -4
package/src/query.js
ADDED
|
@@ -0,0 +1,1352 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DatalogSymbol,
|
|
3
|
+
Form,
|
|
4
|
+
Keyword,
|
|
5
|
+
RawForm,
|
|
6
|
+
ednList,
|
|
7
|
+
formData,
|
|
8
|
+
immutableSnapshot,
|
|
9
|
+
quote
|
|
10
|
+
} from "./form.js";
|
|
11
|
+
import { udfReference } from "./udf-value.js";
|
|
12
|
+
|
|
13
|
+
export function kw(name) {
|
|
14
|
+
return name instanceof Keyword ? name : new Keyword(name);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function sym(name) {
|
|
18
|
+
return name instanceof DatalogSymbol ? name : new DatalogSymbol(name);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function variable(name) {
|
|
22
|
+
if (name instanceof DatalogSymbol) {
|
|
23
|
+
if (!name.name.startsWith("?")) {
|
|
24
|
+
throw new TypeError(`Expected a Datalog variable, got ${name}.`);
|
|
25
|
+
}
|
|
26
|
+
return name;
|
|
27
|
+
}
|
|
28
|
+
const text = String(name);
|
|
29
|
+
return sym(text.startsWith("?") ? text : `?${text}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const v = variable;
|
|
33
|
+
|
|
34
|
+
export function source(name = "$") {
|
|
35
|
+
if (name instanceof DatalogSymbol) {
|
|
36
|
+
if (!name.name.startsWith("$")) {
|
|
37
|
+
throw new TypeError(`Expected a Datalog source, got ${name}.`);
|
|
38
|
+
}
|
|
39
|
+
return name;
|
|
40
|
+
}
|
|
41
|
+
const text = String(name);
|
|
42
|
+
return sym(text.startsWith("$") ? text : `$${text}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const DB = sym("$");
|
|
46
|
+
export const RULES = sym("%");
|
|
47
|
+
export const IGNORE = sym("_");
|
|
48
|
+
const ELLIPSIS = sym("...");
|
|
49
|
+
const DOT = sym(".");
|
|
50
|
+
|
|
51
|
+
function asForm(value) {
|
|
52
|
+
return value instanceof Form ? value.toForm() : value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function attribute(value) {
|
|
56
|
+
return typeof value === "string" ? kw(value) : value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function callable(value) {
|
|
60
|
+
return typeof value === "string" ? sym(value) : value;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isVariableSymbol(value) {
|
|
64
|
+
return value instanceof DatalogSymbol && value.name.startsWith("?");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isOrderIndex(value) {
|
|
68
|
+
return (
|
|
69
|
+
(typeof value === "number" && Number.isSafeInteger(value) && value >= 0)
|
|
70
|
+
|| (typeof value === "bigint" && value >= 0n)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function plainObject(value) {
|
|
75
|
+
if (value === null || typeof value !== "object") {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
const prototype = Object.getPrototypeOf(value);
|
|
79
|
+
return prototype === Object.prototype || prototype === null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class Expression extends Form {
|
|
83
|
+
constructor(form) {
|
|
84
|
+
super();
|
|
85
|
+
this.form = immutableSnapshot([...form]);
|
|
86
|
+
Object.freeze(this);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toForm() {
|
|
90
|
+
return this.form;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class Clause extends Form {
|
|
95
|
+
constructor(form, kind = "clause") {
|
|
96
|
+
super();
|
|
97
|
+
this.form = immutableSnapshot([...form]);
|
|
98
|
+
this.kind = kind;
|
|
99
|
+
Object.freeze(this);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
toForm() {
|
|
103
|
+
return this.form;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function clauseOperator(value) {
|
|
108
|
+
return value instanceof Clause ? value.kind : null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function rejectAndClauses(values, context) {
|
|
112
|
+
if (values.some((value) => clauseOperator(value) === "and")) {
|
|
113
|
+
throw new TypeError(
|
|
114
|
+
`q.and() is only valid as a branch of q.or() or q.orJoin(), not ${context}.`
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class Binding extends Form {
|
|
120
|
+
constructor(form) {
|
|
121
|
+
super();
|
|
122
|
+
this.form = immutableSnapshot(form);
|
|
123
|
+
Object.freeze(this);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
toForm() {
|
|
127
|
+
return this.form;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class QueryOptions extends Form {
|
|
132
|
+
constructor(entries) {
|
|
133
|
+
super();
|
|
134
|
+
this.data = immutableSnapshot(new Map(entries));
|
|
135
|
+
Object.freeze(this);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
get size() {
|
|
139
|
+
return this.data.size;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
get(key) {
|
|
143
|
+
return this.data.get(key);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
has(key) {
|
|
147
|
+
return this.data.has(key);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
entries() {
|
|
151
|
+
return this.data.entries();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
[Symbol.iterator]() {
|
|
155
|
+
return this.data[Symbol.iterator]();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
toForm() {
|
|
159
|
+
return this.data;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
asData() {
|
|
163
|
+
return formData(this);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export class FulltextOptions extends QueryOptions {}
|
|
168
|
+
export class VectorSearchOptions extends QueryOptions {}
|
|
169
|
+
export class IdocMatchOptions extends QueryOptions {}
|
|
170
|
+
|
|
171
|
+
const FULLTEXT_DISPLAYS = new Map([
|
|
172
|
+
["refs", 3],
|
|
173
|
+
["refs+scores", 4],
|
|
174
|
+
["texts", 4],
|
|
175
|
+
["offsets", 4],
|
|
176
|
+
["texts+offsets", 5]
|
|
177
|
+
]);
|
|
178
|
+
const VECTOR_DISPLAYS = new Map([
|
|
179
|
+
["refs", 3],
|
|
180
|
+
["refs+dists", 4]
|
|
181
|
+
]);
|
|
182
|
+
|
|
183
|
+
function optionName(value) {
|
|
184
|
+
if (value instanceof Keyword) {
|
|
185
|
+
return value.name;
|
|
186
|
+
}
|
|
187
|
+
if (typeof value !== "string") {
|
|
188
|
+
throw new TypeError("Query option keys must be keywords or strings.");
|
|
189
|
+
}
|
|
190
|
+
return kw(value).name;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function nonnegativeOption(name, value) {
|
|
194
|
+
const valid = (
|
|
195
|
+
(typeof value === "number" && Number.isSafeInteger(value) && value >= 0)
|
|
196
|
+
|| (typeof value === "bigint" && value >= 0n)
|
|
197
|
+
);
|
|
198
|
+
if (!valid) {
|
|
199
|
+
throw new TypeError(`${name} must be a non-negative integer.`);
|
|
200
|
+
}
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function domainOption(value) {
|
|
205
|
+
if (typeof value === "string" || value === null || !value?.[Symbol.iterator]) {
|
|
206
|
+
throw new TypeError("Search domains must be a sequence of strings.");
|
|
207
|
+
}
|
|
208
|
+
const domains = [...value];
|
|
209
|
+
if (domains.length === 0) {
|
|
210
|
+
throw new TypeError("Search domains must not be empty.");
|
|
211
|
+
}
|
|
212
|
+
if (!domains.every((domain) => typeof domain === "string" && domain.length > 0)) {
|
|
213
|
+
throw new TypeError("Search domains must contain non-empty strings.");
|
|
214
|
+
}
|
|
215
|
+
return domains;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function displayOption(value, supported) {
|
|
219
|
+
const display = typeof value === "string" ? kw(value) : value;
|
|
220
|
+
if (!(display instanceof Keyword) || !supported.has(display.name)) {
|
|
221
|
+
const expected = [...supported.keys()].map((name) => `:${name}`).join(", ");
|
|
222
|
+
throw new TypeError(`Unsupported search display; expected one of ${expected}.`);
|
|
223
|
+
}
|
|
224
|
+
return display;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function extraOptionEntries(extra) {
|
|
228
|
+
if (extra === null || extra === undefined) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
if (extra instanceof Map) {
|
|
232
|
+
return [...extra.entries()];
|
|
233
|
+
}
|
|
234
|
+
if (plainObject(extra)) {
|
|
235
|
+
return Object.entries(extra);
|
|
236
|
+
}
|
|
237
|
+
throw new TypeError("extra query options must be a Map or plain object.");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function queryOptions(
|
|
241
|
+
OptionType,
|
|
242
|
+
values,
|
|
243
|
+
{ extra = null, displays = null, nonnegative = new Set() } = {}
|
|
244
|
+
) {
|
|
245
|
+
const raw = new Map();
|
|
246
|
+
for (const [name, value] of values) {
|
|
247
|
+
if (value !== null && value !== undefined) {
|
|
248
|
+
raw.set(name, value);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const [key, value] of extraOptionEntries(extra)) {
|
|
252
|
+
raw.set(optionName(key), value);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const entries = [];
|
|
256
|
+
for (const [name, original] of raw) {
|
|
257
|
+
let value = original;
|
|
258
|
+
if (name === "domains") {
|
|
259
|
+
value = domainOption(value);
|
|
260
|
+
} else if (name === "display" && displays !== null) {
|
|
261
|
+
value = displayOption(value, displays);
|
|
262
|
+
} else if (nonnegative.has(name)) {
|
|
263
|
+
value = nonnegativeOption(name, value);
|
|
264
|
+
}
|
|
265
|
+
entries.push([kw(name), immutableSnapshot(value)]);
|
|
266
|
+
}
|
|
267
|
+
return new OptionType(entries);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function fulltextOptions({
|
|
271
|
+
top = null,
|
|
272
|
+
limit = null,
|
|
273
|
+
offset = null,
|
|
274
|
+
pagingCachePages = null,
|
|
275
|
+
display = null,
|
|
276
|
+
domains = null,
|
|
277
|
+
proximityExpansion = null,
|
|
278
|
+
proximityMaxDist = null,
|
|
279
|
+
docFilter = null,
|
|
280
|
+
extra = null
|
|
281
|
+
} = {}) {
|
|
282
|
+
return queryOptions(
|
|
283
|
+
FulltextOptions,
|
|
284
|
+
[
|
|
285
|
+
["top", top],
|
|
286
|
+
["limit", limit],
|
|
287
|
+
["offset", offset],
|
|
288
|
+
["paging-cache-pages", pagingCachePages],
|
|
289
|
+
["display", display],
|
|
290
|
+
["domains", domains],
|
|
291
|
+
["proximity-expansion", proximityExpansion],
|
|
292
|
+
["proximity-max-dist", proximityMaxDist],
|
|
293
|
+
["doc-filter", docFilter]
|
|
294
|
+
],
|
|
295
|
+
{
|
|
296
|
+
extra,
|
|
297
|
+
displays: FULLTEXT_DISPLAYS,
|
|
298
|
+
nonnegative: new Set(["top", "limit", "offset", "paging-cache-pages"])
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function vectorSearchOptions({
|
|
304
|
+
top = null,
|
|
305
|
+
display = null,
|
|
306
|
+
domains = null,
|
|
307
|
+
vecFilter = null,
|
|
308
|
+
extra = null
|
|
309
|
+
} = {}) {
|
|
310
|
+
return queryOptions(
|
|
311
|
+
VectorSearchOptions,
|
|
312
|
+
[
|
|
313
|
+
["top", top],
|
|
314
|
+
["display", display],
|
|
315
|
+
["domains", domains],
|
|
316
|
+
["vec-filter", vecFilter]
|
|
317
|
+
],
|
|
318
|
+
{
|
|
319
|
+
extra,
|
|
320
|
+
displays: VECTOR_DISPLAYS,
|
|
321
|
+
nonnegative: new Set(["top"])
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function idocMatchOptions({ domains = null, extra = null } = {}) {
|
|
327
|
+
return queryOptions(
|
|
328
|
+
IdocMatchOptions,
|
|
329
|
+
[["domains", domains]],
|
|
330
|
+
{ extra }
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export class FindSpec extends Form {
|
|
335
|
+
constructor(form) {
|
|
336
|
+
super();
|
|
337
|
+
this.form = immutableSnapshot([...form]);
|
|
338
|
+
Object.freeze(this);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
toForm() {
|
|
342
|
+
return this.form;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function findSpecDetails(find) {
|
|
347
|
+
const form = find.form;
|
|
348
|
+
if (form.length === 2 && form.at(-1) === DOT) {
|
|
349
|
+
return { shape: "scalar", elements: [form[0]] };
|
|
350
|
+
}
|
|
351
|
+
if (form.length > 2 && form.at(-1) === DOT) {
|
|
352
|
+
return { shape: "tuple", elements: form.slice(0, -1) };
|
|
353
|
+
}
|
|
354
|
+
if (form.length === 1 && Array.isArray(form[0])) {
|
|
355
|
+
const nested = form[0];
|
|
356
|
+
if (nested.length === 2 && nested.at(-1) === ELLIPSIS) {
|
|
357
|
+
return { shape: "collection", elements: [nested[0]] };
|
|
358
|
+
}
|
|
359
|
+
return { shape: "tuple", elements: nested };
|
|
360
|
+
}
|
|
361
|
+
return { shape: "relation", elements: form };
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function findElementVariables(value) {
|
|
365
|
+
if (isVariableSymbol(value)) {
|
|
366
|
+
return [value];
|
|
367
|
+
}
|
|
368
|
+
if (!(value instanceof Expression) || value.form.length === 0) {
|
|
369
|
+
return [];
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const operator = value.form[0];
|
|
373
|
+
const name = operator instanceof DatalogSymbol ? operator.name : null;
|
|
374
|
+
if (name === "pull") {
|
|
375
|
+
const variableIndex = value.form.length === 4 ? 2 : 1;
|
|
376
|
+
const variableValue = value.form[variableIndex];
|
|
377
|
+
return isVariableSymbol(variableValue) ? [variableValue] : [];
|
|
378
|
+
}
|
|
379
|
+
if (["+", "-", "*", "/", "mod", "rem", "quot"].includes(name)) {
|
|
380
|
+
return value.form.slice(1).flatMap(findElementVariables);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const args = name === "aggregate" ? value.form.slice(2) : value.form.slice(1);
|
|
384
|
+
return args.length > 0 && isVariableSymbol(args.at(-1)) ? [args.at(-1)] : [];
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function findVariables(find) {
|
|
388
|
+
return new Set(findSpecDetails(find).elements.flatMap(findElementVariables));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export class Order {
|
|
392
|
+
constructor(term, direction) {
|
|
393
|
+
this.term = immutableSnapshot(term);
|
|
394
|
+
if (!isVariableSymbol(this.term) && !isOrderIndex(this.term)) {
|
|
395
|
+
throw new TypeError("An order term must be a query variable or non-negative index.");
|
|
396
|
+
}
|
|
397
|
+
if (!(direction instanceof Keyword) || !["asc", "desc"].includes(direction.name)) {
|
|
398
|
+
throw new TypeError("An order direction must be :asc or :desc.");
|
|
399
|
+
}
|
|
400
|
+
this.direction = direction;
|
|
401
|
+
Object.freeze(this);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export class PullAttr extends Form {
|
|
406
|
+
constructor(attributeName, options = {}) {
|
|
407
|
+
super();
|
|
408
|
+
if (!plainObject(options)) {
|
|
409
|
+
throw new TypeError("Pull attribute options must be an object.");
|
|
410
|
+
}
|
|
411
|
+
const allowed = new Set(["as", "limit", "default", "xform"]);
|
|
412
|
+
for (const key of Object.keys(options)) {
|
|
413
|
+
if (!allowed.has(key)) {
|
|
414
|
+
throw new TypeError(`Unknown pull attribute option: ${key}.`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const entries = [];
|
|
419
|
+
if (Object.hasOwn(options, "as")) {
|
|
420
|
+
entries.push([kw("as"), options.as]);
|
|
421
|
+
}
|
|
422
|
+
if (Object.hasOwn(options, "limit")) {
|
|
423
|
+
entries.push([kw("limit"), pullLimit(options.limit)]);
|
|
424
|
+
}
|
|
425
|
+
if (Object.hasOwn(options, "default")) {
|
|
426
|
+
entries.push([kw("default"), options.default]);
|
|
427
|
+
}
|
|
428
|
+
if (Object.hasOwn(options, "xform")) {
|
|
429
|
+
entries.push([kw("xform"), pullXform(options.xform)]);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
this.attribute = immutableSnapshot(attribute(attributeName));
|
|
433
|
+
this.options = immutableSnapshot(entries);
|
|
434
|
+
Object.freeze(this);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
toForm() {
|
|
438
|
+
const result = [this.attribute];
|
|
439
|
+
for (const [key, value] of this.options) {
|
|
440
|
+
result.push(key, value);
|
|
441
|
+
}
|
|
442
|
+
return immutableSnapshot(result);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
asData() {
|
|
446
|
+
return formData(this);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export class PullNested extends Form {
|
|
451
|
+
constructor(attributeSpec, pattern) {
|
|
452
|
+
super();
|
|
453
|
+
this.attribute = immutableSnapshot(pullAttrSpec(attributeSpec));
|
|
454
|
+
this.pattern = immutableSnapshot(pullNestedPattern(pattern));
|
|
455
|
+
Object.freeze(this);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
toForm() {
|
|
459
|
+
return immutableSnapshot(new Map([[this.attribute, this.pattern]]));
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
asData() {
|
|
463
|
+
return formData(this);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export class PullSelector extends Form {
|
|
468
|
+
constructor(items = []) {
|
|
469
|
+
super();
|
|
470
|
+
this.items = immutableSnapshot(Array.from(items, pullSelectorItem));
|
|
471
|
+
Object.freeze(this);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get length() {
|
|
475
|
+
return this.items.length;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
at(index) {
|
|
479
|
+
return this.items.at(index);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
[Symbol.iterator]() {
|
|
483
|
+
return this.items[Symbol.iterator]();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
toForm() {
|
|
487
|
+
return this.items;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
asData() {
|
|
491
|
+
return formData(this);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export class JoinVars extends Form {
|
|
496
|
+
constructor(free = [], { required = [] } = {}) {
|
|
497
|
+
super();
|
|
498
|
+
if (!Array.isArray(free) || !Array.isArray(required)) {
|
|
499
|
+
throw new TypeError("Required and free join variables must be arrays.");
|
|
500
|
+
}
|
|
501
|
+
this.required = immutableSnapshot([...required]);
|
|
502
|
+
this.free = immutableSnapshot([...free]);
|
|
503
|
+
const variables = [...this.required, ...this.free];
|
|
504
|
+
if (variables.length === 0) {
|
|
505
|
+
throw new TypeError("Join variables must not be empty.");
|
|
506
|
+
}
|
|
507
|
+
if (!variables.every(isVariableSymbol)) {
|
|
508
|
+
throw new TypeError("Join variables must be values created by q.var().");
|
|
509
|
+
}
|
|
510
|
+
if (new Set(variables.map((variableValue) => variableValue.name)).size !== variables.length) {
|
|
511
|
+
throw new TypeError("Join variables must be distinct.");
|
|
512
|
+
}
|
|
513
|
+
Object.freeze(this);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
toForm() {
|
|
517
|
+
return immutableSnapshot(this.required.length > 0
|
|
518
|
+
? [this.required, ...this.free]
|
|
519
|
+
: [...this.free]);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export class RuleBranch extends Form {
|
|
524
|
+
constructor(name, variables, clauses) {
|
|
525
|
+
super();
|
|
526
|
+
this.name = sym(name);
|
|
527
|
+
this.variables = normalizeJoinVars(variables);
|
|
528
|
+
this.clauses = immutableSnapshot([...clauses]);
|
|
529
|
+
if (
|
|
530
|
+
this.name.name.startsWith("?")
|
|
531
|
+
|| this.name.name.startsWith("$")
|
|
532
|
+
|| this.name.name === "%"
|
|
533
|
+
|| this.name.name === "_"
|
|
534
|
+
) {
|
|
535
|
+
throw new TypeError("A rule name must be a plain Datalog symbol.");
|
|
536
|
+
}
|
|
537
|
+
if (this.clauses.length === 0) {
|
|
538
|
+
throw new TypeError("A rule branch requires at least one clause.");
|
|
539
|
+
}
|
|
540
|
+
rejectAndClauses(this.clauses, "at the top level of a rule branch");
|
|
541
|
+
Object.freeze(this);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
toForm() {
|
|
545
|
+
return immutableSnapshot([
|
|
546
|
+
[this.name, ...this.variables.toForm()],
|
|
547
|
+
...this.clauses.map(asForm)
|
|
548
|
+
]);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
export class RuleSet extends Form {
|
|
553
|
+
constructor(branches) {
|
|
554
|
+
super();
|
|
555
|
+
this.branches = immutableSnapshot([...branches]);
|
|
556
|
+
if (this.branches.length === 0) {
|
|
557
|
+
throw new TypeError("A rule set requires at least one branch.");
|
|
558
|
+
}
|
|
559
|
+
if (!this.branches.every((branch) => branch instanceof RuleBranch)) {
|
|
560
|
+
throw new TypeError("A rule set accepts only q.ruleBranch() values.");
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const arities = new Map();
|
|
564
|
+
for (const branch of this.branches) {
|
|
565
|
+
const arity = [branch.variables.required.length, branch.variables.free.length];
|
|
566
|
+
const previous = arities.get(branch.name.name);
|
|
567
|
+
if (previous !== undefined && (previous[0] !== arity[0] || previous[1] !== arity[1])) {
|
|
568
|
+
throw new TypeError(
|
|
569
|
+
`Rule branches named ${branch.name} must have matching required/free arity.`
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
arities.set(branch.name.name, arity);
|
|
573
|
+
}
|
|
574
|
+
Object.freeze(this);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
toForm() {
|
|
578
|
+
return immutableSnapshot(this.branches.map((branch) => branch.toForm()));
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
asData() {
|
|
582
|
+
return formData(this);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function normalizeReturnMap(find, mode, values) {
|
|
587
|
+
if (typeof values === "string" || values?.[Symbol.iterator] === undefined) {
|
|
588
|
+
throw new TypeError(`${mode} must be an iterable of field names.`);
|
|
589
|
+
}
|
|
590
|
+
const names = Array.from(
|
|
591
|
+
values,
|
|
592
|
+
(name) => typeof name === "string" ? sym(name) : name
|
|
593
|
+
);
|
|
594
|
+
if (names.length === 0) {
|
|
595
|
+
throw new TypeError(`${mode} requires at least one field name.`);
|
|
596
|
+
}
|
|
597
|
+
if (!names.every((name) => name instanceof DatalogSymbol)) {
|
|
598
|
+
throw new TypeError(`${mode} field names must be symbols or strings.`);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const { shape, elements } = findSpecDetails(find);
|
|
602
|
+
if (shape === "scalar" || shape === "collection") {
|
|
603
|
+
throw new TypeError(`${mode} does not work with a ${shape} find specification.`);
|
|
604
|
+
}
|
|
605
|
+
if (names.length !== elements.length) {
|
|
606
|
+
throw new TypeError(
|
|
607
|
+
`${mode} field count must match the ${elements.length} find elements.`
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
return immutableSnapshot([kw(mode), names]);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function validateQueryOrder(find, ordering) {
|
|
614
|
+
const variables = findVariables(find);
|
|
615
|
+
const { elements } = findSpecDetails(find);
|
|
616
|
+
const seen = new Set();
|
|
617
|
+
for (const item of ordering) {
|
|
618
|
+
const term = item.term;
|
|
619
|
+
const key = isOrderIndex(term) ? `index:${term}` : `variable:${term.name}`;
|
|
620
|
+
if (seen.has(key)) {
|
|
621
|
+
throw new TypeError("Order terms must be distinct.");
|
|
622
|
+
}
|
|
623
|
+
seen.add(key);
|
|
624
|
+
if (isOrderIndex(term)) {
|
|
625
|
+
if (
|
|
626
|
+
(typeof term === "bigint" && term >= BigInt(elements.length))
|
|
627
|
+
|| (typeof term === "number" && term >= elements.length)
|
|
628
|
+
) {
|
|
629
|
+
throw new TypeError("Order column index is outside the find specification.");
|
|
630
|
+
}
|
|
631
|
+
} else if (!variables.has(term)) {
|
|
632
|
+
throw new TypeError("An order variable must occur in the find specification.");
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
export class Query extends Form {
|
|
638
|
+
constructor({
|
|
639
|
+
find,
|
|
640
|
+
where = [],
|
|
641
|
+
inputs = [],
|
|
642
|
+
with: withVars = [],
|
|
643
|
+
having = [],
|
|
644
|
+
orderBy = [],
|
|
645
|
+
limit = null,
|
|
646
|
+
offset = null,
|
|
647
|
+
timeout = null,
|
|
648
|
+
keys = null,
|
|
649
|
+
strs = null,
|
|
650
|
+
syms = null
|
|
651
|
+
} = {}) {
|
|
652
|
+
super();
|
|
653
|
+
if (find === undefined || find === null) {
|
|
654
|
+
throw new TypeError("query() requires a find specification.");
|
|
655
|
+
}
|
|
656
|
+
this.find = find instanceof FindSpec ? find : relation(...findValues(find));
|
|
657
|
+
this.where = immutableSnapshot([...where]);
|
|
658
|
+
this.inputs = immutableSnapshot([...inputs]);
|
|
659
|
+
this.withVars = immutableSnapshot([...withVars]);
|
|
660
|
+
this.having = immutableSnapshot([...having]);
|
|
661
|
+
this.orderBy = immutableSnapshot(
|
|
662
|
+
orderBy.map((item) => item instanceof Order ? item : asc(item))
|
|
663
|
+
);
|
|
664
|
+
rejectAndClauses(this.where, "at the top level of :where");
|
|
665
|
+
rejectAndClauses(this.having, "at the top level of :having");
|
|
666
|
+
validateQueryOrder(this.find, this.orderBy);
|
|
667
|
+
this.limit = limit;
|
|
668
|
+
this.offset = offset;
|
|
669
|
+
this.timeout = timeout;
|
|
670
|
+
|
|
671
|
+
const selected = Object.entries({ keys, strs, syms }).filter(([, names]) => names !== null);
|
|
672
|
+
if (selected.length > 1) {
|
|
673
|
+
throw new TypeError("Only one of keys, strs, or syms may be supplied.");
|
|
674
|
+
}
|
|
675
|
+
if (selected.length === 1) {
|
|
676
|
+
const [mode, names] = selected[0];
|
|
677
|
+
this.returnMap = normalizeReturnMap(this.find, mode, names);
|
|
678
|
+
} else {
|
|
679
|
+
this.returnMap = null;
|
|
680
|
+
}
|
|
681
|
+
Object.freeze(this);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
toForm() {
|
|
685
|
+
const result = [kw("find"), ...this.find.toForm()];
|
|
686
|
+
if (this.returnMap !== null) {
|
|
687
|
+
result.push(this.returnMap[0], ...this.returnMap[1]);
|
|
688
|
+
}
|
|
689
|
+
if (this.withVars.length > 0) {
|
|
690
|
+
result.push(kw("with"), ...this.withVars);
|
|
691
|
+
}
|
|
692
|
+
if (this.inputs.length > 0) {
|
|
693
|
+
result.push(kw("in"), ...this.inputs);
|
|
694
|
+
}
|
|
695
|
+
if (this.where.length > 0) {
|
|
696
|
+
result.push(kw("where"), ...this.where.map(asForm));
|
|
697
|
+
}
|
|
698
|
+
if (this.having.length > 0) {
|
|
699
|
+
result.push(kw("having"), ...this.having.map(asForm));
|
|
700
|
+
}
|
|
701
|
+
if (this.timeout !== null && this.timeout !== undefined) {
|
|
702
|
+
result.push(kw("timeout"), this.timeout);
|
|
703
|
+
}
|
|
704
|
+
if (this.orderBy.length > 0) {
|
|
705
|
+
const ordering = [];
|
|
706
|
+
for (const item of this.orderBy) {
|
|
707
|
+
ordering.push(item.term, item.direction);
|
|
708
|
+
}
|
|
709
|
+
result.push(kw("order-by"), ordering);
|
|
710
|
+
}
|
|
711
|
+
if (this.offset !== null && this.offset !== undefined) {
|
|
712
|
+
result.push(kw("offset"), this.offset);
|
|
713
|
+
}
|
|
714
|
+
if (this.limit !== null && this.limit !== undefined) {
|
|
715
|
+
result.push(kw("limit"), this.limit);
|
|
716
|
+
}
|
|
717
|
+
return immutableSnapshot(result);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
asData() {
|
|
721
|
+
return formData(this);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
function findValues(value) {
|
|
726
|
+
if (
|
|
727
|
+
typeof value === "string"
|
|
728
|
+
|| value instanceof Keyword
|
|
729
|
+
|| value instanceof DatalogSymbol
|
|
730
|
+
|| value instanceof Expression
|
|
731
|
+
|| value instanceof RawForm
|
|
732
|
+
|| !value?.[Symbol.iterator]
|
|
733
|
+
) {
|
|
734
|
+
return [value];
|
|
735
|
+
}
|
|
736
|
+
return [...value];
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
export function query(options) {
|
|
740
|
+
return new Query(options);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
export function relation(...expressions) {
|
|
744
|
+
return new FindSpec(expressions);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
export function collection(expression) {
|
|
748
|
+
return new FindSpec([[expression, ELLIPSIS]]);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
export function tupleFind(...expressions) {
|
|
752
|
+
return new FindSpec([expressions]);
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
export function scalar(expression) {
|
|
756
|
+
return new FindSpec([expression, DOT]);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
export function tupleScalar(...expressions) {
|
|
760
|
+
return new FindSpec([...expressions, DOT]);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
export function call(functionName, ...args) {
|
|
764
|
+
return new Expression([callable(functionName), ...args]);
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
export function udf(reference, ...args) {
|
|
768
|
+
return call("udf", udfReference(reference), ...args);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
export function aggregate(functionName, ...args) {
|
|
772
|
+
return call(functionName, ...args);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
export function customAggregate(functionVariable, ...args) {
|
|
776
|
+
return new Expression([sym("aggregate"), functionVariable, ...args]);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
export function expression(operator, ...args) {
|
|
780
|
+
return call(operator, ...args);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function pullLimit(value) {
|
|
784
|
+
const validNumber = typeof value === "number" && Number.isInteger(value) && value > 0;
|
|
785
|
+
const validBigInt = typeof value === "bigint" && value > 0n;
|
|
786
|
+
if (value !== null && !validNumber && !validBigInt) {
|
|
787
|
+
throw new TypeError("A pull limit must be a positive integer or null.");
|
|
788
|
+
}
|
|
789
|
+
return value;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
function pullXform(value) {
|
|
793
|
+
if (typeof value === "string") {
|
|
794
|
+
return sym(value);
|
|
795
|
+
}
|
|
796
|
+
if (!(value instanceof DatalogSymbol)) {
|
|
797
|
+
throw new TypeError("A pull xform must be a symbol or symbol name.");
|
|
798
|
+
}
|
|
799
|
+
return value;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
function pullTokenName(value) {
|
|
803
|
+
if (value instanceof Keyword || value instanceof DatalogSymbol) {
|
|
804
|
+
return value.name;
|
|
805
|
+
}
|
|
806
|
+
if (typeof value === "string") {
|
|
807
|
+
return value.startsWith(":") ? value.slice(1) : value;
|
|
808
|
+
}
|
|
809
|
+
return null;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function pullAttrExpression(values) {
|
|
813
|
+
if (values.length === 0) {
|
|
814
|
+
throw new TypeError("A pull attribute expression must not be empty.");
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const legacyOp = pullTokenName(values[0]);
|
|
818
|
+
if ((legacyOp === "default" || legacyOp === "limit") && values.length === 3) {
|
|
819
|
+
const value = legacyOp === "limit" ? pullLimit(values[2]) : values[2];
|
|
820
|
+
return [sym(legacyOp), pullAttrSpec(values[1]), value];
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
if (values.length % 2 === 0) {
|
|
824
|
+
throw new TypeError("A pull attribute expression requires option/value pairs.");
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
const result = [pullAttrSpec(values[0])];
|
|
828
|
+
for (let index = 1; index < values.length; index += 2) {
|
|
829
|
+
const optionName = pullTokenName(values[index]);
|
|
830
|
+
if (!["as", "limit", "default", "xform"].includes(optionName)) {
|
|
831
|
+
throw new TypeError(`Unknown pull attribute option: ${String(values[index])}.`);
|
|
832
|
+
}
|
|
833
|
+
let value = values[index + 1];
|
|
834
|
+
if (optionName === "limit") {
|
|
835
|
+
value = pullLimit(value);
|
|
836
|
+
} else if (optionName === "xform") {
|
|
837
|
+
value = pullXform(value);
|
|
838
|
+
}
|
|
839
|
+
result.push(kw(optionName), value);
|
|
840
|
+
}
|
|
841
|
+
return result;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function pullAttrSpec(value) {
|
|
845
|
+
if (typeof value === "string") {
|
|
846
|
+
return kw(value);
|
|
847
|
+
}
|
|
848
|
+
if (Array.isArray(value)) {
|
|
849
|
+
return pullAttrExpression(value);
|
|
850
|
+
}
|
|
851
|
+
return value;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function pullNestedPattern(value) {
|
|
855
|
+
if (typeof value === "string") {
|
|
856
|
+
return value === "..." ? ELLIPSIS : new PullSelector([value]);
|
|
857
|
+
}
|
|
858
|
+
if (value instanceof DatalogSymbol && value.name === "...") {
|
|
859
|
+
return value;
|
|
860
|
+
}
|
|
861
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
862
|
+
const validNumber = typeof value === "number" && Number.isInteger(value) && value > 0;
|
|
863
|
+
const validBigInt = typeof value === "bigint" && value > 0n;
|
|
864
|
+
if (!validNumber && !validBigInt) {
|
|
865
|
+
throw new TypeError("A pull recursion depth must be a positive integer.");
|
|
866
|
+
}
|
|
867
|
+
return value;
|
|
868
|
+
}
|
|
869
|
+
if (value instanceof PullSelector || value instanceof RawForm) {
|
|
870
|
+
return value;
|
|
871
|
+
}
|
|
872
|
+
if (Array.isArray(value)) {
|
|
873
|
+
return new PullSelector(value);
|
|
874
|
+
}
|
|
875
|
+
if (value instanceof Map || plainObject(value)) {
|
|
876
|
+
return new PullSelector([value]);
|
|
877
|
+
}
|
|
878
|
+
return value;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
function pullSelectorItem(item) {
|
|
882
|
+
if (typeof item === "string") {
|
|
883
|
+
return item === "*" ? sym("*") : kw(item);
|
|
884
|
+
}
|
|
885
|
+
if (item instanceof Map) {
|
|
886
|
+
return new Map(Array.from(
|
|
887
|
+
item,
|
|
888
|
+
([key, value]) => [pullAttrSpec(key), pullNestedPattern(value)]
|
|
889
|
+
));
|
|
890
|
+
}
|
|
891
|
+
if (plainObject(item)) {
|
|
892
|
+
return new Map(Object.entries(item).map(
|
|
893
|
+
([key, value]) => [pullAttrSpec(key), pullNestedPattern(value)]
|
|
894
|
+
));
|
|
895
|
+
}
|
|
896
|
+
if (Array.isArray(item)) {
|
|
897
|
+
return pullAttrExpression(item);
|
|
898
|
+
}
|
|
899
|
+
return item;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
function pullPattern(pattern) {
|
|
903
|
+
if (
|
|
904
|
+
pattern instanceof PullSelector
|
|
905
|
+
|| pattern instanceof RawForm
|
|
906
|
+
|| pattern instanceof DatalogSymbol
|
|
907
|
+
) {
|
|
908
|
+
return pattern;
|
|
909
|
+
}
|
|
910
|
+
if (typeof pattern === "string") {
|
|
911
|
+
return new PullSelector([pattern]);
|
|
912
|
+
}
|
|
913
|
+
if (Array.isArray(pattern)) {
|
|
914
|
+
return new PullSelector(pattern);
|
|
915
|
+
}
|
|
916
|
+
if (pattern instanceof Map || plainObject(pattern)) {
|
|
917
|
+
return new PullSelector([pattern]);
|
|
918
|
+
}
|
|
919
|
+
return pattern;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
export function selector(...attributes) {
|
|
923
|
+
if (attributes.length === 1 && attributes[0] instanceof PullSelector) {
|
|
924
|
+
return attributes[0];
|
|
925
|
+
}
|
|
926
|
+
if (attributes.length === 1 && Array.isArray(attributes[0])) {
|
|
927
|
+
attributes = attributes[0];
|
|
928
|
+
}
|
|
929
|
+
return new PullSelector(attributes);
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
export function pullAttr(attributeName, options = {}) {
|
|
933
|
+
return new PullAttr(attributeName, options);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
export function pullNested(attributeSpec, pattern) {
|
|
937
|
+
return new PullNested(attributeSpec, pattern);
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
export function pullRecursive(attributeSpec, depth = null) {
|
|
941
|
+
if (depth === null || depth === undefined) {
|
|
942
|
+
return new PullNested(attributeSpec, ELLIPSIS);
|
|
943
|
+
}
|
|
944
|
+
const validNumber = typeof depth === "number" && Number.isInteger(depth) && depth > 0;
|
|
945
|
+
const validBigInt = typeof depth === "bigint" && depth > 0n;
|
|
946
|
+
if (!validNumber && !validBigInt) {
|
|
947
|
+
throw new TypeError("A pull recursion depth must be a positive integer or null.");
|
|
948
|
+
}
|
|
949
|
+
return new PullNested(attributeSpec, depth);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
export function pull(variableValue, pattern, { source: sourceValue = null } = {}) {
|
|
953
|
+
const items = [sym("pull")];
|
|
954
|
+
if (sourceValue !== null && sourceValue !== undefined) {
|
|
955
|
+
items.push(sourceValue);
|
|
956
|
+
}
|
|
957
|
+
items.push(variableValue, pullPattern(pattern));
|
|
958
|
+
return new Expression(items);
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
function databasePattern(sourceValue, terms) {
|
|
962
|
+
if (terms.length === 0) {
|
|
963
|
+
throw new TypeError("A database pattern requires at least one term.");
|
|
964
|
+
}
|
|
965
|
+
const items = sourceValue === null || sourceValue === undefined ? [] : [sourceValue];
|
|
966
|
+
const normalized = [...terms];
|
|
967
|
+
if (normalized.length >= 2) {
|
|
968
|
+
normalized[1] = attribute(normalized[1]);
|
|
969
|
+
}
|
|
970
|
+
items.push(...normalized);
|
|
971
|
+
return new Clause(items);
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
export function pattern(...terms) {
|
|
975
|
+
return databasePattern(null, terms);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export function patternFrom(sourceValue, ...terms) {
|
|
979
|
+
return databasePattern(sourceValue, terms);
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
export function datom(entity, attributeName, value, { source: sourceValue = null } = {}) {
|
|
983
|
+
return databasePattern(sourceValue, [entity, attributeName, value]);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
export function predicate(functionName, ...args) {
|
|
987
|
+
if (functionName instanceof Expression) {
|
|
988
|
+
if (args.length > 0) {
|
|
989
|
+
throw new TypeError("An expression predicate does not accept extra arguments.");
|
|
990
|
+
}
|
|
991
|
+
return new Clause([asForm(functionName)]);
|
|
992
|
+
}
|
|
993
|
+
return new Clause([[callable(functionName), ...args]]);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
export function bind(functionName, bindingValue, ...args) {
|
|
997
|
+
if (functionName instanceof Expression) {
|
|
998
|
+
if (args.length > 0) {
|
|
999
|
+
throw new TypeError("An expression binding does not accept extra arguments.");
|
|
1000
|
+
}
|
|
1001
|
+
return new Clause([asForm(functionName), asForm(bindingValue)]);
|
|
1002
|
+
}
|
|
1003
|
+
return new Clause([[callable(functionName), ...args], asForm(bindingValue)]);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
function searchSource(value) {
|
|
1007
|
+
if (!(value instanceof DatalogSymbol) || !value.name.startsWith("$")) {
|
|
1008
|
+
throw new TypeError("A search source must be a value created by q.source().");
|
|
1009
|
+
}
|
|
1010
|
+
return value;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
function searchAttribute(value) {
|
|
1014
|
+
if (value === null || value === undefined) {
|
|
1015
|
+
return null;
|
|
1016
|
+
}
|
|
1017
|
+
const normalized = attribute(value);
|
|
1018
|
+
if (!(normalized instanceof Keyword)) {
|
|
1019
|
+
throw new TypeError("A search attribute must be a keyword or attribute name.");
|
|
1020
|
+
}
|
|
1021
|
+
return normalized;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
function searchOptionsValue(value, OptionType, builder) {
|
|
1025
|
+
if (value === null || value === undefined || value instanceof OptionType) {
|
|
1026
|
+
return { value: value ?? null, static: true };
|
|
1027
|
+
}
|
|
1028
|
+
if (value instanceof QueryOptions) {
|
|
1029
|
+
throw new TypeError(`Expected ${OptionType.name}, got ${value.constructor.name}.`);
|
|
1030
|
+
}
|
|
1031
|
+
if (value instanceof Map || plainObject(value)) {
|
|
1032
|
+
return { value: builder({ extra: value }), static: true };
|
|
1033
|
+
}
|
|
1034
|
+
if (isVariableSymbol(value) || value instanceof RawForm) {
|
|
1035
|
+
return { value, static: false };
|
|
1036
|
+
}
|
|
1037
|
+
throw new TypeError(
|
|
1038
|
+
`Search options must be ${OptionType.name}, a Map/object, or a query variable.`
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
function relationResultBinding(results, allowedArities, context) {
|
|
1043
|
+
let bindingValue;
|
|
1044
|
+
if (results instanceof Binding) {
|
|
1045
|
+
bindingValue = results;
|
|
1046
|
+
} else {
|
|
1047
|
+
if (typeof results === "string" || !results?.[Symbol.iterator]) {
|
|
1048
|
+
throw new TypeError(`${context} results must be a sequence or relation binding.`);
|
|
1049
|
+
}
|
|
1050
|
+
bindingValue = relationBinding(...results);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
const form = bindingValue.toForm();
|
|
1054
|
+
if (!(Array.isArray(form) && form.length === 1 && Array.isArray(form[0]))) {
|
|
1055
|
+
throw new TypeError(`${context} requires a relation result binding.`);
|
|
1056
|
+
}
|
|
1057
|
+
const items = form[0];
|
|
1058
|
+
if (!items.every((item) => isVariableSymbol(item) || item === IGNORE)) {
|
|
1059
|
+
throw new TypeError(`${context} result items must be q.var() or q.IGNORE values.`);
|
|
1060
|
+
}
|
|
1061
|
+
if (!allowedArities.has(items.length)) {
|
|
1062
|
+
const expected = [...allowedArities].sort().join(", ");
|
|
1063
|
+
throw new TypeError(`${context} requires ${expected} result items.`);
|
|
1064
|
+
}
|
|
1065
|
+
return bindingValue;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function staticDisplayArity(options, displays, defaultArity = 3) {
|
|
1069
|
+
if (options === null) {
|
|
1070
|
+
return new Set([defaultArity]);
|
|
1071
|
+
}
|
|
1072
|
+
const display = options.get(kw("display"));
|
|
1073
|
+
return new Set([display === undefined ? defaultArity : displays.get(display.name)]);
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
export function fulltext(
|
|
1077
|
+
queryValue,
|
|
1078
|
+
results,
|
|
1079
|
+
{ attribute: attributeValue = null, options = null, source: sourceValue = DB } = {}
|
|
1080
|
+
) {
|
|
1081
|
+
const normalizedSource = searchSource(sourceValue);
|
|
1082
|
+
const normalizedAttribute = searchAttribute(attributeValue);
|
|
1083
|
+
const normalizedOptions = searchOptionsValue(options, FulltextOptions, fulltextOptions);
|
|
1084
|
+
const arities = normalizedOptions.static
|
|
1085
|
+
? staticDisplayArity(normalizedOptions.value, FULLTEXT_DISPLAYS)
|
|
1086
|
+
: new Set(FULLTEXT_DISPLAYS.values());
|
|
1087
|
+
const bindingValue = relationResultBinding(results, arities, "q.fulltext");
|
|
1088
|
+
const args = [normalizedSource];
|
|
1089
|
+
if (normalizedAttribute !== null) {
|
|
1090
|
+
args.push(normalizedAttribute);
|
|
1091
|
+
}
|
|
1092
|
+
args.push(queryValue);
|
|
1093
|
+
if (normalizedOptions.value !== null) {
|
|
1094
|
+
args.push(normalizedOptions.value);
|
|
1095
|
+
}
|
|
1096
|
+
return bind("fulltext", bindingValue, ...args);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
export function vecNeighbors(
|
|
1100
|
+
queryVector,
|
|
1101
|
+
results,
|
|
1102
|
+
{ attribute: attributeValue = null, options = null, source: sourceValue = DB } = {}
|
|
1103
|
+
) {
|
|
1104
|
+
const normalizedSource = searchSource(sourceValue);
|
|
1105
|
+
const normalizedAttribute = searchAttribute(attributeValue);
|
|
1106
|
+
const normalizedOptions = searchOptionsValue(
|
|
1107
|
+
options, VectorSearchOptions, vectorSearchOptions
|
|
1108
|
+
);
|
|
1109
|
+
if (normalizedAttribute === null && normalizedOptions.static) {
|
|
1110
|
+
const domains = normalizedOptions.value?.get(kw("domains"));
|
|
1111
|
+
if (domains === undefined || domains.length === 0) {
|
|
1112
|
+
throw new TypeError(
|
|
1113
|
+
"q.vecNeighbors requires an attribute or vector search domains."
|
|
1114
|
+
);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
const arities = normalizedOptions.static
|
|
1118
|
+
? staticDisplayArity(normalizedOptions.value, VECTOR_DISPLAYS)
|
|
1119
|
+
: new Set(VECTOR_DISPLAYS.values());
|
|
1120
|
+
const bindingValue = relationResultBinding(results, arities, "q.vecNeighbors");
|
|
1121
|
+
const args = [normalizedSource];
|
|
1122
|
+
if (normalizedAttribute !== null) {
|
|
1123
|
+
args.push(normalizedAttribute);
|
|
1124
|
+
}
|
|
1125
|
+
args.push(queryVector);
|
|
1126
|
+
if (normalizedOptions.value !== null) {
|
|
1127
|
+
args.push(normalizedOptions.value);
|
|
1128
|
+
}
|
|
1129
|
+
return bind("vec-neighbors", bindingValue, ...args);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
export function idocMatch(
|
|
1133
|
+
queryValue,
|
|
1134
|
+
results,
|
|
1135
|
+
{ attribute: attributeValue = null, options = null, source: sourceValue = DB } = {}
|
|
1136
|
+
) {
|
|
1137
|
+
const normalizedSource = searchSource(sourceValue);
|
|
1138
|
+
const normalizedAttribute = searchAttribute(attributeValue);
|
|
1139
|
+
const normalizedOptions = searchOptionsValue(
|
|
1140
|
+
options, IdocMatchOptions, idocMatchOptions
|
|
1141
|
+
);
|
|
1142
|
+
const bindingValue = relationResultBinding(results, new Set([3]), "q.idocMatch");
|
|
1143
|
+
const args = [normalizedSource];
|
|
1144
|
+
if (normalizedAttribute !== null) {
|
|
1145
|
+
args.push(normalizedAttribute);
|
|
1146
|
+
}
|
|
1147
|
+
args.push(queryValue);
|
|
1148
|
+
if (normalizedOptions.value !== null) {
|
|
1149
|
+
args.push(normalizedOptions.value);
|
|
1150
|
+
}
|
|
1151
|
+
return bind("idoc-match", bindingValue, ...args);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
export function bindUdf(reference, bindingValue, ...args) {
|
|
1155
|
+
return bind(udf(reference, ...args), bindingValue);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
export function udfPredicate(reference, ...args) {
|
|
1159
|
+
return predicate(udf(reference, ...args));
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
export function rule(name, ...args) {
|
|
1163
|
+
return new Clause([callable(name), ...args]);
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
export function ruleFrom(sourceValue, name, ...args) {
|
|
1167
|
+
return new Clause([sourceValue, callable(name), ...args]);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
function clauses(operator, values, sourceValue = null) {
|
|
1171
|
+
if (values.length === 0) {
|
|
1172
|
+
throw new TypeError(`${operator} requires at least one clause.`);
|
|
1173
|
+
}
|
|
1174
|
+
if (operator === "and" || operator === "not") {
|
|
1175
|
+
rejectAndClauses(values, `inside q.${operator}()`);
|
|
1176
|
+
}
|
|
1177
|
+
const result = sourceValue === null || sourceValue === undefined ? [] : [sourceValue];
|
|
1178
|
+
result.push(sym(operator), ...values.map(asForm));
|
|
1179
|
+
return new Clause(result, operator);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
export function andClause(...values) {
|
|
1183
|
+
return clauses("and", values);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
export function orClause(...values) {
|
|
1187
|
+
return clauses("or", values);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
export function orFrom(sourceValue, ...values) {
|
|
1191
|
+
return clauses("or", values, sourceValue);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
export function notClause(...values) {
|
|
1195
|
+
return clauses("not", values);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
export function notFrom(sourceValue, ...values) {
|
|
1199
|
+
return clauses("not", values, sourceValue);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
export function joinVars(...free) {
|
|
1203
|
+
let options = {};
|
|
1204
|
+
if (free.length > 0 && plainObject(free.at(-1)) && Object.hasOwn(free.at(-1), "required")) {
|
|
1205
|
+
options = free.pop();
|
|
1206
|
+
}
|
|
1207
|
+
return new JoinVars(free, options);
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function normalizeJoinVars(value) {
|
|
1211
|
+
if (value instanceof JoinVars) {
|
|
1212
|
+
return value;
|
|
1213
|
+
}
|
|
1214
|
+
if (!Array.isArray(value)) {
|
|
1215
|
+
throw new TypeError("Join variables must be a JoinVars value or an array.");
|
|
1216
|
+
}
|
|
1217
|
+
return new JoinVars(value);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
function joinClause(operator, variables, values, sourceValue = null) {
|
|
1221
|
+
if (values.length === 0) {
|
|
1222
|
+
throw new TypeError(`${operator} requires at least one clause.`);
|
|
1223
|
+
}
|
|
1224
|
+
const normalized = normalizeJoinVars(variables);
|
|
1225
|
+
if (operator === "not-join") {
|
|
1226
|
+
rejectAndClauses(values, "inside q.notJoin()");
|
|
1227
|
+
}
|
|
1228
|
+
if (operator === "not-join" && normalized.required.length > 0) {
|
|
1229
|
+
throw new TypeError("not-join does not support required join variables.");
|
|
1230
|
+
}
|
|
1231
|
+
const result = sourceValue === null || sourceValue === undefined ? [] : [sourceValue];
|
|
1232
|
+
result.push(sym(operator), normalized.toForm(), ...values.map(asForm));
|
|
1233
|
+
return new Clause(result);
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
export function orJoin(variables, ...values) {
|
|
1237
|
+
return joinClause("or-join", variables, values);
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
export function orJoinFrom(sourceValue, variables, ...values) {
|
|
1241
|
+
return joinClause("or-join", variables, values, sourceValue);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
export function notJoin(variables, ...values) {
|
|
1245
|
+
return joinClause("not-join", variables, values);
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
export function notJoinFrom(sourceValue, variables, ...values) {
|
|
1249
|
+
return joinClause("not-join", variables, values, sourceValue);
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
export function tupleBinding(...bindings) {
|
|
1253
|
+
if (bindings.length === 0) {
|
|
1254
|
+
throw new TypeError("A tuple binding requires at least one item.");
|
|
1255
|
+
}
|
|
1256
|
+
return new Binding(bindings.map(asForm));
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
export function collectionBinding(bindingValue) {
|
|
1260
|
+
return new Binding([asForm(bindingValue), ELLIPSIS]);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
export function relationBinding(...bindings) {
|
|
1264
|
+
return new Binding([bindings.map(asForm)]);
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
export function ignoreBinding() {
|
|
1268
|
+
return new Binding(IGNORE);
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
export function asc(term) {
|
|
1272
|
+
return new Order(term, kw("asc"));
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
export function desc(term) {
|
|
1276
|
+
return new Order(term, kw("desc"));
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
export function ruleBranch(name, variables, ...ruleClauses) {
|
|
1280
|
+
return new RuleBranch(name, variables, ruleClauses);
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
export function rules(...branches) {
|
|
1284
|
+
return new RuleSet(branches);
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
export function raw(form) {
|
|
1288
|
+
return new RawForm(form);
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
export const q = Object.freeze({
|
|
1292
|
+
DB,
|
|
1293
|
+
IGNORE,
|
|
1294
|
+
RULES,
|
|
1295
|
+
aggregate,
|
|
1296
|
+
and: andClause,
|
|
1297
|
+
asc,
|
|
1298
|
+
bind,
|
|
1299
|
+
bindUdf,
|
|
1300
|
+
call,
|
|
1301
|
+
collection,
|
|
1302
|
+
collectionBinding,
|
|
1303
|
+
customAggregate,
|
|
1304
|
+
datom,
|
|
1305
|
+
desc,
|
|
1306
|
+
ednList,
|
|
1307
|
+
expression,
|
|
1308
|
+
fulltext,
|
|
1309
|
+
fulltextOptions,
|
|
1310
|
+
ignoreBinding,
|
|
1311
|
+
idocMatch,
|
|
1312
|
+
idocMatchOptions,
|
|
1313
|
+
joinVars,
|
|
1314
|
+
kw,
|
|
1315
|
+
not: notClause,
|
|
1316
|
+
notFrom,
|
|
1317
|
+
notJoin,
|
|
1318
|
+
notJoinFrom,
|
|
1319
|
+
or: orClause,
|
|
1320
|
+
orFrom,
|
|
1321
|
+
orJoin,
|
|
1322
|
+
orJoinFrom,
|
|
1323
|
+
pattern,
|
|
1324
|
+
patternFrom,
|
|
1325
|
+
predicate,
|
|
1326
|
+
pull,
|
|
1327
|
+
pullAttr,
|
|
1328
|
+
pullNested,
|
|
1329
|
+
pullRecursive,
|
|
1330
|
+
query,
|
|
1331
|
+
quote,
|
|
1332
|
+
raw,
|
|
1333
|
+
relation,
|
|
1334
|
+
relationBinding,
|
|
1335
|
+
rule,
|
|
1336
|
+
ruleBranch,
|
|
1337
|
+
ruleFrom,
|
|
1338
|
+
rules,
|
|
1339
|
+
scalar,
|
|
1340
|
+
selector,
|
|
1341
|
+
source,
|
|
1342
|
+
sym,
|
|
1343
|
+
tuple: tupleFind,
|
|
1344
|
+
tupleBinding,
|
|
1345
|
+
tupleScalar,
|
|
1346
|
+
udf,
|
|
1347
|
+
udfPredicate,
|
|
1348
|
+
v,
|
|
1349
|
+
var: variable,
|
|
1350
|
+
vecNeighbors,
|
|
1351
|
+
vectorSearchOptions
|
|
1352
|
+
});
|