@ahoo-wang/wow-client 10.0.0-beta.1 → 10.0.0-beta.2
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 +4 -2
- package/README.zh-CN.md +1 -1
- package/dist/commands.cjs +1 -1
- package/dist/commands.cjs.map +1 -1
- package/dist/commands.d.cts +1 -8
- package/dist/commands.d.ts +1 -8
- package/dist/commands.es.js +45 -51
- package/dist/commands.es.js.map +1 -1
- package/dist/dsl/aggregation/admit.cjs +1 -1
- package/dist/dsl/aggregation/admit.cjs.map +1 -1
- package/dist/dsl/aggregation/admit.es.js +73 -69
- package/dist/dsl/aggregation/admit.es.js.map +1 -1
- package/dist/dsl/aggregation/aliases.cjs +2 -0
- package/dist/dsl/aggregation/aliases.cjs.map +1 -0
- package/dist/dsl/aggregation/aliases.d.cts +2 -0
- package/dist/dsl/aggregation/aliases.d.ts +2 -0
- package/dist/dsl/aggregation/aliases.es.js +13 -0
- package/dist/dsl/aggregation/aliases.es.js.map +1 -0
- package/dist/dsl/aggregation/builders.cjs +1 -1
- package/dist/dsl/aggregation/builders.cjs.map +1 -1
- package/dist/dsl/aggregation/builders.d.cts +17 -243
- package/dist/dsl/aggregation/builders.d.ts +17 -243
- package/dist/dsl/aggregation/builders.es.js +36 -171
- package/dist/dsl/aggregation/builders.es.js.map +1 -1
- package/dist/dsl/aggregation/derived.cjs +1 -1
- package/dist/dsl/aggregation/derived.cjs.map +1 -1
- package/dist/dsl/aggregation/derived.d.cts +123 -1
- package/dist/dsl/aggregation/derived.d.ts +123 -1
- package/dist/dsl/aggregation/derived.es.js +43 -28
- package/dist/dsl/aggregation/derived.es.js.map +1 -1
- package/dist/dsl/aggregation/groups.cjs +2 -0
- package/dist/dsl/aggregation/groups.cjs.map +1 -0
- package/dist/dsl/aggregation/groups.d.cts +325 -0
- package/dist/dsl/aggregation/groups.d.ts +325 -0
- package/dist/dsl/aggregation/groups.es.js +80 -0
- package/dist/dsl/aggregation/groups.es.js.map +1 -0
- package/dist/dsl/aggregation/having.cjs +1 -1
- package/dist/dsl/aggregation/having.cjs.map +1 -1
- package/dist/dsl/aggregation/having.d.cts +63 -1
- package/dist/dsl/aggregation/having.d.ts +63 -1
- package/dist/dsl/aggregation/having.es.js +33 -30
- package/dist/dsl/aggregation/having.es.js.map +1 -1
- package/dist/dsl/aggregation/index.d.cts +4 -2
- package/dist/dsl/aggregation/index.d.ts +4 -2
- package/dist/dsl/aggregation/metrics.cjs +2 -0
- package/dist/dsl/aggregation/metrics.cjs.map +1 -0
- package/dist/dsl/aggregation/metrics.d.cts +279 -0
- package/dist/dsl/aggregation/metrics.d.ts +279 -0
- package/dist/dsl/aggregation/metrics.es.js +75 -0
- package/dist/dsl/aggregation/metrics.es.js.map +1 -0
- package/dist/dsl/aggregation/types.cjs +1 -1
- package/dist/dsl/aggregation/types.cjs.map +1 -1
- package/dist/dsl/aggregation/types.d.cts +4 -524
- package/dist/dsl/aggregation/types.d.ts +4 -524
- package/dist/dsl/aggregation/types.es.js +5 -25
- package/dist/dsl/aggregation/types.es.js.map +1 -1
- package/dist/dsl/cursorQuery.cjs +1 -1
- package/dist/dsl/cursorQuery.cjs.map +1 -1
- package/dist/dsl/cursorQuery.d.cts +18 -7
- package/dist/dsl/cursorQuery.d.ts +18 -7
- package/dist/dsl/cursorQuery.es.js +13 -12
- package/dist/dsl/cursorQuery.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 +17 -379
- package/dist/dsl/descriptor.d.ts +17 -379
- package/dist/dsl/descriptor.es.js +4 -31
- package/dist/dsl/descriptor.es.js.map +1 -1
- package/dist/dsl/fieldDescriptor.cjs +2 -0
- package/dist/dsl/fieldDescriptor.cjs.map +1 -0
- package/dist/dsl/fieldDescriptor.d.cts +246 -0
- package/dist/dsl/fieldDescriptor.d.ts +246 -0
- package/dist/dsl/fieldDescriptor.es.js +24 -0
- package/dist/dsl/fieldDescriptor.es.js.map +1 -0
- package/dist/dsl/filter/builders.cjs +1 -1
- package/dist/dsl/filter/builders.cjs.map +1 -1
- package/dist/dsl/filter/builders.d.cts +7 -949
- package/dist/dsl/filter/builders.d.ts +7 -949
- package/dist/dsl/filter/builders.es.js +14 -285
- package/dist/dsl/filter/builders.es.js.map +1 -1
- package/dist/dsl/filter/expressionFilter.cjs +2 -0
- package/dist/dsl/filter/expressionFilter.cjs.map +1 -0
- package/dist/dsl/filter/expressionFilter.d.cts +35 -0
- package/dist/dsl/filter/expressionFilter.d.ts +35 -0
- package/dist/dsl/filter/expressionFilter.es.js +18 -0
- package/dist/dsl/filter/expressionFilter.es.js.map +1 -0
- package/dist/dsl/filter/fieldFilters.cjs +2 -0
- package/dist/dsl/filter/fieldFilters.cjs.map +1 -0
- package/dist/dsl/filter/fieldFilters.d.cts +297 -0
- package/dist/dsl/filter/fieldFilters.d.ts +297 -0
- package/dist/dsl/filter/fieldFilters.es.js +110 -0
- package/dist/dsl/filter/fieldFilters.es.js.map +1 -0
- package/dist/dsl/filter/index.d.cts +2 -1
- package/dist/dsl/filter/index.d.ts +2 -1
- package/dist/dsl/filter/logicalFilters.cjs +2 -0
- package/dist/dsl/filter/logicalFilters.cjs.map +1 -0
- package/dist/dsl/filter/logicalFilters.d.cts +109 -0
- package/dist/dsl/filter/logicalFilters.d.ts +109 -0
- package/dist/dsl/filter/logicalFilters.es.js +36 -0
- package/dist/dsl/filter/logicalFilters.es.js.map +1 -0
- package/dist/dsl/filter/metadataFilters.cjs +2 -0
- package/dist/dsl/filter/metadataFilters.cjs.map +1 -0
- package/dist/dsl/filter/metadataFilters.d.cts +132 -0
- package/dist/dsl/filter/metadataFilters.d.ts +132 -0
- package/dist/dsl/filter/metadataFilters.es.js +56 -0
- package/dist/dsl/filter/metadataFilters.es.js.map +1 -0
- package/dist/dsl/filter/operator.cjs +1 -1
- package/dist/dsl/filter/operator.cjs.map +1 -1
- package/dist/dsl/filter/operator.d.cts +11 -9
- package/dist/dsl/filter/operator.d.ts +11 -9
- package/dist/dsl/filter/operator.es.js +1 -1
- package/dist/dsl/filter/operator.es.js.map +1 -1
- package/dist/dsl/filter/searchFilter.cjs +2 -0
- package/dist/dsl/filter/searchFilter.cjs.map +1 -0
- package/dist/dsl/filter/searchFilter.d.cts +39 -0
- package/dist/dsl/filter/searchFilter.d.ts +39 -0
- package/dist/dsl/filter/searchFilter.es.js +19 -0
- package/dist/dsl/filter/searchFilter.es.js.map +1 -0
- package/dist/dsl/filter/timeFilters.cjs +2 -0
- package/dist/dsl/filter/timeFilters.cjs.map +1 -0
- package/dist/dsl/filter/timeFilters.d.cts +290 -0
- package/dist/dsl/filter/timeFilters.d.ts +290 -0
- package/dist/dsl/filter/timeFilters.es.js +89 -0
- package/dist/dsl/filter/timeFilters.es.js.map +1 -0
- package/dist/dsl/filter/types.d.cts +4 -14
- package/dist/dsl/filter/types.d.ts +4 -14
- package/dist/dsl/filter/validate.cjs +1 -1
- package/dist/dsl/filter/validate.cjs.map +1 -1
- package/dist/dsl/filter/validate.d.cts +2 -4
- package/dist/dsl/filter/validate.d.ts +2 -4
- package/dist/dsl/filter/validate.es.js +25 -31
- package/dist/dsl/filter/validate.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 +0 -2
- package/dist/dsl/queryErrorCodes.d.ts +0 -2
- package/dist/dsl/queryErrorCodes.es.js +0 -1
- package/dist/dsl/queryErrorCodes.es.js.map +1 -1
- package/dist/dsl/queryFilter.cjs +2 -0
- package/dist/dsl/queryFilter.cjs.map +1 -0
- package/dist/dsl/queryFilter.d.cts +3 -0
- package/dist/dsl/queryFilter.d.ts +3 -0
- package/dist/dsl/queryFilter.es.js +9 -0
- package/dist/dsl/queryFilter.es.js.map +1 -0
- package/dist/dsl/queryable.cjs +1 -1
- package/dist/dsl/queryable.cjs.map +1 -1
- package/dist/dsl/queryable.es.js +12 -16
- package/dist/dsl/queryable.es.js.map +1 -1
- package/dist/dsl/semanticTypes.cjs +2 -0
- package/dist/dsl/semanticTypes.cjs.map +1 -0
- package/dist/dsl/semanticTypes.d.cts +128 -0
- package/dist/dsl/semanticTypes.d.ts +128 -0
- package/dist/dsl/semanticTypes.es.js +13 -0
- package/dist/dsl/semanticTypes.es.js.map +1 -0
- package/dist/dsl.cjs +1 -1
- package/dist/dsl.d.cts +2 -0
- package/dist/dsl.d.ts +2 -0
- package/dist/dsl.es.js +14 -8
- package/dist/exactJson.cjs +2 -2
- package/dist/exactJson.cjs.map +1 -1
- package/dist/exactJson.d.cts +41 -16
- package/dist/exactJson.d.ts +41 -16
- package/dist/exactJson.es.js +104 -102
- package/dist/exactJson.es.js.map +1 -1
- package/dist/frameworkErrors.cjs.map +1 -1
- package/dist/frameworkErrors.d.cts +4 -4
- package/dist/frameworkErrors.d.ts +4 -4
- package/dist/frameworkErrors.es.js.map +1 -1
- package/dist/http.cjs +1 -1
- package/dist/http.cjs.map +1 -1
- package/dist/http.d.cts +29 -9
- package/dist/http.d.ts +29 -9
- package/dist/http.es.js +24 -19
- package/dist/http.es.js.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.es.js +12 -12
- package/dist/manifest.cjs +1 -1
- package/dist/manifest.cjs.map +1 -1
- package/dist/manifest.d.cts +24 -0
- package/dist/manifest.d.ts +24 -0
- package/dist/manifest.es.js +34 -6
- package/dist/manifest.es.js.map +1 -1
- package/dist/manifestTypes.d.cts +16 -1
- package/dist/manifestTypes.d.ts +16 -1
- package/dist/queries.cjs +1 -1
- package/dist/queries.cjs.map +1 -1
- package/dist/queries.d.cts +18 -8
- package/dist/queries.d.ts +18 -8
- package/dist/queries.es.js +46 -35
- package/dist/queries.es.js.map +1 -1
- package/dist/routes.cjs +1 -1
- package/dist/routes.cjs.map +1 -1
- package/dist/routes.d.cts +16 -3
- package/dist/routes.d.ts +16 -3
- package/dist/routes.es.js +24 -25
- package/dist/routes.es.js.map +1 -1
- package/dist/state.cjs +1 -1
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.cts +21 -19
- package/dist/state.d.ts +21 -19
- package/dist/state.es.js +27 -22
- 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 +14 -7
- package/dist/streams.d.ts +14 -7
- package/dist/streams.es.js +65 -50
- package/dist/streams.es.js.map +1 -1
- package/dist/testing/wire.cjs +1 -1
- package/dist/testing/wire.cjs.map +1 -1
- package/dist/testing/wire.es.js +1 -1
- package/dist/testing/wire.es.js.map +1 -1
- package/dist/wait.cjs +1 -1
- package/dist/wait.cjs.map +1 -1
- package/dist/wait.d.cts +17 -4
- package/dist/wait.d.ts +17 -4
- package/dist/wait.es.js +36 -34
- 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 +75 -31
- package/dist/wire.d.ts +75 -31
- package/dist/wire.es.js +1 -3
- package/dist/wire.es.js.map +1 -1
- package/package.json +5 -5
- package/dist/dsl/filter/datePattern.cjs +0 -2
- package/dist/dsl/filter/datePattern.cjs.map +0 -1
- package/dist/dsl/filter/datePattern.d.cts +0 -8
- package/dist/dsl/filter/datePattern.d.ts +0 -8
- package/dist/dsl/filter/datePattern.es.js +0 -96
- package/dist/dsl/filter/datePattern.es.js.map +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("../filter/operator.cjs"),t=require("
|
|
1
|
+
const e=require("../filter/operator.cjs"),t=require("./types.cjs"),n=require("./expressionTrees.cjs"),r=require("../constantName.cjs"),i=require("./groups.cjs"),a=require("./metrics.cjs"),o=require("./derived.cjs"),s=require("./having.cjs"),c=require("./sort.cjs");function l(e,t,n,r){if(n.length>r)throw TypeError(`${e} must contain at most ${r} ${t}.`)}function u(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(u)}}function d(e,t){n.requireValidExpressionTrees(e.flatMap(e=>e.type===a.AggregationMetricType.NUMERIC||e.type===a.AggregationMetricType.DISTINCT_COUNT||e.type===a.AggregationMetricType.PERCENTILE?[e.expression]:[])),t.forEach(e=>{if(e.type===i.AggregationGroupType.TERMS||e.type===i.AggregationGroupType.HISTOGRAM){if(e.field===void 0==(e.expression===void 0))throw TypeError(`${r.constantName(e.type)} group requires exactly one of field and expression.`);if(e.expression&&n.requireValidExpressionTrees([e.expression]),e.type===i.AggregationGroupType.TERMS&&e.missingKey!==void 0&&e.expression)throw TypeError(`terms missingKey requires a field input.`);if(e.scope===i.AggregationGroupScope.ROOT&&e.expression)throw TypeError(`${r.constantName(e.type)} with a ROOT scope requires a field input.`)}})}var f=new Set([a.AggregationMetricType.ANY,a.AggregationMetricType.FIRST,a.AggregationMetricType.LAST]);function p(e){let t=new Map,n=new Set,r={nodes:0,windows:0};e.forEach(e=>{e.type===a.AggregationMetricType.DERIVED&&(m(e,t,n,r),n.add(e.alias)),t.set(e.alias,f.has(e.type)?e.type:void 0)})}function m(e,n,i,a){let s=[{expression:e.expression,depth:1}];for(;s.length>0;){let{expression:c,depth:l}=s.pop();if(l>t.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH)throw TypeError(`derived expression depth must be at most ${t.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`);if(a.nodes++,a.nodes>t.AGGREGATION_LIMITS.MAX_EXPRESSION_NODES)throw TypeError(`derived expressions must contain at most ${t.AGGREGATION_LIMITS.MAX_EXPRESSION_NODES} nodes.`);switch(c.type){case o.DerivedExpressionType.METRIC_REF:{let t=c.metric;if(!n.has(t))throw TypeError(`derived metric [${e.alias}] must reference a metric declared before it, but was [${t}].`);let i=n.get(t);if(i!==void 0)throw TypeError(`derived metric [${e.alias}] cannot reference ${r.constantName(i)} metric [${t}].`);break}case o.DerivedExpressionType.CONSTANT:if(!Number.isFinite(c.value))throw TypeError(`derived constant must be finite.`);break;case o.DerivedExpressionType.BINARY:s.push({expression:c.left,depth:l+1}),s.push({expression:c.right,depth:l+1});break;case o.DerivedExpressionType.TOTAL:case o.DerivedExpressionType.RUNNING:case o.DerivedExpressionType.PREVIOUS:case o.DerivedExpressionType.RANK:if(a.windows++,a.windows>t.AGGREGATION_LIMITS.MAX_WINDOWS)throw TypeError(`an aggregation must compute at most ${t.AGGREGATION_LIMITS.MAX_WINDOWS} windows.`);h(e.alias,c,n,i);break;default:throw TypeError(`Unsupported derived expression: ${String(c.type)}.`)}}}function h(e,t,n,i){let a=t.metric;if(!n.has(a))throw TypeError(`${r.constantName(t.type)} window of derived metric [${e}] must read a metric declared before it, but was [${a}].`);if(n.get(a)!==void 0||i.has(a))throw TypeError(`${r.constantName(t.type)} window of derived metric [${e}] must read a non-derived numeric metric, but was [${a}].`)}function g(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 _(e,n,i){if(e===void 0)return;if(n.length===0)throw TypeError(`having requires at least one groupBy.`);let o=new Set(i.map(e=>e.alias)),c=new Set(i.filter(e=>e.type===a.AggregationMetricType.ANY).map(e=>e.alias)),l=new Set(i.filter(e=>e.type===a.AggregationMetricType.FIRST||e.type===a.AggregationMetricType.LAST).map(e=>e.alias)),u=[{expression:e,depth:1}];for(;u.length>0;){let{expression:e,depth:n}=u.pop();if(n>t.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH)throw TypeError(`having expression depth must be at most ${t.AGGREGATION_LIMITS.MAX_EXPRESSION_DEPTH}.`);switch(e.type){case s.HavingExpressionType.CONDITION:if(g(e.metric,o,c,l),!Number.isFinite(e.value))throw TypeError(`having condition [${e.metric}] value must be finite.`);break;case s.HavingExpressionType.BETWEEN:if(g(e.metric,o,c,l),!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 s.HavingExpressionType.IN:if(g(e.metric,o,c,l),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 s.HavingExpressionType.IS_NULL:g(e.metric,o,c,l);break;case s.HavingExpressionType.AND:case s.HavingExpressionType.OR:if(e.operands.length===0)throw TypeError(`having ${r.constantName(e.type)} operands must not be empty.`);e.operands.forEach(e=>u.push({expression:e,depth:n+1}));break;default:throw TypeError(`Unsupported having expression: ${String(e.type)}.`)}}}function v(e,n,r){if(l(`sort`,`fields`,e,t.AGGREGATION_LIMITS.MAX_SORT_FIELDS),n.length===0&&e.length>0)throw TypeError(`sort requires at least one groupBy.`);let i=e.map(e=>e.field);if(new Set(i).size!==i.length)throw TypeError(`sort fields must be unique.`);if(!i.every(e=>r.includes(e)))throw TypeError(`sort fields must reference aggregation aliases.`);l(`effective sort`,`fields`,c.effectiveSort({groupBy:[...n],sort:[...e]}),t.AGGREGATION_LIMITS.MAX_SORT_FIELDS)}function y(e){let t=new Map((e.groupBy??[]).map(e=>[e.alias,e.scope===i.AggregationGroupScope.ROOT])),n=c.effectiveSort(e).flatMap(e=>t.has(e.field)?[t.get(e.field)]:[]),r=n.indexOf(!1);if(r>=0&&n.slice(r).includes(!0))throw TypeError(`groups with a ROOT scope must sort before the element groups.`)}function b(e){let n=e.elements??[],a=e.groupBy??[],o=e.metrics,s=e.sort??[];if(l(`elements`,`paths`,n,t.AGGREGATION_LIMITS.MAX_ELEMENTS),l(`groupBy`,`dimensions`,a,t.AGGREGATION_LIMITS.MAX_GROUPS),!Array.isArray(o)||o.length===0)throw TypeError(`metrics must not be empty.`);if(l(`metrics`,`entries`,o,t.AGGREGATION_LIMITS.MAX_METRICS),e.limit!==void 0&&(!Number.isInteger(e.limit)||e.limit<1||e.limit>t.AGGREGATION_LIMITS.MAX_LIMIT))throw TypeError(`limit must be between 1 and ${t.AGGREGATION_LIMITS.MAX_LIMIT}.`);d(o,a),p(o),_(e.having,a,o),o.forEach(e=>{`filter`in e&&e.filter&&u(e.filter)}),a.forEach(e=>{if((e.type===i.AggregationGroupType.DATE_HISTOGRAM||e.type===i.AggregationGroupType.DATE_PART)&&e.dense===!0&&a.length!==1)throw TypeError(`dense requires ${r.constantName(e.type)} to be the only groupBy.`)});let c=[...a.map(e=>e.alias),...o.map(e=>e.alias)];if(new Set(c).size!==c.length)throw TypeError(`aggregation aliases must be unique.`);return v(s,a,c),n.length>0&&y(e),{...e,metrics:[...o]}}exports.admitAggregationQuery=b;
|
|
2
2
|
//# sourceMappingURL=admit.cjs.map
|
|
@@ -1 +1 @@
|
|
|
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,KAA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C,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,C"}
|
|
1
|
+
{"version":3,"file":"admit.cjs","names":["FilterOperator","AggregationMetricType","AggregationGroupType","constantName","AggregationGroupScope","AGGREGATION_LIMITS","DerivedExpressionType","HavingExpressionType","effectiveSort","require_operator","require_types","require_expressionTrees","require_constantName","require_groups","require_metrics","require_derived","require_having","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 DerivedExpressionType,\n type DerivedAggregationMetric,\n type DerivedExpression,\n type DerivedWindow,\n} from './derived.js';\nimport {\n AggregationGroupScope,\n AggregationGroupType,\n type AggregationGroup,\n} from './groups.js';\nimport { HavingExpressionType, type HavingExpression } from './having.js';\nimport { AggregationMetricType, type AggregationMetric } from './metrics.js';\nimport { AGGREGATION_LIMITS, type AggregationQuery } 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,yQAoCA,SAASkB,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,KAAKxB,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,IAClBwB,EAAO,SAAS,QAAQD,CAAoB,CAEhD,CACF,CAMA,SAASE,EACP,EACA,EACM,CACNd,EAAA,4BACEe,EAAQ,QAAQ,GACdC,EAAO,OAAS1B,EAAAA,sBAAsB,SACtC0B,EAAO,OAAS1B,EAAAA,sBAAsB,gBACtC0B,EAAO,OAAS1B,EAAAA,sBAAsB,WAClC,CAAC0B,EAAO,UAAU,EAClB,CAAC,CACP,CACF,EACAC,EAAQ,QAAQ,GAAS,CACvB,GACEC,EAAM,OAAS3B,EAAAA,qBAAqB,OACpC2B,EAAM,OAAS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
|
|
3
|
+
import { requireValidExpressionTrees as n } from "./expressionTrees.es.js";
|
|
4
|
+
import { constantName as r } from "../constantName.es.js";
|
|
5
|
+
import { AggregationGroupScope as i, AggregationGroupType as a } from "./groups.es.js";
|
|
6
|
+
import { AggregationMetricType as o } from "./metrics.es.js";
|
|
7
|
+
import { DerivedExpressionType as s } from "./derived.es.js";
|
|
8
|
+
import { HavingExpressionType as c } from "./having.es.js";
|
|
5
9
|
import { effectiveSort as l } from "./sort.es.js";
|
|
6
10
|
// src/dsl/aggregation/admit.ts
|
|
7
11
|
function u(e, t, n, r) {
|
|
@@ -16,19 +20,19 @@ function d(t) {
|
|
|
16
20
|
case e.NOR: t.operands.forEach(d);
|
|
17
21
|
}
|
|
18
22
|
}
|
|
19
|
-
function f(e,
|
|
20
|
-
|
|
21
|
-
if (e.type ===
|
|
22
|
-
if (e.field === void 0 == (e.expression === void 0)) throw TypeError(`${
|
|
23
|
-
if (e.expression &&
|
|
24
|
-
if (e.scope ===
|
|
23
|
+
function f(e, t) {
|
|
24
|
+
n(e.flatMap((e) => e.type === o.NUMERIC || e.type === o.DISTINCT_COUNT || e.type === o.PERCENTILE ? [e.expression] : [])), t.forEach((e) => {
|
|
25
|
+
if (e.type === a.TERMS || e.type === a.HISTOGRAM) {
|
|
26
|
+
if (e.field === void 0 == (e.expression === void 0)) throw TypeError(`${r(e.type)} group requires exactly one of field and expression.`);
|
|
27
|
+
if (e.expression && n([e.expression]), e.type === a.TERMS && e.missingKey !== void 0 && e.expression) throw TypeError("terms missingKey requires a field input.");
|
|
28
|
+
if (e.scope === i.ROOT && e.expression) throw TypeError(`${r(e.type)} with a ROOT scope requires a field input.`);
|
|
25
29
|
}
|
|
26
30
|
});
|
|
27
31
|
}
|
|
28
32
|
var p = /* @__PURE__ */ new Set([
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
o.ANY,
|
|
34
|
+
o.FIRST,
|
|
35
|
+
o.LAST
|
|
32
36
|
]);
|
|
33
37
|
function m(e) {
|
|
34
38
|
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Set(), r = {
|
|
@@ -36,122 +40,122 @@ function m(e) {
|
|
|
36
40
|
windows: 0
|
|
37
41
|
};
|
|
38
42
|
e.forEach((e) => {
|
|
39
|
-
e.type ===
|
|
43
|
+
e.type === o.DERIVED && (h(e, t, n, r), n.add(e.alias)), t.set(e.alias, p.has(e.type) ? e.type : void 0);
|
|
40
44
|
});
|
|
41
45
|
}
|
|
42
|
-
function h(e,
|
|
43
|
-
let
|
|
46
|
+
function h(e, n, i, a) {
|
|
47
|
+
let o = [{
|
|
44
48
|
expression: e.expression,
|
|
45
49
|
depth: 1
|
|
46
50
|
}];
|
|
47
|
-
for (;
|
|
48
|
-
let { expression: c, depth: l } =
|
|
49
|
-
if (l >
|
|
50
|
-
if (a.nodes++, a.nodes >
|
|
51
|
+
for (; o.length > 0;) {
|
|
52
|
+
let { expression: c, depth: l } = o.pop();
|
|
53
|
+
if (l > t.MAX_EXPRESSION_DEPTH) throw TypeError(`derived expression depth must be at most ${t.MAX_EXPRESSION_DEPTH}.`);
|
|
54
|
+
if (a.nodes++, a.nodes > t.MAX_EXPRESSION_NODES) throw TypeError(`derived expressions must contain at most ${t.MAX_EXPRESSION_NODES} nodes.`);
|
|
51
55
|
switch (c.type) {
|
|
52
|
-
case
|
|
53
|
-
let
|
|
54
|
-
if (!
|
|
55
|
-
let i =
|
|
56
|
-
if (i !== void 0) throw TypeError(`derived metric [${e.alias}] cannot reference ${
|
|
56
|
+
case s.METRIC_REF: {
|
|
57
|
+
let t = c.metric;
|
|
58
|
+
if (!n.has(t)) throw TypeError(`derived metric [${e.alias}] must reference a metric declared before it, but was [${t}].`);
|
|
59
|
+
let i = n.get(t);
|
|
60
|
+
if (i !== void 0) throw TypeError(`derived metric [${e.alias}] cannot reference ${r(i)} metric [${t}].`);
|
|
57
61
|
break;
|
|
58
62
|
}
|
|
59
|
-
case
|
|
63
|
+
case s.CONSTANT:
|
|
60
64
|
if (!Number.isFinite(c.value)) throw TypeError("derived constant must be finite.");
|
|
61
65
|
break;
|
|
62
|
-
case
|
|
63
|
-
|
|
66
|
+
case s.BINARY:
|
|
67
|
+
o.push({
|
|
64
68
|
expression: c.left,
|
|
65
69
|
depth: l + 1
|
|
66
|
-
}),
|
|
70
|
+
}), o.push({
|
|
67
71
|
expression: c.right,
|
|
68
72
|
depth: l + 1
|
|
69
73
|
});
|
|
70
74
|
break;
|
|
71
|
-
case
|
|
72
|
-
case
|
|
73
|
-
case
|
|
74
|
-
case
|
|
75
|
-
if (a.windows++, a.windows >
|
|
76
|
-
g(e.alias, c,
|
|
75
|
+
case s.TOTAL:
|
|
76
|
+
case s.RUNNING:
|
|
77
|
+
case s.PREVIOUS:
|
|
78
|
+
case s.RANK:
|
|
79
|
+
if (a.windows++, a.windows > t.MAX_WINDOWS) throw TypeError(`an aggregation must compute at most ${t.MAX_WINDOWS} windows.`);
|
|
80
|
+
g(e.alias, c, n, i);
|
|
77
81
|
break;
|
|
78
82
|
default: throw TypeError(`Unsupported derived expression: ${String(c.type)}.`);
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
|
-
function g(e,
|
|
83
|
-
let a =
|
|
84
|
-
if (!
|
|
85
|
-
if (
|
|
86
|
+
function g(e, t, n, i) {
|
|
87
|
+
let a = t.metric;
|
|
88
|
+
if (!n.has(a)) throw TypeError(`${r(t.type)} window of derived metric [${e}] must read a metric declared before it, but was [${a}].`);
|
|
89
|
+
if (n.get(a) !== void 0 || i.has(a)) throw TypeError(`${r(t.type)} window of derived metric [${e}] must read a non-derived numeric metric, but was [${a}].`);
|
|
86
90
|
}
|
|
87
91
|
function _(e, t, n, r) {
|
|
88
92
|
if (!t.has(e)) throw TypeError(`having condition [${e}] must reference a declared metric alias.`);
|
|
89
93
|
if (n.has(e)) throw TypeError(`having condition [${e}] cannot reference ANY metric.`);
|
|
90
94
|
if (r.has(e)) throw TypeError(`having condition [${e}] cannot reference FIRST or LAST metric.`);
|
|
91
95
|
}
|
|
92
|
-
function v(e,
|
|
96
|
+
function v(e, n, i) {
|
|
93
97
|
if (e === void 0) return;
|
|
94
|
-
if (
|
|
95
|
-
let
|
|
98
|
+
if (n.length === 0) throw TypeError("having requires at least one groupBy.");
|
|
99
|
+
let a = new Set(i.map((e) => e.alias)), s = new Set(i.filter((e) => e.type === o.ANY).map((e) => e.alias)), l = new Set(i.filter((e) => e.type === o.FIRST || e.type === o.LAST).map((e) => e.alias)), u = [{
|
|
96
100
|
expression: e,
|
|
97
101
|
depth: 1
|
|
98
102
|
}];
|
|
99
103
|
for (; u.length > 0;) {
|
|
100
|
-
let { expression: e, depth:
|
|
101
|
-
if (
|
|
104
|
+
let { expression: e, depth: n } = u.pop();
|
|
105
|
+
if (n > t.MAX_EXPRESSION_DEPTH) throw TypeError(`having expression depth must be at most ${t.MAX_EXPRESSION_DEPTH}.`);
|
|
102
106
|
switch (e.type) {
|
|
103
|
-
case
|
|
104
|
-
if (_(e.metric,
|
|
107
|
+
case c.CONDITION:
|
|
108
|
+
if (_(e.metric, a, s, l), !Number.isFinite(e.value)) throw TypeError(`having condition [${e.metric}] value must be finite.`);
|
|
105
109
|
break;
|
|
106
|
-
case
|
|
107
|
-
if (_(e.metric,
|
|
110
|
+
case c.BETWEEN:
|
|
111
|
+
if (_(e.metric, a, s, l), !Number.isFinite(e.lower) || !Number.isFinite(e.upper)) throw TypeError(`having between [${e.metric}] bounds must be finite.`);
|
|
108
112
|
if (e.lower > e.upper) throw TypeError(`having between [${e.metric}] lower bound must not exceed upper bound.`);
|
|
109
113
|
break;
|
|
110
|
-
case
|
|
111
|
-
if (_(e.metric,
|
|
114
|
+
case c.IN:
|
|
115
|
+
if (_(e.metric, a, s, l), e.values.length === 0) throw TypeError(`having in [${e.metric}] values must not be empty.`);
|
|
112
116
|
if (!e.values.every(Number.isFinite)) throw TypeError(`having in [${e.metric}] values must be finite.`);
|
|
113
117
|
break;
|
|
114
|
-
case
|
|
115
|
-
_(e.metric,
|
|
118
|
+
case c.IS_NULL:
|
|
119
|
+
_(e.metric, a, s, l);
|
|
116
120
|
break;
|
|
117
|
-
case
|
|
118
|
-
case
|
|
119
|
-
if (e.operands.length === 0) throw TypeError(`having ${
|
|
121
|
+
case c.AND:
|
|
122
|
+
case c.OR:
|
|
123
|
+
if (e.operands.length === 0) throw TypeError(`having ${r(e.type)} operands must not be empty.`);
|
|
120
124
|
e.operands.forEach((e) => u.push({
|
|
121
125
|
expression: e,
|
|
122
|
-
depth:
|
|
126
|
+
depth: n + 1
|
|
123
127
|
}));
|
|
124
128
|
break;
|
|
125
129
|
default: throw TypeError(`Unsupported having expression: ${String(e.type)}.`);
|
|
126
130
|
}
|
|
127
131
|
}
|
|
128
132
|
}
|
|
129
|
-
function y(e,
|
|
130
|
-
if (u("sort", "fields", e,
|
|
133
|
+
function y(e, n, r) {
|
|
134
|
+
if (u("sort", "fields", e, t.MAX_SORT_FIELDS), n.length === 0 && e.length > 0) throw TypeError("sort requires at least one groupBy.");
|
|
131
135
|
let i = e.map((e) => e.field);
|
|
132
136
|
if (new Set(i).size !== i.length) throw TypeError("sort fields must be unique.");
|
|
133
137
|
if (!i.every((e) => r.includes(e))) throw TypeError("sort fields must reference aggregation aliases.");
|
|
134
138
|
u("effective sort", "fields", l({
|
|
135
|
-
groupBy: [...
|
|
139
|
+
groupBy: [...n],
|
|
136
140
|
sort: [...e]
|
|
137
|
-
}),
|
|
141
|
+
}), t.MAX_SORT_FIELDS);
|
|
138
142
|
}
|
|
139
143
|
function b(e) {
|
|
140
|
-
let t = new Map((e.groupBy ?? []).map((e) => [e.alias, e.scope ===
|
|
141
|
-
if (
|
|
144
|
+
let t = new Map((e.groupBy ?? []).map((e) => [e.alias, e.scope === i.ROOT])), n = l(e).flatMap((e) => t.has(e.field) ? [t.get(e.field)] : []), r = n.indexOf(!1);
|
|
145
|
+
if (r >= 0 && n.slice(r).includes(!0)) throw TypeError("groups with a ROOT scope must sort before the element groups.");
|
|
142
146
|
}
|
|
143
147
|
function x(e) {
|
|
144
|
-
let
|
|
145
|
-
if (u("elements", "paths",
|
|
146
|
-
if (u("metrics", "entries", o,
|
|
147
|
-
f(o,
|
|
148
|
+
let n = e.elements ?? [], i = e.groupBy ?? [], o = e.metrics, s = e.sort ?? [];
|
|
149
|
+
if (u("elements", "paths", n, t.MAX_ELEMENTS), u("groupBy", "dimensions", i, t.MAX_GROUPS), !Array.isArray(o) || o.length === 0) throw TypeError("metrics must not be empty.");
|
|
150
|
+
if (u("metrics", "entries", o, t.MAX_METRICS), e.limit !== void 0 && (!Number.isInteger(e.limit) || e.limit < 1 || e.limit > t.MAX_LIMIT)) throw TypeError(`limit must be between 1 and ${t.MAX_LIMIT}.`);
|
|
151
|
+
f(o, i), m(o), v(e.having, i, o), o.forEach((e) => {
|
|
148
152
|
"filter" in e && e.filter && d(e.filter);
|
|
149
|
-
}),
|
|
150
|
-
if ((e.type ===
|
|
153
|
+
}), i.forEach((e) => {
|
|
154
|
+
if ((e.type === a.DATE_HISTOGRAM || e.type === a.DATE_PART) && e.dense === !0 && i.length !== 1) throw TypeError(`dense requires ${r(e.type)} to be the only groupBy.`);
|
|
151
155
|
});
|
|
152
|
-
let c = [...
|
|
156
|
+
let c = [...i.map((e) => e.alias), ...o.map((e) => e.alias)];
|
|
153
157
|
if (new Set(c).size !== c.length) throw TypeError("aggregation aliases must be unique.");
|
|
154
|
-
return y(s,
|
|
158
|
+
return y(s, i, c), n.length > 0 && b(e), {
|
|
155
159
|
...e,
|
|
156
160
|
metrics: [...o]
|
|
157
161
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admit.es.js","names":["requireAtMost","items","max","name","noun","validateMetricFilter","filter","FilterOperator","validateExpressions","requireValidExpressionTrees","metrics","metric","AggregationMetricType","groupBy","group","AggregationGroupType","constantName","AggregationGroupScope","NON_NUMERIC","validateDerivedMetrics","declared","derived","counter","validateDerivedExpression","pending","depth","AGGREGATION_LIMITS","expression","DerivedExpressionType","reference","nonNumeric","requireWindowMetric","target","window","alias","requireValidHavingMetric","metricAliases","anyAliases","edgeAliases","validateHaving","having","HavingExpressionType","operand","validateSort","sort","fields","entry","aliases","field","effectiveSort","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":";;;;;;AAqCA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
|
|
1
|
+
{"version":3,"file":"admit.es.js","names":["requireAtMost","items","max","name","noun","validateMetricFilter","filter","FilterOperator","validateExpressions","requireValidExpressionTrees","metrics","metric","AggregationMetricType","groupBy","group","AggregationGroupType","constantName","AggregationGroupScope","NON_NUMERIC","validateDerivedMetrics","declared","derived","counter","validateDerivedExpression","pending","depth","AGGREGATION_LIMITS","expression","DerivedExpressionType","reference","nonNumeric","requireWindowMetric","target","window","alias","requireValidHavingMetric","metricAliases","anyAliases","edgeAliases","validateHaving","having","HavingExpressionType","operand","validateSort","sort","fields","entry","aliases","field","effectiveSort","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 DerivedExpressionType,\n type DerivedAggregationMetric,\n type DerivedExpression,\n type DerivedWindow,\n} from './derived.js';\nimport {\n AggregationGroupScope,\n AggregationGroupType,\n type AggregationGroup,\n} from './groups.js';\nimport { HavingExpressionType, type HavingExpression } from './having.js';\nimport { AggregationMetricType, type AggregationMetric } from './metrics.js';\nimport { AGGREGATION_LIMITS, type AggregationQuery } 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":";;;;;;;;;;AAoCA,SAASA,EACP,GACA,GACA,GACA,GACM;CACN,IAAIC,EAAM,SAASC,GACjB,MAAU,UAAU,GAAGC,EAAK,wBAAwBD,EAAI,GAAGE,EAAK,EAAE;AAEtE;AAWA,SAASC,EAAqB,GAAgC;CAC5D,QAAQC,EAAO,IAAf;EACE,KAAKC,EAAe,QAClB,MAAU,UACR,2DACF;EACF,KAAKA,EAAe,eAClB,MAAU,UACR,4DACF;EACF,KAAKA,EAAe;EACpB,KAAKA,EAAe;EACpB,KAAKA,EAAe,KAClB,EAAO,SAAS,QAAQF,CAAoB;CAEhD;AACF;AAMA,SAASG,EACP,GACA,GACM;CAUN,AATAC,EACEC,EAAQ,SAAQ,MACdC,EAAO,SAASC,EAAsB,WACtCD,EAAO,SAASC,EAAsB,kBACtCD,EAAO,SAASC,EAAsB,aAClC,CAACD,EAAO,UAAU,IAClB,CAAC,CACP,CACF,GACAE,EAAQ,SAAQ,MAAS;EACvB,IACEC,EAAM,SAASC,EAAqB,SACpCD,EAAM,SAASC,EAAqB,WAGtC;OAAKD,EAAM,UAAU,KAAA,MAAgBA,EAAM,eAAe,KAAA,IACxD,MAAU,UACR,GAAGE,EAAaF,EAAM,IAAI,EAAE,qDAC9B;GAGF,IADIA,EAAM,cAAYL,EAA4B,CAACK,EAAM,UAAU,CAAC,GAElEA,EAAM,SAASC,EAAqB,SACpCD,EAAM,eAAe,KAAA,KACrBA,EAAM,YAEN,MAAU,UAAU,0CAA0C;GAEhE,IAAIA,EAAM,UAAUG,EAAsB,QAAQH,EAAM,YACt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nC,GAAGJ,EAAQ,KAAI,MAAUC,EAAO,KAAK,CACvC;CACA,IAAI,IAAI,IAAIoC,CAAO,CAAC,CAAC,SAASA,EAAQ,QACpC,MAAU,UAAU,qCAAqC;CAK3D,OAHAJ,EAAaC,GAAM/B,GAASkC,CAAO,GAC/BS,EAAS,SAAS,KAAGN,EAAuBE,CAAK,GAE9C;EACL,GAAGA;EACH,SAAS,CAAC,GAAG1C,CAAO;CACtB;AACF"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=require("../field.cjs");function t(t){return e.queryField(t)}function n(e){if(t(e),e.includes(`.`))throw TypeError(`aggregation alias must contain one segment.`);if(e.startsWith(`__wow`))throw TypeError(`aggregation alias must not use the reserved __wow prefix.`);return e}exports.aggregationAlias=n,exports.aggregationField=t;
|
|
2
|
+
//# sourceMappingURL=aliases.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aliases.cjs","names":["queryField","require_field","aggregationField","field","aggregationAlias","alias"],"sources":["../../../src/dsl/aggregation/aliases.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 { queryField } from '../field.js';\n\n// Internal: how the parts of an aggregation name their fields and their\n// columns. index.ts does not re-export this file.\n\nexport function aggregationField<FIELDS extends string>(field: FIELDS): FIELDS {\n return queryField(field);\n}\n\nexport function aggregationAlias(alias: string): string {\n aggregationField(alias);\n if (alias.includes('.')) {\n throw new TypeError('aggregation alias must contain one segment.');\n }\n if (alias.startsWith('__wow')) {\n throw new TypeError(\n 'aggregation alias must not use the reserved __wow prefix.',\n );\n }\n return alias;\n}\n"],"mappings":"A,gCAkBA,SAAgBE,EAAwC,EAAuB,CAC7E,OAAOF,EAAAA,WAAWG,CAAK,CACzB,CAEA,SAAgBC,EAAiB,EAAuB,CAEtD,GADAF,EAAiBG,CAAK,EAClBA,EAAM,SAAS,GAAG,EACpB,MAAU,UAAU,6CAA6C,EAEnE,GAAIA,EAAM,WAAW,OAAO,EAC1B,MAAU,UACR,2DACF,EAEF,OAAOA,CACT,C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { queryField as e } from "../field.es.js";
|
|
2
|
+
// src/dsl/aggregation/aliases.ts
|
|
3
|
+
function t(t) {
|
|
4
|
+
return e(t);
|
|
5
|
+
}
|
|
6
|
+
function n(e) {
|
|
7
|
+
if (t(e), e.includes(".")) throw TypeError("aggregation alias must contain one segment.");
|
|
8
|
+
if (e.startsWith("__wow")) throw TypeError("aggregation alias must not use the reserved __wow prefix.");
|
|
9
|
+
return e;
|
|
10
|
+
}
|
|
11
|
+
export { n as aggregationAlias, t as aggregationField };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=aliases.es.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aliases.es.js","names":["aggregationField","queryField","field","aggregationAlias","alias"],"sources":["../../../src/dsl/aggregation/aliases.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 { queryField } from '../field.js';\n\n// Internal: how the parts of an aggregation name their fields and their\n// columns. index.ts does not re-export this file.\n\nexport function aggregationField<FIELDS extends string>(field: FIELDS): FIELDS {\n return queryField(field);\n}\n\nexport function aggregationAlias(alias: string): string {\n aggregationField(alias);\n if (alias.includes('.')) {\n throw new TypeError('aggregation alias must contain one segment.');\n }\n if (alias.startsWith('__wow')) {\n throw new TypeError(\n 'aggregation alias must not use the reserved __wow prefix.',\n );\n }\n return alias;\n}\n"],"mappings":";;AAkBA,SAAgBA,EAAwC,GAAuB;CAC7E,OAAOC,EAAWC,CAAK;AACzB;AAEA,SAAgBC,EAAiB,GAAuB;CAEtD,IADAH,EAAiBI,CAAK,GAClBA,EAAM,SAAS,GAAG,GACpB,MAAU,UAAU,6CAA6C;CAEnE,IAAIA,EAAM,WAAW,OAAO,GAC1B,MAAU,UACR,2DACF;CAEF,OAAOA;AACT"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=require("
|
|
1
|
+
const e=require("./types.cjs"),t=require("../filter/scope.cjs"),n=require("./aliases.cjs"),r=require("./groups.cjs"),i=require("./metrics.cjs"),a=require("./derived.cjs"),o=require("./having.cjs"),s=require("./admit.cjs");function c(t,n,r){return{type:e.AggregationExpressionType.BINARY,operator:t,left:n,right:r}}var l={element(e,r){let i=n.aggregationField(e);return r===void 0?{path:i}:(t.requireElementScopedFilter(r,`Aggregation element filter`,!1),{path:i,filter:r})},field(t){return{type:e.AggregationExpressionType.FIELD,field:n.aggregationField(t)}},constant(t){if(!Number.isFinite(t))throw TypeError(`aggregation constant must be finite.`);return{type:e.AggregationExpressionType.CONSTANT,value:t}},dateDiff(t,r,i){if(!Object.values(e.DateDiffUnit).includes(i))throw TypeError(`date diff unit is invalid.`);return{type:e.AggregationExpressionType.DATE_DIFF,from:n.aggregationField(t),to:n.aggregationField(r),unit:i}},add:(t,n)=>c(e.AggregationExpressionOperator.ADD,t,n),subtract:(t,n)=>c(e.AggregationExpressionOperator.SUBTRACT,t,n),multiply:(t,n)=>c(e.AggregationExpressionOperator.MULTIPLY,t,n),divide:(t,n)=>c(e.AggregationExpressionOperator.DIVIDE,t,n),query(e){return s.admitAggregationQuery(e)}},u={...l,...r.groupBuilders,...i.metricBuilders,...a.derivedBuilders,...o.havingBuilders};exports.aggregation=u,exports.queryBuilders=l;
|
|
2
2
|
//# sourceMappingURL=builders.cjs.map
|