@hypequery/datasets 0.32.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/dist/agent-catalog.d.ts +3 -3
  2. package/dist/agent-catalog.d.ts.map +1 -1
  3. package/dist/cache/protocol-cache-keys.d.ts.map +1 -1
  4. package/dist/cache/protocol-cache-keys.js +10 -48
  5. package/dist/cache/query-signature.d.ts.map +1 -1
  6. package/dist/cache/query-signature.js +16 -5
  7. package/dist/catalog.d.ts +5 -1
  8. package/dist/catalog.d.ts.map +1 -1
  9. package/dist/catalog.js +9 -0
  10. package/dist/client-contract/build-from-deployment.d.ts +2 -2
  11. package/dist/client-contract/build-from-deployment.d.ts.map +1 -1
  12. package/dist/client-contract/build-from-deployment.js +7 -0
  13. package/dist/client-contract/dataset-fields.d.ts.map +1 -1
  14. package/dist/client-contract/dataset-fields.js +2 -1
  15. package/dist/cloud-publication.d.ts +3 -3
  16. package/dist/cloud-publication.d.ts.map +1 -1
  17. package/dist/cloud-publishing.d.ts +2 -2
  18. package/dist/cloud-publishing.d.ts.map +1 -1
  19. package/dist/codegen/dataset-source.d.ts.map +1 -1
  20. package/dist/codegen/dataset-source.js +10 -1
  21. package/dist/contract-3-authoring.fixture.d.ts +103 -0
  22. package/dist/contract-3-authoring.fixture.d.ts.map +1 -0
  23. package/dist/contract-3-authoring.fixture.js +40 -0
  24. package/dist/contract.d.ts +5 -1
  25. package/dist/contract.d.ts.map +1 -1
  26. package/dist/contract.js +9 -0
  27. package/dist/dataset-compilation.d.ts +3 -0
  28. package/dist/dataset-compilation.d.ts.map +1 -1
  29. package/dist/dataset-query.d.ts +5 -0
  30. package/dist/dataset-query.d.ts.map +1 -1
  31. package/dist/dataset.d.ts.map +1 -1
  32. package/dist/dataset.js +12 -2
  33. package/dist/event-operations.d.ts +43 -0
  34. package/dist/event-operations.d.ts.map +1 -0
  35. package/dist/event-operations.js +137 -0
  36. package/dist/execution-capabilities.d.ts +16 -3
  37. package/dist/execution-capabilities.d.ts.map +1 -1
  38. package/dist/execution-capabilities.js +5 -3
  39. package/dist/execution-capabilities.json +7 -8
  40. package/dist/executor.d.ts +13 -0
  41. package/dist/executor.d.ts.map +1 -1
  42. package/dist/executor.js +42 -8
  43. package/dist/index.d.ts +7 -3
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +4 -1
  46. package/dist/portable-execution-errors.d.ts +11 -0
  47. package/dist/portable-execution-errors.d.ts.map +1 -1
  48. package/dist/portable-execution-errors.js +14 -0
  49. package/dist/portable-executor.d.ts +22 -5
  50. package/dist/portable-executor.d.ts.map +1 -1
  51. package/dist/portable-executor.js +64 -5
  52. package/dist/protocol-adapter.d.ts +12 -0
  53. package/dist/protocol-adapter.d.ts.map +1 -1
  54. package/dist/protocol-adapter.js +32 -10
  55. package/dist/protocol-deployment-adapter.d.ts +10 -3
  56. package/dist/protocol-deployment-adapter.d.ts.map +1 -1
  57. package/dist/protocol-deployment-adapter.js +60 -26
  58. package/dist/protocol-rehydrate.d.ts +3 -16
  59. package/dist/protocol-rehydrate.d.ts.map +1 -1
  60. package/dist/protocol-rehydrate.js +72 -14
  61. package/dist/query-builder-protocol.d.ts +10 -0
  62. package/dist/query-builder-protocol.d.ts.map +1 -1
  63. package/dist/query-planner.d.ts.map +1 -1
  64. package/dist/query-planner.js +3 -1
  65. package/dist/relationship-check.d.ts.map +1 -1
  66. package/dist/relationship-check.js +2 -0
  67. package/dist/semantic-planner.d.ts.map +1 -1
  68. package/dist/semantic-planner.js +17 -4
  69. package/dist/semantic-query-schema.d.ts +18 -0
  70. package/dist/semantic-query-schema.d.ts.map +1 -1
  71. package/dist/semantic-query-schema.js +79 -55
  72. package/dist/tools.d.ts +6 -0
  73. package/dist/tools.d.ts.map +1 -1
  74. package/dist/types.d.ts +121 -10
  75. package/dist/types.d.ts.map +1 -1
  76. package/dist/utils/agent-catalog-projection.d.ts +2 -12
  77. package/dist/utils/agent-catalog-projection.d.ts.map +1 -1
  78. package/dist/utils/agent-catalog-projection.js +39 -2
  79. package/dist/utils/build-dataset-query-builder.d.ts.map +1 -1
  80. package/dist/utils/build-dataset-query-builder.js +4 -8
  81. package/dist/utils/calculation-stage-sql.d.ts +31 -0
  82. package/dist/utils/calculation-stage-sql.d.ts.map +1 -0
  83. package/dist/utils/calculation-stage-sql.js +98 -0
  84. package/dist/utils/compile-calculated-dataset-query.d.ts +24 -0
  85. package/dist/utils/compile-calculated-dataset-query.d.ts.map +1 -0
  86. package/dist/utils/compile-calculated-dataset-query.js +133 -0
  87. package/dist/utils/compile-dataset-query.d.ts.map +1 -1
  88. package/dist/utils/compile-dataset-query.js +11 -3
  89. package/dist/utils/dataset-definition-validation.d.ts.map +1 -1
  90. package/dist/utils/dataset-definition-validation.js +94 -7
  91. package/dist/utils/dataset-events.d.ts +19 -0
  92. package/dist/utils/dataset-events.d.ts.map +1 -0
  93. package/dist/utils/dataset-events.js +46 -0
  94. package/dist/utils/dataset-having.d.ts.map +1 -1
  95. package/dist/utils/dataset-having.js +6 -1
  96. package/dist/utils/dataset-query-result.d.ts.map +1 -1
  97. package/dist/utils/dataset-query-result.js +7 -1
  98. package/dist/utils/dataset-query-validation.d.ts +1 -1
  99. package/dist/utils/dataset-query-validation.d.ts.map +1 -1
  100. package/dist/utils/dataset-query-validation.js +25 -47
  101. package/dist/utils/dataset-source.d.ts +2 -2
  102. package/dist/utils/dataset-source.d.ts.map +1 -1
  103. package/dist/utils/dataset-source.js +8 -1
  104. package/dist/utils/dataset-validation.d.ts.map +1 -1
  105. package/dist/utils/dataset-validation.js +2 -1
  106. package/dist/utils/dimension-selection-overrides.d.ts +19 -0
  107. package/dist/utils/dimension-selection-overrides.d.ts.map +1 -0
  108. package/dist/utils/dimension-selection-overrides.js +22 -0
  109. package/dist/utils/event-breakdown-sql.d.ts +15 -0
  110. package/dist/utils/event-breakdown-sql.d.ts.map +1 -0
  111. package/dist/utils/event-breakdown-sql.js +17 -0
  112. package/dist/utils/event-condition-sql.d.ts +18 -0
  113. package/dist/utils/event-condition-sql.d.ts.map +1 -0
  114. package/dist/utils/event-condition-sql.js +63 -0
  115. package/dist/utils/event-operation-shape.d.ts +8 -0
  116. package/dist/utils/event-operation-shape.d.ts.map +1 -0
  117. package/dist/utils/event-operation-shape.js +66 -0
  118. package/dist/utils/event-operation-spec.d.ts +43 -0
  119. package/dist/utils/event-operation-spec.d.ts.map +1 -0
  120. package/dist/utils/event-operation-spec.js +68 -0
  121. package/dist/utils/event-range-span.d.ts +4 -0
  122. package/dist/utils/event-range-span.d.ts.map +1 -0
  123. package/dist/utils/event-range-span.js +22 -0
  124. package/dist/utils/event-result-values.d.ts +8 -0
  125. package/dist/utils/event-result-values.d.ts.map +1 -0
  126. package/dist/utils/event-result-values.js +21 -0
  127. package/dist/utils/event-source-sql.d.ts +38 -0
  128. package/dist/utils/event-source-sql.d.ts.map +1 -0
  129. package/dist/utils/event-source-sql.js +71 -0
  130. package/dist/utils/filter-predicate-tree.d.ts +28 -0
  131. package/dist/utils/filter-predicate-tree.d.ts.map +1 -0
  132. package/dist/utils/filter-predicate-tree.js +80 -0
  133. package/dist/utils/filter-tree.d.ts +42 -0
  134. package/dist/utils/filter-tree.d.ts.map +1 -0
  135. package/dist/utils/filter-tree.js +164 -0
  136. package/dist/utils/filtered-aggregation-sql.d.ts.map +1 -1
  137. package/dist/utils/filtered-aggregation-sql.js +22 -1
  138. package/dist/utils/funnel-rows.d.ts +10 -0
  139. package/dist/utils/funnel-rows.d.ts.map +1 -0
  140. package/dist/utils/funnel-rows.js +30 -0
  141. package/dist/utils/funnel-sql.d.ts +18 -0
  142. package/dist/utils/funnel-sql.d.ts.map +1 -0
  143. package/dist/utils/funnel-sql.js +104 -0
  144. package/dist/utils/metric-derived-query.d.ts.map +1 -1
  145. package/dist/utils/metric-derived-query.js +4 -5
  146. package/dist/utils/metric-query-builder.d.ts.map +1 -1
  147. package/dist/utils/metric-query-builder.js +4 -5
  148. package/dist/utils/metric-query-validation.d.ts.map +1 -1
  149. package/dist/utils/metric-query-validation.js +18 -7
  150. package/dist/utils/other-bucket-sql.d.ts +23 -0
  151. package/dist/utils/other-bucket-sql.d.ts.map +1 -0
  152. package/dist/utils/other-bucket-sql.js +51 -0
  153. package/dist/utils/portable-aggregations.d.ts +10 -6
  154. package/dist/utils/portable-aggregations.d.ts.map +1 -1
  155. package/dist/utils/portable-aggregations.js +13 -8
  156. package/dist/utils/portable-grains.d.ts +17 -8
  157. package/dist/utils/portable-grains.d.ts.map +1 -1
  158. package/dist/utils/portable-grains.js +26 -11
  159. package/dist/utils/portable-query-calculations.d.ts +14 -0
  160. package/dist/utils/portable-query-calculations.d.ts.map +1 -0
  161. package/dist/utils/portable-query-calculations.js +31 -0
  162. package/dist/utils/portable-semantic-query.d.ts.map +1 -1
  163. package/dist/utils/portable-semantic-query.js +27 -1
  164. package/dist/utils/protocol-contract-3.d.ts +30 -0
  165. package/dist/utils/protocol-contract-3.d.ts.map +1 -0
  166. package/dist/utils/protocol-contract-3.js +93 -0
  167. package/dist/utils/protocol-dataset-query.d.ts +19 -0
  168. package/dist/utils/protocol-dataset-query.d.ts.map +1 -0
  169. package/dist/utils/protocol-dataset-query.js +50 -0
  170. package/dist/utils/protocol-filter-nodes.d.ts +9 -0
  171. package/dist/utils/protocol-filter-nodes.d.ts.map +1 -0
  172. package/dist/utils/protocol-filter-nodes.js +68 -0
  173. package/dist/utils/protocol-metric-capabilities.d.ts.map +1 -1
  174. package/dist/utils/protocol-metric-capabilities.js +2 -1
  175. package/dist/utils/protocol-metric-expressions.d.ts +9 -1
  176. package/dist/utils/protocol-metric-expressions.d.ts.map +1 -1
  177. package/dist/utils/protocol-metric-expressions.js +27 -2
  178. package/dist/utils/protocol-rehydrate-filters.d.ts +8 -1
  179. package/dist/utils/protocol-rehydrate-filters.d.ts.map +1 -1
  180. package/dist/utils/protocol-rehydrate-filters.js +20 -1
  181. package/dist/utils/protocol-rehydrate-time-keys.d.ts +6 -0
  182. package/dist/utils/protocol-rehydrate-time-keys.d.ts.map +1 -0
  183. package/dist/utils/protocol-rehydrate-time-keys.js +7 -0
  184. package/dist/utils/query-calculation-errors.d.ts +6 -0
  185. package/dist/utils/query-calculation-errors.d.ts.map +1 -0
  186. package/dist/utils/query-calculation-errors.js +9 -0
  187. package/dist/utils/query-calculation-formula.d.ts +19 -0
  188. package/dist/utils/query-calculation-formula.d.ts.map +1 -0
  189. package/dist/utils/query-calculation-formula.js +77 -0
  190. package/dist/utils/query-calculation-outputs.d.ts +24 -0
  191. package/dist/utils/query-calculation-outputs.d.ts.map +1 -0
  192. package/dist/utils/query-calculation-outputs.js +72 -0
  193. package/dist/utils/query-calculation-plan.d.ts +67 -0
  194. package/dist/utils/query-calculation-plan.d.ts.map +1 -0
  195. package/dist/utils/query-calculation-plan.js +416 -0
  196. package/dist/utils/query-calculation-types.d.ts +97 -0
  197. package/dist/utils/query-calculation-types.d.ts.map +1 -0
  198. package/dist/utils/query-calculation-types.js +8 -0
  199. package/dist/utils/query-field-references.d.ts.map +1 -1
  200. package/dist/utils/query-field-references.js +3 -2
  201. package/dist/utils/query-filter-errors.d.ts +11 -0
  202. package/dist/utils/query-filter-errors.d.ts.map +1 -0
  203. package/dist/utils/query-filter-errors.js +65 -0
  204. package/dist/utils/query-schema-calculations.d.ts +55 -0
  205. package/dist/utils/query-schema-calculations.d.ts.map +1 -0
  206. package/dist/utils/query-schema-calculations.js +150 -0
  207. package/dist/utils/query-schema-filters.d.ts +47 -0
  208. package/dist/utils/query-schema-filters.d.ts.map +1 -0
  209. package/dist/utils/query-schema-filters.js +132 -0
  210. package/dist/utils/query-timezone.d.ts +6 -3
  211. package/dist/utils/query-timezone.d.ts.map +1 -1
  212. package/dist/utils/query-timezone.js +5 -1
  213. package/dist/utils/relationship-builder-plan.d.ts.map +1 -1
  214. package/dist/utils/relationship-builder-plan.js +2 -1
  215. package/dist/utils/relationship-fields.d.ts.map +1 -1
  216. package/dist/utils/relationship-fields.js +5 -0
  217. package/dist/utils/relationship-measure-sql.d.ts.map +1 -1
  218. package/dist/utils/relationship-measure-sql.js +4 -1
  219. package/dist/utils/relationship-measures.d.ts.map +1 -1
  220. package/dist/utils/relationship-measures.js +2 -1
  221. package/dist/utils/relationship-validation.d.ts +3 -1
  222. package/dist/utils/relationship-validation.d.ts.map +1 -1
  223. package/dist/utils/relationship-validation.js +8 -4
  224. package/dist/utils/relative-date-filters.d.ts +21 -0
  225. package/dist/utils/relative-date-filters.d.ts.map +1 -0
  226. package/dist/utils/relative-date-filters.js +86 -0
  227. package/dist/utils/relative-date-range.d.ts +20 -0
  228. package/dist/utils/relative-date-range.d.ts.map +1 -0
  229. package/dist/utils/relative-date-range.js +75 -0
  230. package/dist/utils/retention-rows.d.ts +9 -0
  231. package/dist/utils/retention-rows.d.ts.map +1 -0
  232. package/dist/utils/retention-rows.js +30 -0
  233. package/dist/utils/retention-sql.d.ts +16 -0
  234. package/dist/utils/retention-sql.d.ts.map +1 -0
  235. package/dist/utils/retention-sql.js +68 -0
  236. package/dist/utils/scoped-dataset-source.d.ts.map +1 -1
  237. package/dist/utils/scoped-dataset-source.js +4 -0
  238. package/dist/utils/segments.d.ts +3 -2
  239. package/dist/utils/segments.d.ts.map +1 -1
  240. package/dist/utils/segments.js +5 -4
  241. package/dist/utils/semantic-query-syntax.d.ts.map +1 -1
  242. package/dist/utils/semantic-query-syntax.js +8 -2
  243. package/dist/utils/sql-dataset-source.d.ts +36 -0
  244. package/dist/utils/sql-dataset-source.d.ts.map +1 -0
  245. package/dist/utils/sql-dataset-source.js +126 -0
  246. package/dist/utils/time-axis-bounds.d.ts +11 -4
  247. package/dist/utils/time-axis-bounds.d.ts.map +1 -1
  248. package/dist/utils/time-axis-bounds.js +14 -6
  249. package/dist/utils/time-measure-axis.d.ts +2 -2
  250. package/dist/utils/time-measure-axis.d.ts.map +1 -1
  251. package/dist/utils/time-measure-axis.js +13 -3
  252. package/dist/utils/time-measure-dataset-sql.d.ts +1 -1
  253. package/dist/utils/time-measure-dataset-sql.d.ts.map +1 -1
  254. package/dist/utils/time-measure-dataset-sql.js +15 -5
  255. package/dist/utils/time-measure-source-sql.d.ts +2 -2
  256. package/dist/utils/time-measure-source-sql.d.ts.map +1 -1
  257. package/dist/utils/time-measure-source-sql.js +7 -8
  258. package/dist/utils/time-query-measures.d.ts +5 -0
  259. package/dist/utils/time-query-measures.d.ts.map +1 -1
  260. package/dist/utils/time-query-measures.js +7 -0
  261. package/dist/utils/unsupported-contract-reasons.d.ts +5 -1
  262. package/dist/utils/unsupported-contract-reasons.d.ts.map +1 -1
  263. package/dist/utils/unsupported-contract-reasons.js +5 -1
  264. package/package.json +3 -3
