@bjornpagen/bumbledb 0.1.0 → 0.2.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 +1394 -0
- package/README.md +4 -0
- package/dist/closed.d.ts +60 -37
- package/dist/closed.d.ts.map +1 -1
- package/dist/closed.js +159 -40
- package/dist/closed.js.map +1 -1
- package/dist/count.d.ts +64 -17
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +37 -14
- package/dist/count.js.map +1 -1
- package/dist/db.d.ts +15 -7
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +13 -5
- package/dist/db.js.map +1 -1
- package/dist/exhume.d.ts +12 -11
- package/dist/exhume.d.ts.map +1 -1
- package/dist/exhume.js +7 -6
- package/dist/exhume.js.map +1 -1
- package/dist/face.d.ts +79 -19
- package/dist/face.d.ts.map +1 -1
- package/dist/face.js +14 -15
- package/dist/face.js.map +1 -1
- package/dist/fields.d.ts +158 -129
- package/dist/fields.d.ts.map +1 -1
- package/dist/fields.js +80 -81
- package/dist/fields.js.map +1 -1
- package/dist/index.d.ts +21 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -12
- package/dist/index.js.map +1 -1
- package/dist/lower.d.ts +6 -3
- package/dist/lower.d.ts.map +1 -1
- package/dist/lower.js +48 -17
- package/dist/lower.js.map +1 -1
- package/dist/marshal.d.ts +36 -21
- package/dist/marshal.d.ts.map +1 -1
- package/dist/marshal.js +41 -25
- package/dist/marshal.js.map +1 -1
- package/dist/native.js +2 -2
- package/dist/query/atom.d.ts +332 -174
- package/dist/query/atom.d.ts.map +1 -1
- package/dist/query/atom.js +72 -172
- package/dist/query/atom.js.map +1 -1
- package/dist/query/lower.d.ts +295 -107
- package/dist/query/lower.d.ts.map +1 -1
- package/dist/query/lower.js +860 -388
- package/dist/query/lower.js.map +1 -1
- package/dist/query/predicate.d.ts +69 -88
- package/dist/query/predicate.d.ts.map +1 -1
- package/dist/query/predicate.js +118 -72
- package/dist/query/predicate.js.map +1 -1
- package/dist/query/run.d.ts +19 -16
- package/dist/query/run.d.ts.map +1 -1
- package/dist/query/run.js +31 -24
- package/dist/query/run.js.map +1 -1
- package/dist/query/scope.d.ts +139 -123
- package/dist/query/scope.d.ts.map +1 -1
- package/dist/query/scope.js +71 -115
- package/dist/query/scope.js.map +1 -1
- package/dist/query/select.d.ts +102 -80
- package/dist/query/select.d.ts.map +1 -1
- package/dist/query/select.js +39 -34
- package/dist/query/select.js.map +1 -1
- package/dist/relation.d.ts +33 -36
- package/dist/relation.d.ts.map +1 -1
- package/dist/relation.js +15 -16
- package/dist/relation.js.map +1 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +19 -8
- package/dist/schema.js.map +1 -1
- package/dist/spec.d.ts +6 -5
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js.map +1 -1
- package/dist/statements.d.ts +56 -31
- package/dist/statements.d.ts.map +1 -1
- package/dist/statements.js +38 -13
- package/dist/statements.js.map +1 -1
- package/package.json +4 -3
- package/src/closed.ts +271 -93
- package/src/count.ts +112 -18
- package/src/db.ts +22 -12
- package/src/exhume.ts +12 -11
- package/src/face.ts +114 -22
- package/src/fields.ts +261 -217
- package/src/index.ts +50 -59
- package/src/lower.ts +62 -20
- package/src/marshal.ts +48 -30
- package/src/native.ts +2 -2
- package/src/query/atom.ts +480 -376
- package/src/query/lower.ts +1341 -542
- package/src/query/predicate.ts +198 -161
- package/src/query/run.ts +35 -25
- package/src/query/scope.ts +188 -218
- package/src/query/select.ts +168 -93
- package/src/relation.ts +38 -44
- package/src/schema.ts +22 -12
- package/src/spec.ts +6 -5
- package/src/statements.ts +69 -26
- package/dist/brand.d.ts +0 -59
- package/dist/brand.d.ts.map +0 -1
- package/dist/brand.js +0 -47
- package/dist/brand.js.map +0 -1
- package/src/brand.ts +0 -82
package/dist/query/scope.js
CHANGED
|
@@ -1,134 +1,90 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Query scope terms
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
2
|
+
* Query scope terms, STRUCTURAL edition: string-named variables and
|
|
3
|
+
* parameters as plain frozen values. A `Var` is a NAME — it is typed by the
|
|
4
|
+
* field it first binds (structurally, off the schema type, through the rule
|
|
5
|
+
* builder's environment), reuse of the name within one rule IS the join,
|
|
6
|
+
* and a domain-mismatched reuse is a compile error (the structural analog
|
|
7
|
+
* of the old brand-equal join — now domain-equal, no value brands
|
|
8
|
+
* anywhere). Params are query-global by name and typed BY USE: the field
|
|
9
|
+
* position or comparison sibling that anchors a param types it, the
|
|
10
|
+
* query's inferred `Params` object is exactly the params the rules use,
|
|
11
|
+
* and a param value that no rule uses simply never registers — the query
|
|
12
|
+
* executes under its own inferred type (the bug-hunt law). This module
|
|
13
|
+
* also owns the environment/typing utilities the whole surface shares:
|
|
14
|
+
* the env shape (var name → field descriptor), the domain-equality
|
|
15
|
+
* judgment, and the record-folding helpers `Params` and `Row` inference
|
|
16
|
+
* ride.
|
|
14
17
|
*/
|
|
15
|
-
import * as errors from "@superbuilders/errors";
|
|
16
|
-
import { phantom } from "#brand.ts";
|
|
17
18
|
/**
|
|
18
19
|
* The runtime discriminant of query term values. Host literals (bigints,
|
|
19
|
-
* strings, interval objects
|
|
20
|
-
*
|
|
20
|
+
* strings, interval objects) never carry it, so "is this position a term
|
|
21
|
+
* or a literal" is one property probe, never a guess.
|
|
21
22
|
*/
|
|
22
23
|
const term = Symbol("bumbledb.query.term");
|
|
23
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* The carrier of a value's INFERRED types (a rule's row/params, a query's
|
|
26
|
+
* row/params, a rec's params). The property is never present at runtime —
|
|
27
|
+
* it exists so inference rides plain values without any brand on any
|
|
28
|
+
* field value.
|
|
29
|
+
*/
|
|
30
|
+
const inferred = Symbol("bumbledb.query.inferred");
|
|
31
|
+
/** Narrows an unknown position value to a scope term (vs a host literal). */
|
|
24
32
|
function isTerm(value) {
|
|
25
33
|
return typeof value === "object" && value !== null && term in value;
|
|
26
34
|
}
|
|
27
|
-
/**
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
/** Builds one variable term. */
|
|
36
|
+
function makeVar(name) {
|
|
37
|
+
const value = { [term]: "var", name };
|
|
38
|
+
return Object.freeze(value);
|
|
39
|
+
}
|
|
40
|
+
/** Builds one scalar-parameter term. */
|
|
41
|
+
function makeParam(name) {
|
|
42
|
+
const value = { [term]: "param", name };
|
|
43
|
+
return Object.freeze(value);
|
|
44
|
+
}
|
|
45
|
+
/** Builds one set-parameter term. */
|
|
46
|
+
function makeSetParam(name) {
|
|
47
|
+
const value = { [term]: "setParam", name };
|
|
48
|
+
return Object.freeze(value);
|
|
49
|
+
}
|
|
50
|
+
/** Builds one Allen-mask-parameter term. */
|
|
51
|
+
function makeMaskParam(name) {
|
|
52
|
+
const value = { [term]: "maskParam", name };
|
|
53
|
+
return Object.freeze(value);
|
|
54
|
+
}
|
|
55
|
+
/** Builds one measure term over an interval-typed variable's name. */
|
|
56
|
+
function makeDuration(name) {
|
|
57
|
+
const value = { [term]: "duration", name };
|
|
58
|
+
return Object.freeze(value);
|
|
36
59
|
}
|
|
37
60
|
/**
|
|
38
|
-
* The
|
|
39
|
-
*
|
|
61
|
+
* The runtime twin of {@link JoinOk}: two field descriptors join iff kind,
|
|
62
|
+
* domain label, width label, and interval element all agree — the same
|
|
63
|
+
* structural comparison the type tier makes, judged on the descriptor
|
|
64
|
+
* VALUES (S1 descriptors are honest at runtime). The rule builders throw
|
|
65
|
+
* through this on a domain-unequal variable reuse, so the wall holds for
|
|
66
|
+
* untyped callers too, not only where the compiler can see.
|
|
40
67
|
*/
|
|
41
|
-
function
|
|
42
|
-
|
|
68
|
+
function fieldJoins(a, b) {
|
|
69
|
+
const widthA = "width" in a ? a.width : undefined;
|
|
70
|
+
const widthB = "width" in b ? b.width : undefined;
|
|
71
|
+
const elementA = "element" in a ? a.element : undefined;
|
|
72
|
+
const elementB = "element" in b ? b.element : undefined;
|
|
73
|
+
return a.kind === b.kind && a.domain === b.domain && widthA === widthB && elementA === elementB;
|
|
43
74
|
}
|
|
44
75
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* level already carries the brand; this recovers the structural type the
|
|
48
|
-
* lowering and the param marshaler direct by).
|
|
76
|
+
* Renders one field descriptor for join-mismatch diagnostics — the schema
|
|
77
|
+
* grammar's own spelling (`u64 as HolderId`, `interval<i64, 7> as Window`).
|
|
49
78
|
*/
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
if (isClosedMember(member)) {
|
|
56
|
-
if (fieldName === "id") {
|
|
57
|
-
return member.id.data;
|
|
58
|
-
}
|
|
59
|
-
const column = member.data.columns.find(function byName(candidate) {
|
|
60
|
-
return candidate.name === fieldName;
|
|
61
|
-
});
|
|
62
|
-
if (column === undefined) {
|
|
63
|
-
throw errors.new(`closed relation ${relationName} has no column ${fieldName}`);
|
|
64
|
-
}
|
|
65
|
-
return column.field;
|
|
79
|
+
function renderFieldKind(field) {
|
|
80
|
+
let base = field.kind;
|
|
81
|
+
if (field.kind === "bytes") {
|
|
82
|
+
base = `bytes<${field.width}>`;
|
|
66
83
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
});
|
|
70
|
-
if (declared === undefined) {
|
|
71
|
-
throw errors.new(`relation ${relationName} has no field ${fieldName}`);
|
|
84
|
+
if (field.kind === "interval") {
|
|
85
|
+
base = field.width === undefined ? `interval<${field.element}>` : `interval<${field.element}, ${field.width}>`;
|
|
72
86
|
}
|
|
73
|
-
return
|
|
74
|
-
}
|
|
75
|
-
/** Declares one variable in the scope (the `$.var` implementation). */
|
|
76
|
-
function scopeVar(registry, ref) {
|
|
77
|
-
const value = Object.freeze({
|
|
78
|
-
[term]: "var",
|
|
79
|
-
relation: ref.relation,
|
|
80
|
-
field: ref.field,
|
|
81
|
-
data: resolveFieldData(registry.theory, ref.relation, ref.field)
|
|
82
|
-
});
|
|
83
|
-
registry.vars.add(value);
|
|
84
|
-
return value;
|
|
85
|
-
}
|
|
86
|
-
/** Rejects a second parameter under an already-taken name. */
|
|
87
|
-
function assertFreshParamName(registry, name) {
|
|
88
|
-
const taken = registry.params.some(function byName(entry) {
|
|
89
|
-
return entry.name === name;
|
|
90
|
-
});
|
|
91
|
-
if (taken) {
|
|
92
|
-
throw errors.new(`query scope already declares a param named ${name} — param names key the execute params object, one declaration each`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/** Declares one scalar parameter (the `$.param` implementation). */
|
|
96
|
-
function scopeParam(registry, name, ref) {
|
|
97
|
-
assertFreshParamName(registry, name);
|
|
98
|
-
const data = resolveFieldData(registry.theory, ref.relation, ref.field);
|
|
99
|
-
const value = Object.freeze({
|
|
100
|
-
[term]: "param",
|
|
101
|
-
name,
|
|
102
|
-
relation: ref.relation,
|
|
103
|
-
field: ref.field,
|
|
104
|
-
data
|
|
105
|
-
});
|
|
106
|
-
registry.paramIndex.set(value, registry.params.length);
|
|
107
|
-
registry.params.push(Object.freeze({ name, shape: "value", data }));
|
|
108
|
-
return value;
|
|
109
|
-
}
|
|
110
|
-
/** Declares one set parameter (the `$.paramSet` implementation). */
|
|
111
|
-
function scopeParamSet(registry, name, ref) {
|
|
112
|
-
assertFreshParamName(registry, name);
|
|
113
|
-
const data = resolveFieldData(registry.theory, ref.relation, ref.field);
|
|
114
|
-
const value = Object.freeze({
|
|
115
|
-
[term]: "paramSet",
|
|
116
|
-
name,
|
|
117
|
-
relation: ref.relation,
|
|
118
|
-
field: ref.field,
|
|
119
|
-
data
|
|
120
|
-
});
|
|
121
|
-
registry.paramIndex.set(value, registry.params.length);
|
|
122
|
-
registry.params.push(Object.freeze({ name, shape: "set", data }));
|
|
123
|
-
return value;
|
|
124
|
-
}
|
|
125
|
-
/** Declares one Allen-mask parameter (the `$.allenParam` implementation). */
|
|
126
|
-
function scopeAllenParam(registry, name) {
|
|
127
|
-
assertFreshParamName(registry, name);
|
|
128
|
-
const value = Object.freeze({ [term]: "maskParam", name });
|
|
129
|
-
registry.paramIndex.set(value, registry.params.length);
|
|
130
|
-
registry.params.push(Object.freeze({ name, shape: "mask", data: undefined }));
|
|
131
|
-
return value;
|
|
87
|
+
return field.domain === undefined ? base : `${base} as ${field.domain}`;
|
|
132
88
|
}
|
|
133
|
-
export {
|
|
89
|
+
export { fieldJoins, inferred, isTerm, makeDuration, makeMaskParam, makeParam, makeSetParam, makeVar, renderFieldKind, term };
|
|
134
90
|
//# sourceMappingURL=scope.js.map
|
package/dist/query/scope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scope.js","sourceRoot":"","sources":["../../src/query/scope.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"scope.js","sourceRoot":"","sources":["../../src/query/scope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH;;;;GAIG;AACH,MAAM,IAAI,GAAkB,MAAM,CAAC,qBAAqB,CAAC,CAAA;AAEzD;;;;;GAKG;AACH,MAAM,QAAQ,GAAkB,MAAM,CAAC,yBAAyB,CAAC,CAAA;AAgEjE,6EAA6E;AAC7E,SAAS,MAAM,CAAC,KAAc;IAC7B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,IAAI,IAAI,KAAK,CAAA;AACpE,CAAC;AAED,gCAAgC;AAChC,SAAS,OAAO,CAA4B,IAAU;IACrD,MAAM,KAAK,GAAc,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;IAChD,OAAO,MAAM,CAAC,MAAM,C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|
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
|
|
10
12
|
* implicit: the non-aggregate select entries are the group key; over empty
|
|
11
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).
|
|
12
17
|
*/
|
|
13
|
-
import type {
|
|
14
|
-
import {
|
|
15
|
-
import type { Duration } from "#query/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/** The three folds the measure admits (and the plain-variable folds). */
|
|
19
|
-
type FoldOp = "sum" | "min" | "max";
|
|
20
|
-
/** One aggregate's runtime description. */
|
|
21
|
-
type AggregateData = {
|
|
22
|
-
readonly op: "count";
|
|
23
|
-
} | {
|
|
24
|
-
readonly op: "countDistinct";
|
|
25
|
-
readonly over: AnyVar;
|
|
26
|
-
} | {
|
|
27
|
-
readonly op: "fold";
|
|
28
|
-
readonly fold: FoldOp;
|
|
29
|
-
readonly over: AnyVar | Duration;
|
|
30
|
-
} | {
|
|
31
|
-
readonly op: "arg";
|
|
32
|
-
readonly direction: "argMax" | "argMin";
|
|
33
|
-
readonly key: AnyVar;
|
|
34
|
-
readonly over: AnyVar;
|
|
35
|
-
} | {
|
|
36
|
-
readonly op: "pack";
|
|
37
|
-
readonly over: AnyVar;
|
|
38
|
-
};
|
|
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";
|
|
39
23
|
/**
|
|
40
|
-
* One aggregate select
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
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`.
|
|
44
28
|
*/
|
|
45
|
-
interface
|
|
46
|
-
readonly
|
|
47
|
-
readonly
|
|
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;
|
|
48
33
|
}
|
|
49
|
-
/**
|
|
50
|
-
type
|
|
51
|
-
/**
|
|
52
|
-
type
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
} ? Exclude<R, undefined> : T extends {
|
|
58
|
-
readonly measure: AnyVar;
|
|
59
|
-
} ? bigint : T extends {
|
|
60
|
-
readonly [term]: "var";
|
|
61
|
-
readonly [phantom]?: infer V;
|
|
62
|
-
} ? Exclude<V, undefined> : never;
|
|
63
|
-
/** The inferred answer-row object type of a select record. */
|
|
64
|
-
type RowOf<Sel extends SelectShape> = Flatten<{
|
|
65
|
-
[K in keyof Sel]: SelectValue<Sel[K]>;
|
|
66
|
-
}>;
|
|
67
|
-
/** Nullary count: |the group's set of distinct full bindings|, `bigint`. */
|
|
68
|
-
declare function count(): Aggregate<bigint>;
|
|
69
|
-
/** |the distinct values of `over` across the group|, `bigint`; legal over every type. */
|
|
70
|
-
declare function countDistinct<V>(over: Var<V>): Aggregate<bigint>;
|
|
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>;
|
|
71
42
|
/**
|
|
72
|
-
* Exact checked sum over
|
|
73
|
-
* finalize range check
|
|
74
|
-
* never a wrap
|
|
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"))`).
|
|
75
47
|
*/
|
|
76
|
-
declare function sum<
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
declare function
|
|
81
|
-
/** Maximum over a u64/i64 variable or the measure (orderable types only). */
|
|
82
|
-
declare function max<V extends bigint>(over: Var<V>): Aggregate<V>;
|
|
83
|
-
declare function max(over: Duration): Aggregate<bigint>;
|
|
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>;
|
|
84
53
|
/**
|
|
85
54
|
* Arg-restriction toward the maximum of `key` (`ir::AggOp::ArgMax`): the
|
|
86
55
|
* group's binding set is restricted to the bindings attaining the extreme
|
|
@@ -89,9 +58,9 @@ declare function max(over: Duration): Aggregate<bigint>;
|
|
|
89
58
|
* direction; Arg and fold aggregates never mix (both the engine's typed
|
|
90
59
|
* rules).
|
|
91
60
|
*/
|
|
92
|
-
declare function
|
|
93
|
-
/** Arg-restriction toward the minimum of `key`; rules as {@link
|
|
94
|
-
declare function
|
|
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>;
|
|
95
64
|
/**
|
|
96
65
|
* The coalescing fold (Snodgrass coalesce, `ir::AggOp::Pack`): per group,
|
|
97
66
|
* the maximal disjoint half-open segments of the union of the group's
|
|
@@ -100,7 +69,60 @@ declare function argmin<K extends bigint, V>(key: Var<K>, value: Var<V>): Aggreg
|
|
|
100
69
|
* input's element type. At most one `pack` per select, never beside a
|
|
101
70
|
* fold or an Arg entry (the engine's typed rules).
|
|
102
71
|
*/
|
|
103
|
-
declare function pack<
|
|
104
|
-
|
|
105
|
-
|
|
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]>;
|
|
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]>;
|
|
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]>;
|
|
111
|
+
} : E extends Agg<"pack", infer O extends string> ? {
|
|
112
|
+
readonly [K in O]: Infer<Env[O & keyof Env]>;
|
|
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]>;
|
|
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 };
|
|
106
128
|
//# sourceMappingURL=select.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/query/select.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/query/select.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE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@
|
|
|
1
1
|
/**
|
|
2
|
-
* Select entries and aggregates
|
|
3
|
-
* the IR's aggregate roster exactly
|
|
4
|
-
* `
|
|
5
|
-
* `
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* maximal segment), the result
|
|
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
|
|
10
12
|
* implicit: the non-aggregate select entries are the group key; over empty
|
|
11
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).
|
|
12
17
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
/** Builds one aggregate value. */
|
|
19
|
+
function aggregate(op, over, key) {
|
|
20
|
+
return Object.freeze({ agg: op, over, key });
|
|
21
|
+
}
|
|
22
|
+
/** Nullary count: |the group's set of distinct full bindings|, `bigint`; the answer column is named `count`. */
|
|
16
23
|
function count() {
|
|
17
|
-
return
|
|
24
|
+
return aggregate("count", undefined, undefined);
|
|
18
25
|
}
|
|
19
|
-
/** |the distinct values of
|
|
26
|
+
/** |the distinct values of the named variable across the group|, `bigint`; legal over every type. */
|
|
20
27
|
function countDistinct(over) {
|
|
21
|
-
return
|
|
28
|
+
return aggregate("countDistinct", over, undefined);
|
|
22
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Exact checked sum over an orderable (u64/i64) variable — wide
|
|
32
|
+
* accumulator, one finalize range check; overflow is the engine's typed
|
|
33
|
+
* runtime error, never a wrap — or over the measure
|
|
34
|
+
* (`r.sum(r.duration("w"))`).
|
|
35
|
+
*/
|
|
23
36
|
function sum(over) {
|
|
24
|
-
return
|
|
25
|
-
aggregate: Object.freeze({ op: "fold", fold: "sum", over })
|
|
26
|
-
});
|
|
37
|
+
return aggregate("sum", over, undefined);
|
|
27
38
|
}
|
|
39
|
+
/** Minimum over an orderable variable or the measure (orderable types only). */
|
|
28
40
|
function min(over) {
|
|
29
|
-
return
|
|
30
|
-
aggregate: Object.freeze({ op: "fold", fold: "min", over })
|
|
31
|
-
});
|
|
41
|
+
return aggregate("min", over, undefined);
|
|
32
42
|
}
|
|
43
|
+
/** Maximum over an orderable variable or the measure (orderable types only). */
|
|
33
44
|
function max(over) {
|
|
34
|
-
return
|
|
35
|
-
aggregate: Object.freeze({ op: "fold", fold: "max", over })
|
|
36
|
-
});
|
|
45
|
+
return aggregate("max", over, undefined);
|
|
37
46
|
}
|
|
38
47
|
/**
|
|
39
48
|
* Arg-restriction toward the maximum of `key` (`ir::AggOp::ArgMax`): the
|
|
@@ -43,16 +52,12 @@ function max(over) {
|
|
|
43
52
|
* direction; Arg and fold aggregates never mix (both the engine's typed
|
|
44
53
|
* rules).
|
|
45
54
|
*/
|
|
46
|
-
function
|
|
47
|
-
return
|
|
48
|
-
aggregate: Object.freeze({ op: "arg", direction: "argMax", key, over: value })
|
|
49
|
-
});
|
|
55
|
+
function argMax(value, key) {
|
|
56
|
+
return aggregate("argMax", value, key);
|
|
50
57
|
}
|
|
51
|
-
/** Arg-restriction toward the minimum of `key`; rules as {@link
|
|
52
|
-
function
|
|
53
|
-
return
|
|
54
|
-
aggregate: Object.freeze({ op: "arg", direction: "argMin", key, over: value })
|
|
55
|
-
});
|
|
58
|
+
/** Arg-restriction toward the minimum of `key`; rules as {@link argMax}. */
|
|
59
|
+
function argMin(value, key) {
|
|
60
|
+
return aggregate("argMin", value, key);
|
|
56
61
|
}
|
|
57
62
|
/**
|
|
58
63
|
* The coalescing fold (Snodgrass coalesce, `ir::AggOp::Pack`): per group,
|
|
@@ -63,7 +68,7 @@ function argmin(key, value) {
|
|
|
63
68
|
* fold or an Arg entry (the engine's typed rules).
|
|
64
69
|
*/
|
|
65
70
|
function pack(over) {
|
|
66
|
-
return
|
|
71
|
+
return aggregate("pack", over, undefined);
|
|
67
72
|
}
|
|
68
|
-
export {
|
|
73
|
+
export { argMax, argMin, count, countDistinct, max, min, pack, sum };
|
|
69
74
|
//# sourceMappingURL=select.js.map
|
package/dist/query/select.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/query/select.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/query/select.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AA+BH,kCAAkC;AAClC,SAAS,SAAS,CAIhB,EAAM,EAAE,IAAU,EAAE,GAAQ;IAC7B,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAA;AAC7C,CAAC;AAED,gHAAgH;AAChH,SAAS,KAAK;IACb,OAAO,SAAS,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA;AAChD,CAAC;AAED,qGAAqG;AACrG,SAAS,aAAa,CAAyB,IAAO;IACrD,OAAO,SAAS,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,CAAA;AACnD,CAAC;AAED;;;;;GAKG;AACH,SAAS,GAAG,CAA4C,IAAO;IAC9D,OAAO,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE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