@bjornpagen/bumbledb 0.1.0

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 (108) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +84 -0
  3. package/dist/brand.d.ts +59 -0
  4. package/dist/brand.d.ts.map +1 -0
  5. package/dist/brand.js +47 -0
  6. package/dist/brand.js.map +1 -0
  7. package/dist/closed.d.ts +97 -0
  8. package/dist/closed.d.ts.map +1 -0
  9. package/dist/closed.js +107 -0
  10. package/dist/closed.js.map +1 -0
  11. package/dist/count.d.ts +55 -0
  12. package/dist/count.d.ts.map +1 -0
  13. package/dist/count.js +92 -0
  14. package/dist/count.js.map +1 -0
  15. package/dist/db.d.ts +341 -0
  16. package/dist/db.d.ts.map +1 -0
  17. package/dist/db.js +1016 -0
  18. package/dist/db.js.map +1 -0
  19. package/dist/exhume.d.ts +130 -0
  20. package/dist/exhume.d.ts.map +1 -0
  21. package/dist/exhume.js +145 -0
  22. package/dist/exhume.js.map +1 -0
  23. package/dist/face.d.ts +91 -0
  24. package/dist/face.d.ts.map +1 -0
  25. package/dist/face.js +69 -0
  26. package/dist/face.js.map +1 -0
  27. package/dist/fields.d.ts +187 -0
  28. package/dist/fields.d.ts.map +1 -0
  29. package/dist/fields.js +204 -0
  30. package/dist/fields.js.map +1 -0
  31. package/dist/index.d.ts +49 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +31 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/lower.d.ts +34 -0
  36. package/dist/lower.d.ts.map +1 -0
  37. package/dist/lower.js +105 -0
  38. package/dist/lower.js.map +1 -0
  39. package/dist/marshal.d.ts +79 -0
  40. package/dist/marshal.d.ts.map +1 -0
  41. package/dist/marshal.js +167 -0
  42. package/dist/marshal.js.map +1 -0
  43. package/dist/native.d.ts +532 -0
  44. package/dist/native.d.ts.map +1 -0
  45. package/dist/native.js +64 -0
  46. package/dist/native.js.map +1 -0
  47. package/dist/query/atom.d.ts +285 -0
  48. package/dist/query/atom.d.ts.map +1 -0
  49. package/dist/query/atom.js +281 -0
  50. package/dist/query/atom.js.map +1 -0
  51. package/dist/query/lower.d.ts +145 -0
  52. package/dist/query/lower.d.ts.map +1 -0
  53. package/dist/query/lower.js +604 -0
  54. package/dist/query/lower.js.map +1 -0
  55. package/dist/query/predicate.d.ts +101 -0
  56. package/dist/query/predicate.d.ts.map +1 -0
  57. package/dist/query/predicate.js +85 -0
  58. package/dist/query/predicate.js.map +1 -0
  59. package/dist/query/run.d.ts +30 -0
  60. package/dist/query/run.d.ts.map +1 -0
  61. package/dist/query/run.js +94 -0
  62. package/dist/query/run.js.map +1 -0
  63. package/dist/query/scope.d.ts +168 -0
  64. package/dist/query/scope.d.ts.map +1 -0
  65. package/dist/query/scope.js +134 -0
  66. package/dist/query/scope.js.map +1 -0
  67. package/dist/query/select.d.ts +106 -0
  68. package/dist/query/select.d.ts.map +1 -0
  69. package/dist/query/select.js +69 -0
  70. package/dist/query/select.js.map +1 -0
  71. package/dist/relation.d.ts +120 -0
  72. package/dist/relation.d.ts.map +1 -0
  73. package/dist/relation.js +108 -0
  74. package/dist/relation.js.map +1 -0
  75. package/dist/schema.d.ts +50 -0
  76. package/dist/schema.d.ts.map +1 -0
  77. package/dist/schema.js +235 -0
  78. package/dist/schema.js.map +1 -0
  79. package/dist/spec.d.ts +218 -0
  80. package/dist/spec.d.ts.map +1 -0
  81. package/dist/spec.js +154 -0
  82. package/dist/spec.js.map +1 -0
  83. package/dist/statements.d.ts +91 -0
  84. package/dist/statements.d.ts.map +1 -0
  85. package/dist/statements.js +101 -0
  86. package/dist/statements.js.map +1 -0
  87. package/package.json +66 -0
  88. package/src/brand.ts +82 -0
  89. package/src/closed.ts +228 -0
  90. package/src/count.ts +117 -0
  91. package/src/db.ts +1519 -0
  92. package/src/exhume.ts +243 -0
  93. package/src/face.ts +161 -0
  94. package/src/fields.ts +385 -0
  95. package/src/index.ts +185 -0
  96. package/src/lower.ts +118 -0
  97. package/src/marshal.ts +220 -0
  98. package/src/native.ts +576 -0
  99. package/src/query/atom.ts +556 -0
  100. package/src/query/lower.ts +855 -0
  101. package/src/query/predicate.ts +195 -0
  102. package/src/query/run.ts +106 -0
  103. package/src/query/scope.ts +301 -0
  104. package/src/query/select.ts +140 -0
  105. package/src/relation.ts +252 -0
  106. package/src/schema.ts +297 -0
  107. package/src/spec.ts +325 -0
  108. package/src/statements.ts +148 -0