@@ -0,0 +1,30 @@
1
+ /**
2
+ * The contract 3 parts of a published dataset (RFC 0015): segments, window and
3
+ * shift measures, and the `approximate` marker on derived measures.
4
+ *
5
+ * Producers follow the lowest-version rule: a deployment is contract 3 only
6
+ * when one of its datasets uses a feature contract 2 cannot express, so every
7
+ * other deployment keeps its contract 2 bytes and identity. Python's builder
8
+ * (`hypequery.datasets.deployment`) emits the same contract for the same
9
+ * definition; `specs/datasets/contract-3-authoring-v1.json` pins both.
10
+ */
11
+ import { type ProtocolDatasetSegment, type ProtocolDatasetShiftMeasure, type ProtocolDatasetWindowMeasure } from '@hypequery/protocol';
12
+ import type { AnyDatasetInstance } from '../types.js';
13
+ /** The contract 3 features `dataset` uses, by name, in a fixed order. */
14
+ export declare function contract3Features(dataset: AnyDatasetInstance): string[];
15
+ /** True when the deployment holding `dataset` must be contract 3. */
16
+ export declare function usesContract3Features(dataset: AnyDatasetInstance): boolean;
17
+ /**
18
+ * Segments as contract predicates, sorted by name. One condition is a bare
19
+ * comparison; several are the `and` of their comparisons in authored order.
20
+ */
21
+ export declare function protocolSegments(dataset: AnyDatasetInstance): ProtocolDatasetSegment[];
22
+ type ProtocolTimeMeasure = ProtocolDatasetWindowMeasure | ProtocolDatasetShiftMeasure;
23
+ /** Window and shift measures in the order the dataset declares them. */
24
+ export declare function protocolTimeMeasures(dataset: AnyDatasetInstance): ProtocolTimeMeasure[];
25
+ /** The `approximate` marker for a derived measure: set when any input is approximate. */
26
+ export declare function derivedApproximateMarker(dataset: AnyDatasetInstance, name: string): {
27
+ readonly approximate?: true;
28
+ };
29
+ export {};
30
+ //# sourceMappingURL=protocol-contract-3.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol-contract-3.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-contract-3.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAEL,KAAK,sBAAsB,EAC3B,KAAK,2BAA2B,EAChC,KAAK,4BAA4B,EAElC,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EACV,kBAAkB,EAGnB,MAAM,aAAa,CAAC;AAQrB,yEAAyE;AACzE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,MAAM,EAAE,CAavE;AAED,qEAAqE;AACrE,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAE1E;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,GAAG,sBAAsB,EAAE,CAatF;AAGD,KAAK,mBAAmB,GAAG,4BAA4B,GAAG,2BAA2B,CAAC;AAoCtF,wEAAwE;AACxE,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,kBAAkB,GAAG,mBAAmB,EAAE,CAMvF;AAED,yFAAyF;AACzF,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,kBAAkB,EAC3B,IAAI,EAAE,MAAM,GACX;IAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,IAAI,CAAA;CAAE,CAEjC"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * The contract 3 parts of a published dataset (RFC 0015): segments, window and
3
+ * shift measures, and the `approximate` marker on derived measures.
4
+ *
5
+ * Producers follow the lowest-version rule: a deployment is contract 3 only
6
+ * when one of its datasets uses a feature contract 2 cannot express, so every
7
+ * other deployment keeps its contract 2 bytes and identity. Python's builder
8
+ * (`hypequery.datasets.deployment`) emits the same contract for the same
9
+ * definition; `specs/datasets/contract-3-authoring-v1.json` pins both.
10
+ */
11
+ import { parseProtocolIdentifier, } from '@hypequery/protocol';
12
+ import { isApproximateBaseMeasure, isApproximateMeasure } from './approximate-measures.js';
13
+ import { byName } from './by-name.js';
14
+ import { splitDatasetMeasures } from './dataset-measures.js';
15
+ import { isSubDayTimeGrain } from './portable-grains.js';
16
+ import { filterExpression } from './protocol-metric-expressions.js';
17
+ import { toProtocolSemanticMetadata } from './protocol-semantic-metadata.js';
18
+ /** The contract 3 features `dataset` uses, by name, in a fixed order. */
19
+ export function contract3Features(dataset) {
20
+ const { base, windows, shifts } = splitDatasetMeasures(dataset.measures);
21
+ return [
22
+ ...(Object.keys(dataset.segments ?? {}).length > 0 ? ['segments'] : []),
23
+ ...(Object.keys(windows).length > 0 ? ['window measures'] : []),
24
+ ...(Object.keys(shifts).length > 0 ? ['shift measures'] : []),
25
+ ...(Object.values(base).some(measure => measure.aggregation === 'approxCountDistinct')
26
+ ? ['approxCountDistinct'] : []),
27
+ ...(dataset.defaults?.timeGrain !== undefined && isSubDayTimeGrain(dataset.defaults.timeGrain)
28
+ ? ['a sub-day default grain'] : []),
29
+ // RFC 0029: published as `alternateTimeFields`, never silently dropped.
30
+ ...((dataset.timeKeys?.length ?? 0) > 0 ? ['alternate time keys'] : []),
31
+ ];
32
+ }
33
+ /** True when the deployment holding `dataset` must be contract 3. */
34
+ export function usesContract3Features(dataset) {
35
+ return contract3Features(dataset).length > 0;
36
+ }
37
+ /**
38
+ * Segments as contract predicates, sorted by name. One condition is a bare
39
+ * comparison; several are the `and` of their comparisons in authored order.
40
+ */
41
+ export function protocolSegments(dataset) {
42
+ return Object.entries(dataset.segments ?? {}).map(([name, segment]) => {
43
+ const conditions = segment.filters.map(filterExpression);
44
+ const predicate = conditions.length === 1
45
+ ? conditions[0]
46
+ : { kind: 'logical', operator: 'and', operands: conditions };
47
+ return {
48
+ name: parseProtocolIdentifier(name),
49
+ predicate,
50
+ ...(segment.label !== undefined ? { label: segment.label } : {}),
51
+ ...(segment.description !== undefined ? { description: segment.description } : {}),
52
+ };
53
+ }).sort(byName);
54
+ }
55
+ function timeMeasure(dataset, name, definition) {
56
+ const wrapped = dataset.measures[definition.measure];
57
+ const approximate = wrapped?.__type === 'measure_definition' && isApproximateBaseMeasure(wrapped);
58
+ const common = {
59
+ name: parseProtocolIdentifier(name),
60
+ measure: parseProtocolIdentifier(definition.measure),
61
+ requiresTimeRange: true,
62
+ ...(approximate ? { approximate: true } : {}),
63
+ ...(definition.label !== undefined ? { label: definition.label } : {}),
64
+ ...(definition.description !== undefined ? { description: definition.description } : {}),
65
+ ...toProtocolSemanticMetadata(definition),
66
+ };
67
+ if (definition.__type === 'shift_measure_definition') {
68
+ return {
69
+ kind: 'shift',
70
+ ...common,
71
+ interval: { amount: definition.interval.amount, unit: definition.interval.unit },
72
+ };
73
+ }
74
+ return {
75
+ kind: 'window',
76
+ ...common,
77
+ ...(definition.trailing !== undefined
78
+ ? { trailing: { amount: definition.trailing.amount, unit: definition.trailing.unit } }
79
+ : {}),
80
+ ...(definition.toDate !== undefined ? { toDate: definition.toDate } : {}),
81
+ ...(definition.cumulative === true ? { cumulative: true } : {}),
82
+ };
83
+ }
84
+ /** Window and shift measures in the order the dataset declares them. */
85
+ export function protocolTimeMeasures(dataset) {
86
+ return Object.entries(dataset.measures)
87
+ .filter((entry) => (entry[1].__type === 'window_measure_definition' || entry[1].__type === 'shift_measure_definition'))
88
+ .map(([name, definition]) => timeMeasure(dataset, name, definition));
89
+ }
90
+ /** The `approximate` marker for a derived measure: set when any input is approximate. */
91
+ export function derivedApproximateMarker(dataset, name) {
92
+ return isApproximateMeasure(dataset.measures, name) ? { approximate: true } : {};
93
+ }
@@ -0,0 +1,19 @@
1
+ import type { DatasetQuery } from '../types.js';
2
+ /**
3
+ * A dataset client query as an RFC 0003 dataset query envelope, the operation
4
+ * a semantic data plane validates (`validateProtocolSemanticQuery`) and runs.
5
+ *
6
+ * The inverse of what the portable runtime does with an operation it is
7
+ * handed: comparisons and `and`/`or`/`not` groups become `comparison` and
8
+ * `logical` predicates (RFC 0029), a filter calculation's `where` becomes
9
+ * comparisons (RFC 0026), and every other field is copied in document order.
10
+ * Nothing is dropped, so a field the receiving validator does not accept fails
11
+ * there rather than being silently ignored. A grain is the envelope's `by`.
12
+ *
13
+ * `inDateRange` is carried as written: the executor resolves it with its own
14
+ * clock and the query's timezone, never the caller's.
15
+ */
16
+ export declare function toProtocolDatasetQuery(dataset: string, query: DatasetQuery & {
17
+ readonly includeMeta?: boolean;
18
+ }): Record<string, unknown>;
19
+ //# sourceMappingURL=protocol-dataset-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol-dataset-query.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-dataset-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIhD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,YAAY,GAAG;IAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,GACvD,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAyBzB"}
@@ -0,0 +1,50 @@
1
+ import { protocolComparisonNode, protocolFilterNode } from './protocol-filter-nodes.js';
2
+ /**
3
+ * A dataset client query as an RFC 0003 dataset query envelope, the operation
4
+ * a semantic data plane validates (`validateProtocolSemanticQuery`) and runs.
5
+ *
6
+ * The inverse of what the portable runtime does with an operation it is
7
+ * handed: comparisons and `and`/`or`/`not` groups become `comparison` and
8
+ * `logical` predicates (RFC 0029), a filter calculation's `where` becomes
9
+ * comparisons (RFC 0026), and every other field is copied in document order.
10
+ * Nothing is dropped, so a field the receiving validator does not accept fails
11
+ * there rather than being silently ignored. A grain is the envelope's `by`.
12
+ *
13
+ * `inDateRange` is carried as written: the executor resolves it with its own
14
+ * clock and the query's timezone, never the caller's.
15
+ */
16
+ export function toProtocolDatasetQuery(dataset, query) {
17
+ const copy = (value) => (value === undefined ? undefined : structuredClone(value));
18
+ const entries = [
19
+ ['dimensions', copy(query.dimensions)],
20
+ ['measures', copy(query.measures)],
21
+ ['filters', query.filters?.map(protocolFilterNode)],
22
+ ['segments', copy(query.segments)],
23
+ ['calculations', query.calculations?.map(protocolCalculation)],
24
+ ['having', copy(query.having)],
25
+ ['limitBy', copy(query.limitBy)],
26
+ ['other', copy(query.other)],
27
+ ['orderBy', query.orderBy?.map(({ field, direction }) => ({ field, direction }))],
28
+ ['limit', query.limit],
29
+ ['offset', query.offset],
30
+ ['by', query.by],
31
+ ['timeKey', query.timeKey],
32
+ ['timezone', query.timezone],
33
+ ['fill', query.fill],
34
+ ['includeMeta', query.includeMeta],
35
+ ];
36
+ return {
37
+ kind: 'dataset',
38
+ dataset,
39
+ ...Object.fromEntries(entries.filter(([, value]) => value !== undefined)),
40
+ };
41
+ }
42
+ /** A calculation entry; only a filter calculation's `where` changes shape. */
43
+ function protocolCalculation(entry) {
44
+ if (!('filter' in entry) || entry.filter === undefined)
45
+ return structuredClone(entry);
46
+ return {
47
+ name: entry.name,
48
+ filter: { measure: entry.filter.measure, where: entry.filter.where.map(protocolComparisonNode) },
49
+ };
50
+ }
@@ -0,0 +1,9 @@
1
+ import type { DatasetFilterNode, MetricFilter } from '../types.js';
2
+ /** One comparison as a `comparison` node; a null check has no right operand (RFC 0023). */
3
+ export declare function protocolComparisonNode(filter: MetricFilter): Record<string, unknown>;
4
+ /**
5
+ * A caller filter as an RFC 0003 predicate: a group becomes a `logical` node
6
+ * (RFC 0029), a one-item group its item, since `and`/`or` take two or more.
7
+ */
8
+ export declare function protocolFilterNode(node: DatasetFilterNode): Record<string, unknown>;
9
+ //# sourceMappingURL=protocol-filter-nodes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol-filter-nodes.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-filter-nodes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAgCnE,2FAA2F;AAC3F,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAoBpF;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CASnF"}
@@ -0,0 +1,68 @@
1
+ import { validateCanonicalValue } from '@hypequery/protocol';
2
+ import { isFilterGroup } from './filter-tree.js';
3
+ /**
4
+ * Dataset client filters as RFC 0003 predicates: the inverse of
5
+ * `operationFilter` in `portable-semantic-query.ts`.
6
+ */
7
+ function datetimeValue(value) {
8
+ return validateCanonicalValue({
9
+ $hypequery: {
10
+ type: 'datetime',
11
+ version: 1,
12
+ clickhouseType: 'DateTime64',
13
+ precision: 3,
14
+ timezone: 'UTC',
15
+ value: value.toISOString(),
16
+ },
17
+ });
18
+ }
19
+ /** A filter value as an RFC 0001 canonical value. Throws when it has none. */
20
+ function canonicalLiteral(value) {
21
+ if (value instanceof Date)
22
+ return datetimeValue(value);
23
+ if (Array.isArray(value)) {
24
+ return validateCanonicalValue({
25
+ $hypequery: { type: 'array', version: 1, values: value.map(canonicalLiteral) },
26
+ });
27
+ }
28
+ return validateCanonicalValue(value);
29
+ }
30
+ /** One comparison as a `comparison` node; a null check has no right operand (RFC 0023). */
31
+ export function protocolComparisonNode(filter) {
32
+ const left = { kind: 'reference', name: filter.field };
33
+ if (filter.operator === 'isNull' || filter.operator === 'isNotNull') {
34
+ return { kind: 'comparison', operator: filter.operator, left };
35
+ }
36
+ let literal;
37
+ if (filter.operator === 'between' && Array.isArray(filter.value)) {
38
+ // RFC 0003 bounds are a two-item tuple; in/notIn take an array.
39
+ literal = validateCanonicalValue({
40
+ $hypequery: { type: 'tuple', version: 1, values: filter.value.map(canonicalLiteral) },
41
+ });
42
+ }
43
+ else {
44
+ literal = canonicalLiteral(filter.value);
45
+ }
46
+ return {
47
+ kind: 'comparison',
48
+ operator: filter.operator,
49
+ left,
50
+ right: { kind: 'literal', value: literal },
51
+ };
52
+ }
53
+ /**
54
+ * A caller filter as an RFC 0003 predicate: a group becomes a `logical` node
55
+ * (RFC 0029), a one-item group its item, since `and`/`or` take two or more.
56
+ */
57
+ export function protocolFilterNode(node) {
58
+ if (!isFilterGroup(node))
59
+ return protocolComparisonNode(node);
60
+ if ('not' in node && node.not !== undefined) {
61
+ return { kind: 'logical', operator: 'not', operand: protocolFilterNode(node.not) };
62
+ }
63
+ const operator = 'and' in node && node.and !== undefined ? 'and' : 'or';
64
+ const items = (operator === 'and' ? node.and : node.or);
65
+ if (items.length === 1)
66
+ return protocolFilterNode(items[0]);
67
+ return { kind: 'logical', operator, operands: items.map(protocolFilterNode) };
68
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"protocol-metric-capabilities.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-metric-capabilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAwB,MAAM,aAAa,CAAC;AAUnF,uFAAuF;AACvF,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE,CAajG;AAED,mFAAmF;AACnF,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,qBAAqB,GAAG,YAAY,CAkC1G"}
1
+ {"version":3,"file":"protocol-metric-capabilities.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-metric-capabilities.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAwB,MAAM,aAAa,CAAC;AAUnF,uFAAuF;AACvF,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE,CAajG;AAED,mFAAmF;AACnF,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,qBAAqB,GAAG,YAAY,CAkC1G"}
@@ -1,3 +1,4 @@
1
+ import { filterLeaves } from './filter-tree.js';
1
2
  const CAPABILITIES = Symbol('hypequery.protocol-metric-capabilities');
