@finos/legend-query-builder 4.16.7 → 4.16.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. package/lib/graph/QueryBuilderMetaModelConst.d.ts +1 -0
  2. package/lib/graph/QueryBuilderMetaModelConst.d.ts.map +1 -1
  3. package/lib/graph/QueryBuilderMetaModelConst.js +1 -0
  4. package/lib/graph/QueryBuilderMetaModelConst.js.map +1 -1
  5. package/lib/graph-manager/protocol/pure/QueryBuilder_PureProtocolProcessorPlugin.js +5 -2
  6. package/lib/graph-manager/protocol/pure/QueryBuilder_PureProtocolProcessorPlugin.js.map +1 -1
  7. package/lib/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.d.ts +3 -1
  8. package/lib/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.d.ts.map +1 -1
  9. package/lib/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.js +99 -2
  10. package/lib/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.js.map +1 -1
  11. package/lib/index.css +1 -1
  12. package/lib/package.json +5 -5
  13. package/lib/stores/QueryBuilderStateBuilder.d.ts.map +1 -1
  14. package/lib/stores/QueryBuilderStateBuilder.js +21 -2
  15. package/lib/stores/QueryBuilderStateBuilder.js.map +1 -1
  16. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.d.ts +19 -0
  17. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.d.ts.map +1 -0
  18. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.js +98 -0
  19. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.js.map +1 -0
  20. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.d.ts +21 -0
  21. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.d.ts.map +1 -0
  22. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.js +102 -0
  23. package/lib/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.js.map +1 -0
  24. package/lib/stores/fetch-structure/tds/projection/QueryBuilderProjectionValueSpecificationBuilder.d.ts.map +1 -1
  25. package/lib/stores/fetch-structure/tds/projection/QueryBuilderProjectionValueSpecificationBuilder.js +9 -3
  26. package/lib/stores/fetch-structure/tds/projection/QueryBuilderProjectionValueSpecificationBuilder.js.map +1 -1
  27. package/lib/stores/fetch-structure/tds/projection/{QueryBuilderRelationProjectValueSpecBuidler.d.ts → QueryBuilderRelationProjectValueSpecBuilder.d.ts} +1 -1
  28. package/lib/stores/fetch-structure/tds/projection/{QueryBuilderRelationProjectValueSpecBuidler.d.ts.map → QueryBuilderRelationProjectValueSpecBuilder.d.ts.map} +1 -1
  29. package/lib/stores/fetch-structure/tds/projection/{QueryBuilderRelationProjectValueSpecBuidler.js → QueryBuilderRelationProjectValueSpecBuilder.js} +1 -1
  30. package/lib/stores/fetch-structure/tds/projection/{QueryBuilderRelationProjectValueSpecBuidler.js.map → QueryBuilderRelationProjectValueSpecBuilder.js.map} +1 -1
  31. package/lib/stores/fetch-structure/tds/projection/QueryBuilderTypedProjectionStateBuilder.d.ts.map +1 -1
  32. package/lib/stores/fetch-structure/tds/projection/QueryBuilderTypedProjectionStateBuilder.js +8 -9
  33. package/lib/stores/fetch-structure/tds/projection/QueryBuilderTypedProjectionStateBuilder.js.map +1 -1
  34. package/package.json +15 -15
  35. package/src/graph/QueryBuilderMetaModelConst.ts +1 -0
  36. package/src/graph-manager/protocol/pure/QueryBuilder_PureProtocolProcessorPlugin.ts +4 -4
  37. package/src/graph-manager/protocol/pure/v1/V1_QueryValueSpecificationBuilderHelper.ts +223 -2
  38. package/src/stores/QueryBuilderStateBuilder.ts +40 -8
  39. package/src/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.ts +196 -0
  40. package/src/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.ts +253 -0
  41. package/src/stores/fetch-structure/tds/projection/QueryBuilderProjectionValueSpecificationBuilder.ts +11 -3
  42. package/src/stores/fetch-structure/tds/projection/QueryBuilderTypedProjectionStateBuilder.ts +8 -9
  43. package/tsconfig.json +3 -1
  44. /package/src/stores/fetch-structure/tds/projection/{QueryBuilderRelationProjectValueSpecBuidler.ts → QueryBuilderRelationProjectValueSpecBuilder.ts} +0 -0
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Copyright (c) 2020-present, Goldman Sachs
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import {
18
+ type ColSpec,
19
+ type LambdaFunction,
20
+ ColSpecArrayInstance,
21
+ LambdaFunctionInstanceValue,
22
+ matchFunctionName,
23
+ RelationType,
24
+ SimpleFunctionExpression,
25
+ VariableExpression,
26
+ } from '@finos/legend-graph';
27
+ import {
28
+ assertTrue,
29
+ assertType,
30
+ guaranteeNonNullable,
31
+ guaranteeType,
32
+ returnUndefOnError,
33
+ UnsupportedOperationError,
34
+ } from '@finos/legend-shared';
35
+ import {
36
+ QUERY_BUILDER_LAMBDA_WRITER_MODE,
37
+ type QueryBuilderState,
38
+ } from '../../../QueryBuilderState.js';
39
+ import { QUERY_BUILDER_SUPPORTED_FUNCTIONS } from '../../../../graph/QueryBuilderMetaModelConst.js';
40
+ import { QueryBuilderValueSpecificationProcessor } from '../../../QueryBuilderStateBuilder.js';
41
+ import { QueryBuilderTDSState } from '../QueryBuilderTDSState.js';
42
+ import { QueryBuilderAggregateOperator_Wavg } from './operators/QueryBuilderAggregateOperator_Wavg.js';
43
+
44
+ export const processTypedAggregationColSpec = (
45
+ colSpec: ColSpec,
46
+ parentExpression: SimpleFunctionExpression | undefined,
47
+ queryBuilderState: QueryBuilderState,
48
+ ): void => {
49
+ // check parent expression
50
+ assertTrue(
51
+ Boolean(
52
+ parentExpression &&
53
+ matchFunctionName(
54
+ parentExpression.functionName,
55
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_GROUP_BY,
56
+ ),
57
+ ),
58
+ `Can't process typed aggregation ColSpec: only supported when used within a groupBy() expression`,
59
+ );
60
+
61
+ // Check that there are 2 lambdas, one for map and one for reduce
62
+ const mapLambdaFunctionInstance = guaranteeType(
63
+ colSpec.function1,
64
+ LambdaFunctionInstanceValue,
65
+ `Can't process colSpec: function1 is not a lambda function instance value`,
66
+ );
67
+ assertTrue(
68
+ mapLambdaFunctionInstance.values.length === 1,
69
+ `Can't process typed aggregation ColSpec. function1 should only have 1 lambda value.`,
70
+ );
71
+ assertTrue(
72
+ guaranteeNonNullable(mapLambdaFunctionInstance.values[0]).expressionSequence
73
+ .length === 1,
74
+ `Can't process typed aggregation ColSpec. function1 lambda should only have 1 expression.`,
75
+ );
76
+
77
+ const reduceLambdaFunctionInstance = guaranteeType(
78
+ colSpec.function2,
79
+ LambdaFunctionInstanceValue,
80
+ `Can't process colSpec: function2 is not a lambda function instance value`,
81
+ );
82
+
83
+ // build state
84
+ if (
85
+ queryBuilderState.fetchStructureState.implementation instanceof
86
+ QueryBuilderTDSState
87
+ ) {
88
+ const tdsState = queryBuilderState.fetchStructureState.implementation;
89
+ const aggregationState = tdsState.aggregationState;
90
+ const projectionColumnState = guaranteeNonNullable(
91
+ tdsState.projectionColumns.find(
92
+ (projectionColumn) => projectionColumn.columnName === colSpec.name,
93
+ ),
94
+ `Projection column with name ${colSpec.name} not found`,
95
+ );
96
+ const reduceLambdaFunction = guaranteeNonNullable(
97
+ reduceLambdaFunctionInstance.values[0],
98
+ `Can't process colSpec: function2 lambda function is missing`,
99
+ );
100
+ assertTrue(
101
+ reduceLambdaFunction.expressionSequence.length === 1,
102
+ `Can't process colSpec: only support colSpec function2 lambda body with 1 expression`,
103
+ );
104
+ assertTrue(
105
+ reduceLambdaFunction.functionType.parameters.length === 1,
106
+ `Can't process colSpec function2 lambda: only support lambda with 1 parameter`,
107
+ );
108
+ const reduceFunctionExpression = guaranteeType(
109
+ reduceLambdaFunction.expressionSequence[0],
110
+ SimpleFunctionExpression,
111
+ `Can't process colSpec: only support colSpec function2 lambda body with 1 expression`,
112
+ );
113
+
114
+ const lambdaParam = guaranteeType(
115
+ reduceLambdaFunction.functionType.parameters[0],
116
+ VariableExpression,
117
+ `Can't process colSpec function2 lambda: parameter is missing`,
118
+ );
119
+
120
+ for (const operator of aggregationState.operators) {
121
+ // NOTE: this allow plugin author to either return `undefined` or throw error
122
+ // if there is a problem with building the lambda. Either case, the plugin is
123
+ // considered as not supporting the lambda.
124
+ const aggregateColumnState = returnUndefOnError(() =>
125
+ operator.buildAggregateColumnState(
126
+ reduceFunctionExpression,
127
+ lambdaParam,
128
+ projectionColumnState,
129
+ ),
130
+ );
131
+ if (
132
+ projectionColumnState.wavgWeight &&
133
+ aggregateColumnState &&
134
+ aggregateColumnState.operator instanceof
135
+ QueryBuilderAggregateOperator_Wavg
136
+ ) {
137
+ aggregateColumnState.operator.setWeight(
138
+ projectionColumnState.wavgWeight,
139
+ );
140
+ }
141
+ if (aggregateColumnState) {
142
+ aggregationState.addColumn(aggregateColumnState);
143
+
144
+ // Update parent groupBy() expression's return type with this column's return type
145
+ // (a temporary return type was set when we processed the groupBy() protocol)
146
+ const parentGroupByRelationType = guaranteeType(
147
+ parentExpression?.genericType?.value.typeArguments?.[0]?.value
148
+ .rawType,
149
+ RelationType,
150
+ `Can't process colSpec: parent groupBy() expression's return type is not a relation`,
151
+ );
152
+ const relationTypeColumn = guaranteeNonNullable(
153
+ parentGroupByRelationType.columns.find(
154
+ (_column) => _column.name === colSpec.name,
155
+ ),
156
+ `Can't process colSpec: Can't find column '${colSpec.name}' in parent groupBy() expression's relation return type`,
157
+ );
158
+ relationTypeColumn.type =
159
+ aggregateColumnState.getColumnType() ?? relationTypeColumn.type;
160
+ return;
161
+ }
162
+ }
163
+ }
164
+ throw new UnsupportedOperationError(
165
+ `Can't process aggregate expression function: no compatible aggregate operator processer available from plugins`,
166
+ );
167
+ };
168
+
169
+ export const processTypedGroupByExpression = (
170
+ expression: SimpleFunctionExpression,
171
+ queryBuilderState: QueryBuilderState,
172
+ parentLambda: LambdaFunction,
173
+ ): void => {
174
+ // check parameters
175
+ assertTrue(
176
+ expression.parametersValues.length === 3,
177
+ `Can't process groupBy() expression: groupBy() expects 2 arguments`,
178
+ );
179
+
180
+ // check preceding expression is relation project, then process the project
181
+ const precedingExpression = guaranteeType(
182
+ expression.parametersValues[0],
183
+ SimpleFunctionExpression,
184
+ `Can't process groupBy() expression: only support groupBy() immediately following an expression`,
185
+ );
186
+ assertTrue(
187
+ matchFunctionName(precedingExpression.functionName, [
188
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_PROJECT,
189
+ ]),
190
+ `Can't process groupBy() expression: only support groupBy() immediately following relation project()`,
191
+ );
192
+ QueryBuilderValueSpecificationProcessor.process(
193
+ precedingExpression,
194
+ parentLambda,
195
+ queryBuilderState,
196
+ );
197
+
198
+ const tdsState = guaranteeType(
199
+ queryBuilderState.fetchStructureState.implementation,
200
+ QueryBuilderTDSState,
201
+ );
202
+
203
+ // process normal (non-aggregation) columns (ensure columns exist in project expression)
204
+ const columnExpressions = guaranteeType(
205
+ expression.parametersValues[1],
206
+ ColSpecArrayInstance,
207
+ `Can't process groupBy() expression: groupBy() expects argument #1 to be a ColSpecArrayInstance`,
208
+ );
209
+ assertTrue(
210
+ columnExpressions.values.length === 1,
211
+ `Can't process groupBy() expression: groupBy() expects argument #1 to be a ColSpecArrayInstance with 1 element`,
212
+ );
213
+ queryBuilderState.setLambdaWriteMode(
214
+ QUERY_BUILDER_LAMBDA_WRITER_MODE.TYPED_FETCH_STRUCTURE,
215
+ );
216
+ columnExpressions.values[0]?.colSpecs.forEach((colSpec) => {
217
+ assertTrue(
218
+ tdsState.projectionColumns.filter(
219
+ (projectedColumn) => projectedColumn.columnName === colSpec.name,
220
+ ).length === 1,
221
+ `Can't process groupBy() expression: column '${colSpec.name}' not found in project() expression`,
222
+ );
223
+ });
224
+
225
+ // process aggregation columns
226
+ const aggregationLambdas = expression.parametersValues[2];
227
+ assertType(
228
+ aggregationLambdas,
229
+ ColSpecArrayInstance,
230
+ `Can't process groupBy() expression: groupBy() expects argument #2 to be a ColSpecArrayInstance`,
231
+ );
232
+ QueryBuilderValueSpecificationProcessor.processChild(
233
+ aggregationLambdas,
234
+ expression,
235
+ parentLambda,
236
+ queryBuilderState,
237
+ );
238
+ };
239
+
240
+ export const isTypedGroupByExpression = (
241
+ expression: SimpleFunctionExpression,
242
+ ): boolean => {
243
+ return (
244
+ expression.functionName ===
245
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_GROUP_BY ||
246
+ (matchFunctionName(expression.functionName, [
247
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.TDS_GROUP_BY,
248
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_GROUP_BY,
249
+ ]) &&
250
+ expression.parametersValues.length === 3 &&
251
+ expression.parametersValues[1] instanceof ColSpecArrayInstance)
252
+ );
253
+ };
@@ -49,9 +49,10 @@ import {
49
49
  } from '../../../QueryBuilderValueSpecificationBuilderHelper.js';
