@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.
- package/COOKBOOK.md +96 -131
- package/README.md +9 -9
- package/dist/db.js +2 -2
- package/dist/db.js.map +1 -1
- package/dist/index.d.ts +8 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/query/atom.d.ts +132 -201
- package/dist/query/atom.d.ts.map +1 -1
- package/dist/query/atom.js +30 -42
- package/dist/query/atom.js.map +1 -1
- package/dist/query/find.d.ts +116 -0
- package/dist/query/find.d.ts.map +1 -0
- package/dist/query/{select.js → find.js} +22 -22
- package/dist/query/find.js.map +1 -0
- package/dist/query/lower.d.ts +123 -158
- package/dist/query/lower.d.ts.map +1 -1
- package/dist/query/lower.js +437 -493
- package/dist/query/lower.js.map +1 -1
- package/dist/query/predicate.d.ts +22 -14
- package/dist/query/predicate.d.ts.map +1 -1
- package/dist/query/predicate.js +35 -42
- package/dist/query/predicate.js.map +1 -1
- package/dist/query/run.d.ts +3 -3
- package/dist/query/run.d.ts.map +1 -1
- package/dist/query/run.js +9 -9
- package/dist/query/run.js.map +1 -1
- package/dist/query/scope.d.ts +127 -72
- package/dist/query/scope.d.ts.map +1 -1
- package/dist/query/scope.js +80 -33
- package/dist/query/scope.js.map +1 -1
- package/package.json +2 -2
- package/src/db.ts +4 -4
- package/src/index.ts +11 -7
- package/src/query/atom.ts +181 -263
- package/src/query/find.ts +212 -0
- package/src/query/lower.ts +588 -722
- package/src/query/predicate.ts +37 -45
- package/src/query/run.ts +10 -10
- package/src/query/scope.ts +172 -87
- package/dist/query/select.d.ts +0 -128
- package/dist/query/select.d.ts.map +0 -1
- package/dist/query/select.js.map +0 -1
- package/src/query/select.ts +0 -215
package/src/query/atom.ts
CHANGED
|
@@ -1,86 +1,60 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Atoms and conditions,
|
|
3
|
-
* rule, mirroring the engine IR variant for variant
|
|
2
|
+
* Atoms and conditions, REFERENCE-IDENTITY edition — the body vocabulary of
|
|
3
|
+
* a rule, mirroring the engine IR variant for variant
|
|
4
4
|
* (`bumbledb/crates/bumbledb/src/ir.rs`, the bijection target;
|
|
5
5
|
* `docs/architecture/20-query-ir.md` normative). A `match` binding record
|
|
6
|
-
* binds fields to
|
|
7
|
-
* — a closed-reference field's literal is its
|
|
8
|
-
* ARRAY of names there is membership
|
|
9
|
-
* by owner ruling; see {@link BindingInput})
|
|
6
|
+
* binds fields to VARIABLES (minted by {@link v}), params, ∈-set params, or
|
|
7
|
+
* bare structural literals — a closed-reference field's literal is its
|
|
8
|
+
* handle NAME, and a plain ARRAY of names there is membership
|
|
10
9
|
* (unmentioned fields ARE the wildcard — no wildcard value exists);
|
|
11
10
|
* `not(Rel, {...})` is negation-as-position (anti-join); `eq`/`ne` and the
|
|
12
11
|
* order roster, `pointIn` (the one spelling of `ir::CmpOp::PointIn`,
|
|
13
12
|
* always lowered interval-left), `allen` (the 13-bit mask pair
|
|
14
13
|
* comparison), and `and`/`or` (the input condition-tree grammar) complete
|
|
15
14
|
* the roster. Nothing beyond the IR exists here — and the walls the engine
|
|
16
|
-
* enforces at prepare are TYPES first: a
|
|
17
|
-
* fields
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
15
|
+
* enforces at prepare are TYPES first: a variable joins only class-equal
|
|
16
|
+
* fields, judged at every binding position against the var's MINT slot
|
|
17
|
+
* ({@link MintSlotOf}); because {@link JoinOk} is an equality, that ALONE
|
|
18
|
+
* makes every cross-binding join transitively class-equal (the env/sibling
|
|
19
|
+
* checks the name-keyed edition needed are gone). An interval-typed var
|
|
20
|
+
* under a non-`pointIn` comparison is unwritable. BOUNDNESS (a negated
|
|
21
|
+
* atom's variables must be positively bound) is the one check types cannot
|
|
22
|
+
* carry — object identity is invisible to TS — so it is a construction-time
|
|
23
|
+
* wall only.
|
|
24
24
|
*
|
|
25
25
|
* This module also owns the plain runtime DATA a built rule is made of
|
|
26
26
|
* (`RuleData`/`RecData` and friends): frozen values the lowering walks —
|
|
27
|
-
* pure data, so lowering stays a pure,
|
|
27
|
+
* pure data (variable references included), so lowering stays a pure,
|
|
28
|
+
* stable function of the query value.
|
|
28
29
|
*/
|
|
29
30
|
|
|
30
31
|
import * as errors from "@superbuilders/errors"
|
|
31
|
-
import type { AnyClosed } from "#closed.ts"
|
|
32
32
|
import type { AnyField, ClosedIdField, ClosedRoster, Infer, IntervalValue } from "#fields.ts"
|
|
33
33
|
import type { ClassLookup, ClassRecordOf, SchemaClasses } from "#law.ts"
|
|
34
34
|
import type {
|
|
35
|
+
AnyVar,
|
|
35
36
|
ClassedField,
|
|
36
37
|
Duration,
|
|
37
|
-
EnvShape,
|
|
38
38
|
JoinOk,
|
|
39
39
|
MaskParam,
|
|
40
|
+
MatchFields,
|
|
41
|
+
MatchOwner,
|
|
42
|
+
MintSlotOf,
|
|
40
43
|
Param,
|
|
41
44
|
ParamValueAt,
|
|
42
45
|
SetParam,
|
|
43
|
-
ShapeOf
|
|
44
|
-
Var
|
|
46
|
+
ShapeOf
|
|
45
47
|
} from "#query/scope.ts"
|
|
46
48
|
import { isTerm } from "#query/scope.ts"
|
|
47
|
-
import type {
|
|
49
|
+
import type { FieldsShape } from "#relation.ts"
|
|
48
50
|
|
|
49
51
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* virtual image when the shape does not fold; the SDK never pre-folds and
|
|
54
|
-
* never knows which — transparency is the contract).
|
|
55
|
-
*/
|
|
56
|
-
type MatchOwner = AnyRelation | AnyClosed
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The matchable field block of an atom owner: a relation's declared
|
|
60
|
-
* fields; a closed relation's SEALED shape — the synthetic `id` (the
|
|
61
|
-
* value's OWN roster-carrying descriptor, at its precise type: the handle
|
|
62
|
-
* union rides into ψ id bindings and joins exactly as it does on a
|
|
63
|
-
* referencing column) first, then the declared payload columns read
|
|
64
|
-
* through the typed `columns` carrier (the one source of payload typing —
|
|
65
|
-
* no parallel column table exists). The runtime twin is `sealedFieldsOf` in
|
|
66
|
-
* `#query/lower.ts`; the id-first ordinal shift the two tiers share is
|
|
67
|
-
* pinned by the lowering golden.
|
|
68
|
-
*/
|
|
69
|
-
type MatchFields<R extends MatchOwner> = R extends AnyClosed
|
|
70
|
-
? { readonly id: R["id"] } & R["columns"]
|
|
71
|
-
: R extends AnyRelation
|
|
72
|
-
? RelationFields<R>
|
|
73
|
-
: never
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* One atom-binding position as runtime data. `literalSet` is a membership
|
|
77
|
-
* ARRAY at a closed-reference field, folded into the program: `name` is
|
|
78
|
-
* the content-addressed registry key its dense `ParamId` is minted under
|
|
79
|
-
* (the lowering rides the existing param-set term; the SDK itself supplies
|
|
80
|
-
* the translated member set at every execute — never the host).
|
|
52
|
+
* One atom-binding position as runtime data. A variable rides BY REFERENCE
|
|
53
|
+
* (`ref`) — object identity is the join. `literalSet` is a membership
|
|
54
|
+
* ARRAY at a closed-reference field, folded into the program.
|
|
81
55
|
*/
|
|
82
56
|
type BindingTermData =
|
|
83
|
-
| { readonly kind: "var"; readonly
|
|
57
|
+
| { readonly kind: "var"; readonly ref: AnyVar }
|
|
84
58
|
| { readonly kind: "param"; readonly name: string }
|
|
85
59
|
| { readonly kind: "setParam"; readonly name: string }
|
|
86
60
|
| { readonly kind: "literalSet"; readonly name: string; readonly members: readonly string[] }
|
|
@@ -103,12 +77,12 @@ interface AtomData {
|
|
|
103
77
|
/** One comparison operator name (mirrors `ir::CmpOp`). */
|
|
104
78
|
type CmpKind = "eq" | "ne" | "lt" | "le" | "gt" | "ge" | "pointIn" | "allen"
|
|
105
79
|
|
|
106
|
-
/** One comparison side as runtime data. */
|
|
80
|
+
/** One comparison side as runtime data (variables and the measure ride BY REFERENCE). */
|
|
107
81
|
type CmpTermData =
|
|
108
|
-
| { readonly kind: "var"; readonly
|
|
82
|
+
| { readonly kind: "var"; readonly ref: AnyVar }
|
|
109
83
|
| { readonly kind: "param"; readonly name: string }
|
|
110
84
|
| { readonly kind: "setParam"; readonly name: string }
|
|
111
|
-
| { readonly kind: "measure"; readonly
|
|
85
|
+
| { readonly kind: "measure"; readonly ref: AnyVar }
|
|
112
86
|
| { readonly kind: "literal"; readonly value: unknown }
|
|
113
87
|
|
|
114
88
|
/** The `allen` mask position as runtime data. */
|
|
@@ -133,48 +107,61 @@ interface TreeData {
|
|
|
133
107
|
/** Any condition node as runtime data. */
|
|
134
108
|
type CondData = CmpData | TreeData
|
|
135
109
|
|
|
136
|
-
/** One aggregate's runtime description (
|
|
110
|
+
/** One aggregate's runtime description (find vocabulary, over variable REFERENCES). */
|
|
137
111
|
type AggData =
|
|
138
112
|
| { readonly op: "count" }
|
|
139
|
-
| { readonly op: "countDistinct"; readonly over:
|
|
140
|
-
| {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
| { readonly
|
|
147
|
-
|
|
113
|
+
| { readonly op: "countDistinct"; readonly over: AnyVar }
|
|
114
|
+
| {
|
|
115
|
+
readonly op: "fold"
|
|
116
|
+
readonly fold: "sum" | "min" | "max"
|
|
117
|
+
readonly over: AnyVar | { readonly duration: AnyVar }
|
|
118
|
+
}
|
|
119
|
+
| { readonly op: "arg"; readonly direction: "argMax" | "argMin"; readonly over: AnyVar; readonly key: AnyVar }
|
|
120
|
+
| { readonly op: "pack"; readonly over: AnyVar }
|
|
121
|
+
|
|
122
|
+
/** One classified find entry as runtime data (variables and the measure ride BY REFERENCE). */
|
|
123
|
+
type FindEntryData =
|
|
124
|
+
| { readonly kind: "var"; readonly over: AnyVar }
|
|
125
|
+
| { readonly kind: "measure"; readonly over: AnyVar }
|
|
148
126
|
| { readonly kind: "aggregate"; readonly agg: AggData }
|
|
149
127
|
|
|
150
128
|
/**
|
|
151
|
-
* One answer column: its name (the row object key
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
129
|
+
* One answer column: its name (the row object key — the find record's key,
|
|
130
|
+
* so renames are real and a duplicate column is unrepresentable), its entry,
|
|
131
|
+
* the classed mint SLOT its values flow from (a projected var or an
|
|
132
|
+
* Arg-carried payload; `undefined` for counts/folds/measures/pack, which
|
|
133
|
+
* derive numbers or intervals), and — when that slot is a closed reference —
|
|
134
|
+
* the roster the decode lifts row ids back to handle NAMES through
|
|
135
|
+
* (`undefined` on every bare column). The slice is SDK-side marshaling data
|
|
136
|
+
* only: the wire `ProgramIr` never carries it.
|
|
157
137
|
*/
|
|
158
|
-
interface
|
|
138
|
+
interface FindColumn {
|
|
159
139
|
readonly name: string
|
|
160
|
-
readonly entry:
|
|
140
|
+
readonly entry: FindEntryData
|
|
161
141
|
readonly closed: ClosedRoster | undefined
|
|
142
|
+
readonly slot: ClassedField | undefined
|
|
162
143
|
}
|
|
163
144
|
|
|
164
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* One body item of a rule, in written order. The idb join is a NAMED record
|
|
147
|
+
* over the rec's head keys (`key`) bound to local variables (`ref`) — the
|
|
148
|
+
* typed wall a record's unordered keys would otherwise lose.
|
|
149
|
+
*/
|
|
165
150
|
type RuleItem =
|
|
166
151
|
| { readonly kind: "atom"; readonly atom: AtomData }
|
|
167
152
|
| { readonly kind: "negated"; readonly atom: AtomData }
|
|
168
|
-
| {
|
|
153
|
+
| {
|
|
154
|
+
readonly kind: "idb"
|
|
155
|
+
readonly rec: RecData
|
|
156
|
+
readonly bindings: ReadonlyArray<{ readonly key: string; readonly ref: AnyVar }>
|
|
157
|
+
}
|
|
169
158
|
| { readonly kind: "cond"; readonly cond: CondData }
|
|
170
159
|
|
|
171
160
|
/**
|
|
172
161
|
* One use of a parameter inside a rule, in written order: the census the
|
|
173
162
|
* query-level registry folds (first use mints the dense `ParamId`, first
|
|
174
163
|
* FIELD-ANCHORED use types the wire). `members` is present exactly on a
|
|
175
|
-
* membership-array use
|
|
176
|
-
* names the SDK itself translates and supplies at execute — the entry
|
|
177
|
-
* never appears in the host's params object.
|
|
164
|
+
* membership-array use.
|
|
178
165
|
*/
|
|
179
166
|
interface ParamUse {
|
|
180
167
|
readonly name: string
|
|
@@ -187,9 +174,7 @@ interface ParamUse {
|
|
|
187
174
|
/** One complete rule as runtime data. */
|
|
188
175
|
interface RuleData {
|
|
189
176
|
readonly items: readonly RuleItem[]
|
|
190
|
-
readonly
|
|
191
|
-
/** Variable name → the classed slot its FIRST positive binding carries (the runtime env — descriptor + class). */
|
|
192
|
-
readonly varFields: Readonly<Record<string, ClassedField>>
|
|
177
|
+
readonly finds: readonly FindColumn[]
|
|
193
178
|
readonly paramUses: readonly ParamUse[]
|
|
194
179
|
}
|
|
195
180
|
|
|
@@ -205,23 +190,15 @@ interface RecData {
|
|
|
205
190
|
|
|
206
191
|
/**
|
|
207
192
|
* What a binding position of field `F` accepts: a bare structural literal
|
|
208
|
-
* of the field's value type, a
|
|
209
|
-
* field is interval-typed, a bare point literal
|
|
210
|
-
*
|
|
211
|
-
* membership; an interval-typed term is value equality). A
|
|
212
|
-
* CLOSED-reference field additionally takes a plain ARRAY of handle names
|
|
213
|
-
* read as membership — `kind: ["Practice", "Review"]` (the drizzle law:
|
|
214
|
-
* set membership is an array, never an operator). Arrays are CLOSED-ONLY
|
|
215
|
-
* in this packet by owner ruling: ordinary u64/str membership already has
|
|
216
|
-
* its spelling through `r.inSet` params; widening literal arrays to every
|
|
217
|
-
* literal-capable kind is a separate future taste call — deliberately not
|
|
218
|
-
* done here.
|
|
193
|
+
* of the field's value type, a variable/param/∈-set-param term — and, when
|
|
194
|
+
* the field is interval-typed, a bare point literal. A CLOSED-reference
|
|
195
|
+
* field additionally takes a plain ARRAY of handle names read as membership.
|
|
219
196
|
*/
|
|
220
197
|
type BindingInput<F extends AnyField> =
|
|
221
198
|
| Infer<F>
|
|
222
199
|
| (F extends ClosedIdField ? readonly Infer<F>[] : never)
|
|
223
200
|
| (F extends { readonly kind: "interval" } ? bigint : never)
|
|
224
|
-
|
|
|
201
|
+
| AnyVar
|
|
225
202
|
| Param<string>
|
|
226
203
|
| SetParam<string>
|
|
227
204
|
|
|
@@ -237,8 +214,8 @@ type MatchShape<F extends FieldsShape> = {
|
|
|
237
214
|
/**
|
|
238
215
|
* One field position of a bindings record as a classed slot: the declared
|
|
239
216
|
* descriptor plus the slot's law-computed class, read off the relation's
|
|
240
|
-
* class record (`CR`
|
|
241
|
-
*
|
|
217
|
+
* class record (`CR`). The one shape a binding position's join judgment
|
|
218
|
+
* compares against.
|
|
242
219
|
*/
|
|
243
220
|
type SlotAt<F extends FieldsShape, CR, K> = {
|
|
244
221
|
readonly field: F[K & keyof F]
|
|
@@ -246,49 +223,20 @@ type SlotAt<F extends FieldsShape, CR, K> = {
|
|
|
246
223
|
}
|
|
247
224
|
|
|
248
225
|
/**
|
|
249
|
-
* The
|
|
250
|
-
*
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
: true
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* The var binding's judgment against its OWN record's siblings: two
|
|
258
|
-
* bindings of one var name inside a single bindings record are the same
|
|
259
|
-
* join the environment check judges across atoms, so every same-named
|
|
260
|
-
* sibling must be class-equal too. Without this arm two FIRST occurrences
|
|
261
|
-
* of one name (a record the environment has not seen yet) would meet no
|
|
262
|
-
* check at all — the intra-atom join would silently cross classes.
|
|
263
|
-
*/
|
|
264
|
-
type SiblingJoinOk<F extends FieldsShape, CR, B, K extends keyof B, N extends string> = false extends {
|
|
265
|
-
[K2 in Exclude<keyof B & keyof F, K>]: B[K2] extends Var<N> ? JoinOk<SlotAt<F, CR, K2>, SlotAt<F, CR, K>> : true
|
|
266
|
-
}[Exclude<keyof B & keyof F, K>]
|
|
267
|
-
? false
|
|
268
|
-
: true
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* The per-property join judgment of a bindings record: a var binding must
|
|
272
|
-
* be class-equal to the rule environment's binding of the name AND to
|
|
273
|
-
* every same-named sibling of its own record (a cross-class reuse maps
|
|
274
|
-
* the property to `never` — the compile error the old value brand carried,
|
|
275
|
-
* now law-born off the schema type's class map).
|
|
226
|
+
* The per-property join judgment of a bindings record: a VARIABLE binding
|
|
227
|
+
* must join its own MINT slot to the position slot (a cross-class reuse maps
|
|
228
|
+
* the property to `never`). Because {@link JoinOk} is an equality, judging
|
|
229
|
+
* every position against the mint slot makes all cross-binding joins
|
|
230
|
+
* mutually class-equal by transitivity — no env or sibling arm is needed.
|
|
276
231
|
*/
|
|
277
|
-
type
|
|
278
|
-
|
|
279
|
-
? [
|
|
280
|
-
? true
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
type CheckBindings<Env extends EnvShape, F extends FieldsShape, CR, B> = {
|
|
286
|
-
readonly [K in keyof B]: K extends keyof F ? (BindingOk<Env, F, CR, B, K> extends true ? B[K] : never) : never
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/** The environment a bindings record contributes: var name → the bound slot (descriptor + class). */
|
|
290
|
-
type BindEnv<F extends FieldsShape, CR, B> = {
|
|
291
|
-
readonly [K in keyof B & keyof F as B[K] extends Var<infer N extends string> ? N : never]: SlotAt<F, CR, K>
|
|
232
|
+
type CheckBindings<Classes extends SchemaClasses, F extends FieldsShape, CR, B> = {
|
|
233
|
+
readonly [K in keyof B]: K extends keyof F
|
|
234
|
+
? B[K] extends AnyVar
|
|
235
|
+
? JoinOk<MintSlotOf<Classes, B[K]>, SlotAt<F, CR, K>> extends true
|
|
236
|
+
? B[K]
|
|
237
|
+
: never
|
|
238
|
+
: B[K]
|
|
239
|
+
: never
|
|
292
240
|
}
|
|
293
241
|
|
|
294
242
|
/** The params-object fragments a bindings record contributes (one union member per param use). */
|
|
@@ -302,8 +250,8 @@ type BindParams<F extends FieldsShape, B> = {
|
|
|
302
250
|
|
|
303
251
|
/**
|
|
304
252
|
* One comparison VALUE: op plus its operands, raw — the runtime
|
|
305
|
-
* representation carries the
|
|
306
|
-
*
|
|
253
|
+
* representation carries the operands (variable references included), so
|
|
254
|
+
* `.where`'s judgment and the params inference both read the value itself.
|
|
307
255
|
* `mask` is populated exactly for `allen`.
|
|
308
256
|
*/
|
|
309
257
|
interface Cmp<Op extends CmpKind, L, R, M = undefined> {
|
|
@@ -324,9 +272,9 @@ interface Tree<Ch extends readonly AnyTreeChild[]> {
|
|
|
324
272
|
/**
|
|
325
273
|
* One negated-atom VALUE — negation is a position in the rule (anti-join
|
|
326
274
|
* over sets, no null trick): a binding satisfies it iff NO fact matches.
|
|
327
|
-
* Its variables must be positively bound in the rule —
|
|
328
|
-
*
|
|
329
|
-
*
|
|
275
|
+
* Its variables must be positively bound in the rule — a construction-time
|
|
276
|
+
* wall (BOUNDNESS is invisible to the type tier), before the engine's own
|
|
277
|
+
* refusal.
|
|
330
278
|
*/
|
|
331
279
|
interface NotAtom<R extends MatchOwner, B> {
|
|
332
280
|
readonly cond: "not"
|
|
@@ -347,19 +295,19 @@ type AnyNotAtom = NotAtom<MatchOwner, unknown>
|
|
|
347
295
|
type AnyCond = AnyCmp | Tree<readonly AnyTreeChild[]> | AnyNotAtom
|
|
348
296
|
|
|
349
297
|
/** What `eq`'s right side accepts (`ParamSet` is `Eq`-only — the IR's rule). */
|
|
350
|
-
type EqRight =
|
|
298
|
+
type EqRight = AnyVar | Param<string> | SetParam<string> | bigint | string | boolean | Uint8Array | IntervalValue
|
|
351
299
|
|
|
352
300
|
/** What `ne`'s right side accepts. */
|
|
353
|
-
type NeRight =
|
|
301
|
+
type NeRight = AnyVar | Param<string> | bigint | string | boolean | Uint8Array | IntervalValue
|
|
354
302
|
|
|
355
303
|
/** One side of an order comparison: orderable terms only (the IR's comparison rules). */
|
|
356
|
-
type OrderSide =
|
|
304
|
+
type OrderSide = AnyVar | Param<string> | Duration | bigint
|
|
357
305
|
|
|
358
306
|
/** The point side of `pointIn`. */
|
|
359
|
-
type PointSide =
|
|
307
|
+
type PointSide = AnyVar | Param<string> | bigint
|
|
360
308
|
|
|
361
309
|
/** The interval side of `pointIn`/`allen`. */
|
|
362
|
-
type IntervalSide =
|
|
310
|
+
type IntervalSide = AnyVar | Param<string> | IntervalValue
|
|
363
311
|
|
|
364
312
|
/** Builds one comparison value. */
|
|
365
313
|
function comparison<Op extends CmpKind, L, R, M>(op: Op, lhs: L, rhs: R, mask: M): Cmp<Op, L, R, M> {
|
|
@@ -381,17 +329,17 @@ function assertTermSide(op: string, lhs: unknown, rhs: unknown): void {
|
|
|
381
329
|
|
|
382
330
|
/**
|
|
383
331
|
* The equality comparison (`ir::CmpOp::Eq`) — a bound variable against a
|
|
384
|
-
* variable (var-to-var unification,
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
332
|
+
* variable (var-to-var unification, class-equal by the join judgment), a
|
|
333
|
+
* param (typed by the variable), an ∈-set param (`Eq`-only), or a bare
|
|
334
|
+
* literal of the variable's own value type. Prefer direct placement in
|
|
335
|
+
* `match` where punning applies.
|
|
388
336
|
*/
|
|
389
|
-
function eq<L extends
|
|
337
|
+
function eq<L extends AnyVar, const R extends EqRight>(left: L, right: R): Cmp<"eq", L, R> {
|
|
390
338
|
return comparison("eq", left, right, undefined)
|
|
391
339
|
}
|
|
392
340
|
|
|
393
341
|
/** Typed disequality (`ir::CmpOp::Ne`). "Not in set" has no operator — write a negated atom. */
|
|
394
|
-
function ne<L extends
|
|
342
|
+
function ne<L extends AnyVar, const R extends NeRight>(left: L, right: R): Cmp<"ne", L, R> {
|
|
395
343
|
return comparison("ne", left, right, undefined)
|
|
396
344
|
}
|
|
397
345
|
|
|
@@ -429,12 +377,8 @@ function ge<const L extends OrderSide, const R extends OrderSide>(left: L, right
|
|
|
429
377
|
* Point membership as a predicate (`ir::CmpOp::PointIn`) — THE one
|
|
430
378
|
* spelling: `pointIn(t, w)` holds iff `w.start ≤ t < w.end`. The IR
|
|
431
379
|
* orders the operands interval-left, point-right; the value stores them
|
|
432
|
-
* that way whatever the surface argument order
|
|
433
|
-
*
|
|
434
|
-
* domain — the bug-hunt fix, now also a type-level guarantee).
|
|
435
|
-
* Interval ⊇ interval is NOT this operator; that predicate is
|
|
436
|
-
* `allen(a, ALLEN.covers, b)` — the name `covers` belongs to the Allen
|
|
437
|
-
* roster alone (the canonical-utterance law: one meaning, one spelling).
|
|
380
|
+
* that way whatever the surface argument order. Interval ⊇ interval is NOT
|
|
381
|
+
* this operator; that predicate is `allen(a, ALLEN.covers, b)`.
|
|
438
382
|
*/
|
|
439
383
|
function pointIn<const P extends PointSide, const I extends IntervalSide>(point: P, interval: I): Cmp<"pointIn", I, P> {
|
|
440
384
|
assertTermSide("pointIn", point, interval)
|
|
@@ -444,17 +388,14 @@ function pointIn<const P extends PointSide, const I extends IntervalSide>(point:
|
|
|
444
388
|
/**
|
|
445
389
|
* The 13-bit mask range: bits above the low 13 are unrepresentable in the
|
|
446
390
|
* engine's `AllenMask` (`bumbledb/crates/bumbledb/src/allen.rs`:
|
|
447
|
-
* `AllenMask::new` refuses them)
|
|
448
|
-
* moved to construction where the message can name the constants.
|
|
391
|
+
* `AllenMask::new` refuses them).
|
|
449
392
|
*/
|
|
450
393
|
const ALLEN_ALL_BITS = (1 << 13) - 1
|
|
451
394
|
|
|
452
395
|
/**
|
|
453
396
|
* The Allen coordinate system's named constants — the 13 basics in the
|
|
454
|
-
* engine's palindromic bit order
|
|
455
|
-
* `
|
|
456
|
-
* plus the workload composites, values identical to the engine's. Compose
|
|
457
|
-
* with `|`: `ALLEN.before | ALLEN.meets`.
|
|
397
|
+
* engine's palindromic bit order plus the workload composites, values
|
|
398
|
+
* identical to the engine's. Compose with `|`: `ALLEN.before | ALLEN.meets`.
|
|
458
399
|
*/
|
|
459
400
|
const ALLEN = Object.freeze({
|
|
460
401
|
before: 1 << 0,
|
|
@@ -484,9 +425,7 @@ const ALLEN = Object.freeze({
|
|
|
484
425
|
* THE interval-pair comparison (`ir::CmpOp::Allen`): two interval terms of
|
|
485
426
|
* one element type, satisfied iff the pair's classification is in the
|
|
486
427
|
* 13-bit mask — a literal built from the `ALLEN` constants, or a mask
|
|
487
|
-
* parameter (`r.maskParam`).
|
|
488
|
-
* two distinct typed rejections; nothing is pre-judged here beyond the
|
|
489
|
-
* representable bit range.
|
|
428
|
+
* parameter (`r.maskParam`).
|
|
490
429
|
*/
|
|
491
430
|
function allen<const A extends IntervalSide, const M extends number | MaskParam<string>, const B extends IntervalSide>(
|
|
492
431
|
left: A,
|
|
@@ -515,22 +454,18 @@ function and<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C> {
|
|
|
515
454
|
/**
|
|
516
455
|
* Disjunction node of the input condition grammar (`ConditionTree::Or`) —
|
|
517
456
|
* the one place the surface admits a nested OR; validation distributes it
|
|
518
|
-
* to DNF rules engine-side
|
|
519
|
-
* its algebraic reading (false: the rule denotes nothing).
|
|
457
|
+
* to DNF rules engine-side. `Or([])` keeps its algebraic reading (false).
|
|
520
458
|
*/
|
|
521
459
|
function or<const C extends readonly AnyTreeChild[]>(...children: C): Tree<C> {
|
|
522
460
|
return Object.freeze({ cond: "tree", op: "or", children: Object.freeze(children) })
|
|
523
461
|
}
|
|
524
462
|
|
|
525
463
|
/**
|
|
526
|
-
* Negation — anti-join over sets: `not(Rel, { field:
|
|
464
|
+
* Negation — anti-join over sets: `not(Rel, { field: someVar, ... })`
|
|
527
465
|
* rejects every binding some matching fact extends. A negated atom binds
|
|
528
466
|
* nothing, only rejects: every variable it names must be positively bound
|
|
529
|
-
* in the rule,
|
|
530
|
-
*
|
|
531
|
-
* here too — the engine folds a resolvable negated closed atom to the
|
|
532
|
-
* COMPLEMENT of its member set (domain-witness guarded), and the SDK's
|
|
533
|
-
* negation rules apply to it unchanged.
|
|
467
|
+
* in the rule, a construction-time wall (the engine's safety refusal stands
|
|
468
|
+
* behind it). A CLOSED owner is legal here too.
|
|
534
469
|
*/
|
|
535
470
|
function not<R extends MatchOwner, const B extends MatchShape<MatchFields<R>>>(
|
|
536
471
|
relation: R,
|
|
@@ -541,122 +476,106 @@ function not<R extends MatchOwner, const B extends MatchShape<MatchFields<R>>>(
|
|
|
541
476
|
}
|
|
542
477
|
|
|
543
478
|
/**
|
|
544
|
-
* Whether a
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
550
|
-
* judgment is the one gate they all read, and the construction-time
|
|
551
|
-
* validations in `#query/lower.ts` are its runtime twin.
|
|
479
|
+
* Whether a variable's OWN field is orderable (u64/i64). A CLOSED reference
|
|
480
|
+
* is excluded even though its kind is `u64`: a vocabulary's declaration-id
|
|
481
|
+
* order is an accident, not semantics (`docs/architecture/10-data-model.md`
|
|
482
|
+
* § orderability), so every order-comparison and fold position refuses
|
|
483
|
+
* closed-bound terms — this judgment is the one gate they all read, and the
|
|
484
|
+
* construction-time validations in `#query/lower.ts` are its runtime twin.
|
|
552
485
|
*/
|
|
553
|
-
type OrderVarOk<
|
|
554
|
-
?
|
|
555
|
-
|
|
556
|
-
: Env[N]["field"]["kind"] extends "u64" | "i64"
|
|
557
|
-
? true
|
|
558
|
-
: false
|
|
559
|
-
: false
|
|
560
|
-
|
|
561
|
-
/** Whether a var name is bound at an interval field. */
|
|
562
|
-
type IntervalVarOk<Env extends EnvShape, N extends string> = N extends keyof Env
|
|
563
|
-
? Env[N]["field"]["kind"] extends "interval"
|
|
486
|
+
type OrderVarOk<V extends AnyVar> = V["field"] extends { readonly closed: ClosedRoster }
|
|
487
|
+
? false
|
|
488
|
+
: V["field"]["kind"] extends "u64" | "i64"
|
|
564
489
|
? true
|
|
565
490
|
: false
|
|
566
|
-
: false
|
|
567
491
|
|
|
568
|
-
/**
|
|
569
|
-
type
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
492
|
+
/** Whether a variable's OWN field is interval-typed. */
|
|
493
|
+
type IntervalVarOk<V extends AnyVar> = V["field"]["kind"] extends "interval" ? true : false
|
|
494
|
+
|
|
495
|
+
/** One order-comparison side's judgment (off the term's own field). */
|
|
496
|
+
type OrderSideOk<T> = T extends AnyVar
|
|
497
|
+
? OrderVarOk<T>
|
|
498
|
+
: T extends Duration<infer V extends AnyVar>
|
|
499
|
+
? IntervalVarOk<V>
|
|
500
|
+
: true
|
|
575
501
|
|
|
576
502
|
/** One point side's judgment. */
|
|
577
|
-
type PointSideOk<
|
|
503
|
+
type PointSideOk<T> = T extends AnyVar ? OrderVarOk<T> : true
|
|
578
504
|
|
|
579
505
|
/** One interval side's judgment. */
|
|
580
|
-
type IntervalSideOk<
|
|
581
|
-
|
|
582
|
-
/** The `eq`/`ne` judgment:
|
|
583
|
-
type EqOk<
|
|
584
|
-
|
|
585
|
-
?
|
|
586
|
-
?
|
|
587
|
-
? M extends keyof Env
|
|
588
|
-
? JoinOk<Env[N], Env[M]>
|
|
589
|
-
: false
|
|
590
|
-
: R extends Param<string> | SetParam<string>
|
|
591
|
-
? true
|
|
592
|
-
: [R] extends [Infer<Env[N]["field"]>]
|
|
593
|
-
? true
|
|
594
|
-
: false
|
|
506
|
+
type IntervalSideOk<T> = T extends AnyVar ? IntervalVarOk<T> : true
|
|
507
|
+
|
|
508
|
+
/** The `eq`/`ne` judgment: var-var joins by mint slot; var-literal is exact-typed by the var's own field. */
|
|
509
|
+
type EqOk<Classes extends SchemaClasses, L, R> = L extends AnyVar
|
|
510
|
+
? R extends AnyVar
|
|
511
|
+
? JoinOk<MintSlotOf<Classes, L>, MintSlotOf<Classes, R>> extends true
|
|
512
|
+
? true
|
|
595
513
|
: false
|
|
596
|
-
:
|
|
514
|
+
: R extends Param<string> | SetParam<string>
|
|
515
|
+
? true
|
|
516
|
+
: [R] extends [Infer<L["field"]>]
|
|
517
|
+
? true
|
|
518
|
+
: false
|
|
519
|
+
: false
|
|
597
520
|
|
|
598
|
-
/** One negated-atom binding's judgment: a
|
|
599
|
-
type NotBindingOk<
|
|
600
|
-
|
|
521
|
+
/** One negated-atom binding's judgment: a variable must be class-equal (boundness is a runtime wall). */
|
|
522
|
+
type NotBindingOk<Classes extends SchemaClasses, S extends ClassedField, T> = T extends AnyVar
|
|
523
|
+
? JoinOk<MintSlotOf<Classes, T>, S> extends true
|
|
524
|
+
? true
|
|
525
|
+
: false
|
|
526
|
+
: true
|
|
601
527
|
|
|
602
528
|
/** The whole negated atom's judgment (`CR` — the negated relation's class record off the schema class map). */
|
|
603
|
-
type NotOk<
|
|
604
|
-
[K in keyof B]: NotBindingOk<
|
|
529
|
+
type NotOk<Classes extends SchemaClasses, F extends FieldsShape, CR, B> = false extends {
|
|
530
|
+
[K in keyof B]: NotBindingOk<Classes, SlotAt<F, CR, K>, B[K]>
|
|
605
531
|
}[keyof B]
|
|
606
532
|
? false
|
|
607
533
|
: true
|
|
608
534
|
|
|
609
535
|
/**
|
|
610
|
-
* One condition's judgment
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
* `
|
|
615
|
-
* relation's class record is resolved through `Classes` by its name). The
|
|
616
|
-
* leading `[AnyTreeChild] extends [C]` arm is the recursion's base case:
|
|
617
|
-
* at an UNRESOLVED constraint (the whole condition union — or a tree's
|
|
618
|
-
* child union, which is the union itself) the judgment is vacuously true —
|
|
619
|
-
* without it the constraint instantiation recurses into itself.
|
|
536
|
+
* One condition's judgment — the type-level twin of the engine's comparison
|
|
537
|
+
* roster: class-equal joins (off the mint slots), orderable order sides (an
|
|
538
|
+
* interval var under a non-`pointIn` op is exactly here refused),
|
|
539
|
+
* kind-correct `pointIn`/`allen` sides, and negated-atom class safety. The
|
|
540
|
+
* leading `[AnyTreeChild] extends [C]` arm is the recursion's base case.
|
|
620
541
|
*/
|
|
621
|
-
type CondOkBool<
|
|
542
|
+
type CondOkBool<Classes extends SchemaClasses, C> = [AnyTreeChild] extends [C]
|
|
622
543
|
? true
|
|
623
544
|
: C extends Cmp<infer Op, infer L, infer R, unknown>
|
|
624
545
|
? Op extends "eq" | "ne"
|
|
625
|
-
? EqOk<
|
|
546
|
+
? EqOk<Classes, L, R>
|
|
626
547
|
: Op extends "lt" | "le" | "gt" | "ge"
|
|
627
|
-
? [OrderSideOk<
|
|
548
|
+
? [OrderSideOk<L>, OrderSideOk<R>] extends [true, true]
|
|
628
549
|
? true
|
|
629
550
|
: false
|
|
630
551
|
: Op extends "pointIn"
|
|
631
|
-
? [IntervalSideOk<
|
|
552
|
+
? [IntervalSideOk<L>, PointSideOk<R>] extends [true, true]
|
|
632
553
|
? true
|
|
633
554
|
: false
|
|
634
555
|
: Op extends "allen"
|
|
635
|
-
? [IntervalSideOk<
|
|
556
|
+
? [IntervalSideOk<L>, IntervalSideOk<R>] extends [true, true]
|
|
636
557
|
? true
|
|
637
558
|
: false
|
|
638
559
|
: false
|
|
639
560
|
: C extends Tree<infer Ch extends readonly AnyTreeChild[]>
|
|
640
|
-
? false extends CondOkBool<
|
|
561
|
+
? false extends CondOkBool<Classes, Ch[number]>
|
|
641
562
|
? false
|
|
642
563
|
: true
|
|
643
564
|
: C extends NotAtom<infer R extends MatchOwner, infer B>
|
|
644
|
-
? NotOk<
|
|
565
|
+
? NotOk<Classes, MatchFields<R>, ClassRecordOf<Classes, R["name"]>, B>
|
|
645
566
|
: false
|
|
646
567
|
|
|
647
568
|
/** The validated `.where` argument (intersect with the inferred condition type). */
|
|
648
|
-
type CheckCond<
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
? { readonly [Q in P]: Infer<
|
|
656
|
-
:
|
|
657
|
-
|
|
658
|
-
: never
|
|
659
|
-
: never
|
|
569
|
+
type CheckCond<Classes extends SchemaClasses, C> = CondOkBool<Classes, C> extends true ? C : never
|
|
570
|
+
|
|
571
|
+
/** The `eq`/`ne` params contribution: the param typed by the left variable's own field. */
|
|
572
|
+
type EqParams<L, R> = L extends AnyVar
|
|
573
|
+
? R extends Param<infer P extends string>
|
|
574
|
+
? { readonly [Q in P]: Infer<L["field"]> }
|
|
575
|
+
: R extends SetParam<infer P extends string>
|
|
576
|
+
? { readonly [Q in P]: readonly Infer<L["field"]>[] }
|
|
577
|
+
: never
|
|
578
|
+
: never
|
|
660
579
|
|
|
661
580
|
/** An order side's params contribution (order params are always `bigint`). */
|
|
662
581
|
type OrderSideParams<T> = T extends Param<infer P extends string> ? { readonly [Q in P]: bigint } : never
|
|
@@ -670,14 +589,13 @@ type MaskParams<M> = M extends MaskParam<infer P extends string> ? { readonly [Q
|
|
|
670
589
|
/**
|
|
671
590
|
* One condition's params-object fragments (a union; the rule builder folds
|
|
672
591
|
* them into the inferred `Params` record) — every param typed by its use.
|
|
673
|
-
* The leading arm is the same base case as {@link CondOkBool}'s
|
|
674
|
-
* unresolved constraint contributes nothing.
|
|
592
|
+
* The leading arm is the same base case as {@link CondOkBool}'s.
|
|
675
593
|
*/
|
|
676
|
-
type CondParams<
|
|
594
|
+
type CondParams<C> = [AnyTreeChild] extends [C]
|
|
677
595
|
? never
|
|
678
596
|
: C extends Cmp<infer Op, infer L, infer R, infer M>
|
|
679
597
|
? Op extends "eq" | "ne"
|
|
680
|
-
? EqParams<
|
|
598
|
+
? EqParams<L, R>
|
|
681
599
|
: Op extends "lt" | "le" | "gt" | "ge"
|
|
682
600
|
? OrderSideParams<L> | OrderSideParams<R>
|
|
683
601
|
: Op extends "pointIn"
|
|
@@ -686,7 +604,7 @@ type CondParams<Env extends EnvShape, C> = [AnyTreeChild] extends [C]
|
|
|
686
604
|
? IntervalSideParams<L> | IntervalSideParams<R> | MaskParams<M>
|
|
687
605
|
: never
|
|
688
606
|
: C extends Tree<infer Ch extends readonly AnyTreeChild[]>
|
|
689
|
-
? CondParams<
|
|
607
|
+
? CondParams<Ch[number]>
|
|
690
608
|
: C extends NotAtom<infer R extends MatchOwner, infer B>
|
|
691
609
|
? BindParams<MatchFields<R>, B>
|
|
692
610
|
: never
|
|
@@ -695,7 +613,7 @@ type CondParams<Env extends EnvShape, C> = [AnyTreeChild] extends [C]
|
|
|
695
613
|
type BindParamsShape<F extends FieldsShape, B> = ShapeOf<BindParams<F, B>>
|
|
696
614
|
|
|
697
615
|
/** The flattened params record one condition contributes. */
|
|
698
|
-
type CondParamsShape<
|
|
616
|
+
type CondParamsShape<C> = ShapeOf<CondParams<C>>
|
|
699
617
|
|
|
700
618
|
export type {
|
|
701
619
|
AggData,
|
|
@@ -704,7 +622,6 @@ export type {
|
|
|
704
622
|
AnyNotAtom,
|
|
705
623
|
AnyTreeChild,
|
|
706
624
|
AtomData,
|
|
707
|
-
BindEnv,
|
|
708
625
|
BindingEntry,
|
|
709
626
|
BindingInput,
|
|
710
627
|
BindingTermData,
|
|
@@ -720,6 +637,8 @@ export type {
|
|
|
720
637
|
CondOkBool,
|
|
721
638
|
CondParams,
|
|
722
639
|
CondParamsShape,
|
|
640
|
+
FindColumn,
|
|
641
|
+
FindEntryData,
|
|
723
642
|
IntervalSide,
|
|
724
643
|
IntervalVarOk,
|
|
725
644
|
MaskData,
|
|
@@ -734,8 +653,7 @@ export type {
|
|
|
734
653
|
RecData,
|
|
735
654
|
RuleData,
|
|
736
655
|
RuleItem,
|
|
737
|
-
|
|
738
|
-
SelectEntryData,
|
|
656
|
+
SlotAt,
|
|
739
657
|
Tree,
|
|
740
658
|
TreeData
|
|
741
659
|
}
|