@m6d/cortex 0.2.0 → 0.4.0
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 +6 -2
- package/dist/drizzle.d.ts +461 -5
- package/dist/drizzle.js +172 -44
- package/dist/{index-z0594rs3.js → index-jw8cnjqv.js} +159 -124
- package/dist/index.d.ts +1592 -240
- package/dist/index.js +352 -179
- package/package.json +2 -14
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// src/cortex-error.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { bodyLimit } from "hono/body-limit";
|
|
4
|
+
|
|
1
5
|
// ../../internal/contracts/src/errors/index.ts
|
|
2
6
|
import { z } from "zod";
|
|
3
7
|
var BACKEND_ERROR_KINDS = [
|
|
@@ -21,6 +25,11 @@ var backendErrorSchema = z.object({
|
|
|
21
25
|
detail: z.string().max(BACKEND_ERROR_DETAIL_MAX)
|
|
22
26
|
})
|
|
23
27
|
});
|
|
28
|
+
function errorEnvelope(kind, detail) {
|
|
29
|
+
return {
|
|
30
|
+
error: { kind, detail: detail.slice(0, BACKEND_ERROR_DETAIL_MAX) }
|
|
31
|
+
};
|
|
32
|
+
}
|
|
24
33
|
|
|
25
34
|
// src/cortex-error.ts
|
|
26
35
|
class CortexError extends Error {
|
|
@@ -31,11 +40,7 @@ class CortexError extends Error {
|
|
|
31
40
|
this.name = "CortexError";
|
|
32
41
|
}
|
|
33
42
|
}
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
error: { kind, detail: detail.slice(0, BACKEND_ERROR_DETAIL_MAX) }
|
|
37
|
-
};
|
|
38
|
-
}
|
|
43
|
+
var malformedBody = errorEnvelope("invalid_request", "Malformed request body");
|
|
39
44
|
function envelopeOf(error) {
|
|
40
45
|
if (error instanceof CortexError)
|
|
41
46
|
return errorEnvelope(error.kind, error.message);
|
|
@@ -44,6 +49,15 @@ function envelopeOf(error) {
|
|
|
44
49
|
}
|
|
45
50
|
return errorEnvelope("internal", "The call failed");
|
|
46
51
|
}
|
|
52
|
+
function protocolBodyLimit(maxSize) {
|
|
53
|
+
return bodyLimit({
|
|
54
|
+
maxSize,
|
|
55
|
+
onError: (c) => c.json(errorEnvelope("invalid_request", "Request body too large"), 413)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function contentVersion(value) {
|
|
59
|
+
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
60
|
+
}
|
|
47
61
|
|
|
48
62
|
// ../../internal/contracts/src/datasets/datasets.ts
|
|
49
63
|
import { z as z2 } from "zod";
|
|
@@ -55,6 +69,33 @@ function isRecord(value) {
|
|
|
55
69
|
function unescapePointerToken(token) {
|
|
56
70
|
return token.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
57
71
|
}
|
|
72
|
+
function pointerTokens(pointer) {
|
|
73
|
+
let path = pointer;
|
|
74
|
+
if (pointer.startsWith("#")) {
|
|
75
|
+
try {
|
|
76
|
+
path = decodeURIComponent(pointer.slice(1));
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (path === "")
|
|
82
|
+
return [];
|
|
83
|
+
if (!path.startsWith("/"))
|
|
84
|
+
return;
|
|
85
|
+
return path.slice(1).split("/").map(unescapePointerToken);
|
|
86
|
+
}
|
|
87
|
+
var ARRAY_INDEX = /^(?:0|[1-9]\d*)$/;
|
|
88
|
+
var JSON_NUMBER_PATTERN = /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?$/;
|
|
89
|
+
function pointerChild(container, token) {
|
|
90
|
+
if (Array.isArray(container)) {
|
|
91
|
+
const items = container;
|
|
92
|
+
return ARRAY_INDEX.test(token) ? items[Number(token)] : undefined;
|
|
93
|
+
}
|
|
94
|
+
return isRecord(container) && Object.hasOwn(container, token) ? container[token] : undefined;
|
|
95
|
+
}
|
|
96
|
+
function readJsonPointer(document, pointer) {
|
|
97
|
+
return pointerTokens(pointer)?.reduce(pointerChild, document);
|
|
98
|
+
}
|
|
58
99
|
|
|
59
100
|
// ../../internal/contracts/src/datasets/datasets.ts
|
|
60
101
|
var DATASET_IDENTIFIER_PATTERN = /^[a-zA-Z][a-zA-Z0-9_]*$/;
|
|
@@ -63,10 +104,22 @@ var DATASET_COLUMN_REF_PATTERN = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*
|
|
|
63
104
|
var datasetIdentifierSchema = z2.string().regex(DATASET_IDENTIFIER_PATTERN).max(DATASET_IDENTIFIER_MAX);
|
|
64
105
|
var datasetColumnRefSchema = z2.string().regex(DATASET_COLUMN_REF_PATTERN).max(256);
|
|
65
106
|
var DATASET_COLUMN_TYPES = ["string", "number", "boolean", "date"];
|
|
107
|
+
var DATASET_DEFINITION_LIMITS = {
|
|
108
|
+
description: 500,
|
|
109
|
+
columns: 200,
|
|
110
|
+
columnDescription: 200,
|
|
111
|
+
values: 100,
|
|
112
|
+
valueLength: 200,
|
|
113
|
+
maxRows: 5000
|
|
114
|
+
};
|
|
66
115
|
var datasetColumnSchema = z2.object({
|
|
67
116
|
name: datasetIdentifierSchema,
|
|
68
117
|
type: z2.enum(DATASET_COLUMN_TYPES),
|
|
69
|
-
description: z2.string().max(
|
|
118
|
+
description: z2.string().max(DATASET_DEFINITION_LIMITS.columnDescription).default(""),
|
|
119
|
+
values: z2.array(z2.string().min(1).max(DATASET_DEFINITION_LIMITS.valueLength)).min(1).max(DATASET_DEFINITION_LIMITS.values).optional()
|
|
120
|
+
}).refine((column) => column.values === undefined || column.type === "string", {
|
|
121
|
+
message: "only a string column lists its values",
|
|
122
|
+
path: ["values"]
|
|
70
123
|
});
|
|
71
124
|
var datasetRelationSchema = z2.object({
|
|
72
125
|
dataset: datasetIdentifierSchema,
|
|
@@ -79,10 +132,10 @@ var datasetRelationSchema = z2.object({
|
|
|
79
132
|
var DATASET_MAX_ROWS_DEFAULT = 500;
|
|
80
133
|
var datasetDefinitionSchema = z2.object({
|
|
81
134
|
name: datasetIdentifierSchema,
|
|
82
|
-
description: z2.string().max(
|
|
83
|
-
columns: z2.array(datasetColumnSchema).min(1).max(
|
|
135
|
+
description: z2.string().max(DATASET_DEFINITION_LIMITS.description),
|
|
136
|
+
columns: z2.array(datasetColumnSchema).min(1).max(DATASET_DEFINITION_LIMITS.columns),
|
|
84
137
|
relations: z2.record(datasetIdentifierSchema, datasetRelationSchema).default({}),
|
|
85
|
-
maxRows: z2.number().int().min(1).max(
|
|
138
|
+
maxRows: z2.number().int().min(1).max(DATASET_DEFINITION_LIMITS.maxRows).default(DATASET_MAX_ROWS_DEFAULT)
|
|
86
139
|
});
|
|
87
140
|
var datasetManifestSchema = z2.object({
|
|
88
141
|
version: z2.string().min(1).max(128),
|
|
@@ -162,11 +215,12 @@ var datasetConditionSchema = z2.object({
|
|
|
162
215
|
z2.object({ col: datasetOperandSchema })
|
|
163
216
|
]).optional()
|
|
164
217
|
});
|
|
218
|
+
var notACondition = { col: z2.never().optional() };
|
|
165
219
|
var datasetFilterSchema = z2.lazy(() => z2.union([
|
|
166
220
|
datasetConditionSchema,
|
|
167
|
-
z2.object({ any: z2.array(datasetFilterSchema).min(1).max(20) }),
|
|
168
|
-
z2.object({ all: z2.array(datasetFilterSchema).min(1).max(20) }),
|
|
169
|
-
z2.object({ not: datasetFilterSchema })
|
|
221
|
+
z2.object({ any: z2.array(datasetFilterSchema).min(1).max(20), ...notACondition }),
|
|
222
|
+
z2.object({ all: z2.array(datasetFilterSchema).min(1).max(20), ...notACondition }),
|
|
223
|
+
z2.object({ not: datasetFilterSchema, ...notACondition })
|
|
170
224
|
]));
|
|
171
225
|
var datasetGroupBySchema = z2.union([datasetColumnRefSchema, datasetNamedOperandSchema]);
|
|
172
226
|
var datasetSortSchema = z2.object({
|
|
@@ -198,10 +252,12 @@ var datasetQueryObjectSchema = z2.object({
|
|
|
198
252
|
limit: z2.number().int().min(1).max(100)
|
|
199
253
|
}).optional(),
|
|
200
254
|
sort: z2.array(datasetSortSchema).max(5).default([]).describe("By a column or select alias, a group key, an aggregate alias or a computed alias."),
|
|
201
|
-
limit: z2.
|
|
202
|
-
offset: z2.
|
|
255
|
+
limit: z2.int32().min(1).optional(),
|
|
256
|
+
offset: z2.int32().min(0).optional()
|
|
203
257
|
});
|
|
204
258
|
var DATASET_QUERY_MAX_DEPTH = 32;
|
|
259
|
+
var DATASET_REQUEST_MAX_BYTES = 256 * 1024;
|
|
260
|
+
var DATASET_COMPUTE_REFERENCES_MAX = 100;
|
|
205
261
|
function nestsWithin(value, budget) {
|
|
206
262
|
if (typeof value !== "object" || value === null)
|
|
207
263
|
return true;
|
|
@@ -212,7 +268,8 @@ var datasetRequestSchema = z2.object({
|
|
|
212
268
|
query: datasetQuerySchema,
|
|
213
269
|
context: z2.object({
|
|
214
270
|
threadId: z2.string().max(128),
|
|
215
|
-
requestId: z2.string().max(128).optional()
|
|
271
|
+
requestId: z2.string().max(128).optional(),
|
|
272
|
+
timeZone: z2.string().max(64).optional()
|
|
216
273
|
})
|
|
217
274
|
});
|
|
218
275
|
var datasetResultSchema = z2.object({
|
|
@@ -248,9 +305,31 @@ function resolveDatasetColumn(catalog, from, ref) {
|
|
|
248
305
|
}
|
|
249
306
|
const column = dataset.columns.find((candidate) => candidate.name === columnName);
|
|
250
307
|
if (!column)
|
|
251
|
-
return missing(
|
|
308
|
+
return missing(unknownColumnError(dataset, columnName));
|
|
252
309
|
return { ok: true, column, path: segments.join(".") };
|
|
253
310
|
}
|
|
311
|
+
function unknownColumnError(dataset, name) {
|
|
312
|
+
const columns = dataset.columns.map((column) => column.name);
|
|
313
|
+
const wanted = name.toLowerCase();
|
|
314
|
+
const containing = columns.filter((column) => {
|
|
315
|
+
const lower = column.toLowerCase();
|
|
316
|
+
return wanted.length > 2 && lower.includes(wanted) || lower.length > 2 && wanted.includes(lower);
|
|
317
|
+
});
|
|
318
|
+
const closest = (containing.length > 0 ? containing : columns.filter((column) => Math.abs(column.length - wanted.length) <= 2 && editDistance(column.toLowerCase(), wanted) <= 2)).slice(0, 3);
|
|
319
|
+
const hint = closest.length > 0 ? ` Closest: ${closest.join(", ")}.` : "";
|
|
320
|
+
return `${dataset.name} has no column "${name}".${hint} Columns: ${columns.join(", ")}.`;
|
|
321
|
+
}
|
|
322
|
+
function editDistance(a, b) {
|
|
323
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
324
|
+
for (let i = 1;i <= a.length; i++) {
|
|
325
|
+
const current = [i];
|
|
326
|
+
for (let j = 1;j <= b.length; j++) {
|
|
327
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
328
|
+
}
|
|
329
|
+
previous = current;
|
|
330
|
+
}
|
|
331
|
+
return previous[b.length];
|
|
332
|
+
}
|
|
254
333
|
function refError(kind, ref, reason) {
|
|
255
334
|
return `${kind} "${ref}": ${reason}`;
|
|
256
335
|
}
|
|
@@ -449,6 +528,14 @@ function validateDatasetQuery(catalog, query) {
|
|
|
449
528
|
};
|
|
450
529
|
for (const filter of query.filter)
|
|
451
530
|
checkFilter(check, filter);
|
|
531
|
+
const row = checkAnsweredRow(check, catalog, query);
|
|
532
|
+
checkFinishedRow(check, query, row);
|
|
533
|
+
if (computeReferences(query) > DATASET_COMPUTE_REFERENCES_MAX) {
|
|
534
|
+
check.issue(`computed aliases are named more than ${DATASET_COMPUTE_REFERENCES_MAX} times once each is written out where it is named; name earlier computed aliases fewer times`);
|
|
535
|
+
}
|
|
536
|
+
return issues;
|
|
537
|
+
}
|
|
538
|
+
function checkAnsweredRow(check, catalog, query) {
|
|
452
539
|
const groupKeys = checkGroupBy(check, query.groupBy);
|
|
453
540
|
const aliases = checkAggregates(check, query.aggregate);
|
|
454
541
|
for (const alias of aliases.keys()) {
|
|
@@ -465,111 +552,34 @@ function validateDatasetQuery(catalog, query) {
|
|
|
465
552
|
check.issue("a grouped query answers the group keys, the aggregate aliases and the computed aliases; put computed values in aggregate or compute and leave select out");
|
|
466
553
|
}
|
|
467
554
|
const selectAliases = checkSelect(check, query.select ?? []);
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
555
|
+
return grouped ? {
|
|
556
|
+
names: new Map([...groupKeys, ...aliases]),
|
|
557
|
+
kind: "grouped",
|
|
558
|
+
columns: passedColumns(catalog, query, query.groupBy)
|
|
559
|
+
} : {
|
|
560
|
+
names: selectAliases,
|
|
561
|
+
kind: "plain",
|
|
562
|
+
columns: passedColumns(catalog, query, query.select ?? [])
|
|
563
|
+
};
|
|
472
564
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
const aggregates = query.aggregate.map((entry) => {
|
|
481
|
-
const parts = [
|
|
482
|
-
...entry.col === undefined ? [] : [operandCost(entry.col, source)],
|
|
483
|
-
...entry.filter.map((filter) => filterCost(filter, source))
|
|
484
|
-
];
|
|
485
|
-
costs.push(...parts);
|
|
486
|
-
expanded.set(entry.as, nodeCost(parts));
|
|
487
|
-
return nodeCost(parts);
|
|
488
|
-
});
|
|
489
|
-
const grouped = query.groupBy.length > 0 || query.aggregate.length > 0;
|
|
490
|
-
const projected = grouped ? query.groupBy : query.select ?? [];
|
|
491
|
-
const carried = Math.min(DATASET_EXPANSION_BUDGET + 1, [
|
|
492
|
-
...where,
|
|
493
|
-
...projected.map((entry) => typeof entry === "string" ? 1 : expanded.get(entry.as) ?? 1),
|
|
494
|
-
...grouped || projected.length ? [] : Array(columnCount).fill(1),
|
|
495
|
-
...aggregates
|
|
496
|
-
].reduce((total, cost) => total + cost, 0));
|
|
497
|
-
const windows = query.window.map((entry) => {
|
|
498
|
-
const total = nodeCost([
|
|
499
|
-
...entry.col === undefined ? [] : [operandCost(entry.col, expanded)],
|
|
500
|
-
...entry.filter.map((filter) => filterCost(filter, expanded)),
|
|
501
|
-
...entry.partitionBy.map((name) => operandCost(name, expanded)),
|
|
502
|
-
carried
|
|
503
|
-
]);
|
|
504
|
-
costs.push(total);
|
|
505
|
-
return [entry.as, total];
|
|
506
|
-
});
|
|
507
|
-
for (const [as, total] of windows)
|
|
508
|
-
expanded.set(as, total);
|
|
565
|
+
function computeReferences(query) {
|
|
566
|
+
const written = new Map;
|
|
567
|
+
const count = (refs) => Math.min(refs.reduce((sum, ref) => {
|
|
568
|
+
const inner = written.get(ref);
|
|
569
|
+
return inner === undefined ? sum : sum + 1 + inner;
|
|
570
|
+
}, 0), DATASET_COMPUTE_REFERENCES_MAX + 1);
|
|
571
|
+
let total = 0;
|
|
509
572
|
for (const entry of query.compute) {
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
}
|
|
514
|
-
for (const filter of query.having)
|
|
515
|
-
costs.push(filterCost(filter, expanded));
|
|
516
|
-
const qualifying = query.groupBy.length ? source : expanded;
|
|
517
|
-
for (const filter of query.qualify)
|
|
518
|
-
costs.push(filterCost(filter, qualifying));
|
|
519
|
-
if (costs.some((total) => total > DATASET_EXPANSION_BUDGET)) {
|
|
520
|
-
check.issue(`Computed aliases expand to more than ${DATASET_EXPANSION_BUDGET} references; name intermediate results and reuse them less`);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
var EXPANSION_WEIGHTS = {
|
|
524
|
-
divisor: 2,
|
|
525
|
-
roundDigits: 2,
|
|
526
|
-
lengthText: 2,
|
|
527
|
-
conditionLeft: 3,
|
|
528
|
-
conditionRight: 2,
|
|
529
|
-
dateTrunc: { day: 9, month: 6, quarter: 6, year: 3, week: 17 },
|
|
530
|
-
datePart: { day: 1, month: 1, quarter: 1, year: 1, dayOfWeek: 16, week: 17 }
|
|
531
|
-
};
|
|
532
|
-
function nodeCost(children) {
|
|
533
|
-
return Math.min(DATASET_EXPANSION_BUDGET + 1, children.reduce((total, cost) => total + cost, 1));
|
|
534
|
-
}
|
|
535
|
-
function argumentWeight(fn, index) {
|
|
536
|
-
if (fn === "length")
|
|
537
|
-
return EXPANSION_WEIGHTS.lengthText;
|
|
538
|
-
return fn === "round" && index === 1 ? EXPANSION_WEIGHTS.roundDigits : 1;
|
|
539
|
-
}
|
|
540
|
-
function operandCost(operand, expanded) {
|
|
541
|
-
if (typeof operand === "string")
|
|
542
|
-
return expanded.get(operand) ?? 1;
|
|
543
|
-
if (typeof operand === "number" || "value" in operand)
|
|
544
|
-
return 1;
|
|
545
|
-
if ("op" in operand) {
|
|
546
|
-
const divisor = operand.op === "/" ? EXPANSION_WEIGHTS.divisor : 1;
|
|
547
|
-
return nodeCost([
|
|
548
|
-
operandCost(operand.left, expanded),
|
|
549
|
-
operandCost(operand.right, expanded) * divisor
|
|
550
|
-
]);
|
|
573
|
+
const own = count(operandRefs(entry.expr));
|
|
574
|
+
written.set(entry.as, own);
|
|
575
|
+
total += own;
|
|
551
576
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
if ("args" in operand) {
|
|
559
|
-
return nodeCost(operand.args.map((arg, index) => operandCost(arg, expanded) * argumentWeight(operand.fn, index)));
|
|
560
|
-
}
|
|
561
|
-
const weight = operand.fn === "dateTrunc" ? EXPANSION_WEIGHTS.dateTrunc[operand.unit] : EXPANSION_WEIGHTS.datePart[operand.unit];
|
|
562
|
-
return nodeCost([operandCost(operand.of, expanded) * weight]);
|
|
563
|
-
}
|
|
564
|
-
function filterCost(filter, expanded) {
|
|
565
|
-
if ("not" in filter)
|
|
566
|
-
return nodeCost([filterCost(filter.not, expanded)]);
|
|
567
|
-
if ("any" in filter || "all" in filter) {
|
|
568
|
-
const entries = "any" in filter ? filter.any : filter.all;
|
|
569
|
-
return nodeCost(entries.map((entry) => filterCost(entry, expanded)));
|
|
570
|
-
}
|
|
571
|
-
const right = isColumnValue(filter.value) ? operandCost(filter.value.col, expanded) * EXPANSION_WEIGHTS.conditionRight : Array.isArray(filter.value) ? filter.value.length : filter.value === undefined ? 0 : 1;
|
|
572
|
-
return nodeCost([operandCost(filter.col, expanded) * EXPANSION_WEIGHTS.conditionLeft, right]);
|
|
577
|
+
const rowNames = [
|
|
578
|
+
...[...query.having, ...query.qualify].flatMap(filterRefs),
|
|
579
|
+
...[...query.sort, ...query.rank?.sort ?? []].map((entry) => entry.col),
|
|
580
|
+
...query.rank?.partitionBy ?? []
|
|
581
|
+
];
|
|
582
|
+
return total + count(rowNames);
|
|
573
583
|
}
|
|
574
584
|
function checkFinishedRow(check, query, row) {
|
|
575
585
|
const rowCheck = rowScope(check, row);
|
|
@@ -586,7 +596,7 @@ function rowScope(check, row) {
|
|
|
586
596
|
columnOf(kind, ref) {
|
|
587
597
|
const type = row.names.get(ref);
|
|
588
598
|
if (type)
|
|
589
|
-
return { name: ref, type, description: "" };
|
|
599
|
+
return row.columns.get(ref) ?? { name: ref, type, description: "" };
|
|
590
600
|
if (row.kind === "plain")
|
|
591
601
|
return check.columnOf(kind, ref);
|
|
592
602
|
check.issue(refError(kind, ref, `${kind} names a group key, an aggregate alias, a window alias or a computed alias`));
|
|
@@ -803,10 +813,23 @@ function checkFilter(check, filter, kind = "filter") {
|
|
|
803
813
|
check.issue(refError(kind, shown, `${filter.op} is not valid on a ${type} column`));
|
|
804
814
|
return;
|
|
805
815
|
}
|
|
806
|
-
const issue = isColumnValue(filter.value) ? columnValueIssue(check, kind, type, filter.op, filter.value.col) : filterValueIssue(filter) ?? filterValueTypeIssue(type, filter) ?? truncatedDateIssue(filter);
|
|
816
|
+
const issue = isColumnValue(filter.value) ? columnValueIssue(check, kind, type, filter.op, filter.value.col) : filterValueIssue(filter) ?? filterValueTypeIssue(type, filter) ?? truncatedDateIssue(filter) ?? listedValueIssue(check, kind, filter);
|
|
807
817
|
if (issue)
|
|
808
818
|
check.issue(refError(kind, shown, issue));
|
|
809
819
|
}
|
|
820
|
+
var LISTED_VALUE_OPS = new Set(["=", "!=", "in", "notIn"]);
|
|
821
|
+
function listedValueIssue(check, kind, filter) {
|
|
822
|
+
if (typeof filter.col !== "string" || !LISTED_VALUE_OPS.has(filter.op))
|
|
823
|
+
return;
|
|
824
|
+
if (filter.value === undefined || isColumnValue(filter.value))
|
|
825
|
+
return;
|
|
826
|
+
const listed = check.columnOf(kind, filter.col)?.values;
|
|
827
|
+
if (!listed)
|
|
828
|
+
return;
|
|
829
|
+
const values = Array.isArray(filter.value) ? filter.value : [filter.value];
|
|
830
|
+
const odd = values.find((value) => !listed.includes(String(value)));
|
|
831
|
+
return odd === undefined ? undefined : `${JSON.stringify(odd)} is not one of its values: ${listed.join(", ")}`;
|
|
832
|
+
}
|
|
810
833
|
function truncatedDateIssue(filter) {
|
|
811
834
|
const truncated = typeof filter.col === "object" && "fn" in filter.col && filter.col.fn === "dateTrunc";
|
|
812
835
|
if (!truncated || filter.value === undefined || isColumnValue(filter.value))
|
|
@@ -828,7 +851,7 @@ function filterValueTypeIssue(type, filter) {
|
|
|
828
851
|
const fits = (value) => {
|
|
829
852
|
switch (type) {
|
|
830
853
|
case "number":
|
|
831
|
-
return typeof value === "number" || typeof value === "string" &&
|
|
854
|
+
return typeof value === "number" || typeof value === "string" && JSON_NUMBER_PATTERN.test(value) && Number.isFinite(Number(value));
|
|
832
855
|
case "date":
|
|
833
856
|
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
834
857
|
case "boolean":
|
|
@@ -880,6 +903,15 @@ function aggregateType(check, aggregate, label = `aggregate "${aggregate.as}"`)
|
|
|
880
903
|
const counts = aggregate.fn === "count" || aggregate.fn === "countDistinct";
|
|
881
904
|
return counts || numeric ? "number" : type ?? "number";
|
|
882
905
|
}
|
|
906
|
+
function passedColumns(catalog, query, entries) {
|
|
907
|
+
return new Map(entries.flatMap((entry) => {
|
|
908
|
+
const [name, ref] = typeof entry === "string" ? [entry, entry] : [entry.as, entry.expr];
|
|
909
|
+
if (typeof ref !== "string")
|
|
910
|
+
return [];
|
|
911
|
+
const resolved = resolveDatasetColumn(catalog, query.from, ref);
|
|
912
|
+
return resolved.ok ? [[name, resolved.column]] : [];
|
|
913
|
+
}));
|
|
914
|
+
}
|
|
883
915
|
function checkSort(check, sort, kind, row) {
|
|
884
916
|
for (const { col } of sort)
|
|
885
917
|
checkRowName(check, kind, col, row);
|
|
@@ -1042,6 +1074,9 @@ class Sources {
|
|
|
1042
1074
|
if (isLink(source)) {
|
|
1043
1075
|
throw new DatasetSqlError(`"${qualifier}" is a link table; read its columns from the datasets it links.`);
|
|
1044
1076
|
}
|
|
1077
|
+
if (!source.dataset.columns.some((c) => c.name === column)) {
|
|
1078
|
+
throw new DatasetSqlError(unknownColumnError(source.dataset, column));
|
|
1079
|
+
}
|
|
1045
1080
|
return source.path ? `${source.path}.${column}` : column;
|
|
1046
1081
|
}
|
|
1047
1082
|
name(qualifier, source) {
|
|
@@ -1107,8 +1142,8 @@ class Sources {
|
|
|
1107
1142
|
if (owners.length > 1) {
|
|
1108
1143
|
throw new DatasetSqlError(`"${column}" is a column of ${owners.map((o) => o.dataset.name).join(" and ")}; qualify it as dataset.column.`);
|
|
1109
1144
|
}
|
|
1110
|
-
const joined = this.paths.length ? "
|
|
1111
|
-
throw new DatasetSqlError(
|
|
1145
|
+
const joined = this.paths.length ? " Nor does any joined dataset." : "";
|
|
1146
|
+
throw new DatasetSqlError(unknownColumnError(this.root.dataset, column) + joined);
|
|
1112
1147
|
}
|
|
1113
1148
|
sources() {
|
|
1114
1149
|
const seen = new Set;
|
|
@@ -1146,4 +1181,4 @@ class Sources {
|
|
|
1146
1181
|
}
|
|
1147
1182
|
// ../../internal/contracts/src/datasets/dataset-sql-guide.ts
|
|
1148
1183
|
var DATASET_SQL_GRAMMAR = "One SELECT over one dataset. LEFT JOIN related datasets exactly as the dataset's description lists them (LEFT JOIN keeps rows without a match; JOIN or INNER JOIN drops them); reference dataset.column. " + "SELECT: columns, DATE_TRUNC('unit', column), EXTRACT(part FROM column), CASE WHEN ... END, LOWER/UPPER/TRIM/LENGTH/ABS/ROUND/COALESCE, COUNT/SUM/AVG/MIN/MAX over a column or arithmetic each with an optional FILTER (WHERE ...), and named arithmetic between aggregates (SUM(a) / COUNT(*) AS avg). " + "WHERE and HAVING: AND, OR, NOT, parentheses, comparisons with literals or other columns, IN, BETWEEN, LIKE, IS NULL; CURRENT_DATE and INTERVAL arithmetic on it. " + "GROUP BY columns or select aliases. An aggregate with OVER () or OVER (PARTITION BY ...) is a window: that aggregate over the rows sharing the partition, on every row (SUM(COUNT(*)) OVER () is the grand total); name it AS, use it in arithmetic, and QUALIFY filters the finished rows. " + "QUALIFY RANK() OVER (PARTITION BY ... ORDER BY ...) <= n for the top n per group (rows tied on every ORDER BY key share a rank and all stay); ORDER BY; LIMIT; OFFSET. " + "Rows nest qualified columns under the qualifier as written (pets.name comes back as row.pets.name); aliases and aggregates are top-level.";
|
|
1149
|
-
export { BACKEND_ERROR_STATUS, backendErrorSchema, isRecord,
|
|
1184
|
+
export { BACKEND_ERROR_STATUS, backendErrorSchema, errorEnvelope, isRecord, readJsonPointer, CortexError, malformedBody, envelopeOf, protocolBodyLimit, contentVersion, datasetIdentifierSchema, datasetColumnSchema, datasetDefinitionSchema, DATASET_REQUEST_MAX_BYTES, datasetRequestSchema, datasetCatalogOf, groupKeyName, filterRefs, isColumnValue, checkDatasetQuery, boundDatasetResult, FUNCTION_SIGNATURES };
|