@crvouga/mockingbird-service-postgres 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -1
- package/COMPATIBILITY.md +3 -1
- package/compat/coverage.json +1 -1
- package/compat/sections/cat.ts +1 -0
- package/dist/ast/nodes.d.ts +11 -2
- package/dist/executor/ddl.d.ts +2 -1
- package/dist/executor/relation.d.ts +5 -0
- package/dist/functions/pgcrypto.d.ts +9 -0
- package/dist/functions/pgtrgm.d.ts +24 -0
- package/dist/index.js +1011 -20
- package/dist/index.js.map +4 -4
- package/dist/unstable.js +1011 -20
- package/dist/unstable.js.map +4 -4
- package/dist/wire/cli.js +1018 -27
- package/dist/wire/cli.js.map +4 -4
- package/dist/wire/index.js +1018 -27
- package/dist/wire/index.js.map +4 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4196,7 +4196,13 @@ var RowScope = class {
|
|
|
4196
4196
|
}
|
|
4197
4197
|
};
|
|
4198
4198
|
function childEnv(env, outer) {
|
|
4199
|
-
return {
|
|
4199
|
+
return {
|
|
4200
|
+
ctx: env.ctx,
|
|
4201
|
+
params: env.params,
|
|
4202
|
+
ctes: env.ctes,
|
|
4203
|
+
outer,
|
|
4204
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
4205
|
+
};
|
|
4200
4206
|
}
|
|
4201
4207
|
function relationResult(rel2, command) {
|
|
4202
4208
|
const visible = rel2.columns.map((c, i) => ({ i, c })).filter(({ c }) => !c.hidden);
|
|
@@ -8536,6 +8542,234 @@ function callScalarFunction(ctx, name, args) {
|
|
|
8536
8542
|
return fn(ctx, args);
|
|
8537
8543
|
}
|
|
8538
8544
|
|
|
8545
|
+
// src/functions/pgtrgm.ts
|
|
8546
|
+
var PGTRGM_BODY = "pg_trgm";
|
|
8547
|
+
var WORD_SIMILARITY_THRESHOLD = Math.fround(0.6);
|
|
8548
|
+
var GIN_TRGM_TYPES = /* @__PURE__ */ new Set(["text", "varchar"]);
|
|
8549
|
+
var WORD_CHAR = /[\p{L}\p{N}]+/gu;
|
|
8550
|
+
function pgTrgmSchema(state) {
|
|
8551
|
+
for (const schema of state.schemas.values()) {
|
|
8552
|
+
for (const fns of schema.functions.values()) {
|
|
8553
|
+
if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGTRGM_BODY)) return schema.name;
|
|
8554
|
+
}
|
|
8555
|
+
}
|
|
8556
|
+
return null;
|
|
8557
|
+
}
|
|
8558
|
+
function pgTrgmInstalled(state) {
|
|
8559
|
+
return pgTrgmSchema(state) !== null;
|
|
8560
|
+
}
|
|
8561
|
+
function installPgTrgm(state, schemaName) {
|
|
8562
|
+
const schema = state.getSchema(schemaName);
|
|
8563
|
+
const existing = schema.functions.get("similarity") ?? [];
|
|
8564
|
+
const fn = {
|
|
8565
|
+
name: "similarity",
|
|
8566
|
+
schema: schema.name,
|
|
8567
|
+
argNames: [null, null],
|
|
8568
|
+
argTypes: ["text", "text"],
|
|
8569
|
+
argDefaults: [null, null],
|
|
8570
|
+
returns: "float4",
|
|
8571
|
+
returnsSet: false,
|
|
8572
|
+
returnsTable: null,
|
|
8573
|
+
language: "internal",
|
|
8574
|
+
body: null,
|
|
8575
|
+
rawBody: PGTRGM_BODY,
|
|
8576
|
+
strict: true,
|
|
8577
|
+
oid: state.nextOid()
|
|
8578
|
+
};
|
|
8579
|
+
existing.push(fn);
|
|
8580
|
+
schema.functions.set("similarity", existing);
|
|
8581
|
+
}
|
|
8582
|
+
function uninstallPgTrgm(state) {
|
|
8583
|
+
let removed = false;
|
|
8584
|
+
for (const schema of state.schemas.values()) {
|
|
8585
|
+
for (const [name, fns] of [...schema.functions.entries()]) {
|
|
8586
|
+
const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGTRGM_BODY);
|
|
8587
|
+
if (keep.length === fns.length) continue;
|
|
8588
|
+
removed = true;
|
|
8589
|
+
if (keep.length === 0) schema.functions.delete(name);
|
|
8590
|
+
else schema.functions.set(name, keep);
|
|
8591
|
+
}
|
|
8592
|
+
}
|
|
8593
|
+
return removed;
|
|
8594
|
+
}
|
|
8595
|
+
function evalPgTrgmFunction(ctx, fn, args) {
|
|
8596
|
+
const ret = fn.returns ?? "float4";
|
|
8597
|
+
if (fn.strict && args.some((arg) => arg.v === null)) return tv(ret, null);
|
|
8598
|
+
if (fn.name !== "similarity" || args.length !== 2) {
|
|
8599
|
+
throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
|
|
8600
|
+
}
|
|
8601
|
+
const left = castTo(ctx, args[0], "text");
|
|
8602
|
+
const right = castTo(ctx, args[1], "text");
|
|
8603
|
+
if (left.v === null || right.v === null) return tv("float4", null);
|
|
8604
|
+
return tv("float4", trigramSimilarity(left.v, right.v));
|
|
8605
|
+
}
|
|
8606
|
+
function evalTrgmWordSimilar(ctx, left, right) {
|
|
8607
|
+
const missing = () => {
|
|
8608
|
+
throw pgError(
|
|
8609
|
+
"undefined_function",
|
|
8610
|
+
`operator does not exist: ${typeDisplayName(left.t)} <% ${typeDisplayName(right.t)}`
|
|
8611
|
+
);
|
|
8612
|
+
};
|
|
8613
|
+
if (!pgTrgmInstalled(ctx.state)) missing();
|
|
8614
|
+
if (!canImplicitCast(left.t, "text") || !canImplicitCast(right.t, "text")) missing();
|
|
8615
|
+
const a = castTo(ctx, left, "text");
|
|
8616
|
+
const b = castTo(ctx, right, "text");
|
|
8617
|
+
if (a.v === null || b.v === null) return tv("bool", null);
|
|
8618
|
+
const score = wordSimilarity(a.v, b.v);
|
|
8619
|
+
return tv("bool", score >= WORD_SIMILARITY_THRESHOLD);
|
|
8620
|
+
}
|
|
8621
|
+
function assertGinTrgmIndex(state, table, stmt) {
|
|
8622
|
+
const trgmCols = stmt.columns.filter((col2) => isGinTrgmOpclass(col2.opclass));
|
|
8623
|
+
if (trgmCols.length === 0) return;
|
|
8624
|
+
if (trgmCols.length !== stmt.columns.length || stmt.columns.length !== 1) {
|
|
8625
|
+
throw unsupported("multicolumn gin_trgm_ops");
|
|
8626
|
+
}
|
|
8627
|
+
const method = stmt.using ?? "btree";
|
|
8628
|
+
const col = trgmCols[0];
|
|
8629
|
+
const opclass = col.opclass;
|
|
8630
|
+
const label = opclass.join(".");
|
|
8631
|
+
if (method !== "gin" || !opclassVisible(state, opclass)) {
|
|
8632
|
+
throw pgError(
|
|
8633
|
+
"undefined_object",
|
|
8634
|
+
`operator class "${label}" does not exist for access method "${method}"`,
|
|
8635
|
+
"42704"
|
|
8636
|
+
);
|
|
8637
|
+
}
|
|
8638
|
+
if (stmt.unique) {
|
|
8639
|
+
throw pgError("feature_not_supported", `access method "gin" does not support unique indexes`, "0A000");
|
|
8640
|
+
}
|
|
8641
|
+
if (col.expr.type !== "colref" || col.expr.parts.length !== 1) {
|
|
8642
|
+
throw unsupported("expression gin_trgm_ops");
|
|
8643
|
+
}
|
|
8644
|
+
const index = table.columnIndex(col.expr.parts[0]);
|
|
8645
|
+
const typeId = table.columns[index].type.id;
|
|
8646
|
+
if (!GIN_TRGM_TYPES.has(typeId)) {
|
|
8647
|
+
throw pgError(
|
|
8648
|
+
"datatype_mismatch",
|
|
8649
|
+
`operator class "${label}" does not accept data type ${typeDisplayName(typeId)}`,
|
|
8650
|
+
"42804"
|
|
8651
|
+
);
|
|
8652
|
+
}
|
|
8653
|
+
}
|
|
8654
|
+
function isGinTrgmOpclass(opclass) {
|
|
8655
|
+
return opclass !== null && opclass[opclass.length - 1] === "gin_trgm_ops";
|
|
8656
|
+
}
|
|
8657
|
+
function opclassVisible(state, opclass) {
|
|
8658
|
+
const schema = pgTrgmSchema(state);
|
|
8659
|
+
if (schema === null || opclass[opclass.length - 1] !== "gin_trgm_ops") return false;
|
|
8660
|
+
if (opclass.length === 1) return state.effectiveSearchPath().includes(schema);
|
|
8661
|
+
if (opclass.length === 2) return opclass[0] === schema;
|
|
8662
|
+
return false;
|
|
8663
|
+
}
|
|
8664
|
+
function trigramSimilarity(left, right) {
|
|
8665
|
+
const a = uniqueSorted(orderedTrigrams(left));
|
|
8666
|
+
const b = uniqueSorted(orderedTrigrams(right));
|
|
8667
|
+
if (a.length === 0 || b.length === 0) return 0;
|
|
8668
|
+
let i = 0;
|
|
8669
|
+
let j = 0;
|
|
8670
|
+
let count = 0;
|
|
8671
|
+
while (i < a.length && j < b.length) {
|
|
8672
|
+
const cmp = a[i] < b[j] ? -1 : a[i] > b[j] ? 1 : 0;
|
|
8673
|
+
if (cmp < 0) i++;
|
|
8674
|
+
else if (cmp > 0) j++;
|
|
8675
|
+
else {
|
|
8676
|
+
count++;
|
|
8677
|
+
i++;
|
|
8678
|
+
j++;
|
|
8679
|
+
}
|
|
8680
|
+
}
|
|
8681
|
+
return calcSml(count, a.length, b.length);
|
|
8682
|
+
}
|
|
8683
|
+
function wordSimilarity(pattern, haystack) {
|
|
8684
|
+
const trg1 = orderedTrigrams(pattern);
|
|
8685
|
+
const trg2 = orderedTrigrams(haystack);
|
|
8686
|
+
const len1 = trg1.length;
|
|
8687
|
+
const len2 = trg2.length;
|
|
8688
|
+
if (len1 === 0 || len2 === 0) return 0;
|
|
8689
|
+
const positional = [];
|
|
8690
|
+
for (let i = 0; i < len1; i++) positional.push({ trg: trg1[i], index: -1 });
|
|
8691
|
+
for (let i = 0; i < len2; i++) positional.push({ trg: trg2[i], index: i });
|
|
8692
|
+
positional.sort((x, y) => x.trg < y.trg ? -1 : x.trg > y.trg ? 1 : x.index - y.index);
|
|
8693
|
+
const len = len1 + len2;
|
|
8694
|
+
const trg2indexes = new Array(len2);
|
|
8695
|
+
const found = new Array(len).fill(false);
|
|
8696
|
+
let ulen1 = 0;
|
|
8697
|
+
let group = 0;
|
|
8698
|
+
for (let i = 0; i < len; i++) {
|
|
8699
|
+
if (i > 0 && positional[i - 1].trg !== positional[i].trg) {
|
|
8700
|
+
if (found[group]) ulen1++;
|
|
8701
|
+
group++;
|
|
8702
|
+
}
|
|
8703
|
+
const index = positional[i].index;
|
|
8704
|
+
if (index >= 0) trg2indexes[index] = group;
|
|
8705
|
+
else found[group] = true;
|
|
8706
|
+
}
|
|
8707
|
+
if (found[group]) ulen1++;
|
|
8708
|
+
return iterateWordSimilarity(trg2indexes, found, ulen1, len2);
|
|
8709
|
+
}
|
|
8710
|
+
function orderedTrigrams(input) {
|
|
8711
|
+
const out = [];
|
|
8712
|
+
for (const match of input.matchAll(WORD_CHAR)) {
|
|
8713
|
+
const padded = ` ${match[0].toLowerCase()} `;
|
|
8714
|
+
for (let i = 0; i + 3 <= padded.length; i++) out.push(padded.slice(i, i + 3));
|
|
8715
|
+
}
|
|
8716
|
+
return out;
|
|
8717
|
+
}
|
|
8718
|
+
function uniqueSorted(trigrams) {
|
|
8719
|
+
return [...new Set(trigrams)].sort();
|
|
8720
|
+
}
|
|
8721
|
+
function calcSml(count, len1, len2) {
|
|
8722
|
+
const denom = len1 + len2 - count;
|
|
8723
|
+
if (denom <= 0) return 0;
|
|
8724
|
+
return Math.fround(count / denom);
|
|
8725
|
+
}
|
|
8726
|
+
function iterateWordSimilarity(trg2indexes, found, ulen1, len2) {
|
|
8727
|
+
const lastpos = new Array(found.length).fill(-1);
|
|
8728
|
+
let ulen2 = 0;
|
|
8729
|
+
let count = 0;
|
|
8730
|
+
let lower = -1;
|
|
8731
|
+
let smlrMax = 0;
|
|
8732
|
+
for (let i = 0; i < len2; i++) {
|
|
8733
|
+
const trgindex = trg2indexes[i];
|
|
8734
|
+
if (lower >= 0 || found[trgindex]) {
|
|
8735
|
+
if (lastpos[trgindex] < 0) {
|
|
8736
|
+
ulen2++;
|
|
8737
|
+
if (found[trgindex]) count++;
|
|
8738
|
+
}
|
|
8739
|
+
lastpos[trgindex] = i;
|
|
8740
|
+
}
|
|
8741
|
+
if (!found[trgindex]) continue;
|
|
8742
|
+
if (lower === -1) {
|
|
8743
|
+
lower = i;
|
|
8744
|
+
ulen2 = 1;
|
|
8745
|
+
}
|
|
8746
|
+
let smlrCur = calcSml(count, ulen1, ulen2);
|
|
8747
|
+
let tmpCount = count;
|
|
8748
|
+
let tmpUlen2 = ulen2;
|
|
8749
|
+
const prevLower = lower;
|
|
8750
|
+
for (let tmpLower = lower; tmpLower <= i; tmpLower++) {
|
|
8751
|
+
const smlrTmp = calcSml(tmpCount, ulen1, tmpUlen2);
|
|
8752
|
+
if (smlrTmp > smlrCur) {
|
|
8753
|
+
smlrCur = smlrTmp;
|
|
8754
|
+
ulen2 = tmpUlen2;
|
|
8755
|
+
lower = tmpLower;
|
|
8756
|
+
count = tmpCount;
|
|
8757
|
+
}
|
|
8758
|
+
const tmpTrgindex = trg2indexes[tmpLower];
|
|
8759
|
+
if (lastpos[tmpTrgindex] === tmpLower) {
|
|
8760
|
+
tmpUlen2--;
|
|
8761
|
+
if (found[tmpTrgindex]) tmpCount--;
|
|
8762
|
+
}
|
|
8763
|
+
}
|
|
8764
|
+
if (smlrCur > smlrMax) smlrMax = smlrCur;
|
|
8765
|
+
for (let tmpLower = prevLower; tmpLower < lower; tmpLower++) {
|
|
8766
|
+
const tmpTrgindex = trg2indexes[tmpLower];
|
|
8767
|
+
if (lastpos[tmpTrgindex] === tmpLower) lastpos[tmpTrgindex] = -1;
|
|
8768
|
+
}
|
|
8769
|
+
}
|
|
8770
|
+
return smlrMax;
|
|
8771
|
+
}
|
|
8772
|
+
|
|
8539
8773
|
// src/expressions/operators.ts
|
|
8540
8774
|
var NUM_LADDER = ["int2", "int4", "int8", "numeric", "float4", "float8"];
|
|
8541
8775
|
function opNotExist(op, l, r) {
|
|
@@ -8679,6 +8913,8 @@ function evalBinary(ctx, op, l0, r0) {
|
|
|
8679
8913
|
return evalJsonbDeletePath(ctx, l, r);
|
|
8680
8914
|
case "@@":
|
|
8681
8915
|
return evalTextSearchMatch(ctx, l, r);
|
|
8916
|
+
case "<%":
|
|
8917
|
+
return evalTrgmWordSimilar(ctx, l, r);
|
|
8682
8918
|
default:
|
|
8683
8919
|
opNotExist(op, l.t, r.t);
|
|
8684
8920
|
}
|
|
@@ -10899,6 +11135,634 @@ function unifyAggType(a, b) {
|
|
|
10899
11135
|
return unifyTypes(a, b) ?? a;
|
|
10900
11136
|
}
|
|
10901
11137
|
|
|
11138
|
+
// src/functions/pgcrypto.ts
|
|
11139
|
+
var PGCRYPTO_BODY = "pgcrypto";
|
|
11140
|
+
var utf82 = new TextEncoder();
|
|
11141
|
+
var MD5_S = [
|
|
11142
|
+
7,
|
|
11143
|
+
12,
|
|
11144
|
+
17,
|
|
11145
|
+
22,
|
|
11146
|
+
7,
|
|
11147
|
+
12,
|
|
11148
|
+
17,
|
|
11149
|
+
22,
|
|
11150
|
+
7,
|
|
11151
|
+
12,
|
|
11152
|
+
17,
|
|
11153
|
+
22,
|
|
11154
|
+
7,
|
|
11155
|
+
12,
|
|
11156
|
+
17,
|
|
11157
|
+
22,
|
|
11158
|
+
5,
|
|
11159
|
+
9,
|
|
11160
|
+
14,
|
|
11161
|
+
20,
|
|
11162
|
+
5,
|
|
11163
|
+
9,
|
|
11164
|
+
14,
|
|
11165
|
+
20,
|
|
11166
|
+
5,
|
|
11167
|
+
9,
|
|
11168
|
+
14,
|
|
11169
|
+
20,
|
|
11170
|
+
5,
|
|
11171
|
+
9,
|
|
11172
|
+
14,
|
|
11173
|
+
20,
|
|
11174
|
+
4,
|
|
11175
|
+
11,
|
|
11176
|
+
16,
|
|
11177
|
+
23,
|
|
11178
|
+
4,
|
|
11179
|
+
11,
|
|
11180
|
+
16,
|
|
11181
|
+
23,
|
|
11182
|
+
4,
|
|
11183
|
+
11,
|
|
11184
|
+
16,
|
|
11185
|
+
23,
|
|
11186
|
+
4,
|
|
11187
|
+
11,
|
|
11188
|
+
16,
|
|
11189
|
+
23,
|
|
11190
|
+
6,
|
|
11191
|
+
10,
|
|
11192
|
+
15,
|
|
11193
|
+
21,
|
|
11194
|
+
6,
|
|
11195
|
+
10,
|
|
11196
|
+
15,
|
|
11197
|
+
21,
|
|
11198
|
+
6,
|
|
11199
|
+
10,
|
|
11200
|
+
15,
|
|
11201
|
+
21,
|
|
11202
|
+
6,
|
|
11203
|
+
10,
|
|
11204
|
+
15,
|
|
11205
|
+
21
|
|
11206
|
+
];
|
|
11207
|
+
var MD5_K = new Uint32Array([
|
|
11208
|
+
3614090360,
|
|
11209
|
+
3905402710,
|
|
11210
|
+
606105819,
|
|
11211
|
+
3250441966,
|
|
11212
|
+
4118548399,
|
|
11213
|
+
1200080426,
|
|
11214
|
+
2821735955,
|
|
11215
|
+
4249261313,
|
|
11216
|
+
1770035416,
|
|
11217
|
+
2336552879,
|
|
11218
|
+
4294925233,
|
|
11219
|
+
2304563134,
|
|
11220
|
+
1804603682,
|
|
11221
|
+
4254626195,
|
|
11222
|
+
2792965006,
|
|
11223
|
+
1236535329,
|
|
11224
|
+
4129170786,
|
|
11225
|
+
3225465664,
|
|
11226
|
+
643717713,
|
|
11227
|
+
3921069994,
|
|
11228
|
+
3593408605,
|
|
11229
|
+
38016083,
|
|
11230
|
+
3634488961,
|
|
11231
|
+
3889429448,
|
|
11232
|
+
568446438,
|
|
11233
|
+
3275163606,
|
|
11234
|
+
4107603335,
|
|
11235
|
+
1163531501,
|
|
11236
|
+
2850285829,
|
|
11237
|
+
4243563512,
|
|
11238
|
+
1735328473,
|
|
11239
|
+
2368359562,
|
|
11240
|
+
4294588738,
|
|
11241
|
+
2272392833,
|
|
11242
|
+
1839030562,
|
|
11243
|
+
4259657740,
|
|
11244
|
+
2763975236,
|
|
11245
|
+
1272893353,
|
|
11246
|
+
4139469664,
|
|
11247
|
+
3200236656,
|
|
11248
|
+
681279174,
|
|
11249
|
+
3936430074,
|
|
11250
|
+
3572445317,
|
|
11251
|
+
76029189,
|
|
11252
|
+
3654602809,
|
|
11253
|
+
3873151461,
|
|
11254
|
+
530742520,
|
|
11255
|
+
3299628645,
|
|
11256
|
+
4096336452,
|
|
11257
|
+
1126891415,
|
|
11258
|
+
2878612391,
|
|
11259
|
+
4237533241,
|
|
11260
|
+
1700485571,
|
|
11261
|
+
2399980690,
|
|
11262
|
+
4293915773,
|
|
11263
|
+
2240044497,
|
|
11264
|
+
1873313359,
|
|
11265
|
+
4264355552,
|
|
11266
|
+
2734768916,
|
|
11267
|
+
1309151649,
|
|
11268
|
+
4149444226,
|
|
11269
|
+
3174756917,
|
|
11270
|
+
718787259,
|
|
11271
|
+
3951481745
|
|
11272
|
+
]);
|
|
11273
|
+
var SHA256_K = new Uint32Array([
|
|
11274
|
+
1116352408,
|
|
11275
|
+
1899447441,
|
|
11276
|
+
3049323471,
|
|
11277
|
+
3921009573,
|
|
11278
|
+
961987163,
|
|
11279
|
+
1508970993,
|
|
11280
|
+
2453635748,
|
|
11281
|
+
2870763221,
|
|
11282
|
+
3624381080,
|
|
11283
|
+
310598401,
|
|
11284
|
+
607225278,
|
|
11285
|
+
1426881987,
|
|
11286
|
+
1925078388,
|
|
11287
|
+
2162078206,
|
|
11288
|
+
2614888103,
|
|
11289
|
+
3248222580,
|
|
11290
|
+
3835390401,
|
|
11291
|
+
4022224774,
|
|
11292
|
+
264347078,
|
|
11293
|
+
604807628,
|
|
11294
|
+
770255983,
|
|
11295
|
+
1249150122,
|
|
11296
|
+
1555081692,
|
|
11297
|
+
1996064986,
|
|
11298
|
+
2554220882,
|
|
11299
|
+
2821834349,
|
|
11300
|
+
2952996808,
|
|
11301
|
+
3210313671,
|
|
11302
|
+
3336571891,
|
|
11303
|
+
3584528711,
|
|
11304
|
+
113926993,
|
|
11305
|
+
338241895,
|
|
11306
|
+
666307205,
|
|
11307
|
+
773529912,
|
|
11308
|
+
1294757372,
|
|
11309
|
+
1396182291,
|
|
11310
|
+
1695183700,
|
|
11311
|
+
1986661051,
|
|
11312
|
+
2177026350,
|
|
11313
|
+
2456956037,
|
|
11314
|
+
2730485921,
|
|
11315
|
+
2820302411,
|
|
11316
|
+
3259730800,
|
|
11317
|
+
3345764771,
|
|
11318
|
+
3516065817,
|
|
11319
|
+
3600352804,
|
|
11320
|
+
4094571909,
|
|
11321
|
+
275423344,
|
|
11322
|
+
430227734,
|
|
11323
|
+
506948616,
|
|
11324
|
+
659060556,
|
|
11325
|
+
883997877,
|
|
11326
|
+
958139571,
|
|
11327
|
+
1322822218,
|
|
11328
|
+
1537002063,
|
|
11329
|
+
1747873779,
|
|
11330
|
+
1955562222,
|
|
11331
|
+
2024104815,
|
|
11332
|
+
2227730452,
|
|
11333
|
+
2361852424,
|
|
11334
|
+
2428436474,
|
|
11335
|
+
2756734187,
|
|
11336
|
+
3204031479,
|
|
11337
|
+
3329325298
|
|
11338
|
+
]);
|
|
11339
|
+
var SHA512_K = [
|
|
11340
|
+
0x428a2f98d728ae22n,
|
|
11341
|
+
0x7137449123ef65cdn,
|
|
11342
|
+
0xb5c0fbcfec4d3b2fn,
|
|
11343
|
+
0xe9b5dba58189dbbcn,
|
|
11344
|
+
0x3956c25bf348b538n,
|
|
11345
|
+
0x59f111f1b605d019n,
|
|
11346
|
+
0x923f82a4af194f9bn,
|
|
11347
|
+
0xab1c5ed5da6d8118n,
|
|
11348
|
+
0xd807aa98a3030242n,
|
|
11349
|
+
0x12835b0145706fben,
|
|
11350
|
+
0x243185be4ee4b28cn,
|
|
11351
|
+
0x550c7dc3d5ffb4e2n,
|
|
11352
|
+
0x72be5d74f27b896fn,
|
|
11353
|
+
0x80deb1fe3b1696b1n,
|
|
11354
|
+
0x9bdc06a725c71235n,
|
|
11355
|
+
0xc19bf174cf692694n,
|
|
11356
|
+
0xe49b69c19ef14ad2n,
|
|
11357
|
+
0xefbe4786384f25e3n,
|
|
11358
|
+
0x0fc19dc68b8cd5b5n,
|
|
11359
|
+
0x240ca1cc77ac9c65n,
|
|
11360
|
+
0x2de92c6f592b0275n,
|
|
11361
|
+
0x4a7484aa6ea6e483n,
|
|
11362
|
+
0x5cb0a9dcbd41fbd4n,
|
|
11363
|
+
0x76f988da831153b5n,
|
|
11364
|
+
0x983e5152ee66dfabn,
|
|
11365
|
+
0xa831c66d2db43210n,
|
|
11366
|
+
0xb00327c898fb213fn,
|
|
11367
|
+
0xbf597fc7beef0ee4n,
|
|
11368
|
+
0xc6e00bf33da88fc2n,
|
|
11369
|
+
0xd5a79147930aa725n,
|
|
11370
|
+
0x06ca6351e003826fn,
|
|
11371
|
+
0x142929670a0e6e70n,
|
|
11372
|
+
0x27b70a8546d22ffcn,
|
|
11373
|
+
0x2e1b21385c26c926n,
|
|
11374
|
+
0x4d2c6dfc5ac42aedn,
|
|
11375
|
+
0x53380d139d95b3dfn,
|
|
11376
|
+
0x650a73548baf63den,
|
|
11377
|
+
0x766a0abb3c77b2a8n,
|
|
11378
|
+
0x81c2c92e47edaee6n,
|
|
11379
|
+
0x92722c851482353bn,
|
|
11380
|
+
0xa2bfe8a14cf10364n,
|
|
11381
|
+
0xa81a664bbc423001n,
|
|
11382
|
+
0xc24b8b70d0f89791n,
|
|
11383
|
+
0xc76c51a30654be30n,
|
|
11384
|
+
0xd192e819d6ef5218n,
|
|
11385
|
+
0xd69906245565a910n,
|
|
11386
|
+
0xf40e35855771202an,
|
|
11387
|
+
0x106aa07032bbd1b8n,
|
|
11388
|
+
0x19a4c116b8d2d0c8n,
|
|
11389
|
+
0x1e376c085141ab53n,
|
|
11390
|
+
0x2748774cdf8eeb99n,
|
|
11391
|
+
0x34b0bcb5e19b48a8n,
|
|
11392
|
+
0x391c0cb3c5c95a63n,
|
|
11393
|
+
0x4ed8aa4ae3418acbn,
|
|
11394
|
+
0x5b9cca4f7763e373n,
|
|
11395
|
+
0x682e6ff3d6b2b8a3n,
|
|
11396
|
+
0x748f82ee5defb2fcn,
|
|
11397
|
+
0x78a5636f43172f60n,
|
|
11398
|
+
0x84c87814a1f0ab72n,
|
|
11399
|
+
0x8cc702081a6439ecn,
|
|
11400
|
+
0x90befffa23631e28n,
|
|
11401
|
+
0xa4506cebde82bde9n,
|
|
11402
|
+
0xbef9a3f7b2c67915n,
|
|
11403
|
+
0xc67178f2e372532bn,
|
|
11404
|
+
0xca273eceea26619cn,
|
|
11405
|
+
0xd186b8c721c0c207n,
|
|
11406
|
+
0xeada7dd6cde0eb1en,
|
|
11407
|
+
0xf57d4f7fee6ed178n,
|
|
11408
|
+
0x06f067aa72176fban,
|
|
11409
|
+
0x0a637dc5a2c898a6n,
|
|
11410
|
+
0x113f9804bef90daen,
|
|
11411
|
+
0x1b710b35131c471bn,
|
|
11412
|
+
0x28db77f523047d84n,
|
|
11413
|
+
0x32caab7b40c72493n,
|
|
11414
|
+
0x3c9ebe0a15c9bebcn,
|
|
11415
|
+
0x431d67c49c100d4cn,
|
|
11416
|
+
0x4cc5d4becb3e42b6n,
|
|
11417
|
+
0x597f299cfc657e2an,
|
|
11418
|
+
0x5fcb6fab3ad6faecn,
|
|
11419
|
+
0x6c44198c4a475817n
|
|
11420
|
+
];
|
|
11421
|
+
var MASK64 = (1n << 64n) - 1n;
|
|
11422
|
+
function rotl32(x, n) {
|
|
11423
|
+
return (x << n | x >>> 32 - n) >>> 0;
|
|
11424
|
+
}
|
|
11425
|
+
function rotr32(x, n) {
|
|
11426
|
+
return (x >>> n | x << 32 - n) >>> 0;
|
|
11427
|
+
}
|
|
11428
|
+
function rotr64(x, n) {
|
|
11429
|
+
return (x >> n | x << 64n - n) & MASK64;
|
|
11430
|
+
}
|
|
11431
|
+
function padBe(data, block, lengthBytes) {
|
|
11432
|
+
const bitLen = BigInt(data.length) * 8n;
|
|
11433
|
+
let len = data.length + 1 + lengthBytes;
|
|
11434
|
+
const rem = len % block;
|
|
11435
|
+
if (rem !== 0) len += block - rem;
|
|
11436
|
+
const out = new Uint8Array(len);
|
|
11437
|
+
out.set(data);
|
|
11438
|
+
out[data.length] = 128;
|
|
11439
|
+
const view = new DataView(out.buffer);
|
|
11440
|
+
view.setUint32(out.length - 8, Number(bitLen >> 32n & 0xffffffffn), false);
|
|
11441
|
+
view.setUint32(out.length - 4, Number(bitLen & 0xffffffffn), false);
|
|
11442
|
+
return out;
|
|
11443
|
+
}
|
|
11444
|
+
function padMd5(data) {
|
|
11445
|
+
const bitLen = BigInt(data.length) * 8n;
|
|
11446
|
+
let len = data.length + 1 + 8;
|
|
11447
|
+
const rem = len % 64;
|
|
11448
|
+
if (rem !== 0) len += 64 - rem;
|
|
11449
|
+
const out = new Uint8Array(len);
|
|
11450
|
+
out.set(data);
|
|
11451
|
+
out[data.length] = 128;
|
|
11452
|
+
const view = new DataView(out.buffer);
|
|
11453
|
+
view.setUint32(out.length - 8, Number(bitLen & 0xffffffffn), true);
|
|
11454
|
+
view.setUint32(out.length - 4, Number(bitLen >> 32n & 0xffffffffn), true);
|
|
11455
|
+
return out;
|
|
11456
|
+
}
|
|
11457
|
+
function md5(data) {
|
|
11458
|
+
const bytes = padMd5(data);
|
|
11459
|
+
const view = new DataView(bytes.buffer);
|
|
11460
|
+
let a0 = 1732584193;
|
|
11461
|
+
let b0 = 4023233417;
|
|
11462
|
+
let c0 = 2562383102;
|
|
11463
|
+
let d0 = 271733878;
|
|
11464
|
+
const m = new Uint32Array(16);
|
|
11465
|
+
for (let off = 0; off < bytes.length; off += 64) {
|
|
11466
|
+
for (let i = 0; i < 16; i++) m[i] = view.getUint32(off + i * 4, true);
|
|
11467
|
+
let a = a0;
|
|
11468
|
+
let b = b0;
|
|
11469
|
+
let c = c0;
|
|
11470
|
+
let d = d0;
|
|
11471
|
+
for (let i = 0; i < 64; i++) {
|
|
11472
|
+
let f;
|
|
11473
|
+
let g;
|
|
11474
|
+
if (i < 16) {
|
|
11475
|
+
f = b & c | ~b & d;
|
|
11476
|
+
g = i;
|
|
11477
|
+
} else if (i < 32) {
|
|
11478
|
+
f = d & b | ~d & c;
|
|
11479
|
+
g = (5 * i + 1) % 16;
|
|
11480
|
+
} else if (i < 48) {
|
|
11481
|
+
f = b ^ c ^ d;
|
|
11482
|
+
g = (3 * i + 5) % 16;
|
|
11483
|
+
} else {
|
|
11484
|
+
f = c ^ (b | ~d);
|
|
11485
|
+
g = 7 * i % 16;
|
|
11486
|
+
}
|
|
11487
|
+
const sum = f + a + MD5_K[i] + m[g] >>> 0;
|
|
11488
|
+
a = d;
|
|
11489
|
+
d = c;
|
|
11490
|
+
c = b;
|
|
11491
|
+
b = b + rotl32(sum, MD5_S[i]) >>> 0;
|
|
11492
|
+
}
|
|
11493
|
+
a0 = a0 + a >>> 0;
|
|
11494
|
+
b0 = b0 + b >>> 0;
|
|
11495
|
+
c0 = c0 + c >>> 0;
|
|
11496
|
+
d0 = d0 + d >>> 0;
|
|
11497
|
+
}
|
|
11498
|
+
const out = new Uint8Array(16);
|
|
11499
|
+
const ov = new DataView(out.buffer);
|
|
11500
|
+
ov.setUint32(0, a0, true);
|
|
11501
|
+
ov.setUint32(4, b0, true);
|
|
11502
|
+
ov.setUint32(8, c0, true);
|
|
11503
|
+
ov.setUint32(12, d0, true);
|
|
11504
|
+
return out;
|
|
11505
|
+
}
|
|
11506
|
+
function sha1(data) {
|
|
11507
|
+
const bytes = padBe(data, 64, 8);
|
|
11508
|
+
const view = new DataView(bytes.buffer);
|
|
11509
|
+
let h0 = 1732584193;
|
|
11510
|
+
let h1 = 4023233417;
|
|
11511
|
+
let h2 = 2562383102;
|
|
11512
|
+
let h3 = 271733878;
|
|
11513
|
+
let h4 = 3285377520;
|
|
11514
|
+
const w = new Uint32Array(80);
|
|
11515
|
+
for (let off = 0; off < bytes.length; off += 64) {
|
|
11516
|
+
for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
|
|
11517
|
+
for (let t = 16; t < 80; t++) w[t] = rotl32(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
|
|
11518
|
+
let a = h0;
|
|
11519
|
+
let b = h1;
|
|
11520
|
+
let c = h2;
|
|
11521
|
+
let d = h3;
|
|
11522
|
+
let e = h4;
|
|
11523
|
+
for (let t = 0; t < 80; t++) {
|
|
11524
|
+
let f;
|
|
11525
|
+
let k;
|
|
11526
|
+
if (t < 20) {
|
|
11527
|
+
f = b & c | ~b & d;
|
|
11528
|
+
k = 1518500249;
|
|
11529
|
+
} else if (t < 40) {
|
|
11530
|
+
f = b ^ c ^ d;
|
|
11531
|
+
k = 1859775393;
|
|
11532
|
+
} else if (t < 60) {
|
|
11533
|
+
f = b & c | b & d | c & d;
|
|
11534
|
+
k = 2400959708;
|
|
11535
|
+
} else {
|
|
11536
|
+
f = b ^ c ^ d;
|
|
11537
|
+
k = 3395469782;
|
|
11538
|
+
}
|
|
11539
|
+
const temp = rotl32(a, 5) + f + e + k + w[t] >>> 0;
|
|
11540
|
+
e = d;
|
|
11541
|
+
d = c;
|
|
11542
|
+
c = rotl32(b, 30);
|
|
11543
|
+
b = a;
|
|
11544
|
+
a = temp;
|
|
11545
|
+
}
|
|
11546
|
+
h0 = h0 + a >>> 0;
|
|
11547
|
+
h1 = h1 + b >>> 0;
|
|
11548
|
+
h2 = h2 + c >>> 0;
|
|
11549
|
+
h3 = h3 + d >>> 0;
|
|
11550
|
+
h4 = h4 + e >>> 0;
|
|
11551
|
+
}
|
|
11552
|
+
const out = new Uint8Array(20);
|
|
11553
|
+
const ov = new DataView(out.buffer);
|
|
11554
|
+
ov.setUint32(0, h0, false);
|
|
11555
|
+
ov.setUint32(4, h1, false);
|
|
11556
|
+
ov.setUint32(8, h2, false);
|
|
11557
|
+
ov.setUint32(12, h3, false);
|
|
11558
|
+
ov.setUint32(16, h4, false);
|
|
11559
|
+
return out;
|
|
11560
|
+
}
|
|
11561
|
+
function sha256(data, sha224) {
|
|
11562
|
+
const bytes = padBe(data, 64, 8);
|
|
11563
|
+
const view = new DataView(bytes.buffer);
|
|
11564
|
+
let h0 = sha224 ? 3238371032 : 1779033703;
|
|
11565
|
+
let h1 = sha224 ? 914150663 : 3144134277;
|
|
11566
|
+
let h2 = sha224 ? 812702999 : 1013904242;
|
|
11567
|
+
let h3 = sha224 ? 4144912697 : 2773480762;
|
|
11568
|
+
let h4 = sha224 ? 4290775857 : 1359893119;
|
|
11569
|
+
let h5 = sha224 ? 1750603025 : 2600822924;
|
|
11570
|
+
let h6 = sha224 ? 1694076839 : 528734635;
|
|
11571
|
+
let h7 = sha224 ? 3204075428 : 1541459225;
|
|
11572
|
+
const w = new Uint32Array(64);
|
|
11573
|
+
for (let off = 0; off < bytes.length; off += 64) {
|
|
11574
|
+
for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
|
|
11575
|
+
for (let t = 16; t < 64; t++) {
|
|
11576
|
+
const x15 = w[t - 15];
|
|
11577
|
+
const x2 = w[t - 2];
|
|
11578
|
+
const s0 = rotr32(x15, 7) ^ rotr32(x15, 18) ^ x15 >>> 3;
|
|
11579
|
+
const s1 = rotr32(x2, 17) ^ rotr32(x2, 19) ^ x2 >>> 10;
|
|
11580
|
+
w[t] = w[t - 16] + s0 + w[t - 7] + s1 >>> 0;
|
|
11581
|
+
}
|
|
11582
|
+
let a = h0;
|
|
11583
|
+
let b = h1;
|
|
11584
|
+
let c = h2;
|
|
11585
|
+
let d = h3;
|
|
11586
|
+
let e = h4;
|
|
11587
|
+
let f = h5;
|
|
11588
|
+
let g = h6;
|
|
11589
|
+
let h = h7;
|
|
11590
|
+
for (let t = 0; t < 64; t++) {
|
|
11591
|
+
const s1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
|
|
11592
|
+
const ch = e & f ^ ~e & g;
|
|
11593
|
+
const t1 = h + s1 + ch + SHA256_K[t] + w[t] >>> 0;
|
|
11594
|
+
const s0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22);
|
|
11595
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
11596
|
+
const t2 = s0 + maj >>> 0;
|
|
11597
|
+
h = g;
|
|
11598
|
+
g = f;
|
|
11599
|
+
f = e;
|
|
11600
|
+
e = d + t1 >>> 0;
|
|
11601
|
+
d = c;
|
|
11602
|
+
c = b;
|
|
11603
|
+
b = a;
|
|
11604
|
+
a = t1 + t2 >>> 0;
|
|
11605
|
+
}
|
|
11606
|
+
h0 = h0 + a >>> 0;
|
|
11607
|
+
h1 = h1 + b >>> 0;
|
|
11608
|
+
h2 = h2 + c >>> 0;
|
|
11609
|
+
h3 = h3 + d >>> 0;
|
|
11610
|
+
h4 = h4 + e >>> 0;
|
|
11611
|
+
h5 = h5 + f >>> 0;
|
|
11612
|
+
h6 = h6 + g >>> 0;
|
|
11613
|
+
h7 = h7 + h >>> 0;
|
|
11614
|
+
}
|
|
11615
|
+
const words = sha224 ? 7 : 8;
|
|
11616
|
+
const out = new Uint8Array(words * 4);
|
|
11617
|
+
const ov = new DataView(out.buffer);
|
|
11618
|
+
const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
|
|
11619
|
+
for (let i = 0; i < words; i++) ov.setUint32(i * 4, hs[i], false);
|
|
11620
|
+
return out;
|
|
11621
|
+
}
|
|
11622
|
+
function readUint64BE(view, offset) {
|
|
11623
|
+
return BigInt(view.getUint32(offset, false)) << 32n | BigInt(view.getUint32(offset + 4, false));
|
|
11624
|
+
}
|
|
11625
|
+
function sha512(data, sha384) {
|
|
11626
|
+
const bytes = padBe(data, 128, 16);
|
|
11627
|
+
const view = new DataView(bytes.buffer);
|
|
11628
|
+
let h0 = sha384 ? 0xcbbb9d5dc1059ed8n : 0x6a09e667f3bcc908n;
|
|
11629
|
+
let h1 = sha384 ? 0x629a292a367cd507n : 0xbb67ae8584caa73bn;
|
|
11630
|
+
let h2 = sha384 ? 0x9159015a3070dd17n : 0x3c6ef372fe94f82bn;
|
|
11631
|
+
let h3 = sha384 ? 0x152fecd8f70e5939n : 0xa54ff53a5f1d36f1n;
|
|
11632
|
+
let h4 = sha384 ? 0x67332667ffc00b31n : 0x510e527fade682d1n;
|
|
11633
|
+
let h5 = sha384 ? 0x8eb44a8768581511n : 0x9b05688c2b3e6c1fn;
|
|
11634
|
+
let h6 = sha384 ? 0xdb0c2e0d64f98fa7n : 0x1f83d9abfb41bd6bn;
|
|
11635
|
+
let h7 = sha384 ? 0x47b5481dbefa4fa4n : 0x5be0cd19137e2179n;
|
|
11636
|
+
const w = new Array(80);
|
|
11637
|
+
for (let off = 0; off < bytes.length; off += 128) {
|
|
11638
|
+
for (let t = 0; t < 16; t++) w[t] = readUint64BE(view, off + t * 8);
|
|
11639
|
+
for (let t = 16; t < 80; t++) {
|
|
11640
|
+
const x15 = w[t - 15];
|
|
11641
|
+
const x2 = w[t - 2];
|
|
11642
|
+
const s0 = rotr64(x15, 1n) ^ rotr64(x15, 8n) ^ x15 >> 7n;
|
|
11643
|
+
const s1 = rotr64(x2, 19n) ^ rotr64(x2, 61n) ^ x2 >> 6n;
|
|
11644
|
+
w[t] = w[t - 16] + s0 + w[t - 7] + s1 & MASK64;
|
|
11645
|
+
}
|
|
11646
|
+
let a = h0;
|
|
11647
|
+
let b = h1;
|
|
11648
|
+
let c = h2;
|
|
11649
|
+
let d = h3;
|
|
11650
|
+
let e = h4;
|
|
11651
|
+
let f = h5;
|
|
11652
|
+
let g = h6;
|
|
11653
|
+
let h = h7;
|
|
11654
|
+
for (let t = 0; t < 80; t++) {
|
|
11655
|
+
const s1 = rotr64(e, 14n) ^ rotr64(e, 18n) ^ rotr64(e, 41n);
|
|
11656
|
+
const ch = e & f ^ ~e & g & MASK64;
|
|
11657
|
+
const t1 = h + s1 + ch + SHA512_K[t] + w[t] & MASK64;
|
|
11658
|
+
const s0 = rotr64(a, 28n) ^ rotr64(a, 34n) ^ rotr64(a, 39n);
|
|
11659
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
11660
|
+
const t2 = s0 + maj & MASK64;
|
|
11661
|
+
h = g;
|
|
11662
|
+
g = f;
|
|
11663
|
+
f = e;
|
|
11664
|
+
e = d + t1 & MASK64;
|
|
11665
|
+
d = c;
|
|
11666
|
+
c = b;
|
|
11667
|
+
b = a;
|
|
11668
|
+
a = t1 + t2 & MASK64;
|
|
11669
|
+
}
|
|
11670
|
+
h0 = h0 + a & MASK64;
|
|
11671
|
+
h1 = h1 + b & MASK64;
|
|
11672
|
+
h2 = h2 + c & MASK64;
|
|
11673
|
+
h3 = h3 + d & MASK64;
|
|
11674
|
+
h4 = h4 + e & MASK64;
|
|
11675
|
+
h5 = h5 + f & MASK64;
|
|
11676
|
+
h6 = h6 + g & MASK64;
|
|
11677
|
+
h7 = h7 + h & MASK64;
|
|
11678
|
+
}
|
|
11679
|
+
const words = sha384 ? 6 : 8;
|
|
11680
|
+
const out = new Uint8Array(words * 8);
|
|
11681
|
+
const ov = new DataView(out.buffer);
|
|
11682
|
+
const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
|
|
11683
|
+
for (let i = 0; i < words; i++) {
|
|
11684
|
+
const word = hs[i];
|
|
11685
|
+
ov.setUint32(i * 8, Number(word >> 32n & 0xffffffffn), false);
|
|
11686
|
+
ov.setUint32(i * 8 + 4, Number(word & 0xffffffffn), false);
|
|
11687
|
+
}
|
|
11688
|
+
return out;
|
|
11689
|
+
}
|
|
11690
|
+
function digestBytes(data, algorithm) {
|
|
11691
|
+
switch (algorithm.toLowerCase().replaceAll("-", "")) {
|
|
11692
|
+
case "md5":
|
|
11693
|
+
return md5(data);
|
|
11694
|
+
case "sha1":
|
|
11695
|
+
return sha1(data);
|
|
11696
|
+
case "sha224":
|
|
11697
|
+
return sha256(data, true);
|
|
11698
|
+
case "sha256":
|
|
11699
|
+
return sha256(data, false);
|
|
11700
|
+
case "sha384":
|
|
11701
|
+
return sha512(data, true);
|
|
11702
|
+
case "sha512":
|
|
11703
|
+
return sha512(data, false);
|
|
11704
|
+
default:
|
|
11705
|
+
throw pgError("invalid_parameter_value", `Cannot use "${algorithm}": No such hash algorithm`, "22023");
|
|
11706
|
+
}
|
|
11707
|
+
}
|
|
11708
|
+
function pgcryptoInstalled(state) {
|
|
11709
|
+
for (const schema of state.schemas.values()) {
|
|
11710
|
+
for (const fns of schema.functions.values()) {
|
|
11711
|
+
if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGCRYPTO_BODY)) return true;
|
|
11712
|
+
}
|
|
11713
|
+
}
|
|
11714
|
+
return false;
|
|
11715
|
+
}
|
|
11716
|
+
function installPgcrypto(state, schemaName) {
|
|
11717
|
+
const schema = state.getSchema(schemaName);
|
|
11718
|
+
const existing = schema.functions.get("digest") ?? [];
|
|
11719
|
+
const arg0Types = ["text", "bytea"];
|
|
11720
|
+
for (const arg0 of arg0Types) {
|
|
11721
|
+
const fn = {
|
|
11722
|
+
name: "digest",
|
|
11723
|
+
schema: schema.name,
|
|
11724
|
+
argNames: [null, null],
|
|
11725
|
+
argTypes: [arg0, "text"],
|
|
11726
|
+
argDefaults: [null, null],
|
|
11727
|
+
returns: "bytea",
|
|
11728
|
+
returnsSet: false,
|
|
11729
|
+
returnsTable: null,
|
|
11730
|
+
language: "internal",
|
|
11731
|
+
body: null,
|
|
11732
|
+
rawBody: PGCRYPTO_BODY,
|
|
11733
|
+
strict: true,
|
|
11734
|
+
oid: state.nextOid()
|
|
11735
|
+
};
|
|
11736
|
+
existing.push(fn);
|
|
11737
|
+
}
|
|
11738
|
+
schema.functions.set("digest", existing);
|
|
11739
|
+
}
|
|
11740
|
+
function uninstallPgcrypto(state) {
|
|
11741
|
+
let removed = false;
|
|
11742
|
+
for (const schema of state.schemas.values()) {
|
|
11743
|
+
for (const [name, fns] of [...schema.functions.entries()]) {
|
|
11744
|
+
const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGCRYPTO_BODY);
|
|
11745
|
+
if (keep.length === fns.length) continue;
|
|
11746
|
+
removed = true;
|
|
11747
|
+
if (keep.length === 0) schema.functions.delete(name);
|
|
11748
|
+
else schema.functions.set(name, keep);
|
|
11749
|
+
}
|
|
11750
|
+
}
|
|
11751
|
+
return removed;
|
|
11752
|
+
}
|
|
11753
|
+
function evalPgcryptoFunction(ctx, fn, args) {
|
|
11754
|
+
const ret = fn.returns ?? "bytea";
|
|
11755
|
+
if (fn.strict && args.some((arg) => arg.v === null)) return tv(ret, null);
|
|
11756
|
+
if (fn.name !== "digest" || args.length !== 2) {
|
|
11757
|
+
throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
|
|
11758
|
+
}
|
|
11759
|
+
const data = castTo(ctx, args[0], fn.argTypes[0] ?? "bytea", {});
|
|
11760
|
+
const algorithm = castTo(ctx, args[1], "text", {});
|
|
11761
|
+
if (data.v === null || algorithm.v === null) return tv("bytea", null);
|
|
11762
|
+
const bytes = fn.argTypes[0] === "bytea" ? data.v : utf82.encode(data.v);
|
|
11763
|
+
return tv("bytea", digestBytes(bytes, algorithm.v));
|
|
11764
|
+
}
|
|
11765
|
+
|
|
10902
11766
|
// src/functions/srf.ts
|
|
10903
11767
|
function jsonInput(ctx, a) {
|
|
10904
11768
|
void ctx;
|
|
@@ -13092,10 +13956,32 @@ var Parser = class {
|
|
|
13092
13956
|
}
|
|
13093
13957
|
if (this.atKw("extension")) {
|
|
13094
13958
|
this.pos++;
|
|
13095
|
-
this.parseIfNotExists();
|
|
13959
|
+
const ifNotExists = this.parseIfNotExists();
|
|
13096
13960
|
const name = this.ident(true);
|
|
13097
|
-
|
|
13098
|
-
|
|
13961
|
+
let schema = null;
|
|
13962
|
+
let version = null;
|
|
13963
|
+
let cascade = false;
|
|
13964
|
+
this.eatKw("with");
|
|
13965
|
+
while (!this.atPunct(";") && this.peek().type !== "eof") {
|
|
13966
|
+
if (this.eatKw("schema")) {
|
|
13967
|
+
schema = this.ident(true);
|
|
13968
|
+
continue;
|
|
13969
|
+
}
|
|
13970
|
+
if (this.eatKw("version")) {
|
|
13971
|
+
const token = this.next();
|
|
13972
|
+
if (token.type !== "string" && token.type !== "ident" && token.type !== "quoted_ident") {
|
|
13973
|
+
this.errorAt(token, "expected extension version");
|
|
13974
|
+
}
|
|
13975
|
+
version = token.value;
|
|
13976
|
+
continue;
|
|
13977
|
+
}
|
|
13978
|
+
if (this.eatKw("cascade")) {
|
|
13979
|
+
cascade = true;
|
|
13980
|
+
continue;
|
|
13981
|
+
}
|
|
13982
|
+
throw unsupported("CREATE EXTENSION option");
|
|
13983
|
+
}
|
|
13984
|
+
return { type: "create_extension", name, ifNotExists, schema, version, cascade };
|
|
13099
13985
|
}
|
|
13100
13986
|
if (this.atKw("database")) throw unsupported("CREATE DATABASE");
|
|
13101
13987
|
if (this.atKw("role") || this.atKw("user") || this.atKw("group")) throw unsupported("roles");
|
|
@@ -13572,7 +14458,11 @@ var Parser = class {
|
|
|
13572
14458
|
let opclass = null;
|
|
13573
14459
|
const t = this.peek();
|
|
13574
14460
|
if (t.type === "ident" && !RESERVED.has(t.value) && !["asc", "desc", "nulls", "with", "where", "include"].includes(t.value)) {
|
|
13575
|
-
opclass = this.ident();
|
|
14461
|
+
opclass = [this.ident()];
|
|
14462
|
+
while (this.atPunct(".")) {
|
|
14463
|
+
this.pos++;
|
|
14464
|
+
opclass.push(this.ident(true));
|
|
14465
|
+
}
|
|
13576
14466
|
}
|
|
13577
14467
|
let dir = null;
|
|
13578
14468
|
if (this.eatKw("asc")) dir = "asc";
|
|
@@ -15808,7 +16698,13 @@ function runStmt(env, stmt, vars, emit, tableNames) {
|
|
|
15808
16698
|
return;
|
|
15809
16699
|
}
|
|
15810
16700
|
case "for": {
|
|
15811
|
-
const forEnv = {
|
|
16701
|
+
const forEnv = {
|
|
16702
|
+
ctx: env.ctx,
|
|
16703
|
+
params: env.params,
|
|
16704
|
+
ctes: env.ctes,
|
|
16705
|
+
outer: vars.scope(),
|
|
16706
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
16707
|
+
};
|
|
15812
16708
|
const rel2 = executeSelectStmt(forEnv, stmt.query);
|
|
15813
16709
|
for (const row of rel2.rows) {
|
|
15814
16710
|
for (let i = 0; i < stmt.targets.length; i++) {
|
|
@@ -15850,7 +16746,13 @@ function initVars(env, fn, bound, program) {
|
|
|
15850
16746
|
if (fn.returnsTable) {
|
|
15851
16747
|
for (const c of fn.returnsTable) vars.declare(c.name, c.type, null);
|
|
15852
16748
|
}
|
|
15853
|
-
const seed = () => ({
|
|
16749
|
+
const seed = () => ({
|
|
16750
|
+
ctx: env.ctx,
|
|
16751
|
+
params: bound,
|
|
16752
|
+
ctes: /* @__PURE__ */ new Map(),
|
|
16753
|
+
outer: vars.scope(),
|
|
16754
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
16755
|
+
});
|
|
15854
16756
|
for (const d of program.decls) {
|
|
15855
16757
|
const id = resolveTypeName(env.ctx.state, d.typeName).column.id;
|
|
15856
16758
|
let val = null;
|
|
@@ -15863,7 +16765,13 @@ function initVars(env, fn, bound, program) {
|
|
|
15863
16765
|
return vars;
|
|
15864
16766
|
}
|
|
15865
16767
|
function plpgsqlEnv(env, bound, vars) {
|
|
15866
|
-
return {
|
|
16768
|
+
return {
|
|
16769
|
+
ctx: env.ctx,
|
|
16770
|
+
params: bound,
|
|
16771
|
+
ctes: /* @__PURE__ */ new Map(),
|
|
16772
|
+
outer: vars.scope(),
|
|
16773
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
16774
|
+
};
|
|
15867
16775
|
}
|
|
15868
16776
|
function callPlpgsqlScalar(env, fn, args) {
|
|
15869
16777
|
const retT = fn.returns ?? "text";
|
|
@@ -16462,7 +17370,13 @@ function functionEnv(env, fn, args) {
|
|
|
16462
17370
|
bound.push(castTo(env.ctx, evalScalar(env, null, dflt), fn.argTypes[i], {}));
|
|
16463
17371
|
}
|
|
16464
17372
|
}
|
|
16465
|
-
const fnEnv = {
|
|
17373
|
+
const fnEnv = {
|
|
17374
|
+
ctx: env.ctx,
|
|
17375
|
+
params: bound,
|
|
17376
|
+
ctes: /* @__PURE__ */ new Map(),
|
|
17377
|
+
outer: null,
|
|
17378
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
17379
|
+
};
|
|
16466
17380
|
const namedCols = fn.argNames.map((n, i) => ({ n, i })).filter((x) => x.n !== null);
|
|
16467
17381
|
if (namedCols.length > 0) {
|
|
16468
17382
|
const cols = namedCols.map(({ n, i }) => ({ name: n, type: fn.argTypes[i] ?? "text", table: null }));
|
|
@@ -16488,6 +17402,10 @@ function callSqlFunctionScalar(env, fn, args) {
|
|
|
16488
17402
|
if (fn.language === "plpgsql") {
|
|
16489
17403
|
return callPlpgsqlScalar(env, fn, args);
|
|
16490
17404
|
}
|
|
17405
|
+
if (fn.language === "internal") {
|
|
17406
|
+
if (fn.rawBody === "pg_trgm") return evalPgTrgmFunction(env.ctx, fn, args);
|
|
17407
|
+
return evalPgcryptoFunction(env.ctx, fn, args);
|
|
17408
|
+
}
|
|
16491
17409
|
if (fn.language === "js") {
|
|
16492
17410
|
if (!fn.jsImpl) {
|
|
16493
17411
|
throw pgError("undefined_function", `JavaScript function ${fn.name} is not registered`, "42883");
|
|
@@ -16564,7 +17482,13 @@ function applyWith(env, w) {
|
|
|
16564
17482
|
if (!w) return env;
|
|
16565
17483
|
const ctes = new Map(env.ctes);
|
|
16566
17484
|
for (const cte of w.ctes) {
|
|
16567
|
-
const scopedEnv = {
|
|
17485
|
+
const scopedEnv = {
|
|
17486
|
+
ctx: env.ctx,
|
|
17487
|
+
params: env.params,
|
|
17488
|
+
ctes,
|
|
17489
|
+
outer: env.outer,
|
|
17490
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
17491
|
+
};
|
|
16568
17492
|
let rel2;
|
|
16569
17493
|
if (w.recursive && cte.query.type === "select" && cte.query.body.type === "setop" && cte.query.body.op === "union" && referencesRelation(cte.query.body.right, cte.name)) {
|
|
16570
17494
|
rel2 = executeRecursiveCte(scopedEnv, cte, cte.query);
|
|
@@ -16579,7 +17503,13 @@ function applyWith(env, w) {
|
|
|
16579
17503
|
}
|
|
16580
17504
|
ctes.set(cte.name, applyCteColumnNames(rel2, cte));
|
|
16581
17505
|
}
|
|
16582
|
-
return {
|
|
17506
|
+
return {
|
|
17507
|
+
ctx: env.ctx,
|
|
17508
|
+
params: env.params,
|
|
17509
|
+
ctes,
|
|
17510
|
+
outer: env.outer,
|
|
17511
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
17512
|
+
};
|
|
16583
17513
|
}
|
|
16584
17514
|
var MAX_RECURSION_ITERATIONS = 2e5;
|
|
16585
17515
|
var MAX_RECURSION_ROWS = 5e6;
|
|
@@ -16611,7 +17541,13 @@ function executeRecursiveCte(env, cte, query) {
|
|
|
16611
17541
|
}
|
|
16612
17542
|
const iterCtes = new Map(env.ctes);
|
|
16613
17543
|
iterCtes.set(cte.name, { columns, rows: working });
|
|
16614
|
-
const iterEnv = {
|
|
17544
|
+
const iterEnv = {
|
|
17545
|
+
ctx: env.ctx,
|
|
17546
|
+
params: env.params,
|
|
17547
|
+
ctes: iterCtes,
|
|
17548
|
+
outer: env.outer,
|
|
17549
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
17550
|
+
};
|
|
16615
17551
|
const step = executeBody(iterEnv, body.right);
|
|
16616
17552
|
if (step.columns.length !== columns.length) {
|
|
16617
17553
|
throw pgError("syntax", "each UNION query must have the same number of columns", "42601");
|
|
@@ -16683,7 +17619,7 @@ function materializeItem(env, item, scope) {
|
|
|
16683
17619
|
if (view) {
|
|
16684
17620
|
let rel2;
|
|
16685
17621
|
if (view.materialized) {
|
|
16686
|
-
if (view.
|
|
17622
|
+
if (view.matColumns === null || view.matRows === null && !env.allowUnpopulatedMatviews) {
|
|
16687
17623
|
throw pgError(
|
|
16688
17624
|
"object_not_in_prerequisite_state",
|
|
16689
17625
|
`materialized view "${view.name}" has not been populated`,
|
|
@@ -16692,10 +17628,16 @@ function materializeItem(env, item, scope) {
|
|
|
16692
17628
|
}
|
|
16693
17629
|
rel2 = {
|
|
16694
17630
|
columns: view.matColumns.map((c) => ({ name: c.name, type: c.type, table: label })),
|
|
16695
|
-
rows: view.matRows
|
|
17631
|
+
rows: view.matRows ?? []
|
|
16696
17632
|
};
|
|
16697
17633
|
} else {
|
|
16698
|
-
const viewEnv = {
|
|
17634
|
+
const viewEnv = {
|
|
17635
|
+
ctx: env.ctx,
|
|
17636
|
+
params: null,
|
|
17637
|
+
ctes: /* @__PURE__ */ new Map(),
|
|
17638
|
+
outer: null,
|
|
17639
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
17640
|
+
};
|
|
16699
17641
|
rel2 = executeSelectStmt(viewEnv, view.query);
|
|
16700
17642
|
rel2 = {
|
|
16701
17643
|
columns: rel2.columns.map((c, i) => ({
|
|
@@ -18830,7 +19772,13 @@ function applyUpdateSets(env, table, sets, scope, row) {
|
|
|
18830
19772
|
});
|
|
18831
19773
|
if (typeof set.value === "object" && "kind" in set.value && set.value.kind === "row_subquery") {
|
|
18832
19774
|
const rel2 = executeSelectStmt(
|
|
18833
|
-
{
|
|
19775
|
+
{
|
|
19776
|
+
ctx: env.ctx,
|
|
19777
|
+
params: env.params,
|
|
19778
|
+
ctes: env.ctes,
|
|
19779
|
+
outer: scope,
|
|
19780
|
+
allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
|
|
19781
|
+
},
|
|
18834
19782
|
set.value.query
|
|
18835
19783
|
);
|
|
18836
19784
|
if (rel2.rows.length > 1) {
|
|
@@ -20613,6 +21561,9 @@ function executeTrigger(env, table, trigger, _event, oldRow, newRow) {
|
|
|
20613
21561
|
setTriggerExecutor(executeTrigger);
|
|
20614
21562
|
|
|
20615
21563
|
// src/executor/ddl.ts
|
|
21564
|
+
function matviewQueryEnv(env, allowUnpopulatedMatviews) {
|
|
21565
|
+
return { ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null, allowUnpopulatedMatviews };
|
|
21566
|
+
}
|
|
20616
21567
|
function targetSchema(env, parts) {
|
|
20617
21568
|
const state = env.ctx.state;
|
|
20618
21569
|
if (parts.length >= 2) {
|
|
@@ -21016,6 +21967,7 @@ function executeCreateIndex(env, stmt) {
|
|
|
21016
21967
|
}
|
|
21017
21968
|
}
|
|
21018
21969
|
}
|
|
21970
|
+
assertGinTrgmIndex(state, table, stmt);
|
|
21019
21971
|
schema.indexes.set(name, {
|
|
21020
21972
|
name,
|
|
21021
21973
|
schema: schema.name,
|
|
@@ -21060,7 +22012,7 @@ function executeCreateView(env, stmt) {
|
|
|
21060
22012
|
let matRows = null;
|
|
21061
22013
|
let matColumns = null;
|
|
21062
22014
|
if (stmt.materialized) {
|
|
21063
|
-
const rel2 = executeSelectStmt(
|
|
22015
|
+
const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), stmt.query);
|
|
21064
22016
|
matColumns = rel2.columns.map((c, i) => ({
|
|
21065
22017
|
name: stmt.columns?.[i] ?? c.name,
|
|
21066
22018
|
type: c.type === UNKNOWN ? "text" : c.type
|
|
@@ -21086,7 +22038,7 @@ function executeRefreshMatView(env, stmt) {
|
|
|
21086
22038
|
throw pgError("undefined_table", `materialized view "${stmt.name.join(".")}" does not exist`, "42P01");
|
|
21087
22039
|
}
|
|
21088
22040
|
const view = env.ctx.state.ensureWritableView(found);
|
|
21089
|
-
const rel2 = executeSelectStmt(
|
|
22041
|
+
const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), view.query);
|
|
21090
22042
|
view.matColumns = rel2.columns.map((c, i) => ({
|
|
21091
22043
|
name: view.columns?.[i] ?? c.name,
|
|
21092
22044
|
type: c.type === UNKNOWN ? "text" : c.type
|
|
@@ -21094,6 +22046,32 @@ function executeRefreshMatView(env, stmt) {
|
|
|
21094
22046
|
view.matRows = stmt.withData ? rel2.rows : null;
|
|
21095
22047
|
return commandResult("REFRESH MATERIALIZED VIEW", 0);
|
|
21096
22048
|
}
|
|
22049
|
+
function executeCreateExtension(env, stmt) {
|
|
22050
|
+
const state = env.ctx.state;
|
|
22051
|
+
const schemaName = stmt.schema ?? state.currentSchema();
|
|
22052
|
+
const install = () => {
|
|
22053
|
+
if (stmt.name === "pgcrypto") {
|
|
22054
|
+
if (pgcryptoInstalled(state)) return "exists";
|
|
22055
|
+
installPgcrypto(state, state.getSchema(schemaName).name);
|
|
22056
|
+
return "installed";
|
|
22057
|
+
}
|
|
22058
|
+
if (stmt.name === "pg_trgm") {
|
|
22059
|
+
if (pgTrgmInstalled(state)) return "exists";
|
|
22060
|
+
installPgTrgm(state, state.getSchema(schemaName).name);
|
|
22061
|
+
return "installed";
|
|
22062
|
+
}
|
|
22063
|
+
return "unsupported";
|
|
22064
|
+
};
|
|
22065
|
+
const outcome = install();
|
|
22066
|
+
if (outcome === "unsupported") {
|
|
22067
|
+
throw pgError("feature_not_supported", `extension "${stmt.name}" is not available`, "0A000");
|
|
22068
|
+
}
|
|
22069
|
+
if (outcome === "exists") {
|
|
22070
|
+
if (stmt.ifNotExists) return commandResult("CREATE EXTENSION", 0);
|
|
22071
|
+
throw pgError("duplicate_object", `extension "${stmt.name}" already exists`, "42710");
|
|
22072
|
+
}
|
|
22073
|
+
return commandResult("CREATE EXTENSION", 0);
|
|
22074
|
+
}
|
|
21097
22075
|
function executeCreateSchema(env, stmt) {
|
|
21098
22076
|
const state = env.ctx.state;
|
|
21099
22077
|
if (state.schemas.has(stmt.name)) {
|
|
@@ -21882,8 +22860,19 @@ function executeDrop(env, stmt) {
|
|
|
21882
22860
|
table.triggers.splice(idx, 1);
|
|
21883
22861
|
break;
|
|
21884
22862
|
}
|
|
21885
|
-
case "extension":
|
|
21886
|
-
|
|
22863
|
+
case "extension": {
|
|
22864
|
+
const ext = parts[parts.length - 1];
|
|
22865
|
+
if (ext === "pgcrypto" && pgcryptoInstalled(state)) {
|
|
22866
|
+
uninstallPgcrypto(state);
|
|
22867
|
+
break;
|
|
22868
|
+
}
|
|
22869
|
+
if (ext === "pg_trgm" && pgTrgmInstalled(state)) {
|
|
22870
|
+
uninstallPgTrgm(state);
|
|
22871
|
+
break;
|
|
22872
|
+
}
|
|
22873
|
+
if (stmt.ifExists) break;
|
|
22874
|
+
throw pgError("undefined_object", `extension "${ext}" does not exist`, "42704");
|
|
22875
|
+
}
|
|
21887
22876
|
}
|
|
21888
22877
|
}
|
|
21889
22878
|
const label = stmt.kind === "materialized_view" ? "MATERIALIZED VIEW" : stmt.kind.toUpperCase();
|
|
@@ -21965,6 +22954,8 @@ function executeStatement(env, stmt) {
|
|
|
21965
22954
|
return executeAlterSequence(env, stmt);
|
|
21966
22955
|
case "create_schema":
|
|
21967
22956
|
return executeCreateSchema(env, stmt);
|
|
22957
|
+
case "create_extension":
|
|
22958
|
+
return executeCreateExtension(env, stmt);
|
|
21968
22959
|
case "create_enum":
|
|
21969
22960
|
return executeCreateEnum(env, stmt);
|
|
21970
22961
|
case "alter_enum":
|