2
3
  /** Runtime restrictions travel with a cloned handle, including publication aliases. */
3
4
  export function protocolMetricCapabilityErrors(handle, query) {
@@ -9,7 +10,7 @@ export function protocolMetricCapabilityErrors(handle, query) {
9
10
  if (!capabilities.dimensions.includes(dimension))
10
11
  errors.push(`Dimension "${dimension}" is not published for this metric.`);
11
12
  }
12
- for (const filter of query.filters ?? []) {
13
+ for (const filter of filterLeaves(query.filters)) {
13
14
  if (!capabilities.filters.includes(filter.field))
14
15
  errors.push(`Filter "${filter.field}" is not published for this metric.`);
15
16
  }
@@ -1,6 +1,14 @@
1
1
  import { type ProtocolExpression, type ProtocolMetricDerivation } from '@hypequery/protocol';
2
- import type { AggregationSpec, DerivedMeasureDefinition, DerivedMetricSpec, MetricFilter } from '../types.js';
2
+ import type { AggregationSpec, DerivedMeasureDefinition, DerivedMetricSpec, MeasureFilterNode, MetricFilter } from '../types.js';
3
3
  export declare function filterExpression(filter: MetricFilter): ProtocolExpression;
4
+ /**
5
+ * A measure's fixed filter node as an RFC 0003 predicate: a comparison as
6
+ * `filterExpression` writes it, and an `and` / `or` / `not` group as the
7
+ * matching `logical` expression, in authored order. A one-item `and` or `or`
8
+ * means its item, and RFC 0003 requires two operands, so it publishes as the
9
+ * item; `renderMeasureFilterNode` renders it the same way.
10
+ */
11
+ export declare function measureFilterExpression(node: MeasureFilterNode): ProtocolExpression;
4
12
  /** The published form; aliases must satisfy the protocol identifier grammar. */
5
13
  export declare function derivedMeasureExpression(definition: DerivedMeasureDefinition): ProtocolExpression;
6
14
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"protocol-metric-expressions.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-metric-expressions.ts"],"names":[],"mappings":"AAAA,OAAO,EAEgB,KAAK,kBAAkB,EAAE,KAAK,wBAAwB,EAC5E,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,eAAe,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAuC9G,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,GAAG,kBAAkB,CAoBzE;AAsDD,gFAAgF;AAChF,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,wBAAwB,GAAG,kBAAkB,CAEjG;AAED;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAAC,UAAU,EAAE,wBAAwB,GAAG,kBAAkB,CAExG;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,eAAe,GAAG,iBAAiB,GAAG,kBAAkB,CAQ9F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,iBAAiB,GAAG,wBAAwB,CASlF"}
1
+ {"version":3,"file":"protocol-metric-expressions.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-metric-expressions.ts"],"names":[],"mappings":"AACA,OAAO,EAEgB,KAAK,kBAAkB,EAAE,KAAK,wBAAwB,EAC5E,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,eAAe,EAAE,wBAAwB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAuCjI,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,GAAG,kBAAkB,CAyBzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB,CASnF;AAsDD,gFAAgF;AAChF,wBAAgB,wBAAwB,CAAC,UAAU,EAAE,wBAAwB,GAAG,kBAAkB,CAEjG;AAED;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAAC,UAAU,EAAE,wBAAwB,GAAG,kBAAkB,CAExG;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,eAAe,GAAG,iBAAiB,GAAG,kBAAkB,CAQ9F;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,iBAAiB,GAAG,wBAAwB,CASlF"}
@@ -1,3 +1,4 @@
1
+ import { isFilterGroup } from './filter-tree.js';
1
2
  import { parseProtocolIdentifier, parseProtocolQualifiedIdentifier, validateCanonicalValue, } from '@hypequery/protocol';
