@hypequery/datasets 0.0.0-canary-20260520183507
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/LICENSE +201 -0
- package/dist/aggregations.d.ts +24 -0
- package/dist/aggregations.d.ts.map +1 -0
- package/dist/aggregations.js +41 -0
- package/dist/constants.d.ts +9 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +13 -0
- package/dist/dataset.d.ts +33 -0
- package/dist/dataset.d.ts.map +1 -0
- package/dist/dataset.js +203 -0
- package/dist/executor.d.ts +39 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +244 -0
- package/dist/field.d.ts +25 -0
- package/dist/field.d.ts.map +1 -0
- package/dist/field.js +35 -0
- package/dist/formulas.d.ts +25 -0
- package/dist/formulas.d.ts.map +1 -0
- package/dist/formulas.js +60 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/measure.d.ts +10 -0
- package/dist/measure.d.ts.map +1 -0
- package/dist/measure.js +18 -0
- package/dist/query-builder-protocol.d.ts +36 -0
- package/dist/query-builder-protocol.d.ts.map +1 -0
- package/dist/query-builder-protocol.js +11 -0
- package/dist/query-helpers.d.ts +30 -0
- package/dist/query-helpers.d.ts.map +1 -0
- package/dist/query-helpers.js +55 -0
- package/dist/query-planner.d.ts +13 -0
- package/dist/query-planner.d.ts.map +1 -0
- package/dist/query-planner.js +87 -0
- package/dist/registry.d.ts +8 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +25 -0
- package/dist/relationships.d.ts +44 -0
- package/dist/relationships.d.ts.map +1 -0
- package/dist/relationships.js +39 -0
- package/dist/sql-utils.d.ts +28 -0
- package/dist/sql-utils.d.ts.map +1 -0
- package/dist/sql-utils.js +38 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/validation.d.ts +28 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +73 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are advised to review the applicable
|
|
150
|
+
documentation for the specific language governing permissions and
|
|
151
|
+
limitations under each license.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 hypequery
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aggregation helpers — return AggregationSpec objects used in metric definitions.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { sum, count, countDistinct, avg } from '@hypequery/serve';
|
|
7
|
+
*
|
|
8
|
+
* const totalRevenue = Orders.metric("totalRevenue", {
|
|
9
|
+
* value: sum("amount"),
|
|
10
|
+
* });
|
|
11
|
+
*
|
|
12
|
+
* const orderCount = Orders.metric("orderCount", {
|
|
13
|
+
* value: count("id"),
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
import type { AggregationSpec } from './types.js';
|
|
18
|
+
export declare function sum(field: string): AggregationSpec;
|
|
19
|
+
export declare function count(field: string): AggregationSpec;
|
|
20
|
+
export declare function countDistinct(field: string): AggregationSpec;
|
|
21
|
+
export declare function avg(field: string): AggregationSpec;
|
|
22
|
+
export declare function min(field: string): AggregationSpec;
|
|
23
|
+
export declare function max(field: string): AggregationSpec;
|
|
24
|
+
//# sourceMappingURL=aggregations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aggregations.d.ts","sourceRoot":"","sources":["../src/aggregations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAmB,MAAM,YAAY,CAAC;AAUnE,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAElD;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAEpD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAE5D;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAElD;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAElD;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,eAAe,CAElD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Aggregation helpers — return AggregationSpec objects used in metric definitions.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* import { sum, count, countDistinct, avg } from '@hypequery/serve';
|
|
7
|
+
*
|
|
8
|
+
* const totalRevenue = Orders.metric("totalRevenue", {
|
|
9
|
+
* value: sum("amount"),
|
|
10
|
+
* });
|
|
11
|
+
*
|
|
12
|
+
* const orderCount = Orders.metric("orderCount", {
|
|
13
|
+
* value: count("id"),
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function createAggregation(aggregation, field) {
|
|
18
|
+
return {
|
|
19
|
+
__type: 'aggregation_spec',
|
|
20
|
+
aggregation,
|
|
21
|
+
field,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function sum(field) {
|
|
25
|
+
return createAggregation('sum', field);
|
|
26
|
+
}
|
|
27
|
+
export function count(field) {
|
|
28
|
+
return createAggregation('count', field);
|
|
29
|
+
}
|
|
30
|
+
export function countDistinct(field) {
|
|
31
|
+
return createAggregation('countDistinct', field);
|
|
32
|
+
}
|
|
33
|
+
export function avg(field) {
|
|
34
|
+
return createAggregation('avg', field);
|
|
35
|
+
}
|
|
36
|
+
export function min(field) {
|
|
37
|
+
return createAggregation('min', field);
|
|
38
|
+
}
|
|
39
|
+
export function max(field) {
|
|
40
|
+
return createAggregation('max', field);
|
|
41
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants for semantic layer implementation.
|
|
3
|
+
*/
|
|
4
|
+
import type { TimeGrain } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* Maps time grain to ClickHouse date truncation functions.
|
|
7
|
+
*/
|
|
8
|
+
export declare const GRAIN_FUNCTIONS: Record<TimeGrain, string>;
|
|
9
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAMrD,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants for semantic layer implementation.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Maps time grain to ClickHouse date truncation functions.
|
|
6
|
+
*/
|
|
7
|
+
export const GRAIN_FUNCTIONS = {
|
|
8
|
+
day: 'toStartOfDay',
|
|
9
|
+
week: 'toStartOfWeek',
|
|
10
|
+
month: 'toStartOfMonth',
|
|
11
|
+
quarter: 'toStartOfQuarter',
|
|
12
|
+
year: 'toStartOfYear',
|
|
13
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dataset() — creates a typed semantic model over a physical table.
|
|
3
|
+
*
|
|
4
|
+
* Relationships are modeled on the dataset today, but current query execution
|
|
5
|
+
* only supports same-dataset dimensions, measures, and metrics. Joined
|
|
6
|
+
* relationship traversal is not part of the shipped execution surface yet.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { dataset, dimension, measure, belongsTo } from '@hypequery/serve';
|
|
11
|
+
*
|
|
12
|
+
* const Orders = dataset("orders", {
|
|
13
|
+
* source: "orders",
|
|
14
|
+
* tenantKey: "tenant_id",
|
|
15
|
+
* timeKey: "created_at",
|
|
16
|
+
* dimensions: {
|
|
17
|
+
* id: dimension.string(),
|
|
18
|
+
* status: dimension.string({ label: "Order Status" }),
|
|
19
|
+
* createdAt: dimension.timestamp({ column: "created_at" }),
|
|
20
|
+
* },
|
|
21
|
+
* measures: {
|
|
22
|
+
* totalRevenue: measure.sum("amount", { label: "Total Revenue" }),
|
|
23
|
+
* orderCount: measure.count("id"),
|
|
24
|
+
* },
|
|
25
|
+
* relationships: {
|
|
26
|
+
* customer: belongsTo(() => Customers, { from: "customerId", to: "id" }),
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
import type { DatasetConfig, DatasetInstance, DimensionDefinition, MeasureDefinition, RelationshipDefinition } from './types.js';
|
|
32
|
+
export declare function dataset<TDimensions extends Record<string, DimensionDefinition>, TMeasures extends Record<string, MeasureDefinition> = Record<string, never>, TRelationships extends Record<string, RelationshipDefinition> = Record<string, never>>(name: string, config: DatasetConfig<TDimensions, TMeasures, TRelationships>): DatasetInstance<TDimensions, TMeasures, TRelationships>;
|
|
33
|
+
//# sourceMappingURL=dataset.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dataset.d.ts","sourceRoot":"","sources":["../src/dataset.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EACV,aAAa,EACb,eAAe,EACf,mBAAmB,EACnB,iBAAiB,EACjB,sBAAsB,EAYvB,MAAM,YAAY,CAAC;AAqMpB,wBAAgB,OAAO,CACrB,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,EACvD,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAC3E,cAAc,SAAS,MAAM,CAAC,MAAM,EAAE,sBAAsB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAErF,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,aAAa,CAAC,WAAW,EAAE,SAAS,EAAE,cAAc,CAAC,GAC5D,eAAe,CAAC,WAAW,EAAE,SAAS,EAAE,cAAc,CAAC,CAgEzD"}
|
package/dist/dataset.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dataset() — creates a typed semantic model over a physical table.
|
|
3
|
+
*
|
|
4
|
+
* Relationships are modeled on the dataset today, but current query execution
|
|
5
|
+
* only supports same-dataset dimensions, measures, and metrics. Joined
|
|
6
|
+
* relationship traversal is not part of the shipped execution surface yet.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { dataset, dimension, measure, belongsTo } from '@hypequery/serve';
|
|
11
|
+
*
|
|
12
|
+
* const Orders = dataset("orders", {
|
|
13
|
+
* source: "orders",
|
|
14
|
+
* tenantKey: "tenant_id",
|
|
15
|
+
* timeKey: "created_at",
|
|
16
|
+
* dimensions: {
|
|
17
|
+
* id: dimension.string(),
|
|
18
|
+
* status: dimension.string({ label: "Order Status" }),
|
|
19
|
+
* createdAt: dimension.timestamp({ column: "created_at" }),
|
|
20
|
+
* },
|
|
21
|
+
* measures: {
|
|
22
|
+
* totalRevenue: measure.sum("amount", { label: "Total Revenue" }),
|
|
23
|
+
* orderCount: measure.count("id"),
|
|
24
|
+
* },
|
|
25
|
+
* relationships: {
|
|
26
|
+
* customer: belongsTo(() => Customers, { from: "customerId", to: "id" }),
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
const ALL_GRAINS = ['day', 'week', 'month', 'quarter', 'year'];
|
|
32
|
+
const NUMERIC_FIELD_TYPES = new Set(['number']);
|
|
33
|
+
function isDerivedMetricConfig(config) {
|
|
34
|
+
return 'uses' in config && 'formula' in config;
|
|
35
|
+
}
|
|
36
|
+
function buildContract(metricName, ds, spec, label, description, grain) {
|
|
37
|
+
const dimensionNames = Object.keys(ds.dimensions);
|
|
38
|
+
const measureNames = Object.keys(ds.measures);
|
|
39
|
+
const filterNames = Object.keys(ds.filters).length > 0
|
|
40
|
+
? Object.keys(ds.filters)
|
|
41
|
+
: dimensionNames.filter((name) => ds.dimensions[name]?.filterable !== false);
|
|
42
|
+
const kind = grain
|
|
43
|
+
? 'grained_metric'
|
|
44
|
+
: spec.__type === 'derived_metric_spec'
|
|
45
|
+
? 'derived_metric'
|
|
46
|
+
: 'metric';
|
|
47
|
+
return {
|
|
48
|
+
kind,
|
|
49
|
+
name: metricName,
|
|
50
|
+
dataset: ds.name,
|
|
51
|
+
valueType: 'number',
|
|
52
|
+
label,
|
|
53
|
+
description,
|
|
54
|
+
dimensions: dimensionNames,
|
|
55
|
+
measures: measureNames,
|
|
56
|
+
filters: filterNames,
|
|
57
|
+
grains: ds.timeKey ? ALL_GRAINS : [],
|
|
58
|
+
grain,
|
|
59
|
+
requires: spec.__type === 'derived_metric_spec'
|
|
60
|
+
? Object.keys(spec.uses)
|
|
61
|
+
: undefined,
|
|
62
|
+
tenantScoped: !!ds.tenantKey,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function createMetricRef(ds, name, spec, label, description) {
|
|
66
|
+
const ref = {
|
|
67
|
+
__type: 'metric_ref',
|
|
68
|
+
datasetName: ds.name,
|
|
69
|
+
name,
|
|
70
|
+
spec,
|
|
71
|
+
label,
|
|
72
|
+
description,
|
|
73
|
+
dataset: ds,
|
|
74
|
+
by(grain) {
|
|
75
|
+
if (!ds.timeKey) {
|
|
76
|
+
throw new Error(`Cannot apply .by("${grain}") to metric "${name}" — ` +
|
|
77
|
+
`dataset "${ds.name}" has no timeKey defined.`);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
__type: 'grained_metric_ref',
|
|
81
|
+
metric: ref,
|
|
82
|
+
grain,
|
|
83
|
+
contract() {
|
|
84
|
+
return buildContract(name, ds, spec, label, description, grain);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
contract() {
|
|
89
|
+
return buildContract(name, ds, spec, label, description);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
return ref;
|
|
93
|
+
}
|
|
94
|
+
function validateBaseMetric(ds, metricName, spec, options) {
|
|
95
|
+
const dimension = ds.dimensions[spec.field];
|
|
96
|
+
if (!dimension && !options?.allowHiddenField) {
|
|
97
|
+
throw new Error(`Invalid metric "${metricName}": dimension "${spec.field}" does not exist on dataset "${ds.name}".`);
|
|
98
|
+
}
|
|
99
|
+
if (dimension &&
|
|
100
|
+
(spec.aggregation === 'sum' || spec.aggregation === 'avg') &&
|
|
101
|
+
!NUMERIC_FIELD_TYPES.has(dimension.fieldType)) {
|
|
102
|
+
throw new Error(`Invalid metric "${metricName}": ${spec.aggregation}() requires a numeric dimension, but "${spec.field}" is ${dimension.fieldType}.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function validateDerivedMetric(ds, metricName, config) {
|
|
106
|
+
const usedMetrics = Object.entries(config.uses);
|
|
107
|
+
if (usedMetrics.length === 0) {
|
|
108
|
+
throw new Error(`Invalid metric "${metricName}": derived metrics must reference at least one base metric.`);
|
|
109
|
+
}
|
|
110
|
+
for (const [alias, metric] of usedMetrics) {
|
|
111
|
+
if (metric.datasetName !== ds.name) {
|
|
112
|
+
throw new Error(`Invalid metric "${metricName}": referenced metric "${alias}" belongs to dataset "${metric.datasetName}", expected "${ds.name}".`);
|
|
113
|
+
}
|
|
114
|
+
if (metric.spec.__type !== 'aggregation_spec') {
|
|
115
|
+
throw new Error(`Invalid metric "${metricName}": referenced metric "${alias}" must be a base aggregation on dataset "${ds.name}".`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function normalizeDimensions(config) {
|
|
120
|
+
return config.dimensions;
|
|
121
|
+
}
|
|
122
|
+
function normalizeFilters(dimensions, filters) {
|
|
123
|
+
if (filters) {
|
|
124
|
+
return filters;
|
|
125
|
+
}
|
|
126
|
+
return Object.fromEntries(Object.entries(dimensions)
|
|
127
|
+
.filter(([, definition]) => definition.filterable !== false)
|
|
128
|
+
.map(([name]) => [
|
|
129
|
+
name,
|
|
130
|
+
{
|
|
131
|
+
__type: 'filter_definition',
|
|
132
|
+
field: name,
|
|
133
|
+
},
|
|
134
|
+
]));
|
|
135
|
+
}
|
|
136
|
+
function measureToAggregationSpec(measureName, definition) {
|
|
137
|
+
if (!definition.field) {
|
|
138
|
+
throw new Error(`Invalid measure "${measureName}": a backing field is required.`);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
__type: 'aggregation_spec',
|
|
142
|
+
aggregation: definition.aggregation,
|
|
143
|
+
field: definition.field,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function buildDatasetQueryContract(ds, config) {
|
|
147
|
+
return {
|
|
148
|
+
dataset: ds.name,
|
|
149
|
+
dimensions: config.dimensions ? [...config.dimensions] : Object.keys(ds.dimensions),
|
|
150
|
+
measures: config.measures ? [...config.measures] : Object.keys(ds.measures),
|
|
151
|
+
filters: Object.keys(ds.filters),
|
|
152
|
+
grains: ds.timeKey ? ALL_GRAINS : [],
|
|
153
|
+
tenantScoped: !!ds.tenantKey,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
export function dataset(name, config) {
|
|
157
|
+
const dimensions = normalizeDimensions(config);
|
|
158
|
+
const measures = (config.measures ?? {});
|
|
159
|
+
const filters = normalizeFilters(dimensions, config.filters);
|
|
160
|
+
const ds = {
|
|
161
|
+
__type: 'dataset',
|
|
162
|
+
name,
|
|
163
|
+
source: config.source,
|
|
164
|
+
tenantKey: config.tenantKey,
|
|
165
|
+
timeKey: config.timeKey,
|
|
166
|
+
dimensions,
|
|
167
|
+
measures,
|
|
168
|
+
filters,
|
|
169
|
+
relationships: (config.relationships ?? {}),
|
|
170
|
+
limits: config.limits,
|
|
171
|
+
metric(metricName, metricConfig) {
|
|
172
|
+
if (isDerivedMetricConfig(metricConfig)) {
|
|
173
|
+
validateDerivedMetric(ds, metricName, metricConfig);
|
|
174
|
+
const derivedSpec = {
|
|
175
|
+
__type: 'derived_metric_spec',
|
|
176
|
+
uses: metricConfig.uses,
|
|
177
|
+
formula: metricConfig.formula,
|
|
178
|
+
};
|
|
179
|
+
return createMetricRef(ds, metricName, derivedSpec, metricConfig.label, metricConfig.description);
|
|
180
|
+
}
|
|
181
|
+
// Base metric (measure-based)
|
|
182
|
+
const measureName = metricConfig.measure;
|
|
183
|
+
const measure = ds.measures[measureName];
|
|
184
|
+
if (!measure) {
|
|
185
|
+
throw new Error(`Invalid metric "${metricName}": measure "${measureName}" does not exist on dataset "${ds.name}".`);
|
|
186
|
+
}
|
|
187
|
+
const spec = measureToAggregationSpec(measureName, measure);
|
|
188
|
+
validateBaseMetric(ds, metricName, spec, { allowHiddenField: true });
|
|
189
|
+
return createMetricRef(ds, metricName, spec, metricConfig.label ?? measure.label, metricConfig.description ?? measure.description);
|
|
190
|
+
},
|
|
191
|
+
query(queryConfig) {
|
|
192
|
+
return {
|
|
193
|
+
__type: 'dataset_query_ref',
|
|
194
|
+
dataset: ds,
|
|
195
|
+
config: queryConfig,
|
|
196
|
+
contract() {
|
|
197
|
+
return buildDatasetQueryContract(ds, queryConfig);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
return ds;
|
|
203
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MetricExecutor — resolves metrics to SQL and executes them.
|
|
3
|
+
*
|
|
4
|
+
* Supports two modes:
|
|
5
|
+
* 1. **Query builder** (recommended) — pass a `QueryBuilderFactoryLike` and the
|
|
6
|
+
* executor builds queries via the builder's fluent API, then calls `.execute()`.
|
|
7
|
+
* 2. **Raw adapter** (deprecated) — pass a `MetricAdapter` with a `rawQuery` function
|
|
8
|
+
* and the executor generates SQL strings manually.
|
|
9
|
+
*
|
|
10
|
+
* The executor stays DB-agnostic via duck-typed protocol interfaces.
|
|
11
|
+
*/
|
|
12
|
+
import type { MetricRef, GrainedMetricRef, MetricQuery, MetricResult, ExecutionContext } from './types.js';
|
|
13
|
+
import type { QueryBuilderFactoryLike } from './query-builder-protocol.js';
|
|
14
|
+
import { type ValidationResult } from './validation.js';
|
|
15
|
+
export interface MetricExecutorOptions {
|
|
16
|
+
/** Query builder factory for executing metrics. */
|
|
17
|
+
builderFactory: QueryBuilderFactoryLike;
|
|
18
|
+
}
|
|
19
|
+
export declare class MetricExecutor {
|
|
20
|
+
private builderFactory;
|
|
21
|
+
constructor(options: MetricExecutorOptions);
|
|
22
|
+
getBuilderFactory(): QueryBuilderFactoryLike;
|
|
23
|
+
/**
|
|
24
|
+
* Execute a metric query. Generates SQL, applies tenant/filter context, executes.
|
|
25
|
+
*/
|
|
26
|
+
run<T = Record<string, unknown>>(metric: MetricRef | GrainedMetricRef, query?: MetricQuery, context?: ExecutionContext): Promise<MetricResult<T>>;
|
|
27
|
+
/**
|
|
28
|
+
* Generate SQL without executing.
|
|
29
|
+
*/
|
|
30
|
+
toSQL(metric: MetricRef | GrainedMetricRef, query?: MetricQuery, context?: ExecutionContext): string;
|
|
31
|
+
/**
|
|
32
|
+
* Validate a metric query against the metric's contract.
|
|
33
|
+
*/
|
|
34
|
+
validate(metric: MetricRef | GrainedMetricRef, query: MetricQuery): ValidationResult;
|
|
35
|
+
private runViaBuilder;
|
|
36
|
+
private buildBaseQuery;
|
|
37
|
+
private buildDerivedSQLViaBuilder;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=executor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../src/executor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,YAAY,EAGZ,gBAAgB,EAOjB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAEV,uBAAuB,EACxB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EAAyC,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAwG/F,MAAM,WAAW,qBAAqB;IACpC,mDAAmD;IACnD,cAAc,EAAE,uBAAuB,CAAC;CACzC;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,cAAc,CAA0B;gBAEpC,OAAO,EAAE,qBAAqB;IAI1C,iBAAiB,IAAI,uBAAuB;IAI5C;;OAEG;IACG,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,MAAM,EAAE,SAAS,GAAG,gBAAgB,EACpC,KAAK,GAAE,WAAgB,EACvB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAU3B;;OAEG;IACH,KAAK,CACH,MAAM,EAAE,SAAS,GAAG,gBAAgB,EACpC,KAAK,GAAE,WAAgB,EACvB,OAAO,CAAC,EAAE,gBAAgB,GACzB,MAAM;IAaT;;OAEG;IACH,QAAQ,CACN,MAAM,EAAE,SAAS,GAAG,gBAAgB,EACpC,KAAK,EAAE,WAAW,GACjB,gBAAgB;YAQL,aAAa;IA2B3B,OAAO,CAAC,cAAc;IAgDtB,OAAO,CAAC,yBAAyB;CA0FlC"}
|