@mnmal-ai/substrate 1.0.0-alpha.15 → 1.0.0-alpha.17
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 +3 -2
- package/dist/{boot-store-CvQabBdk.js → boot-store-DNU0UfX7.js} +19 -66
- package/dist/{bridge-CvZGzNIO.d.ts → bridge-5VHZiH9o.d.ts} +1 -1
- package/dist/{cli-6evrDG1o.js → cli-CwWUYUMv.js} +71 -13
- package/dist/client.d.ts +3 -3
- package/dist/client.js +3 -3
- package/dist/{errors-BXxm9eZF.d.ts → errors-DHgT7Fa8.d.ts} +8 -2
- package/dist/{index-EN-nQsX8.d.ts → index-DKJXQ031.d.ts} +1 -1
- package/dist/{node-version-C5FSmwJX.js → node-version-CxqcqECU.js} +1 -1
- package/dist/pack.d.ts +18 -2
- package/dist/pack.js +2 -2
- package/dist/{pg-edges-store-DHbpr8QY.js → pg-edges-store-CSJ7sG2l.js} +1 -1
- package/dist/{plugin-Dpa_Y7Ep.js → plugin-Dk9RC3LH.js} +2 -2
- package/dist/proposals-bootstrap.js +2 -2
- package/dist/proposals.d.ts +5 -3
- package/dist/proposals.js +1 -1
- package/dist/recall-bootstrap.js +2 -2
- package/dist/recall.d.ts +1 -1
- package/dist/recall.js +2 -2
- package/dist/{repl-DWXNLUyd.js → repl-BNnKFG3y.js} +1 -1
- package/dist/{row-Dh57ShWL.d.ts → row-DHxnCnhA.d.ts} +10 -8
- package/dist/{run-BTdS6Z6i.js → run-NmwNmVUD.js} +142 -106
- package/dist/{schema-B--_bHjn.d.ts → schema-B5WBO8yh.d.ts} +79 -19
- package/dist/sdk.d.ts +48 -6
- package/dist/sdk.js +4 -4
- package/dist/{src-EqhJm93D.js → src-7lp1y4HK.js} +3 -3
- package/dist/{src-BMHI_rTJ.js → src-BHh6UZ7L.js} +2141 -493
- package/dist/{src-DWqzrm3O.js → src-CdbUzLhB.js} +209 -36
- package/dist/{src-Bi3NhEsT.js → src-DR_hUj2p.js} +1 -1
- package/dist/{src-CHND7-Pm.js → src-DV6PcXym.js} +2 -2
- package/dist/{src-CZQ25H4i.js → src-DWrFdHrY.js} +1018 -252
- package/dist/{src-B4sqnLRy.js → src-DwynOVMk.js} +77 -28
- package/dist/substrate-admin.js +2 -2
- package/dist/substrate.js +2 -2
- package/dist/testing.d.ts +5 -3
- package/dist/testing.js +4 -4
- package/dist/{ws-CpHWxwp9.d.ts → ws-BL9nSELO.d.ts} +2 -2
- package/package.json +23 -23
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import { A as expandFlat, D as isActiveAdminKeyRow, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, l as requireNamespace, m as grantedIn, o as denyVerbs, p as grantOf, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or } from "./src-
|
|
3
|
-
import {
|
|
2
|
+
import { A as expandFlat, D as isActiveAdminKeyRow, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, l as requireNamespace, m as grantedIn, o as denyVerbs, p as grantOf, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or } from "./src-DR_hUj2p.js";
|
|
3
|
+
import { A as parseInt64, D as parseQualifiedKey, E as formatQualifiedKey, F as EXPR_FUNCTIONS, I as EXPR_LIMITS, L as DEFAULT_FILTER_WRITE_LIMITS, N as DATE_UNITS, O as isInt16, P as EXPR_AGGREGATES, R as assertNever, S as withData, T as naturalOrderOf, _ as encodeEnvelope, b as packEnvelopeParts, c as isPlainRequestData, j as stringifyJson, k as isInt32, o as decodeWsEnvelope, s as encodeWsFrame, t as WS_SUBPROTOCOL_SUBSTRATE_PACK, v as encodePositional, w as isColumnField, x as positionValue } from "./src-CdbUzLhB.js";
|
|
4
4
|
import { n as acceptsPack, t as PACK_MIME } from "./content-type-C7T28xNb.js";
|
|
5
5
|
import { readFileSync } from "node:fs";
|
|
6
6
|
import { createCipheriv, createDecipheriv, createHash, createHmac, createPrivateKey, createPublicKey, createVerify, hkdfSync, randomBytes, randomUUID, sign, timingSafeEqual, verify } from "node:crypto";
|
|
@@ -99,6 +99,12 @@ const WALK_BUDGET_EXCEEDED = "walk_budget_exceeded";
|
|
|
99
99
|
const WALKS_UNSUPPORTED_STORE = "walks_unsupported_store";
|
|
100
100
|
/** A read cancelled by the store's statement timeout; `details: { timeoutMs }`. */
|
|
101
101
|
const QUERY_TIMEOUT = "query_timeout";
|
|
102
|
+
/** No store connection came free within the wait limit, so the request never started; retry later. */
|
|
103
|
+
const STORE_BUSY = "store_busy";
|
|
104
|
+
/** An aggregate or expression whose value leaves its type's range (an int64 sum past 2^63-1); the same code on every engine. */
|
|
105
|
+
const VALUE_OUT_OF_RANGE = "value_out_of_range";
|
|
106
|
+
/** A regex call on the memory evaluator that would take more steps than its budget; `details: { budget }`. */
|
|
107
|
+
const REGEX_BUDGET_EXCEEDED = "regex_budget_exceeded";
|
|
102
108
|
/**
|
|
103
109
|
* A walk construct refused where it cannot be answered: inside a struct, over
|
|
104
110
|
* a list or struct-nested crossRef, under a reused field name, on a virtual
|
|
@@ -1194,24 +1200,22 @@ function nestedFields(sel) {
|
|
|
1194
1200
|
function projectEnvelope(envelope, frame) {
|
|
1195
1201
|
const data = {};
|
|
1196
1202
|
for (const [key, value] of Object.entries(envelope.data)) {
|
|
1197
|
-
const
|
|
1198
|
-
|
|
1199
|
-
data[key] = value;
|
|
1200
|
-
continue;
|
|
1201
|
-
}
|
|
1202
|
-
const fields = entry.fields;
|
|
1203
|
-
const shape = envelope.shapes[key];
|
|
1204
|
-
if (fields === void 0 || shape === "count" || shape === "groups") {
|
|
1205
|
-
data[key] = value;
|
|
1206
|
-
continue;
|
|
1207
|
-
}
|
|
1208
|
-
data[key] = projectValue(value, fields);
|
|
1203
|
+
const fields = projectionFields(envelope, frame, key);
|
|
1204
|
+
data[key] = fields === void 0 ? value : projectValue(value, fields);
|
|
1209
1205
|
}
|
|
1210
1206
|
return {
|
|
1211
1207
|
...envelope,
|
|
1212
1208
|
data
|
|
1213
1209
|
};
|
|
1214
1210
|
}
|
|
1211
|
+
/** The selection `envelope.data[key]` is projected through, or undefined when it passes through whole. */
|
|
1212
|
+
function projectionFields(envelope, frame, key) {
|
|
1213
|
+
const entry = frame[key];
|
|
1214
|
+
if (entry === void 0 || Array.isArray(entry)) return void 0;
|
|
1215
|
+
const shape = envelope.shapes[key];
|
|
1216
|
+
if (shape === "count" || shape === "groups") return void 0;
|
|
1217
|
+
return entry.fields;
|
|
1218
|
+
}
|
|
1215
1219
|
/**
|
|
1216
1220
|
* WS analog of `projectEnvelope` — applies the same row-trimming to the
|
|
1217
1221
|
* `data` payload of a `WsResponseFrame` using the per-key `fieldsByKey`
|
|
@@ -1240,6 +1244,95 @@ function projectWsFrame(frame, fieldsByKey) {
|
|
|
1240
1244
|
};
|
|
1241
1245
|
}
|
|
1242
1246
|
|
|
1247
|
+
//#endregion
|
|
1248
|
+
//#region ../../packages/substrate-compose/src/routes/body-stream.ts
|
|
1249
|
+
/**
|
|
1250
|
+
* A query body large enough to hold the event loop is built and sent a batch
|
|
1251
|
+
* of rows at a time, with a macrotask yield between batches: the same bytes
|
|
1252
|
+
* as the unstreamed body, JSON or substrate-pack.
|
|
1253
|
+
*/
|
|
1254
|
+
/** A body streams once one `data` key holds more rows than this. */
|
|
1255
|
+
const STREAM_ROWS = 2e4;
|
|
1256
|
+
/** Rows projected and serialised per chunk (a few ms of work). */
|
|
1257
|
+
const ROWS_PER_CHUNK = 2e3;
|
|
1258
|
+
/** True when some `data` value is a row list longer than `STREAM_ROWS`. */
|
|
1259
|
+
function streamsRows(envelope) {
|
|
1260
|
+
return Object.values(envelope.data).some((v) => Array.isArray(v) && v.length > 2e4);
|
|
1261
|
+
}
|
|
1262
|
+
function jsonEnvelopeStream(envelope, frame) {
|
|
1263
|
+
return partsStream(jsonParts(envelope, frame));
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* The substrate-pack body as a stream. Rows are positioned up front, a batch
|
|
1267
|
+
* at a time, so an encode error still fails the request before any byte is sent.
|
|
1268
|
+
*/
|
|
1269
|
+
async function packEnvelopeStream(envelope, ctx) {
|
|
1270
|
+
const data = {};
|
|
1271
|
+
for (const [key, value] of Object.entries(envelope.data)) {
|
|
1272
|
+
if (envelope.shapes[key] !== "list" || !Array.isArray(value)) {
|
|
1273
|
+
data[key] = positionValue(envelope, ctx, key);
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
const rows = [];
|
|
1277
|
+
for (let i = 0; i < value.length; i += ROWS_PER_CHUNK) {
|
|
1278
|
+
if (i > 0) await nextTurn();
|
|
1279
|
+
const part = positionValue(envelope, ctx, key, value.slice(i, i + ROWS_PER_CHUNK));
|
|
1280
|
+
for (const r of part) rows.push(r);
|
|
1281
|
+
}
|
|
1282
|
+
data[key] = rows;
|
|
1283
|
+
}
|
|
1284
|
+
return partsStream(packEnvelopeParts(withData(envelope, data), ROWS_PER_CHUNK));
|
|
1285
|
+
}
|
|
1286
|
+
function partsStream(parts) {
|
|
1287
|
+
const encoder = new TextEncoder();
|
|
1288
|
+
return new ReadableStream({ async pull(controller) {
|
|
1289
|
+
await nextTurn();
|
|
1290
|
+
const next = parts.next();
|
|
1291
|
+
if (next.done) controller.close();
|
|
1292
|
+
else controller.enqueue(typeof next.value === "string" ? encoder.encode(next.value) : next.value);
|
|
1293
|
+
} });
|
|
1294
|
+
}
|
|
1295
|
+
function nextTurn() {
|
|
1296
|
+
return new Promise((resolve) => setImmediate(resolve));
|
|
1297
|
+
}
|
|
1298
|
+
/** `JSON.stringify(projectEnvelope(envelope, frame))` in pieces, a long row list `ROWS_PER_CHUNK` rows per piece. */
|
|
1299
|
+
function* jsonParts(envelope, frame) {
|
|
1300
|
+
yield "{";
|
|
1301
|
+
let sep = "";
|
|
1302
|
+
for (const [k, v] of Object.entries(envelope)) {
|
|
1303
|
+
if (k !== "data") {
|
|
1304
|
+
const s = stringifyJson(v);
|
|
1305
|
+
if (s === void 0) continue;
|
|
1306
|
+
yield `${sep}${JSON.stringify(k)}:${s}`;
|
|
1307
|
+
sep = ",";
|
|
1308
|
+
continue;
|
|
1309
|
+
}
|
|
1310
|
+
yield `${sep}"data":{`;
|
|
1311
|
+
sep = ",";
|
|
1312
|
+
let dsep = "";
|
|
1313
|
+
for (const [key, value] of Object.entries(envelope.data)) {
|
|
1314
|
+
const fields = projectionFields(envelope, frame, key);
|
|
1315
|
+
const project = (x) => fields === void 0 ? x : projectValue(x, fields);
|
|
1316
|
+
if (!Array.isArray(value) || value.length <= ROWS_PER_CHUNK) {
|
|
1317
|
+
const s = stringifyJson(project(value));
|
|
1318
|
+
if (s === void 0) continue;
|
|
1319
|
+
yield `${dsep}${JSON.stringify(key)}:${s}`;
|
|
1320
|
+
dsep = ",";
|
|
1321
|
+
continue;
|
|
1322
|
+
}
|
|
1323
|
+
yield `${dsep}${JSON.stringify(key)}:[`;
|
|
1324
|
+
dsep = ",";
|
|
1325
|
+
for (let i = 0; i < value.length; i += ROWS_PER_CHUNK) {
|
|
1326
|
+
const chunk = stringifyJson(value.slice(i, i + ROWS_PER_CHUNK).map(project));
|
|
1327
|
+
yield `${i === 0 ? "" : ","}${chunk.slice(1, -1)}`;
|
|
1328
|
+
}
|
|
1329
|
+
yield "]";
|
|
1330
|
+
}
|
|
1331
|
+
yield "}";
|
|
1332
|
+
}
|
|
1333
|
+
yield "}";
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1243
1336
|
//#endregion
|
|
1244
1337
|
//#region ../../packages/substrate-compose/src/routes/helpers.ts
|
|
1245
1338
|
/**
|
|
@@ -1255,7 +1348,7 @@ function jsonResponse(status, value, extraHeaders) {
|
|
|
1255
1348
|
...JSON_HEADERS,
|
|
1256
1349
|
...extraHeaders
|
|
1257
1350
|
},
|
|
1258
|
-
body: Buffer.from(
|
|
1351
|
+
body: Buffer.from(stringifyJson(value), "utf8")
|
|
1259
1352
|
};
|
|
1260
1353
|
}
|
|
1261
1354
|
function error400(message) {
|
|
@@ -1405,20 +1498,25 @@ function readAcceptHeader(req) {
|
|
|
1405
1498
|
* msgpack wrapper. This handles error-only envelopes (unqualified/unknown_name)
|
|
1406
1499
|
* without a schema lookup.
|
|
1407
1500
|
*/
|
|
1408
|
-
function serializeEnvelope(req, envelope, engine, frame, opts = {}) {
|
|
1501
|
+
async function serializeEnvelope(req, envelope, engine, frame, opts = {}) {
|
|
1409
1502
|
if (!acceptsPack(readAcceptHeader(req))) {
|
|
1503
|
+
if (streamsRows(envelope)) return {
|
|
1504
|
+
status: 200,
|
|
1505
|
+
headers: JSON_HEADERS,
|
|
1506
|
+
body: jsonEnvelopeStream(envelope, frame)
|
|
1507
|
+
};
|
|
1410
1508
|
const projected = projectEnvelope(envelope, frame);
|
|
1411
1509
|
return {
|
|
1412
1510
|
status: 200,
|
|
1413
1511
|
headers: JSON_HEADERS,
|
|
1414
|
-
body: Buffer.from(
|
|
1512
|
+
body: Buffer.from(stringifyJson(projected), "utf8")
|
|
1415
1513
|
};
|
|
1416
1514
|
}
|
|
1417
1515
|
const ctx = buildEncodeCtx(engine, frame, opts);
|
|
1418
1516
|
return {
|
|
1419
1517
|
status: 200,
|
|
1420
1518
|
headers: PACK_HEADERS,
|
|
1421
|
-
body: encodeEnvelope(envelope, ctx)
|
|
1519
|
+
body: streamsRows(envelope) ? await packEnvelopeStream(envelope, ctx) : encodeEnvelope(envelope, ctx)
|
|
1422
1520
|
};
|
|
1423
1521
|
}
|
|
1424
1522
|
function buildEncodeCtx(engine, frame, opts) {
|
|
@@ -1898,6 +1996,8 @@ const STRING_OPS$1 = [
|
|
|
1898
1996
|
"contains",
|
|
1899
1997
|
"startsWith",
|
|
1900
1998
|
"endsWith",
|
|
1999
|
+
"like",
|
|
2000
|
+
"notLike",
|
|
1901
2001
|
"in",
|
|
1902
2002
|
"nin"
|
|
1903
2003
|
];
|
|
@@ -1938,6 +2038,9 @@ const JSON_PATH_OPS$1 = [
|
|
|
1938
2038
|
function fullBuiltinOperators() {
|
|
1939
2039
|
return {
|
|
1940
2040
|
"scalar.number": [...NUMBER_OPS$1],
|
|
2041
|
+
"scalar.int16": [...NUMBER_OPS$1],
|
|
2042
|
+
"scalar.int32": [...NUMBER_OPS$1],
|
|
2043
|
+
"scalar.int64": [...NUMBER_OPS$1],
|
|
1941
2044
|
"scalar.string": [...STRING_OPS$1],
|
|
1942
2045
|
"scalar.boolean": [...BOOLEAN_OPS$1],
|
|
1943
2046
|
"scalar.date": [...NUMBER_OPS$1],
|
|
@@ -1964,7 +2067,7 @@ var CursorDecodeError = class extends ResolverFrameError {
|
|
|
1964
2067
|
}
|
|
1965
2068
|
};
|
|
1966
2069
|
function encodeCursor(input) {
|
|
1967
|
-
const json =
|
|
2070
|
+
const json = stringifyJson(input);
|
|
1968
2071
|
return Buffer.from(json, "utf8").toString("base64url");
|
|
1969
2072
|
}
|
|
1970
2073
|
const orderText = (order) => order.map(([f, d]) => `${f} ${d}`).join(", ");
|
|
@@ -2021,172 +2124,512 @@ function normalizeOrderBy(orderBy) {
|
|
|
2021
2124
|
}
|
|
2022
2125
|
|
|
2023
2126
|
//#endregion
|
|
2024
|
-
//#region ../../packages/substrate-filter/src/
|
|
2127
|
+
//#region ../../packages/substrate-filter/src/regex-ast.ts
|
|
2128
|
+
function parse(pattern) {
|
|
2129
|
+
const cps = Array.from(pattern, (c) => c.codePointAt(0));
|
|
2130
|
+
let i = 0;
|
|
2131
|
+
let groups = 0;
|
|
2132
|
+
let reps = 0;
|
|
2133
|
+
const ch = (c) => c.codePointAt(0);
|
|
2134
|
+
const peek = () => cps[i];
|
|
2135
|
+
const alt = () => {
|
|
2136
|
+
const items = [seq()];
|
|
2137
|
+
while (peek() === ch("|")) {
|
|
2138
|
+
i++;
|
|
2139
|
+
items.push(seq());
|
|
2140
|
+
}
|
|
2141
|
+
return items.length === 1 ? items[0] : {
|
|
2142
|
+
t: "alt",
|
|
2143
|
+
items
|
|
2144
|
+
};
|
|
2145
|
+
};
|
|
2146
|
+
const seq = () => {
|
|
2147
|
+
const items = [];
|
|
2148
|
+
for (;;) {
|
|
2149
|
+
const c = peek();
|
|
2150
|
+
if (c === void 0 || c === ch("|") || c === ch(")")) break;
|
|
2151
|
+
items.push(quant(atom()));
|
|
2152
|
+
}
|
|
2153
|
+
return {
|
|
2154
|
+
t: "cat",
|
|
2155
|
+
items
|
|
2156
|
+
};
|
|
2157
|
+
};
|
|
2158
|
+
const atom = () => {
|
|
2159
|
+
const c = cps[i++];
|
|
2160
|
+
if (c === ch("(")) {
|
|
2161
|
+
let cap = null;
|
|
2162
|
+
if (peek() === ch("?")) i += 2;
|
|
2163
|
+
else cap = groups++;
|
|
2164
|
+
const body = alt();
|
|
2165
|
+
i++;
|
|
2166
|
+
return {
|
|
2167
|
+
t: "group",
|
|
2168
|
+
cap,
|
|
2169
|
+
body
|
|
2170
|
+
};
|
|
2171
|
+
}
|
|
2172
|
+
if (c === ch("[")) return cls();
|
|
2173
|
+
if (c === ch(".")) return { t: "any" };
|
|
2174
|
+
if (c === ch("^")) return { t: "bol" };
|
|
2175
|
+
if (c === ch("$")) return { t: "eol" };
|
|
2176
|
+
if (c === ch("\\")) return {
|
|
2177
|
+
t: "char",
|
|
2178
|
+
c: cps[i++]
|
|
2179
|
+
};
|
|
2180
|
+
return {
|
|
2181
|
+
t: "char",
|
|
2182
|
+
c
|
|
2183
|
+
};
|
|
2184
|
+
};
|
|
2185
|
+
const cls = () => {
|
|
2186
|
+
const neg = peek() === ch("^");
|
|
2187
|
+
if (neg) i++;
|
|
2188
|
+
const ranges = [];
|
|
2189
|
+
const cchar = () => {
|
|
2190
|
+
const c = cps[i++];
|
|
2191
|
+
return c === ch("\\") ? cps[i++] : c;
|
|
2192
|
+
};
|
|
2193
|
+
while (peek() !== ch("]")) {
|
|
2194
|
+
const lo = cchar();
|
|
2195
|
+
if (peek() === ch("-") && cps[i + 1] !== ch("]")) {
|
|
2196
|
+
i++;
|
|
2197
|
+
ranges.push([lo, cchar()]);
|
|
2198
|
+
} else ranges.push([lo, lo]);
|
|
2199
|
+
}
|
|
2200
|
+
i++;
|
|
2201
|
+
return {
|
|
2202
|
+
t: "class",
|
|
2203
|
+
neg,
|
|
2204
|
+
ranges
|
|
2205
|
+
};
|
|
2206
|
+
};
|
|
2207
|
+
const quant = (body) => {
|
|
2208
|
+
const c = peek();
|
|
2209
|
+
const simple = {
|
|
2210
|
+
[ch("*")]: [0, Number.POSITIVE_INFINITY],
|
|
2211
|
+
[ch("+")]: [1, Number.POSITIVE_INFINITY],
|
|
2212
|
+
[ch("?")]: [0, 1]
|
|
2213
|
+
};
|
|
2214
|
+
const bounds = c === void 0 ? void 0 : simple[c];
|
|
2215
|
+
if (bounds) {
|
|
2216
|
+
i++;
|
|
2217
|
+
return {
|
|
2218
|
+
t: "rep",
|
|
2219
|
+
body,
|
|
2220
|
+
min: bounds[0],
|
|
2221
|
+
max: bounds[1],
|
|
2222
|
+
id: reps++
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
if (c !== ch("{")) return body;
|
|
2226
|
+
const close = cps.indexOf(ch("}"), i);
|
|
2227
|
+
const [lo, comma, hi] = String.fromCodePoint(...cps.slice(i + 1, close)).split(/(,)/);
|
|
2228
|
+
i = close + 1;
|
|
2229
|
+
const min = Number(lo);
|
|
2230
|
+
return {
|
|
2231
|
+
t: "rep",
|
|
2232
|
+
body,
|
|
2233
|
+
min,
|
|
2234
|
+
max: comma ? hi ? Number(hi) : Number.POSITIVE_INFINITY : min,
|
|
2235
|
+
id: reps++
|
|
2236
|
+
};
|
|
2237
|
+
};
|
|
2238
|
+
return {
|
|
2239
|
+
node: alt(),
|
|
2240
|
+
groups,
|
|
2241
|
+
reps
|
|
2242
|
+
};
|
|
2243
|
+
}
|
|
2244
|
+
/** True when the node can match the empty string. */
|
|
2245
|
+
function nullable(n) {
|
|
2246
|
+
switch (n.t) {
|
|
2247
|
+
case "bol":
|
|
2248
|
+
case "eol": return true;
|
|
2249
|
+
case "group": return nullable(n.body);
|
|
2250
|
+
case "cat": return n.items.every(nullable);
|
|
2251
|
+
case "alt": return n.items.some(nullable);
|
|
2252
|
+
case "rep": return n.min === 0 || nullable(n.body);
|
|
2253
|
+
default: return false;
|
|
2254
|
+
}
|
|
2255
|
+
}
|
|
2256
|
+
/** The number of instructions the matcher emits for a node (counted repeats are unrolled). */
|
|
2257
|
+
function nodeSize(n) {
|
|
2258
|
+
switch (n.t) {
|
|
2259
|
+
case "group": return nodeSize(n.body) + (n.cap === null ? 0 : 2);
|
|
2260
|
+
case "cat": return n.items.reduce((s, x) => s + nodeSize(x), 0);
|
|
2261
|
+
case "alt": return n.items.reduce((s, x) => s + nodeSize(x), 0) + 2 * (n.items.length - 1);
|
|
2262
|
+
case "rep": {
|
|
2263
|
+
const b = nodeSize(n.body);
|
|
2264
|
+
const opt = b + (nullable(n.body) ? 2 : 0);
|
|
2265
|
+
const tail = n.max === Number.POSITIVE_INFINITY ? opt + 2 : (n.max - n.min) * (opt + 1);
|
|
2266
|
+
return n.min * b + tail;
|
|
2267
|
+
}
|
|
2268
|
+
default: return 1;
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
/** Instructions in a whole program: the node plus the two capture saves and the match. */
|
|
2272
|
+
/**
|
|
2273
|
+
* True when the pattern has both a capturing group and an alternation. Postgres
|
|
2274
|
+
* then assigns captures by POSIX subexpression rules the VM does not follow, so
|
|
2275
|
+
* `regexpReplace` refuses it.
|
|
2276
|
+
*/
|
|
2277
|
+
function capturesWithAlternation(pattern) {
|
|
2278
|
+
const { node, groups } = parse(pattern);
|
|
2279
|
+
const alt = (n) => n.t === "alt" || (n.t === "group" || n.t === "rep") && alt(n.body) || n.t === "cat" && n.items.some(alt);
|
|
2280
|
+
return groups > 0 && alt(node);
|
|
2281
|
+
}
|
|
2282
|
+
const programSize = (n) => nodeSize(n) + 3;
|
|
2283
|
+
|
|
2284
|
+
//#endregion
|
|
2285
|
+
//#region ../../packages/substrate-filter/src/regex-subset.ts
|
|
2025
2286
|
/**
|
|
2026
|
-
*
|
|
2287
|
+
* The portable regex subset: the syntax on which Postgres (POSIX ARE) and
|
|
2288
|
+
* JavaScript agree.
|
|
2027
2289
|
*
|
|
2028
|
-
*
|
|
2029
|
-
*
|
|
2030
|
-
*
|
|
2031
|
-
*
|
|
2032
|
-
*
|
|
2033
|
-
*
|
|
2034
|
-
*
|
|
2290
|
+
* regex := alt
|
|
2291
|
+
* alt := seq ('|' seq)*
|
|
2292
|
+
* seq := (atom quant?)*
|
|
2293
|
+
* atom := '(' '?:'? alt ')' | '[' '^'? item+ ']' | '.' | '^' | '$'
|
|
2294
|
+
* | '\' esc | char
|
|
2295
|
+
* item := cchar ('-' cchar)? (a range may not chain, as in a-c-e)
|
|
2296
|
+
* cchar := '\' (esc | '-') | any char but ] [ \ newline
|
|
2297
|
+
* quant := '*' | '+' | '?' | '{' n '}' | '{' n ',' '}' | '{' n ',' m '}' (n, m <= 255)
|
|
2298
|
+
* esc := one of ^ $ \ . * + ? ( ) [ ] { } | /
|
|
2299
|
+
* char := any character but ( ) [ ] { } | * + ? \ newline
|
|
2035
2300
|
*
|
|
2036
|
-
*
|
|
2037
|
-
*
|
|
2301
|
+
* The pattern is read by code point. `.` matches a newline on both engines
|
|
2302
|
+
* (Postgres by default, JavaScript under the `s` flag), so JavaScript compiles
|
|
2303
|
+
* it with flags `us`; every accepted pattern constructs under `new RegExp(p, 'us')`.
|
|
2038
2304
|
*
|
|
2039
|
-
*
|
|
2040
|
-
*
|
|
2041
|
-
*
|
|
2042
|
-
* operator registered through one copy would be silently unknown to the other.
|
|
2305
|
+
* Bounded for linear-ish matching: a pattern is at most 256 code points, and a
|
|
2306
|
+
* quantifier may not apply to a group that itself contains a quantifier or an
|
|
2307
|
+
* alternation (star height at most 1), so `(a+)+`, `(a|aa)*` and `(?:a*)*` are refused.
|
|
2043
2308
|
*
|
|
2044
|
-
*
|
|
2045
|
-
*
|
|
2046
|
-
* entries. Bump the suffix when `FilterOperatorDef` changes shape.
|
|
2047
|
-
*/
|
|
2048
|
-
const REGISTRY_KEY = Symbol.for("substrate.filter.extensionOperators.v1");
|
|
2049
|
-
const OWNERS_KEY = Symbol.for("substrate.filter.computedFieldOwners.v1");
|
|
2050
|
-
const host = globalThis;
|
|
2051
|
-
const extensionOperators = host[REGISTRY_KEY] ?? /* @__PURE__ */ new Map();
|
|
2052
|
-
host[REGISTRY_KEY] = extensionOperators;
|
|
2053
|
-
/**
|
|
2054
|
-
* computedField → owning operator name. An operator may declare
|
|
2055
|
-
* `computedFields` (e.g. embeddingSimilar owns `_score` + `_vector`): fields
|
|
2056
|
-
* that only exist on a row when the operator runs. The engine's computed-field
|
|
2057
|
-
* gate reads this map to refuse projecting a computed field without its operator in
|
|
2058
|
-
* the where-clause.
|
|
2059
|
-
*/
|
|
2060
|
-
const computedFieldOwners = host[OWNERS_KEY] ?? /* @__PURE__ */ new Map();
|
|
2061
|
-
host[OWNERS_KEY] = computedFieldOwners;
|
|
2062
|
-
/** Where-clause keys the grammar already owns: combinators, ids and row metadata. */
|
|
2063
|
-
function isReservedName(name) {
|
|
2064
|
-
return [
|
|
2065
|
-
"AND",
|
|
2066
|
-
"OR",
|
|
2067
|
-
"NOT",
|
|
2068
|
-
"shortId",
|
|
2069
|
-
"_metadata"
|
|
2070
|
-
].includes(name) || Object.hasOwn(METADATA_FILTER_FIELDS, name) || Object.hasOwn(METADATA_AUTHOR_FIELDS, name);
|
|
2071
|
-
}
|
|
2072
|
-
/**
|
|
2073
|
-
* Register a custom where-clause operator under `name`. Throws if `name` is a
|
|
2074
|
-
* reserved where-clause key or already registered, or if any of its
|
|
2075
|
-
* `computedFields` is already claimed by another operator. `name` comes from the Record key in
|
|
2076
|
-
* `KernelPlugin.filterOperators`.
|
|
2309
|
+
* Its compiled program is bounded too: counted repeats are unrolled, and a pattern
|
|
2310
|
+
* whose program would exceed 2,000 instructions is refused ('program too large').
|
|
2077
2311
|
*
|
|
2078
|
-
*
|
|
2079
|
-
*
|
|
2080
|
-
*/
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
* the per-frame fields walk entirely); the per-field `computedFieldOwner`
|
|
2103
|
-
* check remains authoritative.
|
|
2104
|
-
*/
|
|
2105
|
-
function hasComputedFields() {
|
|
2106
|
-
return computedFieldOwners.size > 0;
|
|
2107
|
-
}
|
|
2108
|
-
/** Look up a registered extension operator by name. Returns undefined when absent. */
|
|
2109
|
-
function getExtensionOperator(name) {
|
|
2110
|
-
return extensionOperators.get(name);
|
|
2111
|
-
}
|
|
2112
|
-
/**
|
|
2113
|
-
* Sorted list of registered extension-operator names. Surfaced by
|
|
2114
|
-
* `/substrate/v1/onboard` so HTTP consumers can branch on "is `embeddingSimilar`
|
|
2115
|
-
* composed?" without sniffing error strings from a failed query.
|
|
2116
|
-
*/
|
|
2117
|
-
function listExtensionOperators() {
|
|
2118
|
-
return Array.from(extensionOperators.keys()).sort();
|
|
2119
|
-
}
|
|
2120
|
-
/** Remove `name` and the computed fields it owns. A no-op when `name` is not registered. */
|
|
2121
|
-
function unregisterExtensionOperator(name) {
|
|
2122
|
-
const op = extensionOperators.get(name);
|
|
2123
|
-
if (op === void 0) return;
|
|
2124
|
-
extensionOperators.delete(name);
|
|
2125
|
-
for (const cf of op.computedFields ?? []) if (computedFieldOwners.get(cf) === name) computedFieldOwners.delete(cf);
|
|
2126
|
-
}
|
|
2127
|
-
/**
|
|
2128
|
-
* True when any key in the where-tree (root, nested under AND/OR/NOT, or
|
|
2129
|
-
* inside a struct-field sub-clause) is a registered extension operator.
|
|
2130
|
-
* Used by the subscribe path to refuse semantic filters at registration —
|
|
2131
|
-
* extension operators have no incremental evaluation, so a subscription
|
|
2132
|
-
* carrying one would silently degrade. Leaf predicate objects ({ eq: 'x' }) recurse harmlessly: their
|
|
2133
|
-
* keys are not registered operators and their values are not objects.
|
|
2134
|
-
*/
|
|
2135
|
-
function whereContainsExtensionOperator(where) {
|
|
2136
|
-
if (Array.isArray(where)) return where.some((w) => whereContainsExtensionOperator(w));
|
|
2137
|
-
if (where === null || typeof where !== "object") return false;
|
|
2138
|
-
for (const [key, value] of Object.entries(where)) {
|
|
2139
|
-
if (key === "AND" || key === "OR") {
|
|
2140
|
-
if (Array.isArray(value) && value.some((w) => whereContainsExtensionOperator(w))) return true;
|
|
2141
|
-
continue;
|
|
2312
|
+
* Refused: classes such as `\d`, backreferences, lookaround, inline flags,
|
|
2313
|
+
* lazy quantifiers, POSIX bracket classes, quantified anchors, newlines.
|
|
2314
|
+
*/
|
|
2315
|
+
const MAX_REPEAT = 255;
|
|
2316
|
+
const MAX_PATTERN = 256;
|
|
2317
|
+
/** The most instructions a pattern may compile to once counted repeats are unrolled. */
|
|
2318
|
+
const MAX_PROGRAM = 2e3;
|
|
2319
|
+
function checkRegexSubset(pattern) {
|
|
2320
|
+
if (/[\r\n]/.test(pattern)) return "a newline is not allowed in a pattern";
|
|
2321
|
+
const cps = Array.from(pattern);
|
|
2322
|
+
if (cps.length > MAX_PATTERN) return `a pattern longer than ${MAX_PATTERN} code points`;
|
|
2323
|
+
let i = 0;
|
|
2324
|
+
let marks = 0;
|
|
2325
|
+
const fail = (why) => {
|
|
2326
|
+
throw new RegexRefusal(`${why} at position ${i}`);
|
|
2327
|
+
};
|
|
2328
|
+
const ESCAPABLE = "^$\\.*+?()[]{}|/";
|
|
2329
|
+
const escapable = (c, inClass = false) => c !== void 0 && (ESCAPABLE.includes(c) || inClass && c === "-");
|
|
2330
|
+
const alt = (depth) => {
|
|
2331
|
+
seq(depth);
|
|
2332
|
+
while (cps[i] === "|") {
|
|
2333
|
+
i++;
|
|
2334
|
+
marks++;
|
|
2335
|
+
seq(depth);
|
|
2142
2336
|
}
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2337
|
+
};
|
|
2338
|
+
const seq = (depth) => {
|
|
2339
|
+
for (;;) {
|
|
2340
|
+
const c = cps[i];
|
|
2341
|
+
if (c === void 0 || c === "|" || c === ")" && depth > 0) return;
|
|
2342
|
+
let quantifiable = true;
|
|
2343
|
+
let before = marks;
|
|
2344
|
+
let group = false;
|
|
2345
|
+
if (c === "(") {
|
|
2346
|
+
i++;
|
|
2347
|
+
if (cps[i] === "?") {
|
|
2348
|
+
if (cps.slice(i, i + 2).join("") !== "?:") fail("only the non-capturing group (?: is allowed after (?");
|
|
2349
|
+
i += 2;
|
|
2350
|
+
}
|
|
2351
|
+
before = marks;
|
|
2352
|
+
group = true;
|
|
2353
|
+
alt(depth + 1);
|
|
2354
|
+
if (cps[i] !== ")") fail("unclosed group");
|
|
2355
|
+
i++;
|
|
2356
|
+
} else if (c === "[") cls();
|
|
2357
|
+
else if (c === "^" || c === "$") {
|
|
2358
|
+
quantifiable = false;
|
|
2359
|
+
i++;
|
|
2360
|
+
} else if (c === "\\") {
|
|
2361
|
+
const n = cps[i + 1];
|
|
2362
|
+
if (!escapable(n)) fail("only an escaped syntax character is allowed (no \\d, \\w, \\1)");
|
|
2363
|
+
i += 2;
|
|
2364
|
+
} else if (c === ".") i++;
|
|
2365
|
+
else if (")]}{*+?".includes(c)) fail(`unexpected '${c}'`);
|
|
2366
|
+
else i++;
|
|
2367
|
+
const risky = group && marks > before;
|
|
2368
|
+
if (quant(quantifiable) && risky) fail("a quantifier on a group that contains a quantifier or alternation");
|
|
2146
2369
|
}
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2370
|
+
};
|
|
2371
|
+
const quant = (quantifiable) => {
|
|
2372
|
+
const c = cps[i];
|
|
2373
|
+
if (c === "*" || c === "+" || c === "?") i++;
|
|
2374
|
+
else if (c === "{") {
|
|
2375
|
+
const m = /^\{(\d+)(?:(,)(\d*))?\}/.exec(cps.slice(i).join(""));
|
|
2376
|
+
if (!m) fail("malformed {n,m} quantifier");
|
|
2377
|
+
else {
|
|
2378
|
+
if (Number(m[1]) > MAX_REPEAT || Number(m[3] || 0) > MAX_REPEAT) fail(`a repetition count above ${MAX_REPEAT}`);
|
|
2379
|
+
if (m[3] && Number(m[3]) < Number(m[1])) fail("{n,m} with m below n");
|
|
2380
|
+
i += m[0].length;
|
|
2381
|
+
}
|
|
2382
|
+
} else return false;
|
|
2383
|
+
marks++;
|
|
2384
|
+
if (!quantifiable) fail("an anchor cannot be quantified");
|
|
2385
|
+
const n = cps[i];
|
|
2386
|
+
if (n === "?") fail("lazy quantifiers are not allowed");
|
|
2387
|
+
if (n === "*" || n === "+" || n === "{") fail("a quantifier cannot follow a quantifier");
|
|
2388
|
+
return true;
|
|
2389
|
+
};
|
|
2390
|
+
const cls = () => {
|
|
2391
|
+
i++;
|
|
2392
|
+
if (cps[i] === "^") i++;
|
|
2393
|
+
let items = 0;
|
|
2394
|
+
const cchar = () => {
|
|
2395
|
+
const c = cps[i];
|
|
2396
|
+
if (c === void 0) return fail("unclosed class");
|
|
2397
|
+
if (c === "[") return fail("'[' is not allowed in a class (no POSIX classes)");
|
|
2398
|
+
if (c === "\\") {
|
|
2399
|
+
const n = cps[i + 1];
|
|
2400
|
+
if (!escapable(n, true)) return fail("only an escaped syntax character is allowed in a class");
|
|
2401
|
+
i += 2;
|
|
2402
|
+
return n;
|
|
2403
|
+
}
|
|
2404
|
+
i++;
|
|
2405
|
+
return c;
|
|
2406
|
+
};
|
|
2407
|
+
while (cps[i] !== "]") {
|
|
2408
|
+
const lo = cchar();
|
|
2409
|
+
items++;
|
|
2410
|
+
if (cps[i] === "-" && cps[i + 1] !== "]" && cps[i + 1] !== void 0) {
|
|
2411
|
+
i++;
|
|
2412
|
+
const hi = cchar();
|
|
2413
|
+
if (lo.codePointAt(0) > hi.codePointAt(0)) fail("class range out of order");
|
|
2414
|
+
if (cps[i] === "-" && cps[i + 1] !== "]") fail("chained class ranges such as a-c-e are not allowed");
|
|
2415
|
+
}
|
|
2150
2416
|
}
|
|
2417
|
+
if (items === 0) fail("empty class");
|
|
2418
|
+
i++;
|
|
2419
|
+
};
|
|
2420
|
+
try {
|
|
2421
|
+
alt(0);
|
|
2422
|
+
if (i < cps.length) fail("unbalanced )");
|
|
2423
|
+
if (programSize(parse(pattern).node) > 2e3) fail("program too large");
|
|
2424
|
+
return null;
|
|
2425
|
+
} catch (e) {
|
|
2426
|
+
if (e instanceof RegexRefusal) return e.message;
|
|
2427
|
+
throw e;
|
|
2151
2428
|
}
|
|
2152
|
-
return false;
|
|
2153
2429
|
}
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
*
|
|
2160
|
-
*
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2430
|
+
var RegexRefusal = class extends Error {};
|
|
2431
|
+
|
|
2432
|
+
//#endregion
|
|
2433
|
+
//#region ../../packages/substrate-filter/src/expr-check.ts
|
|
2434
|
+
/**
|
|
2435
|
+
* Type checker for read-grammar expressions. Infers the type of every node,
|
|
2436
|
+
* refuses mismatches, unknown functions and fields, and enforces the per-
|
|
2437
|
+
* expression limits (`EXPR_LIMITS`). `int64` mixed with `number` widens to
|
|
2438
|
+
* `number`; a safe-integer literal beside an int64 stays int64.
|
|
2439
|
+
*/
|
|
2440
|
+
const SIGS = new Map(EXPR_FUNCTIONS.map((s) => [s.name, s]));
|
|
2441
|
+
const isNumeric = (t) => t === "number" || t === "int64";
|
|
2442
|
+
var Refusal = class extends Error {
|
|
2443
|
+
at;
|
|
2444
|
+
constructor(message, at) {
|
|
2445
|
+
super(message);
|
|
2446
|
+
this.at = at;
|
|
2447
|
+
}
|
|
2448
|
+
};
|
|
2449
|
+
const LIKE_TRAILING_ESCAPE = "LIKE pattern must not end with escape character";
|
|
2450
|
+
/** Whether a LIKE pattern ends in an unescaped backslash. */
|
|
2451
|
+
const endsWithEscape = (text) => /(^|[^\\])(\\\\)*\\$/.test(text);
|
|
2452
|
+
function checkExpr(expr, scope, path) {
|
|
2453
|
+
let nodes = 0;
|
|
2454
|
+
const refuse = (msg, at) => {
|
|
2455
|
+
throw new Refusal(`${at.join(".")}: ${msg}`, at);
|
|
2456
|
+
};
|
|
2457
|
+
const checkDepth = (depth, at) => {
|
|
2458
|
+
if (depth > EXPR_LIMITS.maxDepth) refuse(`expression is deeper than maxDepth (${EXPR_LIMITS.maxDepth})`, at);
|
|
2459
|
+
};
|
|
2460
|
+
const count = (at) => {
|
|
2461
|
+
if (++nodes > EXPR_LIMITS.maxNodesPerExpr) refuse(`expression has more than maxNodesPerExpr (${EXPR_LIMITS.maxNodesPerExpr}) nodes`, at);
|
|
2462
|
+
};
|
|
2463
|
+
const unify = (items, what, at) => {
|
|
2464
|
+
const types = items.map((x) => x.type).filter((t) => t !== null);
|
|
2465
|
+
if (types.length === 0) return refuse(`${what}: cannot infer a type from null alone`, at);
|
|
2466
|
+
if (types.every(isNumeric)) {
|
|
2467
|
+
if (types.includes("int64")) {
|
|
2468
|
+
if (items.some((x) => x.bigLiteral)) refuse(`${what}: a number beside an int64 must be a safe integer`, at);
|
|
2469
|
+
return items.every((x) => x.type === "int64" || x.type === null || x.intLiteral) ? "int64" : "number";
|
|
2470
|
+
}
|
|
2471
|
+
return "number";
|
|
2169
2472
|
}
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2473
|
+
const first = types[0];
|
|
2474
|
+
if (types.some((t) => t !== first)) refuse(`${what}: arguments must share a type, got ${types.join(" and ")}`, at);
|
|
2475
|
+
return first;
|
|
2476
|
+
};
|
|
2477
|
+
const need = (a, want, what, at) => {
|
|
2478
|
+
if (a.type === null) return;
|
|
2479
|
+
if (!(want === "number" ? isNumeric(a.type) : a.type === want)) refuse(`${what}: expected ${want}, got ${a.type}`, at);
|
|
2480
|
+
};
|
|
2481
|
+
const lit = (arg, what, at) => {
|
|
2482
|
+
if (typeof arg === "object" && arg !== null && typeof arg.lit === "string") return arg.lit;
|
|
2483
|
+
return refuse(`${what}: expected a { lit } string`, at);
|
|
2484
|
+
};
|
|
2485
|
+
const walk = (e, at, depth) => {
|
|
2486
|
+
if (depth > EXPR_LIMITS.maxDepth) refuse(`expression is deeper than maxDepth (${EXPR_LIMITS.maxDepth})`, at);
|
|
2487
|
+
count(at);
|
|
2488
|
+
if (e === null) return {
|
|
2489
|
+
t: { type: null },
|
|
2490
|
+
depth
|
|
2491
|
+
};
|
|
2492
|
+
if (typeof e === "boolean") return {
|
|
2493
|
+
t: { type: "boolean" },
|
|
2494
|
+
depth
|
|
2495
|
+
};
|
|
2496
|
+
if (typeof e === "number") {
|
|
2497
|
+
if (!Number.isFinite(e)) return refuse("number literal must be finite", at);
|
|
2498
|
+
return {
|
|
2499
|
+
t: {
|
|
2500
|
+
type: "number",
|
|
2501
|
+
intLiteral: Number.isSafeInteger(e),
|
|
2502
|
+
bigLiteral: Number.isInteger(e) && !Number.isSafeInteger(e)
|
|
2503
|
+
},
|
|
2504
|
+
depth
|
|
2505
|
+
};
|
|
2173
2506
|
}
|
|
2174
|
-
if (
|
|
2175
|
-
|
|
2176
|
-
if (
|
|
2507
|
+
if (typeof e === "string") {
|
|
2508
|
+
const t = scope.field(e);
|
|
2509
|
+
if (t === void 0) return refuse(`unknown field '${e}'`, at);
|
|
2510
|
+
return {
|
|
2511
|
+
t: { type: t },
|
|
2512
|
+
depth
|
|
2513
|
+
};
|
|
2177
2514
|
}
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
}
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2515
|
+
if (typeof e !== "object" || Array.isArray(e)) return refuse("not an expression", at);
|
|
2516
|
+
const keys = Object.keys(e);
|
|
2517
|
+
if (keys.length !== 1) return refuse(`an expression object has exactly one key, got ${keys.length}`, at);
|
|
2518
|
+
const key = keys[0];
|
|
2519
|
+
const val = e[key];
|
|
2520
|
+
if (key === "lit") return typeof val === "string" ? {
|
|
2521
|
+
t: { type: "string" },
|
|
2522
|
+
depth
|
|
2523
|
+
} : refuse("lit takes a string", at);
|
|
2524
|
+
if (key === "int64") return typeof val === "string" && parseInt64(val) !== void 0 ? {
|
|
2525
|
+
t: { type: "int64" },
|
|
2526
|
+
depth
|
|
2527
|
+
} : refuse(`int64 takes a decimal string within the int64 range, got ${JSON.stringify(val)}`, at);
|
|
2528
|
+
const sig = SIGS.get(key);
|
|
2529
|
+
if (!sig) return refuse(`unknown function '${key}'`, at);
|
|
2530
|
+
if (!Array.isArray(val)) return refuse(`'${key}' takes an array of arguments`, at);
|
|
2531
|
+
const fnAt = [...at, key];
|
|
2532
|
+
let maxDepth = depth;
|
|
2533
|
+
const arg = (a, idx) => {
|
|
2534
|
+
const r = walk(a, [...fnAt, String(idx)], depth + 1);
|
|
2535
|
+
maxDepth = Math.max(maxDepth, r.depth);
|
|
2536
|
+
return r.t;
|
|
2537
|
+
};
|
|
2538
|
+
const arity = (n) => {
|
|
2539
|
+
const [lo, hi] = Array.isArray(n) ? [n[0], n[1]] : [n, n];
|
|
2540
|
+
if (val.length < lo || hi !== void 0 && val.length > hi) refuse(`'${key}' takes ${hi === lo ? lo : `at least ${lo}`} argument(s), got ${val.length}`, fnAt);
|
|
2541
|
+
};
|
|
2542
|
+
const result = (type) => ({
|
|
2543
|
+
t: { type },
|
|
2544
|
+
depth: maxDepth
|
|
2545
|
+
});
|
|
2546
|
+
const where = (i) => [...fnAt, String(i)];
|
|
2547
|
+
if (key === "case") {
|
|
2548
|
+
arity([2]);
|
|
2549
|
+
const branches = [];
|
|
2550
|
+
for (let i = 0; i < val.length - 1; i++) {
|
|
2551
|
+
const pair = val[i];
|
|
2552
|
+
if (!Array.isArray(pair) || pair.length !== 2) return refuse(`'case' branch ${i} must be [cond, then]`, where(i));
|
|
2553
|
+
need(arg(pair[0], `${i}.0`), "boolean", `'case' condition ${i}`, where(i));
|
|
2554
|
+
branches.push(arg(pair[1], `${i}.1`));
|
|
2555
|
+
}
|
|
2556
|
+
branches.push(arg(val[val.length - 1], val.length - 1));
|
|
2557
|
+
return result(unify(branches, "'case' branches", fnAt));
|
|
2558
|
+
}
|
|
2559
|
+
if (key === "coalesce") {
|
|
2560
|
+
arity([1]);
|
|
2561
|
+
return result(unify(val.map((a, i) => arg(a, i)), "'coalesce'", fnAt));
|
|
2562
|
+
}
|
|
2563
|
+
if (key === "in") {
|
|
2564
|
+
arity([2]);
|
|
2565
|
+
unify(val.map((a, i) => arg(a, i)), "'in'", fnAt);
|
|
2566
|
+
return result("boolean");
|
|
2567
|
+
}
|
|
2568
|
+
if (key === "and" || key === "or") {
|
|
2569
|
+
arity([1]);
|
|
2570
|
+
for (const [i, a] of val.entries()) need(arg(a, i), "boolean", `'${key}'`, where(i));
|
|
2571
|
+
return result("boolean");
|
|
2572
|
+
}
|
|
2573
|
+
if (key === "concat") {
|
|
2574
|
+
arity([1]);
|
|
2575
|
+
for (const [i, a] of val.entries()) need(arg(a, i), "string", "'concat'", where(i));
|
|
2576
|
+
return result("string");
|
|
2577
|
+
}
|
|
2578
|
+
const kinds = sig.args;
|
|
2579
|
+
arity(kinds.length);
|
|
2580
|
+
const group = [];
|
|
2581
|
+
for (const [i, a] of val.entries()) {
|
|
2582
|
+
const kind = kinds[i];
|
|
2583
|
+
if (kind === "unit" || kind === "pattern" || kind === "likePattern") {
|
|
2584
|
+
const text = lit(a, `'${key}' ${kind}`, where(i));
|
|
2585
|
+
checkDepth(depth + 1, where(i));
|
|
2586
|
+
count(where(i));
|
|
2587
|
+
if (kind === "unit" && !DATE_UNITS.includes(text)) refuse(`'${key}' unit '${text}' is not one of ${DATE_UNITS.join(", ")}`, where(i));
|
|
2588
|
+
if (kind === "likePattern" && endsWithEscape(text)) refuse(LIKE_TRAILING_ESCAPE, where(i));
|
|
2589
|
+
if (kind === "pattern") {
|
|
2590
|
+
const why = checkRegexSubset(text);
|
|
2591
|
+
if (why) refuse(`'${key}' regex is outside the portable subset: ${why}`, where(i));
|
|
2592
|
+
if (key === "regexpReplace" && capturesWithAlternation(text)) refuse(`'regexpReplace' regex may use capturing groups or '|', not both: the engines pick different captures`, where(i));
|
|
2593
|
+
}
|
|
2594
|
+
maxDepth = Math.max(maxDepth, depth + 1);
|
|
2595
|
+
continue;
|
|
2596
|
+
}
|
|
2597
|
+
const t = arg(a, i);
|
|
2598
|
+
if (kind !== "any" && !(kind === "int64" && t.intLiteral)) need(t, kind, `'${key}' argument ${i}`, where(i));
|
|
2599
|
+
if (kind === "any" || kind === "number" && sig.returns === "any") group.push(t);
|
|
2600
|
+
}
|
|
2601
|
+
if (sig.returns === "any") return result(unify(group, `'${key}'`, fnAt));
|
|
2602
|
+
if (group.length > 1) unify(group, `'${key}'`, fnAt);
|
|
2603
|
+
return result(sig.returns);
|
|
2604
|
+
};
|
|
2605
|
+
try {
|
|
2606
|
+
const { t, depth } = walk(expr, path, 1);
|
|
2607
|
+
if (t.type === null) return refuse("cannot infer a type from null alone", path);
|
|
2608
|
+
return {
|
|
2609
|
+
ok: true,
|
|
2610
|
+
type: t.type,
|
|
2611
|
+
nodes,
|
|
2612
|
+
depth
|
|
2613
|
+
};
|
|
2614
|
+
} catch (e) {
|
|
2615
|
+
if (e instanceof Refusal) return {
|
|
2616
|
+
ok: false,
|
|
2617
|
+
code: "expr_invalid",
|
|
2618
|
+
message: e.message,
|
|
2619
|
+
path: e.at
|
|
2620
|
+
};
|
|
2621
|
+
throw e;
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
//#endregion
|
|
2626
|
+
//#region ../../packages/substrate-filter/src/json-path.ts
|
|
2627
|
+
/**
|
|
2628
|
+
* Path-addressed predicates on json fields.
|
|
2629
|
+
*
|
|
2630
|
+
* `{ values: { path: ['year'], gt: 1990 } }`. The data keys live inside the
|
|
2631
|
+
* `path` ARRAY, so a key an end user invented — `contains`, `in`, `has` — can
|
|
2632
|
+
* never be read as an operator. An array-valued `path` is also what tells this apart from a
|
|
2190
2633
|
* struct that declares a sub-field named `path`: that one's value is an object.
|
|
2191
2634
|
*
|
|
2192
2635
|
* This file is the reference semantics. The pg compiler
|
|
@@ -2196,7 +2639,7 @@ function whereContainsOperator(where, opName) {
|
|
|
2196
2639
|
const JSON_PATH_MAX_DEPTH = 16;
|
|
2197
2640
|
/** Most `in` / `nin` members both substrates accept (pg binds two per member). */
|
|
2198
2641
|
const JSON_PATH_MAX_IN = 1e4;
|
|
2199
|
-
const OPS = new Set(JSON_PATH_OPS$1);
|
|
2642
|
+
const OPS$1 = new Set(JSON_PATH_OPS$1);
|
|
2200
2643
|
const isScalar = (v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
2201
2644
|
function isJsonPathPredicate(p) {
|
|
2202
2645
|
return p !== null && typeof p === "object" && !Array.isArray(p) && Array.isArray(p.path);
|
|
@@ -2209,7 +2652,7 @@ function jsonPathProblem(path) {
|
|
|
2209
2652
|
}
|
|
2210
2653
|
/** Why `operand` cannot go with `op` on a json path, or null when it can. */
|
|
2211
2654
|
function jsonPathOperandProblem(op, operand) {
|
|
2212
|
-
if (!OPS.has(op)) return "is not a recognised filter operator";
|
|
2655
|
+
if (!OPS$1.has(op)) return "is not a recognised filter operator";
|
|
2213
2656
|
if (operand instanceof Date) return "cannot take a Date — json has no date type, pass an ISO string";
|
|
2214
2657
|
if ((Array.isArray(operand) ? operand : [operand]).some((m) => typeof m === "number" && !Number.isFinite(m))) return "requires finite numbers — json has no NaN or Infinity";
|
|
2215
2658
|
switch (op) {
|
|
@@ -2320,103 +2763,215 @@ function applyJsonPathOp(op, value, operand) {
|
|
|
2320
2763
|
}
|
|
2321
2764
|
|
|
2322
2765
|
//#endregion
|
|
2323
|
-
//#region ../../packages/substrate-filter/src/
|
|
2766
|
+
//#region ../../packages/substrate-filter/src/expr-scope.ts
|
|
2767
|
+
/** `number`, `int16` and `int32` (as `int64`), `int64`, `string`, `boolean` and `date` fields, and a crossRef's id (a string); json, list and struct fields are not usable. */
|
|
2768
|
+
function exprTypeOfField(def) {
|
|
2769
|
+
if (def === void 0) return void 0;
|
|
2770
|
+
if (def.kind === "crossRef") return "string";
|
|
2771
|
+
if (def.kind !== "scalar") return void 0;
|
|
2772
|
+
if (def.type === "json") return void 0;
|
|
2773
|
+
return def.type === "int16" || def.type === "int32" ? "int64" : def.type;
|
|
2774
|
+
}
|
|
2775
|
+
/**
|
|
2776
|
+
* The type of a date or number metadata name (`createdAt`, `updatedAt`, `seq`), which an
|
|
2777
|
+
* expression reads as metadata; the names are reserved, so no declared field shares them. The
|
|
2778
|
+
* string metadata names are not expression fields.
|
|
2779
|
+
*/
|
|
2780
|
+
function metadataExprType(name) {
|
|
2781
|
+
if (typeof name !== "string" || !Object.hasOwn(METADATA_FILTER_FIELDS, name)) return void 0;
|
|
2782
|
+
const t = exprTypeOfField(METADATA_FILTER_FIELDS[name]);
|
|
2783
|
+
return t === "date" || t === "number" ? t : void 0;
|
|
2784
|
+
}
|
|
2785
|
+
const lookups = /* @__PURE__ */ new WeakMap();
|
|
2786
|
+
/**
|
|
2787
|
+
* A stable `fieldType` lookup per `fieldDefOf`: the evaluator keys its per-node
|
|
2788
|
+
* type cache on the lookup, so one read must reuse one.
|
|
2789
|
+
*/
|
|
2790
|
+
function fieldTypeLookup(fieldDefOf) {
|
|
2791
|
+
if (fieldDefOf === void 0) return metadataExprType;
|
|
2792
|
+
let f = lookups.get(fieldDefOf);
|
|
2793
|
+
if (f === void 0) {
|
|
2794
|
+
f = (name) => metadataExprType(name) ?? exprTypeOfField(fieldDefOf(name));
|
|
2795
|
+
lookups.set(fieldDefOf, f);
|
|
2796
|
+
}
|
|
2797
|
+
return f;
|
|
2798
|
+
}
|
|
2799
|
+
/**
|
|
2800
|
+
* The expression type of a frame `_agg`'s value: `number` for count, sum and
|
|
2801
|
+
* avg, else the type of the field it reads on the far type. `near` is the seed
|
|
2802
|
+
* type; `fieldDef` resolves any type's field.
|
|
2803
|
+
*/
|
|
2804
|
+
function walkAggExprType(spec, near, fieldDef) {
|
|
2805
|
+
const rec = spec;
|
|
2806
|
+
if (Object.hasOwn(rec, "count") || Object.hasOwn(rec, "sum") || Object.hasOwn(rec, "avg")) return "number";
|
|
2807
|
+
const latest = Object.hasOwn(rec, "latest") ? rec.latest : void 0;
|
|
2808
|
+
const field = latest !== void 0 ? latest.value : rec.min ?? rec.max;
|
|
2809
|
+
let far;
|
|
2810
|
+
if (spec.in !== void 0) far = /^[^/]+\/([^./]+)\./.exec(spec.in)?.[1];
|
|
2811
|
+
else {
|
|
2812
|
+
const def = fieldDef(near, spec.out ?? "");
|
|
2813
|
+
far = def?.kind === "crossRef" ? def.type : void 0;
|
|
2814
|
+
}
|
|
2815
|
+
return far === void 0 || typeof field !== "string" ? void 0 : exprTypeOfField(fieldDef(far, field));
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
//#endregion
|
|
2819
|
+
//#region ../../packages/substrate-filter/src/extension-operators.ts
|
|
2324
2820
|
/**
|
|
2325
|
-
*
|
|
2326
|
-
*
|
|
2821
|
+
* Extension-operator registry. Spec: §5.2.
|
|
2822
|
+
*
|
|
2823
|
+
* Substrate plugins (e.g. substrate-recall's `embeddingSimilar`) register custom
|
|
2824
|
+
* where-clause operators here at compose-time. The registry is module-scope
|
|
2825
|
+
* state shared between:
|
|
2826
|
+
* - substrate-core's filter validator (accepts the operator key during
|
|
2827
|
+
* where-clause validation)
|
|
2828
|
+
* - substrate-dataset-pg's pre-resolver (dispatches op.execute(...) before SQL
|
|
2829
|
+
* translation, substitutes the result into an `id IN (...)` clause)
|
|
2830
|
+
*
|
|
2831
|
+
* Tests should call `clearExtensionOperators()` in afterEach to prevent
|
|
2832
|
+
* cross-test leakage.
|
|
2833
|
+
*
|
|
2834
|
+
* The backing Maps hang off `globalThis` under `Symbol.for` keys because a
|
|
2835
|
+
* process can hold more than one copy of this package (a plugin resolving its
|
|
2836
|
+
* own `substrate-filter`); module-scope Maps would split the registry, and an
|
|
2837
|
+
* operator registered through one copy would be silently unknown to the other.
|
|
2838
|
+
*
|
|
2839
|
+
* The keys carry `.v1` so two copies with incompatible `FilterOperatorDef`
|
|
2840
|
+
* shapes fall back to separate registries rather than reading each other's
|
|
2841
|
+
* entries. Bump the suffix when `FilterOperatorDef` changes shape.
|
|
2327
2842
|
*/
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
this.operator = operator;
|
|
2334
|
-
}
|
|
2335
|
-
};
|
|
2843
|
+
const REGISTRY_KEY = Symbol.for("substrate.filter.extensionOperators.v1");
|
|
2844
|
+
const OWNERS_KEY = Symbol.for("substrate.filter.computedFieldOwners.v1");
|
|
2845
|
+
const host = globalThis;
|
|
2846
|
+
const extensionOperators = host[REGISTRY_KEY] ?? /* @__PURE__ */ new Map();
|
|
2847
|
+
host[REGISTRY_KEY] = extensionOperators;
|
|
2336
2848
|
/**
|
|
2337
|
-
*
|
|
2338
|
-
*
|
|
2339
|
-
*
|
|
2849
|
+
* computedField → owning operator name. An operator may declare
|
|
2850
|
+
* `computedFields` (e.g. embeddingSimilar owns `_score` + `_vector`): fields
|
|
2851
|
+
* that only exist on a row when the operator runs. The engine's computed-field
|
|
2852
|
+
* gate reads this map to refuse projecting a computed field without its operator in
|
|
2853
|
+
* the where-clause.
|
|
2854
|
+
*/
|
|
2855
|
+
const computedFieldOwners = host[OWNERS_KEY] ?? /* @__PURE__ */ new Map();
|
|
2856
|
+
host[OWNERS_KEY] = computedFieldOwners;
|
|
2857
|
+
/** Where-clause keys the grammar already owns: combinators, ids and row metadata. */
|
|
2858
|
+
function isReservedName(name) {
|
|
2859
|
+
return [
|
|
2860
|
+
"AND",
|
|
2861
|
+
"OR",
|
|
2862
|
+
"NOT",
|
|
2863
|
+
"shortId",
|
|
2864
|
+
"_metadata"
|
|
2865
|
+
].includes(name) || Object.hasOwn(METADATA_FILTER_FIELDS, name) || Object.hasOwn(METADATA_AUTHOR_FIELDS, name);
|
|
2866
|
+
}
|
|
2867
|
+
/**
|
|
2868
|
+
* Register a custom where-clause operator under `name`. Throws if `name` is a
|
|
2869
|
+
* reserved where-clause key or already registered, or if any of its
|
|
2870
|
+
* `computedFields` is already claimed by another operator. `name` comes from the Record key in
|
|
2871
|
+
* `KernelPlugin.filterOperators`.
|
|
2340
2872
|
*
|
|
2341
|
-
*
|
|
2873
|
+
* Computed-field collisions throw rather than overwrite: silently overwriting
|
|
2874
|
+
* ownership would make the computed-field gate name the wrong operator.
|
|
2342
2875
|
*/
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2876
|
+
function registerExtensionOperator(name, op) {
|
|
2877
|
+
if (isReservedName(name)) throw new Error(`Filter operator name '${name}' is reserved by the where-clause grammar`);
|
|
2878
|
+
if (extensionOperators.has(name)) throw new Error(`Filter operator '${name}' already registered`);
|
|
2879
|
+
for (const cf of op.computedFields ?? []) {
|
|
2880
|
+
const existing = computedFieldOwners.get(cf);
|
|
2881
|
+
if (existing !== void 0) throw new Error(`Computed field '${cf}' is already owned by operator '${existing}'; '${name}' cannot also claim it`);
|
|
2882
|
+
}
|
|
2883
|
+
extensionOperators.set(name, op);
|
|
2884
|
+
for (const cf of op.computedFields ?? []) computedFieldOwners.set(cf, name);
|
|
2349
2885
|
}
|
|
2350
|
-
/**
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2886
|
+
/**
|
|
2887
|
+
* The operator name owning a computed projection field, or undefined when no
|
|
2888
|
+
* registered operator claims it. Used by the engine's computed-field gate to refuse
|
|
2889
|
+
* projecting a computed field without its operator present in where.
|
|
2890
|
+
*/
|
|
2891
|
+
function computedFieldOwner(field) {
|
|
2892
|
+
return computedFieldOwners.get(field);
|
|
2893
|
+
}
|
|
2894
|
+
/**
|
|
2895
|
+
* True when any registered operator declares computed fields. Common-case
|
|
2896
|
+
* fast-path for the engine's computed-field gate (no semantic operator composed → skip
|
|
2897
|
+
* the per-frame fields walk entirely); the per-field `computedFieldOwner`
|
|
2898
|
+
* check remains authoritative.
|
|
2899
|
+
*/
|
|
2900
|
+
function hasComputedFields() {
|
|
2901
|
+
return computedFieldOwners.size > 0;
|
|
2902
|
+
}
|
|
2903
|
+
/** Look up a registered extension operator by name. Returns undefined when absent. */
|
|
2904
|
+
function getExtensionOperator(name) {
|
|
2905
|
+
return extensionOperators.get(name);
|
|
2906
|
+
}
|
|
2907
|
+
/**
|
|
2908
|
+
* Sorted list of registered extension-operator names. Surfaced by
|
|
2909
|
+
* `/substrate/v1/onboard` so HTTP consumers can branch on "is `embeddingSimilar`
|
|
2910
|
+
* composed?" without sniffing error strings from a failed query.
|
|
2911
|
+
*/
|
|
2912
|
+
function listExtensionOperators() {
|
|
2913
|
+
return Array.from(extensionOperators.keys()).sort();
|
|
2914
|
+
}
|
|
2915
|
+
/** Remove `name` and the computed fields it owns. A no-op when `name` is not registered. */
|
|
2916
|
+
function unregisterExtensionOperator(name) {
|
|
2917
|
+
const op = extensionOperators.get(name);
|
|
2918
|
+
if (op === void 0) return;
|
|
2919
|
+
extensionOperators.delete(name);
|
|
2920
|
+
for (const cf of op.computedFields ?? []) if (computedFieldOwners.get(cf) === name) computedFieldOwners.delete(cf);
|
|
2921
|
+
}
|
|
2922
|
+
/**
|
|
2923
|
+
* True when any key in the where-tree (root, nested under AND/OR/NOT, or
|
|
2924
|
+
* inside a struct-field sub-clause) is a registered extension operator.
|
|
2925
|
+
* Used by the subscribe path to refuse semantic filters at registration —
|
|
2926
|
+
* extension operators have no incremental evaluation, so a subscription
|
|
2927
|
+
* carrying one would silently degrade. Leaf predicate objects ({ eq: 'x' }) recurse harmlessly: their
|
|
2928
|
+
* keys are not registered operators and their values are not objects.
|
|
2929
|
+
*/
|
|
2930
|
+
function whereContainsExtensionOperator(where) {
|
|
2931
|
+
if (Array.isArray(where)) return where.some((w) => whereContainsExtensionOperator(w));
|
|
2932
|
+
if (where === null || typeof where !== "object") return false;
|
|
2933
|
+
for (const [key, value] of Object.entries(where)) {
|
|
2359
2934
|
if (key === "AND" || key === "OR") {
|
|
2360
|
-
if (Array.isArray(value)
|
|
2361
|
-
else out[key] = value;
|
|
2935
|
+
if (Array.isArray(value) && value.some((w) => whereContainsExtensionOperator(w))) return true;
|
|
2362
2936
|
continue;
|
|
2363
2937
|
}
|
|
2364
2938
|
if (key === "NOT") {
|
|
2365
|
-
if (
|
|
2366
|
-
else out[key] = value;
|
|
2939
|
+
if (whereContainsExtensionOperator(value)) return true;
|
|
2367
2940
|
continue;
|
|
2368
2941
|
}
|
|
2369
|
-
|
|
2370
|
-
if (
|
|
2371
|
-
|
|
2372
|
-
continue;
|
|
2942
|
+
if (getExtensionOperator(key) !== void 0) return true;
|
|
2943
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
2944
|
+
if (whereContainsExtensionOperator(value)) return true;
|
|
2373
2945
|
}
|
|
2374
|
-
const result = await op.execute(value, context);
|
|
2375
|
-
if (!isHitsResult(result)) throw new ExtensionOperatorResultError(key);
|
|
2376
|
-
const meta = buildMeta(result.hits);
|
|
2377
|
-
holder.current = meta;
|
|
2378
|
-
idClauses.push({ id: { in: [...meta.orderedIds] } });
|
|
2379
2946
|
}
|
|
2380
|
-
|
|
2381
|
-
if (out.id !== void 0) idClauses.unshift({ id: out.id });
|
|
2382
|
-
if (idClauses.length === 1) return {
|
|
2383
|
-
...out,
|
|
2384
|
-
...idClauses[0]
|
|
2385
|
-
};
|
|
2386
|
-
delete out.id;
|
|
2387
|
-
return { AND: [out, ...idClauses] };
|
|
2388
|
-
}
|
|
2389
|
-
/** Narrow an operator result to the hits envelope. */
|
|
2390
|
-
function isHitsResult(result) {
|
|
2391
|
-
return result !== null && typeof result === "object" && Array.isArray(result.hits);
|
|
2947
|
+
return false;
|
|
2392
2948
|
}
|
|
2393
2949
|
/**
|
|
2394
|
-
*
|
|
2395
|
-
*
|
|
2396
|
-
*
|
|
2397
|
-
*
|
|
2950
|
+
* True when the where-tree contains a key equal to `opName` in operator
|
|
2951
|
+
* position (root, AND/OR/NOT combinator, struct-field sub-clause, or array
|
|
2952
|
+
* element). The named variant of `whereContainsExtensionOperator`: the
|
|
2953
|
+
* computed-field gate needs the operator that OWNS the projected field present,
|
|
2954
|
+
* not merely SOME registered operator. Same structural walk; matches by name
|
|
2955
|
+
* rather than registry membership.
|
|
2398
2956
|
*/
|
|
2399
|
-
function
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
for (const
|
|
2403
|
-
if (
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
}
|
|
2957
|
+
function whereContainsOperator(where, opName) {
|
|
2958
|
+
if (Array.isArray(where)) return where.some((w) => whereContainsOperator(w, opName));
|
|
2959
|
+
if (where === null || typeof where !== "object") return false;
|
|
2960
|
+
for (const [key, value] of Object.entries(where)) {
|
|
2961
|
+
if (key === "AND" || key === "OR") {
|
|
2962
|
+
if (Array.isArray(value) && value.some((w) => whereContainsOperator(w, opName))) return true;
|
|
2963
|
+
continue;
|
|
2964
|
+
}
|
|
2965
|
+
if (key === "NOT") {
|
|
2966
|
+
if (whereContainsOperator(value, opName)) return true;
|
|
2967
|
+
continue;
|
|
2968
|
+
}
|
|
2969
|
+
if (key === opName) return true;
|
|
2970
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
2971
|
+
if (whereContainsOperator(value, opName)) return true;
|
|
2972
|
+
}
|
|
2415
2973
|
}
|
|
2416
|
-
return
|
|
2417
|
-
orderedIds,
|
|
2418
|
-
computed
|
|
2419
|
-
};
|
|
2974
|
+
return false;
|
|
2420
2975
|
}
|
|
2421
2976
|
|
|
2422
2977
|
//#endregion
|
|
@@ -2641,21 +3196,20 @@ function applyAgg(rows, fn, fieldDefOf) {
|
|
|
2641
3196
|
const v = one === void 0 ? void 0 : getFieldValue(one, field);
|
|
2642
3197
|
return v == null || storedMicros(v) === void 0 ? null : v;
|
|
2643
3198
|
}
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
n++;
|
|
2651
|
-
}
|
|
3199
|
+
return numericAgg(rows.map((r) => getFieldValue(r, field)), op);
|
|
3200
|
+
}
|
|
3201
|
+
/** avg/sum/min/max over the numbers among `values` (the rest drop out), null over none; float64, a sum past its range is null. */
|
|
3202
|
+
function numericAgg(values, op) {
|
|
3203
|
+
const nums = values.filter((v) => typeof v === "number");
|
|
3204
|
+
const n = nums.length;
|
|
2652
3205
|
if (n === 0) return null;
|
|
3206
|
+
let acc = 0;
|
|
3207
|
+
nums.forEach((v, i) => {
|
|
3208
|
+
acc = i === 0 ? v : op === "min" ? Math.min(acc, v) : op === "max" ? Math.max(acc, v) : acc + v;
|
|
3209
|
+
});
|
|
2653
3210
|
if (!Number.isFinite(acc) && (op === "sum" || op === "avg")) {
|
|
2654
3211
|
let scaled = 0;
|
|
2655
|
-
for (const
|
|
2656
|
-
const v = getFieldValue(r, field);
|
|
2657
|
-
if (typeof v === "number") scaled += v * 2 ** -64;
|
|
2658
|
-
}
|
|
3212
|
+
for (const v of nums) scaled += v * 2 ** -64;
|
|
2659
3213
|
const out = (op === "avg" ? scaled / n : scaled) * 2 ** 64;
|
|
2660
3214
|
return Number.isFinite(out) ? out : null;
|
|
2661
3215
|
}
|
|
@@ -2752,6 +3306,27 @@ function isDateField(field, fieldDefOf) {
|
|
|
2752
3306
|
const def = METADATA_FILTER_FIELDS[field] ?? fieldDefOf?.(field);
|
|
2753
3307
|
return def?.kind === "scalar" && def.type === "date";
|
|
2754
3308
|
}
|
|
3309
|
+
function isInt64Field(field, fieldDefOf) {
|
|
3310
|
+
const def = fieldDefOf?.(field);
|
|
3311
|
+
return def?.kind === "scalar" && def.type === "int64";
|
|
3312
|
+
}
|
|
3313
|
+
/**
|
|
3314
|
+
* A predicate on an int64 field with its operands read as bigints, the way pg
|
|
3315
|
+
* casts them. An operand that is not an int64 is a caller error; `null` and
|
|
3316
|
+
* `$ref` operands pass through.
|
|
3317
|
+
*/
|
|
3318
|
+
function int64Predicate(field, predicate) {
|
|
3319
|
+
if (predicate === null || typeof predicate !== "object" || Array.isArray(predicate)) return predicate;
|
|
3320
|
+
const one = (op, v) => {
|
|
3321
|
+
if (v === null || isRefOperand(v)) return v;
|
|
3322
|
+
const n = parseInt64(v);
|
|
3323
|
+
if (n === void 0) throw new UnsupportedFilterError(field, op, "requires an int64 operand");
|
|
3324
|
+
return n;
|
|
3325
|
+
};
|
|
3326
|
+
const out = {};
|
|
3327
|
+
for (const [op, v] of Object.entries(predicate)) out[op] = Array.isArray(v) && (op === "in" || op === "nin" || op === "between") ? v.map((e) => one(op, e)) : KNOWN_OPS.has(op) ? one(op, v) : v;
|
|
3328
|
+
return out;
|
|
3329
|
+
}
|
|
2755
3330
|
const KNOWN_OPS = /* @__PURE__ */ new Set([
|
|
2756
3331
|
"eq",
|
|
2757
3332
|
"neq",
|
|
@@ -2765,6 +3340,8 @@ const KNOWN_OPS = /* @__PURE__ */ new Set([
|
|
|
2765
3340
|
"contains",
|
|
2766
3341
|
"startsWith",
|
|
2767
3342
|
"endsWith",
|
|
3343
|
+
"like",
|
|
3344
|
+
"notLike",
|
|
2768
3345
|
"has"
|
|
2769
3346
|
]);
|
|
2770
3347
|
/**
|
|
@@ -2787,6 +3364,7 @@ function assertWhereHonourable(where) {
|
|
|
2787
3364
|
if (where === null || typeof where !== "object" || Array.isArray(where)) return;
|
|
2788
3365
|
if (Object.hasOwn(where, "_agg")) return;
|
|
2789
3366
|
for (const [key, child] of Object.entries(where)) {
|
|
3367
|
+
if (key === "$expr") continue;
|
|
2790
3368
|
if (key === "AND" || key === "OR") {
|
|
2791
3369
|
if (Array.isArray(child)) for (const sub of child) assertWhereHonourable(sub);
|
|
2792
3370
|
continue;
|
|
@@ -2830,7 +3408,11 @@ function assertFieldPredicateHonourable(field, predicate) {
|
|
|
2830
3408
|
break;
|
|
2831
3409
|
case "contains":
|
|
2832
3410
|
case "startsWith":
|
|
2833
|
-
case "endsWith":
|
|
3411
|
+
case "endsWith":
|
|
3412
|
+
case "like":
|
|
3413
|
+
case "notLike":
|
|
3414
|
+
if (typeof operand !== "string") throw new UnsupportedFilterError(field, op, "requires a string value");
|
|
3415
|
+
if ((op === "like" || op === "notLike") && endsWithEscape(operand)) throw new UnsupportedFilterError(field, op, LIKE_TRAILING_ESCAPE);
|
|
2834
3416
|
}
|
|
2835
3417
|
}
|
|
2836
3418
|
/**
|
|
@@ -2876,6 +3458,8 @@ function evaluateMembers(row, where, fieldDefOf, walk, bound) {
|
|
|
2876
3458
|
}
|
|
2877
3459
|
} else if (k === "NOT") {
|
|
2878
3460
|
if (evaluateWhere(row, child, fieldDefOf, walk)) return false;
|
|
3461
|
+
} else if (k === "$expr") {
|
|
3462
|
+
if (evalExpr(child, row, { fieldType: fieldTypeLookup(fieldDefOf) }) !== true) return false;
|
|
2879
3463
|
} else if (k === "_walk") {
|
|
2880
3464
|
if (walk === void 0) throw new UnsupportedFilterError(k, "<none>", "walks need a walk context");
|
|
2881
3465
|
const node = child;
|
|
@@ -2883,7 +3467,8 @@ function evaluateMembers(row, where, fieldDefOf, walk, bound) {
|
|
|
2883
3467
|
} else {
|
|
2884
3468
|
const asDate = isDateField(k, fieldDefOf);
|
|
2885
3469
|
const ref = walk === void 0 ? void 0 : (r) => resolveRef(r, asDate, walk);
|
|
2886
|
-
|
|
3470
|
+
const pred = isInt64Field(k, fieldDefOf) ? int64Predicate(k, child) : child;
|
|
3471
|
+
if (!evaluateFieldPredicate(k, getFieldValue(row, k), pred, asDate, ref)) return false;
|
|
2887
3472
|
}
|
|
2888
3473
|
return true;
|
|
2889
3474
|
}
|
|
@@ -2952,6 +3537,12 @@ function applyOp(field, op, value, operand, asDate) {
|
|
|
2952
3537
|
if (op === "startsWith") return v.startsWith(o);
|
|
2953
3538
|
return v.endsWith(o);
|
|
2954
3539
|
}
|
|
3540
|
+
case "like":
|
|
3541
|
+
case "notLike":
|
|
3542
|
+
if (typeof operand !== "string") throw new UnsupportedFilterError(field, op, "requires a string value");
|
|
3543
|
+
if (endsWithEscape(operand)) throw new UnsupportedFilterError(field, op, LIKE_TRAILING_ESCAPE);
|
|
3544
|
+
if (typeof value !== "string") return false;
|
|
3545
|
+
return likeMatch(value, operand) === (op === "like");
|
|
2955
3546
|
case "has": return Array.isArray(value) && value.some((e) => equals(e, operand));
|
|
2956
3547
|
default: throw new UnsupportedFilterError(field, op, "is not a recognised filter operator");
|
|
2957
3548
|
}
|
|
@@ -2978,64 +3569,888 @@ function rangeCmp(value, operand, asDate) {
|
|
|
2978
3569
|
return compareFilterValues(value, operand, asDate);
|
|
2979
3570
|
}
|
|
2980
3571
|
/**
|
|
2981
|
-
* Orders two filter values: nullish last, dates (when `asDate`, or either side
|
|
2982
|
-
* is a `Date`) by instant, numbers numerically, anything else as text by code point.
|
|
3572
|
+
* Orders two filter values: nullish last, dates (when `asDate`, or either side
|
|
3573
|
+
* is a `Date`) by instant, numbers numerically, anything else as text by code point.
|
|
3574
|
+
*/
|
|
3575
|
+
function compareFilterValues(a, b, asDate = false) {
|
|
3576
|
+
if (a == null && b == null) return 0;
|
|
3577
|
+
if (a == null) return 1;
|
|
3578
|
+
if (b == null) return -1;
|
|
3579
|
+
if (asDate || a instanceof Date || b instanceof Date) {
|
|
3580
|
+
const at = toMicros(a);
|
|
3581
|
+
const bt = toMicros(b);
|
|
3582
|
+
if (at !== void 0 && bt !== void 0) return at < bt ? -1 : at > bt ? 1 : 0;
|
|
3583
|
+
}
|
|
3584
|
+
if (typeof a === "bigint" || typeof b === "bigint") {
|
|
3585
|
+
const x = parseInt64(a);
|
|
3586
|
+
const y = parseInt64(b);
|
|
3587
|
+
if (x !== void 0 && y !== void 0) return x < y ? -1 : x > y ? 1 : 0;
|
|
3588
|
+
}
|
|
3589
|
+
if (typeof a === "number" && typeof b === "number") return a < b ? -1 : a > b ? 1 : 0;
|
|
3590
|
+
return compareCodePoints(String(a), String(b));
|
|
3591
|
+
}
|
|
3592
|
+
/**
|
|
3593
|
+
* {@link toMicros} for a stored value: `undefined` wherever `substrate_ts` is
|
|
3594
|
+
* NULL, a number included. Only the shared ISO grammar is read; operands are
|
|
3595
|
+
* cast leniently, as pg's `tsParam` casts them.
|
|
3596
|
+
*/
|
|
3597
|
+
function storedMicros(v) {
|
|
3598
|
+
if (v instanceof Date) return toMicros(v);
|
|
3599
|
+
if (typeof v !== "string" || normalizeIsoTimestamp(v) === void 0) return void 0;
|
|
3600
|
+
return toMicros(v);
|
|
3601
|
+
}
|
|
3602
|
+
/**
|
|
3603
|
+
* A row value as pg sorts it: a date-field value `substrate_ts` cannot read, or
|
|
3604
|
+
* a number-field value that is not a number, becomes null and sorts last.
|
|
3605
|
+
*/
|
|
3606
|
+
function asSortValue(field, value, fieldDefOf) {
|
|
3607
|
+
if (value == null) return value;
|
|
3608
|
+
if (isDateField(field, fieldDefOf)) return storedMicros(value) === void 0 ? null : value;
|
|
3609
|
+
const def = METADATA_FILTER_FIELDS[field] ?? fieldDefOf?.(field);
|
|
3610
|
+
if (def?.kind === "scalar" && def.type === "number" && typeof value !== "number") return null;
|
|
3611
|
+
if (def?.kind === "scalar" && def.type === "int64" && parseInt64(value) === void 0) return null;
|
|
3612
|
+
return value;
|
|
3613
|
+
}
|
|
3614
|
+
/**
|
|
3615
|
+
* Microseconds since the epoch, pg's timestamptz precision, as a bigint: a
|
|
3616
|
+
* double is exact only to 2^53 µs (about 1685-2255), and the grammar spans
|
|
3617
|
+
* years 0001-9999. `Date.parse` keeps only milliseconds, so the 4th-6th
|
|
3618
|
+
* fraction digits are added back. A number is read as epoch milliseconds;
|
|
3619
|
+
* anything unparseable is `undefined`.
|
|
3620
|
+
*/
|
|
3621
|
+
function toMicros(v) {
|
|
3622
|
+
if (v instanceof Date) return msToMicros(v.getTime());
|
|
3623
|
+
if (typeof v === "number") return msToMicros(v);
|
|
3624
|
+
if (typeof v !== "string") return void 0;
|
|
3625
|
+
const iso = normalizeIsoTimestamp(v) ?? v;
|
|
3626
|
+
const micros = msToMicros(Date.parse(iso));
|
|
3627
|
+
if (micros === void 0) return void 0;
|
|
3628
|
+
const frac = /[T ][\d:]+\.(\d+)/.exec(iso)?.[1] ?? "";
|
|
3629
|
+
return micros + BigInt(frac.padEnd(6, "0").slice(3, 6));
|
|
3630
|
+
}
|
|
3631
|
+
/** Epoch milliseconds as microseconds, exact: `ms * 1000` as a double is not, past 2^53. */
|
|
3632
|
+
function msToMicros(ms) {
|
|
3633
|
+
if (!Number.isFinite(ms)) return void 0;
|
|
3634
|
+
const whole = Math.floor(ms);
|
|
3635
|
+
return BigInt(whole) * 1000n + BigInt(Math.round((ms - whole) * 1e3));
|
|
3636
|
+
}
|
|
3637
|
+
|
|
3638
|
+
//#endregion
|
|
3639
|
+
//#region ../../packages/substrate-filter/src/regex-vm.ts
|
|
3640
|
+
/**
|
|
3641
|
+
* A Pike VM (Thompson NFA simulation with thread priorities and capture slots)
|
|
3642
|
+
* for exactly the portable subset `checkRegexSubset` accepts. Matching is
|
|
3643
|
+
* O(subject length x program length) with no recursion over the subject, and a
|
|
3644
|
+
* call past MAX_STEPS throws, so a caller's pattern cannot block the event loop. It works over code points; `.`
|
|
3645
|
+
* matches any code point including a newline, `^` and `$` match only at the
|
|
3646
|
+
* subject's start and end. The match is the leftmost-longest one Postgres (POSIX
|
|
3647
|
+
* ARE) reports; among equally long matches, thread priority picks the captures.
|
|
3648
|
+
*/
|
|
3649
|
+
function emit(node, out, base) {
|
|
3650
|
+
switch (node.t) {
|
|
3651
|
+
case "char":
|
|
3652
|
+
out.push({
|
|
3653
|
+
op: "char",
|
|
3654
|
+
c: node.c
|
|
3655
|
+
});
|
|
3656
|
+
return;
|
|
3657
|
+
case "any":
|
|
3658
|
+
case "bol":
|
|
3659
|
+
case "eol":
|
|
3660
|
+
out.push({ op: node.t });
|
|
3661
|
+
return;
|
|
3662
|
+
case "class":
|
|
3663
|
+
out.push({
|
|
3664
|
+
op: "class",
|
|
3665
|
+
neg: node.neg,
|
|
3666
|
+
ranges: node.ranges
|
|
3667
|
+
});
|
|
3668
|
+
return;
|
|
3669
|
+
case "group":
|
|
3670
|
+
if (node.cap === null) {
|
|
3671
|
+
emit(node.body, out, base);
|
|
3672
|
+
return;
|
|
3673
|
+
}
|
|
3674
|
+
out.push({
|
|
3675
|
+
op: "save",
|
|
3676
|
+
slot: 2 * node.cap + 2
|
|
3677
|
+
});
|
|
3678
|
+
emit(node.body, out, base);
|
|
3679
|
+
out.push({
|
|
3680
|
+
op: "save",
|
|
3681
|
+
slot: 2 * node.cap + 3
|
|
3682
|
+
});
|
|
3683
|
+
return;
|
|
3684
|
+
case "cat":
|
|
3685
|
+
for (const n of node.items) emit(n, out, base);
|
|
3686
|
+
return;
|
|
3687
|
+
case "alt": {
|
|
3688
|
+
const jumps = [];
|
|
3689
|
+
node.items.forEach((n, k) => {
|
|
3690
|
+
if (k < node.items.length - 1) {
|
|
3691
|
+
const split = out.length;
|
|
3692
|
+
out.push({
|
|
3693
|
+
op: "split",
|
|
3694
|
+
x: split + 1,
|
|
3695
|
+
y: 0
|
|
3696
|
+
});
|
|
3697
|
+
emit(n, out, base);
|
|
3698
|
+
jumps.push(out.length);
|
|
3699
|
+
out.push({
|
|
3700
|
+
op: "jmp",
|
|
3701
|
+
x: 0
|
|
3702
|
+
});
|
|
3703
|
+
out[split].y = out.length;
|
|
3704
|
+
} else emit(n, out, base);
|
|
3705
|
+
});
|
|
3706
|
+
for (const j of jumps) out[j].x = out.length;
|
|
3707
|
+
return;
|
|
3708
|
+
}
|
|
3709
|
+
case "rep": {
|
|
3710
|
+
for (let k = 0; k < node.min; k++) emit(node.body, out, base);
|
|
3711
|
+
const slot = base + node.id;
|
|
3712
|
+
const guarded = nullable(node.body);
|
|
3713
|
+
const iteration = () => {
|
|
3714
|
+
if (guarded) out.push({
|
|
3715
|
+
op: "mark",
|
|
3716
|
+
slot
|
|
3717
|
+
});
|
|
3718
|
+
emit(node.body, out, base);
|
|
3719
|
+
if (guarded) out.push({
|
|
3720
|
+
op: "progress",
|
|
3721
|
+
slot
|
|
3722
|
+
});
|
|
3723
|
+
};
|
|
3724
|
+
if (node.max === Number.POSITIVE_INFINITY) {
|
|
3725
|
+
const split = out.length;
|
|
3726
|
+
out.push({
|
|
3727
|
+
op: "split",
|
|
3728
|
+
x: split + 1,
|
|
3729
|
+
y: 0
|
|
3730
|
+
});
|
|
3731
|
+
iteration();
|
|
3732
|
+
out.push({
|
|
3733
|
+
op: "jmp",
|
|
3734
|
+
x: split
|
|
3735
|
+
});
|
|
3736
|
+
out[split].y = out.length;
|
|
3737
|
+
return;
|
|
3738
|
+
}
|
|
3739
|
+
const splits = [];
|
|
3740
|
+
for (let k = node.min; k < node.max; k++) {
|
|
3741
|
+
splits.push(out.length);
|
|
3742
|
+
out.push({
|
|
3743
|
+
op: "split",
|
|
3744
|
+
x: out.length + 1,
|
|
3745
|
+
y: 0
|
|
3746
|
+
});
|
|
3747
|
+
iteration();
|
|
3748
|
+
}
|
|
3749
|
+
for (const s of splits) out[s].y = out.length;
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
}
|
|
3753
|
+
/** Compiles a pattern the subset accepts; throws on one it refuses. */
|
|
3754
|
+
function compileRegex(pattern) {
|
|
3755
|
+
const bad = checkRegexSubset(pattern);
|
|
3756
|
+
if (bad) throw new Error(`invalid regex: ${bad}`);
|
|
3757
|
+
const { node, groups, reps } = parse(pattern);
|
|
3758
|
+
if (programSize(node) > 2e3) throw new Error("invalid regex: program too large");
|
|
3759
|
+
const insts = [{
|
|
3760
|
+
op: "save",
|
|
3761
|
+
slot: 0
|
|
3762
|
+
}];
|
|
3763
|
+
emit(node, insts, 2 * groups + 2);
|
|
3764
|
+
insts.push({
|
|
3765
|
+
op: "save",
|
|
3766
|
+
slot: 1
|
|
3767
|
+
}, { op: "match" });
|
|
3768
|
+
return {
|
|
3769
|
+
insts,
|
|
3770
|
+
groups,
|
|
3771
|
+
slots: 2 * groups + 2 + reps
|
|
3772
|
+
};
|
|
3773
|
+
}
|
|
3774
|
+
/** Steps one call may take, about 200 ms: a 10 KB value against the largest program takes ~15M. */
|
|
3775
|
+
const MAX_STEPS = 4e6;
|
|
3776
|
+
/** The leftmost-longest match of `prog` in `subject`, as Postgres reports it, or null. */
|
|
3777
|
+
function execRegex(prog, subject) {
|
|
3778
|
+
const { insts } = prog;
|
|
3779
|
+
const cps = [];
|
|
3780
|
+
const offsets = [];
|
|
3781
|
+
for (let k = 0; k < subject.length;) {
|
|
3782
|
+
const cp = subject.codePointAt(k);
|
|
3783
|
+
offsets.push(k);
|
|
3784
|
+
cps.push(cp);
|
|
3785
|
+
k += cp > 65535 ? 2 : 1;
|
|
3786
|
+
}
|
|
3787
|
+
offsets.push(subject.length);
|
|
3788
|
+
const n = cps.length;
|
|
3789
|
+
const nslots = prog.slots;
|
|
3790
|
+
const seen = new Int32Array(insts.length).fill(-1);
|
|
3791
|
+
let clist = [];
|
|
3792
|
+
let nlist = [];
|
|
3793
|
+
let found = null;
|
|
3794
|
+
let steps = 0;
|
|
3795
|
+
const step = () => {
|
|
3796
|
+
if (++steps > 4e6) throw new ResolverFrameError(REGEX_BUDGET_EXCEEDED, `regex needs more than ${MAX_STEPS} steps on this value; use a shorter pattern or value`, { budget: MAX_STEPS });
|
|
3797
|
+
};
|
|
3798
|
+
const add = (list, startPc, startCaps, pos, gen) => {
|
|
3799
|
+
const stack = [{
|
|
3800
|
+
pc: startPc,
|
|
3801
|
+
caps: startCaps
|
|
3802
|
+
}];
|
|
3803
|
+
while (stack.length > 0) {
|
|
3804
|
+
step();
|
|
3805
|
+
const { pc, caps } = stack.pop();
|
|
3806
|
+
const inst = insts[pc];
|
|
3807
|
+
if (inst.op === "progress" && caps[inst.slot] === pos) continue;
|
|
3808
|
+
if (seen[pc] === gen) continue;
|
|
3809
|
+
seen[pc] = gen;
|
|
3810
|
+
switch (inst.op) {
|
|
3811
|
+
case "jmp":
|
|
3812
|
+
stack.push({
|
|
3813
|
+
pc: inst.x,
|
|
3814
|
+
caps
|
|
3815
|
+
});
|
|
3816
|
+
break;
|
|
3817
|
+
case "split":
|
|
3818
|
+
stack.push({
|
|
3819
|
+
pc: inst.y,
|
|
3820
|
+
caps
|
|
3821
|
+
}, {
|
|
3822
|
+
pc: inst.x,
|
|
3823
|
+
caps
|
|
3824
|
+
});
|
|
3825
|
+
break;
|
|
3826
|
+
case "progress":
|
|
3827
|
+
stack.push({
|
|
3828
|
+
pc: pc + 1,
|
|
3829
|
+
caps
|
|
3830
|
+
});
|
|
3831
|
+
break;
|
|
3832
|
+
case "mark":
|
|
3833
|
+
case "save": {
|
|
3834
|
+
const next = caps.slice();
|
|
3835
|
+
next[inst.slot] = pos;
|
|
3836
|
+
stack.push({
|
|
3837
|
+
pc: pc + 1,
|
|
3838
|
+
caps: next
|
|
3839
|
+
});
|
|
3840
|
+
break;
|
|
3841
|
+
}
|
|
3842
|
+
case "bol":
|
|
3843
|
+
if (pos === 0) stack.push({
|
|
3844
|
+
pc: pc + 1,
|
|
3845
|
+
caps
|
|
3846
|
+
});
|
|
3847
|
+
break;
|
|
3848
|
+
case "eol":
|
|
3849
|
+
if (pos === n) stack.push({
|
|
3850
|
+
pc: pc + 1,
|
|
3851
|
+
caps
|
|
3852
|
+
});
|
|
3853
|
+
break;
|
|
3854
|
+
default: list.push({
|
|
3855
|
+
pc,
|
|
3856
|
+
caps
|
|
3857
|
+
});
|
|
3858
|
+
}
|
|
3859
|
+
}
|
|
3860
|
+
};
|
|
3861
|
+
const blank = new Array(nslots).fill(-1);
|
|
3862
|
+
for (let pos = 0; pos <= n; pos++) {
|
|
3863
|
+
if (found === null) add(clist, 0, blank, pos, pos);
|
|
3864
|
+
if (clist.length === 0 && found !== null) break;
|
|
3865
|
+
const cp = pos < n ? cps[pos] : -1;
|
|
3866
|
+
for (const t of clist) {
|
|
3867
|
+
step();
|
|
3868
|
+
const inst = insts[t.pc];
|
|
3869
|
+
if (found !== null && t.caps[0] > found[0]) continue;
|
|
3870
|
+
if (inst.op === "match") {
|
|
3871
|
+
if (found === null || t.caps[0] < found[0] || pos > found[1]) found = t.caps;
|
|
3872
|
+
continue;
|
|
3873
|
+
}
|
|
3874
|
+
if (cp < 0) continue;
|
|
3875
|
+
if (inst.op === "char" ? inst.c === cp : inst.op === "any" ? true : inst.op === "class" && inst.ranges.some(([lo, hi]) => cp >= lo && cp <= hi) !== inst.neg) add(nlist, t.pc + 1, t.caps, pos + 1, pos + 1);
|
|
3876
|
+
}
|
|
3877
|
+
[clist, nlist] = [nlist, clist];
|
|
3878
|
+
nlist.length = 0;
|
|
3879
|
+
}
|
|
3880
|
+
if (found === null) return null;
|
|
3881
|
+
const at = (s) => offsets[s];
|
|
3882
|
+
const groups = [];
|
|
3883
|
+
for (let g = 0; g < prog.groups; g++) {
|
|
3884
|
+
const s = found[2 * g + 2];
|
|
3885
|
+
const e = found[2 * g + 3];
|
|
3886
|
+
groups.push(s === void 0 || s < 0 || e === void 0 || e < 0 ? void 0 : subject.slice(at(s), at(e)));
|
|
3887
|
+
}
|
|
3888
|
+
return {
|
|
3889
|
+
index: at(found[0]),
|
|
3890
|
+
end: at(found[1]),
|
|
3891
|
+
groups
|
|
3892
|
+
};
|
|
3893
|
+
}
|
|
3894
|
+
|
|
3895
|
+
//#endregion
|
|
3896
|
+
//#region ../../packages/substrate-filter/src/expr-eval.ts
|
|
3897
|
+
const MIN_INT64$1 = -(2n ** 63n);
|
|
3898
|
+
const MAX_INT64$1 = 2n ** 63n - 1n;
|
|
3899
|
+
const int64 = (n) => {
|
|
3900
|
+
if (n < MIN_INT64$1 || n > MAX_INT64$1) throw new RangeError("bigint out of range");
|
|
3901
|
+
return n;
|
|
3902
|
+
};
|
|
3903
|
+
/** A small Map-based LRU: bounded so caller-supplied patterns cannot grow memory. */
|
|
3904
|
+
function lru(max, make) {
|
|
3905
|
+
const m = /* @__PURE__ */ new Map();
|
|
3906
|
+
return (k) => {
|
|
3907
|
+
let v = m.get(k);
|
|
3908
|
+
if (v !== void 0) m.delete(k);
|
|
3909
|
+
else {
|
|
3910
|
+
v = make(k);
|
|
3911
|
+
if (m.size >= max) m.delete(m.keys().next().value);
|
|
3912
|
+
}
|
|
3913
|
+
m.set(k, v);
|
|
3914
|
+
return v;
|
|
3915
|
+
};
|
|
3916
|
+
}
|
|
3917
|
+
const compile = lru(256, compileRegex);
|
|
3918
|
+
function likeTokens(pattern) {
|
|
3919
|
+
const cps = Array.from(pattern);
|
|
3920
|
+
const out = [];
|
|
3921
|
+
for (let i = 0; i < cps.length; i++) {
|
|
3922
|
+
const c = cps[i];
|
|
3923
|
+
if (c === "\\") out.push({ lit: i + 1 < cps.length ? cps[++i] : c });
|
|
3924
|
+
else if (c === "%" || c === "_") out.push(c);
|
|
3925
|
+
else out.push({ lit: c });
|
|
3926
|
+
}
|
|
3927
|
+
return out;
|
|
3928
|
+
}
|
|
3929
|
+
/** Linear-time-ish wildcard match (backtracks only to the last `%`), over code points. */
|
|
3930
|
+
/** Parsed once per pattern, not per row. */
|
|
3931
|
+
const likeTokensOf = lru(256, likeTokens);
|
|
3932
|
+
function likeMatch(text, pattern) {
|
|
3933
|
+
const toks = likeTokensOf(pattern);
|
|
3934
|
+
const t = Array.from(text);
|
|
3935
|
+
let ti = 0;
|
|
3936
|
+
let pi = 0;
|
|
3937
|
+
let star = -1;
|
|
3938
|
+
let mark = 0;
|
|
3939
|
+
while (ti < t.length) {
|
|
3940
|
+
const tok = toks[pi];
|
|
3941
|
+
if (tok === "%") {
|
|
3942
|
+
star = pi++;
|
|
3943
|
+
mark = ti;
|
|
3944
|
+
} else if (tok !== void 0 && (tok === "_" || tok.lit === t[ti])) {
|
|
3945
|
+
pi++;
|
|
3946
|
+
ti++;
|
|
3947
|
+
} else if (star >= 0) {
|
|
3948
|
+
pi = star + 1;
|
|
3949
|
+
ti = ++mark;
|
|
3950
|
+
} else return false;
|
|
3951
|
+
}
|
|
3952
|
+
while (toks[pi] === "%") pi++;
|
|
3953
|
+
return pi === toks.length;
|
|
3954
|
+
}
|
|
3955
|
+
/** Maps ASCII letters only, as pg does under the "C" collation; every other code point is unchanged. */
|
|
3956
|
+
const asciiCase = (s, from, f) => s.replace(from, f);
|
|
3957
|
+
/** float8 arithmetic as Postgres does it: a non-finite result from finite operands, or a nonzero product or quotient that underflows to 0, raises. */
|
|
3958
|
+
function float8(v, a, b, underflows) {
|
|
3959
|
+
if (!Number.isFinite(v) && Number.isFinite(a) && Number.isFinite(b)) throw new Error("value out of range: overflow");
|
|
3960
|
+
if (underflows && v === 0 && a !== 0 && b !== 0) throw new Error("value out of range: underflow");
|
|
3961
|
+
return v;
|
|
3962
|
+
}
|
|
3963
|
+
/** The replacement's `\1`-`\9` as group references, `\&` as the whole match; `\\` is a backslash; everything else is literal. */
|
|
3964
|
+
function expandReplacement(rep, whole, groups) {
|
|
3965
|
+
let out = "";
|
|
3966
|
+
const cps = Array.from(rep);
|
|
3967
|
+
for (let i = 0; i < cps.length; i++) {
|
|
3968
|
+
const c = cps[i];
|
|
3969
|
+
const next = cps[i + 1];
|
|
3970
|
+
if (c === "\\" && next !== void 0 && /[1-9]/.test(next)) {
|
|
3971
|
+
out += groups[Number(next) - 1] ?? "";
|
|
3972
|
+
i++;
|
|
3973
|
+
} else if (c === "\\" && next === "&") {
|
|
3974
|
+
out += whole;
|
|
3975
|
+
i++;
|
|
3976
|
+
} else if (c === "\\" && next === "\\") {
|
|
3977
|
+
out += "\\";
|
|
3978
|
+
i++;
|
|
3979
|
+
} else out += c;
|
|
3980
|
+
}
|
|
3981
|
+
return out;
|
|
3982
|
+
}
|
|
3983
|
+
/** A date value as microseconds, read by the shared ISO grammar (no offset is UTC); null when unreadable, as pg's substrate_ts. */
|
|
3984
|
+
const microsOf = (v) => storedMicros(v) ?? null;
|
|
3985
|
+
/** The millisecond Date of an instant in microseconds, floored. */
|
|
3986
|
+
const dateOf = (m) => new Date(Number((m - (m % 1000n + 1000n) % 1000n) / 1000n));
|
|
3987
|
+
/** A UTC Date; unlike Date.UTC, years 0-99 stay as given. */
|
|
3988
|
+
function utc(y, mo, da, h = 0, mi = 0, s = 0) {
|
|
3989
|
+
const d = /* @__PURE__ */ new Date(0);
|
|
3990
|
+
d.setUTCFullYear(y, mo, da);
|
|
3991
|
+
d.setUTCHours(h, mi, s, 0);
|
|
3992
|
+
return d;
|
|
3993
|
+
}
|
|
3994
|
+
function trunc(unit, d) {
|
|
3995
|
+
const [y, mo, da, h, mi, s] = [
|
|
3996
|
+
d.getUTCFullYear(),
|
|
3997
|
+
d.getUTCMonth(),
|
|
3998
|
+
d.getUTCDate(),
|
|
3999
|
+
d.getUTCHours(),
|
|
4000
|
+
d.getUTCMinutes(),
|
|
4001
|
+
d.getUTCSeconds()
|
|
4002
|
+
];
|
|
4003
|
+
switch (unit) {
|
|
4004
|
+
case "year": return utc(y, 0, 1);
|
|
4005
|
+
case "month": return utc(y, mo, 1);
|
|
4006
|
+
case "week": return utc(y, mo, da - (d.getUTCDay() + 6) % 7);
|
|
4007
|
+
case "day": return utc(y, mo, da);
|
|
4008
|
+
case "hour": return utc(y, mo, da, h);
|
|
4009
|
+
case "minute": return utc(y, mo, da, h, mi);
|
|
4010
|
+
default: return utc(y, mo, da, h, mi, s);
|
|
4011
|
+
}
|
|
4012
|
+
}
|
|
4013
|
+
function isoWeek(d) {
|
|
4014
|
+
const day = (d.getUTCDay() + 6) % 7;
|
|
4015
|
+
const thursday = utc(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate() - day + 3);
|
|
4016
|
+
const jan1 = utc(thursday.getUTCFullYear(), 0, 1);
|
|
4017
|
+
return Math.floor((thursday.getTime() - jan1.getTime()) / 6048e5) + 1;
|
|
4018
|
+
}
|
|
4019
|
+
function extract(unit, m) {
|
|
4020
|
+
const d = dateOf(m);
|
|
4021
|
+
switch (unit) {
|
|
4022
|
+
case "year": return d.getUTCFullYear();
|
|
4023
|
+
case "month": return d.getUTCMonth() + 1;
|
|
4024
|
+
case "week": return isoWeek(d);
|
|
4025
|
+
case "day": return d.getUTCDate();
|
|
4026
|
+
case "hour": return d.getUTCHours();
|
|
4027
|
+
case "minute": return d.getUTCMinutes();
|
|
4028
|
+
default: {
|
|
4029
|
+
const frac = (m % 1000000n + 1000000n) % 1000000n;
|
|
4030
|
+
return Number(`${d.getUTCSeconds()}.${frac.toString().padStart(6, "0")}`);
|
|
4031
|
+
}
|
|
4032
|
+
}
|
|
4033
|
+
}
|
|
4034
|
+
const widen = (x, y) => typeof x === "bigint" && typeof y === "number" ? [Number(x), y] : typeof x === "number" && typeof y === "bigint" ? [x, Number(y)] : [x, y];
|
|
4035
|
+
/** SQL order of two non-null operands: int64 beside number as number, text by code point (pg's "C" collation). */
|
|
4036
|
+
function compare$1(x, y) {
|
|
4037
|
+
const [a, b] = widen(x, y);
|
|
4038
|
+
if (typeof a === "string" && typeof b === "string") return compareCodePoints(a, b);
|
|
4039
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
4040
|
+
}
|
|
4041
|
+
const isSafeIntLiteral = (e) => typeof e === "number" && Number.isSafeInteger(e);
|
|
4042
|
+
const ARITH = {
|
|
4043
|
+
add: [(a, b) => a + b, (a, b) => a + b],
|
|
4044
|
+
sub: [(a, b) => a - b, (a, b) => a - b],
|
|
4045
|
+
mul: [(a, b) => a * b, (a, b) => a * b]
|
|
4046
|
+
};
|
|
4047
|
+
function evalExpr(expr, row, env) {
|
|
4048
|
+
if (expr === null) return null;
|
|
4049
|
+
if (typeof expr === "string") {
|
|
4050
|
+
const v = env.output !== void 0 && Object.hasOwn(env.output, expr) ? env.output[expr] : getFieldValue(row, expr) ?? null;
|
|
4051
|
+
if (v === void 0 || v === null) return null;
|
|
4052
|
+
if (typeof v !== "number" && env.fieldType(expr) === "number") return null;
|
|
4053
|
+
if (typeof v === "number" && env.fieldType(expr) === "int64") return Number.isSafeInteger(v) ? BigInt(v) : null;
|
|
4054
|
+
if (env.fieldType(expr) === "date" || v instanceof Date) {
|
|
4055
|
+
const m = microsOf(v);
|
|
4056
|
+
return m === null ? null : isoFromMicros(m);
|
|
4057
|
+
}
|
|
4058
|
+
return v;
|
|
4059
|
+
}
|
|
4060
|
+
if (typeof expr !== "object") return expr;
|
|
4061
|
+
if ("lit" in expr) return expr.lit;
|
|
4062
|
+
if ("int64" in expr) return BigInt(expr.int64);
|
|
4063
|
+
const key = Object.keys(expr)[0];
|
|
4064
|
+
const args = expr[key];
|
|
4065
|
+
const ev = (e) => evalExpr(e, row, env);
|
|
4066
|
+
switch (key) {
|
|
4067
|
+
case "case":
|
|
4068
|
+
for (let i = 0; i < args.length - 1; i++) {
|
|
4069
|
+
const [cond, then] = args[i];
|
|
4070
|
+
if (ev(cond) === true) return unified(expr, ev(then), env);
|
|
4071
|
+
}
|
|
4072
|
+
return unified(expr, ev(args[args.length - 1]), env);
|
|
4073
|
+
case "coalesce":
|
|
4074
|
+
for (const a of args) {
|
|
4075
|
+
const v = ev(a);
|
|
4076
|
+
if (v !== null) return unified(expr, v, env);
|
|
4077
|
+
}
|
|
4078
|
+
return null;
|
|
4079
|
+
case "isNull": return ev(args[0]) === null;
|
|
4080
|
+
case "and":
|
|
4081
|
+
case "or": {
|
|
4082
|
+
const stop = key === "or";
|
|
4083
|
+
let sawNull = false;
|
|
4084
|
+
for (const a of args) {
|
|
4085
|
+
const v = ev(a);
|
|
4086
|
+
if (v === null) sawNull = true;
|
|
4087
|
+
else if (v === stop) return stop;
|
|
4088
|
+
}
|
|
4089
|
+
return sawNull ? null : !stop;
|
|
4090
|
+
}
|
|
4091
|
+
}
|
|
4092
|
+
const vals = args.map(ev);
|
|
4093
|
+
if (key === "in") {
|
|
4094
|
+
const [needle, ...rest] = vals;
|
|
4095
|
+
if (needle === null) return null;
|
|
4096
|
+
const hit = (v) => v !== null && compare$1(needle, v) === 0;
|
|
4097
|
+
if (rest.some(hit)) return true;
|
|
4098
|
+
return rest.includes(null) ? null : false;
|
|
4099
|
+
}
|
|
4100
|
+
if (key === "dateTrunc" || key === "extract") {
|
|
4101
|
+
const m = microsOf(vals[1]);
|
|
4102
|
+
if (m === null) return null;
|
|
4103
|
+
return key === "extract" ? extract(vals[0], m) : isoFromMicros(BigInt(trunc(vals[0], dateOf(m)).getTime()) * 1000n);
|
|
4104
|
+
}
|
|
4105
|
+
if (vals.some((v) => v === null)) return null;
|
|
4106
|
+
const [a, b, c] = vals;
|
|
4107
|
+
switch (key) {
|
|
4108
|
+
case "add":
|
|
4109
|
+
case "sub":
|
|
4110
|
+
case "mul": {
|
|
4111
|
+
const [big, num] = ARITH[key];
|
|
4112
|
+
return arith(args, vals, big, num, key === "mul");
|
|
4113
|
+
}
|
|
4114
|
+
case "div": return Number(b) === 0 ? null : float8(Number(a) / Number(b), Number(a), Number(b), true);
|
|
4115
|
+
case "mod": return BigInt(b) === 0n ? null : int64(BigInt(a) % BigInt(b));
|
|
4116
|
+
case "neg": return typeof a === "bigint" ? int64(-a) : -a;
|
|
4117
|
+
case "eq": return compare$1(a, b) === 0;
|
|
4118
|
+
case "neq": return compare$1(a, b) !== 0;
|
|
4119
|
+
case "gt": return compare$1(a, b) > 0;
|
|
4120
|
+
case "gte": return compare$1(a, b) >= 0;
|
|
4121
|
+
case "lt": return compare$1(a, b) < 0;
|
|
4122
|
+
case "lte": return compare$1(a, b) <= 0;
|
|
4123
|
+
case "not": return !a;
|
|
4124
|
+
case "length": return Array.from(a).length;
|
|
4125
|
+
case "lower": return asciiCase(a, /[A-Z]/g, (x) => x.toLowerCase());
|
|
4126
|
+
case "upper": return asciiCase(a, /[a-z]/g, (x) => x.toUpperCase());
|
|
4127
|
+
case "substring": {
|
|
4128
|
+
const int4 = (x, lo) => Math.min(Math.max(Math.round(Number(x)), lo), 2 ** 31 - 1);
|
|
4129
|
+
const start = int4(b, -(2 ** 31));
|
|
4130
|
+
const end = start + int4(c, 0);
|
|
4131
|
+
return Array.from(a).slice(Math.max(start, 1) - 1, Math.max(end - 1, 0)).join("");
|
|
4132
|
+
}
|
|
4133
|
+
case "concat": return vals.join("");
|
|
4134
|
+
case "like": return likeMatch(a, b);
|
|
4135
|
+
case "notLike": return !likeMatch(a, b);
|
|
4136
|
+
case "regexpMatch": return execRegex(compile(b), a) !== null;
|
|
4137
|
+
case "regexpReplace": {
|
|
4138
|
+
const m = execRegex(compile(b), a);
|
|
4139
|
+
if (!m) return a;
|
|
4140
|
+
return a.slice(0, m.index) + expandReplacement(c, a.slice(m.index, m.end), m.groups) + a.slice(m.end);
|
|
4141
|
+
}
|
|
4142
|
+
default: throw new Error(`unknown function '${key}'`);
|
|
4143
|
+
}
|
|
4144
|
+
}
|
|
4145
|
+
/** int64 when both operands are int64 (a safe-integer literal counts), else number. */
|
|
4146
|
+
function arith(args, vals, big, num, mulUnderflow = false) {
|
|
4147
|
+
const [a, b] = vals;
|
|
4148
|
+
const lift = (v, e, other) => typeof v === "number" && typeof other === "bigint" && isSafeIntLiteral(e) ? BigInt(v) : v;
|
|
4149
|
+
const x = lift(a, args[0], b);
|
|
4150
|
+
const y = lift(b, args[1], a);
|
|
4151
|
+
if (typeof x === "bigint" && typeof y === "bigint") return int64(big(x, y));
|
|
4152
|
+
return float8(num(Number(x), Number(y)), Number(x), Number(y), mulUnderflow);
|
|
4153
|
+
}
|
|
4154
|
+
/** A case or coalesce value in the type the checker gives the whole call: int64 widens to number, a number literal adopts int64. */
|
|
4155
|
+
function unified(expr, v, env) {
|
|
4156
|
+
if (typeof v !== "bigint" && typeof v !== "number") return v;
|
|
4157
|
+
const t = callType(expr, env);
|
|
4158
|
+
if (t === "number" && typeof v === "bigint") return Number(v);
|
|
4159
|
+
if (t === "int64" && typeof v === "number") return BigInt(v);
|
|
4160
|
+
return v;
|
|
4161
|
+
}
|
|
4162
|
+
/** Each call node's checked type, computed once per node and field-type lookup, never per row. */
|
|
4163
|
+
const callTypes = /* @__PURE__ */ new WeakMap();
|
|
4164
|
+
function callType(expr, env) {
|
|
4165
|
+
const byNode = callTypes.get(env.fieldType) ?? /* @__PURE__ */ new WeakMap();
|
|
4166
|
+
callTypes.set(env.fieldType, byNode);
|
|
4167
|
+
const hit = byNode.get(expr);
|
|
4168
|
+
if (hit !== void 0) return hit;
|
|
4169
|
+
const c = checkExpr(expr, { field: env.fieldType }, []);
|
|
4170
|
+
const t = c.ok ? c.type : null;
|
|
4171
|
+
byNode.set(expr, t);
|
|
4172
|
+
return t;
|
|
4173
|
+
}
|
|
4174
|
+
|
|
4175
|
+
//#endregion
|
|
4176
|
+
//#region ../../packages/substrate-filter/src/grouped.ts
|
|
4177
|
+
/**
|
|
4178
|
+
* Grouped reads (`aggregate` with `groupBy`, `having`, `orderBy`, `offset`) in
|
|
4179
|
+
* memory: the reference the Postgres compiler is held to. Groups are keyed by
|
|
4180
|
+
* a stable encoding of the key tuple; nulls form one group, as SQL's GROUP BY.
|
|
4181
|
+
*/
|
|
4182
|
+
const ARITHMETIC = /* @__PURE__ */ new Set([
|
|
4183
|
+
"add",
|
|
4184
|
+
"sub",
|
|
4185
|
+
"mul",
|
|
4186
|
+
"div",
|
|
4187
|
+
"mod",
|
|
4188
|
+
"neg"
|
|
4189
|
+
]);
|
|
4190
|
+
/** Whether evaluating `e` can raise a range error (arithmetic anywhere in it). */
|
|
4191
|
+
function canRaise(e) {
|
|
4192
|
+
if (Array.isArray(e)) return e.some(canRaise);
|
|
4193
|
+
if (typeof e !== "object" || e === null || "lit" in e || "int64" in e) return false;
|
|
4194
|
+
return Object.entries(e).some(([k, v]) => ARITHMETIC.has(k) || canRaise(v));
|
|
4195
|
+
}
|
|
4196
|
+
const MIN_INT64 = -(2n ** 63n);
|
|
4197
|
+
const MAX_INT64 = 2n ** 63n - 1n;
|
|
4198
|
+
const OPS = [
|
|
4199
|
+
"count",
|
|
4200
|
+
"countDistinct",
|
|
4201
|
+
"avg",
|
|
4202
|
+
"sum",
|
|
4203
|
+
"min",
|
|
4204
|
+
"max"
|
|
4205
|
+
];
|
|
4206
|
+
/** An aggregate's function and argument. */
|
|
4207
|
+
function aggParts(spec) {
|
|
4208
|
+
const op = OPS.find((k) => Object.hasOwn(spec, k));
|
|
4209
|
+
if (op === void 0) throw new Error("internal: aggregate names no function");
|
|
4210
|
+
return {
|
|
4211
|
+
op,
|
|
4212
|
+
arg: spec[op]
|
|
4213
|
+
};
|
|
4214
|
+
}
|
|
4215
|
+
/** The type an aggregate over an argument of type `arg` returns (`count` is a number). */
|
|
4216
|
+
function aggType(op, arg) {
|
|
4217
|
+
if (op === "count" || op === "countDistinct" || op === "avg") return "number";
|
|
4218
|
+
return arg ?? "string";
|
|
4219
|
+
}
|
|
4220
|
+
const isoMicros$1 = (v) => {
|
|
4221
|
+
const m = storedMicros(v);
|
|
4222
|
+
return m === void 0 ? null : isoFromMicros(m);
|
|
4223
|
+
};
|
|
4224
|
+
/** A value as a Map key, equal exactly when SQL would put two values in one group. */
|
|
4225
|
+
function encode(v, t) {
|
|
4226
|
+
if (v === null || v === void 0) return "n";
|
|
4227
|
+
if (t === "date") return `d${storedMicros(v)}`;
|
|
4228
|
+
if (typeof v === "bigint") return `i${v}`;
|
|
4229
|
+
if (typeof v === "number") return `f${v === 0 ? 0 : v}`;
|
|
4230
|
+
return `${typeof v === "string" ? "s" : "b"}${JSON.stringify(v)}`;
|
|
4231
|
+
}
|
|
4232
|
+
/** A date value `substrate_ts` cannot read is NULL on pg; so here. */
|
|
4233
|
+
const readable = (v, t) => t === "date" && v !== null && storedMicros(v) === void 0 ? null : v;
|
|
4234
|
+
const bytes = (a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b));
|
|
4235
|
+
/** Nulls compare after everything; otherwise by type, as pg sorts it (text in byte order). */
|
|
4236
|
+
function compare(a, b, t) {
|
|
4237
|
+
if (a === null && b === null) return 0;
|
|
4238
|
+
if (a === null) return 1;
|
|
4239
|
+
if (b === null) return -1;
|
|
4240
|
+
if (t === "date") return Number(storedMicros(a) - storedMicros(b));
|
|
4241
|
+
if (t === "string") return bytes(a, b);
|
|
4242
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
4243
|
+
}
|
|
4244
|
+
/** The evaluator's int64 range error and its float overflow/underflow error: pg's 22003. */
|
|
4245
|
+
const isRangeError = (err) => err instanceof RangeError && err.message === "bigint out of range" || err instanceof Error && err.message.startsWith("value out of range: ");
|
|
4246
|
+
function outOfRange(err) {
|
|
4247
|
+
if (isRangeError(err)) throw new ResolverFrameError(VALUE_OUT_OF_RANGE, err.message);
|
|
4248
|
+
throw err;
|
|
4249
|
+
}
|
|
4250
|
+
function aggregateOf(op, arg, t, values, rows) {
|
|
4251
|
+
if (op === "count") return arg === true ? rows : values.filter((v) => v !== null).length;
|
|
4252
|
+
const present = values.filter((v) => v !== null);
|
|
4253
|
+
if (op === "countDistinct") return new Set(present.map((v) => encode(v, t))).size;
|
|
4254
|
+
if (present.length === 0) return null;
|
|
4255
|
+
if (t === "int64") {
|
|
4256
|
+
const ns = present;
|
|
4257
|
+
if (op === "min" || op === "max") return ns.reduce((a, b) => (op === "min" ? b < a : b > a) ? b : a);
|
|
4258
|
+
const sum = ns.reduce((a, b) => a + b, 0n);
|
|
4259
|
+
if (op === "avg") return Number(sum) / ns.length;
|
|
4260
|
+
if (sum < MIN_INT64 || sum > MAX_INT64) outOfRange(/* @__PURE__ */ new RangeError("bigint out of range"));
|
|
4261
|
+
return sum;
|
|
4262
|
+
}
|
|
4263
|
+
if (t === "string" || t === "date") {
|
|
4264
|
+
const best = present.reduce((a, b) => (op === "min" ? compare(b, a, t) < 0 : compare(b, a, t) > 0) ? b : a);
|
|
4265
|
+
return t === "date" ? isoMicros$1(best) : best;
|
|
4266
|
+
}
|
|
4267
|
+
return numericAgg(present, op);
|
|
4268
|
+
}
|
|
4269
|
+
/**
|
|
4270
|
+
* `pool`'s rows grouped, aggregated, filtered by `having`, ordered (default:
|
|
4271
|
+
* the group keys ascending, ties by them too), then cut to `limit` after `offset`.
|
|
4272
|
+
* `fieldType` must be one stable lookup per read: the evaluator caches on it.
|
|
4273
|
+
*/
|
|
4274
|
+
function groupRows(pool, p, fieldType, limit) {
|
|
4275
|
+
const keys = Object.entries(p.groupBy ?? {});
|
|
4276
|
+
const aggs = Object.entries(p.aggregate).map(([name, spec]) => ({
|
|
4277
|
+
name,
|
|
4278
|
+
...aggParts(spec)
|
|
4279
|
+
}));
|
|
4280
|
+
const typeOf = (e) => {
|
|
4281
|
+
const r = checkExpr(e, { field: fieldType }, []);
|
|
4282
|
+
return r.ok ? r.type : null;
|
|
4283
|
+
};
|
|
4284
|
+
const keyTypes = keys.map(([, e]) => typeOf(e));
|
|
4285
|
+
const argTypes = aggs.map((a) => typeOf(a.arg));
|
|
4286
|
+
const outTypes = new Map([...keys.map(([n], i) => [n, keyTypes[i]]), ...aggs.map((a, i) => [a.name, aggType(a.op, argTypes[i])])]);
|
|
4287
|
+
const outType = (name) => outTypes.get(name) ?? void 0;
|
|
4288
|
+
const env = { fieldType };
|
|
4289
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
4290
|
+
try {
|
|
4291
|
+
for (const row of pool) {
|
|
4292
|
+
const key = keys.map(([, e], i) => readable(evalExpr(e, row, env) ?? null, keyTypes[i]));
|
|
4293
|
+
const id = key.map((v, i) => encode(v, keyTypes[i])).join("\0");
|
|
4294
|
+
const b = buckets.get(id);
|
|
4295
|
+
if (b === void 0) buckets.set(id, {
|
|
4296
|
+
key,
|
|
4297
|
+
rows: [row]
|
|
4298
|
+
});
|
|
4299
|
+
else b.rows.push(row);
|
|
4300
|
+
}
|
|
4301
|
+
if (keys.length === 0 && buckets.size === 0) buckets.set("", {
|
|
4302
|
+
key: [],
|
|
4303
|
+
rows: []
|
|
4304
|
+
});
|
|
4305
|
+
} catch (err) {
|
|
4306
|
+
outOfRange(err);
|
|
4307
|
+
}
|
|
4308
|
+
let groups = [];
|
|
4309
|
+
try {
|
|
4310
|
+
for (const { key, rows } of buckets.values()) {
|
|
4311
|
+
const g = {};
|
|
4312
|
+
keys.forEach(([n], i) => {
|
|
4313
|
+
g[n] = keyTypes[i] === "date" ? isoMicros$1(key[i]) : key[i];
|
|
4314
|
+
});
|
|
4315
|
+
aggs.forEach((a, i) => {
|
|
4316
|
+
const at = argTypes[i];
|
|
4317
|
+
const values = rows.map((r) => readable(evalExpr(a.arg, r, env) ?? null, at));
|
|
4318
|
+
g[a.name] = aggregateOf(a.op, a.arg, argTypes[i], values, rows.length);
|
|
4319
|
+
});
|
|
4320
|
+
groups.push(g);
|
|
4321
|
+
}
|
|
4322
|
+
} catch (err) {
|
|
4323
|
+
outOfRange(err);
|
|
4324
|
+
}
|
|
4325
|
+
if (p.having !== void 0) {
|
|
4326
|
+
const having = p.having;
|
|
4327
|
+
try {
|
|
4328
|
+
groups = groups.filter((g) => evalExpr(having, {}, {
|
|
4329
|
+
output: g,
|
|
4330
|
+
fieldType: outType
|
|
4331
|
+
}) === true);
|
|
4332
|
+
} catch (err) {
|
|
4333
|
+
outOfRange(err);
|
|
4334
|
+
}
|
|
4335
|
+
}
|
|
4336
|
+
const order = [...(p.orderBy ?? []).map((c) => Object.entries(c)[0]), ...keys.map(([n]) => [n, "asc"])];
|
|
4337
|
+
groups.sort((a, b) => {
|
|
4338
|
+
for (const [name, dir] of order) {
|
|
4339
|
+
const t = outTypes.get(name) ?? null;
|
|
4340
|
+
const x = a[name];
|
|
4341
|
+
const y = b[name];
|
|
4342
|
+
const c = compare(x ?? null, y ?? null, t);
|
|
4343
|
+
if (c !== 0) return dir === "desc" && x != null && y != null ? -c : c;
|
|
4344
|
+
}
|
|
4345
|
+
return 0;
|
|
4346
|
+
});
|
|
4347
|
+
const start = p.offset ?? 0;
|
|
4348
|
+
const end = limit === void 0 ? void 0 : start + limit;
|
|
4349
|
+
const truncated = end !== void 0 && groups.length > end;
|
|
4350
|
+
return {
|
|
4351
|
+
groups: groups.slice(start, end),
|
|
4352
|
+
truncated
|
|
4353
|
+
};
|
|
4354
|
+
}
|
|
4355
|
+
|
|
4356
|
+
//#endregion
|
|
4357
|
+
//#region ../../packages/substrate-filter/src/pre-resolve-extension-operators.ts
|
|
4358
|
+
/**
|
|
4359
|
+
* Thrown when a registered extension operator's `execute` resolves to
|
|
4360
|
+
* anything other than `{ hits: [...] }`.
|
|
2983
4361
|
*/
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
const bt = toMicros(b);
|
|
2991
|
-
if (at !== void 0 && bt !== void 0) return at < bt ? -1 : at > bt ? 1 : 0;
|
|
4362
|
+
var ExtensionOperatorResultError = class extends Error {
|
|
4363
|
+
operator;
|
|
4364
|
+
constructor(operator) {
|
|
4365
|
+
super(`extension operator '${operator}' must resolve to { hits: [{ id, score, vector? }] }`);
|
|
4366
|
+
this.name = "ExtensionOperatorResultError";
|
|
4367
|
+
this.operator = operator;
|
|
2992
4368
|
}
|
|
2993
|
-
|
|
2994
|
-
return compareCodePoints(String(a), String(b));
|
|
2995
|
-
}
|
|
4369
|
+
};
|
|
2996
4370
|
/**
|
|
2997
|
-
*
|
|
2998
|
-
*
|
|
2999
|
-
*
|
|
4371
|
+
* Walks a where-tree, replaces every registered extension-operator entry with
|
|
4372
|
+
* an order-preserving `{ id: { in: [...] } }`, and returns BOTH the
|
|
4373
|
+
* rewritten where and a `meta` side-channel.
|
|
4374
|
+
*
|
|
4375
|
+
* Returns a new where object — the input is not mutated.
|
|
3000
4376
|
*/
|
|
3001
|
-
function
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
4377
|
+
async function preResolveExtensionOperatorsDetailed(where, context) {
|
|
4378
|
+
const holder = { current: null };
|
|
4379
|
+
return {
|
|
4380
|
+
where: await walk(where, context, holder),
|
|
4381
|
+
meta: holder.current
|
|
4382
|
+
};
|
|
3005
4383
|
}
|
|
3006
|
-
/**
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
const
|
|
3014
|
-
|
|
3015
|
-
|
|
4384
|
+
/** Recursive tree walk behind {@link preResolveExtensionOperatorsDetailed}. */
|
|
4385
|
+
async function walk(where, context, holder) {
|
|
4386
|
+
if (where === null || typeof where !== "object" || Array.isArray(where)) return where;
|
|
4387
|
+
const keys = Object.keys(where);
|
|
4388
|
+
if (keys.length === 0) return where;
|
|
4389
|
+
const out = {};
|
|
4390
|
+
const idClauses = [];
|
|
4391
|
+
for (const key of keys) {
|
|
4392
|
+
const value = where[key];
|
|
4393
|
+
if (key === "AND" || key === "OR") {
|
|
4394
|
+
if (Array.isArray(value)) out[key] = await Promise.all(value.map((sub) => walk(sub, context, holder)));
|
|
4395
|
+
else out[key] = value;
|
|
4396
|
+
continue;
|
|
4397
|
+
}
|
|
4398
|
+
if (key === "NOT") {
|
|
4399
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) out[key] = await walk(value, context, holder);
|
|
4400
|
+
else out[key] = value;
|
|
4401
|
+
continue;
|
|
4402
|
+
}
|
|
4403
|
+
const op = getExtensionOperator(key);
|
|
4404
|
+
if (op === void 0) {
|
|
4405
|
+
out[key] = value;
|
|
4406
|
+
continue;
|
|
4407
|
+
}
|
|
4408
|
+
const result = await op.execute(value, context);
|
|
4409
|
+
if (!isHitsResult(result)) throw new ExtensionOperatorResultError(key);
|
|
4410
|
+
const meta = buildMeta(result.hits);
|
|
4411
|
+
holder.current = meta;
|
|
4412
|
+
idClauses.push({ id: { in: [...meta.orderedIds] } });
|
|
4413
|
+
}
|
|
4414
|
+
if (idClauses.length === 0) return out;
|
|
4415
|
+
if (out.id !== void 0) idClauses.unshift({ id: out.id });
|
|
4416
|
+
if (idClauses.length === 1) return {
|
|
4417
|
+
...out,
|
|
4418
|
+
...idClauses[0]
|
|
4419
|
+
};
|
|
4420
|
+
delete out.id;
|
|
4421
|
+
return { AND: [out, ...idClauses] };
|
|
4422
|
+
}
|
|
4423
|
+
/** Narrow an operator result to the hits envelope. */
|
|
4424
|
+
function isHitsResult(result) {
|
|
4425
|
+
return result !== null && typeof result === "object" && Array.isArray(result.hits);
|
|
3016
4426
|
}
|
|
3017
4427
|
/**
|
|
3018
|
-
*
|
|
3019
|
-
*
|
|
3020
|
-
*
|
|
3021
|
-
*
|
|
3022
|
-
* anything unparseable is `undefined`.
|
|
4428
|
+
* Build the ordered-ids list + computed Map from a hits array. Hits with a
|
|
4429
|
+
* non-string id are skipped (defensive — bad input becomes "no match" rather
|
|
4430
|
+
* than an error). The vector key is spread conditionally so a hit without a
|
|
4431
|
+
* vector yields a computed entry with NO `vector` key (exactOptionalPropertyTypes).
|
|
3023
4432
|
*/
|
|
3024
|
-
function
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
4433
|
+
function buildMeta(hits) {
|
|
4434
|
+
const orderedIds = [];
|
|
4435
|
+
const computed = /* @__PURE__ */ new Map();
|
|
4436
|
+
for (const hit of hits) {
|
|
4437
|
+
if (hit === null || typeof hit !== "object") continue;
|
|
4438
|
+
const id = hit.id;
|
|
4439
|
+
if (typeof id !== "string") continue;
|
|
4440
|
+
const rawScore = hit.score;
|
|
4441
|
+
const score = typeof rawScore === "number" ? rawScore : 0;
|
|
4442
|
+
const rawVector = hit.vector;
|
|
4443
|
+
const hasVector = Array.isArray(rawVector);
|
|
4444
|
+
orderedIds.push(id);
|
|
4445
|
+
computed.set(id, {
|
|
4446
|
+
score,
|
|
4447
|
+
...hasVector ? { vector: rawVector } : {}
|
|
4448
|
+
});
|
|
4449
|
+
}
|
|
4450
|
+
return {
|
|
4451
|
+
orderedIds,
|
|
4452
|
+
computed
|
|
4453
|
+
};
|
|
3039
4454
|
}
|
|
3040
4455
|
|
|
3041
4456
|
//#endregion
|
|
@@ -3353,8 +4768,8 @@ const nextKeys = (indexes, lead) => [...new Set(indexes.flatMap((i) => i.only ==
|
|
|
3353
4768
|
* list's order when its keys follow in its directions too (after an
|
|
3354
4769
|
* outbound hop the far row is one primary-key read, so all of it is served);
|
|
3355
4770
|
* - a `$ref` predicate is a residual on rows already narrowed: always served;
|
|
3356
|
-
* - a seed filter, or a
|
|
3357
|
-
* - a
|
|
4771
|
+
* - a seed filter, or the field a group key truncates, is served by an index leading with it;
|
|
4772
|
+
* - an ordering by a select name is served only when a served seed filter bounds the
|
|
3358
4773
|
* result (AND-ed, not under OR or NOT, and not only `neq`); no index ever
|
|
3359
4774
|
* serves it, so it recommends none;
|
|
3360
4775
|
* - anything else is unserved, recommending the index that would serve it
|
|
@@ -3430,14 +4845,15 @@ function classifyWalkService(input, indexesOf) {
|
|
|
3430
4845
|
served: true
|
|
3431
4846
|
});
|
|
3432
4847
|
}
|
|
3433
|
-
if (isObj$3(params.
|
|
3434
|
-
const field =
|
|
3435
|
-
clause(["
|
|
4848
|
+
if (isObj$3(params.groupBy)) for (const [name, e] of Object.entries(params.groupBy)) {
|
|
4849
|
+
const field = (isObj$3(e) && Array.isArray(e.dateTrunc) ? e.dateTrunc : void 0)?.[1];
|
|
4850
|
+
if (typeof field === "string") clause(["groupBy", name], "group", leads(seedIndexes, [field], []), seed, [field]);
|
|
3436
4851
|
}
|
|
4852
|
+
const selected = isObj$3(params.select) ? params.select : {};
|
|
3437
4853
|
orderOf(params.orderBy).forEach(([f], i) => {
|
|
3438
|
-
if (
|
|
4854
|
+
if (Object.hasOwn(selected, f)) out.push({
|
|
3439
4855
|
path: ["orderBy", String(i)],
|
|
3440
|
-
kind: "
|
|
4856
|
+
kind: "select",
|
|
3441
4857
|
served: bounded
|
|
3442
4858
|
});
|
|
3443
4859
|
});
|
|
@@ -3911,8 +5327,9 @@ var Registry = class extends RegistryBase {
|
|
|
3911
5327
|
/** Structural detector for the typed-edge-walk query grammar. Subscriptions and streams use it to refuse walks (query-only). */
|
|
3912
5328
|
const WALK_PARAM_KEYS = [
|
|
3913
5329
|
"aggregate",
|
|
3914
|
-
"
|
|
3915
|
-
"
|
|
5330
|
+
"groupBy",
|
|
5331
|
+
"having",
|
|
5332
|
+
"offset"
|
|
3916
5333
|
];
|
|
3917
5334
|
const isObj$2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
3918
5335
|
function inWhere(v, path) {
|
|
@@ -4021,6 +5438,9 @@ function fieldKindOf(def) {
|
|
|
4021
5438
|
switch (def.kind) {
|
|
4022
5439
|
case "scalar": switch (def.type) {
|
|
4023
5440
|
case "number": return "scalar.number";
|
|
5441
|
+
case "int16": return "scalar.int16";
|
|
5442
|
+
case "int32": return "scalar.int32";
|
|
5443
|
+
case "int64": return "scalar.int64";
|
|
4024
5444
|
case "string": return "scalar.string";
|
|
4025
5445
|
case "boolean": return "scalar.boolean";
|
|
4026
5446
|
case "date": return "scalar.date";
|
|
@@ -4036,6 +5456,9 @@ function fieldKindOf(def) {
|
|
|
4036
5456
|
function opsAllowedFor(kind) {
|
|
4037
5457
|
switch (kind) {
|
|
4038
5458
|
case "scalar.number":
|
|
5459
|
+
case "scalar.int16":
|
|
5460
|
+
case "scalar.int32":
|
|
5461
|
+
case "scalar.int64":
|
|
4039
5462
|
case "scalar.date": return NUMBER_OPS;
|
|
4040
5463
|
case "scalar.string": return STRING_OPS;
|
|
4041
5464
|
case "scalar.boolean": return BOOLEAN_OPS;
|
|
@@ -4052,6 +5475,9 @@ function opsAllowedFor(kind) {
|
|
|
4052
5475
|
*/
|
|
4053
5476
|
const FIELD_KINDS = [
|
|
4054
5477
|
"scalar.number",
|
|
5478
|
+
"scalar.int16",
|
|
5479
|
+
"scalar.int32",
|
|
5480
|
+
"scalar.int64",
|
|
4055
5481
|
"scalar.string",
|
|
4056
5482
|
"scalar.boolean",
|
|
4057
5483
|
"scalar.date",
|
|
@@ -4077,7 +5503,7 @@ function effectiveFilterOperators(capabilities) {
|
|
|
4077
5503
|
return out;
|
|
4078
5504
|
}
|
|
4079
5505
|
function isPrimitive(v) {
|
|
4080
|
-
return v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean" || v instanceof Date;
|
|
5506
|
+
return v === null || typeof v === "string" || typeof v === "number" || typeof v === "bigint" || typeof v === "boolean" || v instanceof Date;
|
|
4081
5507
|
}
|
|
4082
5508
|
/**
|
|
4083
5509
|
* Check the value shape for a given operator against the field's declared
|
|
@@ -4113,6 +5539,15 @@ function validatePrimitiveKind(fieldKind, value, path, op) {
|
|
|
4113
5539
|
case "scalar.number":
|
|
4114
5540
|
if (typeof value !== "number") throw new FilterValidationError("type_mismatch", `operator '${op}' expected number, got ${typeof value}`, path);
|
|
4115
5541
|
return;
|
|
5542
|
+
case "scalar.int16":
|
|
5543
|
+
if (!isInt16(value)) throw new FilterValidationError("type_mismatch", `operator '${op}' expected an int16 (an integer from -32768 to 32767), got ${JSON.stringify(String(value))}`, path);
|
|
5544
|
+
return;
|
|
5545
|
+
case "scalar.int32":
|
|
5546
|
+
if (!isInt32(value)) throw new FilterValidationError("type_mismatch", `operator '${op}' expected an int32 (an integer from -2147483648 to 2147483647), got ${JSON.stringify(String(value))}`, path);
|
|
5547
|
+
return;
|
|
5548
|
+
case "scalar.int64":
|
|
5549
|
+
if (parseInt64(value) === void 0) throw new FilterValidationError("type_mismatch", `operator '${op}' expected an int64 (a safe integer, a decimal string or a bigint), got ${JSON.stringify(String(value))}`, path);
|
|
5550
|
+
return;
|
|
4116
5551
|
case "scalar.string":
|
|
4117
5552
|
if (typeof value !== "string") throw new FilterValidationError("type_mismatch", `operator '${op}' expected string, got ${typeof value}`, path);
|
|
4118
5553
|
return;
|
|
@@ -4219,7 +5654,8 @@ function validateWhereAgainstFields(where, fields, path, capabilities, ctx) {
|
|
|
4219
5654
|
...ctx,
|
|
4220
5655
|
walk: bindGroup(where, path, ctx.walk)
|
|
4221
5656
|
};
|
|
4222
|
-
|
|
5657
|
+
if (Object.hasOwn(where, "$expr")) validateWhereExpr(where, path, ctx);
|
|
5658
|
+
const keys = Object.keys(where).filter((k) => k !== "$expr");
|
|
4223
5659
|
const hasLogical = keys.some((k) => LOGICAL_KEYS.has(k));
|
|
4224
5660
|
const hasField = keys.some((k) => !LOGICAL_KEYS.has(k));
|
|
4225
5661
|
if (hasLogical && hasField) throw new FilterValidationError("invalid_logical", `mixing logical combinators (AND/OR/NOT) with field keys at the same level is not allowed`, path);
|
|
@@ -4267,6 +5703,13 @@ function validateWhereAgainstFields(where, fields, path, capabilities, ctx) {
|
|
|
4267
5703
|
if (def === METADATA_VIRTUAL_FIELDS.seq) assertSeqIntegers(where[fieldName], [...path, fieldName]);
|
|
4268
5704
|
}
|
|
4269
5705
|
}
|
|
5706
|
+
/** `where.$expr`: a boolean expression over the seed row's fields, outside walks and structs. */
|
|
5707
|
+
function validateWhereExpr(where, path, ctx) {
|
|
5708
|
+
const at = [...path, "$expr"];
|
|
5709
|
+
if (ctx.walk.depth > 0 || ctx.insideStruct === true) throw new FilterValidationError("expr_invalid", "$expr is valid only in the frame's own where, not inside a walk or struct", at);
|
|
5710
|
+
const type = checkFrameExpr(where.$expr, ctx.walk, false, at);
|
|
5711
|
+
if (type !== "boolean") throw new FilterValidationError("expr_invalid", `$expr must be boolean, got ${type}`, at);
|
|
5712
|
+
}
|
|
4270
5713
|
const AGG_COMPARATORS = /* @__PURE__ */ new Set([
|
|
4271
5714
|
"eq",
|
|
4272
5715
|
"neq",
|
|
@@ -4313,7 +5756,7 @@ function validateAggPredicate(record, path, scope, h) {
|
|
|
4313
5756
|
function assertSeqIntegers(leaf, path) {
|
|
4314
5757
|
for (const [op, operand] of Object.entries(leaf)) for (const v of Array.isArray(operand) ? operand : [operand]) if (typeof v === "number" && !Number.isSafeInteger(v)) throw new FilterValidationError("type_mismatch", `operator '${op}' expected an integer for 'seq', got ${v}`, [...path, op]);
|
|
4315
5758
|
}
|
|
4316
|
-
function validateOrderBy(orderBy, fields, capabilities, path = ["orderBy"],
|
|
5759
|
+
function validateOrderBy(orderBy, fields, capabilities, path = ["orderBy"], selected = /* @__PURE__ */ new Set()) {
|
|
4317
5760
|
if (!capabilities.orderBy) throw new FilterValidationError("capability_unsupported", "dataset does not support orderBy", path);
|
|
4318
5761
|
if (!Array.isArray(orderBy)) throw new FilterValidationError("type_mismatch", "orderBy must be an array of { field: \"asc\" | \"desc\" } entries", path);
|
|
4319
5762
|
orderBy.forEach((clause, i) => {
|
|
@@ -4327,14 +5770,7 @@ function validateOrderBy(orderBy, fields, capabilities, path = ["orderBy"], deri
|
|
|
4327
5770
|
fieldName
|
|
4328
5771
|
]);
|
|
4329
5772
|
if (fieldName === "id") return;
|
|
4330
|
-
if (
|
|
4331
|
-
if (derived.has(fieldName.slice(1))) return;
|
|
4332
|
-
throw new FilterValidationError("derive_invalid", `orderBy '${fieldName}' needs derive.${fieldName.slice(1)} in params`, [
|
|
4333
|
-
...path,
|
|
4334
|
-
String(i),
|
|
4335
|
-
fieldName
|
|
4336
|
-
]);
|
|
4337
|
-
}
|
|
5773
|
+
if (selected.has(fieldName)) return;
|
|
4338
5774
|
const def = lookupField(fields, fieldName);
|
|
4339
5775
|
if (!def) throw new FilterValidationError("unknown_field", `unknown field '${fieldName}' in orderBy${metadataHint(fieldName)}`, [
|
|
4340
5776
|
...path,
|
|
@@ -4365,7 +5801,8 @@ function validateFilter(input) {
|
|
|
4365
5801
|
hidden: /* @__PURE__ */ new Set(),
|
|
4366
5802
|
underOrNot: false,
|
|
4367
5803
|
groupBound: false,
|
|
4368
|
-
frameBindings: { count: 0 }
|
|
5804
|
+
frameBindings: { count: 0 },
|
|
5805
|
+
exprNodes: { count: 0 }
|
|
4369
5806
|
};
|
|
4370
5807
|
const h = frameHelpers(input.capabilities);
|
|
4371
5808
|
const aggs = validateSelection(input.fields, ["fields"], seed, h);
|
|
@@ -4374,12 +5811,21 @@ function validateFilter(input) {
|
|
|
4374
5811
|
aggs
|
|
4375
5812
|
} : seed;
|
|
4376
5813
|
if (input.where !== void 0) validateWhereAgainstFields(input.where, fields, ["where"], input.capabilities, { walk: scope });
|
|
4377
|
-
const
|
|
4378
|
-
if (input.orderBy !== void 0) validateOrderBy(input.orderBy, fields, input.capabilities, ["orderBy"],
|
|
5814
|
+
const { selected } = validateFrameParams(input, scope, h);
|
|
5815
|
+
if (input.orderBy !== void 0) validateOrderBy(input.orderBy, fields, input.capabilities, ["orderBy"], selected);
|
|
4379
5816
|
}
|
|
4380
5817
|
|
|
4381
5818
|
//#endregion
|
|
4382
5819
|
//#region ../../packages/substrate-core/src/filter/walk-validator.ts
|
|
5820
|
+
/**
|
|
5821
|
+
* Typed edge walk validation (spec 2026-10-24-typed-edge-walks §1, §2).
|
|
5822
|
+
*
|
|
5823
|
+
* Checks `_walk` nodes, `_agg` aggregates (where-side and in `fields`),
|
|
5824
|
+
* nested `{ _walk }` selections, `$ref` operands, and the frame params
|
|
5825
|
+
* `count`, `aggregate`, `groupBy` and `select`. The recursion into a walk's far-side `where` is handed back to the
|
|
5826
|
+
* filter validator, so the far type's fields and operators are checked exactly
|
|
5827
|
+
* as a top-level read of that type would be.
|
|
5828
|
+
*/
|
|
4383
5829
|
const MAX_WALK_DEPTH = 3;
|
|
4384
5830
|
/** Joint quantification enumerates the product of the bound walks' neighbourhoods, so cap the factors. */
|
|
4385
5831
|
const MAX_GROUP_BINDINGS = 3;
|
|
@@ -4612,14 +6058,13 @@ const WALK_AGG_KEYS = /* @__PURE__ */ new Set([
|
|
|
4612
6058
|
"out",
|
|
4613
6059
|
"where"
|
|
4614
6060
|
]);
|
|
4615
|
-
const NO_KEYS = /* @__PURE__ */ new Set();
|
|
4616
6061
|
/**
|
|
4617
6062
|
* One aggregate function over `owner`'s fields, returning its value kind:
|
|
4618
6063
|
* `count`, `avg` and `sum` are numbers; `min`, `max` and `latest` take their
|
|
4619
|
-
* field's kind. `latest` picks a row
|
|
6064
|
+
* field's kind. `latest` picks a row: it belongs to a per-row walk aggregate.
|
|
4620
6065
|
*/
|
|
4621
|
-
function aggKind(agg,
|
|
4622
|
-
for (const k of Object.keys(agg)) if (!
|
|
6066
|
+
function aggKind(agg, owner, path, h) {
|
|
6067
|
+
for (const k of Object.keys(agg)) if (!WALK_AGG_KEYS.has(k) && !AGG_FNS.has(k)) fail("agg_invalid", `unknown aggregate key '${k}'`, [...path, k]);
|
|
4623
6068
|
const fns = Object.keys(agg).filter((k) => AGG_FNS.has(k));
|
|
4624
6069
|
if (fns.length !== 1) fail("agg_invalid", "an aggregate names exactly one of count, avg, sum, min, max or latest", path);
|
|
4625
6070
|
const [fn] = fns;
|
|
@@ -4643,7 +6088,6 @@ function aggKind(agg, extra, owner, path, h, allowLatest) {
|
|
|
4643
6088
|
case "min":
|
|
4644
6089
|
case "max": return field(v, at, (k) => DATE_OR_NUMBER.has(k), "a number or date field");
|
|
4645
6090
|
default:
|
|
4646
|
-
if (!allowLatest) fail("agg_invalid", "latest picks one row per seed row; a frame aggregate takes count, avg, sum, min or max", at);
|
|
4647
6091
|
if (!isObj$1(v) || Object.keys(v).some((k) => k !== "by" && k !== "value")) fail("agg_invalid", "latest takes { by: <dateField>, value: <field> }", at);
|
|
4648
6092
|
field(v.by, [...at, "by"], (k) => k === "scalar.date", "a date field");
|
|
4649
6093
|
return field(v.value, [...at, "value"], (k) => k.startsWith("scalar.") && k !== "scalar.json", "a scalar field");
|
|
@@ -4653,7 +6097,7 @@ function aggKind(agg, extra, owner, path, h, allowLatest) {
|
|
|
4653
6097
|
function validateWalkAgg(agg, path, scope, h) {
|
|
4654
6098
|
if (!isObj$1(agg)) fail("agg_invalid", "_agg expects an aggregate object", path);
|
|
4655
6099
|
const hop = resolveHop(agg, path, scope);
|
|
4656
|
-
const kind = aggKind(agg,
|
|
6100
|
+
const kind = aggKind(agg, hop, path, h);
|
|
4657
6101
|
if (agg.where !== void 0) h.validateWhere(agg.where, [...path, "where"], farScope(scope, hop));
|
|
4658
6102
|
return kind;
|
|
4659
6103
|
}
|
|
@@ -4730,90 +6174,180 @@ function validateSelection(fields, path, scope, h) {
|
|
|
4730
6174
|
}
|
|
4731
6175
|
return aggs;
|
|
4732
6176
|
}
|
|
6177
|
+
/** Row keys a read projects besides the type's fields, which a select value must not overwrite. */
|
|
6178
|
+
const RESERVED_ROW_KEYS = /* @__PURE__ */ new Set([
|
|
6179
|
+
"id",
|
|
6180
|
+
"shortId",
|
|
6181
|
+
"_metadata",
|
|
6182
|
+
"_deletedAt",
|
|
6183
|
+
"createdAt",
|
|
6184
|
+
"updatedAt",
|
|
6185
|
+
"createdBy",
|
|
6186
|
+
"updatedBy",
|
|
6187
|
+
"createdById",
|
|
6188
|
+
"createdByKind",
|
|
6189
|
+
"updatedById",
|
|
6190
|
+
"updatedByKind",
|
|
6191
|
+
"seq"
|
|
6192
|
+
]);
|
|
6193
|
+
/** The expression type of a walk aggregate's value kind; a boolean or int64 value is not usable. */
|
|
6194
|
+
const AGG_EXPR_TYPE = {
|
|
6195
|
+
"scalar.number": "number",
|
|
6196
|
+
"scalar.date": "date",
|
|
6197
|
+
"scalar.string": "string"
|
|
6198
|
+
};
|
|
6199
|
+
function exprScopeOf(scope, withAgg) {
|
|
6200
|
+
return { field(name) {
|
|
6201
|
+
if (name.startsWith("_agg.")) {
|
|
6202
|
+
const kind = withAgg ? scope.aggs?.get(name.slice(5)) : void 0;
|
|
6203
|
+
return kind === void 0 ? void 0 : AGG_EXPR_TYPE[kind];
|
|
6204
|
+
}
|
|
6205
|
+
return (scope.outputsOnly ? void 0 : metadataExprType(name)) ?? (Object.hasOwn(scope.fields, name) ? exprTypeOfField(scope.fields[name]) : void 0);
|
|
6206
|
+
} };
|
|
6207
|
+
}
|
|
6208
|
+
/** Refuses a bare `_agg.<name>` whose value type (boolean, int64, ...) no expression can use. */
|
|
6209
|
+
function refuseUnusableAgg(expr, scope, path, depth = 0) {
|
|
6210
|
+
if (depth > 2 * EXPR_LIMITS.maxDepth + 2) return;
|
|
6211
|
+
if (typeof expr === "string") {
|
|
6212
|
+
const kind = expr.startsWith("_agg.") ? scope.aggs?.get(expr.slice(5)) : void 0;
|
|
6213
|
+
if (kind !== void 0 && AGG_EXPR_TYPE[kind] === void 0) fail("expr_invalid", `'${expr}' has a ${kind.replace("scalar.", "")} value, which an expression cannot use (only number, date and string aggregates)`, path);
|
|
6214
|
+
} else if (Array.isArray(expr)) for (const e of expr) refuseUnusableAgg(e, scope, path, depth + 1);
|
|
6215
|
+
else if (isObj$1(expr)) {
|
|
6216
|
+
for (const [k, v] of Object.entries(expr)) if (k !== "lit" && k !== "int64") refuseUnusableAgg(v, scope, path, depth + 1);
|
|
6217
|
+
}
|
|
6218
|
+
}
|
|
6219
|
+
/** Checks one expression of the frame, charging its nodes to the frame's budget; returns its type. */
|
|
6220
|
+
function checkFrameExpr(expr, scope, withAgg, path) {
|
|
6221
|
+
if (withAgg) refuseUnusableAgg(expr, scope, path);
|
|
6222
|
+
const r = checkExpr(expr, exprScopeOf(scope, withAgg), path);
|
|
6223
|
+
if (!r.ok) fail("expr_invalid", r.message, r.path);
|
|
6224
|
+
const used = scope.exprNodes ?? { count: 0 };
|
|
6225
|
+
used.count += r.nodes;
|
|
6226
|
+
if (used.count > EXPR_LIMITS.maxNodesPerFrame) fail("expr_invalid", `the frame's expressions have more than maxNodesPerFrame (${EXPR_LIMITS.maxNodesPerFrame}) nodes`, path);
|
|
6227
|
+
return r.type;
|
|
6228
|
+
}
|
|
4733
6229
|
/**
|
|
4734
|
-
* `count`, `aggregate`, `
|
|
4735
|
-
* Returns the
|
|
6230
|
+
* `count`, `aggregate`, `groupBy` and `select`, and what each excludes.
|
|
6231
|
+
* Returns the names `orderBy` may use: select names and group outputs.
|
|
4736
6232
|
*/
|
|
4737
6233
|
function validateFrameParams(p, scope, h) {
|
|
4738
6234
|
if (p.count !== void 0 && typeof p.count !== "boolean") fail("type_mismatch", "count takes true or false", ["count"]);
|
|
4739
6235
|
if (p.count === true) {
|
|
4740
6236
|
for (const k of [
|
|
4741
6237
|
"aggregate",
|
|
4742
|
-
"
|
|
4743
|
-
"derive",
|
|
6238
|
+
"select",
|
|
4744
6239
|
"orderBy",
|
|
4745
6240
|
"cursor"
|
|
4746
6241
|
]) if (p[k] !== void 0) fail("count_conflict", `count returns a count; it takes no '${k}'`, [k]);
|
|
4747
6242
|
for (const k of Object.keys(p.fields ?? {})) if (k !== "id") fail("count_conflict", `count returns no rows; it selects only id, not '${k}'`, ["fields", k]);
|
|
4748
6243
|
}
|
|
4749
|
-
const
|
|
4750
|
-
|
|
4751
|
-
|
|
4752
|
-
|
|
6244
|
+
const outputs = /* @__PURE__ */ new Map();
|
|
6245
|
+
const grouped = p.aggregate !== void 0;
|
|
6246
|
+
for (const k of [
|
|
6247
|
+
"groupBy",
|
|
6248
|
+
"having",
|
|
6249
|
+
"offset"
|
|
6250
|
+
]) if (p[k] !== void 0 && !grouped) fail("agg_invalid", `${k} shapes an aggregate's groups; it needs aggregate`, [k]);
|
|
4753
6251
|
if (p.aggregate !== void 0) {
|
|
4754
|
-
|
|
4755
|
-
"
|
|
6252
|
+
const refused = p.groupBy !== void 0 ? ["select", "cursor"] : [
|
|
6253
|
+
"select",
|
|
4756
6254
|
"orderBy",
|
|
4757
6255
|
"cursor"
|
|
4758
|
-
]
|
|
6256
|
+
];
|
|
6257
|
+
for (const k of refused) if (p[k] !== void 0) fail("agg_invalid", `aggregate returns aggregate rows; it takes no '${k}'`, [k]);
|
|
4759
6258
|
for (const k of Object.keys(p.fields ?? {})) if (k !== "id") fail("agg_invalid", `aggregate returns aggregate rows; it selects only id, not '${k}'`, ["fields", k]);
|
|
4760
6259
|
if (!isObj$1(p.aggregate)) fail("agg_invalid", "aggregate maps names to aggregates", ["aggregate"]);
|
|
4761
6260
|
if (Object.keys(p.aggregate).length === 0) fail("agg_invalid", "aggregate needs at least one entry", ["aggregate"]);
|
|
4762
6261
|
for (const [n, agg] of Object.entries(p.aggregate)) {
|
|
4763
6262
|
const at = ["aggregate", n];
|
|
4764
|
-
if (!IDENT.test(n)
|
|
6263
|
+
if (!IDENT.test(n)) fail("agg_invalid", `aggregate name '${n}' must be an identifier`, at);
|
|
4765
6264
|
if (!isObj$1(agg)) fail("agg_invalid", "an aggregate is an object such as { count: true }", at);
|
|
4766
|
-
|
|
4767
|
-
}
|
|
4768
|
-
}
|
|
4769
|
-
if (p.bucket !== void 0) {
|
|
4770
|
-
const b = p.bucket;
|
|
4771
|
-
if (p.aggregate === void 0) fail("bucket_invalid", "bucket groups an aggregate; it needs aggregate", ["bucket"]);
|
|
4772
|
-
if (!isObj$1(b) || Object.keys(b).some((k) => k !== "field" && k !== "by")) fail("bucket_invalid", "bucket takes { field, by }", ["bucket"]);
|
|
4773
|
-
if (b.by !== "day" && b.by !== "hour") fail("bucket_invalid", "bucket by is 'day' or 'hour'", ["bucket", "by"]);
|
|
4774
|
-
const def = typeof b.field === "string" ? h.lookup(scope.fields, b.field) : void 0;
|
|
4775
|
-
if (def === void 0 || h.kindOf(def) !== "scalar.date") fail("bucket_invalid", `bucket field '${String(b.field)}' is not a date field`, ["bucket", "field"]);
|
|
4776
|
-
}
|
|
4777
|
-
const derived = /* @__PURE__ */ new Set();
|
|
4778
|
-
if (p.derive !== void 0) {
|
|
4779
|
-
if (!isObj$1(p.derive)) fail("derive_invalid", "derive maps names to { diff }, { ratio } or { value }", ["derive"]);
|
|
4780
|
-
for (const [n, spec] of Object.entries(p.derive)) {
|
|
4781
|
-
const at = ["derive", n];
|
|
4782
|
-
if (!IDENT.test(n)) fail("derive_invalid", `derive name '${n}' must be an identifier`, at);
|
|
4783
|
-
const entries = isObj$1(spec) ? Object.entries(spec) : [];
|
|
4784
|
-
const [op, operands] = entries[0] ?? [];
|
|
4785
|
-
if (entries.length === 1 && op === "value") {
|
|
4786
|
-
if (typeof operands !== "string") fail("derive_invalid", "a derive { value } takes one operand such as '_agg.x'", at);
|
|
4787
|
-
deriveOperand(operands, [...at, op], scope, h);
|
|
4788
|
-
derived.add(n);
|
|
4789
|
-
continue;
|
|
4790
|
-
}
|
|
4791
|
-
if (entries.length !== 1 || op !== "diff" && op !== "ratio" || !Array.isArray(operands) || operands.length !== 2) fail("derive_invalid", "a derive is { diff: [a, b] }, { ratio: [a, b] } or { value: a }", at);
|
|
4792
|
-
operands.forEach((o, i) => {
|
|
4793
|
-
deriveOperand(o, [
|
|
4794
|
-
...at,
|
|
4795
|
-
op,
|
|
4796
|
-
String(i)
|
|
4797
|
-
], scope, h);
|
|
4798
|
-
});
|
|
4799
|
-
derived.add(n);
|
|
6265
|
+
outputs.set(n, frameAggType(agg, scope, at));
|
|
4800
6266
|
}
|
|
4801
6267
|
}
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
|
|
6268
|
+
if (p.groupBy !== void 0) {
|
|
6269
|
+
if (!isObj$1(p.groupBy) || Object.keys(p.groupBy).length === 0) fail("group_invalid", "groupBy maps names to expressions, and needs at least one", ["groupBy"]);
|
|
6270
|
+
for (const [n, expr] of Object.entries(p.groupBy)) {
|
|
6271
|
+
const at = ["groupBy", n];
|
|
6272
|
+
if (!IDENT.test(n)) fail("group_invalid", `groupBy name '${n}' must be an identifier`, at);
|
|
6273
|
+
if (outputs.has(n)) fail("group_invalid", `groupBy name '${n}' is also an aggregate name`, at);
|
|
6274
|
+
outputs.set(n, checkFrameExpr(expr, scope, false, at));
|
|
6275
|
+
}
|
|
6276
|
+
}
|
|
6277
|
+
if (p.having !== void 0) {
|
|
6278
|
+
const named = {
|
|
6279
|
+
...scope,
|
|
6280
|
+
outputsOnly: true,
|
|
6281
|
+
fields: Object.fromEntries([...outputs].map(([n, t]) => [n, {
|
|
6282
|
+
kind: "scalar",
|
|
6283
|
+
type: t
|
|
6284
|
+
}]))
|
|
6285
|
+
};
|
|
6286
|
+
const t = checkFrameExpr(p.having, named, false, ["having"]);
|
|
6287
|
+
if (t !== "boolean") fail("expr_invalid", `having must be boolean, got ${t}`, ["having"]);
|
|
6288
|
+
}
|
|
6289
|
+
if (p.offset !== void 0 && (!Number.isSafeInteger(p.offset) || p.offset < 0)) fail("type_mismatch", "offset takes a non-negative integer", ["offset"]);
|
|
6290
|
+
if (p.groupBy !== void 0 && Array.isArray(p.orderBy)) p.orderBy.forEach((c, i) => {
|
|
6291
|
+
const k = isObj$1(c) ? Object.keys(c)[0] : void 0;
|
|
6292
|
+
if (k !== void 0 && !outputs.has(k)) fail("agg_invalid", `orderBy '${k}' names no groupBy or aggregate output`, [
|
|
6293
|
+
"orderBy",
|
|
6294
|
+
String(i),
|
|
6295
|
+
k
|
|
6296
|
+
]);
|
|
6297
|
+
});
|
|
6298
|
+
const selected = /* @__PURE__ */ new Set();
|
|
6299
|
+
if (p.select !== void 0) {
|
|
6300
|
+
if (!isObj$1(p.select)) fail("select_invalid", "select maps names to expressions", ["select"]);
|
|
6301
|
+
for (const [n, expr] of Object.entries(p.select)) {
|
|
6302
|
+
const at = ["select", n];
|
|
6303
|
+
if (!IDENT.test(n)) fail("select_invalid", `select name '${n}' must be an identifier`, at);
|
|
6304
|
+
const nested = p.fields?.[n];
|
|
6305
|
+
if (h.lookup(scope.fields, n) !== void 0 || RESERVED_ROW_KEYS.has(n) || isObj$1(nested) && "_walk" in nested) fail("select_invalid", `select name '${n}' collides with a field, a row key or a nested selection of the type`, at);
|
|
6306
|
+
checkFrameExpr(expr, scope, true, at);
|
|
6307
|
+
selected.add(n);
|
|
4814
6308
|
}
|
|
4815
6309
|
}
|
|
4816
|
-
|
|
6310
|
+
return { selected: /* @__PURE__ */ new Set([...selected, ...outputs.keys()]) };
|
|
6311
|
+
}
|
|
6312
|
+
const FRAME_AGG_FNS = [
|
|
6313
|
+
"count",
|
|
6314
|
+
"countDistinct",
|
|
6315
|
+
"avg",
|
|
6316
|
+
"sum",
|
|
6317
|
+
"min",
|
|
6318
|
+
"max"
|
|
6319
|
+
];
|
|
6320
|
+
/** A frame aggregate: one function over an expression (`count: true` counts rows). Returns its value type. */
|
|
6321
|
+
function frameAggType(agg, scope, path) {
|
|
6322
|
+
for (const k of Object.keys(agg)) {
|
|
6323
|
+
if (k === "latest") fail("agg_invalid", "latest picks one row per seed row; a frame aggregate takes count, countDistinct, avg, sum, min or max", [...path, k]);
|
|
6324
|
+
if (!FRAME_AGG_FNS.includes(k)) fail("agg_invalid", `unknown aggregate key '${k}'`, [...path, k]);
|
|
6325
|
+
}
|
|
6326
|
+
if (Object.keys(agg).length !== 1) fail("agg_invalid", "an aggregate names exactly one of count, countDistinct, avg, sum, min or max", path);
|
|
6327
|
+
const [fn] = Object.keys(agg);
|
|
6328
|
+
const at = [...path, fn];
|
|
6329
|
+
if (fn === "count" && agg.count === true) return "number";
|
|
6330
|
+
const arg = agg[fn];
|
|
6331
|
+
if (fn === "count" && (arg === null || typeof arg === "number" || typeof arg === "boolean" || isObj$1(arg) && ("lit" in arg || "int64" in arg))) fail("agg_invalid", "count takes true (rows) or an expression, not another literal", at);
|
|
6332
|
+
const t = checkFrameExpr(arg, scope, false, at);
|
|
6333
|
+
const ok = {
|
|
6334
|
+
avg: ["number", "int64"],
|
|
6335
|
+
sum: ["number", "int64"],
|
|
6336
|
+
min: [
|
|
6337
|
+
"number",
|
|
6338
|
+
"int64",
|
|
6339
|
+
"string",
|
|
6340
|
+
"date"
|
|
6341
|
+
],
|
|
6342
|
+
max: [
|
|
6343
|
+
"number",
|
|
6344
|
+
"int64",
|
|
6345
|
+
"string",
|
|
6346
|
+
"date"
|
|
6347
|
+
]
|
|
6348
|
+
};
|
|
6349
|
+
if (ok[fn] !== void 0 && !ok[fn].includes(t)) fail("agg_invalid", `${fn} needs ${ok[fn].join(", ")}; the argument is ${t}`, at);
|
|
6350
|
+
return fn === "count" || fn === "countDistinct" || fn === "avg" ? "number" : t;
|
|
4817
6351
|
}
|
|
4818
6352
|
|
|
4819
6353
|
//#endregion
|
|
@@ -5417,7 +6951,8 @@ function runFilterGuard(deps, input) {
|
|
|
5417
6951
|
if (entry.fields !== void 0) {
|
|
5418
6952
|
const typeSchema = deps.schemaFor(ns).types[name];
|
|
5419
6953
|
if (typeSchema) {
|
|
5420
|
-
const
|
|
6954
|
+
const selectNames = entry.params?.select ?? {};
|
|
6955
|
+
const unknown = Object.keys(entry.fields).filter((f) => !(f in typeSchema.fields) && !PROJECTION_VIRTUAL_KEYS.has(f) && computedFieldOwner(f) === void 0 && !isWalkSelection(f, entry.fields[f]) && !Object.hasOwn(selectNames, f));
|
|
5421
6956
|
if (unknown.length > 0) {
|
|
5422
6957
|
envelope._diagnostics ??= {};
|
|
5423
6958
|
envelope._diagnostics[key] = { unknownProjectionFields: unknown };
|
|
@@ -5429,11 +6964,12 @@ function runFilterGuard(deps, input) {
|
|
|
5429
6964
|
}
|
|
5430
6965
|
}
|
|
5431
6966
|
const walks = usesWalkGrammar(entry);
|
|
5432
|
-
|
|
6967
|
+
const selects = entry.params?.select !== void 0;
|
|
6968
|
+
if (where === void 0 && orderBy === void 0 && !walks && !selects) return true;
|
|
5433
6969
|
return validateAgainst(deps, envelope, key, ns, name, dataset, {
|
|
5434
6970
|
...where !== void 0 ? { where } : {},
|
|
5435
6971
|
...orderBy !== void 0 ? { orderBy } : {},
|
|
5436
|
-
...walks ? walkInput(entry) : {}
|
|
6972
|
+
...walks || selects ? walkInput(entry) : {}
|
|
5437
6973
|
});
|
|
5438
6974
|
}
|
|
5439
6975
|
/**
|
|
@@ -5489,12 +7025,14 @@ function isWalkSelection(name, sel) {
|
|
|
5489
7025
|
}
|
|
5490
7026
|
/** The walk-grammar params and field selection `validateFilter` checks. */
|
|
5491
7027
|
function walkInput(entry) {
|
|
5492
|
-
const { count, aggregate,
|
|
7028
|
+
const { count, aggregate, groupBy, having, offset, select, cursor } = entry.params ?? {};
|
|
5493
7029
|
return {
|
|
5494
7030
|
count,
|
|
5495
7031
|
aggregate,
|
|
5496
|
-
|
|
5497
|
-
|
|
7032
|
+
groupBy,
|
|
7033
|
+
having,
|
|
7034
|
+
offset,
|
|
7035
|
+
select,
|
|
5498
7036
|
cursor,
|
|
5499
7037
|
...entry.fields !== void 0 ? { fields: entry.fields } : {}
|
|
5500
7038
|
};
|
|
@@ -6287,6 +7825,7 @@ const KNOWN_SUBSCRIBE_PARAMS = /* @__PURE__ */ new Set([
|
|
|
6287
7825
|
"orderBy",
|
|
6288
7826
|
"_allowTableScan"
|
|
6289
7827
|
]);
|
|
7828
|
+
const NO_FIELDS = () => void 0;
|
|
6290
7829
|
var SubscribeSubsystem = class {
|
|
6291
7830
|
#namespaces;
|
|
6292
7831
|
#datasets;
|
|
@@ -6295,6 +7834,8 @@ var SubscribeSubsystem = class {
|
|
|
6295
7834
|
#subscriptionQueueLimit;
|
|
6296
7835
|
#snapshotLimit;
|
|
6297
7836
|
#schemaFor;
|
|
7837
|
+
/** One field lookup per type's field map, so the evaluator's per-node type cache holds across events. */
|
|
7838
|
+
#fieldDefs = /* @__PURE__ */ new WeakMap();
|
|
6298
7839
|
#isTombstoned;
|
|
6299
7840
|
/**
|
|
6300
7841
|
* Live subscription bus (§5.4). Keyed by `"<ns>/<type>"` so the mutate
|
|
@@ -6871,6 +8412,15 @@ var SubscribeSubsystem = class {
|
|
|
6871
8412
|
}
|
|
6872
8413
|
idSet.add(entry);
|
|
6873
8414
|
}
|
|
8415
|
+
#fieldDefOf(fields) {
|
|
8416
|
+
if (fields === void 0) return NO_FIELDS;
|
|
8417
|
+
let f = this.#fieldDefs.get(fields);
|
|
8418
|
+
if (f === void 0) {
|
|
8419
|
+
f = (name) => Object.hasOwn(fields, name) ? fields[name] : void 0;
|
|
8420
|
+
this.#fieldDefs.set(fields, f);
|
|
8421
|
+
}
|
|
8422
|
+
return f;
|
|
8423
|
+
}
|
|
6874
8424
|
/**
|
|
6875
8425
|
* Remove an entry from the bus + per-call index (mirrors the drain teardown).
|
|
6876
8426
|
* Used when a post-registration step bails before the snapshot ships.
|
|
@@ -6928,7 +8478,7 @@ var SubscribeSubsystem = class {
|
|
|
6928
8478
|
let whereMatches;
|
|
6929
8479
|
try {
|
|
6930
8480
|
const fields = this.#schemaFor(entry.ns).types[entry.type]?.fields;
|
|
6931
|
-
whereMatches = entry.where === void 0 ? true : evaluateWhere(row, entry.where, (
|
|
8481
|
+
whereMatches = entry.where === void 0 ? true : evaluateWhere(row, entry.where, this.#fieldDefOf(fields));
|
|
6932
8482
|
} catch (err) {
|
|
6933
8483
|
const code = isResolverFrameError(err) ? err.code : "resolver_error";
|
|
6934
8484
|
this.#enqueueFrame(entry, {
|
|
@@ -7779,7 +9329,7 @@ var Engine = class {
|
|
|
7779
9329
|
return null;
|
|
7780
9330
|
}
|
|
7781
9331
|
if (limit > limits.maxLimit) {
|
|
7782
|
-
addError(envelope, [key, "limit"], `limit ${limit} exceeds the maximum page of ${limits.maxLimit}; page with the returned cursor`, "limit_exceeds_max", { max: limits.maxLimit });
|
|
9332
|
+
addError(envelope, [key, "limit"], `limit ${limit} exceeds the maximum page of ${limits.maxLimit}; ${entry.params?.aggregate !== void 0 ? "page with offset" : "page with the returned cursor"}`, "limit_exceeds_max", { max: limits.maxLimit });
|
|
7783
9333
|
return null;
|
|
7784
9334
|
}
|
|
7785
9335
|
return entry;
|
|
@@ -9727,11 +11277,13 @@ function assertScalarTypes(params, fieldDefs, phase) {
|
|
|
9727
11277
|
if (def.kind !== "scalar" || !Object.hasOwn(params, name)) continue;
|
|
9728
11278
|
const v = params[name];
|
|
9729
11279
|
if (v === null) continue;
|
|
9730
|
-
if (
|
|
9731
|
-
|
|
9732
|
-
|
|
9733
|
-
phase
|
|
9734
|
-
|
|
11280
|
+
if (def.type === "int64") {
|
|
11281
|
+
const n = parseInt64(v);
|
|
11282
|
+
if (n !== void 0) params[name] = n;
|
|
11283
|
+
else throwInvalidScalar(name, v, def.type, phase);
|
|
11284
|
+
continue;
|
|
11285
|
+
}
|
|
11286
|
+
if (!(def.type === "number" ? typeof v === "number" && Number.isFinite(v) : def.type === "int16" ? isInt16(v) : def.type === "int32" ? isInt32(v) : def.type === "boolean" ? typeof v === "boolean" : def.type === "date" ? typeof v === "string" ? isIsoTimestamp(v) : v instanceof Date && Number.isFinite(v.getTime()) : true)) throwInvalidScalar(name, v, def.type, phase);
|
|
9735
11287
|
}
|
|
9736
11288
|
}
|
|
9737
11289
|
/**
|
|
@@ -9827,6 +11379,13 @@ function assertServerSetFieldsFillable(typeName, fieldDefs) {
|
|
|
9827
11379
|
const names = unfillable.map((n) => `'${n}'`).join(", ");
|
|
9828
11380
|
throw new Error(`type '${typeName}': serverSet field(s) ${names} are non-nullable with no default, but clients may not supply them on create and autoCRUD does not stamp serverSet fields, so autoCRUD create could never fill them. Declare them \`nullable: true\` and stamp them from server-side code, or give them a \`default\`.`);
|
|
9829
11381
|
}
|
|
11382
|
+
function throwInvalidScalar(name, v, type, phase) {
|
|
11383
|
+
throw new ResolverFrameError("invalid_field_type", `Cannot ${phase} '${name}' with ${JSON.stringify(typeof v === "bigint" ? String(v) : v)}: expected a ${type === "date" ? "date (ISO 8601 string)" : type}.`, {
|
|
11384
|
+
field: name,
|
|
11385
|
+
expected: type,
|
|
11386
|
+
phase
|
|
11387
|
+
});
|
|
11388
|
+
}
|
|
9830
11389
|
|
|
9831
11390
|
//#endregion
|
|
9832
11391
|
//#region ../../packages/substrate-sdk/src/autocrud/autocrud-get.ts
|
|
@@ -9860,8 +11419,10 @@ const KNOWN_LIST_PARAMS = /* @__PURE__ */ new Set([
|
|
|
9860
11419
|
"_allowTableScan",
|
|
9861
11420
|
"count",
|
|
9862
11421
|
"aggregate",
|
|
9863
|
-
"
|
|
9864
|
-
"
|
|
11422
|
+
"groupBy",
|
|
11423
|
+
"having",
|
|
11424
|
+
"offset",
|
|
11425
|
+
"select"
|
|
9865
11426
|
]);
|
|
9866
11427
|
function extractLimit(params) {
|
|
9867
11428
|
if (!params) return void 0;
|
|
@@ -9890,8 +11451,10 @@ function listResolver(typeName, dataset, options = {}) {
|
|
|
9890
11451
|
const p = args.params ?? {};
|
|
9891
11452
|
if (p.count !== void 0) readParams.count = p.count;
|
|
9892
11453
|
if (p.aggregate !== void 0) readParams.aggregate = p.aggregate;
|
|
9893
|
-
if (p.
|
|
9894
|
-
if (p.
|
|
11454
|
+
if (p.groupBy !== void 0) readParams.groupBy = p.groupBy;
|
|
11455
|
+
if (p.having !== void 0) readParams.having = p.having;
|
|
11456
|
+
if (typeof p.offset === "number") readParams.offset = p.offset;
|
|
11457
|
+
if (p.select !== void 0) readParams.select = p.select;
|
|
9895
11458
|
const fields = args.fields;
|
|
9896
11459
|
if (fields !== void 0) {
|
|
9897
11460
|
const entries = Object.entries(fields);
|
|
@@ -11201,6 +12764,25 @@ const field = {
|
|
|
11201
12764
|
number(opts) {
|
|
11202
12765
|
return makeScalar("number", opts);
|
|
11203
12766
|
},
|
|
12767
|
+
/**
|
|
12768
|
+
* Integers: pick the narrowest kind whose range holds every value the field
|
|
12769
|
+
* will EVER take, because a kind cannot change once rows exist (moving means
|
|
12770
|
+
* a new field and a backfill). All three filter, sort and sum as exact
|
|
12771
|
+
* integers; they differ in storage width.
|
|
12772
|
+
* int16 -32768 to 32767, 2 bytes: small codes, flags, ages, percentages.
|
|
12773
|
+
* int32 about ±2.1 billion, 4 bytes: counts, durations, ids within one source.
|
|
12774
|
+
* int64 about ±9.2e18, 8 bytes, a decimal string on the wire past 2^53: totals, epoch-ms, external ids.
|
|
12775
|
+
* Use `number` for fractional values. When unsure, take the next width up.
|
|
12776
|
+
*/
|
|
12777
|
+
int16(opts) {
|
|
12778
|
+
return makeScalar("int16", opts);
|
|
12779
|
+
},
|
|
12780
|
+
int32(opts) {
|
|
12781
|
+
return makeScalar("int32", opts);
|
|
12782
|
+
},
|
|
12783
|
+
int64(opts) {
|
|
12784
|
+
return makeScalar("int64", opts);
|
|
12785
|
+
},
|
|
11204
12786
|
boolean(opts) {
|
|
11205
12787
|
return makeScalar("boolean", opts);
|
|
11206
12788
|
},
|
|
@@ -11305,6 +12887,22 @@ const RESERVED_AUTOCRUD_FIELDS = { shortId: reserved(field.number({
|
|
|
11305
12887
|
function isReservedAutoCrudField(def) {
|
|
11306
12888
|
return def[RESERVED] === true;
|
|
11307
12889
|
}
|
|
12890
|
+
/** The server metadata names a body field may not take: expressions, where and orderBy read them as metadata. */
|
|
12891
|
+
const RESERVED_METADATA_FIELD_NAMES = [
|
|
12892
|
+
"createdAt",
|
|
12893
|
+
"updatedAt",
|
|
12894
|
+
"seq",
|
|
12895
|
+
"createdBy",
|
|
12896
|
+
"updatedBy",
|
|
12897
|
+
"createdById",
|
|
12898
|
+
"createdByKind",
|
|
12899
|
+
"updatedById",
|
|
12900
|
+
"updatedByKind"
|
|
12901
|
+
];
|
|
12902
|
+
/** Why a field of this name cannot be declared, or undefined when it can. */
|
|
12903
|
+
function reservedMetadataFieldMessage(name) {
|
|
12904
|
+
return RESERVED_METADATA_FIELD_NAMES.includes(name) ? `\`${name}\` is a reserved field name: it is reserved for server metadata` : void 0;
|
|
12905
|
+
}
|
|
11308
12906
|
/** Reserved field names — useful for validation loops. */
|
|
11309
12907
|
const RESERVED_AUTOCRUD_FIELD_NAMES = Object.keys(RESERVED_AUTOCRUD_FIELDS);
|
|
11310
12908
|
|
|
@@ -11471,6 +13069,10 @@ const NAMESPACE_NAME_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
|
11471
13069
|
function defineNamespace(namespace, types, opts = {}) {
|
|
11472
13070
|
if (!NAMESPACE_NAME_PATTERN.test(namespace)) throw new Error(`invalid namespace name '${namespace}': expected ${NAMESPACE_NAME_PATTERN.source}. A namespace must not contain '/' (it would truncate every qualified frame key) or ':' (it would be ambiguous with the per-namespace grant form, e.g. --ns notes:read).`);
|
|
11473
13071
|
for (const [typeName, typeInput] of Object.entries(types)) {
|
|
13072
|
+
for (const name of Object.keys(typeInput.fields)) {
|
|
13073
|
+
const why = reservedMetadataFieldMessage(name);
|
|
13074
|
+
if (why !== void 0) throw new Error(`${why} (type: ${typeName})`);
|
|
13075
|
+
}
|
|
11474
13076
|
if (typeInput.autoCrud === false) continue;
|
|
11475
13077
|
for (const reserved of RESERVED_AUTOCRUD_FIELD_NAMES) if (Object.hasOwn(typeInput.fields, reserved)) throw new Error(`\`${reserved}\` is a reserved field name, auto-injected by the engine (type: ${typeName})`);
|
|
11476
13078
|
assertServerSetFieldsFillable(typeName, typeInput.fields);
|
|
@@ -11564,9 +13166,9 @@ function assertOrderedBy(typeName, def) {
|
|
|
11564
13166
|
}
|
|
11565
13167
|
const field = def.fields[name];
|
|
11566
13168
|
if (field === void 0) throw new Error(`${where}: unknown field '${name}'`);
|
|
11567
|
-
if (!(field.kind === "scalar" && (field.type === "date" || field.type === "number"))) {
|
|
13169
|
+
if (!(field.kind === "scalar" && (field.type === "date" || field.type === "number" || field.type === "int16" || field.type === "int32" || field.type === "int64"))) {
|
|
11568
13170
|
const kind = field.kind === "scalar" ? field.type : field.kind;
|
|
11569
|
-
throw new Error(`${where}: field '${name}' is ${kind}; only date and
|
|
13171
|
+
throw new Error(`${where}: field '${name}' is ${kind}; only date, number, int16, int32 and int64 fields, or ${[...ORDERED_BY_METADATA_COLUMNS].join(", ")}, can be a natural order`);
|
|
11570
13172
|
}
|
|
11571
13173
|
}
|
|
11572
13174
|
function composePolicyLayers(pluginLevel, namespaceLevel, typeLevel) {
|
|
@@ -14542,39 +16144,88 @@ var MemoryDataset = class MemoryDataset {
|
|
|
14542
16144
|
hasMore: false,
|
|
14543
16145
|
count: pool.length
|
|
14544
16146
|
};
|
|
14545
|
-
if (params.aggregate !== void 0)
|
|
16147
|
+
if (params.aggregate !== void 0) {
|
|
16148
|
+
const { groups, truncated } = groupRows(pool, {
|
|
16149
|
+
...params,
|
|
16150
|
+
aggregate: params.aggregate
|
|
16151
|
+
}, fieldTypeLookup(fieldDefOf), limit);
|
|
16152
|
+
return {
|
|
16153
|
+
rows: [],
|
|
16154
|
+
hasMore: false,
|
|
16155
|
+
groups,
|
|
16156
|
+
...truncated ? { truncated: true } : {}
|
|
16157
|
+
};
|
|
16158
|
+
}
|
|
14546
16159
|
const original = /* @__PURE__ */ new Map();
|
|
14547
|
-
const
|
|
14548
|
-
|
|
14549
|
-
|
|
14550
|
-
|
|
14551
|
-
|
|
14552
|
-
|
|
16160
|
+
const selected = /* @__PURE__ */ new Map();
|
|
16161
|
+
const dateNames = /* @__PURE__ */ new Set();
|
|
16162
|
+
const sortDefOf = (f) => dateNames.has(f) ? {
|
|
16163
|
+
kind: "scalar",
|
|
16164
|
+
type: "date"
|
|
16165
|
+
} : fieldDefOf(f);
|
|
16166
|
+
let restSelect;
|
|
16167
|
+
const select = params.select;
|
|
16168
|
+
if (select !== void 0) {
|
|
16169
|
+
const entries = Object.entries(select);
|
|
16170
|
+
const fieldType = fieldTypeLookup(fieldDefOf);
|
|
16171
|
+
const typeOf = (name) => {
|
|
16172
|
+
if (!name.startsWith("_agg.")) return fieldType(name);
|
|
16173
|
+
const spec = frameAggs?.[name.slice(5)];
|
|
16174
|
+
return spec === void 0 ? void 0 : walkAggExprType(spec, type, (t, f) => lookup?.(t, f));
|
|
14553
16175
|
};
|
|
14554
|
-
const
|
|
14555
|
-
|
|
14556
|
-
|
|
14557
|
-
|
|
14558
|
-
|
|
14559
|
-
|
|
14560
|
-
|
|
14561
|
-
|
|
14562
|
-
const
|
|
14563
|
-
|
|
14564
|
-
|
|
14565
|
-
|
|
14566
|
-
|
|
14567
|
-
|
|
16176
|
+
for (const [name, expr] of entries) {
|
|
16177
|
+
const checked = checkExpr(expr, { field: typeOf }, []);
|
|
16178
|
+
if (checked.ok && checked.type === "date") dateNames.add(name);
|
|
16179
|
+
}
|
|
16180
|
+
const text = JSON.stringify(select);
|
|
16181
|
+
const aggRefs = Object.keys(frameAggs ?? {}).filter((n) => text.includes(`"_agg.${n}"`));
|
|
16182
|
+
const orderKeys = new Set((params.orderBy ?? []).flatMap((c) => Object.keys(c)).filter((k) => Object.hasOwn(select, k)));
|
|
16183
|
+
const valuesOf = (row, names) => {
|
|
16184
|
+
const ctx = ctxFor(row);
|
|
16185
|
+
const output = {};
|
|
16186
|
+
for (const name of aggRefs) output[`_agg.${name}`] = ctx.aggValue?.(name);
|
|
16187
|
+
const values = {};
|
|
16188
|
+
for (const [name, expr] of names) values[name] = evalExpr(expr, row, {
|
|
16189
|
+
output,
|
|
16190
|
+
fieldType: typeOf
|
|
16191
|
+
}) ?? null;
|
|
16192
|
+
return values;
|
|
14568
16193
|
};
|
|
14569
|
-
|
|
14570
|
-
|
|
14571
|
-
|
|
16194
|
+
const printed = (values) => {
|
|
16195
|
+
for (const name of dateNames) if (name in values) values[name] = isoMicros(values[name]);
|
|
16196
|
+
return values;
|
|
16197
|
+
};
|
|
16198
|
+
const ordering = entries.filter(([n]) => orderKeys.has(n));
|
|
16199
|
+
const rest = entries.filter(([n]) => !orderKeys.has(n));
|
|
16200
|
+
try {
|
|
16201
|
+
if (ordering.length > 0) pool = pool.map((row) => {
|
|
16202
|
+
const values = valuesOf(row, ordering);
|
|
16203
|
+
const copy = {
|
|
16204
|
+
...row,
|
|
16205
|
+
...values
|
|
16206
|
+
};
|
|
16207
|
+
original.set(copy, row);
|
|
16208
|
+
selected.set(copy, printed({ ...values }));
|
|
16209
|
+
return copy;
|
|
16210
|
+
});
|
|
16211
|
+
} catch (err) {
|
|
16212
|
+
outOfRange(err);
|
|
16213
|
+
}
|
|
16214
|
+
restSelect = (row) => {
|
|
16215
|
+
try {
|
|
16216
|
+
return printed(valuesOf(original.get(row) ?? row, rest));
|
|
16217
|
+
} catch (err) {
|
|
16218
|
+
return outOfRange(err);
|
|
16219
|
+
}
|
|
16220
|
+
};
|
|
16221
|
+
if (rest.length === 0) restSelect = void 0;
|
|
16222
|
+
}
|
|
14572
16223
|
const orderBy = normalizeOrderBy(params.orderBy);
|
|
14573
|
-
pool.sort((a, b2) => compareByClauses(a, b2, orderBy,
|
|
16224
|
+
pool.sort((a, b2) => compareByClauses(a, b2, orderBy, sortDefOf));
|
|
14574
16225
|
let startIdx = 0;
|
|
14575
16226
|
if (params.cursor !== void 0 && params.cursor !== "") {
|
|
14576
16227
|
const cursor = decodeCursor(params.cursor, orderBy);
|
|
14577
|
-
startIdx = findCursorStart(pool, orderBy, cursor.orderByValues, cursor.id,
|
|
16228
|
+
startIdx = findCursorStart(pool, orderBy, cursor.orderByValues, cursor.id, sortDefOf);
|
|
14578
16229
|
}
|
|
14579
16230
|
let hasMore = false;
|
|
14580
16231
|
let slice;
|
|
@@ -14593,11 +16244,15 @@ var MemoryDataset = class MemoryDataset {
|
|
|
14593
16244
|
const columns = this.columnFieldNames(type);
|
|
14594
16245
|
const page = (rows) => {
|
|
14595
16246
|
const base = original.size === 0 ? rows : rows.map((r) => original.get(r) ?? r);
|
|
14596
|
-
|
|
14597
|
-
return base.map((r) => withoutNullColumns(columns, {
|
|
16247
|
+
const out = params.agg === void 0 && params.nested === void 0 ? base.map((r) => withoutNullColumns(columns, r)) : base.map((r) => withoutNullColumns(columns, {
|
|
14598
16248
|
...r,
|
|
14599
16249
|
...walkProjection(r, params.agg, params.nested ?? {}, ctxFor(r), true)
|
|
14600
16250
|
}));
|
|
16251
|
+
return selected.size === 0 && restSelect === void 0 ? out : out.map((r, i) => ({
|
|
16252
|
+
...r,
|
|
16253
|
+
...selected.get(rows[i]),
|
|
16254
|
+
...restSelect?.(rows[i])
|
|
16255
|
+
}));
|
|
14601
16256
|
};
|
|
14602
16257
|
const last = slice[slice.length - 1];
|
|
14603
16258
|
if (hasMore && last !== void 0) out.nextCursor = encodeCursor({
|
|
@@ -15015,40 +16670,6 @@ function conditionalMatchEq(actual, expected) {
|
|
|
15015
16670
|
return actual === expected;
|
|
15016
16671
|
}
|
|
15017
16672
|
/**
|
|
15018
|
-
* A whole-result aggregate: one group, or one per UTC day/hour bucket in
|
|
15019
|
-
* ascending order. A row whose bucket field is null or not a date is in no
|
|
15020
|
-
* bucket. Groups beyond `limit` are cut and say `truncated`.
|
|
15021
|
-
*/
|
|
15022
|
-
function aggregateGroups(pool, aggregate, bucket, limit, fieldDefOf) {
|
|
15023
|
-
const values = (rows) => Object.fromEntries(Object.entries(aggregate).map(([name, fn]) => [name, applyAgg(rows, fn, fieldDefOf)]));
|
|
15024
|
-
if (bucket === void 0) return {
|
|
15025
|
-
rows: [],
|
|
15026
|
-
hasMore: false,
|
|
15027
|
-
groups: [values(pool)]
|
|
15028
|
-
};
|
|
15029
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
15030
|
-
for (const row of pool) {
|
|
15031
|
-
const micros = storedMicros(getFieldValue(row, bucket.field));
|
|
15032
|
-
if (micros === void 0) continue;
|
|
15033
|
-
const ms = Number((micros - (micros % 1000n + 1000n) % 1000n) / 1000n);
|
|
15034
|
-
const key = new Date(ms).toISOString().slice(0, bucket.by === "day" ? 10 : 13);
|
|
15035
|
-
const list = byKey.get(key);
|
|
15036
|
-
if (list === void 0) byKey.set(key, [row]);
|
|
15037
|
-
else list.push(row);
|
|
15038
|
-
}
|
|
15039
|
-
const keys = [...byKey.keys()].sort();
|
|
15040
|
-
const truncated = limit !== void 0 && keys.length > limit;
|
|
15041
|
-
return {
|
|
15042
|
-
rows: [],
|
|
15043
|
-
hasMore: false,
|
|
15044
|
-
groups: (truncated ? keys.slice(0, limit) : keys).map((key) => ({
|
|
15045
|
-
bucket: key,
|
|
15046
|
-
...values(byKey.get(key) ?? [])
|
|
15047
|
-
})),
|
|
15048
|
-
...truncated ? { truncated: true } : {}
|
|
15049
|
-
};
|
|
15050
|
-
}
|
|
15051
|
-
/**
|
|
15052
16673
|
* A row's walk selections: `_agg` values and nested lists, each list
|
|
15053
16674
|
* filtered, ordered (id ascending last) and cut to its limit, with
|
|
15054
16675
|
* `_truncated` naming the lists that were cut. Nested rows carry `id`, their
|
|
@@ -15086,6 +16707,14 @@ function walkProjection(row, aggs, nested, ctx, top = false) {
|
|
|
15086
16707
|
if (truncated.length > 0) out.push(["_truncated", Object.fromEntries(truncated)]);
|
|
15087
16708
|
return Object.fromEntries(out);
|
|
15088
16709
|
}
|
|
16710
|
+
/** A date value as UTC text to the microsecond, the form pg's row projection prints; null when it is not a date. */
|
|
16711
|
+
function isoMicros(v) {
|
|
16712
|
+
const micros = storedMicros(v);
|
|
16713
|
+
if (micros === void 0) return null;
|
|
16714
|
+
const sub = (micros % 1000n + 1000n) % 1000n;
|
|
16715
|
+
const ms = Number((micros - sub) / 1000n);
|
|
16716
|
+
return new Date(ms).toISOString().replace("Z", `${sub.toString().padStart(3, "0")}Z`);
|
|
16717
|
+
}
|
|
15089
16718
|
function compareByClauses(a, b, orderBy, fieldDefOf) {
|
|
15090
16719
|
for (const [field, dir] of orderBy) {
|
|
15091
16720
|
const av = asSortValue(field, getFieldValue(a, field), fieldDefOf);
|
|
@@ -15971,7 +17600,8 @@ function healthRoute(deps) {
|
|
|
15971
17600
|
warnings: await deps.warnings(),
|
|
15972
17601
|
version: deps.version,
|
|
15973
17602
|
walks: true,
|
|
15974
|
-
uptimeSeconds: Math.floor((Date.now() - deps.startedAt) / 1e3)
|
|
17603
|
+
uptimeSeconds: Math.floor((Date.now() - deps.startedAt) / 1e3),
|
|
17604
|
+
...deps.pool !== void 0 ? { pool: deps.pool() } : {}
|
|
15975
17605
|
});
|
|
15976
17606
|
}
|
|
15977
17607
|
|
|
@@ -16407,6 +18037,13 @@ function operatorContract(engine, ns) {
|
|
|
16407
18037
|
metadataAuthorFields: METADATA_AUTHOR_FIELDS
|
|
16408
18038
|
};
|
|
16409
18039
|
}
|
|
18040
|
+
/** The expression catalogue: what `select`, `where.$expr`, `groupBy`, `having` and aggregates accept. */
|
|
18041
|
+
const EXPRESSIONS = {
|
|
18042
|
+
functions: EXPR_FUNCTIONS,
|
|
18043
|
+
aggregates: EXPR_AGGREGATES,
|
|
18044
|
+
limits: EXPR_LIMITS,
|
|
18045
|
+
dateUnits: DATE_UNITS
|
|
18046
|
+
};
|
|
16410
18047
|
/**
|
|
16411
18048
|
* GET /substrate/v1/schema — the `defaultNamespace`'s schema, a pre-federation
|
|
16412
18049
|
* compatibility surface.
|
|
@@ -16430,7 +18067,8 @@ function schemaRoute(engine, defaultNamespace) {
|
|
|
16430
18067
|
try {
|
|
16431
18068
|
return jsonResponse(200, {
|
|
16432
18069
|
...engine.schemaFor(defaultNamespace),
|
|
16433
|
-
...operatorContract(engine, defaultNamespace)
|
|
18070
|
+
...operatorContract(engine, defaultNamespace),
|
|
18071
|
+
expressions: EXPRESSIONS
|
|
16434
18072
|
});
|
|
16435
18073
|
} catch {
|
|
16436
18074
|
return jsonResponse(404, {
|
|
@@ -16542,6 +18180,14 @@ function mutationTargetsType(name, def, type, fields) {
|
|
|
16542
18180
|
|
|
16543
18181
|
//#endregion
|
|
16544
18182
|
//#region ../../packages/substrate-compose/src/routes/sse-frame.ts
|
|
18183
|
+
/**
|
|
18184
|
+
* SSE wire framing. Spec §5. Pure — no I/O, so every wire-format decision is
|
|
18185
|
+
* unit-testable without a server.
|
|
18186
|
+
*
|
|
18187
|
+
* Encoding is `json` ONLY. SSE `data:` is a text field; base64-ing msgpack
|
|
18188
|
+
* would cost ~33% to ship the format that is supposed to be the compact one.
|
|
18189
|
+
* The §7.4 pack codec stays reserved for a future binary transport.
|
|
18190
|
+
*/
|
|
16545
18191
|
const SSE_HEADERS = {
|
|
16546
18192
|
"content-type": "text/event-stream",
|
|
16547
18193
|
"cache-control": "no-store",
|
|
@@ -16561,7 +18207,7 @@ function sseRetry(ms) {
|
|
|
16561
18207
|
* §4). `sequence` travels inside `data` for client-side ordering instead.
|
|
16562
18208
|
*/
|
|
16563
18209
|
function sseEvent(frame, fieldsByKey) {
|
|
16564
|
-
return `event: ${frame.kind}\ndata: ${
|
|
18210
|
+
return `event: ${frame.kind}\ndata: ${stringifyJson(payloadFor(frame, fieldsByKey))}\n\n`;
|
|
16565
18211
|
}
|
|
16566
18212
|
function payloadFor(frame, fieldsByKey) {
|
|
16567
18213
|
if (frame.kind === "end") return { reason: frame.reason ?? (frame._meta?.cancelled === true ? "cancelled" : "complete") };
|
|
@@ -17166,7 +18812,7 @@ function parseRequestFrame(buf, mode) {
|
|
|
17166
18812
|
}
|
|
17167
18813
|
}
|
|
17168
18814
|
function serializeFrame(frame, mode, fieldsByKey, engine) {
|
|
17169
|
-
if (mode === "json") return
|
|
18815
|
+
if (mode === "json") return stringifyJson(projectWsFrame(frame, fieldsByKey));
|
|
17170
18816
|
const encoded = encodeDataForPack(frame, fieldsByKey, engine);
|
|
17171
18817
|
const wire = encodeWsFrame(encoded, mode);
|
|
17172
18818
|
return typeof wire === "string" ? wire : Buffer.from(wire);
|
|
@@ -17482,11 +19128,12 @@ function registerWsRoute(server, engine, trustedProxies, metric, auth, systemIde
|
|
|
17482
19128
|
//#endregion
|
|
17483
19129
|
//#region ../../packages/substrate-compose/src/compose/routes.ts
|
|
17484
19130
|
async function registerCoreRoutes(args) {
|
|
17485
|
-
const { server, engine, gate, trustedProxies, auth, streamTicketSecret, version, stats, routeEventMetric, substrateDataset, substrateKeyRegistry, datasets, edges, namespaces, coordinatorPlugins, systemIdentity, routeMetric, bridges, registerRateLimiter, identityDirectory, adminCapability, configReport, storageReport, promoteFields, fieldWarnings } = args;
|
|
19131
|
+
const { server, engine, gate, trustedProxies, auth, streamTicketSecret, version, stats, routeEventMetric, substrateDataset, substrateKeyRegistry, datasets, edges, namespaces, coordinatorPlugins, systemIdentity, routeMetric, bridges, registerRateLimiter, identityDirectory, adminCapability, configReport, storageReport, promoteFields, fieldWarnings, poolStats } = args;
|
|
17486
19132
|
server.route("GET", "/health", healthRoute({
|
|
17487
19133
|
version: version ?? "0.0.0",
|
|
17488
19134
|
startedAt: Date.now(),
|
|
17489
|
-
warnings: async () => [...engine.postWriteWarnings(), ...await fieldWarnings?.().catch(() => []) ?? []]
|
|
19135
|
+
warnings: async () => [...engine.postWriteWarnings(), ...await fieldWarnings?.().catch(() => []) ?? []],
|
|
19136
|
+
...poolStats !== void 0 ? { pool: poolStats } : {}
|
|
17490
19137
|
}));
|
|
17491
19138
|
server.route("GET", "/substrate/v1/schema", gate(schemaRoute(engine, "default"), "read"));
|
|
17492
19139
|
server.route("GET", "/substrate/v1/schema/:namespace", gate(schemaForNamespaceRoute(engine), "read"));
|
|
@@ -17947,7 +19594,8 @@ async function composeInner(opts, cleanup) {
|
|
|
17947
19594
|
...opts.configReport !== void 0 ? { configReport: opts.configReport } : {},
|
|
17948
19595
|
...opts.storageReport !== void 0 ? { storageReport: opts.storageReport } : {},
|
|
17949
19596
|
promoteFields: promoteRunner.run,
|
|
17950
|
-
...opts.dataset.fieldWarnings !== void 0 ? { fieldWarnings: opts.dataset.fieldWarnings.bind(opts.dataset) } : {}
|
|
19597
|
+
...opts.dataset.fieldWarnings !== void 0 ? { fieldWarnings: opts.dataset.fieldWarnings.bind(opts.dataset) } : {},
|
|
19598
|
+
...opts.dataset.poolStats !== void 0 ? { poolStats: opts.dataset.poolStats.bind(opts.dataset) } : {}
|
|
17951
19599
|
});
|
|
17952
19600
|
let scheduler = null;
|
|
17953
19601
|
if (scheduleEntries.length > 0) {
|
|
@@ -18234,4 +19882,4 @@ function kernelMigrationTracking(sql) {
|
|
|
18234
19882
|
}
|
|
18235
19883
|
|
|
18236
19884
|
//#endregion
|
|
18237
|
-
export {
|
|
19885
|
+
export { publishSchema as $, METADATA_FILTER_FIELDS as $t, isReservedAutoCrudField as A, isAuthCause as An, encodeCursor as At, deleteResolver as B, parseJsonBody as Bt, makeCtx as C, WALKS_IN_MUTATION as Cn, walkAggExprType as Ct, RESERVED_AUTOCRUD_FIELDS as D, WALK_BUDGET_EXCEEDED as Dn, endsWithEscape as Dt, selectAll as E, WALKS_UNSUPPORTED_STORE as En, LIKE_TRAILING_ESCAPE as Et, createResolverWithEdges as F, ROW_METADATA_FIELDS as Fn, HttpBridgeClient as Ft, listResolver as G, WS_REASON_SUBSCRIPTION_OVERLOADED as Gt, applyUpdatePatch as H, signHs256 as Ht, installAutoCrud as I, assertNoRepeatedIds as In, JSON_HEADERS as It, resolveIdentity as J, normaliseVocabWhere as Jt, getResolver as K, canonicalVocabValue as Kt, deleteManyResolver as L, canonicalize as Ln, buildContext as Lt, field as M, isResolverFrameError as Mn, fullBuiltinOperators as Mt, AutoCrudNameCollisionError as N, isRestrictViolation as Nn, applyExtensionResultMeta as Nt, RESERVED_AUTOCRUD_FIELD_NAMES as O, WALK_UNSUPPORTED_REF as On, CursorDecodeError as Ot, autoCrudOptionsOf as P, CLIENT_SUPPLIED_SERVER_FIELD as Pn, fieldRequested as Pt, loadPlugins as Q, METADATA_AUTHOR_FIELDS as Qt, updateManyResolver as R, BootstrapConfigError as Rn, error400 as Rt, EngineQueryError as S, VALUE_OUT_OF_RANGE as Sn, metadataExprType as St, queryOne as T, WALKS_NEED_PARTITIONED as Tn, isJsonPathPredicate as Tt, deleteRow as U, WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME as Ut, updateResolver as V, parseAuthConfig as Vt, purgeResolver as W, WS_REASON_SERVER_RESTART as Wt, resolveEntityRow as X, vocabImplication as Xt, resolveEntityId as Y, storedVocabSpellings as Yt, MemoryEdgesStore as Z, RegistryBase as Zt, AUDIT_EVENT_TYPE as _, ReservedField as _n, getExtensionOperator as _t, composeFromManifests as a, isIsoTimestamp as an, forEachWalkHop as at, assertOrderedBy as b, STORE_BUSY as bn, unregisterExtensionOperator as bt, MemoryDataset as c, validateHardDeleteEnv as cn, preResolveExtensionOperatorsDetailed as ct, uuidFromName as d, FILTER_TOO_BROAD as dn, canRaise as dt, normaliseMetadataOrderBy as en, effectiveFilterOperators as et, emitFieldEvents as f, MissingReferentError as fn, isDateField as ft, buildAuditRow as g, ROW_POLICY_BLOCKS_WALK as gn, refNames as gt, SYSTEM_TYPE_DEFS as h, REGEX_BUDGET_EXCEEDED as hn, isRefOperand as ht, PluginLoadError as i, isImpossibleIsoTimestamp as in, saveSweepState as it, reservedMetadataFieldMessage as j, isBatchElementError as jn, normalizeOrderBy as jt, RESERVED_METADATA_FIELD_NAMES as k, filterTooBroad as kn, decodeCursor as kt, planServesShape as l, BatchElementError as ln, aggParts as lt, runInviteSweep as m, QUERY_TIMEOUT as mn, groupConjuncts as mt, kernelMigrationTracking as n, normaliseWalkNode as nn, loadSweepState as nt, DEFAULT_NAMESPACE as o, normalizeIsoTimestamp as on, sharedPicks as ot, migrateFieldValues as p, NamespaceMismatch as pn, bindingWalks as pt, UnattributedWriteError as q, normaliseVocabValues as qt, mountKernelRoute as r, createKeyedMutex as rn, runSweep as rt, compose as s, assertHardDeleteAllowed as sn, windowGroupBy as st, composeKernelPlugins as t, normaliseMetadataWhere as tn, applyObligations as tt, isStoreProfileName as u, BridgeUnreachableError as un, aggType as ut, INVITE_TYPE as v, ResolverFrameError as vn, listExtensionOperators as vt, queryAll as w, WALKS_IN_SUBSCRIPTION as wn, assertJsonPathPredicate as wt, defineNamespace as x, UnsupportedFilterError as xn, exprTypeOfField as xt, assertIndexDecl as y, RestrictViolation as yn, registerExtensionOperator as yt, createResolver as z, defineBootstrap as zn, jsonResponse as zt };
|