2
3
  import { requirePortableAggregation } from './portable-aggregations.js';
3
4
  function canonicalValue(input) {
@@ -38,9 +39,14 @@ export function filterExpression(filter) {
38
39
  if (filter.operator === 'isNull' || filter.operator === 'isNotNull') {
39
40
  return { kind: 'comparison', operator: filter.operator, left };
40
41
  }
42
+ // RFC 0003: `between` bounds are a two-item tuple; `in`/`notIn` take an array.
41
43
  const right = {
42
44
  kind: 'literal',
43
- value: canonicalValue(filter.value),
45
+ value: filter.operator === 'between' && Array.isArray(filter.value)
46
+ ? validateCanonicalValue({
47
+ $hypequery: { type: 'tuple', version: 1, values: filter.value.map(canonicalValue) },
48
+ })
49
+ : canonicalValue(filter.value),
44
50
  };
45
51
  const result = {
46
52
  kind: 'comparison',
@@ -50,6 +56,25 @@ export function filterExpression(filter) {
50
56
  };
51
57
  return result;
52
58
  }
59
+ /**
60
+ * A measure's fixed filter node as an RFC 0003 predicate: a comparison as
61
+ * `filterExpression` writes it, and an `and` / `or` / `not` group as the
62
+ * matching `logical` expression, in authored order. A one-item `and` or `or`
63
+ * means its item, and RFC 0003 requires two operands, so it publishes as the
64
+ * item; `renderMeasureFilterNode` renders it the same way.
65
+ */
66
+ export function measureFilterExpression(node) {
67
+ if (!isFilterGroup(node))
68
+ return filterExpression(node);
69
+ if ('not' in node && node.not !== undefined) {
70
+ return { kind: 'logical', operator: 'not', operand: measureFilterExpression(node.not) };
71
+ }
72
+ const operator = 'and' in node && node.and !== undefined ? 'and' : 'or';
73
+ const items = (operator === 'and' ? node.and : node.or) ?? [];
74
+ if (items.length === 1)
75
+ return measureFilterExpression(items[0]);
76
+ return { kind: 'logical', operator, operands: items.map(measureFilterExpression) };
77
+ }
53
78
  function aggregationExpression(spec) {
54
79
  const result = {
55
80
  kind: 'aggregate',
@@ -60,7 +85,7 @@ function aggregationExpression(spec) {
60
85
  : {}),
61
86
  ...(spec.level !== undefined ? { level: spec.level } : {}),
62
87
  ...(spec.filters?.length
63
- ? { filters: spec.filters.map(filterExpression) }
88
+ ? { filters: spec.filters.map(measureFilterExpression) }
64
89
  : {}),
65
90
  };
66
91
  return result;
@@ -1,5 +1,5 @@
1
1
  import type { ProtocolExpression } from '@hypequery/protocol';
2
- import type { MetricFilter } from '../types.js';
2
+ import type { MeasureFilterNode, MetricFilter } from '../types.js';
3
3
  /**
4
4
  * A measure's fixed filters cross the contract boundary as RFC 0003
5
5
  * expressions. `protocol-adapter.ts` only ever emits `reference <op> literal`
@@ -7,4 +7,11 @@ import type { MetricFilter } from '../types.js';
7
7
  * than partially interpreting an expression it cannot faithfully rebuild.
8
8
  */
9
9
  export declare function rehydrateMeasureFilter(expression: ProtocolExpression, unsupported: () => Error): MetricFilter;
10
+ /**
11
+ * A measure's fixed filter as the node it was authored as: a comparison, or a
12
+ * `logical` `and` / `or` / `not` of them as the matching group, in authored
13
+ * order, so the rebuilt measure renders the same SQL as the authored one.
14
+ * Anything else is refused, as `rehydrateMeasureFilter` refuses it.
15
+ */
16
+ export declare function rehydrateMeasureFilterNode(expression: ProtocolExpression, unsupported: () => Error, depth?: number): MeasureFilterNode;
10
17
  //# sourceMappingURL=protocol-rehydrate-filters.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"protocol-rehydrate-filters.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-rehydrate-filters.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,kBAAkB,EAC9B,WAAW,EAAE,MAAM,KAAK,GACvB,YAAY,CAcd"}
1
+ {"version":3,"file":"protocol-rehydrate-filters.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-rehydrate-filters.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGnE;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,kBAAkB,EAC9B,WAAW,EAAE,MAAM,KAAK,GACvB,YAAY,CAcd;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,UAAU,EAAE,kBAAkB,EAC9B,WAAW,EAAE,MAAM,KAAK,EACxB,KAAK,SAAI,GACR,iBAAiB,CAQnB"}
@@ -1,3 +1,4 @@
1
+ import { MAX_FILTER_GROUP_DEPTH } from './filter-tree.js';
1
2
  /**
2
3
  * A measure's fixed filters cross the contract boundary as RFC 0003
3
4
  * expressions. `protocol-adapter.ts` only ever emits `reference <op> literal`
@@ -22,6 +23,23 @@ export function rehydrateMeasureFilter(expression, unsupported) {
22
23
  value: fromCanonicalValue(right.value, unsupported),
23
24
  };
24
25
  }
26
+ /**
27
+ * A measure's fixed filter as the node it was authored as: a comparison, or a
28
+ * `logical` `and` / `or` / `not` of them as the matching group, in authored
29
+ * order, so the rebuilt measure renders the same SQL as the authored one.
30
+ * Anything else is refused, as `rehydrateMeasureFilter` refuses it.
31
+ */
32
+ export function rehydrateMeasureFilterNode(expression, unsupported, depth = 1) {
33
+ if (expression.kind !== 'logical')
34
+ return rehydrateMeasureFilter(expression, unsupported);
35
+ if (depth > MAX_FILTER_GROUP_DEPTH)
36
+ throw unsupported();
37
+ if (expression.operator === 'not') {
38
+ return { not: rehydrateMeasureFilterNode(expression.operand, unsupported, depth + 1) };
39
+ }
40
+ const items = expression.operands.map(operand => rehydrateMeasureFilterNode(operand, unsupported, depth + 1));
41
+ return expression.operator === 'and' ? { and: items } : { or: items };
42
+ }
25
43
  /**
26
44
  * Unwraps the RFC 0001 tagged value model back to the plain JavaScript value a
27
45
  * measure filter was authored with.
@@ -33,7 +51,8 @@ function fromCanonicalValue(value, unsupported) {
33
51
  .$hypequery;
34
52
  if (tagged === undefined)
35
53
  throw unsupported();
36
- if (tagged.type === 'array') {
54
+ // `in`/`notIn` carry an array; `between` carries a two-item tuple.
55
+ if (tagged.type === 'array' || tagged.type === 'tuple') {
37
56
  return (tagged.values ?? []).map(item => fromCanonicalValue(item, unsupported));
38
57
  }
39
58
  if (tagged.value === undefined)
@@ -0,0 +1,6 @@
1
+ import { type ProtocolAnyVersionDataset } from '@hypequery/protocol';
2
+ /** Contract 3 `alternateTimeFields` as the rehydrated dataset's `timeKeys` (RFC 0029). */
3
+ export declare function alternateTimeKeys(contract: ProtocolAnyVersionDataset): {
4
+ timeKeys?: string[];
5
+ };
6
+ //# sourceMappingURL=protocol-rehydrate-time-keys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"protocol-rehydrate-time-keys.d.ts","sourceRoot":"","sources":["../../src/utils/protocol-rehydrate-time-keys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAEtF,0FAA0F;AAC1F,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,yBAAyB,GAAG;IAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAI9F"}
@@ -0,0 +1,7 @@
1
+ import { datasetTimeAxes } from '@hypequery/protocol';
2
+ /** Contract 3 `alternateTimeFields` as the rehydrated dataset's `timeKeys` (RFC 0029). */
3
+ export function alternateTimeKeys(contract) {
4
+ // The default axis comes first; the rest are the alternates in authored order.
5
+ const alternates = datasetTimeAxes(contract).slice(1);
6
+ return alternates.length > 0 ? { timeKeys: alternates.map(String) } : {};
7
+ }
@@ -0,0 +1,6 @@
1
+ /** Thrown when a calculation names a kind or field this executor cannot run yet. */
2
+ export declare class UnsupportedQueryCalculationError extends Error {
3
+ readonly reasons: readonly string[];
4
+ constructor(reasons: readonly string[]);
5
+ }
6
+ //# sourceMappingURL=query-calculation-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-calculation-errors.d.ts","sourceRoot":"","sources":["../../src/utils/query-calculation-errors.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,qBAAa,gCAAiC,SAAQ,KAAK;IAC7C,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE;gBAA1B,OAAO,EAAE,SAAS,MAAM,EAAE;CAIhD"}
@@ -0,0 +1,9 @@
1
+ /** Thrown when a calculation names a kind or field this executor cannot run yet. */
2
+ export class UnsupportedQueryCalculationError extends Error {
3
+ reasons;
4
+ constructor(reasons) {
5
+ super(reasons.join('; '));
6
+ this.reasons = reasons;
7
+ this.name = 'UnsupportedQueryCalculationError';
8
+ }
9
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * RFC 0026 formula nodes as derived measures.
3
+ *
4
+ * A formula calculation becomes an ordinary derived measure of the query's
5
+ * dataset view, so the derived and time-measure planners render it through
6
+ * the same dialect formula renderer model-level derived measures use.
7
+ */
8
+ import type { DerivedMeasureDefinition } from '../types.js';
9
+ import type { QueryFormulaNode } from './query-calculation-types.js';
10
+ /** Every reference a formula makes, in first-use order. */
11
+ export declare function formulaReferences(node: QueryFormulaNode): string[];
12
+ /**
13
+ * A derived measure computing `node`. `resolve` maps each reference to the
14
+ * measure it reads (a selected measure, or an earlier calculation's internal
15
+ * name). Each input is its own alias, so the formula honours whatever names a
16
+ * planner passes for them.
17
+ */
18
+ export declare function formulaMeasure(node: QueryFormulaNode, resolve: (name: string) => string): DerivedMeasureDefinition;
19
+ //# sourceMappingURL=query-calculation-formula.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-calculation-formula.d.ts","sourceRoot":"","sources":["../../src/utils/query-calculation-formula.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,wBAAwB,EAAe,MAAM,aAAa,CAAC;AACzE,OAAO,KAAK,EAAwB,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAK3F,2DAA2D;AAC3D,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,EAAE,CAWlE;AAoCD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,gBAAgB,EACtB,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAChC,wBAAwB,CAW1B"}
@@ -0,0 +1,77 @@
1
+ /**
2
+ * RFC 0026 formula nodes as derived measures.
3
+ *
4
+ * A formula calculation becomes an ordinary derived measure of the query's
5
+ * dataset view, so the derived and time-measure planners render it through
6
+ * the same dialect formula renderer model-level derived measures use.
7
+ */
8
+ const BINARY = new Set(['add', 'subtract', 'multiply', 'divide']);
9
+ const BINARY_SQL = { add: '+', subtract: '-', multiply: '*', divide: '/' };
10
+ /** Every reference a formula makes, in first-use order. */
11
+ export function formulaReferences(node) {
12
+ const names = [];
13
+ const visit = (arg) => {
14
+ if (typeof arg === 'string') {
15
+ if (!names.includes(arg))
16
+ names.push(arg);
17
+ }
18
+ else if (typeof arg === 'object' && arg !== null) {
19
+ arg.args.forEach(visit);
20
+ }
21
+ };
22
+ node.args.forEach(visit);
23
+ return names;
24
+ }
25
+ function expression(arg, ref) {
26
+ if (typeof arg === 'string')
27
+ return { kind: 'ref', name: ref(arg) };
28
+ if (typeof arg === 'number')
29
+ return { kind: 'literal', value: arg };
30
+ const args = arg.args.map(child => expression(child, ref));
31
+ if (arg.op === 'percentChange') {
32
+ const [current, previous] = args;
33
+ return {
34
+ kind: 'binary',
35
+ operator: 'divide',
36
+ left: { kind: 'binary', operator: 'subtract', left: current, right: previous },
37
+ right: { kind: 'function', name: 'nullIfZero', args: [previous] },
38
+ };
39
+ }
40
+ if (BINARY.has(arg.op)) {
41
+ const [left, right] = args;
42
+ return { kind: 'binary', operator: arg.op, left, right };
43
+ }
44
+ return { kind: 'function', name: arg.op, args };
45
+ }
46
+ /** Generic SQL for `FormulaExpr.toSQL`; planners render through the dialect instead. */
47
+ function expressionSql(node) {
48
+ switch (node.kind) {
49
+ case 'ref': return node.name;
50
+ case 'literal': return String(node.value);
51
+ case 'binary': return `(${expressionSql(node.left)}) ${BINARY_SQL[node.operator]} (${expressionSql(node.right)})`;
52
+ case 'function': {
53
+ const name = node.name === 'nullIfZero' ? 'NULLIF' : node.name.toUpperCase();
54
+ const args = node.args.map(expressionSql);
55
+ return node.name === 'nullIfZero' ? `${name}(${args[0]}, 0)` : `${name}(${args.join(', ')})`;
56
+ }
57
+ }
58
+ }
59
+ /**
60
+ * A derived measure computing `node`. `resolve` maps each reference to the
61
+ * measure it reads (a selected measure, or an earlier calculation's internal
62
+ * name). Each input is its own alias, so the formula honours whatever names a
63
+ * planner passes for them.
64
+ */
65
+ export function formulaMeasure(node, resolve) {
66
+ const uses = {};
67
+ for (const name of formulaReferences(node))
68
+ uses[resolve(name)] = resolve(name);
69
+ return {
70
+ __type: 'derived_measure_definition',
71
+ uses,
72
+ formula: (inputs) => {
73
+ const built = expression(node, name => inputs[resolve(name)] ?? resolve(name));
74
+ return { __type: 'formula_expr', expression: built, toSQL: () => expressionSql(built) };
75
+ },
76
+ };
77
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The output columns RFC 0026 calculations add to a result, decided from the
3
+ * query alone: names, internal planner names, kinds and types.
4
+ */
5
+ import type { DatasetQuery } from '../types.js';
6
+ import type { DatasetCalculationColumn, DatasetQueryCalculation, QueryCalculationKind } from './query-calculation-types.js';
7
+ /**
8
+ * Calculation measures are planned under reserved names, so a caller's output
9
+ * name never collides with, shadows or reveals a field of the model.
10
+ */
11
+ export declare const CALCULATION_INTERNAL_PREFIX = "_hq_calc_";
12
+ export interface CalculationOutput extends DatasetCalculationColumn {
13
+ /** The measure or column name the planners use. */
14
+ readonly internal: string;
15
+ }
16
+ /** True when the query needs the calculation planner. */
17
+ export declare function hasQueryCalculations(query: Pick<DatasetQuery, 'calculations' | 'limitBy' | 'other'>): boolean;
18
+ /** The single kind member of an entry, or undefined when it has none or several. */
19
+ export declare function calculationKind(entry: DatasetQueryCalculation): QueryCalculationKind | undefined;
20
+ /** Every output column in output order. Entries without exactly one kind add none. */
21
+ export declare function calculationOutputs(query: Pick<DatasetQuery, 'calculations'>): CalculationOutput[];
22
+ /** The public metadata for each output column. */
23
+ export declare function calculationColumns(query: Pick<DatasetQuery, 'calculations'>): DatasetCalculationColumn[];
24
+ //# sourceMappingURL=query-calculation-outputs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-calculation-outputs.d.ts","sourceRoot":"","sources":["../../src/utils/query-calculation-outputs.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAE5H;;;GAGG;AACH,eAAO,MAAM,2BAA2B,cAAc,CAAC;AAEvD,MAAM,WAAW,iBAAkB,SAAQ,wBAAwB;IACjE,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED,yDAAyD;AACzD,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE,cAAc,GAAG,SAAS,GAAG,OAAO,CAAC,GAAG,OAAO,CAI7G;AAED,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,KAAK,EAAE,uBAAuB,GAAG,oBAAoB,GAAG,SAAS,CAIhG;AAED,sFAAsF;AACtF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE,cAAc,CAAC,GAAG,iBAAiB,EAAE,CAiDjG;AAED,kDAAkD;AAClD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE,cAAc,CAAC,GAAG,wBAAwB,EAAE,CAExG"}