@bjornpagen/bumbledb 0.4.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 (103) hide show
  1. package/COOKBOOK.md +191 -92
  2. package/README.md +9 -7
  3. package/dist/closed.d.ts +30 -2
  4. package/dist/closed.d.ts.map +1 -1
  5. package/dist/closed.js +66 -20
  6. package/dist/closed.js.map +1 -1
  7. package/dist/db.d.ts +33 -6
  8. package/dist/db.d.ts.map +1 -1
  9. package/dist/db.js +89 -72
  10. package/dist/db.js.map +1 -1
  11. package/dist/exhume.d.ts.map +1 -1
  12. package/dist/exhume.js +1 -14
  13. package/dist/exhume.js.map +1 -1
  14. package/dist/face.d.ts +1 -1
  15. package/dist/face.d.ts.map +1 -1
  16. package/dist/face.js +2 -1
  17. package/dist/face.js.map +1 -1
  18. package/dist/fields.d.ts +18 -1
  19. package/dist/fields.d.ts.map +1 -1
  20. package/dist/fields.js +44 -16
  21. package/dist/fields.js.map +1 -1
  22. package/dist/index.d.ts +18 -10
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +13 -6
  25. package/dist/index.js.map +1 -1
  26. package/dist/law.d.ts +2 -1
  27. package/dist/law.d.ts.map +1 -1
  28. package/dist/law.js +15 -14
  29. package/dist/law.js.map +1 -1
  30. package/dist/lower.d.ts.map +1 -1
  31. package/dist/lower.js +1 -7
  32. package/dist/lower.js.map +1 -1
  33. package/dist/marshal.d.ts.map +1 -1
  34. package/dist/marshal.js +15 -27
  35. package/dist/marshal.js.map +1 -1
  36. package/dist/native.d.ts +21 -2
  37. package/dist/native.d.ts.map +1 -1
  38. package/dist/native.js +20 -3
  39. package/dist/native.js.map +1 -1
  40. package/dist/order.d.ts +36 -0
  41. package/dist/order.d.ts.map +1 -0
  42. package/dist/order.js +135 -0
  43. package/dist/order.js.map +1 -0
  44. package/dist/query/atom.d.ts +140 -206
  45. package/dist/query/atom.d.ts.map +1 -1
  46. package/dist/query/atom.js +33 -52
  47. package/dist/query/atom.js.map +1 -1
  48. package/dist/query/find.d.ts +116 -0
  49. package/dist/query/find.d.ts.map +1 -0
  50. package/dist/query/{select.js → find.js} +22 -22
  51. package/dist/query/find.js.map +1 -0
  52. package/dist/query/lower.d.ts +124 -162
  53. package/dist/query/lower.d.ts.map +1 -1
  54. package/dist/query/lower.js +462 -507
  55. package/dist/query/lower.js.map +1 -1
  56. package/dist/query/predicate.d.ts +22 -14
  57. package/dist/query/predicate.d.ts.map +1 -1
  58. package/dist/query/predicate.js +45 -20
  59. package/dist/query/predicate.js.map +1 -1
  60. package/dist/query/run.d.ts +3 -3
  61. package/dist/query/run.d.ts.map +1 -1
  62. package/dist/query/run.js +10 -11
  63. package/dist/query/run.js.map +1 -1
  64. package/dist/query/scope.d.ts +124 -83
  65. package/dist/query/scope.d.ts.map +1 -1
  66. package/dist/query/scope.js +77 -70
  67. package/dist/query/scope.js.map +1 -1
  68. package/dist/relation.d.ts +9 -22
  69. package/dist/relation.d.ts.map +1 -1
  70. package/dist/relation.js +9 -28
  71. package/dist/relation.js.map +1 -1
  72. package/dist/schema.d.ts.map +1 -1
  73. package/dist/schema.js +7 -31
  74. package/dist/schema.js.map +1 -1
  75. package/dist/statements.d.ts +5 -2
  76. package/dist/statements.d.ts.map +1 -1
  77. package/dist/statements.js +30 -35
  78. package/dist/statements.js.map +1 -1
  79. package/package.json +2 -5
  80. package/src/closed.ts +73 -28
  81. package/src/db.ts +126 -88
  82. package/src/exhume.ts +1 -15
  83. package/src/face.ts +4 -3
  84. package/src/fields.ts +58 -16
  85. package/src/index.ts +19 -13
  86. package/src/law.ts +15 -14
  87. package/src/lower.ts +2 -9
  88. package/src/marshal.ts +15 -31
  89. package/src/native.ts +22 -4
  90. package/src/order.ts +156 -0
  91. package/src/query/atom.ts +186 -276
  92. package/src/query/find.ts +212 -0
  93. package/src/query/lower.ts +614 -749
  94. package/src/query/predicate.ts +47 -20
  95. package/src/query/run.ts +11 -13
  96. package/src/query/scope.ts +165 -132
  97. package/src/relation.ts +9 -51
  98. package/src/schema.ts +7 -33
  99. package/src/statements.ts +33 -38
  100. package/dist/query/select.d.ts +0 -128
  101. package/dist/query/select.d.ts.map +0 -1
  102. package/dist/query/select.js.map +0 -1
  103. package/src/query/select.ts +0 -215
@@ -1,215 +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
-
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 }