50
50
  import { appendOLAPGroupByState } from '../window/QueryBuilderWindowValueSpecificationBuilder.js';
51
51
  import { appendPostFilter } from '../post-filter/QueryBuilderPostFilterValueSpecificationBuilder.js';
52
- import { buildRelationProjection } from './QueryBuilderRelationProjectValueSpecBuidler.js';
52
+ import { buildRelationProjection } from './QueryBuilderRelationProjectValueSpecBuilder.js';
53
53
  import { QueryBuilderAggregateOperator_Wavg } from '../aggregation/operators/QueryBuilderAggregateOperator_Wavg.js';
54
54
  import { appendResultSetModifier } from '../result-modifier/ResultModifierValueSpecificationBuilder.js';
55
+ import { buildRelationAggregation } from '../aggregation/QueryBuilderRelationAggregationValueSpecBuilder.js';
55
56
 
56
57
  const buildProjectColFunc = (
57
58
  tdsState: QueryBuilderTDSState,
@@ -120,7 +121,10 @@ export const appendProjection = (
120
121
  `Can't build projection expression: preceding expression is not defined`,
121
122
  );
122
123
  // build projection
123
- if (tdsState.aggregationState.columns.length) {
124
+ if (
125
+ tdsState.aggregationState.columns.length &&
126
+ !tdsState.queryBuilderState.isFetchStructureTyped
127
+ ) {
124
128
  // aggregation
125
129
  const groupByFunction = new SimpleFunctionExpression(
126
130
  extractElementNameFromPath(
@@ -397,7 +401,11 @@ export const appendProjection = (
397
401
  tdsState,
398
402
  options,
399
403
  );
400
- lambdaFunction.expressionSequence[0] = projectFunction;
404
+ const aggregationFunction = tdsState.aggregationState.columns.length
405
+ ? buildRelationAggregation(projectFunction, tdsState)
406
+ : null;
407
+ lambdaFunction.expressionSequence[0] =
408
+ aggregationFunction ?? projectFunction;
401
409
  }
402
410
  }
403
411
  // build olapGroupBy
@@ -89,15 +89,14 @@ export const processTypedTDSProjectExpression = (
89
89
  export const isTypedProjectionExpression = (
90
90
  expression: SimpleFunctionExpression,
91
91
  ): boolean => {
92
- if (
93
- matchFunctionName(expression.functionName, [
92
+ return (
93
+ expression.functionName ===
94
+ QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_PROJECT ||
95
+ (matchFunctionName(expression.functionName, [
94
96
  QUERY_BUILDER_SUPPORTED_FUNCTIONS.TDS_PROJECT,
95
97
  QUERY_BUILDER_SUPPORTED_FUNCTIONS.RELATION_PROJECT,
96
- ])
97
- ) {
98
- if (expression.parametersValues.length === 2) {
99
- return expression.parametersValues[1] instanceof ColSpecArrayInstance;
100
- }
101
- }
102
- return false;
98
+ ]) &&
99
+ expression.parametersValues.length === 2 &&
100
+ expression.parametersValues[1] instanceof ColSpecArrayInstance)
101
+ );
103
102
  };
package/tsconfig.json CHANGED
@@ -129,6 +129,8 @@
129
129
  "./src/stores/fetch-structure/tds/aggregation/QueryBuilderAggregateOperatorLoader.ts",
130
130
  "./src/stores/fetch-structure/tds/aggregation/QueryBuilderAggregationState.ts",
131
131
  "./src/stores/fetch-structure/tds/aggregation/QueryBuilderAggregationStateBuilder.ts",
132
+ "./src/stores/fetch-structure/tds/aggregation/QueryBuilderRelationAggregationValueSpecBuilder.ts",
133
+ "./src/stores/fetch-structure/tds/aggregation/QueryBuilderTypedAggregationStateBuilder.ts",
132
134
  "./src/stores/fetch-structure/tds/aggregation/operators/QueryBuilderAggregateOperatorValueSpecificationBuilder.ts",
133
135
  "./src/stores/fetch-structure/tds/aggregation/operators/QueryBuilderAggregateOperator_Average.ts",
134
136
  "./src/stores/fetch-structure/tds/aggregation/operators/QueryBuilderAggregateOperator_Count.ts",
@@ -162,7 +164,7 @@
162
164
  "./src/stores/fetch-structure/tds/projection/QueryBuilderProjectionColumnState.ts",
163
165
  "./src/stores/fetch-structure/tds/projection/QueryBuilderProjectionStateBuilder.ts",
164
166
  "./src/stores/fetch-structure/tds/projection/QueryBuilderProjectionValueSpecificationBuilder.ts",
165
- "./src/stores/fetch-structure/tds/projection/QueryBuilderRelationProjectValueSpecBuidler.ts",
167
+ "./src/stores/fetch-structure/tds/projection/QueryBuilderRelationProjectValueSpecBuilder.ts",
166
168
  "./src/stores/fetch-structure/tds/projection/QueryBuilderTypedProjectionStateBuilder.ts",
167
169
  "./src/stores/fetch-structure/tds/result-modifier/ResultModifierValueSpecificationBuilder.ts",
168
170
  "./src/stores/fetch-structure/tds/window/QueryBuilderWindowGroupByOperatorLoader.ts",