@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
package/src/relation.ts CHANGED
@@ -2,12 +2,14 @@
2
2
  * `relation()` — the ordinary-relation half of the theory's signature. A
3
3
  * relation value is a frozen plain object carrying its name, its ordered
4
4
  * field descriptors (declaration order = ordinal ids, the macro's law),
5
- * typed field references (`R.fields.holder`), and — since selections are
6
- * the relation's own vocabulary `where()`, which resolves a selection
7
- * into lowered bindings eagerly (handles verified against their roster at
8
- * construction). `Fact<>`/`InsertFact<>` are the inferred row object
9
- * types at BARE structural value types (no brands): fresh fields are
10
- * optional on insert input (omit-to-mint) and present on read
5
+ * and — since selections are the relation's own vocabulary — `where()`,
6
+ * which resolves a selection into lowered bindings eagerly (handles
7
+ * verified against their roster at construction). Fields are addressed by
8
+ * NAME everywhere — statements (`on(R, "holder")`), selections, and match
9
+ * records all spell the field's own name, checked by type
10
+ * (`FaceFields`/`MatchShape`). `Fact<>`/`InsertFact<>` are the inferred
11
+ * row object types at BARE structural value types (no brands): fresh
12
+ * fields are optional on insert input (omit-to-mint) and present on read
11
13
  * (resupply-to-preserve-identity), typed exactly.
12
14
  */
13
15
 
@@ -18,23 +20,6 @@ import { type LiteralSetSpec, type LiteralSpec, renderLiteral } from "#spec.ts"
18
20
  /** Flattens an intersection into one displayed object type (hover legibility). */
19
21
  type Flatten<T> = { [K in keyof T]: T[K] }
20
22
 
21
- /**
22
- * The one trusted seam of `relation()`: the reference record is built by
23
- * iterating the declared fields, and this guard verifies the checkable
24
- * facts — one reference per declared field, each carrying its own name —
25
- * before the record is admitted as the typed {@link FieldRefs} (the
26
- * macro-emission analog).
27
- */
28
- function refsComplete<RName extends string, Fields extends FieldsShape>(
29
- refs: Record<string, unknown>,
30
- fields: Fields
31
- ): refs is FieldRefs<RName, Fields> {
32
- return Object.keys(fields).every(function hasRef(fieldName) {
33
- const ref = refs[fieldName]
34
- return typeof ref === "object" && ref !== null && "field" in ref && ref.field === fieldName
35
- })
36
- }
37
-
38
23
  /**
39
24
  * Resolves one selection entry to its lowered literal set: a plain ARRAY
40
25
  * (detected by `Array.isArray` — no field's value type is an array;
@@ -117,22 +102,6 @@ function resolveSelection(
117
102
  /** The field block of a relation: field name to field descriptor. */
118
103
  type FieldsShape = Record<string, AnyField>
119
104
 
120
- /**
121
- * A typed field reference (`Account.fields.holder`) — the value statements,
122
- * selections, and queries address a field through. Purely positional
123
- * (relation name + field name); the field's descriptor is read off the
124
- * relation's schema type structurally.
125
- */
126
- interface FieldRef<Rel extends string, Name extends string> {
127
- readonly relation: Rel
128
- readonly field: Name
129
- }
130
-
131
- /** The typed field-reference record of a relation. */
132
- type FieldRefs<RName extends string, Fields extends FieldsShape> = {
133
- readonly [K in keyof Fields & string]: FieldRef<RName, K>
134
- }
135
-
136
105
  /** One declared field: name plus its descriptor, in declaration order. */
137
106
  interface RelationField {
138
107
  readonly name: string
@@ -178,7 +147,6 @@ interface Selected<Name extends string, Fields extends FieldsShape> {
178
147
  interface Relation<Name extends string, Fields extends FieldsShape> {
179
148
  readonly name: Name
180
149
  readonly data: RelationData
181
- readonly fields: FieldRefs<Name, Fields>
182
150
  where(selection: SelectionInput<Fields>): Selected<Name, Fields>
183
151
  }
184
152
 
@@ -236,14 +204,6 @@ function relation<const Name extends string, Fields extends FieldsShape>(
236
204
  ordered.push(Object.freeze({ name: fieldName, field }))
237
205
  }
238
206
  const data: RelationData = Object.freeze({ name, fields: Object.freeze(ordered) })
239
- const refs: Record<string, unknown> = {}
240
- for (const declared of ordered) {
241
- refs[declared.name] = Object.freeze({ relation: name, field: declared.name })
242
- }
243
- Object.freeze(refs)
244
- if (!refsComplete<Name, Fields>(refs, fields)) {
245
- throw errors.new(`relation ${name}: field-reference construction incomplete`)
246
- }
247
207
  const holder: { value: Relation<Name, Fields> | undefined } = { value: undefined }
248
208
  function where(selection: SelectionInput<Fields>): Selected<Name, Fields> {
249
209
  const owner = holder.value
@@ -255,7 +215,7 @@ function relation<const Name extends string, Fields extends FieldsShape>(
255
215
  selection: resolveSelection(name, ordered, Object.entries(selection))
256
216
  })
257
217
  }
258
- const value: Relation<Name, Fields> = Object.freeze({ name, data, fields: refs, where })
218
+ const value: Relation<Name, Fields> = Object.freeze({ name, data, where })
259
219
  holder.value = value
260
220
  return value
261
221
  }
@@ -264,8 +224,6 @@ export type {
264
224
  AnyRelation,
265
225
  AnySelected,
266
226
  Fact,
267
- FieldRef,
268
- FieldRefs,
269
227
  FieldsShape,
270
228
  FreshKeys,
271
229
  InsertFact,
package/src/schema.ts CHANGED
@@ -11,8 +11,9 @@
11
11
 
12
12
  import * as errors from "@superbuilders/errors"
13
13
  import type { AnyClosed } from "#closed.ts"
14
+ import { isClosedMember, sealedFieldOf } from "#closed.ts"
14
15
  import type { FaceData } from "#face.ts"
15
- import { type AnyField, assertDeclarationOrderKey, type ClosedRoster } from "#fields.ts"
16
+ import { assertDeclarationOrderKey, rosterOf } from "#fields.ts"
16
17
  import { type ClassesOf, classesComplete, computeClasses, type LawfulStatements, type SchemaClasses } from "#law.ts"
17
18
  import type { AnyRelation } from "#relation.ts"
18
19
  import type { LiteralSetSpec, LiteralSpec } from "#spec.ts"
@@ -34,7 +35,7 @@ function collectImplied(name: string, relations: SchemaRelations): Set<string> {
34
35
  `schema ${name}: record key ${recordKey} holds relation ${member.name} — the key must equal the relation's declared name`
35
36
  )
36
37
  }
37
- if ("handles" in member.data) {
38
+ if (isClosedMember(member)) {
38
39
  implied.add(`${member.name}(id) -> ${member.name}`)
39
40
  continue
40
41
  }
@@ -75,33 +76,6 @@ function verifyMembership(name: string, relations: SchemaRelations, statement: S
75
76
  }
76
77
  }
77
78
 
78
- /** Finds a face's field descriptor by name, across both relation kinds. */
79
- function faceField(face: FaceData, fieldName: string): AnyField | undefined {
80
- const data = face.owner.data
81
- if ("handles" in data) {
82
- const column = data.columns.find(function byName(candidate) {
83
- return candidate.name === fieldName
84
- })
85
- return column?.field
86
- }
87
- const declared = data.fields.find(function byName(candidate) {
88
- return candidate.name === fieldName
89
- })
90
- return declared?.field
91
- }
92
-
93
- /**
94
- * The roster a field resolves handles through: present exactly on a closed
95
- * reference descriptor (the structural `closed` property — S1's
96
- * `ClosedIdField`), absent on every other field kind.
97
- */
98
- function rosterOf(field: AnyField | undefined): ClosedRoster | undefined {
99
- if (field !== undefined && "closed" in field) {
100
- return field.closed
101
- }
102
- return undefined
103
- }
104
-
105
79
  /** Flattens one binding's literal set into its literals. */
106
80
  function bindingLiterals(set: LiteralSetSpec): readonly LiteralSpec[] {
107
81
  if (set.kind === "one") {
@@ -122,7 +96,7 @@ function verifyBindingHandles(
122
96
  binding: { readonly field: string; readonly set: LiteralSetSpec },
123
97
  rendered: string
124
98
  ): void {
125
- const roster = rosterOf(faceField(face, binding.field))
99
+ const roster = rosterOf(sealedFieldOf(face.owner, binding.field))
126
100
  for (const literal of bindingLiterals(binding.set)) {
127
101
  if (literal.kind !== "handle") {
128
102
  continue
@@ -177,7 +151,7 @@ function closedTargetOf(statements: readonly Statement[], owner: string, field:
177
151
  source.projection[0] === field &&
178
152
  target.projection.length === 1 &&
179
153
  target.projection[0] === "id" &&
180
- "handles" in target.owner.data
154
+ isClosedMember(target.owner)
181
155
  ) {
182
156
  return target.owner.name
183
157
  }
@@ -227,11 +201,11 @@ function verifyClosedReferenceBinding(
227
201
  if (!spellsHandle) {
228
202
  return
229
203
  }
230
- const roster = rosterOf(faceField(face, binding.field))
204
+ const roster = rosterOf(sealedFieldOf(face.owner, binding.field))
231
205
  if (roster === undefined) {
232
206
  return
233
207
  }
234
- if ("handles" in face.owner.data && binding.field === "id") {
208
+ if (isClosedMember(face.owner) && binding.field === "id") {
235
209
  return
236
210
  }
237
211
  const resolved = closedTargetOf(statements, face.owner.name, binding.field)
package/src/statements.ts CHANGED
@@ -12,8 +12,11 @@
12
12
  * schema type (the minimal kernel — descriptors are pure structure) and
13
13
  * constrained positionwise equal, so a u64 face against a str face, a
14
14
  * bytes width mismatch, an interval element mismatch, or a bare column
15
- * against a closed reference is a compile error. The ROSTER slot carries a
16
- * construction-time runtime twin here ({@link assertRosterAgreement}
15
+ * against a closed reference is a compile error. TWO of those walls carry
16
+ * construction-time runtime twins here for untyped callers: ARITY
17
+ * ({@link assertArityAgreement} — cleanup-0.5.0 ruling 9: an
18
+ * arity-mismatched pairing fails at the statement, never by silent
19
+ * truncation) and the ROSTER slot ({@link assertRosterAgreement} —
17
20
  * roster IDENTITY, positionwise: a closed vocabulary's referencing column
18
21
  * is spelled with the vocabulary's own id descriptor, the ONE spelling, so
19
22
  * a plain u64 column can never alias a vocabulary through a declared law
@@ -32,9 +35,10 @@
32
35
  */
33
36
 
34
37
  import * as errors from "@superbuilders/errors"
38
+ import { isClosedMember, sealedFieldOf } from "#closed.ts"
35
39
  import type { Count } from "#count.ts"
36
40
  import { type AnyFace, type FaceData, renderFace, type SameArity, type SameShapes } from "#face.ts"
37
- import type { AnyField, ClosedRoster } from "#fields.ts"
41
+ import { type ClosedRoster, rosterOf } from "#fields.ts"
38
42
  import type { AnyRelation, RelationFields } from "#relation.ts"
39
43
  import { renderWindow, type WindowSpec } from "#spec.ts"
40
44
 
@@ -105,43 +109,28 @@ interface KeyStatement<R extends AnyRelation, Projection extends readonly string
105
109
  readonly data: KeyData<R, Projection>
106
110
  }
107
111
 
108
- /**
109
- * The field descriptor one face position projects: an ordinary relation's
110
- * declared field; a closed relation's SEALED shape — the synthetic `id`
111
- * (the value's own roster-carrying descriptor, by identity) or a declared
112
- * payload column. `undefined` when the name is foreign (the type tier makes
113
- * that unwritable; the engine re-judges projections at `Db.create`).
114
- */
115
- function projectedFieldOf(face: FaceData, fieldName: string): AnyField | undefined {
116
- const owner = face.owner
117
- if ("axioms" in owner) {
118
- if (fieldName === "id") {
119
- return owner.id
120
- }
121
- const column = owner.data.columns.find(function byName(candidate) {
122
- return candidate.name === fieldName
123
- })
124
- return column?.field
125
- }
126
- const declared = owner.data.fields.find(function byName(candidate) {
127
- return candidate.name === fieldName
128
- })
129
- return declared?.field
130
- }
131
-
132
- /** The roster a descriptor carries: present exactly on a closed reference, absent on every other kind. */
133
- function rosterOfField(field: AnyField | undefined): ClosedRoster | undefined {
134
- if (field !== undefined && "closed" in field) {
135
- return field.closed
136
- }
137
- return undefined
138
- }
139
-
140
112
  /** Renders one face position's closedness for the roster-agreement diagnostics. */
141
113
  function renderRosterSide(roster: ClosedRoster | undefined): string {
142
114
  return roster === undefined ? "a bare column" : `a ${roster.name} reference`
143
115
  }
144
116
 
117
+ /**
118
+ * The runtime twin of {@link SameArity} (cleanup-0.5.0 ruling 9): the two
119
+ * faces must project equally many fields, judged at CONSTRUCTION for
120
+ * untyped callers too — without it an arity-mismatched containment
121
+ * silently truncates to the shorter projection (this module's positionwise
122
+ * walk and `law.ts`'s `unionSlot` both skip unpaired positions) until
123
+ * `Db.create`'s colder engine refusal. The error carries the two faces'
124
+ * own facts: names, arities, and the rendered statement.
125
+ */
126
+ function assertArityAgreement(source: FaceData, target: FaceData, statement: Statement): void {
127
+ if (source.projection.length !== target.projection.length) {
128
+ throw errors.new(
129
+ `${source.owner.name}(${source.projection.join(", ")}) and ${target.owner.name}(${target.projection.join(", ")}) project ${source.projection.length} vs ${target.projection.length} fields — positional pairing requires both faces to project equally many — ${renderStatement(statement)}`
130
+ )
131
+ }
132
+ }
133
+
145
134
  /**
146
135
  * The runtime twin of {@link SameShapes}'s roster slot: the two faces'
147
136
  * projected descriptors must agree POSITIONWISE on closedness — the same
@@ -154,6 +143,9 @@ function renderRosterSide(roster: ClosedRoster | undefined): string {
154
143
  * vocabulary's own descriptor (`Kind.id`) is the ONE spelling of a closed
155
144
  * reference at this surface (the canonical-utterance law); the engine
156
145
  * cannot backstop this one — the wire carries plain u64s, no rosters.
146
+ * Arity agreement ({@link assertArityAgreement}) runs first, so the
147
+ * positionwise walk here never sees an unpaired position from a well-typed
148
+ * OR an untyped caller.
157
149
  */
158
150
  function assertRosterAgreement(source: FaceData, target: FaceData, statement: Statement): void {
159
151
  source.projection.forEach(function agreeAt(fieldName, position) {
@@ -161,8 +153,8 @@ function assertRosterAgreement(source: FaceData, target: FaceData, statement: St
161
153
  if (targetField === undefined) {
162
154
  return
163
155
  }
164
- const sourceRoster = rosterOfField(projectedFieldOf(source, fieldName))
165
- const targetRoster = rosterOfField(projectedFieldOf(target, targetField))
156
+ const sourceRoster = rosterOf(sealedFieldOf(source.owner, fieldName))
157
+ const targetRoster = rosterOf(sealedFieldOf(target.owner, targetField))
166
158
  if (sourceRoster !== targetRoster) {
167
159
  throw errors.new(
168
160
  `${source.owner.name}.${fieldName} is ${renderRosterSide(sourceRoster)} but ${target.owner.name}.${targetField} is ${renderRosterSide(targetRoster)} — closedness rides the descriptor: a closed reference is spelled with the vocabulary's own id descriptor (one meaning, one spelling), so faces pair closed-with-closed through one roster or bare-with-bare, never across — ${renderStatement(statement)}`
@@ -186,7 +178,7 @@ function key<
186
178
  R extends AnyRelation,
187
179
  const Projection extends readonly [keyof RelationFields<R> & string, ...(keyof RelationFields<R> & string)[]]
188
180
  >(relation: R, fields: Projection): KeyStatement<R, Projection> {
189
- if (!("fields" in relation.data)) {
181
+ if (isClosedMember(relation)) {
190
182
  throw errors.new(
191
183
  `key(${relation.name}, ...): closedness already materializes ${relation.name}(id) -> ${relation.name} — an explicit key on a closed relation is rejected as a duplicate`
192
184
  )
@@ -219,6 +211,7 @@ function contained<A extends AnyFace, B extends AnyFace>(
219
211
  bidirectional: false
220
212
  })
221
213
  const statement = Object.freeze({ data })
214
+ assertArityAgreement(data.source, data.target, statement)
222
215
  assertRosterAgreement(data.source, data.target, statement)
223
216
  return statement
224
217
  }
@@ -243,6 +236,7 @@ function mirrors<A extends AnyFace, B extends AnyFace>(
243
236
  bidirectional: true
244
237
  })
245
238
  const statement = Object.freeze({ data })
239
+ assertArityAgreement(data.source, data.target, statement)
246
240
  assertRosterAgreement(data.source, data.target, statement)
247
241
  return statement
248
242
  }
@@ -269,6 +263,7 @@ function window<B extends AnyFace, A extends AnyFace>(
269
263
  source: source.data
270
264
  })
271
265
  const statement = Object.freeze({ data })
266
+ assertArityAgreement(data.source, data.target, statement)
272
267
  assertRosterAgreement(data.source, data.target, statement)
273
268
  return statement
274
269
  }
@@ -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,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