@ahoo-wang/wow-client 10.0.0-alpha.5 → 10.0.0-alpha.7
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/README.md +76 -23
- package/README.zh-CN.md +66 -19
- package/dist/commands.cjs +1 -1
- package/dist/commands.cjs.map +1 -1
- package/dist/commands.d.cts +131 -61
- package/dist/commands.d.ts +131 -61
- package/dist/commands.es.js +117 -59
- package/dist/commands.es.js.map +1 -1
- package/dist/developmentCaller.cjs +2 -0
- package/dist/developmentCaller.cjs.map +1 -0
- package/dist/developmentCaller.d.cts +61 -0
- package/dist/developmentCaller.d.ts +61 -0
- package/dist/developmentCaller.es.js +26 -0
- package/dist/developmentCaller.es.js.map +1 -0
- package/dist/dsl/aggregation/admit.cjs +1 -1
- package/dist/dsl/aggregation/admit.cjs.map +1 -1
- package/dist/dsl/aggregation/admit.es.js +93 -93
- package/dist/dsl/aggregation/admit.es.js.map +1 -1
- package/dist/dsl/aggregation/builders.cjs +1 -1
- package/dist/dsl/aggregation/builders.cjs.map +1 -1
- package/dist/dsl/aggregation/builders.d.cts +20 -20
- package/dist/dsl/aggregation/builders.d.ts +20 -20
- package/dist/dsl/aggregation/builders.es.js +88 -88
- package/dist/dsl/aggregation/builders.es.js.map +1 -1
- package/dist/dsl/aggregation/derived.cjs.map +1 -1
- package/dist/dsl/aggregation/derived.d.cts +8 -8
- package/dist/dsl/aggregation/derived.d.ts +8 -8
- package/dist/dsl/aggregation/derived.es.js +1 -2
- package/dist/dsl/aggregation/derived.es.js.map +1 -1
- package/dist/dsl/aggregation/expressionTrees.cjs.map +1 -1
- package/dist/dsl/aggregation/expressionTrees.es.js +1 -2
- package/dist/dsl/aggregation/having.cjs +1 -1
- package/dist/dsl/aggregation/having.cjs.map +1 -1
- package/dist/dsl/aggregation/having.d.cts +12 -12
- package/dist/dsl/aggregation/having.d.ts +12 -12
- package/dist/dsl/aggregation/having.es.js +33 -33
- package/dist/dsl/aggregation/having.es.js.map +1 -1
- package/dist/dsl/aggregation/sort.cjs.map +1 -1
- package/dist/dsl/aggregation/sort.es.js +1 -2
- package/dist/dsl/aggregation/types.cjs +1 -1
- package/dist/dsl/aggregation/types.cjs.map +1 -1
- package/dist/dsl/aggregation/types.d.cts +69 -69
- package/dist/dsl/aggregation/types.d.ts +69 -69
- package/dist/dsl/aggregation/types.es.js +15 -16
- package/dist/dsl/aggregation/types.es.js.map +1 -1
- package/dist/dsl/constantName.cjs +2 -0
- package/dist/dsl/constantName.cjs.map +1 -0
- package/dist/dsl/constantName.d.cts +9 -0
- package/dist/dsl/constantName.d.ts +9 -0
- package/dist/dsl/constantName.es.js +7 -0
- package/dist/dsl/constantName.es.js.map +1 -0
- package/dist/dsl/cursorQuery.cjs.map +1 -1
- package/dist/dsl/cursorQuery.es.js +1 -2
- package/dist/dsl/deletionState.cjs +1 -1
- package/dist/dsl/deletionState.cjs.map +1 -1
- package/dist/dsl/deletionState.d.cts +3 -3
- package/dist/dsl/deletionState.d.ts +3 -3
- package/dist/dsl/deletionState.es.js +2 -3
- package/dist/dsl/deletionState.es.js.map +1 -1
- package/dist/dsl/descriptor.cjs +1 -1
- package/dist/dsl/descriptor.cjs.map +1 -1
- package/dist/dsl/descriptor.d.cts +92 -42
- package/dist/dsl/descriptor.d.ts +92 -42
- package/dist/dsl/descriptor.es.js +36 -29
- package/dist/dsl/descriptor.es.js.map +1 -1
- package/dist/dsl/field.cjs.map +1 -1
- package/dist/dsl/field.es.js +1 -2
- package/dist/dsl/filter/builders.cjs +1 -1
- package/dist/dsl/filter/builders.cjs.map +1 -1
- package/dist/dsl/filter/builders.d.cts +53 -53
- package/dist/dsl/filter/builders.d.ts +53 -53
- package/dist/dsl/filter/builders.es.js +111 -111
- package/dist/dsl/filter/builders.es.js.map +1 -1
- package/dist/dsl/filter/datePattern.cjs.map +1 -1
- package/dist/dsl/filter/datePattern.es.js +1 -2
- package/dist/dsl/filter/operator.cjs +1 -1
- package/dist/dsl/filter/operator.cjs.map +1 -1
- package/dist/dsl/filter/operator.d.cts +57 -57
- package/dist/dsl/filter/operator.d.ts +57 -57
- package/dist/dsl/filter/operator.es.js +4 -5
- package/dist/dsl/filter/operator.es.js.map +1 -1
- package/dist/dsl/filter/scope.cjs +1 -1
- package/dist/dsl/filter/scope.cjs.map +1 -1
- package/dist/dsl/filter/scope.es.js +12 -12
- package/dist/dsl/filter/scope.es.js.map +1 -1
- package/dist/dsl/filter/validate.cjs +1 -1
- package/dist/dsl/filter/validate.cjs.map +1 -1
- package/dist/dsl/filter/validate.es.js +23 -23
- package/dist/dsl/filter/validate.es.js.map +1 -1
- package/dist/dsl/pagination.cjs.map +1 -1
- package/dist/dsl/pagination.es.js +1 -2
- package/dist/dsl/projection.cjs.map +1 -1
- package/dist/dsl/projection.d.cts +1 -1
- package/dist/dsl/projection.d.ts +1 -1
- package/dist/dsl/projection.es.js +1 -2
- package/dist/dsl/projection.es.js.map +1 -1
- package/dist/dsl/queryErrorCodes.cjs +1 -1
- package/dist/dsl/queryErrorCodes.cjs.map +1 -1
- package/dist/dsl/queryErrorCodes.d.cts +59 -48
- package/dist/dsl/queryErrorCodes.d.ts +59 -48
- package/dist/dsl/queryErrorCodes.es.js +51 -50
- package/dist/dsl/queryErrorCodes.es.js.map +1 -1
- package/dist/dsl/queryable.cjs +1 -1
- package/dist/dsl/queryable.cjs.map +1 -1
- package/dist/dsl/queryable.d.cts +25 -21
- package/dist/dsl/queryable.d.ts +25 -21
- package/dist/dsl/queryable.es.js +5 -5
- package/dist/dsl/queryable.es.js.map +1 -1
- package/dist/dsl/sort.cjs +1 -1
- package/dist/dsl/sort.cjs.map +1 -1
- package/dist/dsl/sort.d.cts +2 -2
- package/dist/dsl/sort.d.ts +2 -2
- package/dist/dsl/sort.es.js +4 -5
- package/dist/dsl/sort.es.js.map +1 -1
- package/dist/dsl.cjs +1 -1
- package/dist/dsl.es.js +3 -3
- package/dist/exactJson.cjs.map +1 -1
- package/dist/exactJson.d.cts +4 -4
- package/dist/exactJson.d.ts +4 -4
- package/dist/exactJson.es.js +1 -2
- package/dist/exactJson.es.js.map +1 -1
- package/dist/frameworkErrors.cjs +2 -0
- package/dist/frameworkErrors.cjs.map +1 -0
- package/dist/frameworkErrors.d.cts +121 -0
- package/dist/frameworkErrors.d.ts +121 -0
- package/dist/frameworkErrors.es.js +95 -0
- package/dist/frameworkErrors.es.js.map +1 -0
- package/dist/http.cjs +1 -1
- package/dist/http.cjs.map +1 -1
- package/dist/http.d.cts +4 -7
- package/dist/http.d.ts +4 -7
- package/dist/http.es.js +12 -20
- package/dist/http.es.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.es.js +11 -9
- package/dist/manifest.cjs +1 -1
- package/dist/manifest.cjs.map +1 -1
- package/dist/manifest.d.cts +33 -95
- package/dist/manifest.d.ts +33 -95
- package/dist/manifest.es.js +12 -7
- package/dist/manifest.es.js.map +1 -1
- package/dist/manifestTypes.d.cts +193 -0
- package/dist/manifestTypes.d.ts +193 -0
- package/dist/problem.cjs +1 -1
- package/dist/problem.cjs.map +1 -1
- package/dist/problem.d.cts +76 -111
- package/dist/problem.d.ts +76 -111
- package/dist/problem.es.js +45 -78
- package/dist/problem.es.js.map +1 -1
- package/dist/queries.cjs +1 -1
- package/dist/queries.cjs.map +1 -1
- package/dist/queries.d.cts +28 -51
- package/dist/queries.d.ts +28 -51
- package/dist/queries.es.js +49 -40
- package/dist/queries.es.js.map +1 -1
- package/dist/routes.cjs +2 -0
- package/dist/routes.cjs.map +1 -0
- package/dist/routes.d.cts +49 -0
- package/dist/routes.d.ts +49 -0
- package/dist/routes.es.js +33 -0
- package/dist/routes.es.js.map +1 -0
- package/dist/state.cjs +1 -1
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +56 -11
- package/dist/state.d.ts +56 -11
- package/dist/state.es.js +56 -21
- package/dist/state.es.js.map +1 -1
- package/dist/streams.cjs +1 -1
- package/dist/streams.cjs.map +1 -1
- package/dist/streams.d.cts +17 -13
- package/dist/streams.d.ts +17 -13
- package/dist/streams.es.js +50 -50
- package/dist/streams.es.js.map +1 -1
- package/dist/testing/wire.cjs +2 -0
- package/dist/testing/wire.cjs.map +1 -0
- package/dist/testing/wire.d.cts +29 -0
- package/dist/testing/wire.d.ts +29 -0
- package/dist/testing/wire.es.js +88 -0
- package/dist/testing/wire.es.js.map +1 -0
- package/dist/testing.cjs +1 -0
- package/dist/testing.d.cts +12 -0
- package/dist/testing.d.ts +12 -0
- package/dist/testing.es.js +3 -0
- package/dist/wait.cjs +1 -1
- package/dist/wait.cjs.map +1 -1
- package/dist/wait.d.cts +67 -18
- package/dist/wait.d.ts +67 -18
- package/dist/wait.es.js +59 -12
- package/dist/wait.es.js.map +1 -1
- package/dist/wire.cjs +1 -1
- package/dist/wire.cjs.map +1 -1
- package/dist/wire.d.cts +100 -50
- package/dist/wire.d.ts +100 -50
- package/dist/wire.es.js +17 -6
- package/dist/wire.es.js.map +1 -1
- package/package.json +26 -7
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { RequestInterceptor } from '@ahoo-wang/fetcher';
|
|
2
|
+
/**
|
|
3
|
+
* The identity headers a development run of a Wow service reads the caller
|
|
4
|
+
* from (the host's development callers, in a profile of `wow.dev-profiles`,
|
|
5
|
+
* when CoSec is off): any client can set them, so a deployment never reads
|
|
6
|
+
* them. Without {@link DevelopmentCallerHeaders.USER} the request is the
|
|
7
|
+
* anonymous caller of the default tenant, and only
|
|
8
|
+
* {@link DevelopmentCallerHeaders.APP} is kept.
|
|
9
|
+
*
|
|
10
|
+
* A console that offers a local run's caller, and a test, send them with
|
|
11
|
+
* {@link developmentCaller}; `@ahoo-wang/wow-client/testing` exports it
|
|
12
|
+
* too.
|
|
13
|
+
*/
|
|
14
|
+
export declare const DevelopmentCallerHeaders: {
|
|
15
|
+
/** The caller's subject. */
|
|
16
|
+
readonly USER: "Wow-User";
|
|
17
|
+
/** The owner of its scope: an owner-level caller; a tenant-level one without it. */
|
|
18
|
+
readonly OWNER: "Wow-Owner";
|
|
19
|
+
/** Its tenant; the service's default tenant without it. */
|
|
20
|
+
readonly TENANT: "Wow-Tenant";
|
|
21
|
+
/** Its roles, comma-separated. */
|
|
22
|
+
readonly ROLES: "Wow-Roles";
|
|
23
|
+
/** The application it calls in, which each command records as its `app_id`. */
|
|
24
|
+
readonly APP: "Wow-App";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Who a request comes from, as a development run of a Wow service reads it
|
|
28
|
+
* ({@link DevelopmentCallerHeaders}).
|
|
29
|
+
*/
|
|
30
|
+
export interface DevelopmentCaller {
|
|
31
|
+
/** The caller's subject. */
|
|
32
|
+
readonly user: string;
|
|
33
|
+
/**
|
|
34
|
+
* The owner of its scope, for an owner-level caller; a tenant-level caller
|
|
35
|
+
* without it. The owner is never the user unless it says so.
|
|
36
|
+
*/
|
|
37
|
+
readonly owner?: string;
|
|
38
|
+
/** Its tenant; the service's default tenant (`wow.tenant.default`) without it. */
|
|
39
|
+
readonly tenant?: string;
|
|
40
|
+
/** Its roles. */
|
|
41
|
+
readonly roles?: readonly string[];
|
|
42
|
+
/** The application it calls in. */
|
|
43
|
+
readonly app?: string;
|
|
44
|
+
}
|
|
45
|
+
/** The headers that name `caller` to a development run: only those it sets. */
|
|
46
|
+
export declare function developmentCallerHeaders(caller: DevelopmentCaller): Record<string, string>;
|
|
47
|
+
/**
|
|
48
|
+
* A request interceptor that sends every request as `caller`, by the headers
|
|
49
|
+
* a development run reads ({@link developmentCallerHeaders}): a fetcher that
|
|
50
|
+
* uses it asks as that caller, as a host's CoSec interceptors send its token.
|
|
51
|
+
* Given a function, it sends the caller the function answers at each request,
|
|
52
|
+
* and nothing when it answers none.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const fetcher = new Fetcher({ baseURL });
|
|
56
|
+
* fetcher.interceptors.request.use(
|
|
57
|
+
* developmentCaller({ user: 'alice', owner: 'alice', tenant: 'acme' }),
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function developmentCaller(caller: DevelopmentCaller | (() => DevelopmentCaller | null | undefined)): RequestInterceptor;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/developmentCaller.ts
|
|
2
|
+
var e = {
|
|
3
|
+
USER: "Wow-User",
|
|
4
|
+
OWNER: "Wow-Owner",
|
|
5
|
+
TENANT: "Wow-Tenant",
|
|
6
|
+
ROLES: "Wow-Roles",
|
|
7
|
+
APP: "Wow-App"
|
|
8
|
+
};
|
|
9
|
+
function t(t) {
|
|
10
|
+
let n = { [e.USER]: t.user };
|
|
11
|
+
return t.owner !== void 0 && (n[e.OWNER] = t.owner), t.tenant !== void 0 && (n[e.TENANT] = t.tenant), t.roles?.length && (n[e.ROLES] = t.roles.join(",")), t.app !== void 0 && (n[e.APP] = t.app), n;
|
|
12
|
+
}
|
|
13
|
+
function n(e) {
|
|
14
|
+
let n = typeof e == "function" ? e : () => e;
|
|
15
|
+
return {
|
|
16
|
+
name: "DevelopmentCaller",
|
|
17
|
+
order: 0,
|
|
18
|
+
intercept(e) {
|
|
19
|
+
let r = n();
|
|
20
|
+
r && Object.assign(e.ensureRequestHeaders(), t(r));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export { e as DevelopmentCallerHeaders, n as developmentCaller, t as developmentCallerHeaders };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=developmentCaller.es.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"developmentCaller.es.js","names":["DevelopmentCallerHeaders","developmentCallerHeaders","headers","caller","developmentCaller","current","asking","exchange"],"sources":["../src/developmentCaller.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { FetchExchange, RequestInterceptor } from '@ahoo-wang/fetcher';\n\n/**\n * The identity headers a development run of a Wow service reads the caller\n * from (the host's development callers, in a profile of `wow.dev-profiles`,\n * when CoSec is off): any client can set them, so a deployment never reads\n * them. Without {@link DevelopmentCallerHeaders.USER} the request is the\n * anonymous caller of the default tenant, and only\n * {@link DevelopmentCallerHeaders.APP} is kept.\n *\n * A console that offers a local run's caller, and a test, send them with\n * {@link developmentCaller}; `@ahoo-wang/wow-client/testing` exports it\n * too.\n */\nexport const DevelopmentCallerHeaders = {\n /** The caller's subject. */\n USER: 'Wow-User',\n /** The owner of its scope: an owner-level caller; a tenant-level one without it. */\n OWNER: 'Wow-Owner',\n /** Its tenant; the service's default tenant without it. */\n TENANT: 'Wow-Tenant',\n /** Its roles, comma-separated. */\n ROLES: 'Wow-Roles',\n /** The application it calls in, which each command records as its `app_id`. */\n APP: 'Wow-App',\n} as const;\n\n/**\n * Who a request comes from, as a development run of a Wow service reads it\n * ({@link DevelopmentCallerHeaders}).\n */\nexport interface DevelopmentCaller {\n /** The caller's subject. */\n readonly user: string;\n /**\n * The owner of its scope, for an owner-level caller; a tenant-level caller\n * without it. The owner is never the user unless it says so.\n */\n readonly owner?: string;\n /** Its tenant; the service's default tenant (`wow.tenant.default`) without it. */\n readonly tenant?: string;\n /** Its roles. */\n readonly roles?: readonly string[];\n /** The application it calls in. */\n readonly app?: string;\n}\n\n/** The headers that name `caller` to a development run: only those it sets. */\nexport function developmentCallerHeaders(\n caller: DevelopmentCaller,\n): Record<string, string> {\n const headers: Record<string, string> = {\n [DevelopmentCallerHeaders.USER]: caller.user,\n };\n if (caller.owner !== undefined)\n headers[DevelopmentCallerHeaders.OWNER] = caller.owner;\n if (caller.tenant !== undefined)\n headers[DevelopmentCallerHeaders.TENANT] = caller.tenant;\n if (caller.roles?.length)\n headers[DevelopmentCallerHeaders.ROLES] = caller.roles.join(',');\n if (caller.app !== undefined)\n headers[DevelopmentCallerHeaders.APP] = caller.app;\n return headers;\n}\n\n/**\n * A request interceptor that sends every request as `caller`, by the headers\n * a development run reads ({@link developmentCallerHeaders}): a fetcher that\n * uses it asks as that caller, as a host's CoSec interceptors send its token.\n * Given a function, it sends the caller the function answers at each request,\n * and nothing when it answers none.\n *\n * ```ts\n * const fetcher = new Fetcher({ baseURL });\n * fetcher.interceptors.request.use(\n * developmentCaller({ user: 'alice', owner: 'alice', tenant: 'acme' }),\n * );\n * ```\n */\nexport function developmentCaller(\n caller: DevelopmentCaller | (() => DevelopmentCaller | null | undefined),\n): RequestInterceptor {\n const current = typeof caller === 'function' ? caller : () => caller;\n return {\n name: 'DevelopmentCaller',\n order: 0,\n intercept(exchange: FetchExchange): void {\n const asking = current();\n if (!asking) return;\n Object.assign(\n exchange.ensureRequestHeaders(),\n developmentCallerHeaders(asking),\n );\n },\n };\n}\n"],"mappings":";AA2BA,IAAaA,IAA2B;CAEtC,MAAM;CAEN,OAAO;CAEP,QAAQ;CAER,OAAO;CAEP,KAAK;AACP;AAuBA,SAAgBC,EACd,GACwB;CACxB,IAAMC,IAAkC,GACrCF,EAAyB,OAAOG,EAAO,KAC1C;CASA,OARIA,EAAO,UAAU,KAAA,MACnB,EAAQH,EAAyB,SAASG,EAAO,QAC/CA,EAAO,WAAW,KAAA,MACpB,EAAQH,EAAyB,UAAUG,EAAO,SAChDA,EAAO,OAAO,WAChB,EAAQH,EAAyB,SAASG,EAAO,MAAM,KAAK,GAAG,IAC7DA,EAAO,QAAQ,KAAA,MACjB,EAAQH,EAAyB,OAAOG,EAAO,MAC1CD;AACT;AAgBA,SAAgBE,EACd,GACoB;CACpB,IAAMC,IAAU,OAAOF,KAAW,aAAaA,UAAeA;CAC9D,OAAO;EACL,MAAM;EACN,OAAO;EACP,UAAU,GAA+B;GACvC,IAAMG,IAASD,EAAQ;GACvB,AAAKC,KACL,OAAO,OACLC,EAAS,qBAAqB,GAC9BN,EAAyBK,CAAM,CACjC;EACF;CACF;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../filter/operator.cjs"),t=require("./types.cjs"),
|
|
1
|
+
const e=require("../filter/operator.cjs"),t=require("../constantName.cjs"),n=require("./types.cjs"),r=require("./expressionTrees.cjs"),i=require("./sort.cjs");function a(e,t,n,r){if(n.length>r)throw TypeError(`${e} must contain at most ${r} ${t}.`)}function o(t){switch(t.op){case e.FilterOperator.SEARCH:throw TypeError(`Aggregation metric filters do not support search filters.`);case e.FilterOperator.ELEMENT_MATCH:throw TypeError(`Aggregation metric filters do not support [ELEMENT_MATCH].`);case e.FilterOperator.AND:case e.FilterOperator.OR:case e.FilterOperator.NOR:t.operands.forEach(o)}}function s(e,i){r.requireValidExpressionTrees(e.flatMap(e=>e.type===n.AggregationMetricType.NUMERIC||e.type===n.AggregationMetricType.DISTINCT_COUNT||e.type===n.AggregationMetricType.PERCENTILE?[e.expression]:[])),i.forEach(e=>{if(e.type===n.AggregationGroupType.TERMS||e.type===n.AggregationGroupType.HISTOGRAM){if(e.field===void 0==(e.expression===void 0))throw TypeError(`${t.constantName(e.type)} group requires exactly one of field and expression.`);if(e.expression&&r.requireValidExpressionTrees([e.expression]),e.type===n.AggregationGroupType.TERMS&&e.missingKey!==void 0&&e.expression)throw TypeError(`terms missingKey requires a field input.`);if(e.scope===n.AggregationGroupScope.ROOT&&e.expression)throw TypeError(`${t.constantName(e.type)} with a ROOT scope requires a field input.`)}})}var c=new Set([n.AggregationMetricType.ANY,n.AggregationMetricType.FIRST,n.AggregationMetricType.LAST]);function l(e){let t=new Map,r=new Set,i={nodes:0,windows:0};e.forEach(e=>{e.type===n.AggregationMetricType.DERIVED&&(u(e,t,r,i),r.add(e.alias)),t.set(e.alias,c.has(e.type)?e.type:void 0)})}function u(e,r,i,a){let o=[{expression:e.expression,depth:1}];for(;o.length>0;){let{expression:s,depth:c}=o.pop();if(c>n.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH)throw TypeError(`derived expression depth must be at most ${n.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`);if(a.nodes++,a.nodes>n.AGGREGATION_LIMITS.MAX_EXPRESSION_NODES)throw TypeError(`derived expressions must contain at most ${n.AGGREGATION_LIMITS.MAX_EXPRESSION_NODES} nodes.`);switch(s.type){case n.DerivedExpressionType.METRIC_REF:{let n=s.metric;if(!r.has(n))throw TypeError(`derived metric [${e.alias}] must reference a metric declared before it, but was [${n}].`);let i=r.get(n);if(i!==void 0)throw TypeError(`derived metric [${e.alias}] cannot reference ${t.constantName(i)} metric [${n}].`);break}case n.DerivedExpressionType.CONSTANT:if(!Number.isFinite(s.value))throw TypeError(`derived constant must be finite.`);break;case n.DerivedExpressionType.BINARY:o.push({expression:s.left,depth:c+1}),o.push({expression:s.right,depth:c+1});break;case n.DerivedExpressionType.TOTAL:case n.DerivedExpressionType.RUNNING:case n.DerivedExpressionType.PREVIOUS:case n.DerivedExpressionType.RANK:if(a.windows++,a.windows>n.AGGREGATION_LIMITS.MAX_WINDOWS)throw TypeError(`an aggregation must compute at most ${n.AGGREGATION_LIMITS.MAX_WINDOWS} windows.`);d(e.alias,s,r,i);break;default:throw TypeError(`Unsupported derived expression: ${String(s.type)}.`)}}}function d(e,n,r,i){let a=n.metric;if(!r.has(a))throw TypeError(`${t.constantName(n.type)} window of derived metric [${e}] must read a metric declared before it, but was [${a}].`);if(r.get(a)!==void 0||i.has(a))throw TypeError(`${t.constantName(n.type)} window of derived metric [${e}] must read a non-derived numeric metric, but was [${a}].`)}function f(e,t,n,r){if(!t.has(e))throw TypeError(`having condition [${e}] must reference a declared metric alias.`);if(n.has(e))throw TypeError(`having condition [${e}] cannot reference ANY metric.`);if(r.has(e))throw TypeError(`having condition [${e}] cannot reference FIRST or LAST metric.`)}function p(e,r,i){if(e===void 0)return;if(r.length===0)throw TypeError(`having requires at least one groupBy.`);let a=new Set(i.map(e=>e.alias)),o=new Set(i.filter(e=>e.type===n.AggregationMetricType.ANY).map(e=>e.alias)),s=new Set(i.filter(e=>e.type===n.AggregationMetricType.FIRST||e.type===n.AggregationMetricType.LAST).map(e=>e.alias)),c=[{expression:e,depth:1}];for(;c.length>0;){let{expression:e,depth:r}=c.pop();if(r>n.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH)throw TypeError(`having expression depth must be at most ${n.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`);switch(e.type){case n.HavingExpressionType.CONDITION:if(f(e.metric,a,o,s),!Number.isFinite(e.value))throw TypeError(`having condition [${e.metric}] value must be finite.`);break;case n.HavingExpressionType.BETWEEN:if(f(e.metric,a,o,s),!Number.isFinite(e.lower)||!Number.isFinite(e.upper))throw TypeError(`having between [${e.metric}] bounds must be finite.`);if(e.lower>e.upper)throw TypeError(`having between [${e.metric}] lower bound must not exceed upper bound.`);break;case n.HavingExpressionType.IN:if(f(e.metric,a,o,s),e.values.length===0)throw TypeError(`having in [${e.metric}] values must not be empty.`);if(!e.values.every(Number.isFinite))throw TypeError(`having in [${e.metric}] values must be finite.`);break;case n.HavingExpressionType.IS_NULL:f(e.metric,a,o,s);break;case n.HavingExpressionType.AND:case n.HavingExpressionType.OR:if(e.operands.length===0)throw TypeError(`having ${t.constantName(e.type)} operands must not be empty.`);e.operands.forEach(e=>c.push({expression:e,depth:r+1}));break;default:throw TypeError(`Unsupported having expression: ${String(e.type)}.`)}}}function m(e,t,r){if(a(`sort`,`fields`,e,n.AGGREGATION_LIMITS.MAX_SORT_FIELDS),t.length===0&&e.length>0)throw TypeError(`sort requires at least one groupBy.`);let o=e.map(e=>e.field);if(new Set(o).size!==o.length)throw TypeError(`sort fields must be unique.`);if(!o.every(e=>r.includes(e)))throw TypeError(`sort fields must reference aggregation aliases.`);a(`effective sort`,`fields`,i.effectiveSort({groupBy:[...t],sort:[...e]}),n.AGGREGATION_LIMITS.MAX_SORT_FIELDS)}function h(e){let t=new Map((e.groupBy??[]).map(e=>[e.alias,e.scope===n.AggregationGroupScope.ROOT])),r=i.effectiveSort(e).flatMap(e=>t.has(e.field)?[t.get(e.field)]:[]),a=r.indexOf(!1);if(a>=0&&r.slice(a).includes(!0))throw TypeError(`groups with a ROOT scope must sort before the element groups.`)}function g(e){let r=e.elements??[],i=e.groupBy??[],c=e.metrics,u=e.sort??[];if(a(`elements`,`paths`,r,n.AGGREGATION_LIMITS.MAX_ELEMENTS),a(`groupBy`,`dimensions`,i,n.AGGREGATION_LIMITS.MAX_GROUPS),!Array.isArray(c)||c.length===0)throw TypeError(`metrics must not be empty.`);if(a(`metrics`,`entries`,c,n.AGGREGATION_LIMITS.MAX_METRICS),e.limit!==void 0&&(!Number.isInteger(e.limit)||e.limit<1||e.limit>n.AGGREGATION_LIMITS.MAX_LIMIT))throw TypeError(`limit must be between 1 and ${n.AGGREGATION_LIMITS.MAX_LIMIT}.`);s(c,i),l(c),p(e.having,i,c),c.forEach(e=>{`filter`in e&&e.filter&&o(e.filter)}),i.forEach(e=>{if((e.type===n.AggregationGroupType.DATE_HISTOGRAM||e.type===n.AggregationGroupType.DATE_PART)&&e.dense===!0&&i.length!==1)throw TypeError(`dense requires ${t.constantName(e.type)} to be the only groupBy.`)});let d=[...i.map(e=>e.alias),...c.map(e=>e.alias)];if(new Set(d).size!==d.length)throw TypeError(`aggregation aliases must be unique.`);return m(u,i,d),r.length>0&&h(e),{...e,metrics:[...c]}}exports.admitAggregationQuery=g;
|
|
2
2
|
//# sourceMappingURL=admit.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admit.cjs","names":["FilterOperator","AggregationMetricType","AggregationGroupType","AggregationGroupScope","AGGREGATION_LIMITS","DerivedExpressionType","HavingExpressionType","effectiveSort","require_operator","require_types","require_expressionTrees","require_sort","requireAtMost","items","max","name","noun","validateMetricFilter","filter","validateExpressions","metrics","metric","groupBy","group","NON_NUMERIC","validateDerivedMetrics","declared","derived","counter","validateDerivedExpression","pending","depth","expression","reference","nonNumeric","requireWindowMetric","target","window","alias","requireValidHavingMetric","metricAliases","anyAliases","edgeAliases","validateHaving","having","operand","validateSort","sort","fields","entry","aliases","field","requireRootGroupsFirst","root","query","order","firstElement","admitAggregationQuery","elements"],"sources":["../../../src/dsl/aggregation/admit.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { FilterOperator, type FilterExpression } from '../filter/index.js';\nimport type { FieldSort } from '../sort.js';\nimport { requireValidExpressionTrees } from './expressionTrees.js';\nimport { effectiveSort } from './sort.js';\nimport {\n AGGREGATION_LIMITS,\n AggregationGroupScope,\n AggregationGroupType,\n AggregationMetricType,\n DerivedExpressionType,\n HavingExpressionType,\n type AggregationGroup,\n type AggregationMetric,\n type AggregationQuery,\n type DerivedAggregationMetric,\n type DerivedExpression,\n type DerivedWindow,\n type HavingExpression,\n} from './types.js';\n\n// Internal: the rules `aggregation.query()` checks across the parts of a\n// query. index.ts does not re-export this file.\n\nfunction requireAtMost(\n name: string,\n noun: string,\n items: readonly unknown[],\n max: number,\n): void {\n if (items.length > max) {\n throw new TypeError(`${name} must contain at most ${max} ${noun}.`);\n }\n}\n\n/**\n * Refuses the filters a metric cannot carry, whatever the aggregate holds.\n *\n * A metric filter is a whole-value predicate on one record — MongoDB turns it\n * into a `$cond` guard and Elasticsearch into a filter aggregation — and\n * element matching and full text have no whole-value reading. Both backends\n * refuse them before touching a schema, so the shape alone settles it. What a\n * schema would add, refusing an array-valued field, stays on the server.\n */\nfunction validateMetricFilter(filter: FilterExpression): void {\n switch (filter.op) {\n case FilterOperator.SEARCH:\n throw new TypeError(\n 'Aggregation metric filters do not support search filters.',\n );\n case FilterOperator.ELEMENT_MATCH:\n throw new TypeError(\n 'Aggregation metric filters do not support [ELEMENT_MATCH].',\n );\n case FilterOperator.AND:\n case FilterOperator.OR:\n case FilterOperator.NOR:\n filter.operands.forEach(validateMetricFilter);\n break;\n }\n}\n\n/**\n * Walks the arithmetic of every metric that carries an expression, as one\n * budget, and of each expression group on its own, as Wow does.\n */\nfunction validateExpressions(\n metrics: readonly AggregationMetric[],\n groupBy: readonly AggregationGroup[],\n): void {\n requireValidExpressionTrees(\n metrics.flatMap(metric =>\n metric.type === AggregationMetricType.NUMERIC ||\n metric.type === AggregationMetricType.DISTINCT_COUNT ||\n metric.type === AggregationMetricType.PERCENTILE\n ? [metric.expression]\n : [],\n ),\n );\n groupBy.forEach(group => {\n if (\n group.type !== AggregationGroupType.TERMS &&\n group.type !== AggregationGroupType.HISTOGRAM\n )\n return;\n if ((group.field === undefined) === (group.expression === undefined)) {\n throw new TypeError(\n `${group.type} group requires exactly one of field and expression.`,\n );\n }\n if (group.expression) requireValidExpressionTrees([group.expression]);\n if (\n group.type === AggregationGroupType.TERMS &&\n group.missingKey !== undefined &&\n group.expression\n ) {\n throw new TypeError('terms missingKey requires a field input.');\n }\n if (group.scope === AggregationGroupScope.ROOT && group.expression) {\n throw new TypeError(\n `${group.type} with a ROOT scope requires a field input.`,\n );\n }\n });\n}\n\n/** The metric types whose value is not a number: derived and having refuse them. */\nconst NON_NUMERIC: ReadonlySet<string> = new Set([\n AggregationMetricType.ANY,\n AggregationMetricType.FIRST,\n AggregationMetricType.LAST,\n]);\n\n/**\n * Admits the derived metrics against what was declared before each of them.\n *\n * Derived arithmetic runs after the aggregation, over values the query has\n * already produced, so a reference may only name a metric declared earlier —\n * that is what makes the list evaluable in one pass — and never an `ANY`,\n * `FIRST` or `LAST` metric, whose value is a field's value rather than a\n * number.\n */\nfunction validateDerivedMetrics(metrics: readonly AggregationMetric[]): void {\n // Each earlier alias, with its type when that is not a number.\n const declared = new Map<string, string | undefined>();\n const derived = new Set<string>();\n const counter = { nodes: 0, windows: 0 };\n metrics.forEach(metric => {\n if (metric.type === AggregationMetricType.DERIVED) {\n validateDerivedExpression(metric, declared, derived, counter);\n derived.add(metric.alias);\n }\n declared.set(\n metric.alias,\n NON_NUMERIC.has(metric.type) ? metric.type : undefined,\n );\n });\n}\n\nfunction validateDerivedExpression(\n metric: DerivedAggregationMetric,\n declared: ReadonlyMap<string, string | undefined>,\n derived: ReadonlySet<string>,\n counter: { nodes: number; windows: number },\n): void {\n const pending: { expression: DerivedExpression; depth: number }[] = [\n { expression: metric.expression, depth: 1 },\n ];\n while (pending.length > 0) {\n const { expression, depth } = pending.pop()!;\n if (depth > AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH) {\n throw new TypeError(\n `derived expression depth must be at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`,\n );\n }\n counter.nodes++;\n if (counter.nodes > AGGREGATION_LIMITS.MAX_EXPRESSION_NODES) {\n throw new TypeError(\n `derived expressions must contain at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_NODES} nodes.`,\n );\n }\n switch (expression.type) {\n case DerivedExpressionType.METRIC_REF: {\n const reference = expression.metric;\n if (!declared.has(reference)) {\n throw new TypeError(\n `derived metric [${metric.alias}] must reference a metric declared before it, but was [${reference}].`,\n );\n }\n const nonNumeric = declared.get(reference);\n if (nonNumeric !== undefined) {\n throw new TypeError(\n `derived metric [${metric.alias}] cannot reference ${nonNumeric} metric [${reference}].`,\n );\n }\n break;\n }\n case DerivedExpressionType.CONSTANT:\n if (!Number.isFinite(expression.value)) {\n throw new TypeError('derived constant must be finite.');\n }\n break;\n case DerivedExpressionType.BINARY:\n pending.push({ expression: expression.left, depth: depth + 1 });\n pending.push({ expression: expression.right, depth: depth + 1 });\n break;\n case DerivedExpressionType.TOTAL:\n case DerivedExpressionType.RUNNING:\n case DerivedExpressionType.PREVIOUS:\n case DerivedExpressionType.RANK:\n counter.windows++;\n if (counter.windows > AGGREGATION_LIMITS.MAX_WINDOWS) {\n throw new TypeError(\n `an aggregation must compute at most ${AGGREGATION_LIMITS.MAX_WINDOWS} windows.`,\n );\n }\n requireWindowMetric(metric.alias, expression, declared, derived);\n break;\n default:\n throw new TypeError(\n `Unsupported derived expression: ${String((expression as { type: unknown }).type)}.`,\n );\n }\n }\n}\n\n/**\n * A window reads a non-derived numeric metric declared before the derived\n * one; the rest of its rules (which metrics add up, which groups it names)\n * the server checks against the whole query.\n */\nfunction requireWindowMetric(\n alias: string,\n window: DerivedWindow,\n declared: ReadonlyMap<string, string | undefined>,\n derived: ReadonlySet<string>,\n): void {\n const target = window.metric;\n if (!declared.has(target)) {\n throw new TypeError(\n `${window.type} window of derived metric [${alias}] must read a metric declared before it, but was [${target}].`,\n );\n }\n if (declared.get(target) !== undefined || derived.has(target)) {\n throw new TypeError(\n `${window.type} window of derived metric [${alias}] must read a non-derived numeric metric, but was [${target}].`,\n );\n }\n}\n\nfunction requireValidHavingMetric(\n metric: string,\n metricAliases: ReadonlySet<string>,\n anyAliases: ReadonlySet<string>,\n edgeAliases: ReadonlySet<string>,\n): void {\n if (!metricAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] must reference a declared metric alias.`,\n );\n }\n if (anyAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] cannot reference ANY metric.`,\n );\n }\n if (edgeAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] cannot reference FIRST or LAST metric.`,\n );\n }\n}\n\n/**\n * `having` filters the grouped rows, so it needs rows to filter: a query with\n * no `groupBy` produces one row and has nothing to select from.\n */\nfunction validateHaving(\n having: HavingExpression | undefined,\n groupBy: readonly AggregationGroup[],\n metrics: readonly AggregationMetric[],\n): void {\n if (having === undefined) return;\n if (groupBy.length === 0) {\n throw new TypeError('having requires at least one groupBy.');\n }\n const metricAliases = new Set(metrics.map(metric => metric.alias));\n const anyAliases = new Set(\n metrics\n .filter(metric => metric.type === AggregationMetricType.ANY)\n .map(metric => metric.alias),\n );\n const edgeAliases = new Set(\n metrics\n .filter(\n metric =>\n metric.type === AggregationMetricType.FIRST ||\n metric.type === AggregationMetricType.LAST,\n )\n .map(metric => metric.alias),\n );\n\n const pending: { expression: HavingExpression; depth: number }[] = [\n { expression: having, depth: 1 },\n ];\n while (pending.length > 0) {\n const { expression, depth } = pending.pop()!;\n if (depth > AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH) {\n throw new TypeError(\n `having expression depth must be at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`,\n );\n }\n switch (expression.type) {\n case HavingExpressionType.CONDITION:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (!Number.isFinite(expression.value)) {\n throw new TypeError(\n `having condition [${expression.metric}] value must be finite.`,\n );\n }\n break;\n case HavingExpressionType.BETWEEN:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (\n !Number.isFinite(expression.lower) ||\n !Number.isFinite(expression.upper)\n ) {\n throw new TypeError(\n `having between [${expression.metric}] bounds must be finite.`,\n );\n }\n if (expression.lower > expression.upper) {\n throw new TypeError(\n `having between [${expression.metric}] lower bound must not exceed upper bound.`,\n );\n }\n break;\n case HavingExpressionType.IN:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (expression.values.length === 0) {\n throw new TypeError(\n `having in [${expression.metric}] values must not be empty.`,\n );\n }\n if (!expression.values.every(Number.isFinite)) {\n throw new TypeError(\n `having in [${expression.metric}] values must be finite.`,\n );\n }\n break;\n case HavingExpressionType.IS_NULL:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n break;\n case HavingExpressionType.AND:\n case HavingExpressionType.OR:\n if (expression.operands.length === 0) {\n throw new TypeError(\n `having ${expression.type} operands must not be empty.`,\n );\n }\n expression.operands.forEach(operand =>\n pending.push({ expression: operand, depth: depth + 1 }),\n );\n break;\n default:\n throw new TypeError(\n `Unsupported having expression: ${String((expression as { type: unknown }).type)}.`,\n );\n }\n }\n}\n\nfunction validateSort(\n sort: readonly FieldSort[],\n groupBy: readonly AggregationGroup[],\n aliases: readonly string[],\n): void {\n requireAtMost('sort', 'fields', sort, AGGREGATION_LIMITS.MAX_SORT_FIELDS);\n if (groupBy.length === 0 && sort.length > 0) {\n throw new TypeError('sort requires at least one groupBy.');\n }\n const fields = sort.map(entry => entry.field);\n if (new Set(fields).size !== fields.length) {\n throw new TypeError('sort fields must be unique.');\n }\n if (!fields.every(field => aliases.includes(field))) {\n throw new TypeError('sort fields must reference aggregation aliases.');\n }\n requireAtMost(\n 'effective sort',\n 'fields',\n effectiveSort({ groupBy: [...groupBy], sort: [...sort] }),\n AGGREGATION_LIMITS.MAX_SORT_FIELDS,\n );\n}\n\n/**\n * With elements, the groups of a ROOT scope lead the effective sort: a storage\n * reads the root records' keys before the keys of their elements.\n */\nfunction requireRootGroupsFirst(\n query: Pick<AggregationQuery<string, string>, 'groupBy' | 'sort'>,\n): void {\n const root = new Map(\n (query.groupBy ?? []).map(group => [\n group.alias,\n group.scope === AggregationGroupScope.ROOT,\n ]),\n );\n const order = effectiveSort(query).flatMap(entry =>\n root.has(entry.field) ? [root.get(entry.field)] : [],\n );\n const firstElement = order.indexOf(false);\n if (firstElement >= 0 && order.slice(firstElement).includes(true)) {\n throw new TypeError(\n 'groups with a ROOT scope must sort before the element groups.',\n );\n }\n}\n\n/**\n * Admits a whole aggregation query against the rules Wow enforces on arrival;\n * {@link aggregation.query} is its public face.\n */\nexport function admitAggregationQuery<\n ROOT_FIELDS extends string = string,\n AGGREGATION_FIELDS extends string = ROOT_FIELDS,\n>(\n query: AggregationQuery<ROOT_FIELDS, AGGREGATION_FIELDS>,\n): AggregationQuery<ROOT_FIELDS, AGGREGATION_FIELDS> {\n const elements = query.elements ?? [];\n const groupBy = query.groupBy ?? [];\n const metrics = query.metrics;\n const sort = query.sort ?? [];\n\n requireAtMost('elements', 'paths', elements, AGGREGATION_LIMITS.MAX_ELEMENTS);\n requireAtMost(\n 'groupBy',\n 'dimensions',\n groupBy,\n AGGREGATION_LIMITS.MAX_GROUPS,\n );\n if (!Array.isArray(metrics) || metrics.length === 0) {\n throw new TypeError('metrics must not be empty.');\n }\n requireAtMost('metrics', 'entries', metrics, AGGREGATION_LIMITS.MAX_METRICS);\n if (query.limit !== undefined) {\n if (\n !Number.isInteger(query.limit) ||\n query.limit < 1 ||\n query.limit > AGGREGATION_LIMITS.MAX_LIMIT\n ) {\n throw new TypeError(\n `limit must be between 1 and ${AGGREGATION_LIMITS.MAX_LIMIT}.`,\n );\n }\n }\n\n validateExpressions(metrics, groupBy);\n validateDerivedMetrics(metrics);\n validateHaving(query.having, groupBy, metrics);\n metrics.forEach(metric => {\n if ('filter' in metric && metric.filter)\n validateMetricFilter(metric.filter);\n });\n\n // A dense DATE_HISTOGRAM fills in the buckets its range implies, and a\n // dense DATE_PART every key of its part's domain, rather than only the ones\n // with rows; a second dimension would multiply that filling out across\n // every one of its own keys.\n groupBy.forEach(group => {\n if (\n (group.type === AggregationGroupType.DATE_HISTOGRAM ||\n group.type === AggregationGroupType.DATE_PART) &&\n group.dense === true &&\n groupBy.length !== 1\n ) {\n throw new TypeError(\n `dense requires ${group.type} to be the only groupBy.`,\n );\n }\n });\n\n const aliases = [\n ...groupBy.map(group => group.alias),\n ...metrics.map(metric => metric.alias),\n ];\n if (new Set(aliases).size !== aliases.length) {\n throw new TypeError('aggregation aliases must be unique.');\n }\n validateSort(sort, groupBy, aliases);\n if (elements.length > 0) requireRootGroupsFirst(query);\n\n return {\n ...query,\n metrics: [...metrics] as typeof query.metrics,\n };\n}\n"],"mappings":"8HAoCA,SAASY,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CAEJ,CAAC,EAED,IAAM2B,EAAU,CACd,GAAG5B,EAAQ,IAAI,GAASC,EAAM,KAAK,EACnC,GAAGH,EAAQ,IAAI,GAAUC,EAAO,KAAK,CACvC,EACA,GAAI,IAAI,IAAI6B,CAAO,CAAC,CAAC,OAASA,EAAQ,OACpC,MAAU,UAAU,qCAAqC,EAK3D,OAHAJ,EAAaC,EAAMzB,EAAS4B,CAAO,EAC/BQ,EAAS,OAAS,GAAGN,EAAuBE,CAAK,EAE9C,CACL,GAAGA,EACH,QAAS,CAAC,GAAGlC,CAAO,CACtB,CACF"}
|
|
1
|
+
{"version":3,"file":"admit.cjs","names":["FilterOperator","AggregationMetricType","AggregationGroupType","constantName","AggregationGroupScope","AGGREGATION_LIMITS","DerivedExpressionType","HavingExpressionType","effectiveSort","require_operator","require_constantName","require_types","require_expressionTrees","require_sort","requireAtMost","items","max","name","noun","validateMetricFilter","filter","validateExpressions","metrics","metric","groupBy","group","NON_NUMERIC","validateDerivedMetrics","declared","derived","counter","validateDerivedExpression","pending","depth","expression","reference","nonNumeric","requireWindowMetric","target","window","alias","requireValidHavingMetric","metricAliases","anyAliases","edgeAliases","validateHaving","having","operand","validateSort","sort","fields","entry","aliases","field","requireRootGroupsFirst","root","query","order","firstElement","admitAggregationQuery","elements"],"sources":["../../../src/dsl/aggregation/admit.ts"],"sourcesContent":["/*\n * Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * http://www.apache.org/licenses/LICENSE-2.0\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { constantName } from '../constantName.js';\nimport { FilterOperator, type FilterExpression } from '../filter/index.js';\nimport type { FieldSort } from '../sort.js';\nimport { requireValidExpressionTrees } from './expressionTrees.js';\nimport { effectiveSort } from './sort.js';\nimport {\n AGGREGATION_LIMITS,\n AggregationGroupScope,\n AggregationGroupType,\n AggregationMetricType,\n DerivedExpressionType,\n HavingExpressionType,\n type AggregationGroup,\n type AggregationMetric,\n type AggregationQuery,\n type DerivedAggregationMetric,\n type DerivedExpression,\n type DerivedWindow,\n type HavingExpression,\n} from './types.js';\n\n// Internal: the rules `aggregation.query()` checks across the parts of a\n// query. index.ts does not re-export this file.\n\nfunction requireAtMost(\n name: string,\n noun: string,\n items: readonly unknown[],\n max: number,\n): void {\n if (items.length > max) {\n throw new TypeError(`${name} must contain at most ${max} ${noun}.`);\n }\n}\n\n/**\n * Refuses the filters a metric cannot carry, whatever the aggregate holds.\n *\n * A metric filter is a whole-value predicate on one record — MongoDB turns it\n * into a `$cond` guard and Elasticsearch into a filter aggregation — and\n * element matching and full text have no whole-value reading. Both backends\n * refuse them before touching a schema, so the shape alone settles it. What a\n * schema would add, refusing an array-valued field, stays on the server.\n */\nfunction validateMetricFilter(filter: FilterExpression): void {\n switch (filter.op) {\n case FilterOperator.SEARCH:\n throw new TypeError(\n 'Aggregation metric filters do not support search filters.',\n );\n case FilterOperator.ELEMENT_MATCH:\n throw new TypeError(\n 'Aggregation metric filters do not support [ELEMENT_MATCH].',\n );\n case FilterOperator.AND:\n case FilterOperator.OR:\n case FilterOperator.NOR:\n filter.operands.forEach(validateMetricFilter);\n break;\n }\n}\n\n/**\n * Walks the arithmetic of every metric that carries an expression, as one\n * budget, and of each expression group on its own, as Wow does.\n */\nfunction validateExpressions(\n metrics: readonly AggregationMetric[],\n groupBy: readonly AggregationGroup[],\n): void {\n requireValidExpressionTrees(\n metrics.flatMap(metric =>\n metric.type === AggregationMetricType.NUMERIC ||\n metric.type === AggregationMetricType.DISTINCT_COUNT ||\n metric.type === AggregationMetricType.PERCENTILE\n ? [metric.expression]\n : [],\n ),\n );\n groupBy.forEach(group => {\n if (\n group.type !== AggregationGroupType.TERMS &&\n group.type !== AggregationGroupType.HISTOGRAM\n )\n return;\n if ((group.field === undefined) === (group.expression === undefined)) {\n throw new TypeError(\n `${constantName(group.type)} group requires exactly one of field and expression.`,\n );\n }\n if (group.expression) requireValidExpressionTrees([group.expression]);\n if (\n group.type === AggregationGroupType.TERMS &&\n group.missingKey !== undefined &&\n group.expression\n ) {\n throw new TypeError('terms missingKey requires a field input.');\n }\n if (group.scope === AggregationGroupScope.ROOT && group.expression) {\n throw new TypeError(\n `${constantName(group.type)} with a ROOT scope requires a field input.`,\n );\n }\n });\n}\n\n/** The metric types whose value is not a number: derived and having refuse them. */\nconst NON_NUMERIC: ReadonlySet<string> = new Set([\n AggregationMetricType.ANY,\n AggregationMetricType.FIRST,\n AggregationMetricType.LAST,\n]);\n\n/**\n * Admits the derived metrics against what was declared before each of them.\n *\n * Derived arithmetic runs after the aggregation, over values the query has\n * already produced, so a reference may only name a metric declared earlier —\n * that is what makes the list evaluable in one pass — and never an `ANY`,\n * `FIRST` or `LAST` metric, whose value is a field's value rather than a\n * number.\n */\nfunction validateDerivedMetrics(metrics: readonly AggregationMetric[]): void {\n // Each earlier alias, with its type when that is not a number.\n const declared = new Map<string, string | undefined>();\n const derived = new Set<string>();\n const counter = { nodes: 0, windows: 0 };\n metrics.forEach(metric => {\n if (metric.type === AggregationMetricType.DERIVED) {\n validateDerivedExpression(metric, declared, derived, counter);\n derived.add(metric.alias);\n }\n declared.set(\n metric.alias,\n NON_NUMERIC.has(metric.type) ? metric.type : undefined,\n );\n });\n}\n\nfunction validateDerivedExpression(\n metric: DerivedAggregationMetric,\n declared: ReadonlyMap<string, string | undefined>,\n derived: ReadonlySet<string>,\n counter: { nodes: number; windows: number },\n): void {\n const pending: { expression: DerivedExpression; depth: number }[] = [\n { expression: metric.expression, depth: 1 },\n ];\n while (pending.length > 0) {\n const { expression, depth } = pending.pop()!;\n if (depth > AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH) {\n throw new TypeError(\n `derived expression depth must be at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`,\n );\n }\n counter.nodes++;\n if (counter.nodes > AGGREGATION_LIMITS.MAX_EXPRESSION_NODES) {\n throw new TypeError(\n `derived expressions must contain at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_NODES} nodes.`,\n );\n }\n switch (expression.type) {\n case DerivedExpressionType.METRIC_REF: {\n const reference = expression.metric;\n if (!declared.has(reference)) {\n throw new TypeError(\n `derived metric [${metric.alias}] must reference a metric declared before it, but was [${reference}].`,\n );\n }\n const nonNumeric = declared.get(reference);\n if (nonNumeric !== undefined) {\n throw new TypeError(\n `derived metric [${metric.alias}] cannot reference ${constantName(nonNumeric)} metric [${reference}].`,\n );\n }\n break;\n }\n case DerivedExpressionType.CONSTANT:\n if (!Number.isFinite(expression.value)) {\n throw new TypeError('derived constant must be finite.');\n }\n break;\n case DerivedExpressionType.BINARY:\n pending.push({ expression: expression.left, depth: depth + 1 });\n pending.push({ expression: expression.right, depth: depth + 1 });\n break;\n case DerivedExpressionType.TOTAL:\n case DerivedExpressionType.RUNNING:\n case DerivedExpressionType.PREVIOUS:\n case DerivedExpressionType.RANK:\n counter.windows++;\n if (counter.windows > AGGREGATION_LIMITS.MAX_WINDOWS) {\n throw new TypeError(\n `an aggregation must compute at most ${AGGREGATION_LIMITS.MAX_WINDOWS} windows.`,\n );\n }\n requireWindowMetric(metric.alias, expression, declared, derived);\n break;\n default:\n throw new TypeError(\n `Unsupported derived expression: ${String((expression as { type: unknown }).type)}.`,\n );\n }\n }\n}\n\n/**\n * A window reads a non-derived numeric metric declared before the derived\n * one; the rest of its rules (which metrics add up, which groups it names)\n * the server checks against the whole query.\n */\nfunction requireWindowMetric(\n alias: string,\n window: DerivedWindow,\n declared: ReadonlyMap<string, string | undefined>,\n derived: ReadonlySet<string>,\n): void {\n const target = window.metric;\n if (!declared.has(target)) {\n throw new TypeError(\n `${constantName(window.type)} window of derived metric [${alias}] must read a metric declared before it, but was [${target}].`,\n );\n }\n if (declared.get(target) !== undefined || derived.has(target)) {\n throw new TypeError(\n `${constantName(window.type)} window of derived metric [${alias}] must read a non-derived numeric metric, but was [${target}].`,\n );\n }\n}\n\nfunction requireValidHavingMetric(\n metric: string,\n metricAliases: ReadonlySet<string>,\n anyAliases: ReadonlySet<string>,\n edgeAliases: ReadonlySet<string>,\n): void {\n if (!metricAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] must reference a declared metric alias.`,\n );\n }\n if (anyAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] cannot reference ANY metric.`,\n );\n }\n if (edgeAliases.has(metric)) {\n throw new TypeError(\n `having condition [${metric}] cannot reference FIRST or LAST metric.`,\n );\n }\n}\n\n/**\n * `having` filters the grouped rows, so it needs rows to filter: a query with\n * no `groupBy` produces one row and has nothing to select from.\n */\nfunction validateHaving(\n having: HavingExpression | undefined,\n groupBy: readonly AggregationGroup[],\n metrics: readonly AggregationMetric[],\n): void {\n if (having === undefined) return;\n if (groupBy.length === 0) {\n throw new TypeError('having requires at least one groupBy.');\n }\n const metricAliases = new Set(metrics.map(metric => metric.alias));\n const anyAliases = new Set(\n metrics\n .filter(metric => metric.type === AggregationMetricType.ANY)\n .map(metric => metric.alias),\n );\n const edgeAliases = new Set(\n metrics\n .filter(\n metric =>\n metric.type === AggregationMetricType.FIRST ||\n metric.type === AggregationMetricType.LAST,\n )\n .map(metric => metric.alias),\n );\n\n const pending: { expression: HavingExpression; depth: number }[] = [\n { expression: having, depth: 1 },\n ];\n while (pending.length > 0) {\n const { expression, depth } = pending.pop()!;\n if (depth > AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH) {\n throw new TypeError(\n `having expression depth must be at most ${AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`,\n );\n }\n switch (expression.type) {\n case HavingExpressionType.CONDITION:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (!Number.isFinite(expression.value)) {\n throw new TypeError(\n `having condition [${expression.metric}] value must be finite.`,\n );\n }\n break;\n case HavingExpressionType.BETWEEN:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (\n !Number.isFinite(expression.lower) ||\n !Number.isFinite(expression.upper)\n ) {\n throw new TypeError(\n `having between [${expression.metric}] bounds must be finite.`,\n );\n }\n if (expression.lower > expression.upper) {\n throw new TypeError(\n `having between [${expression.metric}] lower bound must not exceed upper bound.`,\n );\n }\n break;\n case HavingExpressionType.IN:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n if (expression.values.length === 0) {\n throw new TypeError(\n `having in [${expression.metric}] values must not be empty.`,\n );\n }\n if (!expression.values.every(Number.isFinite)) {\n throw new TypeError(\n `having in [${expression.metric}] values must be finite.`,\n );\n }\n break;\n case HavingExpressionType.IS_NULL:\n requireValidHavingMetric(\n expression.metric,\n metricAliases,\n anyAliases,\n edgeAliases,\n );\n break;\n case HavingExpressionType.AND:\n case HavingExpressionType.OR:\n if (expression.operands.length === 0) {\n throw new TypeError(\n `having ${constantName(expression.type)} operands must not be empty.`,\n );\n }\n expression.operands.forEach(operand =>\n pending.push({ expression: operand, depth: depth + 1 }),\n );\n break;\n default:\n throw new TypeError(\n `Unsupported having expression: ${String((expression as { type: unknown }).type)}.`,\n );\n }\n }\n}\n\nfunction validateSort(\n sort: readonly FieldSort[],\n groupBy: readonly AggregationGroup[],\n aliases: readonly string[],\n): void {\n requireAtMost('sort', 'fields', sort, AGGREGATION_LIMITS.MAX_SORT_FIELDS);\n if (groupBy.length === 0 && sort.length > 0) {\n throw new TypeError('sort requires at least one groupBy.');\n }\n const fields = sort.map(entry => entry.field);\n if (new Set(fields).size !== fields.length) {\n throw new TypeError('sort fields must be unique.');\n }\n if (!fields.every(field => aliases.includes(field))) {\n throw new TypeError('sort fields must reference aggregation aliases.');\n }\n requireAtMost(\n 'effective sort',\n 'fields',\n effectiveSort({ groupBy: [...groupBy], sort: [...sort] }),\n AGGREGATION_LIMITS.MAX_SORT_FIELDS,\n );\n}\n\n/**\n * With elements, the groups of a ROOT scope lead the effective sort: a storage\n * reads the root records' keys before the keys of their elements.\n */\nfunction requireRootGroupsFirst(\n query: Pick<AggregationQuery<string, string>, 'groupBy' | 'sort'>,\n): void {\n const root = new Map(\n (query.groupBy ?? []).map(group => [\n group.alias,\n group.scope === AggregationGroupScope.ROOT,\n ]),\n );\n const order = effectiveSort(query).flatMap(entry =>\n root.has(entry.field) ? [root.get(entry.field)] : [],\n );\n const firstElement = order.indexOf(false);\n if (firstElement >= 0 && order.slice(firstElement).includes(true)) {\n throw new TypeError(\n 'groups with a ROOT scope must sort before the element groups.',\n );\n }\n}\n\n/**\n * Admits a whole aggregation query against the rules Wow enforces on arrival;\n * {@link aggregation.query} is its public face.\n */\nexport function admitAggregationQuery<\n ROOT_FIELDS extends string = string,\n AGGREGATION_FIELDS extends string = ROOT_FIELDS,\n>(\n query: AggregationQuery<ROOT_FIELDS, AGGREGATION_FIELDS>,\n): AggregationQuery<ROOT_FIELDS, AGGREGATION_FIELDS> {\n const elements = query.elements ?? [];\n const groupBy = query.groupBy ?? [];\n const metrics = query.metrics;\n const sort = query.sort ?? [];\n\n requireAtMost('elements', 'paths', elements, AGGREGATION_LIMITS.MAX_ELEMENTS);\n requireAtMost(\n 'groupBy',\n 'dimensions',\n groupBy,\n AGGREGATION_LIMITS.MAX_GROUPS,\n );\n if (!Array.isArray(metrics) || metrics.length === 0) {\n throw new TypeError('metrics must not be empty.');\n }\n requireAtMost('metrics', 'entries', metrics, AGGREGATION_LIMITS.MAX_METRICS);\n if (query.limit !== undefined) {\n if (\n !Number.isInteger(query.limit) ||\n query.limit < 1 ||\n query.limit > AGGREGATION_LIMITS.MAX_LIMIT\n ) {\n throw new TypeError(\n `limit must be between 1 and ${AGGREGATION_LIMITS.MAX_LIMIT}.`,\n );\n }\n }\n\n validateExpressions(metrics, groupBy);\n validateDerivedMetrics(metrics);\n validateHaving(query.having, groupBy, metrics);\n metrics.forEach(metric => {\n if ('filter' in metric && metric.filter)\n validateMetricFilter(metric.filter);\n });\n\n // A dense DATE_HISTOGRAM fills in the buckets its range implies, and a\n // dense DATE_PART every key of its part's domain, rather than only the ones\n // with rows; a second dimension would multiply that filling out across\n // every one of its own keys.\n groupBy.forEach(group => {\n if (\n (group.type === AggregationGroupType.DATE_HISTOGRAM ||\n group.type === AggregationGroupType.DATE_PART) &&\n group.dense === true &&\n groupBy.length !== 1\n ) {\n throw new TypeError(\n `dense requires ${constantName(group.type)} to be the only groupBy.`,\n );\n }\n });\n\n const aliases = [\n ...groupBy.map(group => group.alias),\n ...metrics.map(metric => metric.alias),\n ];\n if (new Set(aliases).size !== aliases.length) {\n throw new TypeError('aggregation aliases must be unique.');\n }\n validateSort(sort, groupBy, aliases);\n if (elements.length > 0) requireRootGroupsFirst(query);\n\n return {\n ...query,\n metrics: [...metrics] as typeof query.metrics,\n };\n}\n"],"mappings":"A,+JAqCA,SAASc,EACP,EACA,EACA,EACA,EACM,CACN,GAAIC,EAAM,OAASC,EACjB,MAAU,UAAU,GAAGC,EAAK,wBAAwBD,EAAI,GAAGE,EAAK,EAAE,CAEtE,CAWA,SAASC,EAAqB,EAAgC,CAC5D,OAAQC,EAAO,GAAf,CACE,KAAKpB,EAAAA,eAAe,OAClB,MAAU,UACR,2DACF,EACF,KAAKA,EAAAA,eAAe,cAClB,MAAU,UACR,4DACF,EACF,KAAKA,EAAAA,eAAe,IACpB,KAAKA,EAAAA,eAAe,GACpB,KAAKA,EAAAA,eAAe,IAClBoB,EAAO,SAAS,QAAQD,CAAoB,CAEhD,CACF,CAMA,SAASE,EACP,EACA,EACM,CACNT,EAAA,4BACEU,EAAQ,QAAQ,GACdC,EAAO,OAAStB,EAAAA,sBAAsB,SACtCsB,EAAO,OAAStB,EAAAA,sBAAsB,gBACtCsB,EAAO,OAAStB,EAAAA,sBAAsB,WAClC,CAACsB,EAAO,UAAU,EAClB,CAAC,CACP,CAC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{
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { constantName as t } from "../constantName.es.js";
|
|
3
|
+
import { AGGREGATION_LIMITS as n, AggregationGroupScope as r, AggregationGroupType as i, AggregationMetricType as a, DerivedExpressionType as o, HavingExpressionType as s } from "./types.es.js";
|
|
4
|
+
import { requireValidExpressionTrees as c } from "./expressionTrees.es.js";
|
|
5
|
+
import { effectiveSort as l } from "./sort.es.js";
|
|
6
|
+
// src/dsl/aggregation/admit.ts
|
|
7
|
+
function u(e, t, n, r) {
|
|
7
8
|
if (n.length > r) throw TypeError(`${e} must contain at most ${r} ${t}.`);
|
|
8
9
|
}
|
|
9
|
-
function
|
|
10
|
+
function d(t) {
|
|
10
11
|
switch (t.op) {
|
|
11
12
|
case e.SEARCH: throw TypeError("Aggregation metric filters do not support search filters.");
|
|
12
13
|
case e.ELEMENT_MATCH: throw TypeError("Aggregation metric filters do not support [ELEMENT_MATCH].");
|
|
13
14
|
case e.AND:
|
|
14
15
|
case e.OR:
|
|
15
|
-
case e.NOR: t.operands.forEach(
|
|
16
|
+
case e.NOR: t.operands.forEach(d);
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
if (e.type ===
|
|
21
|
-
if (e.field === void 0 == (e.expression === void 0)) throw TypeError(`${e.type} group requires exactly one of field and expression.`);
|
|
22
|
-
if (e.expression &&
|
|
23
|
-
if (e.scope ===
|
|
19
|
+
function f(e, n) {
|
|
20
|
+
c(e.flatMap((e) => e.type === a.NUMERIC || e.type === a.DISTINCT_COUNT || e.type === a.PERCENTILE ? [e.expression] : [])), n.forEach((e) => {
|
|
21
|
+
if (e.type === i.TERMS || e.type === i.HISTOGRAM) {
|
|
22
|
+
if (e.field === void 0 == (e.expression === void 0)) throw TypeError(`${t(e.type)} group requires exactly one of field and expression.`);
|
|
23
|
+
if (e.expression && c([e.expression]), e.type === i.TERMS && e.missingKey !== void 0 && e.expression) throw TypeError("terms missingKey requires a field input.");
|
|
24
|
+
if (e.scope === r.ROOT && e.expression) throw TypeError(`${t(e.type)} with a ROOT scope requires a field input.`);
|
|
24
25
|
}
|
|
25
26
|
});
|
|
26
27
|
}
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
var p = /* @__PURE__ */ new Set([
|
|
29
|
+
a.ANY,
|
|
30
|
+
a.FIRST,
|
|
31
|
+
a.LAST
|
|
31
32
|
]);
|
|
32
|
-
function
|
|
33
|
+
function m(e) {
|
|
33
34
|
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Set(), r = {
|
|
34
35
|
nodes: 0,
|
|
35
36
|
windows: 0
|
|
36
37
|
};
|
|
37
38
|
e.forEach((e) => {
|
|
38
|
-
e.type ===
|
|
39
|
+
e.type === a.DERIVED && (h(e, t, n, r), n.add(e.alias)), t.set(e.alias, p.has(e.type) ? e.type : void 0);
|
|
39
40
|
});
|
|
40
41
|
}
|
|
41
|
-
function
|
|
42
|
-
let
|
|
42
|
+
function h(e, r, i, a) {
|
|
43
|
+
let s = [{
|
|
43
44
|
expression: e.expression,
|
|
44
45
|
depth: 1
|
|
45
46
|
}];
|
|
46
|
-
for (;
|
|
47
|
-
let { expression:
|
|
48
|
-
if (
|
|
49
|
-
if (
|
|
50
|
-
switch (
|
|
51
|
-
case
|
|
52
|
-
let
|
|
53
|
-
if (!
|
|
54
|
-
let
|
|
55
|
-
if (
|
|
47
|
+
for (; s.length > 0;) {
|
|
48
|
+
let { expression: c, depth: l } = s.pop();
|
|
49
|
+
if (l > n.MAX_EXPRESSION_DEPTH) throw TypeError(`derived expression depth must be at most ${n.MAX_EXPRESSION_DEPTH}.`);
|
|
50
|
+
if (a.nodes++, a.nodes > n.MAX_EXPRESSION_NODES) throw TypeError(`derived expressions must contain at most ${n.MAX_EXPRESSION_NODES} nodes.`);
|
|
51
|
+
switch (c.type) {
|
|
52
|
+
case o.METRIC_REF: {
|
|
53
|
+
let n = c.metric;
|
|
54
|
+
if (!r.has(n)) throw TypeError(`derived metric [${e.alias}] must reference a metric declared before it, but was [${n}].`);
|
|
55
|
+
let i = r.get(n);
|
|
56
|
+
if (i !== void 0) throw TypeError(`derived metric [${e.alias}] cannot reference ${t(i)} metric [${n}].`);
|
|
56
57
|
break;
|
|
57
58
|
}
|
|
58
|
-
case
|
|
59
|
-
if (!Number.isFinite(
|
|
59
|
+
case o.CONSTANT:
|
|
60
|
+
if (!Number.isFinite(c.value)) throw TypeError("derived constant must be finite.");
|
|
60
61
|
break;
|
|
61
|
-
case
|
|
62
|
-
|
|
63
|
-
expression:
|
|
64
|
-
depth:
|
|
65
|
-
}),
|
|
66
|
-
expression:
|
|
67
|
-
depth:
|
|
62
|
+
case o.BINARY:
|
|
63
|
+
s.push({
|
|
64
|
+
expression: c.left,
|
|
65
|
+
depth: l + 1
|
|
66
|
+
}), s.push({
|
|
67
|
+
expression: c.right,
|
|
68
|
+
depth: l + 1
|
|
68
69
|
});
|
|
69
70
|
break;
|
|
70
|
-
case
|
|
71
|
-
case
|
|
72
|
-
case
|
|
73
|
-
case
|
|
74
|
-
if (
|
|
75
|
-
|
|
71
|
+
case o.TOTAL:
|
|
72
|
+
case o.RUNNING:
|
|
73
|
+
case o.PREVIOUS:
|
|
74
|
+
case o.RANK:
|
|
75
|
+
if (a.windows++, a.windows > n.MAX_WINDOWS) throw TypeError(`an aggregation must compute at most ${n.MAX_WINDOWS} windows.`);
|
|
76
|
+
g(e.alias, c, r, i);
|
|
76
77
|
break;
|
|
77
|
-
default: throw TypeError(`Unsupported derived expression: ${String(
|
|
78
|
+
default: throw TypeError(`Unsupported derived expression: ${String(c.type)}.`);
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
|
-
function
|
|
82
|
-
let
|
|
83
|
-
if (!
|
|
84
|
-
if (
|
|
82
|
+
function g(e, n, r, i) {
|
|
83
|
+
let a = n.metric;
|
|
84
|
+
if (!r.has(a)) throw TypeError(`${t(n.type)} window of derived metric [${e}] must read a metric declared before it, but was [${a}].`);
|
|
85
|
+
if (r.get(a) !== void 0 || i.has(a)) throw TypeError(`${t(n.type)} window of derived metric [${e}] must read a non-derived numeric metric, but was [${a}].`);
|
|
85
86
|
}
|
|
86
|
-
function
|
|
87
|
+
function _(e, t, n, r) {
|
|
87
88
|
if (!t.has(e)) throw TypeError(`having condition [${e}] must reference a declared metric alias.`);
|
|
88
89
|
if (n.has(e)) throw TypeError(`having condition [${e}] cannot reference ANY metric.`);
|
|
89
90
|
if (r.has(e)) throw TypeError(`having condition [${e}] cannot reference FIRST or LAST metric.`);
|
|
90
91
|
}
|
|
91
|
-
function
|
|
92
|
+
function v(e, r, i) {
|
|
92
93
|
if (e === void 0) return;
|
|
93
|
-
if (
|
|
94
|
-
let
|
|
94
|
+
if (r.length === 0) throw TypeError("having requires at least one groupBy.");
|
|
95
|
+
let o = new Set(i.map((e) => e.alias)), c = new Set(i.filter((e) => e.type === a.ANY).map((e) => e.alias)), l = new Set(i.filter((e) => e.type === a.FIRST || e.type === a.LAST).map((e) => e.alias)), u = [{
|
|
95
96
|
expression: e,
|
|
96
97
|
depth: 1
|
|
97
98
|
}];
|
|
98
|
-
for (;
|
|
99
|
-
let { expression: e, depth:
|
|
100
|
-
if (
|
|
99
|
+
for (; u.length > 0;) {
|
|
100
|
+
let { expression: e, depth: r } = u.pop();
|
|
101
|
+
if (r > n.MAX_EXPRESSION_DEPTH) throw TypeError(`having expression depth must be at most ${n.MAX_EXPRESSION_DEPTH}.`);
|
|
101
102
|
switch (e.type) {
|
|
102
|
-
case
|
|
103
|
-
if (
|
|
103
|
+
case s.CONDITION:
|
|
104
|
+
if (_(e.metric, o, c, l), !Number.isFinite(e.value)) throw TypeError(`having condition [${e.metric}] value must be finite.`);
|
|
104
105
|
break;
|
|
105
|
-
case
|
|
106
|
-
if (
|
|
106
|
+
case s.BETWEEN:
|
|
107
|
+
if (_(e.metric, o, c, l), !Number.isFinite(e.lower) || !Number.isFinite(e.upper)) throw TypeError(`having between [${e.metric}] bounds must be finite.`);
|
|
107
108
|
if (e.lower > e.upper) throw TypeError(`having between [${e.metric}] lower bound must not exceed upper bound.`);
|
|
108
109
|
break;
|
|
109
|
-
case
|
|
110
|
-
if (
|
|
110
|
+
case s.IN:
|
|
111
|
+
if (_(e.metric, o, c, l), e.values.length === 0) throw TypeError(`having in [${e.metric}] values must not be empty.`);
|
|
111
112
|
if (!e.values.every(Number.isFinite)) throw TypeError(`having in [${e.metric}] values must be finite.`);
|
|
112
113
|
break;
|
|
113
|
-
case
|
|
114
|
-
|
|
114
|
+
case s.IS_NULL:
|
|
115
|
+
_(e.metric, o, c, l);
|
|
115
116
|
break;
|
|
116
|
-
case
|
|
117
|
-
case
|
|
118
|
-
if (e.operands.length === 0) throw TypeError(`having ${e.type} operands must not be empty.`);
|
|
119
|
-
e.operands.forEach((e) =>
|
|
117
|
+
case s.AND:
|
|
118
|
+
case s.OR:
|
|
119
|
+
if (e.operands.length === 0) throw TypeError(`having ${t(e.type)} operands must not be empty.`);
|
|
120
|
+
e.operands.forEach((e) => u.push({
|
|
120
121
|
expression: e,
|
|
121
|
-
depth:
|
|
122
|
+
depth: r + 1
|
|
122
123
|
}));
|
|
123
124
|
break;
|
|
124
125
|
default: throw TypeError(`Unsupported having expression: ${String(e.type)}.`);
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
|
-
function
|
|
129
|
-
if (
|
|
129
|
+
function y(e, t, r) {
|
|
130
|
+
if (u("sort", "fields", e, n.MAX_SORT_FIELDS), t.length === 0 && e.length > 0) throw TypeError("sort requires at least one groupBy.");
|
|
130
131
|
let i = e.map((e) => e.field);
|
|
131
132
|
if (new Set(i).size !== i.length) throw TypeError("sort fields must be unique.");
|
|
132
133
|
if (!i.every((e) => r.includes(e))) throw TypeError("sort fields must reference aggregation aliases.");
|
|
133
|
-
|
|
134
|
-
groupBy: [...
|
|
134
|
+
u("effective sort", "fields", l({
|
|
135
|
+
groupBy: [...t],
|
|
135
136
|
sort: [...e]
|
|
136
|
-
}),
|
|
137
|
-
}
|
|
138
|
-
function y(e) {
|
|
139
|
-
let t = new Map((e.groupBy ?? []).map((e) => [e.alias, e.scope === n.ROOT])), r = c(e).flatMap((e) => t.has(e.field) ? [t.get(e.field)] : []), i = r.indexOf(!1);
|
|
140
|
-
if (i >= 0 && r.slice(i).includes(!0)) throw TypeError("groups with a ROOT scope must sort before the element groups.");
|
|
137
|
+
}), n.MAX_SORT_FIELDS);
|
|
141
138
|
}
|
|
142
139
|
function b(e) {
|
|
143
|
-
let
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
140
|
+
let t = new Map((e.groupBy ?? []).map((e) => [e.alias, e.scope === r.ROOT])), n = l(e).flatMap((e) => t.has(e.field) ? [t.get(e.field)] : []), i = n.indexOf(!1);
|
|
141
|
+
if (i >= 0 && n.slice(i).includes(!0)) throw TypeError("groups with a ROOT scope must sort before the element groups.");
|
|
142
|
+
}
|
|
143
|
+
function x(e) {
|
|
144
|
+
let r = e.elements ?? [], a = e.groupBy ?? [], o = e.metrics, s = e.sort ?? [];
|
|
145
|
+
if (u("elements", "paths", r, n.MAX_ELEMENTS), u("groupBy", "dimensions", a, n.MAX_GROUPS), !Array.isArray(o) || o.length === 0) throw TypeError("metrics must not be empty.");
|
|
146
|
+
if (u("metrics", "entries", o, n.MAX_METRICS), e.limit !== void 0 && (!Number.isInteger(e.limit) || e.limit < 1 || e.limit > n.MAX_LIMIT)) throw TypeError(`limit must be between 1 and ${n.MAX_LIMIT}.`);
|
|
147
|
+
f(o, a), m(o), v(e.having, a, o), o.forEach((e) => {
|
|
148
|
+
"filter" in e && e.filter && d(e.filter);
|
|
149
|
+
}), a.forEach((e) => {
|
|
150
|
+
if ((e.type === i.DATE_HISTOGRAM || e.type === i.DATE_PART) && e.dense === !0 && a.length !== 1) throw TypeError(`dense requires ${t(e.type)} to be the only groupBy.`);
|
|
150
151
|
});
|
|
151
|
-
let
|
|
152
|
-
if (new Set(
|
|
153
|
-
return
|
|
152
|
+
let c = [...a.map((e) => e.alias), ...o.map((e) => e.alias)];
|
|
153
|
+
if (new Set(c).size !== c.length) throw TypeError("aggregation aliases must be unique.");
|
|
154
|
+
return y(s, a, c), r.length > 0 && b(e), {
|
|
154
155
|
...e,
|
|
155
|
-
metrics: [...
|
|
156
|
+
metrics: [...o]
|
|
156
157
|
};
|
|
157
158
|
}
|
|
158
|
-
|
|
159
|
-
export { b as admitAggregationQuery };
|
|
159
|
+
export { x as admitAggregationQuery };
|
|
160
160
|
|
|
161
161
|
//# sourceMappingURL=admit.es.js.map
|