@hypequery/protocol 0.27.0 → 0.30.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/dist/cache-preimages/cache-preimages.d.ts.map +1 -1
- package/dist/cache-preimages/cache-preimages.js +22 -0
- package/dist/deployment-targets/acceptance.d.ts +41 -0
- package/dist/deployment-targets/acceptance.d.ts.map +1 -0
- package/dist/deployment-targets/acceptance.js +48 -0
- package/dist/deployment-targets/accepts.d.ts +26 -0
- package/dist/deployment-targets/accepts.d.ts.map +1 -0
- package/dist/deployment-targets/accepts.js +67 -0
- package/dist/deployment-targets/index.d.ts +5 -0
- package/dist/deployment-targets/index.d.ts.map +1 -0
- package/dist/deployment-targets/index.js +2 -0
- package/dist/deployments/codec.d.ts +8 -0
- package/dist/deployments/codec.d.ts.map +1 -1
- package/dist/deployments/codec.js +11 -1
- package/dist/deployments/contract-3-features.d.ts +16 -0
- package/dist/deployments/contract-3-features.d.ts.map +1 -0
- package/dist/deployments/contract-3-features.js +28 -0
- package/dist/deployments/events.d.ts +10 -0
- package/dist/deployments/events.d.ts.map +1 -0
- package/dist/deployments/events.js +59 -0
- package/dist/deployments/index.d.ts +12 -4
- package/dist/deployments/index.d.ts.map +1 -1
- package/dist/deployments/index.js +7 -2
- package/dist/deployments/readers.d.ts +39 -0
- package/dist/deployments/readers.d.ts.map +1 -0
- package/dist/deployments/readers.js +59 -0
- package/dist/deployments/sql-source-analysis.d.ts +29 -0
- package/dist/deployments/sql-source-analysis.d.ts.map +1 -0
- package/dist/deployments/sql-source-analysis.js +264 -0
- package/dist/deployments/sql-source.d.ts +8 -0
- package/dist/deployments/sql-source.d.ts.map +1 -0
- package/dist/deployments/sql-source.js +107 -0
- package/dist/deployments/types.d.ts +41 -1
- package/dist/deployments/types.d.ts.map +1 -1
- package/dist/deployments/validate.d.ts +19 -0
- package/dist/deployments/validate.d.ts.map +1 -1
- package/dist/deployments/validate.js +163 -9
- package/dist/event-operations/index.d.ts +6 -0
- package/dist/event-operations/index.d.ts.map +1 -0
- package/dist/event-operations/index.js +4 -0
- package/dist/event-operations/limits.d.ts +17 -0
- package/dist/event-operations/limits.d.ts.map +1 -0
- package/dist/event-operations/limits.js +21 -0
- package/dist/event-operations/predicates.d.ts +4 -0
- package/dist/event-operations/predicates.d.ts.map +1 -0
- package/dist/event-operations/predicates.js +10 -0
- package/dist/event-operations/records.d.ts +9 -0
- package/dist/event-operations/records.d.ts.map +1 -0
- package/dist/event-operations/records.js +55 -0
- package/dist/event-operations/types.d.ts +69 -0
- package/dist/event-operations/types.d.ts.map +1 -0
- package/dist/event-operations/types.js +1 -0
- package/dist/event-operations/validate.d.ts +11 -0
- package/dist/event-operations/validate.d.ts.map +1 -0
- package/dist/event-operations/validate.js +220 -0
- package/dist/event-operations/window.d.ts +8 -0
- package/dist/event-operations/window.d.ts.map +1 -0
- package/dist/event-operations/window.js +23 -0
- package/dist/expressions/calculations.d.ts +103 -0
- package/dist/expressions/calculations.d.ts.map +1 -0
- package/dist/expressions/calculations.js +9 -0
- package/dist/expressions/index.d.ts +3 -0
- package/dist/expressions/index.d.ts.map +1 -1
- package/dist/expressions/index.js +2 -0
- package/dist/expressions/relative-dates.d.ts +31 -0
- package/dist/expressions/relative-dates.d.ts.map +1 -0
- package/dist/expressions/relative-dates.js +38 -0
- package/dist/expressions/types.d.ts +24 -1
- package/dist/expressions/types.d.ts.map +1 -1
- package/dist/expressions/validate-calculations.d.ts +41 -0
- package/dist/expressions/validate-calculations.d.ts.map +1 -0
- package/dist/expressions/validate-calculations.js +397 -0
- package/dist/expressions/validate.d.ts.map +1 -1
- package/dist/expressions/validate.js +80 -4
- package/dist/index.d.ts +13 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -3
- package/dist/invocations/index.d.ts +5 -2
- package/dist/invocations/index.d.ts.map +1 -1
- package/dist/invocations/index.js +2 -1
- package/dist/invocations/types.d.ts +13 -3
- package/dist/invocations/types.d.ts.map +1 -1
- package/dist/invocations/validate.d.ts +14 -1
- package/dist/invocations/validate.d.ts.map +1 -1
- package/dist/invocations/validate.js +75 -7
- package/dist/invocations/version.d.ts +23 -0
- package/dist/invocations/version.d.ts.map +1 -0
- package/dist/invocations/version.js +37 -0
- package/dist/visibility/resolve.d.ts +4 -4
- package/dist/visibility/resolve.d.ts.map +1 -1
- package/dist/visibility/resolve.js +10 -4
- package/dist/visibility/types.d.ts +6 -0
- package/dist/visibility/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { CLIENT_TIMEZONE_NAMES } from '../client-contract/timezone-names.js';
|
|
2
|
+
import { ProtocolExpressionError, validateProtocolExpression, validateProtocolSemanticQuery, } from '../expressions/index.js';
|
|
3
|
+
import { expressionError } from '../expressions/errors.js';
|
|
4
|
+
import { ProtocolIdentifierError, parseProtocolIdentifier } from '../identifiers/index.js';
|
|
5
|
+
import { PROTOCOL_EVENT_OPERATION_LIMITS as LIMITS, PROTOCOL_FUNNEL_COLUMNS, PROTOCOL_RETENTION_COLUMNS, } from './limits.js';
|
|
6
|
+
import { isProtocolPredicate } from './predicates.js';
|
|
7
|
+
import { exactFields, freezeRecord, requireArray, requireRecord } from './records.js';
|
|
8
|
+
import { parseProtocolEventWindow } from './window.js';
|
|
9
|
+
const COHORT_GRAINS = new Set(['day', 'week', 'month']);
|
|
10
|
+
const KINDS = new Set(['funnel', 'retention']);
|
|
11
|
+
const COMMON_OPTIONAL = ['entity', 'breakdown', 'breakdownLimit', 'timezone'];
|
|
12
|
+
/** Predicates per filter list; a time range plus ordinary filters. */
|
|
13
|
+
const MAX_FILTERS = 100;
|
|
14
|
+
/** True when `kind` names an RFC 0027 operation rather than an RFC 0003 query. */
|
|
15
|
+
export function isProtocolEventOperationKind(kind) {
|
|
16
|
+
return typeof kind === 'string' && KINDS.has(kind);
|
|
17
|
+
}
|
|
18
|
+
function identifier(value, path) {
|
|
19
|
+
if (typeof value !== 'string')
|
|
20
|
+
expressionError('HQ_EXPRESSION_TYPE', path);
|
|
21
|
+
try {
|
|
22
|
+
return parseProtocolIdentifier(value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof ProtocolIdentifierError)
|
|
26
|
+
expressionError('HQ_EXPRESSION_INVALID_IDENTIFIER', path);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function boundedInteger(value, path, minimum, maximum) {
|
|
31
|
+
if (typeof value !== 'number')
|
|
32
|
+
expressionError('HQ_EXPRESSION_TYPE', path);
|
|
33
|
+
if (!Number.isSafeInteger(value) || value < minimum || value > maximum) {
|
|
34
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', path);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
/** An RFC 0003 predicate, with failures reported at its place in the operation. */
|
|
39
|
+
function predicate(input, path) {
|
|
40
|
+
let expression;
|
|
41
|
+
try {
|
|
42
|
+
expression = validateProtocolExpression(input);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (error instanceof ProtocolExpressionError) {
|
|
46
|
+
expressionError(error.code, `${path}${error.path.slice(1)}`);
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
if (!isProtocolPredicate(expression))
|
|
51
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', path);
|
|
52
|
+
return expression;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A caller filter, validated exactly as a semantic query's `filters` entry is
|
|
56
|
+
* (RFC 0029): and/or/not trees, and `inDateRange`, which only query filters
|
|
57
|
+
* may carry. Failures are reported at its place in the operation.
|
|
58
|
+
*/
|
|
59
|
+
function queryFilter(input, path) {
|
|
60
|
+
try {
|
|
61
|
+
return validateProtocolSemanticQuery({ kind: 'dataset', dataset: 'event_operation', filters: [input] }).filters[0];
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (error instanceof ProtocolExpressionError) {
|
|
65
|
+
const at = error.path.startsWith('$.filters[0]') ? `${path}${error.path.slice('$.filters[0]'.length)}` : path;
|
|
66
|
+
expressionError(error.code, at);
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function predicates(input, path, minimum, maximum, validate = predicate) {
|
|
72
|
+
const values = requireArray(input, path, maximum);
|
|
73
|
+
if (values.length < minimum)
|
|
74
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', path);
|
|
75
|
+
return Object.freeze(values.map((item, index) => validate(item, `${path}[${index}]`)));
|
|
76
|
+
}
|
|
77
|
+
/** A `{ where }` condition list: 1–10 AND-combined predicates. */
|
|
78
|
+
function conditions(input, path) {
|
|
79
|
+
return predicates(input, path, 1, LIMITS.maxConditions);
|
|
80
|
+
}
|
|
81
|
+
function common(value, result, reserved) {
|
|
82
|
+
result.dataset = identifier(value.dataset, '$.dataset');
|
|
83
|
+
if (value.entity !== undefined)
|
|
84
|
+
result.entity = identifier(value.entity, '$.entity');
|
|
85
|
+
// Every operation needs a time range, so `filters` can never be empty.
|
|
86
|
+
result.filters = predicates(value.filters, '$.filters', 1, MAX_FILTERS, queryFilter);
|
|
87
|
+
if (value.breakdown !== undefined) {
|
|
88
|
+
const names = requireArray(value.breakdown, '$.breakdown', LIMITS.maxBreakdown);
|
|
89
|
+
if (names.length === 0)
|
|
90
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.breakdown');
|
|
91
|
+
const seen = new Set();
|
|
92
|
+
result.breakdown = Object.freeze(names.map((item, index) => {
|
|
93
|
+
const path = `$.breakdown[${index}]`;
|
|
94
|
+
const name = identifier(item, path);
|
|
95
|
+
// A row can't carry two values under one key.
|
|
96
|
+
if (seen.has(name) || reserved.has(name))
|
|
97
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', path);
|
|
98
|
+
seen.add(name);
|
|
99
|
+
return name;
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
if (value.breakdownLimit !== undefined) {
|
|
103
|
+
if (value.breakdown === undefined)
|
|
104
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.breakdownLimit');
|
|
105
|
+
result.breakdownLimit = boundedInteger(value.breakdownLimit, '$.breakdownLimit', 1, LIMITS.maxBreakdownLimit);
|
|
106
|
+
}
|
|
107
|
+
if (value.timezone !== undefined) {
|
|
108
|
+
if (typeof value.timezone !== 'string')
|
|
109
|
+
expressionError('HQ_EXPRESSION_TYPE', '$.timezone');
|
|
110
|
+
if (!CLIENT_TIMEZONE_NAMES.has(value.timezone))
|
|
111
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.timezone');
|
|
112
|
+
result.timezone = value.timezone;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function cohortGrain(value, path) {
|
|
116
|
+
if (typeof value !== 'string')
|
|
117
|
+
expressionError('HQ_EXPRESSION_TYPE', path);
|
|
118
|
+
if (!COHORT_GRAINS.has(value))
|
|
119
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', path);
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
function funnel(value) {
|
|
123
|
+
exactFields(value, ['kind', 'dataset', 'steps', 'window', 'filters'], [...COMMON_OPTIONAL, 'order', 'by'], '$');
|
|
124
|
+
const result = { kind: 'funnel' };
|
|
125
|
+
const steps = requireArray(value.steps, '$.steps', LIMITS.maxSteps);
|
|
126
|
+
if (steps.length < LIMITS.minSteps)
|
|
127
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.steps');
|
|
128
|
+
const names = new Set();
|
|
129
|
+
const validatedSteps = Object.freeze(steps.map((item, index) => {
|
|
130
|
+
const path = `$.steps[${index}]`;
|
|
131
|
+
const step = requireRecord(item, path);
|
|
132
|
+
exactFields(step, ['name', 'where'], [], path);
|
|
133
|
+
const name = identifier(step.name, `${path}.name`);
|
|
134
|
+
if (names.has(name))
|
|
135
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', `${path}.name`);
|
|
136
|
+
names.add(name);
|
|
137
|
+
return freezeRecord({ name, where: conditions(step.where, `${path}.where`) });
|
|
138
|
+
}));
|
|
139
|
+
if (typeof value.window !== 'string')
|
|
140
|
+
expressionError('HQ_EXPRESSION_TYPE', '$.window');
|
|
141
|
+
const window = parseProtocolEventWindow(value.window);
|
|
142
|
+
if (!window || window.milliseconds > LIMITS.maxWindowMilliseconds) {
|
|
143
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.window');
|
|
144
|
+
}
|
|
145
|
+
// Document order: kind, dataset, entity, steps, window, order, filters, …
|
|
146
|
+
const shared = {};
|
|
147
|
+
common(value, shared, PROTOCOL_FUNNEL_COLUMNS);
|
|
148
|
+
result.dataset = shared.dataset;
|
|
149
|
+
if (shared.entity !== undefined)
|
|
150
|
+
result.entity = shared.entity;
|
|
151
|
+
result.steps = validatedSteps;
|
|
152
|
+
result.window = value.window;
|
|
153
|
+
if (value.order !== undefined) {
|
|
154
|
+
if (typeof value.order !== 'string')
|
|
155
|
+
expressionError('HQ_EXPRESSION_TYPE', '$.order');
|
|
156
|
+
if (value.order !== 'strict' && value.order !== 'any')
|
|
157
|
+
expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.order');
|
|
158
|
+
result.order = value.order;
|
|
159
|
+
}
|
|
160
|
+
result.filters = shared.filters;
|
|
161
|
+
if (shared.breakdown !== undefined)
|
|
162
|
+
result.breakdown = shared.breakdown;
|
|
163
|
+
if (shared.breakdownLimit !== undefined)
|
|
164
|
+
result.breakdownLimit = shared.breakdownLimit;
|
|
165
|
+
if (value.by !== undefined)
|
|
166
|
+
result.by = cohortGrain(value.by, '$.by');
|
|
167
|
+
if (shared.timezone !== undefined)
|
|
168
|
+
result.timezone = shared.timezone;
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
/** A `{ where }` record: what starts a cohort, or what counts as a return. */
|
|
172
|
+
function condition(input, path) {
|
|
173
|
+
const value = requireRecord(input, path);
|
|
174
|
+
exactFields(value, ['where'], [], path);
|
|
175
|
+
return freezeRecord({ where: conditions(value.where, `${path}.where`) });
|
|
176
|
+
}
|
|
177
|
+
function retention(value) {
|
|
178
|
+
exactFields(value, ['kind', 'dataset', 'start', 'return', 'grain', 'periods', 'filters'], COMMON_OPTIONAL, '$');
|
|
179
|
+
const start = condition(value.start, '$.start');
|
|
180
|
+
const returning = condition(value.return, '$.return');
|
|
181
|
+
const grain = cohortGrain(value.grain, '$.grain');
|
|
182
|
+
const periods = boundedInteger(value.periods, '$.periods', 1, LIMITS.maxPeriods);
|
|
183
|
+
const shared = {};
|
|
184
|
+
common(value, shared, PROTOCOL_RETENTION_COLUMNS);
|
|
185
|
+
// Document order: kind, dataset, entity, start, return, grain, periods, filters, …
|
|
186
|
+
return {
|
|
187
|
+
kind: 'retention',
|
|
188
|
+
dataset: shared.dataset,
|
|
189
|
+
...(shared.entity === undefined ? {} : { entity: shared.entity }),
|
|
190
|
+
start,
|
|
191
|
+
return: returning,
|
|
192
|
+
grain,
|
|
193
|
+
periods,
|
|
194
|
+
filters: shared.filters,
|
|
195
|
+
...(shared.breakdown === undefined ? {} : { breakdown: shared.breakdown }),
|
|
196
|
+
...(shared.breakdownLimit === undefined ? {} : { breakdownLimit: shared.breakdownLimit }),
|
|
197
|
+
...(shared.timezone === undefined ? {} : { timezone: shared.timezone }),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Validate an RFC 0027 operation: syntax, closed records, limits and output
|
|
202
|
+
* names. Whether the dataset declares events, whether each field exists and
|
|
203
|
+
* may be used, and the time-range rule depend on the deployment contract and
|
|
204
|
+
* the caller, so the data plane checks those.
|
|
205
|
+
*/
|
|
206
|
+
export function validateProtocolEventOperation(input) {
|
|
207
|
+
const value = requireRecord(input, '$');
|
|
208
|
+
if (!Object.hasOwn(value, 'kind'))
|
|
209
|
+
expressionError('HQ_EXPRESSION_TYPE', '$.kind');
|
|
210
|
+
if (typeof value.kind !== 'string')
|
|
211
|
+
expressionError('HQ_EXPRESSION_TYPE', '$.kind');
|
|
212
|
+
switch (value.kind) {
|
|
213
|
+
case 'funnel':
|
|
214
|
+
return freezeRecord(funnel(value));
|
|
215
|
+
case 'retention':
|
|
216
|
+
return freezeRecord(retention(value));
|
|
217
|
+
default:
|
|
218
|
+
return expressionError('HQ_EXPRESSION_INVALID_QUERY', '$.kind');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ProtocolEventWindow } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a funnel window (RFC 0027): an integer from 1, one space, and a
|
|
4
|
+
* singular unit. One encoding per window, so `"7 days"` and `"07 day"` are
|
|
5
|
+
* refused rather than normalized. Returns `undefined` for anything else.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseProtocolEventWindow(value: unknown): ProtocolEventWindow | undefined;
|
|
8
|
+
//# sourceMappingURL=window.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"window.d.ts","sourceRoot":"","sources":["../../src/event-operations/window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAA2B,MAAM,YAAY,CAAC;AAY/E;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,mBAAmB,GAAG,SAAS,CAOxF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const UNIT_MILLISECONDS = Object.freeze({
|
|
2
|
+
second: 1_000,
|
|
3
|
+
minute: 60_000,
|
|
4
|
+
hour: 3_600_000,
|
|
5
|
+
day: 86_400_000,
|
|
6
|
+
week: 604_800_000,
|
|
7
|
+
});
|
|
8
|
+
const WINDOW_PATTERN = /^([1-9][0-9]{0,8}) (second|minute|hour|day|week)$/;
|
|
9
|
+
/**
|
|
10
|
+
* Parse a funnel window (RFC 0027): an integer from 1, one space, and a
|
|
11
|
+
* singular unit. One encoding per window, so `"7 days"` and `"07 day"` are
|
|
12
|
+
* refused rather than normalized. Returns `undefined` for anything else.
|
|
13
|
+
*/
|
|
14
|
+
export function parseProtocolEventWindow(value) {
|
|
15
|
+
if (typeof value !== 'string')
|
|
16
|
+
return undefined;
|
|
17
|
+
const match = WINDOW_PATTERN.exec(value);
|
|
18
|
+
if (!match)
|
|
19
|
+
return undefined;
|
|
20
|
+
const amount = Number(match[1]);
|
|
21
|
+
const unit = match[2];
|
|
22
|
+
return Object.freeze({ amount, unit, milliseconds: amount * UNIT_MILLISECONDS[unit] });
|
|
23
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query-scoped calculations (RFC 0026): one-off variants of published
|
|
3
|
+
* measures carried by an extension 2 dataset query and compiled by the same
|
|
4
|
+
* planner as model-level measures.
|
|
5
|
+
*/
|
|
6
|
+
import type { ProtocolIdentifier } from '../identifiers/index.js';
|
|
7
|
+
import type { ProtocolExpression, ProtocolQueryTimeGrain } from './types.js';
|
|
8
|
+
/** Every calculation kind, in the order RFC 0026 lists them. */
|
|
9
|
+
export declare const PROTOCOL_CALCULATION_KINDS: readonly ["filter", "shift", "window", "formula", "compare", "rank", "share"];
|
|
10
|
+
export type ProtocolCalculationKind = typeof PROTOCOL_CALCULATION_KINDS[number];
|
|
11
|
+
/** Operators a formula node may use. `percentChange(a, b)` is `(a - b) / NULLIF(b, 0)`. */
|
|
12
|
+
export type ProtocolFormulaOperator = 'add' | 'subtract' | 'multiply' | 'divide' | 'nullIfZero' | 'coalesce' | 'round' | 'floor' | 'ceil' | 'percentChange';
|
|
13
|
+
/** A formula operand: an input reference, a finite number, or a nested node. */
|
|
14
|
+
export type ProtocolFormulaArgument = ProtocolIdentifier | number | ProtocolFormulaNode;
|
|
15
|
+
export interface ProtocolFormulaNode {
|
|
16
|
+
readonly op: ProtocolFormulaOperator;
|
|
17
|
+
readonly args: readonly ProtocolFormulaArgument[];
|
|
18
|
+
}
|
|
19
|
+
export interface ProtocolCalculationInterval {
|
|
20
|
+
readonly amount: number;
|
|
21
|
+
readonly unit: ProtocolQueryTimeGrain;
|
|
22
|
+
}
|
|
23
|
+
export interface ProtocolFilterCalculation {
|
|
24
|
+
readonly measure: ProtocolIdentifier;
|
|
25
|
+
/** AND-combined plain comparisons (reference op literal), as in `filters`. */
|
|
26
|
+
readonly where: readonly ProtocolExpression[];
|
|
27
|
+
}
|
|
28
|
+
export interface ProtocolShiftCalculation extends ProtocolCalculationInterval {
|
|
29
|
+
readonly measure: ProtocolIdentifier;
|
|
30
|
+
}
|
|
31
|
+
export type ProtocolWindowCalculation = {
|
|
32
|
+
readonly measure: ProtocolIdentifier;
|
|
33
|
+
readonly trailing: ProtocolCalculationInterval;
|
|
34
|
+
} | {
|
|
35
|
+
readonly measure: ProtocolIdentifier;
|
|
36
|
+
readonly toDate: Exclude<ProtocolQueryTimeGrain, 'minute'>;
|
|
37
|
+
} | {
|
|
38
|
+
readonly measure: ProtocolIdentifier;
|
|
39
|
+
readonly cumulative: true;
|
|
40
|
+
};
|
|
41
|
+
export interface ProtocolCompareCalculation {
|
|
42
|
+
readonly to: 'previousPeriod' | 'previousYear';
|
|
43
|
+
readonly measures: readonly ProtocolIdentifier[];
|
|
44
|
+
}
|
|
45
|
+
export interface ProtocolRankCalculation {
|
|
46
|
+
readonly measure: ProtocolIdentifier;
|
|
47
|
+
/** Partition: selected dimensions, plus `period` when `by` is present. */
|
|
48
|
+
readonly within?: readonly string[];
|
|
49
|
+
/** Defaults to `desc`: rank 1 is the highest value. */
|
|
50
|
+
readonly direction?: 'asc' | 'desc';
|
|
51
|
+
}
|
|
52
|
+
export interface ProtocolShareCalculation {
|
|
53
|
+
readonly measure: ProtocolIdentifier;
|
|
54
|
+
/** The grain the denominator keeps; absent or empty is the grand total. */
|
|
55
|
+
readonly of?: readonly string[];
|
|
56
|
+
}
|
|
57
|
+
export type ProtocolQueryCalculation = {
|
|
58
|
+
readonly name: ProtocolIdentifier;
|
|
59
|
+
readonly filter: ProtocolFilterCalculation;
|
|
60
|
+
} | {
|
|
61
|
+
readonly name: ProtocolIdentifier;
|
|
62
|
+
readonly shift: ProtocolShiftCalculation;
|
|
63
|
+
} | {
|
|
64
|
+
readonly name: ProtocolIdentifier;
|
|
65
|
+
readonly window: ProtocolWindowCalculation;
|
|
66
|
+
} | {
|
|
67
|
+
readonly name: ProtocolIdentifier;
|
|
68
|
+
readonly formula: ProtocolFormulaNode;
|
|
69
|
+
} | {
|
|
70
|
+
readonly name: ProtocolIdentifier;
|
|
71
|
+
readonly compare: ProtocolCompareCalculation;
|
|
72
|
+
} | {
|
|
73
|
+
readonly name: ProtocolIdentifier;
|
|
74
|
+
readonly rank: ProtocolRankCalculation;
|
|
75
|
+
} | {
|
|
76
|
+
readonly name: ProtocolIdentifier;
|
|
77
|
+
readonly share: ProtocolShareCalculation;
|
|
78
|
+
};
|
|
79
|
+
/** Keep the first `n` rows of each `by` group in `orderBy` order (`LIMIT n BY`). */
|
|
80
|
+
export interface ProtocolLimitBy {
|
|
81
|
+
readonly n: number;
|
|
82
|
+
readonly by: readonly string[];
|
|
83
|
+
}
|
|
84
|
+
/** Group `dimension` as itself for its top `n` values of `measure`, else as `label`. */
|
|
85
|
+
export interface ProtocolOtherBucket {
|
|
86
|
+
readonly dimension: ProtocolIdentifier;
|
|
87
|
+
readonly n: number;
|
|
88
|
+
readonly measure: ProtocolIdentifier;
|
|
89
|
+
/** Defaults to `"Other"`. */
|
|
90
|
+
readonly label?: string;
|
|
91
|
+
}
|
|
92
|
+
/** One output column a calculation adds to a result (`meta.calculations`). */
|
|
93
|
+
export interface ProtocolCalculationColumn {
|
|
94
|
+
readonly name: string;
|
|
95
|
+
readonly kind: Exclude<ProtocolCalculationKind, 'compare'>;
|
|
96
|
+
/** The calculation entry that produced the column. */
|
|
97
|
+
readonly calculation: string;
|
|
98
|
+
/** The input the column varies, for filter, shift, window, rank and share. */
|
|
99
|
+
readonly measure?: string;
|
|
100
|
+
readonly type: 'number' | 'integer';
|
|
101
|
+
readonly format?: 'ratio';
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=calculations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/expressions/calculations.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAE7E,gEAAgE;AAChE,eAAO,MAAM,0BAA0B,+EAE5B,CAAC;AAEZ,MAAM,MAAM,uBAAuB,GAAG,OAAO,0BAA0B,CAAC,MAAM,CAAC,CAAC;AAEhF,2FAA2F;AAC3F,MAAM,MAAM,uBAAuB,GAC/B,KAAK,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,GAC1C,YAAY,GAAG,UAAU,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GACtD,eAAe,CAAC;AAEpB,gFAAgF;AAChF,MAAM,MAAM,uBAAuB,GAAG,kBAAkB,GAAG,MAAM,GAAG,mBAAmB,CAAC;AAExF,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,EAAE,EAAE,uBAAuB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,SAAS,uBAAuB,EAAE,CAAC;CACnD;AAED,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,sBAAsB,CAAC;CACvC;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,8EAA8E;IAC9E,QAAQ,CAAC,KAAK,EAAE,SAAS,kBAAkB,EAAE,CAAC;CAC/C;AAED,MAAM,WAAW,wBAAyB,SAAQ,2BAA2B;IAC3E,QAAQ,CAAC,OAAO,EAAE,kBAAkB,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3B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query-scoped calculations (RFC 0026): one-off variants of published
|
|
3
|
+
* measures carried by an extension 2 dataset query and compiled by the same
|
|
4
|
+
* planner as model-level measures.
|
|
5
|
+
*/
|
|
6
|
+
/** Every calculation kind, in the order RFC 0026 lists them. */
|
|
7
|
+
export const PROTOCOL_CALCULATION_KINDS = Object.freeze([
|
|
8
|
+
'filter', 'shift', 'window', 'formula', 'compare', 'rank', 'share',
|
|
9
|
+
]);
|
|
@@ -2,5 +2,8 @@ export { ProtocolExpressionError } from './errors.js';
|
|
|
2
2
|
export { DEFAULT_PROTOCOL_EXPRESSION_LIMITS } from './limits.js';
|
|
3
3
|
export { validateProtocolExpression, validateProtocolSemanticQuery } from './validate.js';
|
|
4
4
|
export { PROTOCOL_NULL_CHECK_OPERATORS, isProtocolNullCheckOperator } from './null-checks.js';
|
|
5
|
+
export { PROTOCOL_CALCULATION_KINDS } from './calculations.js';
|
|
6
|
+
export type { ProtocolCalculationColumn, ProtocolCalculationInterval, ProtocolCalculationKind, ProtocolCompareCalculation, ProtocolFilterCalculation, ProtocolFormulaArgument, ProtocolFormulaNode, ProtocolFormulaOperator, ProtocolLimitBy, ProtocolOtherBucket, ProtocolQueryCalculation, ProtocolRankCalculation, ProtocolShareCalculation, ProtocolShiftCalculation, ProtocolWindowCalculation, } from './calculations.js';
|
|
5
7
|
export type { ProtocolAggregation, ProtocolBinaryOperator, ProtocolComparisonOperator, ProtocolDatasetQuery, ProtocolExpression, ProtocolExpressionErrorCode, ProtocolExpressionExtension, ProtocolExpressionLimits, ProtocolExpressionOptions, ProtocolFunctionName, ProtocolMetricQuery, ProtocolNullCheckOperator, ProtocolHavingComparisonOperator, ProtocolHavingCondition, ProtocolOrderBy, ProtocolSemanticQuery, ProtocolQueryTimeGrain, ProtocolTimeGrain, ProtocolValueComparisonOperator, } from './types.js';
|
|
8
|
+
export { isProtocolRelativeDateRange, parseProtocolRelativeDateRange, type ProtocolRelativeDateRange, type ProtocolRelativeDateUnit, } from './relative-dates.js';
|
|
6
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/expressions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,0BAA0B,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAC1F,OAAO,EAAE,6BAA6B,EAAE,2BAA2B,EAAE,MAAM,kBAAkB,CAAC;AAC9F,YAAY,EACV,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,EAC1B,oBAAoB,EACpB,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,EAC3B,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,yBAAyB,EACzB,gCAAgC,EAChC,uBAAuB,EACvB,eAAe,EACf,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,EACjB,+BAA+B,GAChC,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/expressions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,0BAA0B,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAC1F,OAAO,EAAE,6BAA6B,EAAE,2BAA2B,EAAE,MAAM,kBAAkB,CAAC;AAC9F,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAC/D,YAAY,EACV,yBAAyB,EACzB,2BAA2B,EAC3B,uBAAuB,EACvB,0BAA0B,EAC1B,yBAAyB,EACzB,uBAAuB,EACvB,mBAAmB,EACnB,uBAAuB,EACvB,eAAe,EACf,mBAAmB,EACnB,wBAAwB,EACxB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EACV,mBAAmB,EACnB,sBAAsB,EACtB,0BAA0B,EAC1B,oBAAoB,EACpB,kBAAkB,EAClB,2BAA2B,EAC3B,2BAA2B,EAC3B,wBAAwB,EACxB,yBAAyB,EACzB,oBAAoB,EACpB,mBAAmB,EACnB,yBAAyB,EACzB,gCAAgC,EAChC,uBAAuB,EACvB,eAAe,EACf,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,EACjB,+BAA+B,GAChC,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,EAC9B,KAAK,yBAAyB,EAC9B,KAAK,wBAAwB,GAC9B,MAAM,qBAAqB,CAAC"}
|
|
@@ -2,3 +2,5 @@ export { ProtocolExpressionError } from './errors.js';
|
|
|
2
2
|
export { DEFAULT_PROTOCOL_EXPRESSION_LIMITS } from './limits.js';
|
|
3
3
|
export { validateProtocolExpression, validateProtocolSemanticQuery } from './validate.js';
|
|
4
4
|
export { PROTOCOL_NULL_CHECK_OPERATORS, isProtocolNullCheckOperator } from './null-checks.js';
|
|
5
|
+
export { PROTOCOL_CALCULATION_KINDS } from './calculations.js';
|
|
6
|
+
export { isProtocolRelativeDateRange, parseProtocolRelativeDateRange, } from './relative-dates.js';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relative date grammar of the `inDateRange` comparison (RFC 0029).
|
|
3
|
+
*
|
|
4
|
+
* Values are lowercase ASCII words separated by single spaces. Parsing is
|
|
5
|
+
* exact: no trimming, no case folding, no aliases, so every implementation
|
|
6
|
+
* accepts the same strings.
|
|
7
|
+
*/
|
|
8
|
+
export type ProtocolRelativeDateUnit = 'day' | 'week' | 'month' | 'quarter' | 'year';
|
|
9
|
+
/** A parsed relative range; see RFC 0029 §2 for the bounds each kind resolves to. */
|
|
10
|
+
export type ProtocolRelativeDateRange = {
|
|
11
|
+
readonly kind: 'today';
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: 'yesterday';
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'this';
|
|
16
|
+
readonly unit: ProtocolRelativeDateUnit;
|
|
17
|
+
}
|
|
18
|
+
/** `last <unit>` has count 1; `last N <unit>(s)` has count N. */
|
|
19
|
+
| {
|
|
20
|
+
readonly kind: 'last';
|
|
21
|
+
readonly unit: ProtocolRelativeDateUnit;
|
|
22
|
+
readonly count: number;
|
|
23
|
+
} | {
|
|
24
|
+
readonly kind: 'toDate';
|
|
25
|
+
readonly unit: 'month' | 'quarter' | 'year';
|
|
26
|
+
};
|
|
27
|
+
/** The parsed range, or `undefined` when `value` is not in the grammar. */
|
|
28
|
+
export declare function parseProtocolRelativeDateRange(value: unknown): ProtocolRelativeDateRange | undefined;
|
|
29
|
+
/** True when `value` is a relative date range of the RFC 0029 grammar. */
|
|
30
|
+
export declare function isProtocolRelativeDateRange(value: unknown): value is string;
|
|
31
|
+
//# sourceMappingURL=relative-dates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative-dates.d.ts","sourceRoot":"","sources":["../../src/expressions/relative-dates.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,wBAAwB,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,CAAC;AAErF,qFAAqF;AACrF,MAAM,MAAM,yBAAyB,GACjC;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,wBAAwB,CAAA;CAAE;AACpE,iEAAiE;GAC/D;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,wBAAwB,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC1F;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,CAAA;CAAE,CAAC;AAQ7E,2EAA2E;AAC3E,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,OAAO,GAAG,yBAAyB,GAAG,SAAS,CAapG;AAED,0EAA0E;AAC1E,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE3E"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relative date grammar of the `inDateRange` comparison (RFC 0029).
|
|
3
|
+
*
|
|
4
|
+
* Values are lowercase ASCII words separated by single spaces. Parsing is
|
|
5
|
+
* exact: no trimming, no case folding, no aliases, so every implementation
|
|
6
|
+
* accepts the same strings.
|
|
7
|
+
*/
|
|
8
|
+
const UNITS = '(day|week|month|quarter|year)';
|
|
9
|
+
const THIS = new RegExp(`^this ${UNITS}$`);
|
|
10
|
+
const LAST = new RegExp(`^last ${UNITS}$`);
|
|
11
|
+
const LAST_N = new RegExp(`^last ([1-9][0-9]{0,3}) ${UNITS}s?$`);
|
|
12
|
+
const TO_DATE = /^(month|quarter|year) to date$/;
|
|
13
|
+
/** The parsed range, or `undefined` when `value` is not in the grammar. */
|
|
14
|
+
export function parseProtocolRelativeDateRange(value) {
|
|
15
|
+
if (typeof value !== 'string')
|
|
16
|
+
return undefined;
|
|
17
|
+
if (value === 'today')
|
|
18
|
+
return { kind: 'today' };
|
|
19
|
+
if (value === 'yesterday')
|
|
20
|
+
return { kind: 'yesterday' };
|
|
21
|
+
let match = THIS.exec(value);
|
|
22
|
+
if (match)
|
|
23
|
+
return { kind: 'this', unit: match[1] };
|
|
24
|
+
match = LAST.exec(value);
|
|
25
|
+
if (match)
|
|
26
|
+
return { kind: 'last', unit: match[1], count: 1 };
|
|
27
|
+
match = LAST_N.exec(value);
|
|
28
|
+
if (match)
|
|
29
|
+
return { kind: 'last', unit: match[2], count: Number(match[1]) };
|
|
30
|
+
match = TO_DATE.exec(value);
|
|
31
|
+
if (match)
|
|
32
|
+
return { kind: 'toDate', unit: match[1] };
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
/** True when `value` is a relative date range of the RFC 0029 grammar. */
|
|
36
|
+
export function isProtocolRelativeDateRange(value) {
|
|
37
|
+
return parseProtocolRelativeDateRange(value) !== undefined;
|
|
38
|
+
}
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { ProtocolIdentifier, ProtocolQualifiedIdentifier } from '../identifiers/index.js';
|
|
2
2
|
import type { CanonicalValue } from '../values/index.js';
|
|
3
|
+
import type { ProtocolLimitBy, ProtocolOtherBucket, ProtocolQueryCalculation } from './calculations.js';
|
|
3
4
|
export type ProtocolBinaryOperator = 'add' | 'subtract' | 'multiply' | 'divide';
|
|
4
5
|
export type ProtocolFunctionName = 'nullIfZero' | 'coalesce' | 'round' | 'floor' | 'ceil';
|
|
5
6
|
/** Comparison operators that compare `left` with a `right` value. */
|
|
6
|
-
export type ProtocolValueComparisonOperator = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'notIn' | 'between' | 'like'
|
|
7
|
+
export type ProtocolValueComparisonOperator = 'eq' | 'neq' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'notIn' | 'between' | 'like'
|
|
8
|
+
/**
|
|
9
|
+
* A relative date range such as `"last 30 days"` (RFC 0029). Allowed only
|
|
10
|
+
* in a semantic query's `filters`; the executor resolves it.
|
|
11
|
+
*/
|
|
12
|
+
| 'inDateRange';
|
|
7
13
|
/** Comparison operators that test `left` for NULL and take no `right` (RFC 0023). */
|
|
8
14
|
export type ProtocolNullCheckOperator = 'isNull' | 'isNotNull';
|
|
9
15
|
export type ProtocolComparisonOperator = ProtocolValueComparisonOperator | ProtocolNullCheckOperator;
|
|
@@ -97,6 +103,12 @@ interface ProtocolQueryCommon {
|
|
|
97
103
|
readonly limit?: number;
|
|
98
104
|
readonly offset?: number;
|
|
99
105
|
readonly by?: ProtocolQueryTimeGrain;
|
|
106
|
+
/**
|
|
107
|
+
* The time axis for `by`, `fill`, window and shift measures: the dataset's
|
|
108
|
+
* `timeField` or one of its contract 3 `alternateTimeFields`. Absent means
|
|
109
|
+
* `timeField`. RFC 0029.
|
|
110
|
+
*/
|
|
111
|
+
readonly timeKey?: ProtocolIdentifier;
|
|
100
112
|
/**
|
|
101
113
|
* IANA zone for time-grain buckets and time-key filter bounds, from RFC
|
|
102
114
|
* 0018's pinned registry. Absent means UTC. RFC 0025.
|
|
@@ -116,6 +128,17 @@ export interface ProtocolDatasetQuery extends ProtocolQueryCommon {
|
|
|
116
128
|
* Each condition requires its measure in `measures`. RFC 0025.
|
|
117
129
|
*/
|
|
118
130
|
readonly having?: readonly ProtocolHavingCondition[];
|
|
131
|
+
/** Query-scoped calculations. Extension 2 only. RFC 0026. */
|
|
132
|
+
readonly calculations?: readonly ProtocolQueryCalculation[];
|
|
133
|
+
/** Top-N per group (`LIMIT n BY`). Extension 2 only. RFC 0026. */
|
|
134
|
+
readonly limitBy?: ProtocolLimitBy;
|
|
135
|
+
/** Top-N of one dimension plus a remainder bucket. Extension 2 only. RFC 0026. */
|
|
136
|
+
readonly other?: ProtocolOtherBucket;
|
|
137
|
+
/**
|
|
138
|
+
* Return every `by` period of the query's bounded time range, filling
|
|
139
|
+
* empty ones (RFC 0029). Requires `by`; `false` means the same as absent.
|
|
140
|
+
*/
|
|
141
|
+
readonly fill?: boolean;
|
|
119
142
|
}
|
|
120
143
|
export interface ProtocolMetricQuery extends ProtocolQueryCommon {
|
|
121
144
|
readonly kind: 'metric';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/expressions/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AAC/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/expressions/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AAC/F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAExG,MAAM,MAAM,sBAAsB,GAAG,KAAK,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,CAAC;AAChF,MAAM,MAAM,oBAAoB,GAAG,YAAY,GAAG,UAAU,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;AAC1F,qEAAqE;AACrE,MAAM,MAAM,+BAA+B,GACvC,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM;AAClF;;;GAGG;GACD,aAAa,CAAC;AAClB,qFAAqF;AACrF,MAAM,MAAM,yBAAyB,GAAG,QAAQ,GAAG,WAAW,CAAC;AAC/D,MAAM,MAAM,0BAA0B,GAAG,+BAA+B,GAAG,yBAAyB,CAAC;AACrG;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAC3B,KAAK,GAAG,OAAO,GAAG,eAAe,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GACzD,QAAQ,GAAG,QAAQ,GAAG,YAAY,GAAG,QAAQ,GAAG,UAAU,GAC1D,qBAAqB,CAAC;AAE1B,MAAM,MAAM,kBAAkB,GAC1B;IAAE,QAAQ,CAAC,IAAI,EA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GG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,mBAAoB,SAAQ,mBAAmB;IAC9D,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,kBAAkB,CAAC;CACrC;AAED,MAAM,MAAM,qBAAqB,GAAG,oBAAoB,GAAG,mBAAmB,CAAC;AAE/E,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;CACrC;AAED,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,CAAC;IACpD;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,2BAA2B,CAAC;CAClD;AAED,MAAM,MAAM,2BAA2B,GACnC,oBAAoB,GACpB,6BAA6B,GAC7B,4BAA4B,GAC5B,kCAAkC,GAClC,6BAA6B,GAC7B,gCAAgC,GAChC,6BAA6B,GAC7B,mCAAmC,GACnC,6BAA6B,GAC7B,wBAAwB,GACxB,8BAA8B,GAC9B,8BAA8B,GAC9B,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 0026 envelope validation for `calculations`, `limitBy` and `other`.
|
|
3
|
+
*
|
|
4
|
+
* Everything here is decidable from the query alone: shapes, arities, output
|
|
5
|
+
* names, references between entries, and what the query selects. Whether an
|
|
6
|
+
* input is a published measure, visible to the caller, or of the right
|
|
7
|
+
* aggregation is decided against the contract by the data plane and the
|
|
8
|
+
* executor.
|
|
9
|
+
*/
|
|
10
|
+
import type { ProtocolExpression } from './types.js';
|
|
11
|
+
type DataRecord = Record<string, unknown>;
|
|
12
|
+
/** The primitives `validate.ts` owns, handed over so both modules share one state. */
|
|
13
|
+
export interface CalculationValidationKit {
|
|
14
|
+
requireRecord(input: unknown, path: string): DataRecord;
|
|
15
|
+
exactFields(value: DataRecord, required: readonly string[], optional: readonly string[], path: string): void;
|
|
16
|
+
stringValue(value: unknown, path: string): string;
|
|
17
|
+
identifier(value: unknown, path: string, qualified?: boolean): string;
|
|
18
|
+
/** Counts a node toward the depth/node limits and guards against cycles. */
|
|
19
|
+
enter(value: object, depth: number, path: string): void;
|
|
20
|
+
leave(value: object): void;
|
|
21
|
+
arrayValue(value: unknown, path: string): readonly unknown[];
|
|
22
|
+
freezeRecord(entries: Record<string, unknown>): DataRecord;
|
|
23
|
+
predicate(input: unknown, path: string): ProtocolExpression;
|
|
24
|
+
}
|
|
25
|
+
/** What the rest of the query contributes to calculation validation. */
|
|
26
|
+
export interface CalculationQueryShape {
|
|
27
|
+
readonly dimensions: readonly string[];
|
|
28
|
+
readonly measures: readonly string[];
|
|
29
|
+
readonly hasBy: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Validates `calculations`; returns the normalized entries and every output column name. */
|
|
32
|
+
export declare function validateQueryCalculations(kit: CalculationValidationKit, input: unknown, query: CalculationQueryShape): {
|
|
33
|
+
readonly entries: readonly DataRecord[];
|
|
34
|
+
readonly outputs: readonly string[];
|
|
35
|
+
};
|
|
36
|
+
/** Validates `limitBy`; `ordered` is whether the query has a non-empty `orderBy`. */
|
|
37
|
+
export declare function validateLimitBy(kit: CalculationValidationKit, input: unknown, query: CalculationQueryShape, ordered: boolean): DataRecord;
|
|
38
|
+
/** Validates `other`; `outputs` are calculation output names, which it may not rank by. */
|
|
39
|
+
export declare function validateOtherBucket(kit: CalculationValidationKit, input: unknown, query: CalculationQueryShape, outputs: readonly string[]): DataRecord;
|
|
40
|
+
export {};
|
|
41
|
+
//# sourceMappingURL=validate-calculations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-calculations.d.ts","sourceRoot":"","sources":["../../src/expressions/validate-calculations.ts"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