-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathplan-builder-base.js
More file actions
687 lines (674 loc) · 23.8 KB
/
plan-builder-base.js
File metadata and controls
687 lines (674 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
* Copyright (c) 2015-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
'use strict';
const types = require('./server-types-generated.js');
function checkMinArity(funcName, argsLen, minArity) {
if (argsLen < minArity) {
throw new Error(
`${funcName} takes a minimum of ${minArity} arguments but received: ${argsLen}`
);
}
}
function checkMaxArity(funcName, argsLen, maxArity) {
if (argsLen > maxArity) {
throw new Error(
`${funcName} takes a maximum of ${maxArity} arguments but received: ${argsLen}`
);
}
}
function checkArity(funcName, argsLen, minArity, maxArity) {
checkMinArity(funcName, argsLen, minArity);
checkMaxArity(funcName, argsLen, maxArity);
}
function checkArg(arg, funcName, argPos, paramName, paramTypes, isRequired, isMultiple) {
if (arg === void 0) {
if (isRequired) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} is a required ${typeLabel(paramTypes)} value`
);
}
return null;
} else if (Array.isArray(arg)) {
if (!isMultiple) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} must be one ${typeLabel(paramTypes)} value instead of an array`
);
} else if (arg.length === 0 && isRequired) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} array must have at least one ${typeLabel(paramTypes)} value`
);
}
return arg.map(val => castArg(val, funcName, paramName, argPos, paramTypes));
}
const val = castArg(arg, funcName, paramName, argPos, paramTypes);
const result = isMultiple ? [val] : val;
return result;
}
function castArg(arg, funcName, paramName, argPos, paramTypes) {
if (arg === void 0 || arg === null || !Array.isArray(paramTypes) || paramTypes.length === 0) {
return arg;
} else if (arg instanceof Object) {
if (paramTypes.some(paramType => (arg instanceof paramType))) {
return arg;
} else if (arg instanceof Number || arg instanceof Boolean || arg instanceof String) {
arg = arg.valueOf();
} else if (arg instanceof types.ServerType) {
// We added new VecVector ServerType which is not a sub-type of Item. This makes it a one-off type
// as paramTypes will not include VecVector in any other type checks.
// And if we get here the arg must be and only be a VecVector (arg._ns === 'vec') or we throw an Error
if(arg._ns === 'vec'){
return arg;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} must have type ${typeLabel(paramTypes)}`
);
} else if (paramTypes.some(paramType => {
const paramClass = paramType.name;
switch(paramClass) {
case 'PlanCtsReferenceMap': return Object.keys(arg).every(key => {
const value = arg[key];
if (value instanceof types.CtsReference) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} has ${key} key without a CtsReference value for ${typeLabel(paramTypes)}`
);
});
case 'PlanGroupConcatOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'separator':
if (typeof value === 'string' || value instanceof String) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} separator must be a string for ${typeLabel(paramTypes)} options`
);
case 'values':
if (value === 'distinct') {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} values can only be "distinct" for ${typeLabel(paramTypes)} options`
);
default:
return false;
}});
case 'PlanSampleByOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'limit':
if (typeof value === 'number' || value instanceof Number || typeof value === 'string' || value instanceof String) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} limit must be a number or string for ${typeLabel(paramTypes)} options`
);
default:
return false;
}});
case 'PlanSearchOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'scoreMethod':
if (['logtfidf', 'logtf', 'simple', 'bm25', 'zero', 'random'].includes(value)) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} can only be 'logtfidf', 'logtf', 'simple', 'bm25', 'zero' or 'random'`
);
case 'qualityWeight':
if (typeof value === 'number' || value instanceof Number) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} must be a number`
);
case 'bm25LengthWeight':
if (typeof value === 'number' || value instanceof Number) {
return true;
}
throw new Error(
'bm25LengthWeight must be a number'
);
default:
return false;
}});
case 'PlanSparqlOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'dedup':
switch(value) {
case 'on':
case 'off':
return true;
default:
throw new Error(
`${argLabel(funcName, paramName, argPos)} values for dedup can only be "on" or "off" for ${typeLabel(paramTypes)} options`
);
}
break;
case 'base':
if (typeof value === 'string' || value instanceof String) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} base must be a URI string for ${typeLabel(paramTypes)} options`
);
default:
return false;
}});
case 'PlanTripleOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'dedup':
switch(value) {
case 'on':
case 'off':
return true;
default:
throw new Error(
`${argLabel(funcName, paramName, argPos)} values for dedup can only be "on" or "off" for ${typeLabel(paramTypes)} options`
);
}
break;
default:
return false;
}});
case 'PlanValueOption': return Object.keys(arg).every(key => {
const value = arg[key];
switch(key) {
case 'values':
if (value === 'distinct') {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} values can only be "distinct" for ${typeLabel(paramTypes)} options`
);
default:
return false;
}});
case 'PlanXsValueMap':
const keys = Object.keys(arg);
const columns = (keys.length === 2) ? arg.columnNames : null;
const rows = Array.isArray(columns) ? arg.rowValues : null;
if (Array.isArray(rows)) {
if (columns.every(column => (typeof column === 'string' || column instanceof String))) {
if (rows.every(row => (Array.isArray(row) && row.length <= columns.length))) {
return true;
}
}
return false;
}
return keys.every(key => {
const value = arg[key];
const valtype = typeof value;
switch (valtype) {
case 'boolean': return true;
case 'number': return true;
case 'string': return true;
case 'object':
if (value === null || value instanceof String || value instanceof Number || value instanceof Boolean) {
return true;
} else if (value instanceof types.XsAnyAtomicType) {
const valArgs = value._args;
if (Array.isArray(valArgs) && valArgs.every(valArg => !(valArg instanceof types.ServerType))) {
return true;
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} has ${key} key with expression value`
);
}
break;
default:
throw new Error(
`${argLabel(funcName, paramName, argPos)} has ${key} key with ${valtype} instead of ${typeLabel(paramTypes)} literal value`
);
}});
case 'PlanDocDescriptor':
const expectedKeys = ['uri', 'doc', 'collections', 'metadata', 'permissions', 'quality', 'temporalCollection'];
const objKeysDocDescriptor = Object.keys(arg);
if(!objKeysDocDescriptor.length) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - called with objects without document column keys such as "uri" for {}`
);
}
return objKeysDocDescriptor.every(key => {
const value = arg[key];
const type = typeof value;
if(!expectedKeys.includes(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - no typechecking info available for: ${key}`
);
} else {
if(key === 'uri' || key === 'temporalCollection') {
if(type !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - ${key} key has type ${type} instead of 'string'`
);
}
return true;
}
if(key === 'doc') {
return true;
}
if(key === 'collections' || key === 'permissions') {
if(!Array.isArray(value)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - ${key} key must be array`
);
}
if(key === 'permissions'){
const permissionValue = [];
for(let i=0; i<value.length; i++){
if(value[i].hasOwnProperty('role-name')){
for(let j=0; (value[i].capabilities && j<value[i].capabilities.length); j++){
const tempValue = {};
tempValue.roleName=value[i]['role-name'];
tempValue.capability = value[i].capabilities[j];
permissionValue.push(tempValue);
}
}
}
arg.permissions = permissionValue;
}
return true;
}
if(key === 'metadata') {
if (type !== 'object') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - ${key} key must be array or json object`
);
}
return true;
}
if(key === 'quality') {
if(typeof value !== 'number') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - ${key} key must be type of number`
);
}
return true;
}
}
});
case 'PlanDocColsIdentifier':
const expectedWriteKeys = ['uri', 'doc', 'collections', 'metadata', 'permissions', 'quality', 'temporalCollection'];
const objKeysDocCols = Object.keys(arg);
return objKeysDocCols.every(key => {
const value = arg[key];
const type = typeof value;
if(!expectedWriteKeys.includes(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - unknown document descriptor found: ${key}`
);
} else {
if(type !== 'string' && type !== 'object') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} - invalid type: ${type} for: ${key}`
);
}
return true;
}
});
case 'PlanSchemaDef':
const objKeysPlanSchemaDef = Object.keys(arg);
if(!objKeysPlanSchemaDef.includes('kind')) {
return false;
}
return objKeysPlanSchemaDef.every(key => {
if(key === 'kind') {
if(typeof arg[key] !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has another type than string`
);
} else {
return true;
}
}
if(key === 'mode') {
if(typeof arg[key] !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has another type than string`
);
}
return true;
}
if(key === 'schemaUri') {
if(typeof arg[key] !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has another type than string`
);
}
return true;
}
});
case 'PlanTransformDef':
return Object.keys(arg).every(key => {
const value = arg[key];
const type = typeof value;
if(key === 'path') {
if(type !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} should be a type of string`
);
}
return true;
}
if(key === 'kind') {
if(type !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} should be a type of string`
);
}
return true;
}
if(key === 'params') {
if(type !== 'object') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} should be a type of object`
);
}
}
return true;
});
case 'PlanRowColTypes':
const objKeys = Object.keys(arg);
if(!objKeys.includes('column')) {
return false;
}
objKeys.every(key => {
if(key === 'column') {
if(typeof arg[key] !== 'string') {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has another type than string`
);
}
}
});
return true;
case 'PlanAnnTopKOptions':
const planAnnTopKOptionsSet = new Set(['maxDistance', 'max-distance', 'searchFactor','search-factor']);
if(Object.getPrototypeOf(arg) === Map.prototype){
arg.forEach((value, key) => {
if(!planAnnTopKOptionsSet.has(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has invalid key- ${key}`
);
}
});
}
return true;
case 'PlanTransitiveClosureOptions':
const planTransitiveClosureOptionsSet = new Set(['minLength', 'min-length', 'maxLength', 'max-length']);
if(Object.getPrototypeOf(arg) === Map.prototype){
arg.forEach((value, key) => {
if(!planTransitiveClosureOptionsSet.has(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has invalid key- ${key}`
);
}
});
} else if (typeof arg === 'object') {
Object.keys(arg).forEach(key => {
if(!planTransitiveClosureOptionsSet.has(key)) {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has invalid key- ${key}`
);
}
});
}
return true;
default:
return false;
}
})) {
return arg;
} else {
throw new Error(
`${argLabel(funcName, paramName, argPos)} has invalid argument for ${typeLabel(paramTypes)} value: ${arg}`
);
}
}
const argType = typeof arg;
switch(argType) {
case 'boolean':
if (isProtoChained(paramTypes, [types.XsBoolean, types.BooleanNode, types.JsonContentNode])) {
return arg;
}
break;
case 'number':
if (isProtoChained(paramTypes,
[types.XsDecimal, types.XsDouble, types.XsFloat, types.XsNumeric, types.NumberNode, types.JsonContentNode])) {
return arg;
}
break;
case 'string':
if (isProtoChained(paramTypes, [types.XsAnyAtomicType, types.TextNode, types.JsonContentNode, types.XmlContentNode,
types.XsString, types.ServerType])) {
return arg;
}
break;
default:
throw new Error(
`${argLabel(funcName, paramName, argPos)} must be a ${typeLabel(paramTypes)} instead of ${argType} value`
);
}
throw new Error(
`${argLabel(funcName, paramName, argPos)} must be a ${typeLabel(paramTypes)} value`
);
}
function isProtoChained(declaredTypes, valueTypes) {
return valueTypes.some(valueType => {
const valueProto = valueType.prototype;
return declaredTypes.some(declaredType => {
const declaredProto = declaredType.prototype;
return (
declaredProto === valueProto ||
declaredProto.isPrototypeOf(valueProto) ||
valueProto.isPrototypeOf(declaredProto)
);
});
});
}
function argLabel(funcName, paramName, argPos) {
return `${paramName} argument at ${argPos} of ${funcName}()`;
}
function typeLabel(paramTypes) {
// vulnerable if a type ever has a name property
return paramTypes.map(paramType => paramType.name).join(' or ');
}
function getNamer(args, name) {
const onlyArg = getOptionalArg(args);
const namer = (onlyArg === null || onlyArg[name] === void 0) ? null : onlyArg;
return namer;
}
function getOptionalArg(args) {
const firstArg = (args.length === 1) ? args[0] : void 0;
const onlyArg = (firstArg !== null && !Array.isArray(firstArg) && typeof firstArg === 'object') ?
firstArg : null;
return onlyArg;
}
function makePositionalArgs(funcName, minArity, isUnbounded, paramDefs, args) {
const paramLen = paramDefs.length;
if (isUnbounded) {
checkMinArity(funcName, args.length, minArity);
} else {
checkArity(funcName, args.length, minArity, paramLen);
}
return args.map((arg, i) => {
const paramNum = (isUnbounded && i >= paramLen) ? (paramLen - 1) : i;
const paramDef = paramDefs[paramNum];
return checkArg(arg, funcName, i, paramDef[0], paramDef[1], paramDef[2], paramDef[3]);
});
}
function makeNamedArgs(namer, funcName, minArity, paramNames, paramDefs, args) {
Object.keys(namer).forEach(name => {
if (!paramNames.has(name)) {
throw new Error(`${name} is not a named parameter of ${funcName}`);
}
});
args = [];
let paramDef = null;
let i = 0;
let j = -1;
for (const paramName of paramNames) {
const val = namer[paramName];
if (val !== void 0) {
paramDef = paramDefs[++j];
while (j < i) {
args.push(checkArg(null, funcName, j, paramDef[0], paramDef[1], paramDef[2], paramDef[3]));
paramDef = paramDefs[++j];
}
args.push(checkArg(val, funcName, i, paramName, paramDef[1], paramDef[2], paramDef[3]));
}
i++;
}
while (++j < minArity) {
paramDef = paramDefs[j];
args.push(checkArg(null, funcName, j, paramDef[0], paramDef[1], paramDef[2], paramDef[3]));
}
checkArity(funcName, args.length, minArity, paramNames.length);
return args;
}
function makeSingleArgs(funcName, minArity, paramDef, args) {
const argLen = args.length;
checkArity(funcName, argLen, minArity, 1);
if (argLen === 1) {
const paramName = paramDef[0];
const namer = getNamer(args, paramName);
if (namer === null || namer instanceof types.ServerType) {
args[0] = checkArg(args[0], funcName, 0, paramName, paramDef[1], paramDef[2], paramDef[3]);
} else if (Object.keys(namer).length > 1) {
throw new Error(`named parameter object has keys other than ${paramName} for ${funcName}`);
} else {
args[0] = checkArg(namer[paramName], funcName, 0, paramName, paramDef[1], paramDef[2], paramDef[3]);
}
}
return args;
}
function exportOperators(plan) {
const operList = plan._operators;
if (!Array.isArray(operList)) {
throw new Error('operator list is not an array: '+operList);
}
return {
ns: 'op',
fn: 'operators',
args: operList.map(oper => exportOperator(oper))
};
}
function exportOperator(oper) {
const ns = oper._ns;
const fn = oper._fn;
const args = oper._args;
if (ns !== 'op' || fn === void 0 || !Array.isArray(args)) {
throw new Error(`cannot export invalid operator: ${oper}`);
}
let exportedArgs = null;
switch(fn) {
case 'from-literals':
exportedArgs = args.map((arg, i) => {
if (i === 0) {
const lit = (!Array.isArray(arg)) ? arg : (arg.length ===1) ? arg[0] : void 0;
if (lit === void 0) {
return exportArgs(arg);
} else {
const columnNames = lit.columnNames;
const rowValues = lit.rowValues;
if (columnNames === void 0 || rowValues === void 0) {
return [exportArg(lit)];
} else if (Array.isArray(columnNames) && Array.isArray(rowValues)) {
return rowValues.map((rowIn) => {
if (!Array.isArray(rowIn)) {
throw new Error(`literal row is not array: ${rowIn}`);
}
const rowOut = {};
rowIn.forEach((value, j) => {
if (j > columnNames.length) {
throw new Error(`more values than column names: ${rowIn}`);
}
rowOut[columnNames[j]] = value;
});
return rowOut;
});
} else {
throw new Error(`no literals: ${oper}`);
}
}
} else {
return exportArg(arg);
}
});
break;
case 'join-inner':
case 'join-left-outer':
case 'join-cross-product':
case 'except':
case 'intersect':
case 'union':
exportedArgs = args.map((arg, i) => {
if (i === 0) {
return exportOperators(arg);
}
return exportArg(arg);
});
break;
default:
exportedArgs = exportArgs(args);
break;
}
return {ns:ns, fn:fn, args:exportedArgs};
}
function exportObject(obj) {
const ns = obj._ns;
const fn = obj._fn;
const args = obj._args;
if (ns !== void 0 && fn !== void 0 && Array.isArray(args)) {
return {
ns: ns,
fn: fn,
args: exportArgs(args)
};
}
return Object.getOwnPropertyNames(obj).reduce(
(objCopy, key) => {objCopy[key] = exportArg(obj[key]); return objCopy;},
{}
);
}
function exportArgs(argList) {
if (argList === void 0 || argList === null || argList.length === 0 ||
(argList.length === 1 && argList[0] === null)
) {
return [];
}
return argList.map(arg => {
return exportArg(arg);
});
}
function exportArg(arg) {
if (arg === null || typeof arg !== 'object' ||
arg instanceof String || arg instanceof Number || arg instanceof Boolean
) {
return arg;
} else if (!Array.isArray(arg)) {
return exportObject(arg);
} else if (arg.length === 1) {
return exportArg(arg[0]);
}
return exportArgs(arg);
}
function doExport(plan) {
return {$optic:exportOperators(plan)};
}
module.exports = {
checkArity: checkArity,
checkMaxArity: checkMaxArity,
exportArg: exportArg,
getNamer: getNamer,
makeNamedArgs: makeNamedArgs,
makePositionalArgs: makePositionalArgs,
makeSingleArgs: makeSingleArgs,
doExport: doExport
};