@bjornpagen/bumbledb 0.5.0 → 0.6.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 (45) hide show
  1. package/COOKBOOK.md +96 -131
  2. package/README.md +9 -9
  3. package/dist/db.js +2 -2
  4. package/dist/db.js.map +1 -1
  5. package/dist/index.d.ts +8 -5
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +5 -2
  8. package/dist/index.js.map +1 -1
  9. package/dist/query/atom.d.ts +132 -201
  10. package/dist/query/atom.d.ts.map +1 -1
  11. package/dist/query/atom.js +30 -42
  12. package/dist/query/atom.js.map +1 -1
  13. package/dist/query/find.d.ts +116 -0
  14. package/dist/query/find.d.ts.map +1 -0
  15. package/dist/query/{select.js → find.js} +22 -22
  16. package/dist/query/find.js.map +1 -0
  17. package/dist/query/lower.d.ts +123 -158
  18. package/dist/query/lower.d.ts.map +1 -1
  19. package/dist/query/lower.js +437 -493
  20. package/dist/query/lower.js.map +1 -1
  21. package/dist/query/predicate.d.ts +22 -14
  22. package/dist/query/predicate.d.ts.map +1 -1
  23. package/dist/query/predicate.js +35 -42
  24. package/dist/query/predicate.js.map +1 -1
  25. package/dist/query/run.d.ts +3 -3
  26. package/dist/query/run.d.ts.map +1 -1
  27. package/dist/query/run.js +9 -9
  28. package/dist/query/run.js.map +1 -1
  29. package/dist/query/scope.d.ts +127 -72
  30. package/dist/query/scope.d.ts.map +1 -1
  31. package/dist/query/scope.js +80 -33
  32. package/dist/query/scope.js.map +1 -1
  33. package/package.json +2 -2
  34. package/src/db.ts +4 -4
  35. package/src/index.ts +11 -7
  36. package/src/query/atom.ts +181 -263
  37. package/src/query/find.ts +212 -0
  38. package/src/query/lower.ts +588 -722
  39. package/src/query/predicate.ts +37 -45
  40. package/src/query/run.ts +10 -10
  41. package/src/query/scope.ts +172 -87
  42. package/dist/query/select.d.ts +0 -128
  43. package/dist/query/select.d.ts.map +0 -1
  44. package/dist/query/select.js.map +0 -1
  45. package/src/query/select.ts +0 -215