@@ -0,0 +1,101 @@
1
+ /**
2
+ * IDB predicates (PRD-08) — engine recursion as values, mirroring the IR's
3
+ * cut exactly (`ir::Program`/`PredicateDef`/`AtomSource::Idb`;
4
+ * `docs/architecture/20-query-ir.md` § engine recursion): `$.predicate`
5
+ * declares a predicate with a named, field-typed head; its rules are given
6
+ * IN the declaration (a callback receiving the predicate's own reference,
7
+ * so self-recursion is writable and the rules are data the type system can
8
+ * see — the params they use flow into the query's inferred `Params`); and
9
+ * `.match({...})` uses it as a body atom, bindings addressing head
10
+ * positions by the declared column names (lowered positionally —
11
+ * `FieldId(i)` is head position i, exactly as the IR reads it). Strata
12
+ * legality (no negation/aggregation through a cycle) is the ENGINE's
13
+ * judge; its typed error surfaces at prepare.
14
+ */
15
+ import { phantom } from "#brand.ts";
16
+ import { type FieldData } from "#fields.ts";
17
+ import type { AnyBodyItem, BindingsParams, MatchAtom, TermInput } from "#query/atom.ts";
18
+ import type { AnyVar, Flatten, ItemParams, ParamsRecord, ParamsShape, QueryRegistry, Var } from "#query/scope.ts";
19
+ import type { FieldRef } from "#relation.ts";
20
+ /** One declared head column: name plus the field description that types it. */
21
+ interface PredicateColumn {
22
+ readonly name: string;
23
+ readonly data: FieldData;
24
+ }
25
+ /** One clause of a predicate: head projections (column order) and the body. */
26
+ interface PredicateRuleData {
27
+ readonly finds: readonly AnyVar[];
28
+ readonly body: readonly AnyBodyItem[];
29
+ }
30
+ /** A predicate's runtime description; identity keys the dense `PredId` at lowering. */
31
+ interface PredicateData {
32
+ readonly name: string;
33
+ readonly columns: readonly PredicateColumn[];
34
+ readonly rules: readonly PredicateRuleData[];
35
+ }
36
+ /** Any field reference, whatever its brand — the column-declaration position. */
37
+ type AnyFieldRef = FieldRef<string, string, unknown>;
38
+ /** The columns record `$.predicate` takes: column name to typing field reference. */
39
+ type PredicateColumnsInput = Readonly<Record<string, AnyFieldRef>>;
40
+ /** Extracts a field reference's host value type. */
41
+ type RefValue<T> = T extends {
42
+ readonly [phantom]?: infer V;
43
+ } ? Exclude<V, undefined> : never;
44
+ /** The head's typed column record, derived from the declaration. */
45
+ type ColumnValues<Cols> = {
46
+ [K in keyof Cols]: RefValue<Cols[K]>;
47
+ };
48
+ /**
49
+ * The `.match` bindings of a predicate atom: per head column, a term of
50
+ * the column's brand; unmentioned columns are wildcards, exactly as
51
+ * relation atoms.
52
+ */
53
+ type PredicateBindings<ColsV> = {
54
+ readonly [K in keyof ColsV]?: TermInput<ColsV[K]>;
55
+ };
56
+ /**
57
+ * One rule of a predicate as the declaration callback returns it: `finds`
58
+ * names the projected variable per head column (interior heads project
59
+ * bound variables only — the creation quarantine; the engine's strata
60
+ * judge enforces it), `body` is the clause's conjunction.
61
+ */
62
+ interface PredicateRuleInput<ColsV> {
63
+ readonly finds: {
64
+ readonly [K in keyof ColsV]: Var<ColsV[K]>;
65
+ };
66
+ readonly body: readonly AnyBodyItem[];
67
+ }
68
+ /**
69
+ * The predicate's own reference, passed INTO its rules callback — the
70
+ * fixpoint spelling: `self.match({...})` inside a rule of the same
71
+ * predicate is the recursive atom.
72
+ */
73
+ interface PredicateSelf<ColsV> {
74
+ readonly data: PredicateData;
75
+ match<const B extends PredicateBindings<ColsV>>(bindings: B): MatchAtom<BindingsParams<B>>;
76
+ }
77
+ /**
78
+ * A declared predicate. `.match({...})` uses it as a body atom — in
79
+ * another predicate's rules or in the output rules; the phantom `P`
80
+ * carries the params its own rules contributed, so a query's `Params`
81
+ * type sees through predicates it only reaches transitively.
82
+ */
83
+ interface Predicate<ColsV, P extends ParamsRecord> {
84
+ readonly data: PredicateData;
85
+ match<const B extends PredicateBindings<ColsV>>(bindings: B): MatchAtom<Flatten<P & BindingsParams<B>>>;
86
+ }
87
+ /** The params contributed by a predicate's declared rules. */
88
+ type PredicateParams<Rules extends readonly {
89
+ readonly body: readonly AnyBodyItem[];
90
+ }[]> = ParamsShape<ItemParams<Rules[number]["body"][number]>>;
91
+ /**
92
+ * Declares one predicate in the scope (the `$.predicate` implementation):
93
+ * resolves the head columns, runs the rules callback against the
94
+ * predicate's own reference, verifies every rule projects a declared
95
+ * scope variable per column, and registers the predicate in declaration
96
+ * order (= its dense `PredId`).
97
+ */
98
+ declare function makePredicate<const Cols extends PredicateColumnsInput, const Rules extends readonly PredicateRuleInput<ColumnValues<Cols>>[]>(registry: QueryRegistry, name: string, columns: Cols, rules: (self: PredicateSelf<ColumnValues<Cols>>) => Rules): Predicate<ColumnValues<Cols>, PredicateParams<Rules>>;
99
+ export type { AnyFieldRef, ColumnValues, Predicate, PredicateBindings, PredicateColumn, PredicateColumnsInput, PredicateData, PredicateParams, PredicateRuleData, PredicateRuleInput, PredicateSelf };
100
+ export { makePredicate };
101
+ //# sourceMappingURL=predicate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"predicate.d.ts","sourceRoot":"","sources":["../../src/query/predicate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,YAAY,CAAA;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAEvF,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAEjH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CA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AAI,EAAE,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,KAAK,KAAK,GACvD,SAAS,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,eAAe,CAAC,KAAK,CAAC,CAAC,CAiDvD;AAED,YAAY,EACX,WAAW,EACX,YAAY,EACZ,SAAS,EACT,iBAAiB,EACjB,eAAe,EACf,qBAAqB,EACrB,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,CAAA;AACD,OAAO,EAAE,aAAa,EAAE,CAAA"}
@@ -0,0 +1,85 @@
1
+ /**
2
+ * IDB predicates (PRD-08) — engine recursion as values, mirroring the IR's
3
+ * cut exactly (`ir::Program`/`PredicateDef`/`AtomSource::Idb`;
4
+ * `docs/architecture/20-query-ir.md` § engine recursion): `$.predicate`
5
+ * declares a predicate with a named, field-typed head; its rules are given
6
+ * IN the declaration (a callback receiving the predicate's own reference,
7
+ * so self-recursion is writable and the rules are data the type system can
8
+ * see — the params they use flow into the query's inferred `Params`); and
9
+ * `.match({...})` uses it as a body atom, bindings addressing head
10
+ * positions by the declared column names (lowered positionally —
11
+ * `FieldId(i)` is head position i, exactly as the IR reads it). Strata
12
+ * legality (no negation/aggregation through a cycle) is the ENGINE's
13
+ * judge; its typed error surfaces at prepare.
14
+ */
15
+ import * as errors from "@superbuilders/errors";
16
+ import { phantom } from "#brand.ts";
17
+ import { assertDeclarationOrderKey } from "#fields.ts";
18
+ import { resolveBindings } from "#query/atom.ts";
19
+ import { isTerm, resolveFieldData, term } from "#query/scope.ts";
20
+ /** Builds one predicate atom (shared by `self.match` and `.match`). */
21
+ function predicateAtom(data, bindings) {
22
+ return Object.freeze({
23
+ item: "atom",
24
+ negated: false,
25
+ source: Object.freeze({ kind: "predicate", pred: data }),
26
+ bindings: resolveBindings(`predicate ${data.name}`, data.columns.map(function asField(column) {
27
+ return { name: column.name, field: column.data };
28
+ }), bindings)
29
+ });
30
+ }
31
+ /**
32
+ * Declares one predicate in the scope (the `$.predicate` implementation):
33
+ * resolves the head columns, runs the rules callback against the
34
+ * predicate's own reference, verifies every rule projects a declared
35
+ * scope variable per column, and registers the predicate in declaration
36
+ * order (= its dense `PredId`).
37
+ */
38
+ function makePredicate(registry, name, columns, rules) {
39
+ const ordered = [];
40
+ for (const [columnName, ref] of Object.entries(columns)) {
41
+ assertDeclarationOrderKey(`predicate ${name} column`, columnName);
42
+ ordered.push(Object.freeze({
43
+ name: columnName,
44
+ data: resolveFieldData(registry.theory, ref.relation, ref.field)
45
+ }));
46
+ }
47
+ if (ordered.length === 0) {
48
+ throw errors.new(`predicate ${name}: a predicate head needs at least one column`);
49
+ }
50
+ const ruleSlots = [];
51
+ const data = Object.freeze({
52
+ name,
53
+ columns: Object.freeze(ordered),
54
+ rules: ruleSlots
55
+ });
56
+ const self = Object.freeze({
57
+ data,
58
+ match(bindings) {
59
+ return predicateAtom(data, bindings);
60
+ }
61
+ });
62
+ const declared = rules(self);
63
+ for (const rule of declared) {
64
+ const finds = [];
65
+ const record = Object.fromEntries(Object.entries(rule.finds));
66
+ for (const column of ordered) {
67
+ const found = record[column.name];
68
+ if (!isTerm(found) || found[term] !== "var") {
69
+ throw errors.new(`predicate ${name}: rule finds must project a scope variable for column ${column.name} (interior heads project bound variables only)`);
70
+ }
71
+ finds.push(found);
72
+ }
73
+ ruleSlots.push(Object.freeze({ finds: Object.freeze(finds), body: Object.freeze([...rule.body]) }));
74
+ }
75
+ Object.freeze(ruleSlots);
76
+ registry.predicates.push(data);
77
+ return Object.freeze({
78
+ data,
79
+ match(bindings) {
80
+ return predicateAtom(data, bindings);
81
+ }
82
+ });
83
+ }
84
+ export { makePredicate };
85
+ //# sourceMappingURL=predicate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"predicate.js","sourceRoot":"","sources":["../../src/query/predicate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAA;AAC/C,OAAO,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CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IACpG,CAAC;IACD,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAA;IACxB,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,OAAO,MAAM,CAAC,MAAM,CAAC;QACpB,IAAI;QACJ,KAAK,CAAC,QAA2C;YAChD,OAAO,aAAa,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QACrC,CAAC;KACD,CAAC,CAAA;AACH,CAAC;AAeD,OAAO,EAAE,aAAa,EAAE,CAAA"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Prepared-query marshaling seams (PRD-08), the two rides every execution
3
+ * takes — the typed params object down to the bridge's positional
4
+ * `QueryParam[]` (declaration order = dense `ParamId`s, values tagged by
5
+ * each param's declaring field), and answer rows (positional, head order)
6
+ * back up to plain objects with branded values (the store is the proof
7
+ * carrier: the engine computed the answer, so readback asserts the brand,
8
+ * the same trust direction as fact readback in `#marshal.ts`). Answers are
9
+ * SETS — no order or limit exists anywhere; hosts sort. The `Prepared`
10
+ * VALUE itself (no lifecycle, GC-reclaimed plan) lives in `#db.ts`.
11
+ */
12
+ import type { FactValue, QueryParam } from "#native.ts";
13
+ import type { SelectColumn } from "#query/lower.ts";
14
+ import type { ParamEntry } from "#query/scope.ts";
15
+ /**
16
+ * Marshals the typed params object to the bridge's positional arguments,
17
+ * in param declaration order (= the lowering's dense `ParamId`s). A
18
+ * missing entry is a typed error naming the param; values tag by the
19
+ * declaring field's structural type; a set param takes a readonly array
20
+ * (the empty set is legal and matches nothing — the engine's rule).
21
+ */
22
+ declare function wireParams(entries: readonly ParamEntry[], supplied: Readonly<Record<string, unknown>>): QueryParam[];
23
+ /**
24
+ * Decodes positional answer rows (column order = the program's head order
25
+ * = the select record's written order) to named, branded, frozen row
26
+ * objects.
27
+ */
28
+ declare function decodeAnswers<Row>(select: readonly SelectColumn[], rows: FactValue[][]): Row[];
29
+ export { decodeAnswers, wireParams };
30
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/query/run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAe,MAAM,YAAY,CAAA;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAEnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAajD;;;;;;GAMG;AACH,iBAAS,UAAU,CAAC,OAAO,EAAE,SAAS,UAAU,EAAE,EAAE,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,UAAU,EAAE,CA0B7G;AAiBD;;;;GAIG;AACH,iBAAS,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,YAAY,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,GAAG,GAAG,EAAE,CAmBvF;AAED,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,CAAA"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Prepared-query marshaling seams (PRD-08), the two rides every execution
3
+ * takes — the typed params object down to the bridge's positional
4
+ * `QueryParam[]` (declaration order = dense `ParamId`s, values tagged by
5
+ * each param's declaring field), and answer rows (positional, head order)
6
+ * back up to plain objects with branded values (the store is the proof
7
+ * carrier: the engine computed the answer, so readback asserts the brand,
8
+ * the same trust direction as fact readback in `#marshal.ts`). Answers are
9
+ * SETS — no order or limit exists anywhere; hosts sort. The `Prepared`
10
+ * VALUE itself (no lifecycle, GC-reclaimed plan) lives in `#db.ts`.
11
+ */
12
+ import * as errors from "@superbuilders/errors";
13
+ import { taggedLiteral } from "#query/lower.ts";
14
+ /** The 13-bit Allen mask ceiling (`bumbledb/crates/bumbledb/src/allen.rs`: bits above the low 13 are unrepresentable). */
15
+ const ALLEN_ALL_BITS = (1 << 13) - 1;
16
+ /** Tags one supplied mask-param value. */
17
+ function wireMask(name, value) {
18
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > ALLEN_ALL_BITS) {
19
+ throw errors.new(`param ${name}: an Allen-mask param binds a 13-bit mask number built from the ALLEN constants`);
20
+ }
21
+ return { kind: "allenMask", mask: value };
22
+ }
23
+ /**
24
+ * Marshals the typed params object to the bridge's positional arguments,
25
+ * in param declaration order (= the lowering's dense `ParamId`s). A
26
+ * missing entry is a typed error naming the param; values tag by the
27
+ * declaring field's structural type; a set param takes a readonly array
28
+ * (the empty set is legal and matches nothing — the engine's rule).
29
+ */
30
+ function wireParams(entries, supplied) {
31
+ return entries.map(function wireOne(entry) {
32
+ const value = supplied[entry.name];
33
+ if (value === undefined) {
34
+ throw errors.new(`execute params object is missing param ${entry.name}`);
35
+ }
36
+ if (entry.shape === "mask") {
37
+ return wireMask(entry.name, value);
38
+ }
39
+ if (entry.data === undefined) {
40
+ throw errors.new(`param ${entry.name}: registry entry carries no declaring field`);
41
+ }
42
+ if (entry.shape === "set") {
43
+ if (!Array.isArray(value)) {
44
+ throw errors.new(`param ${entry.name}: a set param binds a readonly array of values`);
45
+ }
46
+ const data = entry.data;
47
+ return {
48
+ kind: "set",
49
+ values: value.map(function wireElement(element, index) {
50
+ return taggedLiteral(`param ${entry.name}[${index}]`, data, element);
51
+ })
52
+ };
53
+ }
54
+ return taggedLiteral(`param ${entry.name}`, entry.data, value);
55
+ });
56
+ }
57
+ /**
58
+ * The read-side trusted seam of answers: a decoded row carrying every
59
+ * select column IS a `Row` — the engine computed it under the prepared
60
+ * head, so the brands are asserted, not re-derived (the `#marshal.ts`
61
+ * trust direction).
62
+ */
63
+ function isAnswerRow(select, decoded) {
64
+ return select.every(function present(column) {
65
+ return decoded[column.name] !== undefined;
66
+ });
67
+ }
68
+ /**
69
+ * Decodes positional answer rows (column order = the program's head order
70
+ * = the select record's written order) to named, branded, frozen row
71
+ * objects.
72
+ */
73
+ function decodeAnswers(select, rows) {
74
+ return rows.map(function decodeRow(row) {
75
+ if (row.length !== select.length) {
76
+ throw errors.new(`query answer arity ${row.length} does not match the ${select.length} select columns`);
77
+ }
78
+ const decoded = {};
79
+ select.forEach(function decodeCell(column, ordinal) {
80
+ const cell = row[ordinal];
81
+ if (cell === undefined) {
82
+ throw errors.new(`query answer cell ${ordinal} (${column.name}) is absent`);
83
+ }
84
+ decoded[column.name] = cell;
85
+ });
86
+ Object.freeze(decoded);
87
+ if (!isAnswerRow(select, decoded)) {
88
+ throw errors.new("query answer row is not a complete select record");
89
+ }
90
+ return decoded;
91
+ });
92
+ }
93
+ export { decodeAnswers, wireParams };
94
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/query/run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAA;AAG/C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAG/C,0HAA0H;AAC1H,MAAM,cAAc,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAA;AAEpC,0CAA0C;AAC1C,SAAS,QAAQ,CAAC,IAAY,EAAE,KAAc;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,cAAc,EAAE,CAAC;QAClG,MAAM,MAAM,CAAC,GAAG,CAAC,SAAS,IAAI,iFAAiF,CAAC,CAAA;IACjH,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AAC1C,CAAC;AAED;;;;;;GAMG;AACH,SAAS,UAAU,CAA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
+ * Query scope terms (PRD-08): typed variables and parameters, declared
3
+ * inside a `query()` scope callback so identity is lexical. A `Var` is
4
+ * typed by the field it is declared from and joins are brand-equal by
5
+ * construction (the nominal join discipline); var identity is OBJECT
6
+ * identity — two `$.var` calls are two variables even from the same field,
7
+ * and no name-collision machinery exists. Params are query-global, carry a
8
+ * mandatory literal name (the key of `execute`'s params object), and lower
9
+ * to dense positional `ParamId`s in declaration order. This module also
10
+ * owns the type-level params extraction: every atom and condition value
11
+ * carries its contributed params object in a phantom, and the query's
12
+ * `Params` type is the flattened intersection over the returned rules —
13
+ * inference rides the return value, never a mutable type accumulator.
14
+ */
15
+ import { phantom } from "#brand.ts";
16
+ import type { FieldData } from "#fields.ts";
17
+ import type { PredicateData } from "#query/predicate.ts";
18
+ import type { AnySchema } from "#schema.ts";
19
+ /**
20
+ * The runtime discriminant of query term values. Host literals (bigints,
21
+ * strings, interval objects, `oneOf` sets) never carry it, so "is this
22
+ * binding a term or a literal" is one property probe, never a guess.
23
+ */
24
+ declare const term: unique symbol;
25
+ /**
26
+ * A query variable, typed by the field it was declared from
27
+ * (`$.var(Holder.fields.id)` → `Var<Brand<bigint, "HolderId">>`). Usable in
28
+ * any atom position whose field carries the same brand; a brand-mismatched
29
+ * placement is a TYPE error. Identity is object identity.
30
+ */
31
+ interface Var<V> {
32
+ readonly [term]: "var";
33
+ readonly relation: string;
34
+ readonly field: string;
35
+ readonly data: FieldData;
36
+ readonly [phantom]?: V;
37
+ }
38
+ /**
39
+ * A scalar query parameter — `$.param("root", Holder.fields.id)`. The name
40
+ * literal is the key of the typed params object `execute` takes; the value
41
+ * is marshaled by the declaring field's structural type at bind.
42
+ */
43
+ interface Param<Name extends string, V> {
44
+ readonly [term]: "param";
45
+ readonly name: Name;
46
+ readonly relation: string;
47
+ readonly field: string;
48
+ readonly data: FieldData;
49
+ readonly [phantom]?: V;
50
+ }
51
+ /**
52
+ * A set-valued query parameter (the IR's `ParamSet` term): bound at
53
+ * execution to an ARRAY of values of the declaring field's type; a binding
54
+ * position matches iff the field value is in the set. Legal in atom
55
+ * bindings (positive and negated) and as the right side of `is` — nowhere
56
+ * else, exactly as the IR rules it.
57
+ */
58
+ interface ParamSet<Name extends string, V> {
59
+ readonly [term]: "paramSet";
60
+ readonly name: Name;
61
+ readonly relation: string;
62
+ readonly field: string;
63
+ readonly data: FieldData;
64
+ readonly [phantom]?: V;
65
+ }
66
+ /**
67
+ * An Allen-mask parameter (the IR's `MaskTerm::Param`): the temporal
68
+ * relation as a bind-time argument — one prepared query answers any of the
69
+ * mask questions per execution. Bound to a 13-bit mask number built from
70
+ * the `ALLEN` constants.
71
+ */
72
+ interface MaskParam<Name extends string> {
73
+ readonly [term]: "maskParam";
74
+ readonly name: Name;
75
+ }
76
+ /** Any query variable, whatever its brand. */
77
+ type AnyVar = Var<unknown>;
78
+ /** Any parameter term of the scope, whatever its name and brand. */
79
+ type AnyParamTerm = Param<string, unknown> | ParamSet<string, unknown> | MaskParam<string>;
80
+ /** Any scope term value. */
81
+ type AnyTerm = AnyVar | AnyParamTerm;
82
+ /** Narrows an unknown binding value to a scope term (vs a host literal). */
83
+ declare function isTerm(value: unknown): value is AnyTerm;
84
+ /** A params object type — what `execute` takes and the phantoms carry. */
85
+ type ParamsRecord = Readonly<Record<string, unknown>>;
86
+ /** Flattens an intersection into one displayed object type (hover legibility). */
87
+ type Flatten<T> = {
88
+ [K in keyof T]: T[K];
89
+ };
90
+ /** The standard union-to-intersection fold (distributes over `U`). */
91
+ type UnionToIntersection<U> = (U extends unknown ? (member: U) => void : never) extends (member: infer I) => void ? I : never;
92
+ /**
93
+ * One term's contribution to the query's params object type: a `Param`
94
+ * contributes its value type under its name, a `ParamSet` the readonly
95
+ * array of it, a `MaskParam` a mask number; everything else contributes
96
+ * nothing.
97
+ */
98
+ type TermContribution<T> = T extends {
99
+ readonly [term]: "param";
100
+ readonly name: infer N extends string;
101
+ readonly [phantom]?: infer V;
102
+ } ? {
103
+ readonly [K in N]: Exclude<V, undefined>;
104
+ } : T extends {
105
+ readonly [term]: "paramSet";
106
+ readonly name: infer N extends string;
107
+ readonly [phantom]?: infer V;
108
+ } ? {
109
+ readonly [K in N]: readonly Exclude<V, undefined>[];
110
+ } : T extends {
111
+ readonly [term]: "maskParam";
112
+ readonly name: infer N extends string;
113
+ } ? {
114
+ readonly [K in N]: number;
115
+ } : Record<never, never>;
116
+ /**
117
+ * Folds a union of per-term/per-item params objects into the one flattened
118
+ * params record (the query's `Params` type).
119
+ */
120
+ type ParamsShape<U> = [U] extends [never] ? Record<never, never> : Flatten<UnionToIntersection<U>>;
121
+ /** Reads an atom's/condition's contributed params object off its phantom. */
122
+ type ItemParams<T> = T extends {
123
+ readonly [phantom]?: infer P;
124
+ } ? Exclude<P, undefined> : Record<never, never>;
125
+ /** One registered parameter: name, wire shape, and the declaring field. */
126
+ interface ParamEntry {
127
+ readonly name: string;
128
+ readonly shape: "value" | "set" | "mask";
129
+ readonly data: FieldData | undefined;
130
+ }
131
+ /**
132
+ * The mutable build-time registry one `query()` scope owns: declared vars
133
+ * (membership polices cross-scope smuggling), params in declaration order
134
+ * (= dense `ParamId`s), and declared predicates in declaration order
135
+ * (= dense `PredId`s; the output predicate is appended by lowering).
136
+ */
137
+ interface QueryRegistry {
138
+ readonly theory: AnySchema;
139
+ readonly vars: Set<AnyVar>;
140
+ readonly params: ParamEntry[];
141
+ readonly paramIndex: Map<AnyTerm, number>;
142
+ readonly predicates: PredicateData[];
143
+ }
144
+ /** Creates one empty scope registry over the query's theory. */
145
+ declare function createRegistry(theory: AnySchema): QueryRegistry;
146
+ /**
147
+ * Resolves a field reference's runtime description through the schema —
148
+ * the seam that types `$.var`/`$.param` declarations at runtime (the type
149
+ * level already carries the brand; this recovers the structural type the
150
+ * lowering and the param marshaler direct by).
151
+ */
152
+ declare function resolveFieldData(theory: AnySchema, relationName: string, fieldName: string): FieldData;
153
+ /** A field reference's runtime half, as the scope factories consume it. */
154
+ interface RefNames {
155
+ readonly relation: string;
156
+ readonly field: string;
157
+ }
158
+ /** Declares one variable in the scope (the `$.var` implementation). */
159
+ declare function scopeVar<V>(registry: QueryRegistry, ref: RefNames): Var<V>;
160
+ /** Declares one scalar parameter (the `$.param` implementation). */
161
+ declare function scopeParam<Name extends string, V>(registry: QueryRegistry, name: Name, ref: RefNames): Param<Name, V>;
162
+ /** Declares one set parameter (the `$.paramSet` implementation). */
163
+ declare function scopeParamSet<Name extends string, V>(registry: QueryRegistry, name: Name, ref: RefNames): ParamSet<Name, V>;
164
+ /** Declares one Allen-mask parameter (the `$.allenParam` implementation). */
165
+ declare function scopeAllenParam<Name extends string>(registry: QueryRegistry, name: Name): MaskParam<Name>;
166
+ export type { AnyParamTerm, AnyTerm, AnyVar, Flatten, ItemParams, MaskParam, Param, ParamEntry, ParamSet, ParamsRecord, ParamsShape, QueryRegistry, TermContribution, UnionToIntersection, Var };
167
+ export { createRegistry, isTerm, resolveFieldData, scopeAllenParam, scopeParam, scopeParamSet, scopeVar, term };
168
+ //# sourceMappingURL=scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../../src/query/scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACxD,OAAO,KAAK,EAAE,SAAS,EAAkB,MAAM,YAAY,CAAA;AAE3D;;;;GAIG;AACH,QAAA,MAAM,IAAI,EAAE,OAAO,MAAsC,CAAA;AAEzD;;;;;GAKG;AACH,UAAU,GAAG,CAAC,CAAC;IACd,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,CAAA;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,Q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AAE,UAAU,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAA"}
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Query scope terms (PRD-08): typed variables and parameters, declared
3
+ * inside a `query()` scope callback so identity is lexical. A `Var` is
4
+ * typed by the field it is declared from and joins are brand-equal by
5
+ * construction (the nominal join discipline); var identity is OBJECT
6
+ * identity — two `$.var` calls are two variables even from the same field,
7
+ * and no name-collision machinery exists. Params are query-global, carry a
8
+ * mandatory literal name (the key of `execute`'s params object), and lower
9
+ * to dense positional `ParamId`s in declaration order. This module also
10
+ * owns the type-level params extraction: every atom and condition value
11
+ * carries its contributed params object in a phantom, and the query's
12
+ * `Params` type is the flattened intersection over the returned rules —
13
+ * inference rides the return value, never a mutable type accumulator.
14
+ */
15
+ import * as errors from "@superbuilders/errors";
16
+ import { phantom } from "#brand.ts";
17
+ /**
18
+ * The runtime discriminant of query term values. Host literals (bigints,
19
+ * strings, interval objects, `oneOf` sets) never carry it, so "is this
20
+ * binding a term or a literal" is one property probe, never a guess.
21
+ */
22
+ const term = Symbol("bumbledb.query.term");
23
+ /** Narrows an unknown binding value to a scope term (vs a host literal). */
24
+ function isTerm(value) {
25
+ return typeof value === "object" && value !== null && term in value;
26
+ }
27
+ /** Creates one empty scope registry over the query's theory. */
28
+ function createRegistry(theory) {
29
+ return {
30
+ theory,
31
+ vars: new Set(),
32
+ params: [],
33
+ paramIndex: new Map(),
34
+ predicates: []
35
+ };
36
+ }
37
+ /**
38
+ * The relation-kind discriminant: a closed relation's runtime description
39
+ * carries its handle roster, an ordinary relation's never does.
40
+ */
41
+ function isClosedMember(member) {
42
+ return "handles" in member.data;
43
+ }
44
+ /**
45
+ * Resolves a field reference's runtime description through the schema —
46
+ * the seam that types `$.var`/`$.param` declarations at runtime (the type
47
+ * level already carries the brand; this recovers the structural type the
48
+ * lowering and the param marshaler direct by).
49
+ */
50
+ function resolveFieldData(theory, relationName, fieldName) {
51
+ const member = theory.relations[relationName];
52
+ if (member === undefined) {
53
+ throw errors.new(`schema ${theory.name} has no relation ${relationName}`);
54
+ }
55
+ if (isClosedMember(member)) {
56
+ if (fieldName === "id") {
57
+ return member.id.data;
58
+ }
59
+ const column = member.data.columns.find(function byName(candidate) {
60
+ return candidate.name === fieldName;
61
+ });
62
+ if (column === undefined) {
63
+ throw errors.new(`closed relation ${relationName} has no column ${fieldName}`);
64
+ }
65
+ return column.field;
66
+ }
67
+ const declared = member.data.fields.find(function byName(candidate) {
68
+ return candidate.name === fieldName;
69
+ });
70
+ if (declared === undefined) {
71
+ throw errors.new(`relation ${relationName} has no field ${fieldName}`);
72
+ }
73
+ return declared.field;
74
+ }
75
+ /** Declares one variable in the scope (the `$.var` implementation). */
76
+ function scopeVar(registry, ref) {
77
+ const value = Object.freeze({
78
+ [term]: "var",
79
+ relation: ref.relation,
80
+ field: ref.field,
81
+ data: resolveFieldData(registry.theory, ref.relation, ref.field)
82
+ });
83
+ registry.vars.add(value);
84
+ return value;
85
+ }
86
+ /** Rejects a second parameter under an already-taken name. */
87
+ function assertFreshParamName(registry, name) {
88
+ const taken = registry.params.some(function byName(entry) {
89
+ return entry.name === name;
90
+ });
91
+ if (taken) {
92
+ throw errors.new(`query scope already declares a param named ${name} — param names key the execute params object, one declaration each`);
93
+ }
94
+ }
95
+ /** Declares one scalar parameter (the `$.param` implementation). */
96
+ function scopeParam(registry, name, ref) {
97
+ assertFreshParamName(registry, name);
98
+ const data = resolveFieldData(registry.theory, ref.relation, ref.field);
99
+ const value = Object.freeze({
100
+ [term]: "param",
101
+ name,
102
+ relation: ref.relation,
103
+ field: ref.field,
104
+ data
105
+ });
106
+ registry.paramIndex.set(value, registry.params.length);
107
+ registry.params.push(Object.freeze({ name, shape: "value", data }));
108
+ return value;
109
+ }
110
+ /** Declares one set parameter (the `$.paramSet` implementation). */
111
+ function scopeParamSet(registry, name, ref) {
112
+ assertFreshParamName(registry, name);
113
+ const data = resolveFieldData(registry.theory, ref.relation, ref.field);
114
+ const value = Object.freeze({
115
+ [term]: "paramSet",
116
+ name,
117
+ relation: ref.relation,
118
+ field: ref.field,
119
+ data
120
+ });
121
+ registry.paramIndex.set(value, registry.params.length);
122
+ registry.params.push(Object.freeze({ name, shape: "set", data }));
123
+ return value;
124
+ }
125
+ /** Declares one Allen-mask parameter (the `$.allenParam` implementation). */
126
+ function scopeAllenParam(registry, name) {
127
+ assertFreshParamName(registry, name);
128
+ const value = Object.freeze({ [term]: "maskParam", name });
129
+ registry.paramIndex.set(value, registry.params.length);
130
+ registry.params.push(Object.freeze({ name, shape: "mask", data: undefined }));
131
+ return value;
132
+ }
133
+ export { createRegistry, isTerm, resolveFieldData, scopeAllenParam, scopeParam, scopeParamSet, scopeVar, term };
134
+ //# sourceMappingURL=scope.js.map