@mnmal-ai/substrate 1.0.0-alpha.16 → 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-HxCgbvNl.js → boot-store-DNU0UfX7.js} +19 -66
- package/dist/{bridge-CvZGzNIO.d.ts → bridge-5VHZiH9o.d.ts} +1 -1
- package/dist/{cli-6mjAWiEZ.js → cli-CwWUYUMv.js} +66 -12
- 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-De5F1tUf.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-2WfRPUb0.js → repl-BNnKFG3y.js} +1 -1
- package/dist/{row-Dh57ShWL.d.ts → row-DHxnCnhA.d.ts} +10 -8
- package/dist/{run-ChcHBIN3.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--uyAqssS.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-DHLw3eCn.js → src-DWrFdHrY.js} +1016 -231
- 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 +21 -21
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { $t as METADATA_FILTER_FIELDS, At as encodeCursor, Ct as walkAggExprType, Dt as endsWithEscape, Et as LIKE_TRAILING_ESCAPE, In as assertNoRepeatedIds, Mt as fullBuiltinOperators, Nt as applyExtensionResultMeta, On as WALK_UNSUPPORTED_REF, Ot as CursorDecodeError, Pt as fieldRequested, Sn as VALUE_OUT_OF_RANGE, St as metadataExprType, Tt as isJsonPathPredicate, _n as ReservedField, at as forEachWalkHop, bn as STORE_BUSY, ct as preResolveExtensionOperatorsDetailed, dt as canRaise, ft as isDateField, gt as refNames, ht as isRefOperand, jt as normalizeOrderBy, kn as filterTooBroad, kt as decodeCursor, lt as aggParts, mn as QUERY_TIMEOUT, mt as groupConjuncts, on as normalizeIsoTimestamp, ot as sharedPicks, pn as NamespaceMismatch, pt as bindingWalks, rn as createKeyedMutex, st as windowGroupBy, ut as aggType, vn as ResolverFrameError, wt as assertJsonPathPredicate, xn as UnsupportedFilterError, xt as exprTypeOfField } from "./src-BHh6UZ7L.js";
|
|
3
|
+
import { A as parseInt64, M as stringifyJsonExact, N as DATE_UNITS, O as isInt16, T as naturalOrderOf, j as stringifyJson, k as isInt32, w as isColumnField } from "./src-CdbUzLhB.js";
|
|
4
4
|
import { createHash, randomUUID } from "node:crypto";
|
|
5
5
|
|
|
6
6
|
//#region ../../packages/substrate-dataset-pg/src/advisory-lock.ts
|
|
@@ -163,7 +163,8 @@ function encodeRow(table, row) {
|
|
|
163
163
|
const written = value instanceof Date ? value.toJSON() : value;
|
|
164
164
|
const v = toColumn(held, written);
|
|
165
165
|
out.columns[held.column] = v;
|
|
166
|
-
|
|
166
|
+
const back = v === null ? void 0 : readBack(held.kind, v);
|
|
167
|
+
if (v === null || (held.kind === "int64" ? back !== parseInt64(written) : back !== written)) out.raw[field] = written;
|
|
167
168
|
}
|
|
168
169
|
return out;
|
|
169
170
|
}
|
|
@@ -202,6 +203,12 @@ function toColumn(column, value) {
|
|
|
202
203
|
if (typeof value === "number") return String(value);
|
|
203
204
|
return typeof value === "string" ? value : null;
|
|
204
205
|
case "number": return typeof value === "number" ? value : null;
|
|
206
|
+
case "int16": return isInt16(value) ? value : null;
|
|
207
|
+
case "int32": return isInt32(value) ? value : null;
|
|
208
|
+
case "int64": {
|
|
209
|
+
const n = parseInt64(value);
|
|
210
|
+
return n === void 0 ? null : String(n);
|
|
211
|
+
}
|
|
205
212
|
case "boolean": return typeof value === "boolean" ? value : null;
|
|
206
213
|
case "date": {
|
|
207
214
|
if (typeof value !== "string") return null;
|
|
@@ -212,18 +219,34 @@ function toColumn(column, value) {
|
|
|
212
219
|
}
|
|
213
220
|
/** What reading column value `v` back gives, or `undefined` when that read is lossy. */
|
|
214
221
|
function readBack(kind, v) {
|
|
222
|
+
if (kind === "int64") return BigInt(v);
|
|
215
223
|
if (kind !== "date") return v;
|
|
216
224
|
const d = new Date(v);
|
|
217
225
|
return d.getUTCFullYear() <= 100 ? void 0 : d.toISOString();
|
|
218
226
|
}
|
|
227
|
+
/** `utcTextSql(col, 'US')` output for a year 1 to 9999 instant. */
|
|
228
|
+
const UTC_US_TEXT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$/;
|
|
219
229
|
/** A column value as pg returns it, in the JSON form a row carries: dates as `toISOString()`. */
|
|
220
230
|
function canonical(kind, value) {
|
|
231
|
+
if (kind === "int64") return BigInt(value);
|
|
221
232
|
if (kind !== "date") return value;
|
|
233
|
+
if (typeof value === "string" && UTC_US_TEXT.test(value)) return `${value.slice(0, 23)}Z`;
|
|
222
234
|
return (value instanceof Date ? value : new Date(String(value))).toISOString();
|
|
223
235
|
}
|
|
224
236
|
|
|
225
237
|
//#endregion
|
|
226
238
|
//#region ../../packages/substrate-dataset-pg/src/pg-sql/primitives.ts
|
|
239
|
+
/**
|
|
240
|
+
* Shared foundations for the pg SQL builders: the Fragment alias, low-level
|
|
241
|
+
* primitives, promoted-column machinery and scalar helpers. Imports nothing
|
|
242
|
+
* from the other builders and holds no connection state.
|
|
243
|
+
*
|
|
244
|
+
* SQL-injection contract: every caller-supplied
|
|
245
|
+
* value flows through postgres.js's parameterised tagged-template slots.
|
|
246
|
+
* `sql.unsafe(...)` is used ONLY for identifiers/cast suffixes drawn from
|
|
247
|
+
* closed constant maps in this file, and for field names inlined as escaped
|
|
248
|
+
* literals by `sqlStringLiteral`.
|
|
249
|
+
*/
|
|
227
250
|
const idToText = (id) => typeof id === "number" ? String(id) : id;
|
|
228
251
|
/**
|
|
229
252
|
* Row-metadata field → native column. Predicates on these fields use the
|
|
@@ -307,6 +330,11 @@ const jsonSql = (path, alias, jsonb = "extra") => `${qualifier(alias)}${jsonb}${
|
|
|
307
330
|
const rawTextSql = (path, alias, jsonb) => `(${jsonSql(path.slice(0, -1), alias, jsonb)} ->> ${sqlStringLiteral(path[path.length - 1])})`;
|
|
308
331
|
const textSql = (path, alias, jsonb) => `(${rawTextSql(path, alias, jsonb)} COLLATE "C")`;
|
|
309
332
|
const numSql = (path, alias, jsonb) => `substrate_num(${jsonSql(path, alias, jsonb)})`;
|
|
333
|
+
/** The stored number as a bigint, or NULL for anything that is not an int64 (never raises). */
|
|
334
|
+
const int64Sql = (path, alias, jsonb) => {
|
|
335
|
+
const n = numSql(path, alias, jsonb);
|
|
336
|
+
return `(CASE WHEN ${n} = trunc(${n}) AND ${n} BETWEEN -9223372036854775808 AND 9223372036854775807 THEN ${n}::bigint END)`;
|
|
337
|
+
};
|
|
310
338
|
const tsSql = (path, alias, jsonb) => `substrate_ts(${jsonSql(path, alias, jsonb)})`;
|
|
311
339
|
/** The jsonb-path expression for `as`: the one every body/extra builder below emits. */
|
|
312
340
|
function pathSql(path, as, alias, jsonb) {
|
|
@@ -314,6 +342,7 @@ function pathSql(path, as, alias, jsonb) {
|
|
|
314
342
|
case "text": return textSql(path, alias, jsonb);
|
|
315
343
|
case "rawText": return rawTextSql(path, alias, jsonb);
|
|
316
344
|
case "number": return numSql(path, alias, jsonb);
|
|
345
|
+
case "int64": return int64Sql(path, alias, jsonb);
|
|
317
346
|
case "date": return tsSql(path, alias, jsonb);
|
|
318
347
|
case "boolean": return `${rawTextSql(path, alias, jsonb)}::boolean`;
|
|
319
348
|
case "json": return `(${jsonSql(path, alias, jsonb)})`;
|
|
@@ -321,7 +350,8 @@ function pathSql(path, as, alias, jsonb) {
|
|
|
321
350
|
}
|
|
322
351
|
/** The `as` a column of `kind` compares and sorts as. */
|
|
323
352
|
function columnAs(kind) {
|
|
324
|
-
|
|
353
|
+
if (kind === "int16" || kind === "int32") return "int64";
|
|
354
|
+
return kind === "number" || kind === "int64" || kind === "date" || kind === "boolean" ? kind : "text";
|
|
325
355
|
}
|
|
326
356
|
/**
|
|
327
357
|
* A date column as text in the `toISOString()` grammar with microseconds, read
|
|
@@ -418,6 +448,10 @@ function bodyJson(sql, path, alias, jsonb) {
|
|
|
418
448
|
function bodyNum(sql, path, alias, jsonb) {
|
|
419
449
|
return sql.unsafe(pathSql(path, "number", alias, jsonb));
|
|
420
450
|
}
|
|
451
|
+
/** The stored number as a bigint, NULL unless it is an int64. */
|
|
452
|
+
function bodyInt64(sql, path, alias, jsonb) {
|
|
453
|
+
return sql.unsafe(pathSql(path, "int64", alias, jsonb));
|
|
454
|
+
}
|
|
421
455
|
/** `substrate_ts(body -> …)`: the instant, or NULL for anything but an ISO date or date-time string. */
|
|
422
456
|
function bodyTs(sql, path, alias, jsonb) {
|
|
423
457
|
return sql.unsafe(pathSql(path, "date", alias, jsonb));
|
|
@@ -427,6 +461,15 @@ function indexExpressionSql(kind, field, alias, jsonb) {
|
|
|
427
461
|
return pathSql([field], kind, alias, jsonb);
|
|
428
462
|
}
|
|
429
463
|
/**
|
|
464
|
+
* `like` / `notLike` on a text expression: case-sensitive, `\` escapes, and never
|
|
465
|
+
* NULL (a NULL value matches neither), as the memory evaluator's.
|
|
466
|
+
*/
|
|
467
|
+
function likeFragment(sql, expr, field, op, operand) {
|
|
468
|
+
if (typeof operand !== "string") throw new UnsupportedFilterError(field, op, "requires a string value");
|
|
469
|
+
if (endsWithEscape(operand)) throw new UnsupportedFilterError(field, op, LIKE_TRAILING_ESCAPE);
|
|
470
|
+
return op === "like" ? sql`COALESCE((${expr} LIKE ${operand} ESCAPE '\\'), FALSE)` : sql`COALESCE((${expr} NOT LIKE ${operand} ESCAPE '\\'), FALSE)`;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
430
473
|
* `cond` where `expr` is not NULL, else FALSE, never NULL, so a caller's NOT
|
|
431
474
|
* stays sound. The bare `cond` conjunct keeps it index-servable, where a
|
|
432
475
|
* COALESCE would hide it from the planner.
|
|
@@ -437,6 +480,7 @@ function notNullAnd(sql, expr, cond) {
|
|
|
437
480
|
const asJsonValue = (v) => v;
|
|
438
481
|
function scalarParam(value) {
|
|
439
482
|
if (value instanceof Date) return value.toISOString();
|
|
483
|
+
if (typeof value === "bigint") return value.toString();
|
|
440
484
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
|
|
441
485
|
return String(value);
|
|
442
486
|
}
|
|
@@ -482,13 +526,17 @@ function interleaveOperator(sql, fragments, op) {
|
|
|
482
526
|
//#endregion
|
|
483
527
|
//#region ../../packages/substrate-dataset-pg/src/pg-sql/projection.ts
|
|
484
528
|
/**
|
|
485
|
-
*
|
|
486
|
-
*
|
|
529
|
+
* Read-path shaping: which body keys to materialise, and how the native
|
|
530
|
+
* metadata columns fold back into the `RowMetadata` shape callers see.
|
|
531
|
+
*/
|
|
532
|
+
/**
|
|
533
|
+
* The SELECT items carrying a read's row, then `walk` (its walk selections'
|
|
534
|
+
* columns, from `buildWalkColumns`). The caller selects the metadata columns.
|
|
487
535
|
*
|
|
488
536
|
* For `decodeRow`: each column (of a requested field, when
|
|
489
537
|
* `fields` is given), `orphaned` ones included (F35), a date column as UTC
|
|
490
538
|
* text; then `extra AS extra`, whole, or its requested keys with `id` and
|
|
491
|
-
* `$raw` kept.
|
|
539
|
+
* `$raw` kept.
|
|
492
540
|
*
|
|
493
541
|
* Keys are bound as parameters, never concatenated.
|
|
494
542
|
*/
|
|
@@ -496,15 +544,20 @@ function buildRowProjection(sql, table, fields, alias = "r", walk = null) {
|
|
|
496
544
|
const q = qualifier(alias);
|
|
497
545
|
const keys = /* @__PURE__ */ new Set(["id"]);
|
|
498
546
|
for (const key of Object.keys(fields ?? {})) if (!NON_BODY_FIELD_KEYS.has(key)) keys.add(key);
|
|
499
|
-
const merged = (doc) => walk === null ? doc : sql`(${doc} || ${walk})`;
|
|
500
547
|
const columns = table.columns().filter((c) => fields === void 0 || Object.hasOwn(fields, c.field)).map((c) => {
|
|
501
548
|
const col = columnSql(c, alias);
|
|
502
549
|
return c.kind === "date" ? `${utcTextSql(col, "US")} AS ${c.column}` : col;
|
|
503
550
|
});
|
|
504
|
-
const
|
|
551
|
+
const ex = sql.unsafe(`${q}extra`);
|
|
552
|
+
const wanted = [...keys, RAW_KEY];
|
|
553
|
+
const subset = sql`COALESCE(CASE WHEN pg_column_size(${ex}) < ${sql.unsafe(String(INLINE_EXTRA_BYTES))} THEN (SELECT jsonb_object_agg(k, ${ex} -> k) FROM unnest(${wanted}::text[]) k WHERE ${ex} ? k) ELSE (SELECT jsonb_object_agg(e.key, e.value) FROM jsonb_each(${ex}) e WHERE e.key = ANY(${wanted}::text[])) END, '{}'::jsonb)`;
|
|
554
|
+
const allColumns = [...keys].every((k) => k === "id" || table.column(k)?.state === "column");
|
|
555
|
+
const extra = fields === void 0 ? ex : allColumns ? sql`CASE WHEN ${ex} ?| ${["id", RAW_KEY]}::text[] THEN ${subset} ELSE '{}'::jsonb END` : subset;
|
|
505
556
|
const head = columns.length === 0 ? "" : `${columns.join(", ")}, `;
|
|
506
|
-
return sql`${sql.unsafe(head)}${
|
|
557
|
+
return sql`${sql.unsafe(head)}${extra} AS extra${walk === null ? sql`` : sql`, ${walk}`}`;
|
|
507
558
|
}
|
|
559
|
+
/** Below pg's TOAST threshold (~2 kB), a jsonb value is stored inline and uncompressed. */
|
|
560
|
+
const INLINE_EXTRA_BYTES = 2e3;
|
|
508
561
|
/**
|
|
509
562
|
* A jsonb object from `'k'::text, <value>` pairs (the output key needs
|
|
510
563
|
* `::text`: jsonb_build_object's VARIADIC any cannot infer a parameter's
|
|
@@ -526,6 +579,21 @@ function join(parts, sep) {
|
|
|
526
579
|
}
|
|
527
580
|
return out;
|
|
528
581
|
}
|
|
582
|
+
/** Projection keys that read row metadata: `_metadata`, the author names, and the metadata columns. */
|
|
583
|
+
const METADATA_KEYS = /* @__PURE__ */ new Set([
|
|
584
|
+
"_metadata",
|
|
585
|
+
"createdBy",
|
|
586
|
+
"updatedBy",
|
|
587
|
+
...Object.keys(METADATA_FILTER_FIELDS).filter((k) => k !== "id")
|
|
588
|
+
]);
|
|
589
|
+
/** True unless `fields` lists its keys and none of them reads row metadata. */
|
|
590
|
+
function readsRowMetadata(fields) {
|
|
591
|
+
return fields === void 0 || Object.keys(fields).some((k) => METADATA_KEYS.has(k));
|
|
592
|
+
}
|
|
593
|
+
/** True unless `fields` lists its keys without `shortId`. */
|
|
594
|
+
function readsShortId(fields) {
|
|
595
|
+
return fields === void 0 || Object.hasOwn(fields, "shortId");
|
|
596
|
+
}
|
|
529
597
|
function assembleRowMetadata(record) {
|
|
530
598
|
const seq = record.seq == null ? void 0 : Number(record.seq);
|
|
531
599
|
if (seq === void 0 && record.created_at === null && record.updated_at === null && record.created_by_kind === null && record.created_by_id === null && record.updated_by_kind === null && record.updated_by_id === null) return;
|
|
@@ -548,12 +616,29 @@ function assembleRowMetadata(record) {
|
|
|
548
616
|
|
|
549
617
|
//#endregion
|
|
550
618
|
//#region ../../packages/substrate-dataset-pg/src/write-rows.ts
|
|
619
|
+
/**
|
|
620
|
+
* The `tables` layout's row writes: one statement for any number of rows.
|
|
621
|
+
* `PgDataset.write` upserts through {@link writeRowsStatement}, and its patch
|
|
622
|
+
* paths merge through {@link inputCte} and {@link mergeAssignments}, so every
|
|
623
|
+
* write applies `applyPatch`'s rule (row-codec.ts) in SQL:
|
|
624
|
+
*
|
|
625
|
+
* - a column the row writes takes its value (NULL for a cleared field), and
|
|
626
|
+
* one it does not write keeps the stored value;
|
|
627
|
+
* - `extra` loses every key the row writes (`unset`) and the old `$raw`, then
|
|
628
|
+
* takes the row's `extra`; `$raw` loses the written keys, takes the row's
|
|
629
|
+
* `$raw`, and is dropped when it ends empty.
|
|
630
|
+
*
|
|
631
|
+
* So no declared column key is ever left in `extra` by a write (F13).
|
|
632
|
+
*/
|
|
551
633
|
/** The SQL type an input column is cast to; every value is bound as text. */
|
|
552
634
|
const CAST = {
|
|
553
635
|
text: "text",
|
|
554
636
|
enum: "text",
|
|
555
637
|
crossRef: "text",
|
|
556
638
|
number: "float8",
|
|
639
|
+
int16: "int2",
|
|
640
|
+
int32: "int4",
|
|
641
|
+
int64: "bigint",
|
|
557
642
|
date: "timestamptz",
|
|
558
643
|
boolean: "boolean"
|
|
559
644
|
};
|
|
@@ -646,8 +731,8 @@ function inputCte(sql, table, rows, metadata, stored) {
|
|
|
646
731
|
const cols = writableColumns(table, rows);
|
|
647
732
|
const arrays = [
|
|
648
733
|
rows.map((r) => r.id),
|
|
649
|
-
rows.map((r) =>
|
|
650
|
-
rows.map((r) =>
|
|
734
|
+
rows.map((r) => stringifyJsonExact(r.extra)),
|
|
735
|
+
rows.map((r) => stringifyJsonExact(r.raw)),
|
|
651
736
|
rows.map((r) => JSON.stringify(r.unset)),
|
|
652
737
|
...cols.map((c) => rows.map((r) => {
|
|
653
738
|
const v = Object.hasOwn(r.columns, c.column) ? r.columns[c.column] : null;
|
|
@@ -1113,7 +1198,7 @@ const SAFE_CASTS = [`CREATE OR REPLACE FUNCTION substrate_num(v jsonb) RETURNS p
|
|
|
1113
1198
|
THEN v OPERATOR(pg_catalog.#>>) '{}' END,
|
|
1114
1199
|
'${String.raw`^([0-9]{4}-[0-9]{2}-[0-9]{2})(?:[T ]((?:[01][0-9]|2[0-3]):[0-5][0-9])(?::([0-5][0-9])(?:\.([0-9]{1,6})[0-9]{0,3})?)?)?(Z|[+-](?:0[0-9]|1[0-5])(?::?[0-5][0-9])?)?$`}') AS m
|
|
1115
1200
|
) AS n $$`];
|
|
1116
|
-
const TX_STATEMENTS$
|
|
1201
|
+
const TX_STATEMENTS$6 = [
|
|
1117
1202
|
`CREATE TABLE IF NOT EXISTS row_ids (
|
|
1118
1203
|
namespace text NOT NULL,
|
|
1119
1204
|
type text NOT NULL,
|
|
@@ -1230,7 +1315,7 @@ const uuidCheck = (table, column) => `DO $$ BEGIN
|
|
|
1230
1315
|
CHECK (${column} ~ '${UUID_REGEX}');
|
|
1231
1316
|
EXCEPTION WHEN duplicate_object THEN NULL;
|
|
1232
1317
|
END $$`;
|
|
1233
|
-
const TX_STATEMENTS$
|
|
1318
|
+
const TX_STATEMENTS$5 = [
|
|
1234
1319
|
uuidCheck("row_ids", "id"),
|
|
1235
1320
|
uuidCheck("short_ids", "row_id"),
|
|
1236
1321
|
uuidCheck("edges", "from_id"),
|
|
@@ -1256,7 +1341,7 @@ const TX_STATEMENTS$2 = [
|
|
|
1256
1341
|
* through a stale descriptor is retried rather than silently misplaced.
|
|
1257
1342
|
* A constant default: no table rewrite.
|
|
1258
1343
|
*/
|
|
1259
|
-
const TX_STATEMENTS$
|
|
1344
|
+
const TX_STATEMENTS$4 = ["ALTER TABLE substrate_type_tables ADD COLUMN IF NOT EXISTS version bigint NOT NULL DEFAULT 0"];
|
|
1260
1345
|
|
|
1261
1346
|
//#endregion
|
|
1262
1347
|
//#region ../../packages/substrate-dataset-pg/src/migrations/0004t_field_error_version.ts
|
|
@@ -1268,7 +1353,34 @@ const TX_STATEMENTS$1 = ["ALTER TABLE substrate_type_tables ADD COLUMN IF NOT EX
|
|
|
1268
1353
|
* than rescanning the whole table every sweep for a conflict that has not
|
|
1269
1354
|
* changed (promote.ts). Nullable, no default: no table rewrite.
|
|
1270
1355
|
*/
|
|
1271
|
-
const TX_STATEMENTS = ["ALTER TABLE substrate_type_fields ADD COLUMN IF NOT EXISTS error_version bigint NULL"];
|
|
1356
|
+
const TX_STATEMENTS$3 = ["ALTER TABLE substrate_type_fields ADD COLUMN IF NOT EXISTS error_version bigint NULL"];
|
|
1357
|
+
|
|
1358
|
+
//#endregion
|
|
1359
|
+
//#region ../../packages/substrate-dataset-pg/src/migrations/0005t_int64_field_kind.ts
|
|
1360
|
+
/**
|
|
1361
|
+
* 0005t_int64_field_kind — `int64` joins the field kinds the type catalog
|
|
1362
|
+
* accepts (a `bigint` column).
|
|
1363
|
+
*/
|
|
1364
|
+
const TX_STATEMENTS$2 = ["ALTER TABLE substrate_type_fields DROP CONSTRAINT IF EXISTS substrate_type_fields_kind_check", `ALTER TABLE substrate_type_fields ADD CONSTRAINT substrate_type_fields_kind_check
|
|
1365
|
+
CHECK (kind IN ('text','enum','number','int64','date','boolean','crossRef'))`];
|
|
1366
|
+
|
|
1367
|
+
//#endregion
|
|
1368
|
+
//#region ../../packages/substrate-dataset-pg/src/migrations/0006t_int32_field_kind.ts
|
|
1369
|
+
/**
|
|
1370
|
+
* 0006t_int32_field_kind — `int32` joins the field kinds the type catalog
|
|
1371
|
+
* accepts (an `integer` column).
|
|
1372
|
+
*/
|
|
1373
|
+
const TX_STATEMENTS$1 = ["ALTER TABLE substrate_type_fields DROP CONSTRAINT IF EXISTS substrate_type_fields_kind_check", `ALTER TABLE substrate_type_fields ADD CONSTRAINT substrate_type_fields_kind_check
|
|
1374
|
+
CHECK (kind IN ('text','enum','number','int32','int64','date','boolean','crossRef'))`];
|
|
1375
|
+
|
|
1376
|
+
//#endregion
|
|
1377
|
+
//#region ../../packages/substrate-dataset-pg/src/migrations/0007t_int16_field_kind.ts
|
|
1378
|
+
/**
|
|
1379
|
+
* 0007t_int16_field_kind — `int16` joins the field kinds the type catalog
|
|
1380
|
+
* accepts (a `smallint` column).
|
|
1381
|
+
*/
|
|
1382
|
+
const TX_STATEMENTS = ["ALTER TABLE substrate_type_fields DROP CONSTRAINT IF EXISTS substrate_type_fields_kind_check", `ALTER TABLE substrate_type_fields ADD CONSTRAINT substrate_type_fields_kind_check
|
|
1383
|
+
CHECK (kind IN ('text','enum','number','int16','int32','int64','date','boolean','crossRef'))`];
|
|
1272
1384
|
|
|
1273
1385
|
//#endregion
|
|
1274
1386
|
//#region ../../packages/substrate-dataset-pg/src/server-version.ts
|
|
@@ -1328,15 +1440,27 @@ async function assertServerVersion(sql) {
|
|
|
1328
1440
|
*/
|
|
1329
1441
|
const TABLES_BASELINE = {
|
|
1330
1442
|
version: "0001t_tables_baseline",
|
|
1331
|
-
txStatements: TX_STATEMENTS$
|
|
1443
|
+
txStatements: TX_STATEMENTS$6,
|
|
1332
1444
|
concurrentStatements: CONCURRENT_STATEMENTS
|
|
1333
1445
|
};
|
|
1334
1446
|
const TYPE_TABLE_VERSION = {
|
|
1335
1447
|
version: "0003t_type_table_version",
|
|
1336
|
-
txStatements: TX_STATEMENTS$
|
|
1448
|
+
txStatements: TX_STATEMENTS$4
|
|
1337
1449
|
};
|
|
1338
1450
|
const FIELD_ERROR_VERSION = {
|
|
1339
1451
|
version: "0004t_field_error_version",
|
|
1452
|
+
txStatements: TX_STATEMENTS$3
|
|
1453
|
+
};
|
|
1454
|
+
const INT64_FIELD_KIND = {
|
|
1455
|
+
version: "0005t_int64_field_kind",
|
|
1456
|
+
txStatements: TX_STATEMENTS$2
|
|
1457
|
+
};
|
|
1458
|
+
const INT32_FIELD_KIND = {
|
|
1459
|
+
version: "0006t_int32_field_kind",
|
|
1460
|
+
txStatements: TX_STATEMENTS$1
|
|
1461
|
+
};
|
|
1462
|
+
const INT16_FIELD_KIND = {
|
|
1463
|
+
version: "0007t_int16_field_kind",
|
|
1340
1464
|
txStatements: TX_STATEMENTS
|
|
1341
1465
|
};
|
|
1342
1466
|
function normalise(input) {
|
|
@@ -1358,12 +1482,12 @@ function normalise(input) {
|
|
|
1358
1482
|
*/
|
|
1359
1483
|
const UUID_CHECK_INPUT = {
|
|
1360
1484
|
version: UUID_CHECK_MIGRATION,
|
|
1361
|
-
txStatements: TX_STATEMENTS$
|
|
1485
|
+
txStatements: TX_STATEMENTS$5
|
|
1362
1486
|
};
|
|
1363
1487
|
/**
|
|
1364
1488
|
* The tables baseline, then the gated UUID CHECKs when
|
|
1365
1489
|
* `SUBSTRATE_ENFORCE_UUID_CHECK=true` (or the explicit override is true), then
|
|
1366
|
-
* the type tables' catalog version, then the field error version. Used
|
|
1490
|
+
* the type tables' catalog version, then the field error version, then the int64, int32 and int16 field kinds. Used
|
|
1367
1491
|
* by `runMigrations` and exported for unit tests.
|
|
1368
1492
|
*/
|
|
1369
1493
|
function getMigrationList(enforceUuidCheck = process.env.SUBSTRATE_ENFORCE_UUID_CHECK === "true") {
|
|
@@ -1371,7 +1495,10 @@ function getMigrationList(enforceUuidCheck = process.env.SUBSTRATE_ENFORCE_UUID_
|
|
|
1371
1495
|
TABLES_BASELINE,
|
|
1372
1496
|
...enforceUuidCheck === true ? [UUID_CHECK_INPUT] : [],
|
|
1373
1497
|
TYPE_TABLE_VERSION,
|
|
1374
|
-
FIELD_ERROR_VERSION
|
|
1498
|
+
FIELD_ERROR_VERSION,
|
|
1499
|
+
INT64_FIELD_KIND,
|
|
1500
|
+
INT32_FIELD_KIND,
|
|
1501
|
+
INT16_FIELD_KIND
|
|
1375
1502
|
].map(normalise);
|
|
1376
1503
|
}
|
|
1377
1504
|
/** @internal — the always-on list, exported for unit tests. */
|
|
@@ -1773,11 +1900,14 @@ function columnKindOf(def) {
|
|
|
1773
1900
|
* walk joins `far.id = near.f_x` (and `far.f_x = near.id`) bare, and an index
|
|
1774
1901
|
* on either side serves it; it is ordered under `"C"` where it sorts.
|
|
1775
1902
|
*/
|
|
1776
|
-
const PG_TYPE = {
|
|
1903
|
+
const PG_TYPE$1 = {
|
|
1777
1904
|
text: "text COLLATE \"C\"",
|
|
1778
1905
|
enum: "text COLLATE \"C\"",
|
|
1779
1906
|
crossRef: "text",
|
|
1780
1907
|
number: "double precision",
|
|
1908
|
+
int16: "smallint",
|
|
1909
|
+
int32: "integer",
|
|
1910
|
+
int64: "bigint",
|
|
1781
1911
|
date: "timestamptz",
|
|
1782
1912
|
boolean: "boolean"
|
|
1783
1913
|
};
|
|
@@ -2144,7 +2274,7 @@ function createTableSql(table, declared, enforceUuidCheck) {
|
|
|
2144
2274
|
];
|
|
2145
2275
|
for (const [field, { kind, vocab }] of declared) {
|
|
2146
2276
|
const column = columnName(field);
|
|
2147
|
-
parts.push(`${ident(column)} ${PG_TYPE[kind]}`);
|
|
2277
|
+
parts.push(`${ident(column)} ${PG_TYPE$1[kind]}`);
|
|
2148
2278
|
if (kind === "enum") parts.push(`CONSTRAINT ${ident(enumCheckName(column))} ${enumCheck(column, vocab)}`);
|
|
2149
2279
|
}
|
|
2150
2280
|
if (enforceUuidCheck) parts.push(`CONSTRAINT id_uuid_format CHECK (id ~ '${UUID_REGEX}')`);
|
|
@@ -2254,7 +2384,7 @@ async function lockTypeTable(tx, namespace, type) {
|
|
|
2254
2384
|
}
|
|
2255
2385
|
/** The ALTER TABLE clauses giving `column` its type, and its CHECK when it is an enum. */
|
|
2256
2386
|
function addColumnClauses(column, kind, vocab) {
|
|
2257
|
-
return [`ADD COLUMN IF NOT EXISTS ${ident(column)} ${PG_TYPE[kind]}`, ...kind === "enum" ? enumCheckClauses(column, vocab) : []];
|
|
2387
|
+
return [`ADD COLUMN IF NOT EXISTS ${ident(column)} ${PG_TYPE$1[kind]}`, ...kind === "enum" ? enumCheckClauses(column, vocab) : []];
|
|
2258
2388
|
}
|
|
2259
2389
|
/**
|
|
2260
2390
|
* Runs one DDL statement in a savepoint under a short lock_timeout. False when
|
|
@@ -2637,7 +2767,7 @@ function keyExpr(field, def) {
|
|
|
2637
2767
|
const typed = typedColumn(field, def);
|
|
2638
2768
|
if (typed !== void 0) return ident(typed);
|
|
2639
2769
|
if (def?.kind === "crossRef") return indexExpressionSql("text", field, void 0, "extra");
|
|
2640
|
-
if (def?.kind === "scalar" && def.type !== "json") return indexExpressionSql(def.type === "
|
|
2770
|
+
if (def?.kind === "scalar" && def.type !== "json") return indexExpressionSql(def.type === "int16" || def.type === "int32" ? "int64" : def.type === "number" || def.type === "int64" || def.type === "date" ? def.type : "text", field, void 0, "extra");
|
|
2641
2771
|
throw new Error(`field '${field}' is not a declared scalar or crossRef field and cannot be indexed`);
|
|
2642
2772
|
}
|
|
2643
2773
|
function desiredSpecs(args) {
|
|
@@ -2889,7 +3019,7 @@ async function build(conn, args, target, spec, name, opts) {
|
|
|
2889
3019
|
}
|
|
2890
3020
|
opts.onBuild?.(spec.name);
|
|
2891
3021
|
try {
|
|
2892
|
-
await
|
|
3022
|
+
await conn.unsafe(createSql(spec, target, name, true)).signal(spec.unique ? void 0 : opts.signal);
|
|
2893
3023
|
return;
|
|
2894
3024
|
} catch (err) {
|
|
2895
3025
|
if (spec.unique) throw err;
|
|
@@ -2939,22 +3069,8 @@ async function buildEstimate(conn, target, name) {
|
|
|
2939
3069
|
0)::text AS n`;
|
|
2940
3070
|
return Number(row?.n ?? 0);
|
|
2941
3071
|
}
|
|
2942
|
-
/** Runs `text` on `conn`, cancelling it in flight when `signal` aborts. */
|
|
2943
|
-
async function cancellable$1(conn, text, signal) {
|
|
2944
|
-
const query = conn.unsafe(text);
|
|
2945
|
-
const cancel = () => {
|
|
2946
|
-
Promise.resolve(query.cancel()).catch(() => void 0);
|
|
2947
|
-
};
|
|
2948
|
-
signal?.addEventListener("abort", cancel, { once: true });
|
|
2949
|
-
try {
|
|
2950
|
-
if (signal?.aborted === true) cancel();
|
|
2951
|
-
await query;
|
|
2952
|
-
} finally {
|
|
2953
|
-
signal?.removeEventListener("abort", cancel);
|
|
2954
|
-
}
|
|
2955
|
-
}
|
|
2956
3072
|
async function dropConcurrently(conn, name, signal) {
|
|
2957
|
-
await
|
|
3073
|
+
await conn.unsafe(`DROP INDEX CONCURRENTLY IF EXISTS ${name}`).signal(signal);
|
|
2958
3074
|
}
|
|
2959
3075
|
async function mark(conn, name) {
|
|
2960
3076
|
await conn.unsafe(`COMMENT ON INDEX ${name} IS '${MARK}'`);
|
|
@@ -3193,6 +3309,10 @@ function buildMetadataColumnPredicate(sql, field, predicate, alias) {
|
|
|
3193
3309
|
case "endsWith":
|
|
3194
3310
|
if (typeof operand === "string") frag = sql`${col} ILIKE ${`%${escapeLike(operand)}`}`;
|
|
3195
3311
|
break;
|
|
3312
|
+
case "like":
|
|
3313
|
+
case "notLike":
|
|
3314
|
+
frag = likeFragment(sql, col, field, op, operand);
|
|
3315
|
+
break;
|
|
3196
3316
|
case "gt":
|
|
3197
3317
|
frag = sql`${col} COLLATE "C" > ${metaStr(field, op, operand)}`;
|
|
3198
3318
|
break;
|
|
@@ -3279,7 +3399,7 @@ function memberOf(sql, col, values, operand) {
|
|
|
3279
3399
|
* `sortExpr` is the single source for ORDER BY and both halves of the keyset
|
|
3280
3400
|
* cursor comparison, so sorting and paging cannot diverge.
|
|
3281
3401
|
*/
|
|
3282
|
-
const
|
|
3402
|
+
const noComputed = () => void 0;
|
|
3283
3403
|
/**
|
|
3284
3404
|
* Maps a FieldDef to the pg cast applied to its `body ->> field` text
|
|
3285
3405
|
* extraction. Returns `null` for kinds where text compare is correct (string),
|
|
@@ -3287,6 +3407,9 @@ const noDerived = () => void 0;
|
|
|
3287
3407
|
*
|
|
3288
3408
|
* Type → cast table:
|
|
3289
3409
|
* field.number() / 'number' → 'numeric'
|
|
3410
|
+
* field.int16() / 'int16' → 'bigint'
|
|
3411
|
+
* field.int32() / 'int32' → 'bigint'
|
|
3412
|
+
* field.int64() / 'int64' → 'bigint'
|
|
3290
3413
|
* field.date() / 'date' → 'timestamptz'
|
|
3291
3414
|
* field.boolean() / 'boolean' → 'boolean'
|
|
3292
3415
|
* field.string() / 'string' → null (text compare is correct)
|
|
@@ -3301,6 +3424,9 @@ function resolveBodyOrderByCast(def) {
|
|
|
3301
3424
|
if (def.kind !== "scalar") return null;
|
|
3302
3425
|
switch (def.type) {
|
|
3303
3426
|
case "number": return "numeric";
|
|
3427
|
+
case "int16":
|
|
3428
|
+
case "int32":
|
|
3429
|
+
case "int64": return "bigint";
|
|
3304
3430
|
case "date": return "timestamptz";
|
|
3305
3431
|
case "boolean": return "boolean";
|
|
3306
3432
|
default: return null;
|
|
@@ -3311,10 +3437,10 @@ function resolveBodyOrderByCast(def) {
|
|
|
3311
3437
|
* SELECT list's column of that name first, and a date column is projected
|
|
3312
3438
|
* under its own name as text, which no index on the column serves.
|
|
3313
3439
|
*/
|
|
3314
|
-
function buildOrderBy(sql, orderBy, castFor, table,
|
|
3440
|
+
function buildOrderBy(sql, orderBy, castFor, table, computedFor = noComputed, alias) {
|
|
3315
3441
|
if (orderBy.length === 0) return sql``;
|
|
3316
3442
|
const parts = [];
|
|
3317
|
-
for (const [field, dir] of orderBy) parts.push(orderByFragment(sql, field, dir, castFor(field), table,
|
|
3443
|
+
for (const [field, dir] of orderBy) parts.push(orderByFragment(sql, field, dir, castFor(field), table, computedFor, alias));
|
|
3318
3444
|
if (parts.length === 1) return sql`ORDER BY ${parts[0]}`;
|
|
3319
3445
|
const commaFragment = sql`,`;
|
|
3320
3446
|
const interleaved = [parts[0]];
|
|
@@ -3328,10 +3454,10 @@ function buildOrderBy(sql, orderBy, castFor, table, derivedFor = noDerived, alia
|
|
|
3328
3454
|
* Do not case out field kinds anywhere else: if sorting and paging use
|
|
3329
3455
|
* different expressions, rows silently vanish from later pages.
|
|
3330
3456
|
*/
|
|
3331
|
-
function sortExpr(sql, field, bodyCast, table,
|
|
3457
|
+
function sortExpr(sql, field, bodyCast, table, computedFor, alias) {
|
|
3332
3458
|
if (field === "id") return sql`id`;
|
|
3333
|
-
const
|
|
3334
|
-
if (
|
|
3459
|
+
const computed = computedFor(field);
|
|
3460
|
+
if (computed !== void 0) return computed;
|
|
3335
3461
|
if (field === "shortId") return sql`s.short_id`;
|
|
3336
3462
|
if (isMetadataField(field)) {
|
|
3337
3463
|
const col = METADATA_COLUMNS$1[field];
|
|
@@ -3350,7 +3476,7 @@ function sortExpr(sql, field, bodyCast, table, derivedFor, alias) {
|
|
|
3350
3476
|
* Aliases are POSITIONAL (`__sort_0`, ...), not field-derived: pg truncates
|
|
3351
3477
|
* identifiers at 63 bytes, so long field names could collide.
|
|
3352
3478
|
*/
|
|
3353
|
-
function buildSortKeyProjection(sql, orderBy, castFor, table,
|
|
3479
|
+
function buildSortKeyProjection(sql, orderBy, castFor, table, computedFor = noComputed) {
|
|
3354
3480
|
const aliases = /* @__PURE__ */ new Map();
|
|
3355
3481
|
const fragments = [];
|
|
3356
3482
|
for (const [field] of orderBy) {
|
|
@@ -3358,8 +3484,8 @@ function buildSortKeyProjection(sql, orderBy, castFor, table, derivedFor = noDer
|
|
|
3358
3484
|
const alias = `__sort_${aliases.size}`;
|
|
3359
3485
|
aliases.set(field, alias);
|
|
3360
3486
|
const cast = castFor(field);
|
|
3361
|
-
const expr = sortExpr(sql, field, cast, table,
|
|
3362
|
-
fragments.push(sortKeyIsTimestamp(field, cast, table) ? sql`, (${expr})::text AS ${sql(alias)}` : sql`, ${expr} AS ${sql(alias)}`);
|
|
3487
|
+
const expr = sortExpr(sql, field, cast, table, computedFor);
|
|
3488
|
+
fragments.push(sortKeyIsTimestamp(field, cast, table, computedFor) ? sql`, (${expr})::text AS ${sql(alias)}` : sql`, ${expr} AS ${sql(alias)}`);
|
|
3363
3489
|
}
|
|
3364
3490
|
if (fragments.length === 0) return {
|
|
3365
3491
|
fragment: sql``,
|
|
@@ -3371,7 +3497,8 @@ function buildSortKeyProjection(sql, orderBy, castFor, table, derivedFor = noDer
|
|
|
3371
3497
|
};
|
|
3372
3498
|
}
|
|
3373
3499
|
/** Whether `buildSortKeyProjection` projects the field's key as timestamptz text. */
|
|
3374
|
-
function sortKeyIsTimestamp(field, cast, table) {
|
|
3500
|
+
function sortKeyIsTimestamp(field, cast, table, computedFor = noComputed) {
|
|
3501
|
+
if (computedFor(field) !== void 0) return computedFor.typeOf?.(field) === "date";
|
|
3375
3502
|
if (field === "createdAt" || field === "updatedAt") return true;
|
|
3376
3503
|
if (isMetadataField(field)) return false;
|
|
3377
3504
|
const column = table.column(field);
|
|
@@ -3383,7 +3510,7 @@ function sortKeyIsTimestamp(field, cast, table) {
|
|
|
3383
3510
|
* directions and NULLS LAST; a timestamp key, projected as text, casts back
|
|
3384
3511
|
* so it orders as a timestamp.
|
|
3385
3512
|
*/
|
|
3386
|
-
function buildProjectedOrderBy(sql, orderBy, aliases, castFor, table, rel) {
|
|
3513
|
+
function buildProjectedOrderBy(sql, orderBy, aliases, castFor, table, rel, computedFor = noComputed) {
|
|
3387
3514
|
const parts = [];
|
|
3388
3515
|
for (const [field, dir] of orderBy) {
|
|
3389
3516
|
let expr;
|
|
@@ -3391,7 +3518,7 @@ function buildProjectedOrderBy(sql, orderBy, aliases, castFor, table, rel) {
|
|
|
3391
3518
|
else {
|
|
3392
3519
|
const alias = aliases.get(field);
|
|
3393
3520
|
if (alias === void 0) throw new Error(`orderBy key '${field}' has no projected alias`);
|
|
3394
|
-
expr = sortKeyIsTimestamp(field, castFor(field), table) ? `${rel}.${alias}::timestamptz` : `${rel}.${alias}`;
|
|
3521
|
+
expr = sortKeyIsTimestamp(field, castFor(field), table, computedFor) ? `${rel}.${alias}::timestamptz` : `${rel}.${alias}`;
|
|
3395
3522
|
}
|
|
3396
3523
|
parts.push(`${expr} ${dir === "asc" ? "ASC" : "DESC"} NULLS LAST`);
|
|
3397
3524
|
}
|
|
@@ -3401,9 +3528,14 @@ function buildProjectedOrderBy(sql, orderBy, aliases, castFor, table, rel) {
|
|
|
3401
3528
|
* The parameter side of a comparison against `sortExpr(field)`, cast to match
|
|
3402
3529
|
* it. Keep its cases in step with `sortExpr`.
|
|
3403
3530
|
*/
|
|
3404
|
-
function sortParam(sql, field, value, bodyCast, table,
|
|
3531
|
+
function sortParam(sql, field, value, bodyCast, table, computedFor) {
|
|
3405
3532
|
if (field === "id") return sql`${idToText(value)}`;
|
|
3406
|
-
if (
|
|
3533
|
+
if (computedFor(field) !== void 0) {
|
|
3534
|
+
const type = computedFor.typeOf?.(field) ?? "number";
|
|
3535
|
+
if (type === "int64") return sql`${String(value)}::bigint`;
|
|
3536
|
+
if (type === "date") return tsParam(sql, value);
|
|
3537
|
+
if (type === "string") return sql`${String(value)}::text`;
|
|
3538
|
+
if (type === "boolean") return sql`${Boolean(value)}::boolean`;
|
|
3407
3539
|
if (typeof value !== "number" || !Number.isFinite(value)) throw new CursorDecodeError(`cursor value for '${field}' is not a finite number`);
|
|
3408
3540
|
return sql`${value}::float8`;
|
|
3409
3541
|
}
|
|
@@ -3414,13 +3546,14 @@ function sortParam(sql, field, value, bodyCast, table, derivedFor) {
|
|
|
3414
3546
|
if (column !== void 0) switch (columnAs(column.kind)) {
|
|
3415
3547
|
case "date": return tsParam(sql, value);
|
|
3416
3548
|
case "number": return sql`${value}::numeric`;
|
|
3549
|
+
case "int64": return sql`${String(value)}::bigint`;
|
|
3417
3550
|
case "boolean": return sql`${Boolean(value)}::boolean`;
|
|
3418
3551
|
default: return sql`${String(value)}::text`;
|
|
3419
3552
|
}
|
|
3420
3553
|
return castParam(sql, value, bodyCast);
|
|
3421
3554
|
}
|
|
3422
|
-
function orderByFragment(sql, field, dir, bodyCast, table,
|
|
3423
|
-
const expr = sortExpr(sql, field, bodyCast, table,
|
|
3555
|
+
function orderByFragment(sql, field, dir, bodyCast, table, computedFor, alias) {
|
|
3556
|
+
const expr = sortExpr(sql, field, bodyCast, table, computedFor, alias);
|
|
3424
3557
|
return dir === "asc" ? sql`${expr} ASC NULLS LAST` : sql`${expr} DESC NULLS LAST`;
|
|
3425
3558
|
}
|
|
3426
3559
|
/**
|
|
@@ -3432,6 +3565,7 @@ function bodyOrderByExpr(sql, field, cast, table) {
|
|
|
3432
3565
|
switch (cast) {
|
|
3433
3566
|
case null: return bodyText(sql, [field], void 0, table.jsonb);
|
|
3434
3567
|
case "numeric": return bodyNum(sql, [field], void 0, table.jsonb);
|
|
3568
|
+
case "bigint": return bodyInt64(sql, [field], void 0, table.jsonb);
|
|
3435
3569
|
case "timestamptz": return bodyTs(sql, [field], void 0, table.jsonb);
|
|
3436
3570
|
case "boolean": return sql`${bodyRawText(sql, [field], void 0, table.jsonb)}::boolean`;
|
|
3437
3571
|
}
|
|
@@ -3455,16 +3589,16 @@ function bodyOrderByExpr(sql, field, cast, table) {
|
|
|
3455
3589
|
* We build the disjunction explicitly — postgres's ROW(...) tuple compare
|
|
3456
3590
|
* would be simpler but doesn't honour per-column direction mixing.
|
|
3457
3591
|
*/
|
|
3458
|
-
function buildCursorCompare(sql, orderBy, values, cursorId, castFor, table,
|
|
3592
|
+
function buildCursorCompare(sql, orderBy, values, cursorId, castFor, table, computedFor = noComputed) {
|
|
3459
3593
|
const resolved = resolveCursorKeys(orderBy, values, cursorId);
|
|
3460
3594
|
const [lead, ...rest] = resolved;
|
|
3461
3595
|
if (lead === void 0) return null;
|
|
3462
|
-
if (lead.field === "id") return cursorDisjunction(sql, resolved, castFor, table,
|
|
3463
|
-
const restCmp = cursorDisjunction(sql, rest, castFor, table,
|
|
3596
|
+
if (lead.field === "id") return cursorDisjunction(sql, resolved, castFor, table, computedFor);
|
|
3597
|
+
const restCmp = cursorDisjunction(sql, rest, castFor, table, computedFor);
|
|
3464
3598
|
const cast = castFor(lead.field);
|
|
3465
|
-
const eq = cursorEqFragment(sql, lead.field, lead.value, cast, table,
|
|
3599
|
+
const eq = cursorEqFragment(sql, lead.field, lead.value, cast, table, computedFor);
|
|
3466
3600
|
if (lead.value === null || lead.value === void 0) return combineAnd(sql, [eq, restCmp]);
|
|
3467
|
-
const cmp = (op) => sql`${sortExpr(sql, lead.field, cast, table,
|
|
3601
|
+
const cmp = (op) => sql`${sortExpr(sql, lead.field, cast, table, computedFor)} ${sql.unsafe(op)} ${sortParam(sql, lead.field, lead.value, cast, table, computedFor)}`;
|
|
3468
3602
|
const asc = lead.dir === "asc";
|
|
3469
3603
|
return combineAnd(sql, [cmp(asc ? ">=" : "<="), combineOr(sql, [cmp(asc ? ">" : "<"), combineAnd(sql, [eq, restCmp])])]);
|
|
3470
3604
|
}
|
|
@@ -3475,11 +3609,11 @@ function buildCursorCompare(sql, orderBy, values, cursorId, castFor, table, deri
|
|
|
3475
3609
|
* tail query needs no other cursor condition. `createdAt` and `updatedAt` count
|
|
3476
3610
|
* as nullable: their columns are nullable in both baselines.
|
|
3477
3611
|
*/
|
|
3478
|
-
function buildCursorNullTail(sql, orderBy, values, castFor, table,
|
|
3612
|
+
function buildCursorNullTail(sql, orderBy, values, castFor, table, computedFor = noComputed) {
|
|
3479
3613
|
const lead = orderBy[0];
|
|
3480
3614
|
if (lead === void 0 || lead[0] === "id") return null;
|
|
3481
3615
|
if (values[0] === null || values[0] === void 0) return null;
|
|
3482
|
-
return sql`${sortExpr(sql, lead[0], castFor(lead[0]), table,
|
|
3616
|
+
return sql`${sortExpr(sql, lead[0], castFor(lead[0]), table, computedFor)} IS NULL`;
|
|
3483
3617
|
}
|
|
3484
3618
|
/** Pairs each orderBy key with its cursor value: `id` from `cursorId`, the rest from `values` in order. */
|
|
3485
3619
|
function resolveCursorKeys(orderBy, values, cursorId) {
|
|
@@ -3501,16 +3635,16 @@ function resolveCursorKeys(orderBy, values, cursorId) {
|
|
|
3501
3635
|
return resolved;
|
|
3502
3636
|
}
|
|
3503
3637
|
/** The OR over i of (eq₁ AND ... AND eqᵢ₋₁ AND cmpᵢ). */
|
|
3504
|
-
function cursorDisjunction(sql, resolved, castFor, table,
|
|
3638
|
+
function cursorDisjunction(sql, resolved, castFor, table, computedFor) {
|
|
3505
3639
|
const orParts = [];
|
|
3506
3640
|
for (let i = 0; i < resolved.length; i++) {
|
|
3507
3641
|
const parts = [];
|
|
3508
3642
|
for (let j = 0; j < i; j++) {
|
|
3509
3643
|
const prior = resolved[j];
|
|
3510
|
-
parts.push(cursorEqFragment(sql, prior.field, prior.value, castFor(prior.field), table,
|
|
3644
|
+
parts.push(cursorEqFragment(sql, prior.field, prior.value, castFor(prior.field), table, computedFor));
|
|
3511
3645
|
}
|
|
3512
3646
|
const cur = resolved[i];
|
|
3513
|
-
parts.push(cursorCmpFragment(sql, cur.field, cur.dir, cur.value, castFor(cur.field), table,
|
|
3647
|
+
parts.push(cursorCmpFragment(sql, cur.field, cur.dir, cur.value, castFor(cur.field), table, computedFor));
|
|
3514
3648
|
orParts.push(combineAnd(sql, parts));
|
|
3515
3649
|
}
|
|
3516
3650
|
return combineOr(sql, orParts);
|
|
@@ -3519,10 +3653,10 @@ function cursorDisjunction(sql, resolved, castFor, table, derivedFor) {
|
|
|
3519
3653
|
* Equality against the sort key, for the `eq₁ AND ... AND cmpᵢ` prefix of the
|
|
3520
3654
|
* keyset disjunction. A NULL sort key compares with `IS NULL`, never `= NULL`.
|
|
3521
3655
|
*/
|
|
3522
|
-
function cursorEqFragment(sql, field, value, bodyCast, table,
|
|
3523
|
-
const expr = sortExpr(sql, field, bodyCast, table,
|
|
3656
|
+
function cursorEqFragment(sql, field, value, bodyCast, table, computedFor) {
|
|
3657
|
+
const expr = sortExpr(sql, field, bodyCast, table, computedFor);
|
|
3524
3658
|
if (value === null || value === void 0) return sql`${expr} IS NULL`;
|
|
3525
|
-
return sql`${expr} = ${sortParam(sql, field, value, bodyCast, table,
|
|
3659
|
+
return sql`${expr} = ${sortParam(sql, field, value, bodyCast, table, computedFor)}`;
|
|
3526
3660
|
}
|
|
3527
3661
|
/**
|
|
3528
3662
|
* "Strictly after the cursor" for one sort key, under `NULLS LAST`. On a
|
|
@@ -3538,10 +3672,10 @@ function cursorEqFragment(sql, field, value, bodyCast, table, derivedFor) {
|
|
|
3538
3672
|
* with `eq` on the preceding keys, and returning `IS NULL` here would
|
|
3539
3673
|
* re-emit every null row that has already been served.
|
|
3540
3674
|
*/
|
|
3541
|
-
function cursorCmpFragment(sql, field, dir, value, bodyCast, table,
|
|
3542
|
-
const expr = sortExpr(sql, field, bodyCast, table,
|
|
3675
|
+
function cursorCmpFragment(sql, field, dir, value, bodyCast, table, computedFor) {
|
|
3676
|
+
const expr = sortExpr(sql, field, bodyCast, table, computedFor);
|
|
3543
3677
|
if (value === null || value === void 0) return sql`false`;
|
|
3544
|
-
const param = sortParam(sql, field, value, bodyCast, table,
|
|
3678
|
+
const param = sortParam(sql, field, value, bodyCast, table, computedFor);
|
|
3545
3679
|
if (field === "id") return dir === "asc" ? sql`${expr} > ${param}` : sql`${expr} < ${param}`;
|
|
3546
3680
|
return dir === "asc" ? sql`(${expr} > ${param} OR ${expr} IS NULL)` : sql`(${expr} < ${param} OR ${expr} IS NULL)`;
|
|
3547
3681
|
}
|
|
@@ -3557,6 +3691,264 @@ function castParam(sql, value, cast) {
|
|
|
3557
3691
|
return sql`${param}${sql.unsafe(`::${cast}`)}`;
|
|
3558
3692
|
}
|
|
3559
3693
|
|
|
3694
|
+
//#endregion
|
|
3695
|
+
//#region ../../packages/substrate-dataset-pg/src/pg-sql/expression.ts
|
|
3696
|
+
/**
|
|
3697
|
+
* Compiles a read-grammar `Expr` to a Postgres scalar expression that returns
|
|
3698
|
+
* what the memory evaluator (`evalExpr`) returns. Every literal is a bound
|
|
3699
|
+
* parameter; the only inlined constants are date units drawn from
|
|
3700
|
+
* `DATE_UNITS`. Types: number is float8, int64 bigint, string text, boolean
|
|
3701
|
+
* boolean, date timestamptz.
|
|
3702
|
+
*/
|
|
3703
|
+
const KIND_TYPE = {
|
|
3704
|
+
text: "string",
|
|
3705
|
+
enum: "string",
|
|
3706
|
+
crossRef: "string",
|
|
3707
|
+
number: "number",
|
|
3708
|
+
int16: "int64",
|
|
3709
|
+
int32: "int64",
|
|
3710
|
+
int64: "int64",
|
|
3711
|
+
date: "date",
|
|
3712
|
+
boolean: "boolean"
|
|
3713
|
+
};
|
|
3714
|
+
const AS = {
|
|
3715
|
+
string: "text",
|
|
3716
|
+
number: "number",
|
|
3717
|
+
int64: "int64",
|
|
3718
|
+
date: "date",
|
|
3719
|
+
boolean: "boolean"
|
|
3720
|
+
};
|
|
3721
|
+
const PG_TYPE = {
|
|
3722
|
+
string: "text",
|
|
3723
|
+
number: "float8",
|
|
3724
|
+
int64: "bigint",
|
|
3725
|
+
date: "timestamptz",
|
|
3726
|
+
boolean: "boolean"
|
|
3727
|
+
};
|
|
3728
|
+
/** The compiled expression and its type (`null` for a bare null literal). */
|
|
3729
|
+
function compileExprTyped(sql, expr, ctx) {
|
|
3730
|
+
const u = (s) => sql.unsafe(s);
|
|
3731
|
+
const cast = (t) => PG_TYPE[t];
|
|
3732
|
+
/** `NULL` typed as `t`, for a slot that needs a type. */
|
|
3733
|
+
const nul = (t) => u(`NULL::${cast(t)}`);
|
|
3734
|
+
/** `n` as type `t`: a null literal takes the type, int64 widens to float8. */
|
|
3735
|
+
const as = (n, t) => n.t === null ? nul(t) : n.t === "int64" && t === "number" ? sql`(${n.f})::float8` : n.f;
|
|
3736
|
+
const join = (fs, op) => {
|
|
3737
|
+
const out = [fs[0]];
|
|
3738
|
+
for (const f of fs.slice(1)) out.push(u(op), f);
|
|
3739
|
+
return out;
|
|
3740
|
+
};
|
|
3741
|
+
const interleave = (fs, op) => sql`(${join(fs, op)})`;
|
|
3742
|
+
const isInt = (a, b) => a.t === "int64" && (b.t === "int64" || b.bigint !== void 0) || b.t === "int64" && a.bigint !== void 0;
|
|
3743
|
+
const big = (n) => n.t === "int64" ? n.f : n.bigint;
|
|
3744
|
+
const toDate = (n) => n.t === "date" ? n.f : n.t === null ? nul("date") : sql`substrate_ts(to_jsonb((${n.f})::text))`;
|
|
3745
|
+
/** The type branches unify to, and each branch cast to it (an int64 beside a number widens). */
|
|
3746
|
+
const unifyAll = (ns) => {
|
|
3747
|
+
const types = ns.map((n) => n.t).filter((t) => t !== null);
|
|
3748
|
+
const numeric_ = types.length > 0 && types.every((t) => t === "number" || t === "int64");
|
|
3749
|
+
const int = types.includes("int64") && ns.every((n) => n.t === "int64" || n.t === null || n.bigint);
|
|
3750
|
+
const t = !numeric_ ? types[0] ?? null : int ? "int64" : "number";
|
|
3751
|
+
return {
|
|
3752
|
+
t,
|
|
3753
|
+
fs: ns.map((n) => t === null ? n.f : t === "int64" && n.t !== "int64" && n.bigint ? n.bigint : as(n, t))
|
|
3754
|
+
};
|
|
3755
|
+
};
|
|
3756
|
+
const go = (e) => {
|
|
3757
|
+
if (e === null) return {
|
|
3758
|
+
f: u("NULL"),
|
|
3759
|
+
t: null
|
|
3760
|
+
};
|
|
3761
|
+
if (typeof e === "boolean") return {
|
|
3762
|
+
f: sql`${e}::boolean`,
|
|
3763
|
+
t: "boolean"
|
|
3764
|
+
};
|
|
3765
|
+
if (typeof e === "number") return {
|
|
3766
|
+
f: sql`${String(e)}::float8`,
|
|
3767
|
+
t: "number",
|
|
3768
|
+
...Number.isSafeInteger(e) ? { bigint: sql`${String(e)}::bigint` } : {}
|
|
3769
|
+
};
|
|
3770
|
+
if (typeof e === "string") return field(e);
|
|
3771
|
+
if ("lit" in e) return {
|
|
3772
|
+
f: sql`${e.lit}::text`,
|
|
3773
|
+
t: "string"
|
|
3774
|
+
};
|
|
3775
|
+
if ("int64" in e) return {
|
|
3776
|
+
f: sql`${e.int64}::bigint`,
|
|
3777
|
+
t: "int64"
|
|
3778
|
+
};
|
|
3779
|
+
const key = Object.keys(e)[0];
|
|
3780
|
+
return call(key, e[key]);
|
|
3781
|
+
};
|
|
3782
|
+
const field = (name) => {
|
|
3783
|
+
const out = ctx.outputs?.get(name);
|
|
3784
|
+
const col = ctx.table.column(name);
|
|
3785
|
+
const meta = out === void 0 ? metadataExprType(name) : void 0;
|
|
3786
|
+
if (meta !== void 0) {
|
|
3787
|
+
const c = u(metadataColumnSql(name, ctx.alias));
|
|
3788
|
+
return meta === "number" ? {
|
|
3789
|
+
f: sql`(${c})::float8`,
|
|
3790
|
+
t: meta
|
|
3791
|
+
} : {
|
|
3792
|
+
f: c,
|
|
3793
|
+
t: meta
|
|
3794
|
+
};
|
|
3795
|
+
}
|
|
3796
|
+
const t = ctx.fieldType?.(name) ?? (col ? KIND_TYPE[col.kind] : void 0);
|
|
3797
|
+
if (t === void 0) throw new Error(`internal: no type for expression field '${name}'`);
|
|
3798
|
+
if (out !== void 0) return {
|
|
3799
|
+
f: t === "number" ? sql`(${out})::float8` : out,
|
|
3800
|
+
t
|
|
3801
|
+
};
|
|
3802
|
+
const f = fieldExprSql(ctx.table, name, AS[t], ctx.alias);
|
|
3803
|
+
const wide = t === "number" ? `(${f})::float8` : col?.kind === "int16" || col?.kind === "int32" ? `(${f})::bigint` : f;
|
|
3804
|
+
return {
|
|
3805
|
+
f: u(wide),
|
|
3806
|
+
t
|
|
3807
|
+
};
|
|
3808
|
+
};
|
|
3809
|
+
const call = (key, args) => {
|
|
3810
|
+
const n = key === "case" ? [] : args.map(go);
|
|
3811
|
+
const [a, b, c] = n;
|
|
3812
|
+
const bool = (f) => ({
|
|
3813
|
+
f,
|
|
3814
|
+
t: "boolean"
|
|
3815
|
+
});
|
|
3816
|
+
const str = (f) => ({
|
|
3817
|
+
f,
|
|
3818
|
+
t: "string"
|
|
3819
|
+
});
|
|
3820
|
+
const text = (x) => x.t === null ? nul("string") : x.f;
|
|
3821
|
+
const compare = (x, y, op) => {
|
|
3822
|
+
if (x.t === "string" || y.t === "string") return sql`((${as(x, "string")}) COLLATE "C" ${u(op)} (${as(y, "string")}) COLLATE "C")`;
|
|
3823
|
+
if (isInt(x, y)) return sql`(${big(x)} ${u(op)} ${big(y)})`;
|
|
3824
|
+
return sql`(${x.f} ${u(op)} ${y.f})`;
|
|
3825
|
+
};
|
|
3826
|
+
const cmp = (op) => bool(compare(a, b, op));
|
|
3827
|
+
switch (key) {
|
|
3828
|
+
case "add":
|
|
3829
|
+
case "sub":
|
|
3830
|
+
case "mul": {
|
|
3831
|
+
const op = {
|
|
3832
|
+
add: "+",
|
|
3833
|
+
sub: "-",
|
|
3834
|
+
mul: "*"
|
|
3835
|
+
}[key];
|
|
3836
|
+
const t = isInt(a, b) ? "int64" : "number";
|
|
3837
|
+
if (a.t === null || b.t === null) return {
|
|
3838
|
+
f: nul(t),
|
|
3839
|
+
t
|
|
3840
|
+
};
|
|
3841
|
+
return t === "int64" ? {
|
|
3842
|
+
f: sql`(${big(a)} ${u(op)} ${big(b)})`,
|
|
3843
|
+
t
|
|
3844
|
+
} : {
|
|
3845
|
+
f: sql`(${as(a, "number")} ${u(op)} ${as(b, "number")})`,
|
|
3846
|
+
t
|
|
3847
|
+
};
|
|
3848
|
+
}
|
|
3849
|
+
case "mod":
|
|
3850
|
+
if (a.t === null || b.t === null) return {
|
|
3851
|
+
f: nul("int64"),
|
|
3852
|
+
t: "int64"
|
|
3853
|
+
};
|
|
3854
|
+
return {
|
|
3855
|
+
f: sql`(${big(a)} % NULLIF(${big(b)}, 0))`,
|
|
3856
|
+
t: "int64"
|
|
3857
|
+
};
|
|
3858
|
+
case "div":
|
|
3859
|
+
if (a.t === null || b.t === null) return {
|
|
3860
|
+
f: nul("number"),
|
|
3861
|
+
t: "number"
|
|
3862
|
+
};
|
|
3863
|
+
return {
|
|
3864
|
+
f: sql`(${as(a, "number")} / NULLIF(${as(b, "number")}, 0))`,
|
|
3865
|
+
t: "number"
|
|
3866
|
+
};
|
|
3867
|
+
case "neg":
|
|
3868
|
+
if (a.t === null) return {
|
|
3869
|
+
f: nul("number"),
|
|
3870
|
+
t: "number"
|
|
3871
|
+
};
|
|
3872
|
+
return {
|
|
3873
|
+
f: sql`(-(${a.f}))`,
|
|
3874
|
+
t: a.t === "int64" ? "int64" : "number"
|
|
3875
|
+
};
|
|
3876
|
+
case "eq": return cmp("=");
|
|
3877
|
+
case "neq": return cmp("<>");
|
|
3878
|
+
case "gt": return cmp(">");
|
|
3879
|
+
case "gte": return cmp(">=");
|
|
3880
|
+
case "lt": return cmp("<");
|
|
3881
|
+
case "lte": return cmp("<=");
|
|
3882
|
+
case "and":
|
|
3883
|
+
case "or": return bool(interleave(n.map((x) => as(x, "boolean")), key === "and" ? " AND " : " OR "));
|
|
3884
|
+
case "not": return bool(sql`(NOT ${as(a, "boolean")})`);
|
|
3885
|
+
case "isNull": return bool(sql`((${a.f}) IS NULL)`);
|
|
3886
|
+
case "in": {
|
|
3887
|
+
const rest = n.slice(1).map((r) => a.t === "int64" && r.bigint ? r.bigint : r.f);
|
|
3888
|
+
return bool(sql`(${a.f} IN (${join(rest, ",")}))`);
|
|
3889
|
+
}
|
|
3890
|
+
case "length": return {
|
|
3891
|
+
f: sql`char_length(${text(a)})::float8`,
|
|
3892
|
+
t: "number"
|
|
3893
|
+
};
|
|
3894
|
+
case "lower": return str(sql`lower((${text(a)}) COLLATE "C")`);
|
|
3895
|
+
case "upper": return str(sql`upper((${text(a)}) COLLATE "C")`);
|
|
3896
|
+
case "substring": {
|
|
3897
|
+
const int = (x, lo) => sql`LEAST(GREATEST(floor(${as(x, "number")} + 0.5), ${u(String(lo))}), 2147483647)::int`;
|
|
3898
|
+
return str(sql`substring(${text(a)} from ${int(b, -2147483648)} for GREATEST(${int(c, 0)}, 0))`);
|
|
3899
|
+
}
|
|
3900
|
+
case "concat": return str(interleave(n.map(text), " || "));
|
|
3901
|
+
case "like":
|
|
3902
|
+
case "notLike": return bool(sql`(${u(key === "like" ? "" : "NOT ")}(${text(a)} LIKE ${text(b)} ESCAPE '\\'))`);
|
|
3903
|
+
case "regexpMatch": return bool(sql`((${text(a)}) COLLATE "default" ~ ${text(b)})`);
|
|
3904
|
+
case "regexpReplace": return str(sql`regexp_replace(${text(a)}, ${text(b)}, ${text(c)})`);
|
|
3905
|
+
case "dateTrunc":
|
|
3906
|
+
case "extract": {
|
|
3907
|
+
const unit = args[0].lit;
|
|
3908
|
+
if (!DATE_UNITS.includes(unit)) throw new Error(`internal: '${unit}' is not a date unit`);
|
|
3909
|
+
const d = sql`(${toDate(b)}) AT TIME ZONE 'UTC'`;
|
|
3910
|
+
return key === "extract" ? {
|
|
3911
|
+
f: sql`extract(${u(unit)} FROM ${d})::float8`,
|
|
3912
|
+
t: "number"
|
|
3913
|
+
} : {
|
|
3914
|
+
f: sql`(date_trunc(${u(`'${unit}'`)}, ${d}) AT TIME ZONE 'UTC')`,
|
|
3915
|
+
t: "date"
|
|
3916
|
+
};
|
|
3917
|
+
}
|
|
3918
|
+
case "case": {
|
|
3919
|
+
const branches = args.slice(0, -1).map((p) => p.map(go));
|
|
3920
|
+
const els = go(args[args.length - 1]);
|
|
3921
|
+
const { t, fs } = unifyAll([...branches.map(([, th]) => th), els]);
|
|
3922
|
+
const parts = [u("CASE")];
|
|
3923
|
+
branches.forEach(([cond], i) => {
|
|
3924
|
+
parts.push(u("WHEN"), as(cond, "boolean"), u("THEN"), fs[i]);
|
|
3925
|
+
});
|
|
3926
|
+
parts.push(u("ELSE"), fs[fs.length - 1], u("END"));
|
|
3927
|
+
return {
|
|
3928
|
+
f: sql`(${parts})`,
|
|
3929
|
+
t
|
|
3930
|
+
};
|
|
3931
|
+
}
|
|
3932
|
+
case "coalesce": {
|
|
3933
|
+
const { t, fs } = unifyAll(n);
|
|
3934
|
+
return {
|
|
3935
|
+
f: sql`COALESCE(${join(fs, ",")})`,
|
|
3936
|
+
t
|
|
3937
|
+
};
|
|
3938
|
+
}
|
|
3939
|
+
default: throw new Error(`unknown function '${key}'`);
|
|
3940
|
+
}
|
|
3941
|
+
};
|
|
3942
|
+
const root = go(expr);
|
|
3943
|
+
return {
|
|
3944
|
+
fragment: root.f,
|
|
3945
|
+
type: root.t
|
|
3946
|
+
};
|
|
3947
|
+
}
|
|
3948
|
+
function compileExpr(sql, expr, ctx) {
|
|
3949
|
+
return compileExprTyped(sql, expr, ctx).fragment;
|
|
3950
|
+
}
|
|
3951
|
+
|
|
3560
3952
|
//#endregion
|
|
3561
3953
|
//#region ../../packages/substrate-dataset-pg/src/pg-sql/predicates-json-path.ts
|
|
3562
3954
|
/**
|
|
@@ -3680,8 +4072,9 @@ function buildJsonPathPredicate(sql, field, predicate, alias, jsonb) {
|
|
|
3680
4072
|
* is-null filter, and a compare on an absent value is FALSE, never NULL, so a
|
|
3681
4073
|
* caller's NOT stays sound. Throws `UnsupportedFilterError` as the body path does.
|
|
3682
4074
|
*/
|
|
3683
|
-
function buildColumnPredicate(sql, field, table, column,
|
|
4075
|
+
function buildColumnPredicate(sql, field, table, column, rawPredicate, alias) {
|
|
3684
4076
|
const as = columnAs(column.kind);
|
|
4077
|
+
const predicate = as === "int64" ? int64Operands(field, rawPredicate) : rawPredicate;
|
|
3685
4078
|
const read = fieldExpr(sql, table, field, as, alias);
|
|
3686
4079
|
/**
|
|
3687
4080
|
* A promoting enum field reads its `$raw` string over the rest, as `decodeRow`
|
|
@@ -3699,6 +4092,7 @@ function buildColumnPredicate(sql, field, table, column, predicate, alias) {
|
|
|
3699
4092
|
if (typeof v === "string") return sql`${v}::text`;
|
|
3700
4093
|
return column.kind === "crossRef" && typeof v === "number" ? sql`${String(v)}::text` : void 0;
|
|
3701
4094
|
case "number": return typeof v === "number" ? sql`${v}` : void 0;
|
|
4095
|
+
case "int64": return typeof v === "bigint" ? sql`${v.toString()}::bigint` : void 0;
|
|
3702
4096
|
case "date": return typeof v === "string" || v instanceof Date ? tsParam(sql, v) : void 0;
|
|
3703
4097
|
case "boolean": return typeof v === "boolean" ? sql.unsafe(v ? "TRUE" : "FALSE") : void 0;
|
|
3704
4098
|
}
|
|
@@ -3730,12 +4124,12 @@ function buildColumnPredicate(sql, field, table, column, predicate, alias) {
|
|
|
3730
4124
|
const fit = values.filter(fits);
|
|
3731
4125
|
const misfit = values.filter((v) => !fits(v));
|
|
3732
4126
|
if (fit.length === 0) return misfit.length === 0 ? sql`FALSE` : jsonAnyOf(misfit);
|
|
3733
|
-
const list = as === "date" ? tsArrayParam(sql, fit) : as === "number" ? sql`${fit}::numeric[]` : as === "boolean" ? sql`${fit}::boolean[]` : sql`${fit.map(String)}::text[]`;
|
|
4127
|
+
const list = as === "date" ? tsArrayParam(sql, fit) : as === "number" ? sql`${fit}::numeric[]` : as === "int64" ? sql`${fit.map(String)}::text[]::bigint[]` : as === "boolean" ? sql`${fit}::boolean[]` : sql`${fit.map(String)}::text[]`;
|
|
3734
4128
|
const any = guarded(sql`${expr} = ANY(${list})`);
|
|
3735
4129
|
return misfit.length === 0 ? any : sql`(${any} OR ${jsonAnyOf(misfit)})`;
|
|
3736
4130
|
};
|
|
3737
4131
|
const ordered = (op, v) => {
|
|
3738
|
-
if (typeof v !== "number" && typeof v !== "string" && !(v instanceof Date)) throw new UnsupportedFilterError(field, op, `cannot compare against a ${v === null ? "null" : typeof v} value`);
|
|
4132
|
+
if (typeof v !== "number" && typeof v !== "bigint" && typeof v !== "string" && !(v instanceof Date)) throw new UnsupportedFilterError(field, op, `cannot compare against a ${v === null ? "null" : typeof v} value`);
|
|
3739
4133
|
return param(v);
|
|
3740
4134
|
};
|
|
3741
4135
|
const fragments = [];
|
|
@@ -3797,6 +4191,10 @@ function buildColumnPredicate(sql, field, table, column, predicate, alias) {
|
|
|
3797
4191
|
fragments.push(sql`${fieldExpr(sql, table, field, "rawText", alias)} ILIKE ${pattern}`);
|
|
3798
4192
|
break;
|
|
3799
4193
|
}
|
|
4194
|
+
case "like":
|
|
4195
|
+
case "notLike":
|
|
4196
|
+
fragments.push(as !== "text" && as !== "date" ? sql`FALSE` : likeFragment(sql, fieldExpr(sql, table, field, "rawText", alias), field, op, operand));
|
|
4197
|
+
break;
|
|
3800
4198
|
case "has":
|
|
3801
4199
|
fragments.push(sql`FALSE`);
|
|
3802
4200
|
break;
|
|
@@ -3805,6 +4203,26 @@ function buildColumnPredicate(sql, field, table, column, predicate, alias) {
|
|
|
3805
4203
|
if (fragments.length === 0) return null;
|
|
3806
4204
|
return combineAnd(sql, fragments);
|
|
3807
4205
|
}
|
|
4206
|
+
/**
|
|
4207
|
+
* An int64 column's predicate with every operand read as a bigint, as the
|
|
4208
|
+
* memory evaluator reads them. An operand that is not an int64 is refused.
|
|
4209
|
+
*/
|
|
4210
|
+
function int64Operands(field, predicate) {
|
|
4211
|
+
const one = (op, v) => {
|
|
4212
|
+
if (v === null) return v;
|
|
4213
|
+
const n = parseInt64(v);
|
|
4214
|
+
if (n === void 0) throw new UnsupportedFilterError(field, op, "requires an int64 operand");
|
|
4215
|
+
return n;
|
|
4216
|
+
};
|
|
4217
|
+
return Object.fromEntries(Object.entries(predicate).map(([op, v]) => [op, Array.isArray(v) && (op === "in" || op === "nin" || op === "between") ? v.map((e) => one(op, e)) : [
|
|
4218
|
+
"eq",
|
|
4219
|
+
"neq",
|
|
4220
|
+
"gt",
|
|
4221
|
+
"gte",
|
|
4222
|
+
"lt",
|
|
4223
|
+
"lte"
|
|
4224
|
+
].includes(op) ? one(op, v) : v]));
|
|
4225
|
+
}
|
|
3808
4226
|
|
|
3809
4227
|
//#endregion
|
|
3810
4228
|
//#region ../../packages/substrate-dataset-pg/src/pg-sql/where.ts
|
|
@@ -3813,6 +4231,13 @@ function buildColumnPredicate(sql, field, table, column, predicate, alias) {
|
|
|
3813
4231
|
* dispatch (id / shortId / metadata / promoted-column / body-path), the
|
|
3814
4232
|
* operator-fragment compiler, and the `conditionalUpdate` match fragment.
|
|
3815
4233
|
*/
|
|
4234
|
+
const CAST_TYPE = {
|
|
4235
|
+
numeric: "number",
|
|
4236
|
+
bigint: "int64",
|
|
4237
|
+
timestamptz: "date",
|
|
4238
|
+
boolean: "boolean",
|
|
4239
|
+
text: "string"
|
|
4240
|
+
};
|
|
3816
4241
|
/**
|
|
3817
4242
|
* The WHERE fragment for `where`, or null when it constrains nothing. With a
|
|
3818
4243
|
* `scope`, expressions are qualified by `scope.alias` and the walk grammar
|
|
@@ -3844,6 +4269,14 @@ function buildWhere(sql, where, castFor, table, scope) {
|
|
|
3844
4269
|
} else if (k === "NOT") {
|
|
3845
4270
|
const inner = buildWhere(sql, child, castFor, table, scope);
|
|
3846
4271
|
if (inner !== null) fragments.push(sql`NOT (${inner})`);
|
|
4272
|
+
} else if (k === "$expr") {
|
|
4273
|
+
const fieldType = (name) => table.column(name) === void 0 ? CAST_TYPE[castFor(name) ?? "text"] : void 0;
|
|
4274
|
+
const expr = compileExpr(sql, child, {
|
|
4275
|
+
table,
|
|
4276
|
+
...scope === void 0 ? {} : { alias: scope.alias },
|
|
4277
|
+
fieldType
|
|
4278
|
+
});
|
|
4279
|
+
fragments.push(sql`COALESCE(${expr}, FALSE)`);
|
|
3847
4280
|
} else if (k === "_walk") {
|
|
3848
4281
|
if (scope === void 0) throw new ResolverFrameError(WALK_UNSUPPORTED_REF, `'_walk' is not supported here`);
|
|
3849
4282
|
fragments.push(buildWalk(sql, child, table, scope));
|
|
@@ -3902,7 +4335,7 @@ function buildFieldPredicateForPath(sql, path, predicate, castFor, alias, jsonb)
|
|
|
3902
4335
|
}
|
|
3903
4336
|
const declaredCast = path.length === 1 ? castFor(field) : null;
|
|
3904
4337
|
const fragments = [];
|
|
3905
|
-
for (const [op, operand] of entries) {
|
|
4338
|
+
for (const [op, operand] of declaredCast === "bigint" ? Object.entries(int64Operands(field, predicate)) : entries) {
|
|
3906
4339
|
const frag = buildOpFragment(sql, path, op, operand, declaredCast, alias, jsonb);
|
|
3907
4340
|
if (frag !== null) fragments.push(frag);
|
|
3908
4341
|
}
|
|
@@ -3927,6 +4360,8 @@ const KNOWN_BODY_OPS = /* @__PURE__ */ new Set([
|
|
|
3927
4360
|
"contains",
|
|
3928
4361
|
"startsWith",
|
|
3929
4362
|
"endsWith",
|
|
4363
|
+
"like",
|
|
4364
|
+
"notLike",
|
|
3930
4365
|
"has"
|
|
3931
4366
|
]);
|
|
3932
4367
|
/** Recurse into a struct sub-clause, carrying the accumulated body path. */
|
|
@@ -3970,6 +4405,10 @@ function buildIdPredicate(sql, predicate, alias) {
|
|
|
3970
4405
|
case "endsWith":
|
|
3971
4406
|
if (typeof operand === "string") frag = sql`${id} ILIKE ${`%${escapeLike(operand)}`}`;
|
|
3972
4407
|
break;
|
|
4408
|
+
case "like":
|
|
4409
|
+
case "notLike":
|
|
4410
|
+
frag = likeFragment(sql, id, "id", op, operand);
|
|
4411
|
+
break;
|
|
3973
4412
|
case "gt":
|
|
3974
4413
|
frag = sql`${id} > ${metaStr("id", op, operand)}`;
|
|
3975
4414
|
break;
|
|
@@ -4058,12 +4497,15 @@ function buildOpFragment(sql, path, op, operand, declaredCast, alias, jsonb) {
|
|
|
4058
4497
|
const field = path.join(".");
|
|
4059
4498
|
const textExpr = bodyText(sql, path, alias, jsonb);
|
|
4060
4499
|
const num = bodyNum(sql, path, alias, jsonb);
|
|
4500
|
+
const i64 = bodyInt64(sql, path, alias, jsonb);
|
|
4061
4501
|
const ts = bodyTs(sql, path, alias, jsonb);
|
|
4062
4502
|
const isTs = (v) => v instanceof Date || declaredCast === "timestamptz" && typeof v === "string";
|
|
4063
4503
|
const anyOf = (values) => {
|
|
4064
4504
|
const nums = values.filter((v) => typeof v === "number");
|
|
4065
|
-
const
|
|
4505
|
+
const bigs = values.filter((v) => typeof v === "bigint");
|
|
4506
|
+
const rest = values.filter((v) => typeof v !== "number" && typeof v !== "bigint");
|
|
4066
4507
|
const parts = [];
|
|
4508
|
+
if (bigs.length > 0) parts.push(notNullAnd(sql, i64, sql`${i64} = ANY(${bigs.map(String)}::text[]::bigint[])`));
|
|
4067
4509
|
if (nums.length > 0) parts.push(notNullAnd(sql, num, sql`${num} = ANY(${nums})`));
|
|
4068
4510
|
if (rest.length > 0) parts.push(isTs(rest[0]) ? notNullAnd(sql, ts, sql`${ts} = ANY(${tsArrayParam(sql, rest)})`) : notNullAnd(sql, textExpr, sql`${textExpr} = ANY(${rest.map(scalarParam)})`));
|
|
4069
4511
|
return combineOr(sql, parts);
|
|
@@ -4073,12 +4515,14 @@ function buildOpFragment(sql, path, op, operand, declaredCast, alias, jsonb) {
|
|
|
4073
4515
|
if (operand === null) return sql`${textExpr} IS NULL`;
|
|
4074
4516
|
if (typeof operand === "boolean") return sql`${textExpr} = ${sql.unsafe(operand ? "'true'" : "'false'")}`;
|
|
4075
4517
|
if (typeof operand === "number") return notNullAnd(sql, num, sql`${num} = ${operand}`);
|
|
4518
|
+
if (typeof operand === "bigint") return notNullAnd(sql, i64, sql`${i64} = ${operand.toString()}::bigint`);
|
|
4076
4519
|
if (isTs(operand)) return notNullAnd(sql, ts, sql`${ts} = ${tsParam(sql, operand)}`);
|
|
4077
4520
|
return sql`${textExpr} = ${scalarParam(operand)}`;
|
|
4078
4521
|
case "neq":
|
|
4079
4522
|
if (operand === null) return sql`${textExpr} IS NOT NULL`;
|
|
4080
4523
|
if (typeof operand === "boolean") return sql`${textExpr} IS DISTINCT FROM ${sql.unsafe(operand ? "'true'" : "'false'")}`;
|
|
4081
4524
|
if (typeof operand === "number") return sql`NOT ${notNullAnd(sql, num, sql`${num} = ${operand}`)}`;
|
|
4525
|
+
if (typeof operand === "bigint") return sql`NOT ${notNullAnd(sql, i64, sql`${i64} = ${operand.toString()}::bigint`)}`;
|
|
4082
4526
|
if (isTs(operand)) return sql`${ts} IS DISTINCT FROM ${tsParam(sql, operand)}`;
|
|
4083
4527
|
return sql`${textExpr} IS DISTINCT FROM ${scalarParam(operand)}`;
|
|
4084
4528
|
case "gt": return rangeCmp(sql, path, operand, ">", declaredCast, alias, jsonb);
|
|
@@ -4090,6 +4534,7 @@ function buildOpFragment(sql, path, op, operand, declaredCast, alias, jsonb) {
|
|
|
4090
4534
|
const low = operand[0];
|
|
4091
4535
|
const high = operand[1];
|
|
4092
4536
|
if (typeof low === "number" && typeof high === "number") return notNullAnd(sql, num, sql`${num} BETWEEN ${low} AND ${high}`);
|
|
4537
|
+
if (typeof low === "bigint" && typeof high === "bigint") return notNullAnd(sql, i64, sql`${i64} BETWEEN ${low.toString()}::bigint AND ${high.toString()}::bigint`);
|
|
4093
4538
|
if (isTs(low) || isTs(high)) return notNullAnd(sql, ts, sql`${ts} BETWEEN ${tsParam(sql, low)} AND ${tsParam(sql, high)}`);
|
|
4094
4539
|
return sql`${textExpr} BETWEEN ${scalarParam(low)} AND ${scalarParam(high)}`;
|
|
4095
4540
|
}
|
|
@@ -4118,6 +4563,8 @@ function buildOpFragment(sql, path, op, operand, declaredCast, alias, jsonb) {
|
|
|
4118
4563
|
const pattern = op === "contains" ? `%${e}%` : op === "startsWith" ? `${e}%` : `%${e}`;
|
|
4119
4564
|
return sql`${bodyRawText(sql, path, alias, jsonb)} ILIKE ${pattern}`;
|
|
4120
4565
|
}
|
|
4566
|
+
case "like":
|
|
4567
|
+
case "notLike": return likeFragment(sql, bodyRawText(sql, path, alias, jsonb), field, op, operand);
|
|
4121
4568
|
case "has": {
|
|
4122
4569
|
const elem = operand instanceof Date ? operand.toISOString() : operand;
|
|
4123
4570
|
return sql`${bodyJson(sql, path, alias, jsonb)} @> ${sql.json(asJsonValue([elem]))}`;
|
|
@@ -4144,6 +4591,10 @@ function rangeCmp(sql, path, operand, cmp, declaredCast, alias, jsonb) {
|
|
|
4144
4591
|
const num = bodyNum(sql, path, alias, jsonb);
|
|
4145
4592
|
return notNullAnd(sql, num, compare(num, operand));
|
|
4146
4593
|
}
|
|
4594
|
+
if (typeof operand === "bigint") {
|
|
4595
|
+
const i64 = bodyInt64(sql, path, alias, jsonb);
|
|
4596
|
+
return notNullAnd(sql, i64, compare(i64, sql`${operand.toString()}::bigint`));
|
|
4597
|
+
}
|
|
4147
4598
|
if (operand instanceof Date || declaredCast === "timestamptz" && typeof operand === "string") {
|
|
4148
4599
|
const ts = bodyTs(sql, path, alias, jsonb);
|
|
4149
4600
|
return notNullAnd(sql, ts, compare(ts, tsParam(sql, operand)));
|
|
@@ -4163,7 +4614,7 @@ function conditionalMatchFragment(sql, field, expected, table) {
|
|
|
4163
4614
|
const v = fieldExpr(sql, table, field, "json");
|
|
4164
4615
|
if (expected === null || expected === void 0) return sql`(${v} IS NULL OR ${v} = 'null'::jsonb)`;
|
|
4165
4616
|
if (expected instanceof Date) return sql`${fieldExpr(sql, table, field, "date")} = ${tsParam(sql, expected)}`;
|
|
4166
|
-
return sql`${v} = ${JSON.stringify(expected)}::text::jsonb`;
|
|
4617
|
+
return sql`${v} = ${typeof expected === "bigint" ? expected.toString() : JSON.stringify(expected)}::text::jsonb`;
|
|
4167
4618
|
}
|
|
4168
4619
|
const textExpr = bodyText(sql, [field]);
|
|
4169
4620
|
if (expected === null || expected === void 0) return sql`${textExpr} IS NULL`;
|
|
@@ -4539,6 +4990,7 @@ function sortKey(sql, table, field, cast, alias) {
|
|
|
4539
4990
|
const column = table.column(field);
|
|
4540
4991
|
if (column !== void 0) return columnOrderExpr(sql, table, column, alias);
|
|
4541
4992
|
if (cast === "numeric") return bodyNum(sql, [field], alias, table.jsonb);
|
|
4993
|
+
if (cast === "bigint") return bodyInt64(sql, [field], alias, table.jsonb);
|
|
4542
4994
|
if (cast === "timestamptz") return bodyTs(sql, [field], alias, table.jsonb);
|
|
4543
4995
|
return bodyText(sql, [field], alias, table.jsonb);
|
|
4544
4996
|
}
|
|
@@ -4664,27 +5116,6 @@ function refOperand(sql, field, op, ref, kind, scope) {
|
|
|
4664
5116
|
const micros = ref.plus === void 0 ? 0 : Math.round(ref.plus * 1e3);
|
|
4665
5117
|
return micros === 0 ? right : sql`(${right} + ${sql.unsafe(`(${micros})`)} * interval '1 microsecond')`;
|
|
4666
5118
|
}
|
|
4667
|
-
/**
|
|
4668
|
-
* A `params.derive` value for the seed row, as float8: `diff` is a − b and
|
|
4669
|
-
* `ratio` a / b, NULL for a null or non-numeric operand or a zero divisor, as
|
|
4670
|
-
* memory's; `value` is its one operand as it is. Operands are `$.<field>` or `_agg.<name>`, read as `$ref`s are.
|
|
4671
|
-
*
|
|
4672
|
-
* As memory's: a magnitude outside [1e-300, 1e300] is NULL, and 0 stays only
|
|
4673
|
-
* when exact (x = y, or a zero numerator). float8 raises on overflow and on a
|
|
4674
|
-
* division that underflows to 0, so the guard computes only where neither can
|
|
4675
|
-
* happen (operands within 1e307 or of one sign; a log ratio within ±692, which
|
|
4676
|
-
* is ±(ln 1e300 + 1.2)), and anything it skips lies outside the range anyway.
|
|
4677
|
-
* One expression, so ORDER BY, the cursor compare and the null tail share it.
|
|
4678
|
-
*/
|
|
4679
|
-
function buildDerive(sql, name, spec, scope) {
|
|
4680
|
-
if ("value" in spec) return refOperand(sql, `$${name}`, "value", { $ref: spec.value }, "number", scope);
|
|
4681
|
-
const [op, [a, b]] = Object.entries(spec)[0];
|
|
4682
|
-
const operand = (r) => refOperand(sql, `$${name}`, op, { $ref: r }, "number", scope);
|
|
4683
|
-
return sql`(SELECT ${op === "diff" ? sql.unsafe(`CASE WHEN sign(x) * sign(y) >= 0 OR greatest(abs(x), abs(y)) <= 1e307
|
|
4684
|
-
THEN CASE WHEN x = y OR abs(x - y) BETWEEN 1e-300 AND 1e300 THEN x - y END END`) : sql.unsafe(`CASE WHEN y = 0 THEN NULL WHEN x = 0 THEN x / y
|
|
4685
|
-
WHEN abs(ln(abs(x)) - ln(abs(y))) <= 692
|
|
4686
|
-
THEN CASE WHEN abs(x / y) BETWEEN 1e-300 AND 1e300 THEN x / y END END`)} FROM (SELECT ${operand(a)} AS x, ${operand(b)} AS y OFFSET 0) d)`;
|
|
4687
|
-
}
|
|
4688
5119
|
const f64 = /* @__PURE__ */ new Float64Array(1);
|
|
4689
5120
|
const u64 = new BigUint64Array(f64.buffer);
|
|
4690
5121
|
function bitsOf(x) {
|
|
@@ -4772,7 +5203,7 @@ function aggOver(sql, spec, table, alias, fieldDef) {
|
|
|
4772
5203
|
agg: sql`to_jsonb(count(*))`,
|
|
4773
5204
|
kind: "number"
|
|
4774
5205
|
};
|
|
4775
|
-
if ("latest" in spec
|
|
5206
|
+
if ("latest" in spec) {
|
|
4776
5207
|
const { by, value } = spec.latest;
|
|
4777
5208
|
const key = sortKey(sql, table, by, resolveBodyOrderByCast(fieldDef(by)), alias);
|
|
4778
5209
|
const numeric = value === "seq" || resolveBodyOrderByCast(fieldDef(value)) === "numeric";
|
|
@@ -4807,19 +5238,22 @@ function aggOver(sql, spec, table, alias, fieldDef) {
|
|
|
4807
5238
|
agg: sql`to_jsonb(${sql.unsafe(op)}(${n}))`,
|
|
4808
5239
|
kind: "number"
|
|
4809
5240
|
};
|
|
4810
|
-
const
|
|
4811
|
-
const v = isMetadataField(field) ? sql.unsafe(`${metadataColumnSql(field, alias)}::numeric`) : c === void 0 || extraWins(c) ? fieldExpr(sql, table, field, "number", alias) : sql`${fieldExpr(sql, table, field, "number", alias)}::text::numeric`;
|
|
4812
|
-
const s = sql`${sql.unsafe(op)}(${v})`;
|
|
5241
|
+
const s = sql`${sql.unsafe(op)}(${exactNumeric(sql, table, field, alias)})`;
|
|
4813
5242
|
return {
|
|
4814
|
-
agg: sql`to_jsonb(CASE WHEN abs(${s}) >= ${sql.unsafe(OVERFLOW)} THEN NULL
|
|
4815
|
-
WHEN abs(${s}) <= ${sql.unsafe(UNDERFLOW)} THEN 0::float8 ELSE ${s}::float8 END)`,
|
|
5243
|
+
agg: sql`to_jsonb(CASE WHEN abs(${s}) >= ${sql.unsafe(OVERFLOW$1)} THEN NULL
|
|
5244
|
+
WHEN abs(${s}) <= ${sql.unsafe(UNDERFLOW$1)} THEN 0::float8 ELSE ${s}::float8 END)`,
|
|
4816
5245
|
kind: "number"
|
|
4817
5246
|
};
|
|
4818
5247
|
}
|
|
5248
|
+
/** A number field read as an exact numeric: a stored decimal is never narrowed through float8. */
|
|
5249
|
+
function exactNumeric(sql, table, field, alias) {
|
|
5250
|
+
const c = table.column(field);
|
|
5251
|
+
return isMetadataField(field) ? sql.unsafe(`${metadataColumnSql(field, alias)}::numeric`) : c === void 0 || extraWins(c) ? fieldExpr(sql, table, field, "number", alias) : sql`${fieldExpr(sql, table, field, "number", alias)}::text::numeric`;
|
|
5252
|
+
}
|
|
4819
5253
|
/** 2^1024 − 2^970, the least value float8 rounds to Infinity, as an exact numeric. */
|
|
4820
|
-
const OVERFLOW = `${2n ** 1024n - 2n ** 970n}::numeric`;
|
|
5254
|
+
const OVERFLOW$1 = `${2n ** 1024n - 2n ** 970n}::numeric`;
|
|
4821
5255
|
/** 2^-1075 (= 5^1075 / 10^1075), the greatest value float8 rounds to 0, as an exact numeric. */
|
|
4822
|
-
const UNDERFLOW = `0.${(5n ** 1075n).toString().padStart(1075, "0")}::numeric`;
|
|
5256
|
+
const UNDERFLOW$1 = `0.${(5n ** 1075n).toString().padStart(1075, "0")}::numeric`;
|
|
4823
5257
|
/**
|
|
4824
5258
|
* `p` over `table`'s rows aliased `alias` passing `where`: `ORDER BY key, id LIMIT 1`,
|
|
4825
5259
|
* a top-1 an index can serve, never a sort of every row. An index keyed the
|
|
@@ -4968,46 +5402,6 @@ function buildFrameCount(sql, where, scope, joins = sql``) {
|
|
|
4968
5402
|
return sql`SELECT count(*)::float8 AS n FROM ${sql.unsafe(from)} ${sql.unsafe(scope.alias)}${joins} WHERE ${where}`;
|
|
4969
5403
|
}
|
|
4970
5404
|
/**
|
|
4971
|
-
* A whole-result aggregate over `scope`'s rows matching `where`, one row
|
|
4972
|
-
* `{ g }` per group: one group, or one per UTC day/hour of `bucket.field`
|
|
4973
|
-
* ascending, rows with no readable date in none, at most `limit` groups.
|
|
4974
|
-
* A pick reads the whole result through `pickRow`, shadowing the outer
|
|
4975
|
-
* alias; in a bucket it runs in two phases: GROUP BY finds the winning key,
|
|
4976
|
-
* then a top-1 on `key = it` (the indexed expression; the bucket key only
|
|
4977
|
-
* pins the lookup, never the frame's WHERE) fetches that row's stored value.
|
|
4978
|
-
*/
|
|
4979
|
-
function buildFrameAggregate(sql, aggregate, bucket, where, limit, scope) {
|
|
4980
|
-
const r = scope.alias;
|
|
4981
|
-
const R = sql.unsafe(r);
|
|
4982
|
-
const table = scope.tableFor(scope.type);
|
|
4983
|
-
const T = sql.unsafe(table.from);
|
|
4984
|
-
const fieldDef = (f) => scope.fieldDef(scope.type, f);
|
|
4985
|
-
const aggs = [];
|
|
4986
|
-
const picks = [];
|
|
4987
|
-
for (const [name, spec] of Object.entries(aggregate)) {
|
|
4988
|
-
const { agg, pick } = aggOver(sql, spec, table, r, fieldDef);
|
|
4989
|
-
if (pick === void 0) aggs.push(sql`${name}::text, ${agg}`);
|
|
4990
|
-
else picks.push([name, pick]);
|
|
4991
|
-
}
|
|
4992
|
-
if (bucket === void 0) {
|
|
4993
|
-
const picked = picks.map(([name, p]) => sql`${name}::text, ${pickRow(sql, p, table, r, where)}`);
|
|
4994
|
-
return sql`SELECT ${aggs.length === 0 ? sql`'{}'::jsonb` : sql`(SELECT ${buildObject(sql, aggs)} FROM ${T} ${R} WHERE ${where})`} || ${buildObject(sql, picked)} AS g`;
|
|
4995
|
-
}
|
|
4996
|
-
const [by, fmt] = bucket.by === "day" ? ["day", "YYYY-MM-DD"] : ["hour", "YYYY-MM-DD\"T\"HH24"];
|
|
4997
|
-
const key = sql`to_char(date_trunc(${sql.unsafe(`'${by}'`)}, ${refValue(sql, table, bucket.field, "date", r)} AT TIME ZONE 'UTC'), ${sql.unsafe(`'${fmt}'`)})`;
|
|
4998
|
-
const winners = picks.reduce((acc, [, p], i) => sql`${acc}, ${sql.unsafe(p.dir === "ASC" ? "min" : "max")}(${p.key}) AS ${sql.unsafe(`m${i}`)}`, sql``);
|
|
4999
|
-
const picked = picks.map(([name, p], i) => {
|
|
5000
|
-
const at = combineAnd(sql, [
|
|
5001
|
-
where,
|
|
5002
|
-
sql`${p.key} = ${sql.unsafe(`x.m${i}`)}`,
|
|
5003
|
-
sql`${key} = x.b`
|
|
5004
|
-
]);
|
|
5005
|
-
return sql`${name}::text, ${pickRow(sql, p, table, r, at)}`;
|
|
5006
|
-
});
|
|
5007
|
-
const cut = limit === void 0 ? sql`` : sql`LIMIT ${limit + 1}`;
|
|
5008
|
-
return sql`SELECT jsonb_build_object('bucket', x.b) || x.g || ${buildObject(sql, picked)} AS g FROM (SELECT ${key} AS b, ${buildObject(sql, aggs)} AS g${winners} FROM ${T} ${R} WHERE ${where} GROUP BY 1) x WHERE x.b IS NOT NULL ORDER BY x.b COLLATE "C" ${cut}`;
|
|
5009
|
-
}
|
|
5010
|
-
/**
|
|
5011
5405
|
* `{ _agg, <cmp>: operand }`: never NULL, false for a null aggregate or a
|
|
5012
5406
|
* null operand. Dates compare as instants; a number operand (or any operand
|
|
5013
5407
|
* of a number aggregate) as float8; anything else as stored, eq/neq by jsonb
|
|
@@ -5067,11 +5461,91 @@ function buildWalkProjection(sql, aggs, nested, table, scope) {
|
|
|
5067
5461
|
return sql`(SELECT ${buildObject(sql, pairs)}${truncated} FROM (SELECT ${list} OFFSET 0) ${sql.unsafe(x)})`;
|
|
5068
5462
|
}
|
|
5069
5463
|
/**
|
|
5070
|
-
*
|
|
5071
|
-
*
|
|
5072
|
-
*
|
|
5464
|
+
* A row's top-level walk selections as their own SELECT items, beside `extra`:
|
|
5465
|
+
* `_agg` as one jsonb object, each nested list as a json array of up to
|
|
5466
|
+
* `limit + 1` rows. {@link mergeWalkColumns} cuts the lists and merges them
|
|
5467
|
+
* into `extra` as {@link buildWalkProjection} does in SQL, which costs pg far
|
|
5468
|
+
* more per row on a large read. Null when there are none.
|
|
5469
|
+
*/
|
|
5470
|
+
function buildWalkColumns(sql, aggs, nested, table, scope) {
|
|
5471
|
+
const columns = [];
|
|
5472
|
+
const items = [];
|
|
5473
|
+
if (aggs !== void 0) {
|
|
5474
|
+
columns.push({
|
|
5475
|
+
name: "_agg",
|
|
5476
|
+
column: "__walk_agg"
|
|
5477
|
+
});
|
|
5478
|
+
items.push(sql`${buildWalkAggs(sql, aggs, table, scope)} AS __walk_agg`);
|
|
5479
|
+
}
|
|
5480
|
+
for (const [i, [name, sel]] of Object.entries(nested).entries()) {
|
|
5481
|
+
const column = `__walk_${i}`;
|
|
5482
|
+
columns.push(sel.limit === void 0 ? {
|
|
5483
|
+
name,
|
|
5484
|
+
column
|
|
5485
|
+
} : {
|
|
5486
|
+
name,
|
|
5487
|
+
column,
|
|
5488
|
+
limit: sel.limit
|
|
5489
|
+
});
|
|
5490
|
+
items.push(sql`${buildNested(sql, sel, table, scope, "json")} AS ${sql.unsafe(column)}`);
|
|
5491
|
+
}
|
|
5492
|
+
if (items.length === 0) return null;
|
|
5493
|
+
return {
|
|
5494
|
+
select: sql`${items.flatMap((f, i) => i === 0 ? [f] : [sql`,`, f])}`,
|
|
5495
|
+
columns
|
|
5496
|
+
};
|
|
5497
|
+
}
|
|
5498
|
+
/**
|
|
5499
|
+
* `record` with its walk columns merged into `extra` as SQL's `extra || walk`
|
|
5500
|
+
* would: each list cut to its limit, `_truncated` naming the lists that had
|
|
5501
|
+
* more, the walk's keys winning, and every key in jsonb's order (shorter
|
|
5502
|
+
* first, then bytewise), so `decodeRow` reads the row it did before. Pass one
|
|
5503
|
+
* `orders` map per read: it caches the sorted order of each key set seen.
|
|
5504
|
+
*/
|
|
5505
|
+
function mergeWalkColumns(record, columns, orders = /* @__PURE__ */ new Map()) {
|
|
5506
|
+
const extra = record.extra ?? {};
|
|
5507
|
+
const walk = {};
|
|
5508
|
+
const cut = {};
|
|
5509
|
+
let anyCut = false;
|
|
5510
|
+
for (const c of columns) {
|
|
5511
|
+
const v = record[c.column];
|
|
5512
|
+
if (c.limit !== void 0 && Array.isArray(v) && v.length > c.limit) {
|
|
5513
|
+
walk[c.name] = v.slice(0, c.limit);
|
|
5514
|
+
cut[c.name] = true;
|
|
5515
|
+
anyCut = true;
|
|
5516
|
+
} else walk[c.name] = v;
|
|
5517
|
+
}
|
|
5518
|
+
if (anyCut) walk._truncated = jsonbOrdered(cut);
|
|
5519
|
+
const extraKeys = Object.keys(extra);
|
|
5520
|
+
const sig = `${anyCut ? 1 : 0}${extraKeys.join("\0")}`;
|
|
5521
|
+
let order = orders.get(sig);
|
|
5522
|
+
if (order === void 0) {
|
|
5523
|
+
order = [.../* @__PURE__ */ new Set([...extraKeys, ...Object.keys(walk)])].sort(jsonbKeyOrder);
|
|
5524
|
+
orders.set(sig, order);
|
|
5525
|
+
}
|
|
5526
|
+
const merged = {};
|
|
5527
|
+
for (const k of order) merged[k] = Object.hasOwn(walk, k) ? walk[k] : extra[k];
|
|
5528
|
+
record.extra = merged;
|
|
5529
|
+
return record;
|
|
5530
|
+
}
|
|
5531
|
+
/** jsonb's object key order: shorter (in UTF-8 bytes) first, then bytewise. */
|
|
5532
|
+
function jsonbKeyOrder(a, b) {
|
|
5533
|
+
if (Buffer.byteLength(a) === a.length && Buffer.byteLength(b) === b.length) return a.length - b.length || (a < b ? -1 : a > b ? 1 : 0);
|
|
5534
|
+
const x = Buffer.from(a);
|
|
5535
|
+
const y = Buffer.from(b);
|
|
5536
|
+
return x.length - y.length || Buffer.compare(x, y);
|
|
5537
|
+
}
|
|
5538
|
+
function jsonbOrdered(o) {
|
|
5539
|
+
const out = {};
|
|
5540
|
+
for (const k of Object.keys(o).sort(jsonbKeyOrder)) out[k] = o[k];
|
|
5541
|
+
return out;
|
|
5542
|
+
}
|
|
5543
|
+
/**
|
|
5544
|
+
* One nested selection for `scope`'s row: a jsonb array (or, with `as` json, a
|
|
5545
|
+
* json array of jsonb objects) of up to `limit + 1` far rows, each `id`, its
|
|
5546
|
+
* selected fields (null when absent) and its own walk selections; no `_metadata`.
|
|
5073
5547
|
*/
|
|
5074
|
-
function buildNested(sql, sel, table, scope) {
|
|
5548
|
+
function buildNested(sql, sel, table, scope, as = "jsonb") {
|
|
5075
5549
|
const alias = scope.nextAlias();
|
|
5076
5550
|
const a = sql.unsafe(alias);
|
|
5077
5551
|
const { type, from, cond } = hop(sql, sel._walk, scope, alias, table);
|
|
@@ -5095,12 +5569,177 @@ function buildNested(sql, sel, table, scope) {
|
|
|
5095
5569
|
const id = sql.unsafe(`CASE WHEN jsonb_typeof(${doc} -> 'id') IN ('number', 'string') THEN ${doc} -> 'id' ELSE to_jsonb(${alias}.id) END`);
|
|
5096
5570
|
const obj = buildObject(sql, [sql`'id'::text, ${id}`, ...plain.map((k) => sql`${k}::text, ${fieldExpr(sql, farTable, k, "json", alias)}`)]);
|
|
5097
5571
|
const sub = buildWalkProjection(sql, subAggs, subNested, farTable, farScope);
|
|
5098
|
-
|
|
5572
|
+
const row = sub === null ? obj : sql`${obj} || ${sub}`;
|
|
5573
|
+
return sql`(SELECT COALESCE(${sql.unsafe(as === "json" ? "json_agg" : "jsonb_agg")}(${row} ORDER BY ${orderBy}), ${sql.unsafe(`'[]'::${as}`)}) FROM (SELECT ${a}.* FROM ${sql.unsafe(from)} ${a} WHERE ${combineAnd(sql, [cond, ...filters])} ORDER BY ${orderBy} ${limit}) ${a})`;
|
|
5574
|
+
}
|
|
5575
|
+
|
|
5576
|
+
//#endregion
|
|
5577
|
+
//#region ../../packages/substrate-dataset-pg/src/pg-sql/grouped.ts
|
|
5578
|
+
/**
|
|
5579
|
+
* A grouped read as one statement:
|
|
5580
|
+
*
|
|
5581
|
+
* SELECT … FROM (SELECT <keys>, <aggs> FROM T r JOIN… WHERE … GROUP BY 1, 2…) x
|
|
5582
|
+
* WHERE <having> ORDER BY … LIMIT limit+1 OFFSET offset
|
|
5583
|
+
*
|
|
5584
|
+
* The inner select's `GROUP BY` names the keys by ordinal and everything after
|
|
5585
|
+
* it reads them as `x.k<i>` / `x.a<j>`: a key expression is never written
|
|
5586
|
+
* twice, because each copy of a literal is its own bind parameter and Postgres
|
|
5587
|
+
* would then not see them as one expression. The memory reference is
|
|
5588
|
+
* `@mnmal-ai/substrate-filter`'s `groupRows`.
|
|
5589
|
+
*/
|
|
5590
|
+
/** 2^1024 − 2^970, the least value float8 rounds to Infinity, as an exact numeric. */
|
|
5591
|
+
const OVERFLOW = `${2n ** 1024n - 2n ** 970n}::numeric`;
|
|
5592
|
+
/** 2^-1075, the greatest value float8 rounds to 0, as an exact numeric. */
|
|
5593
|
+
const UNDERFLOW = `0.${(5n ** 1075n).toString().padStart(1075, "0")}::numeric`;
|
|
5594
|
+
const MICROS = `'YYYY-MM-DD"T"HH24:MI:SS.US"Z"'`;
|
|
5595
|
+
function buildGroupedRead(sql, spec, matched, joins, table, fieldDef) {
|
|
5596
|
+
const u = (s) => sql.unsafe(s);
|
|
5597
|
+
const fieldType = (name) => exprTypeOfField(fieldDef(name));
|
|
5598
|
+
const compile = (e) => compileExprTyped(sql, e, {
|
|
5599
|
+
table,
|
|
5600
|
+
alias: "r",
|
|
5601
|
+
fieldType
|
|
5602
|
+
});
|
|
5603
|
+
const list = (fs) => fs.reduce((a, b) => sql`${a}, ${b}`);
|
|
5604
|
+
const keys = Object.entries(spec.groupBy ?? {}).map(([name, e], i) => {
|
|
5605
|
+
const c = compile(e);
|
|
5606
|
+
if (c.type === null) throw new Error(`internal: group key '${name}' has no type`);
|
|
5607
|
+
return {
|
|
5608
|
+
name,
|
|
5609
|
+
i,
|
|
5610
|
+
frag: c.fragment,
|
|
5611
|
+
type: c.type
|
|
5612
|
+
};
|
|
5613
|
+
});
|
|
5614
|
+
const aggs = Object.entries(spec.aggregate).map(([name, a], j) => {
|
|
5615
|
+
const { op, arg } = aggParts(a);
|
|
5616
|
+
const c = arg === true && op === "count" ? void 0 : compile(arg);
|
|
5617
|
+
const t = c?.type ?? null;
|
|
5618
|
+
const f = c?.fragment;
|
|
5619
|
+
const text = typeof arg === "string" && t === "number" && metadataExprType(arg) === void 0 ? exactNumeric(sql, table, arg, "r") : sql`(${f})::text::numeric`;
|
|
5620
|
+
let raw;
|
|
5621
|
+
switch (op) {
|
|
5622
|
+
case "count":
|
|
5623
|
+
raw = c === void 0 ? sql`count(*)` : sql`count(${f})`;
|
|
5624
|
+
break;
|
|
5625
|
+
case "countDistinct":
|
|
5626
|
+
raw = sql`count(DISTINCT ${f})`;
|
|
5627
|
+
break;
|
|
5628
|
+
case "sum":
|
|
5629
|
+
raw = t === "int64" ? sql`sum(${f})::bigint` : sql`sum(${text})`;
|
|
5630
|
+
break;
|
|
5631
|
+
case "avg":
|
|
5632
|
+
raw = t === "int64" ? sql`sum(${f})::float8 / NULLIF(count(${f}), 0)::float8` : sql`avg(${text})`;
|
|
5633
|
+
break;
|
|
5634
|
+
default: raw = sql`${u(op)}(${t === "string" ? sql`(${f}) COLLATE "C"` : f})`;
|
|
5635
|
+
}
|
|
5636
|
+
const type = aggType(op, t);
|
|
5637
|
+
const a_ = u(`x.a${j}`);
|
|
5638
|
+
let typed;
|
|
5639
|
+
if (op === "count" || op === "countDistinct") typed = sql`${a_}::float8`;
|
|
5640
|
+
else if (op === "avg" || op === "sum") typed = t === "int64" ? a_ : floatOf(a_);
|
|
5641
|
+
else typed = a_;
|
|
5642
|
+
return {
|
|
5643
|
+
name,
|
|
5644
|
+
j,
|
|
5645
|
+
raw,
|
|
5646
|
+
typed,
|
|
5647
|
+
type,
|
|
5648
|
+
raises: op === "sum" && t === "int64" || canRaise(arg)
|
|
5649
|
+
};
|
|
5650
|
+
});
|
|
5651
|
+
function floatOf(s) {
|
|
5652
|
+
return sql`(CASE WHEN abs(${s}) >= ${u(OVERFLOW)} THEN NULL WHEN abs(${s}) <= ${u(UNDERFLOW)} THEN 0::float8 ELSE ${s}::float8 END)`;
|
|
5653
|
+
}
|
|
5654
|
+
const raises = aggs.some((a) => a.raises) || Object.values(spec.groupBy ?? {}).some(canRaise) || canRaise(spec.having);
|
|
5655
|
+
const grouped = sql`SELECT ${list([...keys.map((k) => sql`${k.frag} AS ${u(`k${k.i}`)}`), ...aggs.map((a) => sql`${a.raw} AS ${u(`a${a.j}`)}`)])} FROM ${u(table.from)} r${joins} WHERE ${matched}${keys.length === 0 ? sql`` : sql` GROUP BY ${u(keys.map((_, i) => i + 1).join(", "))}`} OFFSET 0`;
|
|
5656
|
+
const inner = raises ? sql`SELECT y.*, count(*) OVER () AS w FROM (${grouped}) y OFFSET 0` : grouped;
|
|
5657
|
+
const outputs = new Map([...keys.map((k) => [k.name, u(`x.k${k.i}`)]), ...aggs.map((a) => [a.name, a.typed])]);
|
|
5658
|
+
const types = new Map([...keys.map((k) => [k.name, k.type]), ...aggs.map((a) => [a.name, a.type])]);
|
|
5659
|
+
const having = spec.having === void 0 ? sql`` : sql` WHERE ${compileExprTyped(sql, spec.having, {
|
|
5660
|
+
table,
|
|
5661
|
+
alias: "r",
|
|
5662
|
+
outputs,
|
|
5663
|
+
fieldType: (n) => types.get(n)
|
|
5664
|
+
}).fragment}`;
|
|
5665
|
+
const col = new Map([...keys.map((k) => [k.name, u(`x.k${k.i}`)]), ...aggs.map((a) => [a.name, a.typed])]);
|
|
5666
|
+
const order = [...(spec.orderBy ?? []).map((c) => Object.entries(c)[0]), ...keys.map((k) => [k.name, "asc"])].map(([name, dir]) => {
|
|
5667
|
+
const c = col.get(name);
|
|
5668
|
+
if (c === void 0) throw new Error(`internal: orderBy '${name}' names no output`);
|
|
5669
|
+
return sql`${types.get(name) === "string" ? sql`${c} COLLATE "C"` : c} ${u(dir === "asc" ? "ASC" : "DESC")} NULLS LAST`;
|
|
5670
|
+
});
|
|
5671
|
+
const project = (frag, type) => type === "date" ? sql`to_char((${frag}) AT TIME ZONE 'UTC', ${u(MICROS)})` : type === "int64" ? sql`(${frag})::text` : frag;
|
|
5672
|
+
const outs = [...keys.map((k) => ({
|
|
5673
|
+
name: k.name,
|
|
5674
|
+
type: k.type,
|
|
5675
|
+
frag: u(`x.k${k.i}`)
|
|
5676
|
+
})), ...aggs.map((a) => ({
|
|
5677
|
+
name: a.name,
|
|
5678
|
+
type: a.type,
|
|
5679
|
+
frag: a.typed
|
|
5680
|
+
}))];
|
|
5681
|
+
const limit = spec.limit === void 0 ? sql`` : sql` LIMIT ${spec.limit + 1}`;
|
|
5682
|
+
const offset = spec.offset === void 0 || spec.offset === 0 ? sql`` : sql` OFFSET ${spec.offset}`;
|
|
5683
|
+
return {
|
|
5684
|
+
statement: sql`SELECT 1 AS g, ${list([...outs.map((o, n) => sql`${project(o.frag, o.type)} AS ${u(`o${n}`)}`), ...raises ? [sql`x.w`] : []])} FROM (${inner}) x${having}${order.length === 0 ? sql`` : sql` ORDER BY ${list(order)}`}${limit}${offset}`,
|
|
5685
|
+
decode: (row) => Object.fromEntries(outs.map((o, n) => {
|
|
5686
|
+
const v = row[`o${n}`] ?? null;
|
|
5687
|
+
return [o.name, o.type === "int64" && v !== null ? BigInt(v) : v];
|
|
5688
|
+
}))
|
|
5689
|
+
};
|
|
5099
5690
|
}
|
|
5100
5691
|
|
|
5101
5692
|
//#endregion
|
|
5102
5693
|
//#region ../../packages/substrate-dataset-pg/src/promote.ts
|
|
5103
5694
|
/**
|
|
5695
|
+
* The promotion sweep (`tables` layout): each `pending` field gets its column,
|
|
5696
|
+
* and each `promoting` field's values move out of `extra` into it. One field
|
|
5697
|
+
* at a time, so DDL touches one type's table, under a short `lock_timeout`.
|
|
5698
|
+
*
|
|
5699
|
+
* Per field:
|
|
5700
|
+
* 1. `pending` only: ADD COLUMN, with the type lock's wait and the table
|
|
5701
|
+
* lock's both under `lock_timeout`. On a timeout the field is `busy` and
|
|
5702
|
+
* the sweep moves on; the next sweep retries it.
|
|
5703
|
+
* 2. `pending` only: record it `promoting`, and notify.
|
|
5704
|
+
* 3. Backfill, keyset-paginated by `id` (F14): each page moves the values of
|
|
5705
|
+
* its rows that carry the key, through the row codec, so a misfit lands in
|
|
5706
|
+
* `$raw` exactly as a write would put it; and a `$raw` value the column
|
|
5707
|
+
* now admits (its enum vocabulary widened) into the column. Those rows are locked in id order
|
|
5708
|
+
* first, as batch writers lock them; a deadlock or lock timeout is `busy`.
|
|
5709
|
+
* A row a writer changed between the page's read and its update is left
|
|
5710
|
+
* for the straggler pass.
|
|
5711
|
+
* 4. Reconcile the type's indexes, which builds any waiting on the column.
|
|
5712
|
+
* 5. Record it `column` (only from `promoting`, under the type lock, so never
|
|
5713
|
+
* over a boot that orphaned or redeclared it), bump the catalog version,
|
|
5714
|
+
* notify, and run one straggler pass (step 3 again), for values a writer
|
|
5715
|
+
* put in `extra` before the move. The move locks the table SHARE, retried
|
|
5716
|
+
* NOWAIT until `lock_timeout` so writes never queue behind it (held past
|
|
5717
|
+
* it, the field is `busy`; `noWriters` queues instead), so every such write has
|
|
5718
|
+
* committed before it, and any later one fails its version check and is
|
|
5719
|
+
* rebuilt against the new table (`guardCte`).
|
|
5720
|
+
*
|
|
5721
|
+
* A unique index refusing a backfilled value (23505), or one that cannot be
|
|
5722
|
+
* built over the moved values, fails the field and is never weakened to let
|
|
5723
|
+
* the move through. Before step 5 the field stays `promoting`, so reads still
|
|
5724
|
+
* see its `extra` values. A field goes back from `column` only when its enum
|
|
5725
|
+
* vocabulary widens (`ensureTypeTable`), so step 3 moves the `$raw` values the
|
|
5726
|
+
* vocabulary now admits; otherwise never: a straggler
|
|
5727
|
+
* that fails (or a straggler pass that is stopped) leaves the field `column`
|
|
5728
|
+
* with its error recorded, which brings it back to a later sweep's straggler
|
|
5729
|
+
* pass until that pass finishes. A field that failed on a unique conflict,
|
|
5730
|
+
* `promoting` or `column`, comes back only once the type's catalog version has
|
|
5731
|
+
* moved on (`error_version`), since a rescan of the whole table at the same
|
|
5732
|
+
* version only meets it again.
|
|
5733
|
+
*
|
|
5734
|
+
* Every server process sweeps, so each field is swept under a session
|
|
5735
|
+
* advisory lock taken with a try ({@link promotionLockKey}): a field another
|
|
5736
|
+
* sweep holds is skipped, not swept twice, and reported `busy` with
|
|
5737
|
+
* {@link HELD_ELSEWHERE}. The lock never waits, so it is in no wait cycle;
|
|
5738
|
+
* it is session-level, not transaction-level, because the
|
|
5739
|
+
* field's own index builds (CONCURRENTLY) would wait forever on an open
|
|
5740
|
+
* transaction of the sweep's.
|
|
5741
|
+
*/
|
|
5742
|
+
/**
|
|
5104
5743
|
* The error of a `busy` event for a field whose sweep lock another sweep holds:
|
|
5105
5744
|
* not an attempt, and never recorded, only reported.
|
|
5106
5745
|
*/
|
|
@@ -5258,7 +5897,7 @@ async function backfill(sql, table, field, opts) {
|
|
|
5258
5897
|
if (column === void 0) throw new Error(`internal: ${table.type}.${field} has no column`);
|
|
5259
5898
|
const from = sql.unsafe(table.from);
|
|
5260
5899
|
const col = sql.unsafe(ident(column.column));
|
|
5261
|
-
const cast = sql.unsafe(PG_TYPE[column.kind]);
|
|
5900
|
+
const cast = sql.unsafe(PG_TYPE$1[column.kind]);
|
|
5262
5901
|
let last = "";
|
|
5263
5902
|
for (;;) {
|
|
5264
5903
|
if (opts.signal?.aborted === true) return false;
|
|
@@ -5276,7 +5915,8 @@ async function backfill(sql, table, field, opts) {
|
|
|
5276
5915
|
for (const r of page) {
|
|
5277
5916
|
const stored = r.v ?? r.rv;
|
|
5278
5917
|
if (stored === null) continue;
|
|
5279
|
-
const
|
|
5918
|
+
const parsed = column.kind === "int64" && /^-?\d+$/.test(stored) && parseInt64(BigInt(stored)) !== void 0 ? BigInt(stored) : JSON.parse(stored);
|
|
5919
|
+
const encoded = encodeRow(table, { [field]: parsed });
|
|
5280
5920
|
const c = encoded.columns[column.column];
|
|
5281
5921
|
const misfit = Object.hasOwn(encoded.raw, field);
|
|
5282
5922
|
if (r.v === null) {
|
|
@@ -5289,7 +5929,7 @@ async function backfill(sql, table, field, opts) {
|
|
|
5289
5929
|
ids.push(r.id);
|
|
5290
5930
|
values.push(r.v);
|
|
5291
5931
|
columns.push(c === null || c === void 0 ? null : String(c));
|
|
5292
|
-
raws.push(misfit ?
|
|
5932
|
+
raws.push(misfit ? stored : null);
|
|
5293
5933
|
}
|
|
5294
5934
|
if (ids.length + fitIds.length > 0) {
|
|
5295
5935
|
const locked = [...ids, ...fitIds];
|
|
@@ -5601,7 +6241,7 @@ var PgDataset = class PgDataset {
|
|
|
5601
6241
|
if (!usesWalkGrammar(params)) try {
|
|
5602
6242
|
return await this.#read(type, params, false);
|
|
5603
6243
|
} catch (err) {
|
|
5604
|
-
throw this.#queryTimeout(type, err, params.signal) ?? err;
|
|
6244
|
+
throw this.#queryTimeout(type, err, params.signal) ?? storeBusy(err) ?? err;
|
|
5605
6245
|
}
|
|
5606
6246
|
if (!this.#inTx) return this.withTransaction((tx) => tx.read(type, params));
|
|
5607
6247
|
const ms = String(this.#statementTimeoutMs);
|
|
@@ -5612,7 +6252,7 @@ var PgDataset = class PgDataset {
|
|
|
5612
6252
|
WHERE name = 'statement_timeout' AND (setting::bigint = 0 OR setting::bigint > ${ms}::bigint)`, params.signal)).length > 0;
|
|
5613
6253
|
return await this.#read(type, params, true);
|
|
5614
6254
|
} catch (err) {
|
|
5615
|
-
throw this.#queryTimeout(type, err, params.signal, lowered ? this.#statementTimeoutMs : void 0) ?? err;
|
|
6255
|
+
throw this.#queryTimeout(type, err, params.signal, lowered ? this.#statementTimeoutMs : void 0) ?? storeBusy(err) ?? err;
|
|
5616
6256
|
}
|
|
5617
6257
|
}
|
|
5618
6258
|
/**
|
|
@@ -5688,10 +6328,15 @@ var PgDataset = class PgDataset {
|
|
|
5688
6328
|
}));
|
|
5689
6329
|
/** `statement`'s rows, but those of a stale read, which throw {@link StaleRead}; `key` is non-null on a real row. */
|
|
5690
6330
|
const run = async (statement, key) => {
|
|
5691
|
-
|
|
5692
|
-
|
|
5693
|
-
|
|
5694
|
-
|
|
6331
|
+
try {
|
|
6332
|
+
if (guard === null) return await cancellable(statement, params.signal);
|
|
6333
|
+
const out = await cancellable(guardedRead(sql, guard, statement), params.signal);
|
|
6334
|
+
if (out[0]?.guard_ok !== true) throw new StaleRead(types, String(out[0]?.guard_iso));
|
|
6335
|
+
return out.filter((r) => r[key] !== null);
|
|
6336
|
+
} catch (err) {
|
|
6337
|
+
if (err?.code === "22003") throw new ResolverFrameError(VALUE_OUT_OF_RANGE, err.message);
|
|
6338
|
+
throw err;
|
|
6339
|
+
}
|
|
5695
6340
|
};
|
|
5696
6341
|
const clauses = [];
|
|
5697
6342
|
if (hasIdFilter) clauses.push(sql`r.id = ANY(${idList})`);
|
|
@@ -5742,55 +6387,94 @@ var PgDataset = class PgDataset {
|
|
|
5742
6387
|
};
|
|
5743
6388
|
}
|
|
5744
6389
|
const aggregate = params.aggregate ?? {};
|
|
5745
|
-
const
|
|
5746
|
-
|
|
5747
|
-
|
|
6390
|
+
const grouped = buildGroupedRead(sql, {
|
|
6391
|
+
...params,
|
|
6392
|
+
aggregate,
|
|
6393
|
+
limit
|
|
6394
|
+
}, matched, pickJoins, table, (f) => fieldDef(type, f));
|
|
6395
|
+
const rows = await run(sql`${withCtes(sql, scope)}${grouped.statement}`, "g");
|
|
6396
|
+
const truncated = limit !== void 0 && rows.length > limit;
|
|
5748
6397
|
return {
|
|
5749
6398
|
rows: [],
|
|
5750
6399
|
hasMore: false,
|
|
5751
|
-
groups: truncated ?
|
|
6400
|
+
groups: (truncated ? rows.slice(0, limit) : rows).map(grouped.decode),
|
|
5752
6401
|
...truncated ? { truncated: true } : {}
|
|
5753
6402
|
};
|
|
5754
6403
|
}
|
|
5755
6404
|
const orderBy = normalizeOrderBy(params.orderBy);
|
|
5756
6405
|
const orderByCastFor = bodyCastFor;
|
|
5757
6406
|
const joinShortIds = whereReferencesField(params.where, "shortId") || orderBy.some(([f]) => f === "shortId");
|
|
5758
|
-
const
|
|
5759
|
-
const
|
|
5760
|
-
|
|
5761
|
-
|
|
5762
|
-
const
|
|
5763
|
-
|
|
6407
|
+
const selectEntries = Object.entries(params.select ?? {});
|
|
6408
|
+
const selectFragments = /* @__PURE__ */ new Map();
|
|
6409
|
+
const selectTypes = /* @__PURE__ */ new Map();
|
|
6410
|
+
if (selectEntries.length > 0) {
|
|
6411
|
+
const used = JSON.stringify(params.select);
|
|
6412
|
+
const outputs = /* @__PURE__ */ new Map();
|
|
6413
|
+
const aggTypes = /* @__PURE__ */ new Map();
|
|
6414
|
+
for (const [name, spec] of Object.entries(frameAggs ?? {})) {
|
|
6415
|
+
const t = walkAggExprType(spec, type, fieldDef);
|
|
6416
|
+
if (!used.includes(`"_agg.${name}"`) || t === void 0 || scope?.aggValue === void 0) continue;
|
|
6417
|
+
const v = scope.aggValue(name);
|
|
6418
|
+
outputs.set(`_agg.${name}`, t === "number" ? sql`substrate_num(${v})::float8` : t === "date" ? sql`substrate_ts(${v})` : sql`(${v}) #>> '{}'`);
|
|
6419
|
+
aggTypes.set(`_agg.${name}`, t);
|
|
6420
|
+
}
|
|
6421
|
+
const fieldType = (name) => aggTypes.get(name) ?? exprTypeOfField(fieldDef(type, name));
|
|
6422
|
+
for (const [name, expr] of selectEntries) {
|
|
6423
|
+
const { fragment, type: t } = compileExprTyped(sql, expr, {
|
|
6424
|
+
table,
|
|
6425
|
+
alias: "r",
|
|
6426
|
+
outputs,
|
|
6427
|
+
fieldType
|
|
6428
|
+
});
|
|
6429
|
+
selectFragments.set(name, fragment);
|
|
6430
|
+
selectTypes.set(name, t ?? "string");
|
|
6431
|
+
}
|
|
6432
|
+
}
|
|
6433
|
+
const computedExpr = (field) => {
|
|
6434
|
+
const chosen = Object.hasOwn(params.select ?? {}, field) ? selectFragments.get(field) : void 0;
|
|
6435
|
+
if (chosen !== void 0) return selectTypes.get(field) === "string" ? sql`((${chosen}) COLLATE "C")` : chosen;
|
|
5764
6436
|
};
|
|
5765
6437
|
const lead = orderBy[0]?.[0] ?? "id";
|
|
5766
|
-
const fenced = joinShortIds ? void 0 :
|
|
5767
|
-
const
|
|
6438
|
+
const fenced = joinShortIds ? void 0 : computedExpr(lead);
|
|
6439
|
+
const computedFor = Object.assign((field) => fenced !== void 0 && field === lead ? sql`r.__computed_0` : computedExpr(field), { typeOf: (field) => selectTypes.get(field) });
|
|
5768
6440
|
const rel = sql.unsafe(table.from);
|
|
5769
|
-
const from = fenced === void 0 ? sql`${rel} r${pickJoins}${windowJoins}` : sql`(SELECT r.*, ${fenced} AS
|
|
6441
|
+
const from = fenced === void 0 ? sql`${rel} r${pickJoins}${windowJoins}` : sql`(SELECT r.*, ${fenced} AS __computed_0 FROM ${rel} r${pickJoins} WHERE ${combineAnd(sql, clauses)} OFFSET 0) r${windowJoins}`;
|
|
5770
6442
|
const ctes = withCtes(sql, scope);
|
|
5771
6443
|
const base = fenced === void 0 ? clauses : [];
|
|
5772
6444
|
let whereSql = combineAnd(sql, base);
|
|
5773
6445
|
let tailSql = null;
|
|
5774
6446
|
if (params.cursor !== void 0 && params.cursor !== "") {
|
|
5775
6447
|
const cursor = decodeCursor(params.cursor, orderBy);
|
|
5776
|
-
const cursorFragment = buildCursorCompare(sql, orderBy, cursor.orderByValues, cursor.id, orderByCastFor, table,
|
|
5777
|
-
const tail = buildCursorNullTail(sql, orderBy, cursor.orderByValues, orderByCastFor, table,
|
|
6448
|
+
const cursorFragment = buildCursorCompare(sql, orderBy, cursor.orderByValues, cursor.id, orderByCastFor, table, computedFor);
|
|
6449
|
+
const tail = buildCursorNullTail(sql, orderBy, cursor.orderByValues, orderByCastFor, table, computedFor);
|
|
5778
6450
|
if (tail !== null) tailSql = combineAnd(sql, [...base, tail]);
|
|
5779
6451
|
if (cursorFragment !== null) whereSql = combineAnd(sql, [...base, cursorFragment]);
|
|
5780
6452
|
}
|
|
5781
|
-
const orderBySql = buildOrderBy(sql, orderBy, orderByCastFor, table,
|
|
6453
|
+
const orderBySql = buildOrderBy(sql, orderBy, orderByCastFor, table, computedFor, "r");
|
|
5782
6454
|
const fetchLimit = limit !== void 0 ? limit + 1 : void 0;
|
|
5783
|
-
const
|
|
5784
|
-
const rowProjection = buildRowProjection(sql, table, params.fields, "r",
|
|
5785
|
-
const { fragment: sortKeyProjection, aliases: sortKeyAliases } = buildSortKeyProjection(sql, orderBy, bodyCastFor, table,
|
|
6455
|
+
const walk = scope === void 0 ? null : buildWalkColumns(sql, frameAggs, params.nested ?? {}, table, scope);
|
|
6456
|
+
const rowProjection = buildRowProjection(sql, table, params.fields, "r", walk?.select ?? null);
|
|
6457
|
+
const { fragment: sortKeyProjection, aliases: sortKeyAliases } = buildSortKeyProjection(sql, orderBy, bodyCastFor, table, computedFor);
|
|
5786
6458
|
const owner = sql.unsafe(`${sqlStringLiteral(this.namespace)}::text AS namespace, ${sqlStringLiteral(type)}::text AS type`);
|
|
5787
6459
|
const ownerOn = sql.unsafe(`s.namespace = ${sqlStringLiteral(this.namespace)} AND s.type = ${sqlStringLiteral(type)}`);
|
|
5788
6460
|
const inlineLimit = (n) => n !== void 0 ? sql.unsafe(`LIMIT ${BigInt(n)}`) : sql``;
|
|
5789
|
-
const
|
|
5790
|
-
|
|
5791
|
-
|
|
6461
|
+
const selectColumns = selectEntries.map(([name], i) => {
|
|
6462
|
+
const fragment = fenced !== void 0 && name === lead ? sql`r.__computed_0` : selectFragments.get(name);
|
|
6463
|
+
const alias = sql(`__sel_${i}`);
|
|
6464
|
+
switch (selectTypes.get(name)) {
|
|
6465
|
+
case "date": return sql`, to_char((${fragment}) AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS ${alias}`;
|
|
6466
|
+
case "int64": return sql`, (${fragment})::text AS ${alias}`;
|
|
6467
|
+
default: return sql`, ${fragment} AS ${alias}`;
|
|
6468
|
+
}
|
|
6469
|
+
});
|
|
6470
|
+
const selectProjection = selectColumns.length === 0 ? sql`` : selectColumns;
|
|
6471
|
+
const withMeta = readsRowMetadata(params.fields);
|
|
6472
|
+
const withShortId = readsShortId(params.fields);
|
|
6473
|
+
const metaColumns = withMeta ? sql`, r.created_at, r.updated_at,
|
|
5792
6474
|
r.created_by_kind, r.created_by_id,
|
|
5793
|
-
r.updated_by_kind, r.updated_by_id, r.seq
|
|
6475
|
+
r.updated_by_kind, r.updated_by_id, r.seq` : sql``;
|
|
6476
|
+
const statement = (where, n) => joinShortIds ? sql`
|
|
6477
|
+
SELECT r.id, ${rowProjection}, r.deleted_at, s.short_id${metaColumns}${selectProjection}${sortKeyProjection}
|
|
5794
6478
|
FROM ${from}
|
|
5795
6479
|
LEFT JOIN short_ids s
|
|
5796
6480
|
ON ${ownerOn}
|
|
@@ -5799,24 +6483,21 @@ var PgDataset = class PgDataset {
|
|
|
5799
6483
|
${orderBySql}
|
|
5800
6484
|
${n !== void 0 ? sql`LIMIT ${n}` : sql``}
|
|
5801
6485
|
` : sql`
|
|
5802
|
-
SELECT p
|
|
6486
|
+
SELECT p.*${withShortId ? sql`, s.short_id` : sql``}
|
|
5803
6487
|
FROM (
|
|
5804
|
-
SELECT r.id, ${rowProjection}, r.deleted_at,
|
|
5805
|
-
|
|
5806
|
-
r.created_by_kind, r.created_by_id,
|
|
5807
|
-
r.updated_by_kind, r.updated_by_id, r.seq,
|
|
5808
|
-
${owner}${sortKeyProjection}
|
|
6488
|
+
SELECT r.id, ${rowProjection}, r.deleted_at${metaColumns},
|
|
6489
|
+
${owner}${selectProjection}${sortKeyProjection}
|
|
5809
6490
|
FROM ${from}
|
|
5810
6491
|
WHERE ${where}
|
|
5811
6492
|
${orderBySql}
|
|
5812
6493
|
${inlineLimit(n)}
|
|
5813
6494
|
) p
|
|
5814
|
-
LEFT JOIN LATERAL (
|
|
6495
|
+
${withShortId ? sql`LEFT JOIN LATERAL (
|
|
5815
6496
|
SELECT s.short_id FROM short_ids s
|
|
5816
6497
|
WHERE s.namespace = p.namespace AND s.type = p.type AND s.row_id = p.id
|
|
5817
6498
|
LIMIT 1
|
|
5818
|
-
) s ON true
|
|
5819
|
-
${buildProjectedOrderBy(sql, orderBy, sortKeyAliases, orderByCastFor, table, "p")}
|
|
6499
|
+
) s ON true` : sql``}
|
|
6500
|
+
${buildProjectedOrderBy(sql, orderBy, sortKeyAliases, orderByCastFor, table, "p", computedFor)}
|
|
5820
6501
|
`;
|
|
5821
6502
|
const page = (where, n) => scope?.ctes === void 0 || scope.ctes.length === 0 ? statement(where, n) : sql`${ctes}${statement(where, n)}`;
|
|
5822
6503
|
let rows = await run(page(whereSql, fetchLimit), "id");
|
|
@@ -5826,7 +6507,18 @@ var PgDataset = class PgDataset {
|
|
|
5826
6507
|
rows = rows.concat(fetchLimit === void 0 ? tail : tail.slice(0, fetchLimit - rows.length));
|
|
5827
6508
|
}
|
|
5828
6509
|
const fields = this.#typeDefLookup?.(type, this.namespace)?.fields;
|
|
5829
|
-
const
|
|
6510
|
+
const orders = /* @__PURE__ */ new Map();
|
|
6511
|
+
const materialised = new Array(rows.length);
|
|
6512
|
+
for (let i = 0; i < rows.length; i++) {
|
|
6513
|
+
if (i > 0 && i % MATERIALIZE_BATCH_ROWS === 0) await new Promise((resolve) => setImmediate(resolve));
|
|
6514
|
+
const r = rows[i];
|
|
6515
|
+
const row = this.materialize(walk === null ? r : mergeWalkColumns(r, walk.columns, orders), fields, table, params.fields);
|
|
6516
|
+
for (const [k, [name]] of selectEntries.entries()) {
|
|
6517
|
+
const v = r[`__sel_${k}`];
|
|
6518
|
+
row[name] = v === null || v === void 0 ? null : selectTypes.get(name) === "int64" ? BigInt(v) : v;
|
|
6519
|
+
}
|
|
6520
|
+
materialised[i] = row;
|
|
6521
|
+
}
|
|
5830
6522
|
let hasMore = false;
|
|
5831
6523
|
let sliced = materialised;
|
|
5832
6524
|
if (limit !== void 0 && materialised.length > limit) {
|
|
@@ -6136,7 +6828,7 @@ var PgDataset = class PgDataset {
|
|
|
6136
6828
|
if (this.#inTx) return fn(this);
|
|
6137
6829
|
const rootSql = this.#sql;
|
|
6138
6830
|
const txTables = /* @__PURE__ */ new Map();
|
|
6139
|
-
const out = await rootSql.begin(async (txSql) => {
|
|
6831
|
+
const out = await busyAware(rootSql.begin(async (txSql) => {
|
|
6140
6832
|
const result = await fn(new PgDataset({
|
|
6141
6833
|
namespace: this.namespace,
|
|
6142
6834
|
sql: txSql,
|
|
@@ -6151,7 +6843,7 @@ var PgDataset = class PgDataset {
|
|
|
6151
6843
|
}));
|
|
6152
6844
|
await snapshotUnlessReadCommitted(txSql, txTables);
|
|
6153
6845
|
return result;
|
|
6154
|
-
});
|
|
6846
|
+
}));
|
|
6155
6847
|
this.#commitTables(txTables);
|
|
6156
6848
|
return out;
|
|
6157
6849
|
}
|
|
@@ -6186,11 +6878,12 @@ var PgDataset = class PgDataset {
|
|
|
6186
6878
|
};
|
|
6187
6879
|
};
|
|
6188
6880
|
if (this.#inTx) return fn(buildHandles(this.#sql));
|
|
6189
|
-
const
|
|
6881
|
+
const rootSql = this.#sql;
|
|
6882
|
+
const out = await busyAware(rootSql.begin(async (txSql) => {
|
|
6190
6883
|
const result = await fn(buildHandles(txSql));
|
|
6191
6884
|
await snapshotUnlessReadCommitted(txSql, txTables);
|
|
6192
6885
|
return result;
|
|
6193
|
-
});
|
|
6886
|
+
}));
|
|
6194
6887
|
this.#commitTables(txTables);
|
|
6195
6888
|
return out;
|
|
6196
6889
|
}
|
|
@@ -6533,6 +7226,17 @@ var PgDataset = class PgDataset {
|
|
|
6533
7226
|
}
|
|
6534
7227
|
});
|
|
6535
7228
|
}
|
|
7229
|
+
/** See `Dataset.poolStats`. Read on the root dataset; outside a transaction `#sql` is the pool. */
|
|
7230
|
+
poolStats() {
|
|
7231
|
+
const { connections, opening, idle, leased, waiting } = this.#sql.stats;
|
|
7232
|
+
return {
|
|
7233
|
+
connections,
|
|
7234
|
+
opening,
|
|
7235
|
+
idle,
|
|
7236
|
+
leased,
|
|
7237
|
+
waiting
|
|
7238
|
+
};
|
|
7239
|
+
}
|
|
6536
7240
|
/**
|
|
6537
7241
|
* See `Dataset.fieldWarnings`. Store-wide; none before the store is migrated. Counts
|
|
6538
7242
|
* only, since `/health` shows it unauthenticated: the named lines are in
|
|
@@ -6852,28 +7556,32 @@ var PgDataset = class PgDataset {
|
|
|
6852
7556
|
materialize(record, fields, table, requested) {
|
|
6853
7557
|
const body = decodeRow(table, record);
|
|
6854
7558
|
const raw = record.extra?.[RAW_KEY];
|
|
6855
|
-
|
|
6856
|
-
|
|
6857
|
-
|
|
6858
|
-
|
|
6859
|
-
|
|
6860
|
-
if (k ===
|
|
6861
|
-
if (k === RESERVED_DELETED_AT) continue;
|
|
6862
|
-
if (k === "_metadata") continue;
|
|
6863
|
-
if (v === null && fields !== void 0 && Object.hasOwn(fields, k)) {
|
|
7559
|
+
const trimRaw = requested !== void 0 && raw !== null && typeof raw === "object" ? raw : void 0;
|
|
7560
|
+
const id = this.extractOriginalId(record.id, body);
|
|
7561
|
+
const kept = (k) => {
|
|
7562
|
+
if (k === "id" || k === RESERVED_DELETED_AT || k === "_metadata") return false;
|
|
7563
|
+
if (trimRaw !== void 0 && Object.hasOwn(trimRaw, k) && !Object.hasOwn(requested, k)) return false;
|
|
7564
|
+
if (body[k] === null && fields !== void 0 && Object.hasOwn(fields, k)) {
|
|
6864
7565
|
const def = fields[k];
|
|
6865
|
-
if (def !== void 0 && isColumnField(def))
|
|
7566
|
+
if (def !== void 0 && isColumnField(def)) return false;
|
|
6866
7567
|
}
|
|
6867
|
-
|
|
7568
|
+
return true;
|
|
7569
|
+
};
|
|
7570
|
+
const deletedAt = record.deleted_at !== null ? record.deleted_at : void 0;
|
|
7571
|
+
const shortId = record.short_id !== null && record.short_id !== void 0 ? Number(record.short_id) : void 0;
|
|
7572
|
+
const meta = readsRowMetadata(requested) ? assembleRowMetadata(record) : void 0;
|
|
7573
|
+
const special = (k) => k === RESERVED_DELETED_AT ? deletedAt : k === "shortId" ? shortId : k === "_metadata" ? meta : void 0;
|
|
7574
|
+
const out = requested === void 0 || !Object.hasOwn(requested, "id") ? { id } : {};
|
|
7575
|
+
if (requested !== void 0) {
|
|
7576
|
+
for (const k of Object.keys(requested)) if (k === "id") out.id = id;
|
|
7577
|
+
else if (special(k) !== void 0) out[k] = special(k);
|
|
7578
|
+
else if (Object.hasOwn(body, k) && kept(k)) out[k] = body[k];
|
|
6868
7579
|
}
|
|
6869
|
-
if (
|
|
6870
|
-
if (
|
|
6871
|
-
|
|
6872
|
-
if (meta !== void 0) out._metadata = meta;
|
|
6873
|
-
|
|
6874
|
-
const ordered = Object.hasOwn(requested, "id") ? {} : { id: out.id };
|
|
6875
|
-
for (const k of Object.keys(requested)) if (Object.hasOwn(out, k)) ordered[k] = out[k];
|
|
6876
|
-
return Object.assign(ordered, out);
|
|
7580
|
+
for (const k in body) if (!Object.hasOwn(out, k) && kept(k)) out[k] = special(k) ?? body[k];
|
|
7581
|
+
if (deletedAt !== void 0 && !Object.hasOwn(out, RESERVED_DELETED_AT)) out[RESERVED_DELETED_AT] = deletedAt;
|
|
7582
|
+
if (shortId !== void 0 && !Object.hasOwn(out, "shortId")) out.shortId = shortId;
|
|
7583
|
+
if (meta !== void 0 && !Object.hasOwn(out, "_metadata")) out._metadata = meta;
|
|
7584
|
+
return out;
|
|
6877
7585
|
}
|
|
6878
7586
|
extractOriginalId(textId, body) {
|
|
6879
7587
|
const bodyId = body.id;
|
|
@@ -6881,16 +7589,17 @@ var PgDataset = class PgDataset {
|
|
|
6881
7589
|
return textId;
|
|
6882
7590
|
}
|
|
6883
7591
|
};
|
|
7592
|
+
/** Rows materialised between event-loop yields on a read (a few ms of work). */
|
|
7593
|
+
const MATERIALIZE_BATCH_ROWS = 5e3;
|
|
6884
7594
|
/** The statement timeout a walk read runs under, in milliseconds, unless a shorter one is set. */
|
|
6885
7595
|
const WALK_STATEMENT_TIMEOUT_MS = 3e4;
|
|
6886
|
-
/** The
|
|
7596
|
+
/** The params that run under the statement timeout: `count: true`, and each non-empty object. */
|
|
6887
7597
|
function walkParams(params) {
|
|
6888
7598
|
const used = [
|
|
6889
7599
|
"aggregate",
|
|
6890
|
-
"bucket",
|
|
6891
|
-
"derive",
|
|
6892
7600
|
"agg",
|
|
6893
|
-
"nested"
|
|
7601
|
+
"nested",
|
|
7602
|
+
"select"
|
|
6894
7603
|
].filter((k) => {
|
|
6895
7604
|
const v = params[k];
|
|
6896
7605
|
return typeof v === "object" && v !== null && Object.keys(v).length > 0;
|
|
@@ -6901,22 +7610,98 @@ function walkParams(params) {
|
|
|
6901
7610
|
function usesWalkGrammar(params) {
|
|
6902
7611
|
return whereUsesWalks(params.where) || walkParams(params).length > 0;
|
|
6903
7612
|
}
|
|
7613
|
+
/**
|
|
7614
|
+
* The driver's `QUEUE_TIMEOUT` (no pooled connection came free within
|
|
7615
|
+
* `queue_timeout`) as `store_busy`, else null. The request never reached
|
|
7616
|
+
* Postgres, so a retry is safe.
|
|
7617
|
+
*/
|
|
7618
|
+
function storeBusy(err) {
|
|
7619
|
+
if (err?.code !== "QUEUE_TIMEOUT") return null;
|
|
7620
|
+
return new ResolverFrameError(STORE_BUSY, "the store is busy: no database connection came free in time; retry shortly");
|
|
7621
|
+
}
|
|
7622
|
+
/** Awaits `p`, rethrowing a `QUEUE_TIMEOUT` as `store_busy`. */
|
|
7623
|
+
async function busyAware(p) {
|
|
7624
|
+
try {
|
|
7625
|
+
return await p;
|
|
7626
|
+
} catch (err) {
|
|
7627
|
+
throw storeBusy(err) ?? err;
|
|
7628
|
+
}
|
|
7629
|
+
}
|
|
6904
7630
|
/** Awaits `query`, cancelling the statement in flight when `signal` aborts. */
|
|
6905
7631
|
async function cancellable(query, signal) {
|
|
6906
|
-
const cancel = () => {
|
|
6907
|
-
Promise.resolve(query.cancel()).catch(() => void 0);
|
|
6908
|
-
};
|
|
6909
|
-
signal?.addEventListener("abort", cancel, { once: true });
|
|
6910
7632
|
try {
|
|
6911
|
-
|
|
6912
|
-
return await query;
|
|
7633
|
+
return await query.signal(signal);
|
|
6913
7634
|
} catch (err) {
|
|
6914
7635
|
if (signal?.aborted === true) signal.throwIfAborted();
|
|
6915
7636
|
throw err;
|
|
6916
|
-
} finally {
|
|
6917
|
-
signal?.removeEventListener("abort", cancel);
|
|
6918
7637
|
}
|
|
6919
7638
|
}
|
|
6920
7639
|
|
|
6921
7640
|
//#endregion
|
|
6922
|
-
|
|
7641
|
+
//#region ../../packages/substrate-dataset-pg/src/pg-idempotency-store.ts
|
|
7642
|
+
/**
|
|
7643
|
+
* PgIdempotencyStore — PostgreSQL-backed `IdempotencyStore`. Spec: §5.13.
|
|
7644
|
+
*
|
|
7645
|
+
* Rows live in `idempotency` keyed by the engine's frame-scoped dedup
|
|
7646
|
+
* key; envelopes are stored as JSONB.
|
|
7647
|
+
*
|
|
7648
|
+
* Semantics:
|
|
7649
|
+
* - `get` returns `undefined` for unknown or expired keys. Expiry is lazy.
|
|
7650
|
+
* - `put` is first-write-wins for the envelope; a conflicting put only
|
|
7651
|
+
* extends `expires_at` (never shortens it).
|
|
7652
|
+
* - SQL errors propagate: a swallowed put failure would let the next retry
|
|
7653
|
+
* re-run the mutate.
|
|
7654
|
+
*/
|
|
7655
|
+
var PgIdempotencyStore = class {
|
|
7656
|
+
#sql;
|
|
7657
|
+
constructor(options) {
|
|
7658
|
+
this.#sql = options.sql;
|
|
7659
|
+
}
|
|
7660
|
+
async get(key) {
|
|
7661
|
+
const row = (await this.#sql`
|
|
7662
|
+
SELECT envelope
|
|
7663
|
+
FROM idempotency
|
|
7664
|
+
WHERE key = ${key}
|
|
7665
|
+
AND expires_at > now()
|
|
7666
|
+
LIMIT 1
|
|
7667
|
+
`)[0];
|
|
7668
|
+
if (row === void 0) return void 0;
|
|
7669
|
+
return row.envelope;
|
|
7670
|
+
}
|
|
7671
|
+
async put(key, envelope, ttlMs) {
|
|
7672
|
+
await this.#sql`
|
|
7673
|
+
INSERT INTO idempotency (key, envelope, expires_at)
|
|
7674
|
+
VALUES (
|
|
7675
|
+
${key},
|
|
7676
|
+
${stringifyJson(envelope)}::text::jsonb,
|
|
7677
|
+
now() + (${ttlMs}::bigint || ' milliseconds')::interval
|
|
7678
|
+
)
|
|
7679
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
7680
|
+
-- The envelope is IMMUTABLE while the entry is live: an idempotency key
|
|
7681
|
+
-- must resolve to one stable answer for its whole window, or two
|
|
7682
|
+
-- retries of the same request can get different results, which is the
|
|
7683
|
+
-- thing the store exists to prevent.
|
|
7684
|
+
--
|
|
7685
|
+
-- Once it has EXPIRED that rule inverts. The dedupe window closed, the
|
|
7686
|
+
-- mutation legitimately re-executed, and the new envelope is the real
|
|
7687
|
+
-- result. Keeping the old one would hand back a stale answer wrapped in
|
|
7688
|
+
-- a fresh TTL, to a caller who was never deduped at all.
|
|
7689
|
+
envelope = CASE
|
|
7690
|
+
WHEN idempotency.expires_at <= now() THEN EXCLUDED.envelope
|
|
7691
|
+
ELSE idempotency.envelope
|
|
7692
|
+
END,
|
|
7693
|
+
-- GREATEST rather than a WHERE guard: extends a live window, never
|
|
7694
|
+
-- shortens it, and takes the new value outright when the old is past.
|
|
7695
|
+
expires_at = GREATEST(idempotency.expires_at, EXCLUDED.expires_at)
|
|
7696
|
+
`;
|
|
7697
|
+
}
|
|
7698
|
+
async reapExpired() {
|
|
7699
|
+
return { deleted: (await this.#sql`
|
|
7700
|
+
DELETE FROM idempotency
|
|
7701
|
+
WHERE expires_at <= now()
|
|
7702
|
+
`).count };
|
|
7703
|
+
}
|
|
7704
|
+
};
|
|
7705
|
+
|
|
7706
|
+
//#endregion
|
|
7707
|
+
export { copyRows as C, detectLayout as S, tryWithPgAdvisoryLock as T, pendingMigrations as _, ensureTypeTable as a, UUID_CHECK_MIGRATION as b, typeTableRef as c, PLAIN_SCHEMA_NAME as d, assertNotMigrating as f, isDerivedMigration as g, deriveListIndexMigration as h, seqName as i, whileNoTypeTable as l, assertTablesLayout as m, PgDataset as n, guardedRefRead as o, assertStoreSchema as p, txScopeIdFor as r, loadTypeTable as s, PgIdempotencyStore as t, MIGRATE_STORE_MARKER_TABLE as u, runDerivedMigrations as v, parseSourceBody as w, UUID_REGEX as x, runMigrations as y };
|