@@ -1,128 +0,0 @@
1
- /**
2
- * Select entries and aggregates, STRUCTURAL edition — the head vocabulary,
3
- * mirroring the IR's aggregate roster exactly
4
- * (`bumbledb/crates/bumbledb/src/ir.rs` `AggOp`/`FindTerm`;
5
- * `docs/architecture/20-query-ir.md` § aggregation): `count` (nullary),
6
- * `countDistinct`, `sum`/`min`/`max` (over an orderable variable or the
7
- * measure), `argMax`/`argMin` (arg-restriction: carried value + orderable
8
- * key; a tie yields every attaining row), and `pack` (the coalescing fold —
9
- * RELATION-SHAPED: one answer row per (group, maximal segment), the result
10
- * position interval-typed). Aggregates name their variables — `r.sum("m")`
11
- * — and are typed by the rule environment at `.select`. Grouping is
12
- * implicit: the non-aggregate select entries are the group key; over empty
13
- * input an all-aggregate select yields the EMPTY SET, never a zero row.
14
- * The creation quarantine is representational: a head position is a var
15
- * name, the measure, or an aggregate — no minting or arithmetic term
16
- * exists to spell (permanent law).
17
- */
18
- import type { Infer } from "#fields.ts";
19
- import type { IntervalVarOk, OrderVarOk } from "#query/atom.ts";
20
- import type { Duration, EnvShape, ShapeOf } from "#query/scope.ts";
21
- /** One aggregate operator name of the select vocabulary. */
22
- type AggOpName = "count" | "countDistinct" | "sum" | "min" | "max" | "argMax" | "argMin" | "pack";
23
- /**
24
- * One aggregate select VALUE: the op, the variable name (or measure) it
25
- * folds, and — for the Arg forms — the orderable key's variable name. The
26
- * runtime representation carries the types; the rule environment types the
27
- * result at `.select`.
28
- */
29
- interface Agg<Op extends AggOpName, Over extends string | Duration<string> | undefined, Key extends string | undefined = undefined> {
30
- readonly agg: Op;
31
- readonly over: Over;
32
- readonly key: Key;
33
- }
34
- /** Any aggregate select value. */
35
- type AnyAgg = Agg<AggOpName, string | Duration<string> | undefined, string | undefined>;
36
- /** One select entry: a projected var name, the measure, or an aggregate. */
37
- type SelectEntry = string | Duration<string> | AnyAgg;
38
- /** Nullary count: |the group's set of distinct full bindings|, `bigint`; the answer column is named `count`. */
39
- declare function count(): Agg<"count", undefined>;
40
- /** |the distinct values of the named variable across the group|, `bigint`; legal over every type. */
41
- declare function countDistinct<const N extends string>(over: N): Agg<"countDistinct", N>;
42
- /**
43
- * Exact checked sum over an orderable (u64/i64) variable — wide
44
- * accumulator, one finalize range check; overflow is the engine's typed
45
- * runtime error, never a wrap — or over the measure
46
- * (`r.sum(r.duration("w"))`).
47
- */
48
- declare function sum<const N extends string | Duration<string>>(over: N): Agg<"sum", N>;
49
- /** Minimum over an orderable variable or the measure (orderable types only). */
50
- declare function min<const N extends string | Duration<string>>(over: N): Agg<"min", N>;
51
- /** Maximum over an orderable variable or the measure (orderable types only). */
52
- declare function max<const N extends string | Duration<string>>(over: N): Agg<"max", N>;
53
- /**
54
- * Arg-restriction toward the maximum of `key` (`ir::AggOp::ArgMax`): the
55
- * group's binding set is restricted to the bindings attaining the extreme
56
- * of the orderable key, and `value` is the carried payload — a tie yields
57
- * every attaining row. All Arg entries of one query share one key and one
58
- * direction; Arg and fold aggregates never mix (both the engine's typed
59
- * rules).
60
- */
61
- declare function argMax<const V extends string, const K extends string>(value: V, key: K): Agg<"argMax", V, K>;
62
- /** Arg-restriction toward the minimum of `key`; rules as {@link argMax}. */
63
- declare function argMin<const V extends string, const K extends string>(value: V, key: K): Agg<"argMin", V, K>;
64
- /**
65
- * The coalescing fold (Snodgrass coalesce, `ir::AggOp::Pack`): per group,
66
- * the maximal disjoint half-open segments of the union of the group's
67
- * interval point sets — RELATION-SHAPED, one answer row per (group,
68
- * maximal segment), the result position carrying one interval of the
69
- * input's element type. At most one `pack` per select, never beside a
70
- * fold or an Arg entry (the engine's typed rules).
71
- */
72
- declare function pack<const N extends string>(over: N): Agg<"pack", N>;
73
- /** A fold input's judgment: an orderable variable, or the measure of an interval variable. */
74
- type FoldOverOk<Env extends EnvShape, O> = O extends string ? OrderVarOk<Env, O> : O extends Duration<infer N extends string> ? IntervalVarOk<Env, N> : false;
75
- /**
76
- * One select entry's judgment against the rule environment: projected
77
- * names must be bound, the measure and `pack` demand interval-typed
78
- * variables, folds and Arg keys demand orderable ones.
79
- */
80
- type SelectEntryOk<Env extends EnvShape, E> = E extends string ? E extends keyof Env ? true : false : E extends Duration<infer N extends string> ? IntervalVarOk<Env, N> : E extends Agg<"count", undefined> ? true : E extends Agg<"countDistinct", infer O extends string> ? O extends keyof Env ? true : false : E extends Agg<"sum" | "min" | "max", infer O> ? FoldOverOk<Env, O> : E extends Agg<"argMax" | "argMin", infer O extends string, infer K extends string> ? [O extends keyof Env ? true : false, OrderVarOk<Env, K>] extends [true, true] ? true : false : E extends Agg<"pack", infer O extends string> ? IntervalVarOk<Env, O> : false;
81
- /** The validated select tuple (intersect with the inferred entries — errors land on the offending argument). */
82
- type CheckSelect<Env extends EnvShape, S> = {
83
- readonly [I in keyof S]: SelectEntryOk<Env, S[I]> extends true ? S[I] : never;
84
- };
85
- /** The validated names-only select tuple of a RECURSIVE rule (aggregates and the measure are unwritable there). */
86
- type CheckNameSelect<Env extends EnvShape, S> = {
87
- readonly [I in keyof S]: S[I] extends keyof Env ? S[I] : never;
88
- };
89
- /**
90
- * One select entry's answer-column fragment: the column is named by the
91
- * variable it projects or folds (`count` names its column `count`), and
92
- * its type reflects the entry — `count`/`countDistinct` are `bigint`
93
- * whatever they counted, folds carry their input's type, the measure is
94
- * `bigint`, the Arg forms carry the payload's type, `pack` its interval
95
- * type.
96
- */
97
- type SelectEntryRow<Env extends EnvShape, E> = E extends string ? {
98
- readonly [K in E]: Infer<Env[E & keyof Env]["field"]>;
99
- } : E extends Duration<infer N extends string> ? {
100
- readonly [K in N]: bigint;
101
- } : E extends Agg<"count", undefined> ? {
102
- readonly count: bigint;
103
- } : E extends Agg<"countDistinct", infer O extends string> ? {
104
- readonly [K in O]: bigint;
105
- } : E extends Agg<"sum" | "min" | "max", infer O> ? O extends string ? {
106
- readonly [K in O]: Infer<Env[O & keyof Env]["field"]>;
107
- } : O extends Duration<infer N extends string> ? {
108
- readonly [K in N]: bigint;
109
- } : never : E extends Agg<"argMax" | "argMin", infer O extends string, string> ? {
110
- readonly [K in O]: Infer<Env[O & keyof Env]["field"]>;
111
- } : E extends Agg<"pack", infer O extends string> ? {
112
- readonly [K in O]: Infer<Env[O & keyof Env]["field"]>;
113
- } : never;
114
- /** The inferred answer-row object type of a select tuple. */
115
- type RowOfSelect<Env extends EnvShape, S extends readonly SelectEntry[]> = ShapeOf<SelectEntryRow<Env, S[number]>>;
116
- /**
117
- * One projected name's answer-column fragment — a NAKED parameter, so the
118
- * judgment distributes per name (the union of a multi-name select never
119
- * smears into one column's type), mirroring {@link SelectEntryRow}.
120
- */
121
- type NameSelectRow<Env extends EnvShape, N> = N extends string ? {
122
- readonly [K in N]: Infer<Env[K & keyof Env]["field"]>;
123
- } : never;
124
- /** The inferred answer-row object type of a names-only (recursive-rule) select tuple. */
125
- type RowOfNameSelect<Env extends EnvShape, S extends readonly string[]> = ShapeOf<NameSelectRow<Env, S[number]>>;
126
- export type { Agg, AggOpName, AnyAgg, CheckNameSelect, CheckSelect, RowOfNameSelect, RowOfSelect, SelectEntry, SelectEntryOk, SelectEntryRow };
127
- export { argMax, argMin, count, countDistinct, max, min, pack, sum };
128
- //# sourceMappingURL=select.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/query/select.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC/D,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAElE,4DAA4D;AAC5D,KAAK,SAAS,GAAG,OAAO,GAAG,eAAe,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAAA;AAEjG;;;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the IR's aggregate roster exactly
4
- * (`bumbledb/crates/bumbledb/src/ir.rs` `AggOp`/`FindTerm`;
5
- * `docs/architecture/20-query-ir.md` § aggregation): `count` (nullary),
6
- * `countDistinct`, `sum`/`min`/`max` (over an orderable variable or the
7
- * measure), `argMax`/`argMin` (arg-restriction: carried value + orderable
8
- * key; a tie yields every attaining row), and `pack` (the coalescing fold —
9
- * RELATION-SHAPED: one answer row per (group, maximal segment), the result
10
- * position interval-typed). Aggregates name their variables — `r.sum("m")`
11
- * — and are typed by the rule environment at `.select`. Grouping is
12
- * implicit: the non-aggregate select entries are the group key; over empty
13
- * input an all-aggregate select yields the EMPTY SET, never a zero row.
14
- * The creation quarantine is representational: a head position is a var
15
- * name, the measure, or an aggregate — no minting or arithmetic term
16
- * exists to spell (permanent law).
17
- */
18
-
19
- import type { Infer } from "#fields.ts"
20
- import type { IntervalVarOk, OrderVarOk } from "#query/atom.ts"
21
- import type { Duration, EnvShape, ShapeOf } from "#query/scope.ts"
22
-
23
- /** One aggregate operator name of the select vocabulary. */
24
- type AggOpName = "count" | "countDistinct" | "sum" | "min" | "max" | "argMax" | "argMin" | "pack"
25
-
26
- /**
27
- * One aggregate select VALUE: the op, the variable name (or measure) it
28
- * folds, and — for the Arg forms — the orderable key's variable name. The
29
- * runtime representation carries the types; the rule environment types the
30
- * result at `.select`.
31
- */
32
- interface Agg<
33
- Op extends AggOpName,
34
- Over extends string | Duration<string> | undefined,
35
- Key extends string | undefined = undefined
36
- > {
37
- readonly agg: Op
38
- readonly over: Over
39
- readonly key: Key
40
- }
41
-
42
- /** Any aggregate select value. */
43
- type AnyAgg = Agg<AggOpName, string | Duration<string> | undefined, string | undefined>
44
-
45
- /** One select entry: a projected var name, the measure, or an aggregate. */
46
- type SelectEntry = string | Duration<string> | AnyAgg
47
-
48
- /** Builds one aggregate value. */
49
- function aggregate<
50
- Op extends AggOpName,
51
- Over extends string | Duration<string> | undefined,
52
- Key extends string | undefined
53
- >(op: Op, over: Over, key: Key): Agg<Op, Over, Key> {
54
- return Object.freeze({ agg: op, over, key })
55
- }
56
-
57
- /** Nullary count: |the group's set of distinct full bindings|, `bigint`; the answer column is named `count`. */
58
- function count(): Agg<"count", undefined> {
59
- return aggregate("count", undefined, undefined)
60
- }
61
-
62
- /** |the distinct values of the named variable across the group|, `bigint`; legal over every type. */
63
- function countDistinct<const N extends string>(over: N): Agg<"countDistinct", N> {
64
- return aggregate("countDistinct", over, undefined)
65
- }
66
-
67
- /**
68
- * Exact checked sum over an orderable (u64/i64) variable — wide
69
- * accumulator, one finalize range check; overflow is the engine's typed
70
- * runtime error, never a wrap — or over the measure
71
- * (`r.sum(r.duration("w"))`).
72
- */
73
- function sum<const N extends string | Duration<string>>(over: N): Agg<"sum", N> {
74
- return aggregate("sum", over, undefined)
75
- }
76
-
77
- /** Minimum over an orderable variable or the measure (orderable types only). */
78
- function min<const N extends string | Duration<string>>(over: N): Agg<"min", N> {
79
- return aggregate("min", over, undefined)
80
- }
81
-
82
- /** Maximum over an orderable variable or the measure (orderable types only). */
83
- function max<const N extends string | Duration<string>>(over: N): Agg<"max", N> {
84
- return aggregate("max", over, undefined)
85
- }
86
-
87
- /**
88
- * Arg-restriction toward the maximum of `key` (`ir::AggOp::ArgMax`): the
89
- * group's binding set is restricted to the bindings attaining the extreme
90
- * of the orderable key, and `value` is the carried payload — a tie yields
91
- * every attaining row. All Arg entries of one query share one key and one
92
- * direction; Arg and fold aggregates never mix (both the engine's typed
93
- * rules).
94
- */
95
- function argMax<const V extends string, const K extends string>(value: V, key: K): Agg<"argMax", V, K> {
96
- return aggregate("argMax", value, key)
97
- }
98
-
99
- /** Arg-restriction toward the minimum of `key`; rules as {@link argMax}. */
100
- function argMin<const V extends string, const K extends string>(value: V, key: K): Agg<"argMin", V, K> {
101
- return aggregate("argMin", value, key)
102
- }
103
-
104
- /**
105
- * The coalescing fold (Snodgrass coalesce, `ir::AggOp::Pack`): per group,
106
- * the maximal disjoint half-open segments of the union of the group's
107
- * interval point sets — RELATION-SHAPED, one answer row per (group,
108
- * maximal segment), the result position carrying one interval of the
109
- * input's element type. At most one `pack` per select, never beside a
110
- * fold or an Arg entry (the engine's typed rules).
111
- */
112
- function pack<const N extends string>(over: N): Agg<"pack", N> {
113
- return aggregate("pack", over, undefined)
114
- }
115
-
116
- /** A fold input's judgment: an orderable variable, or the measure of an interval variable. */
117
- type FoldOverOk<Env extends EnvShape, O> = O extends string
118
- ? OrderVarOk<Env, O>
119
- : O extends Duration<infer N extends string>
120
- ? IntervalVarOk<Env, N>
121
- : false
122
-
123
- /**
124
- * One select entry's judgment against the rule environment: projected
125
- * names must be bound, the measure and `pack` demand interval-typed
126
- * variables, folds and Arg keys demand orderable ones.
127
- */
128
- type SelectEntryOk<Env extends EnvShape, E> = E extends string
129
- ? E extends keyof Env
130
- ? true
131
- : false
132
- : E extends Duration<infer N extends string>
133
- ? IntervalVarOk<Env, N>
134
- : E extends Agg<"count", undefined>
135
- ? true
136
- : E extends Agg<"countDistinct", infer O extends string>
137
- ? O extends keyof Env
138
- ? true
139
- : false
140
- : E extends Agg<"sum" | "min" | "max", infer O>
141
- ? FoldOverOk<Env, O>
142
- : E extends Agg<"argMax" | "argMin", infer O extends string, infer K extends string>
143
- ? [O extends keyof Env ? true : false, OrderVarOk<Env, K>] extends [true, true]
144
- ? true
145
- : false
146
- : E extends Agg<"pack", infer O extends string>
147
- ? IntervalVarOk<Env, O>
148
- : false
149
-
150
- /** The validated select tuple (intersect with the inferred entries — errors land on the offending argument). */
151
- type CheckSelect<Env extends EnvShape, S> = {
152
- readonly [I in keyof S]: SelectEntryOk<Env, S[I]> extends true ? S[I] : never
153
- }
154
-
155
- /** The validated names-only select tuple of a RECURSIVE rule (aggregates and the measure are unwritable there). */
156
- type CheckNameSelect<Env extends EnvShape, S> = {
157
- readonly [I in keyof S]: S[I] extends keyof Env ? S[I] : never
158
- }
159
-
160
- /**
161
- * One select entry's answer-column fragment: the column is named by the
162
- * variable it projects or folds (`count` names its column `count`), and
163
- * its type reflects the entry — `count`/`countDistinct` are `bigint`
164
- * whatever they counted, folds carry their input's type, the measure is
165
- * `bigint`, the Arg forms carry the payload's type, `pack` its interval
166
- * type.
167
- */
168
- type SelectEntryRow<Env extends EnvShape, E> = E extends string
169
- ? { readonly [K in E]: Infer<Env[E & keyof Env]["field"]> }
170
- : E extends Duration<infer N extends string>
171
- ? { readonly [K in N]: bigint }
172
- : E extends Agg<"count", undefined>
173
- ? { readonly count: bigint }
174
- : E extends Agg<"countDistinct", infer O extends string>
175
- ? { readonly [K in O]: bigint }
176
- : E extends Agg<"sum" | "min" | "max", infer O>
177
- ? O extends string
178
- ? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
179
- : O extends Duration<infer N extends string>
180
- ? { readonly [K in N]: bigint }
181
- : never
182
- : E extends Agg<"argMax" | "argMin", infer O extends string, string>
183
- ? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
184
- : E extends Agg<"pack", infer O extends string>
185
- ? { readonly [K in O]: Infer<Env[O & keyof Env]["field"]> }
186
- : never
187
-
188
- /** The inferred answer-row object type of a select tuple. */
189
- type RowOfSelect<Env extends EnvShape, S extends readonly SelectEntry[]> = ShapeOf<SelectEntryRow<Env, S[number]>>
190
-
191
- /**
192
- * One projected name's answer-column fragment — a NAKED parameter, so the
193
- * judgment distributes per name (the union of a multi-name select never
194
- * smears into one column's type), mirroring {@link SelectEntryRow}.
195
- */
196
- type NameSelectRow<Env extends EnvShape, N> = N extends string
197
- ? { readonly [K in N]: Infer<Env[K & keyof Env]["field"]> }
198
- : never
199
-
200
- /** The inferred answer-row object type of a names-only (recursive-rule) select tuple. */
201
- type RowOfNameSelect<Env extends EnvShape, S extends readonly string[]> = ShapeOf<NameSelectRow<Env, S[number]>>
202
-
203
- export type {
204
- Agg,
205
- AggOpName,
206
- AnyAgg,
207
- CheckNameSelect,
208
- CheckSelect,
209
- RowOfNameSelect,
210
- RowOfSelect,
211
- SelectEntry,
212
- SelectEntryOk,
213
- SelectEntryRow
214
- }
215
- export { argMax, argMin, count, countDistinct, max, min, pack, sum }