@hypequery/deployment 0.13.0 → 0.15.1
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/authorized-contract.d.ts +5 -4
- package/dist/authorized-contract.d.ts.map +1 -1
- package/dist/authorized-contract.js +10 -8
- package/dist/bundle.d.ts +2 -2
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/semantic-calculation-validation.d.ts +28 -0
- package/dist/semantic-calculation-validation.d.ts.map +1 -0
- package/dist/semantic-calculation-validation.js +110 -0
- package/dist/semantic-data-plane.d.ts +25 -8
- package/dist/semantic-data-plane.d.ts.map +1 -1
- package/dist/semantic-data-plane.js +44 -18
- package/dist/semantic-event-operation-validation.d.ts +25 -0
- package/dist/semantic-event-operation-validation.d.ts.map +1 -0
- package/dist/semantic-event-operation-validation.js +203 -0
- package/dist/semantic-filter-capabilities.d.ts +22 -0
- package/dist/semantic-filter-capabilities.d.ts.map +1 -0
- package/dist/semantic-filter-capabilities.js +38 -0
- package/dist/semantic-invocation-errors.d.ts +4 -3
- package/dist/semantic-invocation-errors.d.ts.map +1 -1
- package/dist/semantic-invocation-errors.js +5 -3
- package/dist/semantic-operation-validation.d.ts +19 -3
- package/dist/semantic-operation-validation.d.ts.map +1 -1
- package/dist/semantic-operation-validation.js +145 -34
- package/dist/utils/event-time-range.d.ts +36 -0
- package/dist/utils/event-time-range.d.ts.map +1 -0
- package/dist/utils/event-time-range.js +116 -0
- package/dist/utils/field-backing-column.d.ts +17 -0
- package/dist/utils/field-backing-column.d.ts.map +1 -0
- package/dist/utils/field-backing-column.js +24 -0
- package/dist/utils/filter-leaves.d.ts +22 -0
- package/dist/utils/filter-leaves.d.ts.map +1 -0
- package/dist/utils/filter-leaves.js +43 -0
- package/dist/utils/semantic-comparison-field.d.ts +17 -0
- package/dist/utils/semantic-comparison-field.d.ts.map +1 -0
- package/dist/utils/semantic-comparison-field.js +19 -0
- package/dist/utils/time-series-range.d.ts +17 -0
- package/dist/utils/time-series-range.d.ts.map +1 -0
- package/dist/utils/time-series-range.js +32 -0
- package/dist/utils/visibility-projection.d.ts +2 -2
- package/dist/utils/visibility-projection.d.ts.map +1 -1
- package/dist/utils/visibility-projection.js +29 -6
- package/dist/utils/visible-measure-selection.d.ts +2 -2
- package/dist/utils/visible-measure-selection.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract-driven validation of RFC 0027 event operations (funnel, retention).
|
|
3
|
+
*
|
|
4
|
+
* The protocol validator already checked syntax, closed records and
|
|
5
|
+
* structural limits. What remains depends on the contract and the caller:
|
|
6
|
+
* whether the dataset declares events, whether every field exists and may be
|
|
7
|
+
* used by this principal, the bounded time range, and the row budget.
|
|
8
|
+
*/
|
|
9
|
+
import { PROTOCOL_EVENT_OPERATION_LIMITS, parseProtocolRelativeDateRange, } from '@hypequery/protocol';
|
|
10
|
+
import { resolveSemanticAllowedFields, semanticComparisonField, } from './semantic-operation-validation.js';
|
|
11
|
+
import { protocolFilterCapabilities } from './semantic-filter-capabilities.js';
|
|
12
|
+
import { effectiveFilterOperators, protocolFilterLeaves } from './utils/filter-leaves.js';
|
|
13
|
+
import { estimateEventBuckets, estimatePlacementFreeBuckets, eventRangeDays, eventTimeRange, } from './utils/event-time-range.js';
|
|
14
|
+
function isTimeDimension(dataset, name) {
|
|
15
|
+
return dataset.dimensions.some(dimension => String(dimension.name) === name && dimension.type === 'timestamp');
|
|
16
|
+
}
|
|
17
|
+
/** The dimension a filter name addresses: a declared alias's field, or the name itself. */
|
|
18
|
+
function filterTarget(dataset, name) {
|
|
19
|
+
const declared = protocolFilterCapabilities(dataset).named.get(name);
|
|
20
|
+
return declared === undefined ? name : String(declared.field);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One comparison leaf of a filter or condition tree (RFC 0029), checked as a
|
|
24
|
+
* dataset query's is: a plain `field <operator> literal`, on a field the
|
|
25
|
+
* caller may filter, with the operator it applies once `not` is pushed down.
|
|
26
|
+
*/
|
|
27
|
+
function checkLeaf(leaf, allowed, fail) {
|
|
28
|
+
const comparison = semanticComparisonField(leaf.expression);
|
|
29
|
+
if (comparison === null) {
|
|
30
|
+
fail('Only a field/operator/value comparison may be filtered on.', leaf.path);
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const operators = allowed.filters.get(comparison.field);
|
|
34
|
+
if (operators === undefined) {
|
|
35
|
+
fail(`Unknown or unfilterable field "${comparison.field}".`, leaf.path);
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const refused = effectiveFilterOperators(comparison.operator, leaf.negated).find(operator => !operators.has(operator));
|
|
39
|
+
if (refused !== undefined) {
|
|
40
|
+
fail(refused === comparison.operator
|
|
41
|
+
? `Operator "${comparison.operator}" is not allowed on "${comparison.field}".`
|
|
42
|
+
: `Operator "${refused}" (the negation of "${comparison.operator}") is not allowed on "${comparison.field}".`, `${leaf.path}.operator`);
|
|
43
|
+
}
|
|
44
|
+
return comparison;
|
|
45
|
+
}
|
|
46
|
+
/** Where a relative range's worst-case span is placed to count its buckets: a month start. */
|
|
47
|
+
const RELATIVE_ANCHOR_MS = Date.UTC(2001, 0, 1);
|
|
48
|
+
const UNIT_MAX_DAYS = { day: 1, week: 7, month: 31, quarter: 92, year: 366 };
|
|
49
|
+
/**
|
|
50
|
+
* The longest span a relative range (RFC 0029) can cover, without a clock:
|
|
51
|
+
* the executor resolves it against its own, so the data plane bounds it.
|
|
52
|
+
*/
|
|
53
|
+
function relativeRangeDays(value) {
|
|
54
|
+
const range = parseProtocolRelativeDateRange(value);
|
|
55
|
+
if (range === undefined)
|
|
56
|
+
return undefined;
|
|
57
|
+
if (range.kind === 'today' || range.kind === 'yesterday')
|
|
58
|
+
return 1;
|
|
59
|
+
return (range.kind === 'last' ? range.count : 1) * UNIT_MAX_DAYS[range.unit];
|
|
60
|
+
}
|
|
61
|
+
/** The condition lists an operation tests events against. */
|
|
62
|
+
function conditionLists(operation) {
|
|
63
|
+
if (operation.kind === 'retention') {
|
|
64
|
+
return [
|
|
65
|
+
{ path: '$.operation.start.where', where: operation.start.where },
|
|
66
|
+
{ path: '$.operation.return.where', where: operation.return.where },
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
return operation.steps.map((step, index) => ({ path: `$.operation.steps[${index}].where`, where: step.where }));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Rows the operation can return, before execution, from its range and
|
|
73
|
+
* options: cohorts × steps (funnel) or cohorts × periods (retention), per
|
|
74
|
+
* breakdown group.
|
|
75
|
+
*/
|
|
76
|
+
function estimatedRows(operation, range, relativeDays) {
|
|
77
|
+
const groups = operation.breakdown === undefined
|
|
78
|
+
? 1
|
|
79
|
+
: operation.breakdownLimit ?? PROTOCOL_EVENT_OPERATION_LIMITS.defaultBreakdownLimit;
|
|
80
|
+
// A relative range is placed by the executor's clock, so its buckets are
|
|
81
|
+
// counted for the worst placement rather than for the anchored span.
|
|
82
|
+
const buckets = (grain) => (relativeDays === undefined
|
|
83
|
+
? estimateEventBuckets(range, grain)
|
|
84
|
+
: estimatePlacementFreeBuckets(relativeDays, grain));
|
|
85
|
+
if (operation.kind === 'retention') {
|
|
86
|
+
return buckets(operation.grain) * operation.periods * groups;
|
|
87
|
+
}
|
|
88
|
+
const cohorts = operation.by === undefined ? 1 : buckets(operation.by);
|
|
89
|
+
return cohorts * groups * operation.steps.length;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Validates one RFC 0027 operation against the contract that will serve it.
|
|
93
|
+
* Returns every violation, so an agent correcting a call sees the whole
|
|
94
|
+
* problem in one round trip. Hidden fields fail exactly like unknown ones.
|
|
95
|
+
*/
|
|
96
|
+
export function validateSemanticEventOperation(operation, dataset, datasets, limits, visible) {
|
|
97
|
+
const violations = [];
|
|
98
|
+
const fail = (message, path) => violations.push({ message, path });
|
|
99
|
+
if (dataset.events === undefined) {
|
|
100
|
+
fail(`Dataset "${String(dataset.name)}" does not declare events, so it has no ${operation.kind}.`, '$.operation.dataset');
|
|
101
|
+
return violations;
|
|
102
|
+
}
|
|
103
|
+
const allowed = resolveSemanticAllowedFields(dataset, datasets, visible);
|
|
104
|
+
const timeField = dataset.timeField === undefined ? undefined : String(dataset.timeField);
|
|
105
|
+
// Entity and breakdown values are grouped on, so they follow grouping rules;
|
|
106
|
+
// a relationship-qualified name or a time dimension is never one.
|
|
107
|
+
const groupable = (name) => !name.includes('.') && allowed.groupable.has(name) && !isTimeDimension(dataset, name);
|
|
108
|
+
const entity = String(operation.entity ?? dataset.events.entity);
|
|
109
|
+
if (!groupable(entity)) {
|
|
110
|
+
fail(`Unknown or non-groupable entity "${entity}".`, operation.entity === undefined ? '$.operation.dataset' : '$.operation.entity');
|
|
111
|
+
}
|
|
112
|
+
// The executor groups by the entity and every breakdown dimension, so they
|
|
113
|
+
// share the dataset's dimension ceiling as a dataset query's would.
|
|
114
|
+
if (1 + (operation.breakdown?.length ?? 0) > limits.maxDimensions) {
|
|
115
|
+
fail(`At most ${limits.maxDimensions} grouping fields (the entity and breakdown dimensions) may be used.`, operation.breakdown === undefined ? '$.operation' : '$.operation.breakdown');
|
|
116
|
+
}
|
|
117
|
+
(operation.breakdown ?? []).forEach((name, index) => {
|
|
118
|
+
if (!groupable(String(name))) {
|
|
119
|
+
fail(`Unknown or non-groupable dimension "${String(name)}".`, `$.operation.breakdown[${index}]`);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
// Conditions select which events count as a step; the time range belongs
|
|
123
|
+
// in `filters`, so a condition on the time field is refused. Conditions and
|
|
124
|
+
// filters may be and/or/not trees (RFC 0029); the budget counts leaves.
|
|
125
|
+
let leaves = 0;
|
|
126
|
+
for (const list of conditionLists(operation)) {
|
|
127
|
+
list.where.forEach((expression, index) => {
|
|
128
|
+
for (const leaf of protocolFilterLeaves(expression, `${list.path}[${index}]`)) {
|
|
129
|
+
leaves += 1;
|
|
130
|
+
const comparison = checkLeaf(leaf, allowed, fail);
|
|
131
|
+
if (comparison !== undefined
|
|
132
|
+
&& (comparison.field.includes('.') || filterTarget(dataset, comparison.field) === timeField)) {
|
|
133
|
+
fail(`Conditions may not use "${comparison.field}"; put the time range in filters.`, leaf.path);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
const isOnAxis = (leaf) => {
|
|
139
|
+
const comparison = semanticComparisonField(leaf.expression);
|
|
140
|
+
return comparison !== null && timeField !== undefined && filterTarget(dataset, comparison.field) === timeField;
|
|
141
|
+
};
|
|
142
|
+
const filterLeaves = operation.filters.flatMap((expression, index) => (protocolFilterLeaves(expression, `$.operation.filters[${index}]`)));
|
|
143
|
+
for (const leaf of filterLeaves) {
|
|
144
|
+
leaves += 1;
|
|
145
|
+
const comparison = checkLeaf(leaf, allowed, fail);
|
|
146
|
+
// RFC 0027: events are read from the dataset alone, so a relationship-
|
|
147
|
+
// qualified field never filters them, though dataset queries may join.
|
|
148
|
+
if (comparison?.field.includes('.')) {
|
|
149
|
+
fail(`Filter "${comparison.field}" is relationship-qualified; event operations filter only the dataset's own fields.`, leaf.path);
|
|
150
|
+
}
|
|
151
|
+
if (comparison?.operator === 'inDateRange' && !isOnAxis(leaf)) {
|
|
152
|
+
fail(`"inDateRange" applies only to the time field "${timeField ?? ''}".`, leaf.path);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (leaves > limits.maxFilters) {
|
|
156
|
+
fail(`At most ${limits.maxFilters} filters and conditions may be applied together.`, '$.operation');
|
|
157
|
+
}
|
|
158
|
+
// The bounded range is made of top-level comparisons on the time field
|
|
159
|
+
// (RFC 0029). A time comparison under "or"/"not" is kept as an event filter
|
|
160
|
+
// but bounds nothing (RFC 0027), so it neither counts toward the range nor
|
|
161
|
+
// refuses the call. A relative range resolves at the executor, so its span
|
|
162
|
+
// is bounded here without a clock.
|
|
163
|
+
const subject = operation.kind === 'funnel' ? 'Funnels' : 'Retention operations';
|
|
164
|
+
const topLevel = filterLeaves.filter(leaf => !leaf.nested && isOnAxis(leaf));
|
|
165
|
+
const operators = topLevel.map(leaf => semanticComparisonField(leaf.expression)?.operator);
|
|
166
|
+
const bounded = (operators.length === 1 && (operators[0] === 'between' || operators[0] === 'inDateRange'))
|
|
167
|
+
|| (operators.length === 2
|
|
168
|
+
&& operators.some(operator => operator === 'gt' || operator === 'gte')
|
|
169
|
+
&& operators.some(operator => operator === 'lt' || operator === 'lte'));
|
|
170
|
+
const shape = timeField === undefined
|
|
171
|
+
? { message: `${subject} require a time field.`, path: '$.operation.filters' }
|
|
172
|
+
: bounded
|
|
173
|
+
? undefined
|
|
174
|
+
: {
|
|
175
|
+
message: `${subject} require exactly one bounded range on "${timeField}" as top-level filters: `
|
|
176
|
+
+ 'between, inDateRange, or gt/gte with lt/lte.',
|
|
177
|
+
path: '$.operation.filters',
|
|
178
|
+
};
|
|
179
|
+
const relative = topLevel.length === 1 && semanticComparisonField(topLevel[0].expression)?.operator === 'inDateRange'
|
|
180
|
+
? relativeRangeDays(topLevel[0].expression.right?.value)
|
|
181
|
+
: undefined;
|
|
182
|
+
const timezone = operation.timezone ?? 'UTC';
|
|
183
|
+
const range = shape !== undefined
|
|
184
|
+
? undefined
|
|
185
|
+
: relative !== undefined
|
|
186
|
+
? { startMs: RELATIVE_ANCHOR_MS, endMs: RELATIVE_ANCHOR_MS + relative * 86_400_000, timezone }
|
|
187
|
+
: eventTimeRange(topLevel.map(leaf => leaf.expression), () => true, timezone);
|
|
188
|
+
if (range === undefined) {
|
|
189
|
+
fail(shape?.message
|
|
190
|
+
?? `A ${operation.kind} requires exactly one bounded time range on "${timeField}": `
|
|
191
|
+
+ 'between, inDateRange, or gt/gte with lt/lte, using ISO timestamps.', shape?.path ?? '$.operation.filters');
|
|
192
|
+
return violations;
|
|
193
|
+
}
|
|
194
|
+
if (eventRangeDays(range) > PROTOCOL_EVENT_OPERATION_LIMITS.maxRangeDays) {
|
|
195
|
+
fail(`The time range may span at most ${PROTOCOL_EVENT_OPERATION_LIMITS.maxRangeDays} days.`, '$.operation.filters');
|
|
196
|
+
}
|
|
197
|
+
const rows = estimatedRows(operation, range, relative);
|
|
198
|
+
if (rows > limits.maxRows) {
|
|
199
|
+
fail(`This ${operation.kind} could return ${rows} rows; the limit is ${limits.maxRows}. `
|
|
200
|
+
+ 'Narrow the time range, use a coarser grain, or lower breakdownLimit.', '$.operation');
|
|
201
|
+
}
|
|
202
|
+
return violations;
|
|
203
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ProtocolDatasetFilter, ProtocolDeploymentDatasetAnyVersion } from '@hypequery/protocol';
|
|
2
|
+
/** The dataset layer's default caller-filter operators; null checks are opt-in. */
|
|
3
|
+
export declare const DEFAULT_SEMANTIC_FILTER_OPERATORS: ReadonlySet<string>;
|
|
4
|
+
export interface ProtocolFilterCapabilities {
|
|
5
|
+
/** Every declared filter, including aliases. */
|
|
6
|
+
readonly named: ReadonlyMap<string, ProtocolDatasetFilter>;
|
|
7
|
+
/** Dimension-name filters exactly as the dataset layer's effectiveFilters exposes them. */
|
|
8
|
+
readonly dimensions: ReadonlyMap<string, ReadonlySet<string>>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the filter surface serialized in a deployment contract using the
|
|
12
|
+
* same rules as datasets' effectiveFilters:
|
|
13
|
+
*
|
|
14
|
+
* - a declared filter remains reachable by its own name;
|
|
15
|
+
* - a same-named declaration defines a dimension filter, even when the
|
|
16
|
+
* dimension itself is not filterable, provided it addresses that dimension;
|
|
17
|
+
* - another declaration on the dimension's field narrows its implied
|
|
18
|
+
* operators; and
|
|
19
|
+
* - otherwise a filterable dimension gets the value-operator defaults.
|
|
20
|
+
*/
|
|
21
|
+
export declare function protocolFilterCapabilities(dataset: ProtocolDeploymentDatasetAnyVersion): ProtocolFilterCapabilities;
|
|
22
|
+
//# sourceMappingURL=semantic-filter-capabilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"semantic-filter-capabilities.d.ts","sourceRoot":"","sources":["../src/semantic-filter-capabilities.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,mCAAmC,EAAE,MAAM,qBAAqB,CAAC;AAEtG,mFAAmF;AACnF,eAAO,MAAM,iCAAiC,EAAE,WAAW,CAAC,MAAM,CAEhE,CAAC;AAEH,MAAM,WAAW,0BAA0B;IACzC,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAC;IAC3D,2FAA2F;IAC3F,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;CAC/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,mCAAmC,GAC3C,0BAA0B,CAyB5B"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { datasetAllowedFilters } from '@hypequery/protocol';
|
|
2
|
+
/** The dataset layer's default caller-filter operators; null checks are opt-in. */
|
|
3
|
+
export const DEFAULT_SEMANTIC_FILTER_OPERATORS = new Set([
|
|
4
|
+
'eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'in', 'notIn', 'between', 'like',
|
|
5
|
+
]);
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the filter surface serialized in a deployment contract using the
|
|
8
|
+
* same rules as datasets' effectiveFilters:
|
|
9
|
+
*
|
|
10
|
+
* - a declared filter remains reachable by its own name;
|
|
11
|
+
* - a same-named declaration defines a dimension filter, even when the
|
|
12
|
+
* dimension itself is not filterable, provided it addresses that dimension;
|
|
13
|
+
* - another declaration on the dimension's field narrows its implied
|
|
14
|
+
* operators; and
|
|
15
|
+
* - otherwise a filterable dimension gets the value-operator defaults.
|
|
16
|
+
*/
|
|
17
|
+
export function protocolFilterCapabilities(dataset) {
|
|
18
|
+
const allowList = datasetAllowedFilters(dataset);
|
|
19
|
+
const named = new Map(allowList.map(filter => [String(filter.name), filter]));
|
|
20
|
+
const dimensions = new Map();
|
|
21
|
+
for (const dimension of dataset.dimensions) {
|
|
22
|
+
const name = String(dimension.name);
|
|
23
|
+
const sameNamed = named.get(name);
|
|
24
|
+
if (sameNamed) {
|
|
25
|
+
if (String(sameNamed.field) === name) {
|
|
26
|
+
dimensions.set(name, new Set(sameNamed.operators));
|
|
27
|
+
}
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (!dimension.filterable)
|
|
31
|
+
continue;
|
|
32
|
+
const onField = allowList.filter(filter => String(filter.field) === name);
|
|
33
|
+
dimensions.set(name, onField.length > 0
|
|
34
|
+
? new Set(onField.flatMap(filter => filter.operators))
|
|
35
|
+
: DEFAULT_SEMANTIC_FILTER_OPERATORS);
|
|
36
|
+
}
|
|
37
|
+
return { named, dimensions };
|
|
38
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ProtocolSemanticInvocationFailure, type ProtocolSemanticInvocationFailureCategory } from '@hypequery/protocol';
|
|
2
2
|
/** A failure that already carries the public category a caller should see. */
|
|
3
3
|
export declare class DeploymentSemanticInvocationError extends Error {
|
|
4
4
|
readonly category: ProtocolSemanticInvocationFailureCategory;
|
|
@@ -22,8 +22,9 @@ export declare function fail(category: ProtocolSemanticInvocationFailureCategory
|
|
|
22
22
|
/**
|
|
23
23
|
* Projects a failure onto the portable record.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
25
|
+
* Public messages are fixed by the protocol. The internal error message is
|
|
26
|
+
* still useful to the deployment's private logs, but copying it into this
|
|
27
|
+
* record would make an otherwise valid failure fail protocol validation.
|
|
27
28
|
*/
|
|
28
29
|
export declare function toProtocolSemanticInvocationFailure(error: unknown, activationRevision?: string): ProtocolSemanticInvocationFailure;
|
|
29
30
|
export declare function throwIfAborted(signal: AbortSignal | undefined): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"semantic-invocation-errors.d.ts","sourceRoot":"","sources":["../src/semantic-invocation-errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"semantic-invocation-errors.d.ts","sourceRoot":"","sources":["../src/semantic-invocation-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,iCAAiC,EACtC,KAAK,yCAAyC,EAC/C,MAAM,qBAAqB,CAAC;AAE7B,8EAA8E;AAC9E,qBAAa,iCAAkC,SAAQ,KAAK;IAC1D,QAAQ,CAAC,QAAQ,EAAE,yCAAyC,CAAC;IAC7D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;gBAGvB,QAAQ,EAAE,yCAAyC,EACnD,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,GAAE;QACP,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;QACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;QAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;KACtB;CAUT;AAED,wBAAgB,IAAI,CAClB,QAAQ,EAAE,yCAAyC,EACnD,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,GAAE;IACP,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CACtB,GACL,KAAK,CAEP;AAED;;;;;;GAMG;AACH,wBAAgB,mCAAmC,CACjD,KAAK,EAAE,OAAO,EACd,kBAAkB,CAAC,EAAE,MAAM,GAC1B,iCAAiC,CAenC;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI,CAIpE"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PROTOCOL_SEMANTIC_FAILURE_MESSAGES, } from '@hypequery/protocol';
|
|
1
2
|
/** A failure that already carries the public category a caller should see. */
|
|
2
3
|
export class DeploymentSemanticInvocationError extends Error {
|
|
3
4
|
category;
|
|
@@ -21,8 +22,9 @@ export function fail(category, code, message, options = {}) {
|
|
|
21
22
|
/**
|
|
22
23
|
* Projects a failure onto the portable record.
|
|
23
24
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
25
|
+
* Public messages are fixed by the protocol. The internal error message is
|
|
26
|
+
* still useful to the deployment's private logs, but copying it into this
|
|
27
|
+
* record would make an otherwise valid failure fail protocol validation.
|
|
26
28
|
*/
|
|
27
29
|
export function toProtocolSemanticInvocationFailure(error, activationRevision) {
|
|
28
30
|
const known = error instanceof DeploymentSemanticInvocationError;
|
|
@@ -31,7 +33,7 @@ export function toProtocolSemanticInvocationFailure(error, activationRevision) {
|
|
|
31
33
|
version: 1,
|
|
32
34
|
category: known ? error.category : 'executor-failed',
|
|
33
35
|
code: known ? error.code : 'HQ_SEMANTIC_EXECUTION_FAILED',
|
|
34
|
-
message: known ? error.
|
|
36
|
+
message: PROTOCOL_SEMANTIC_FAILURE_MESSAGES[known ? error.category : 'executor-failed'],
|
|
35
37
|
...(known && error.path !== undefined ? { path: error.path } : {}),
|
|
36
38
|
retryable: known ? error.retryable : false,
|
|
37
39
|
relist: known ? error.relist : false,
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* loading the authoring package. A gateway must be able to reject a bad
|
|
7
7
|
* invocation without any of the deploying application's code.
|
|
8
8
|
*/
|
|
9
|
-
import type {
|
|
9
|
+
import type { ProtocolDeploymentDatasetAnyVersion, ProtocolExpression, ProtocolSemanticQuery, ProtocolVisibleDatasetFields } from '@hypequery/protocol';
|
|
10
10
|
export interface SemanticOperationViolation {
|
|
11
11
|
readonly message: string;
|
|
12
12
|
readonly path: string;
|
|
@@ -25,7 +25,7 @@ export interface SemanticOperationLimits {
|
|
|
25
25
|
* Returns every violation rather than the first, so a caller correcting an
|
|
26
26
|
* agent-authored query sees the whole problem in one round trip.
|
|
27
27
|
*/
|
|
28
|
-
export declare function validateSemanticOperation(operation: ProtocolSemanticQuery, dataset:
|
|
28
|
+
export declare function validateSemanticOperation(operation: ProtocolSemanticQuery, dataset: ProtocolDeploymentDatasetAnyVersion, datasets: ReadonlyMap<string, ProtocolDeploymentDatasetAnyVersion>, limits: SemanticOperationLimits,
|
|
29
29
|
/** What the principal may see; omitted means everything the contract publishes. */
|
|
30
30
|
visible?: ProtocolVisibleDatasetFields): readonly SemanticOperationViolation[];
|
|
31
31
|
/**
|
|
@@ -34,5 +34,21 @@ visible?: ProtocolVisibleDatasetFields): readonly SemanticOperationViolation[];
|
|
|
34
34
|
* publishes, not against the caller's visibility: a provider may constrain on
|
|
35
35
|
* a field the caller can't see.
|
|
36
36
|
*/
|
|
37
|
-
export declare function validateSemanticConstraints(constraints: readonly ProtocolExpression[], dataset:
|
|
37
|
+
export declare function validateSemanticConstraints(constraints: readonly ProtocolExpression[], dataset: ProtocolDeploymentDatasetAnyVersion, datasets: ReadonlyMap<string, ProtocolDeploymentDatasetAnyVersion>): readonly SemanticOperationViolation[];
|
|
38
|
+
/** What a principal may group and filter by on a dataset (RFC 0027 reuses this). */
|
|
39
|
+
export interface SemanticAllowedFields {
|
|
40
|
+
readonly groupable: ReadonlySet<string>;
|
|
41
|
+
/** Filter name -> allowed operators. */
|
|
42
|
+
readonly filters: ReadonlyMap<string, ReadonlySet<string>>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The dataset's groupable and filterable fields, narrowed to what the
|
|
46
|
+
* principal may see, by exactly the rules dataset queries use.
|
|
47
|
+
*/
|
|
48
|
+
export declare function resolveSemanticAllowedFields(dataset: ProtocolDeploymentDatasetAnyVersion, datasets: ReadonlyMap<string, ProtocolDeploymentDatasetAnyVersion>, visible?: ProtocolVisibleDatasetFields): SemanticAllowedFields;
|
|
49
|
+
/** The field and operator of a plain `field <operator> literal` comparison, else null. */
|
|
50
|
+
export declare function semanticComparisonField(expression: ProtocolExpression): {
|
|
51
|
+
field: string;
|
|
52
|
+
operator: string;
|
|
53
|
+
} | null;
|
|
38
54
|
//# sourceMappingURL=semantic-operation-validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"semantic-operation-validation.d.ts","sourceRoot":"","sources":["../src/semantic-operation-validation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"semantic-operation-validation.d.ts","sourceRoot":"","sources":["../src/semantic-operation-validation.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EACV,mCAAmC,EACnC,kBAAkB,EAElB,qBAAqB,EACrB,4BAA4B,EAC7B,MAAM,qBAAqB,CAAC;AAY7B,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,uBAAuB;IACtC,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAkGD;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,qBAAqB,EAChC,OAAO,EAAE,mCAAmC,EAC5C,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,mCAAmC,CAAC,EAClE,MAAM,EAAE,uBAAuB;AAC/B,mFAAmF;AACnF,OAAO,CAAC,EAAE,4BAA4B,GACrC,SAAS,0BAA0B,EAAE,CAkNvC;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,SAAS,kBAAkB,EAAE,EAC1C,OAAO,EAAE,mCAAmC,EAC5C,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,mCAAmC,CAAC,GACjE,SAAS,0BAA0B,EAAE,CAgBvC;AAED,oFAAoF;AACpF,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACxC,wCAAwC;IACxC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;CAC5D;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,mCAAmC,EAC5C,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,mCAAmC,CAAC,EAClE,OAAO,CAAC,EAAE,4BAA4B,GACrC,qBAAqB,CAIvB;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,kBAAkB,GAC7B;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAE5C"}
|
|
@@ -6,7 +6,15 @@
|
|
|
6
6
|
* loading the authoring package. A gateway must be able to reject a bad
|
|
7
7
|
* invocation without any of the deploying application's code.
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
import { datasetSegments, datasetTimeAxes } from '@hypequery/protocol';
|
|
10
|
+
import { protocolFilterCapabilities } from './semantic-filter-capabilities.js';
|
|
11
|
+
import { comparisonField } from './utils/semantic-comparison-field.js';
|
|
12
|
+
import { calculationConditionCount, calculationOutputNames, validateSemanticCalculations, } from './semantic-calculation-validation.js';
|
|
13
|
+
import { effectiveFilterOperators, protocolFilterLeaves } from './utils/filter-leaves.js';
|
|
14
|
+
import { timeSeriesRangeViolation } from './utils/time-series-range.js';
|
|
15
|
+
import { fieldBackingColumn, isTimeAxisVisible } from './utils/field-backing-column.js';
|
|
16
|
+
/** Invocation 1 refuses `minute` and `hour` before they reach this check (RFC 0015). */
|
|
17
|
+
const ALL_GRAINS = ['minute', 'hour', 'day', 'week', 'month', 'quarter', 'year'];
|
|
10
18
|
/**
|
|
11
19
|
* What a dataset exposes.
|
|
12
20
|
*
|
|
@@ -18,17 +26,29 @@ const ALL_GRAINS = ['day', 'week', 'month', 'quarter', 'year'];
|
|
|
18
26
|
function resolveDataset(dataset, datasets) {
|
|
19
27
|
const groupable = new Set();
|
|
20
28
|
const filters = new Map();
|
|
29
|
+
const impliedFilters = new Set();
|
|
30
|
+
const local = protocolFilterCapabilities(dataset);
|
|
21
31
|
for (const dimension of dataset.dimensions) {
|
|
22
32
|
if (dimension.groupable)
|
|
23
33
|
groupable.add(String(dimension.name));
|
|
24
34
|
}
|
|
25
|
-
for (const
|
|
26
|
-
filters.set(
|
|
35
|
+
for (const [name, operators] of local.dimensions) {
|
|
36
|
+
filters.set(name, operators);
|
|
37
|
+
impliedFilters.add(name);
|
|
38
|
+
}
|
|
39
|
+
for (const [name, filter] of local.named) {
|
|
40
|
+
// A named filter keeps its alias. When its name matches a dimension it
|
|
41
|
+
// replaces that key, exactly as datasets' effectiveFilters does.
|
|
42
|
+
filters.set(name, new Set(filter.operators));
|
|
43
|
+
impliedFilters.delete(name);
|
|
27
44
|
}
|
|
28
45
|
for (const relationship of dataset.relationships) {
|
|
29
46
|
if (!relationship.queryable)
|
|
30
47
|
continue;
|
|
31
48
|
const target = datasets.get(String(relationship.target));
|
|
49
|
+
if (!target)
|
|
50
|
+
continue;
|
|
51
|
+
const targetFilters = protocolFilterCapabilities(target);
|
|
32
52
|
for (const dimension of target?.dimensions ?? []) {
|
|
33
53
|
const qualified = `${String(relationship.name)}.${String(dimension.name)}`;
|
|
34
54
|
// The two capabilities are declared separately and travel separately
|
|
@@ -36,20 +56,20 @@ function resolveDataset(dataset, datasets) {
|
|
|
36
56
|
// cannot be grouped and expose one the target declared unfilterable.
|
|
37
57
|
if (dimension.groupable)
|
|
38
58
|
groupable.add(qualified);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
'eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'in', 'notIn', 'between', 'like',
|
|
44
|
-
]));
|
|
59
|
+
const operators = targetFilters.dimensions.get(String(dimension.name));
|
|
60
|
+
if (operators) {
|
|
61
|
+
filters.set(qualified, operators);
|
|
62
|
+
impliedFilters.add(qualified);
|
|
45
63
|
}
|
|
46
64
|
}
|
|
47
65
|
}
|
|
48
66
|
return {
|
|
49
67
|
groupable,
|
|
50
68
|
filters,
|
|
69
|
+
impliedFilters,
|
|
51
70
|
measures: new Set(dataset.measures.map(measure => String(measure.name))),
|
|
52
71
|
grains: new Set(dataset.timeField === undefined ? [] : ALL_GRAINS),
|
|
72
|
+
segments: new Set(datasetSegments(dataset).map(segment => String(segment.name))),
|
|
53
73
|
};
|
|
54
74
|
}
|
|
55
75
|
/**
|
|
@@ -61,30 +81,16 @@ function resolveDataset(dataset, datasets) {
|
|
|
61
81
|
function narrowResolvedToVisible(allowed, visible) {
|
|
62
82
|
return {
|
|
63
83
|
groupable: new Set([...allowed.groupable].filter(name => visible.dimensions.has(name))),
|
|
64
|
-
filters: new Map([...allowed.filters].filter(([name]) => (visible.filters.has(name)
|
|
84
|
+
filters: new Map([...allowed.filters].filter(([name]) => (visible.filters.has(name)
|
|
85
|
+
|| (allowed.impliedFilters.has(name) && visible.dimensions.has(name))))),
|
|
86
|
+
impliedFilters: new Set([...allowed.impliedFilters].filter(name => visible.dimensions.has(name))),
|
|
65
87
|
measures: new Set([...allowed.measures].filter(name => visible.measures.has(name))),
|
|
66
88
|
grains: visible.timeField ? allowed.grains : new Set(),
|
|
89
|
+
segments: visible.segments === undefined
|
|
90
|
+
? allowed.segments
|
|
91
|
+
: new Set([...allowed.segments].filter(name => visible.segments.has(name))),
|
|
67
92
|
};
|
|
68
93
|
}
|
|
69
|
-
/**
|
|
70
|
-
* The field a filter expression addresses, or null when it is not a plain
|
|
71
|
-
* comparison.
|
|
72
|
-
*
|
|
73
|
-
* Both operands are checked. A right-hand side that is anything but a literal —
|
|
74
|
-
* another reference, an aggregate, a nested comparison — addresses something
|
|
75
|
-
* this validator never matched against the contract's allowlist, so accepting
|
|
76
|
-
* it would leave a published-surface check to whichever executor happens to be
|
|
77
|
-
* injected. It is also a caller mistake rather than a deployment capability
|
|
78
|
-
* gap, and only a violation raised here reports it as one.
|
|
79
|
-
*/
|
|
80
|
-
function comparisonField(expression) {
|
|
81
|
-
if (expression.kind !== 'comparison' || expression.left.kind !== 'reference')
|
|
82
|
-
return null;
|
|
83
|
-
// A null check (RFC 0023) has no right operand; any other comparison needs a literal one.
|
|
84
|
-
if ('right' in expression && expression.right.kind !== 'literal')
|
|
85
|
-
return null;
|
|
86
|
-
return { field: String(expression.left.name), operator: expression.operator };
|
|
87
|
-
}
|
|
88
94
|
/**
|
|
89
95
|
* Validates one semantic operation against the contract that will serve it.
|
|
90
96
|
*
|
|
@@ -120,6 +126,11 @@ visible) {
|
|
|
120
126
|
if (measures.length > limits.maxMeasures) {
|
|
121
127
|
fail(`At most ${limits.maxMeasures} measures may be selected.`, '$.operation.measures');
|
|
122
128
|
}
|
|
129
|
+
// RFC 0026: every calculation output is a column, as a selected measure is.
|
|
130
|
+
const outputs = calculationOutputNames(operation);
|
|
131
|
+
if (measures.length <= limits.maxMeasures && measures.length + outputs.length > limits.maxMeasures) {
|
|
132
|
+
fail(`At most ${limits.maxMeasures} measures and calculation outputs may be selected together.`, '$.operation.calculations');
|
|
133
|
+
}
|
|
123
134
|
measures.forEach((name, index) => {
|
|
124
135
|
if (!allowed.measures.has(String(name))) {
|
|
125
136
|
fail(`Unknown measure "${String(name)}".`, `$.operation.measures[${index}]`);
|
|
@@ -128,7 +139,9 @@ visible) {
|
|
|
128
139
|
if (dimensions.length === 0 && measures.length === 0) {
|
|
129
140
|
fail('At least one dimension or measure must be selected.', '$.operation');
|
|
130
141
|
}
|
|
131
|
-
|
|
142
|
+
// RFC 0029: the budget counts comparisons. A filter may be an and/or/not
|
|
143
|
+
// tree; each leaf is checked as a top-level comparison is.
|
|
144
|
+
const filters = (operation.filters ?? []).flatMap((expression, index) => (protocolFilterLeaves(expression, `$.operation.filters[${index}]`)));
|
|
132
145
|
if (filters.length > limits.maxFilters) {
|
|
133
146
|
fail(`At most ${limits.maxFilters} filters may be applied.`, '$.operation.filters');
|
|
134
147
|
}
|
|
@@ -140,8 +153,19 @@ visible) {
|
|
|
140
153
|
if (filters.length <= limits.maxFilters && filters.length + having.length > limits.maxFilters) {
|
|
141
154
|
fail(`At most ${limits.maxFilters} filters and having conditions may be applied together.`, '$.operation.having');
|
|
142
155
|
}
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
// RFC 0026: a filter calculation's conditions share the same budget.
|
|
157
|
+
const conditions = calculationConditionCount(operation);
|
|
158
|
+
if (conditions > 0 && filters.length + having.length <= limits.maxFilters
|
|
159
|
+
&& filters.length + having.length + conditions > limits.maxFilters) {
|
|
160
|
+
fail(`At most ${limits.maxFilters} filters, having and filter-calculation conditions may be applied together.`, '$.operation.calculations');
|
|
161
|
+
}
|
|
162
|
+
for (const violation of validateSemanticCalculations(operation, dataset, {
|
|
163
|
+
measures: allowed.measures,
|
|
164
|
+
filters: allowed.filters,
|
|
165
|
+
hasTimeField: allowed.grains.size > 0,
|
|
166
|
+
}))
|
|
167
|
+
fail(violation.message, violation.path);
|
|
168
|
+
filters.forEach(({ expression, path, negated }) => {
|
|
145
169
|
const comparison = comparisonField(expression);
|
|
146
170
|
if (comparison === null) {
|
|
147
171
|
// Only `field <operator> value` is expressible against a contract; a
|
|
@@ -155,14 +179,88 @@ visible) {
|
|
|
155
179
|
fail(`Unknown or unfilterable field "${comparison.field}".`, path);
|
|
156
180
|
return;
|
|
157
181
|
}
|
|
158
|
-
|
|
159
|
-
|
|
182
|
+
// Under `not`, the negation is what runs, so it is what must be allowed.
|
|
183
|
+
const refused = effectiveFilterOperators(comparison.operator, negated)
|
|
184
|
+
.find(operator => !operators.has(operator));
|
|
185
|
+
if (refused !== undefined) {
|
|
186
|
+
fail(refused === comparison.operator
|
|
187
|
+
? `Operator "${comparison.operator}" is not allowed on "${comparison.field}".`
|
|
188
|
+
: `Operator "${refused}" (the negation of "${comparison.operator}") is not allowed on "${comparison.field}".`, `${path}.operator`);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
// RFC 0029: a relative range filters a time axis; the executor resolves it.
|
|
192
|
+
const namedFilters = protocolFilterCapabilities(dataset).named;
|
|
193
|
+
const fieldOf = (leaf) => {
|
|
194
|
+
const comparison = comparisonField(leaf.expression);
|
|
195
|
+
if (comparison === null)
|
|
196
|
+
return undefined;
|
|
197
|
+
return String(namedFilters.get(comparison.field)?.field ?? comparison.field);
|
|
198
|
+
};
|
|
199
|
+
// RFC 0029: the time axes a caller may use, in contract order. A hidden
|
|
200
|
+
// alternate reads exactly like an undeclared one.
|
|
201
|
+
const axes = allowed.grains.size === 0
|
|
202
|
+
? []
|
|
203
|
+
: datasetTimeAxes(dataset)
|
|
204
|
+
.map(String)
|
|
205
|
+
.filter((name, index) => index === 0 || visible === undefined || isTimeAxisVisible(dataset, name, visible.dimensions));
|
|
206
|
+
let timeAxis = axes[0];
|
|
207
|
+
if (operation.timeKey !== undefined) {
|
|
208
|
+
if (axes.includes(String(operation.timeKey))) {
|
|
209
|
+
timeAxis = String(operation.timeKey);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
timeAxis = undefined;
|
|
213
|
+
fail(axes.length === 0
|
|
214
|
+
? 'This dataset has no time field, so a timeKey does not apply.'
|
|
215
|
+
: `Unknown time key "${String(operation.timeKey)}". Available: ${axes.join(', ')}.`, '$.operation.timeKey');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Axes and filters are compared by backing column: an axis may name the
|
|
219
|
+
// column that a differently named dimension reads.
|
|
220
|
+
const columnOf = (leaf) => {
|
|
221
|
+
const field = fieldOf(leaf);
|
|
222
|
+
return field === undefined ? undefined : fieldBackingColumn(dataset, field);
|
|
223
|
+
};
|
|
224
|
+
const axisColumns = new Set(axes.map(axis => fieldBackingColumn(dataset, axis)));
|
|
225
|
+
const selectedColumn = timeAxis === undefined ? undefined : fieldBackingColumn(dataset, timeAxis);
|
|
226
|
+
const onAxis = (leaf) => {
|
|
227
|
+
const column = columnOf(leaf);
|
|
228
|
+
return column !== undefined && column === selectedColumn;
|
|
229
|
+
};
|
|
230
|
+
filters.forEach(leaf => {
|
|
231
|
+
if (comparisonField(leaf.expression)?.operator !== 'inDateRange')
|
|
232
|
+
return;
|
|
233
|
+
const column = columnOf(leaf);
|
|
234
|
+
if (column === undefined || !axisColumns.has(column)) {
|
|
235
|
+
fail(axes.length === 0
|
|
236
|
+
? 'This dataset has no time field, so "inDateRange" does not apply.'
|
|
237
|
+
: `"inDateRange" applies only to a time axis: ${axes.join(', ')}.`, leaf.path);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
// A filled series, like a window or shift measure, needs a bounded range.
|
|
241
|
+
const timeMeasures = new Set(dataset.measures
|
|
242
|
+
.filter(measure => measure.kind === 'window' || measure.kind === 'shift')
|
|
243
|
+
.map(measure => String(measure.name)));
|
|
244
|
+
const series = operation.fill === true
|
|
245
|
+
? 'Filled queries'
|
|
246
|
+
: measures.some(name => timeMeasures.has(String(name))) ? 'Window and shift measures' : undefined;
|
|
247
|
+
if (series !== undefined && timeAxis !== undefined) {
|
|
248
|
+
const violation = timeSeriesRangeViolation(filters, onAxis, timeAxis, series);
|
|
249
|
+
if (violation)
|
|
250
|
+
fail(violation.message, violation.path);
|
|
251
|
+
}
|
|
252
|
+
// RFC 0015: an unknown segment is refused like an unknown dimension, and a
|
|
253
|
+
// hidden one reads exactly like an unknown one.
|
|
254
|
+
(operation.segments ?? []).forEach((name, index) => {
|
|
255
|
+
if (!allowed.segments.has(String(name))) {
|
|
256
|
+
fail(`Unknown segment "${String(name)}".`, `$.operation.segments[${index}]`);
|
|
160
257
|
}
|
|
161
258
|
});
|
|
162
259
|
const orderBy = operation.orderBy ?? [];
|
|
163
260
|
const orderable = new Set([
|
|
164
261
|
...allowed.groupable,
|
|
165
262
|
...allowed.measures,
|
|
263
|
+
...outputs,
|
|
166
264
|
...(operation.by === undefined ? [] : ['period']),
|
|
167
265
|
]);
|
|
168
266
|
orderBy.forEach((entry, index) => {
|
|
@@ -209,3 +307,16 @@ export function validateSemanticConstraints(constraints, dataset, datasets) {
|
|
|
209
307
|
});
|
|
210
308
|
return violations;
|
|
211
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* The dataset's groupable and filterable fields, narrowed to what the
|
|
312
|
+
* principal may see, by exactly the rules dataset queries use.
|
|
313
|
+
*/
|
|
314
|
+
export function resolveSemanticAllowedFields(dataset, datasets, visible) {
|
|
315
|
+
const published = resolveDataset(dataset, datasets);
|
|
316
|
+
const allowed = visible === undefined ? published : narrowResolvedToVisible(published, visible);
|
|
317
|
+
return { groupable: allowed.groupable, filters: allowed.filters };
|
|
318
|
+
}
|
|
319
|
+
/** The field and operator of a plain `field <operator> literal` comparison, else null. */
|
|
320
|
+
export function semanticComparisonField(expression) {
|
|
321
|
+
return comparisonField(expression);
|
|
322
|
+
}
|