@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/dist/wire/cli.js CHANGED
@@ -4201,7 +4201,13 @@ var RowScope = class {
4201
4201
  }
4202
4202
  };
4203
4203
  function childEnv(env, outer) {
4204
- return { ctx: env.ctx, params: env.params, ctes: env.ctes, outer };
4204
+ return {
4205
+ ctx: env.ctx,
4206
+ params: env.params,
4207
+ ctes: env.ctes,
4208
+ outer,
4209
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
4210
+ };
4205
4211
  }
4206
4212
  function relationResult(rel2, command2) {
4207
4213
  const visible = rel2.columns.map((c, i) => ({ i, c })).filter(({ c }) => !c.hidden);
@@ -8541,6 +8547,234 @@ function callScalarFunction(ctx, name, args2) {
8541
8547
  return fn(ctx, args2);
8542
8548
  }
8543
8549
 
8550
+ // src/functions/pgtrgm.ts
8551
+ var PGTRGM_BODY = "pg_trgm";
8552
+ var WORD_SIMILARITY_THRESHOLD = Math.fround(0.6);
8553
+ var GIN_TRGM_TYPES = /* @__PURE__ */ new Set(["text", "varchar"]);
8554
+ var WORD_CHAR = /[\p{L}\p{N}]+/gu;
8555
+ function pgTrgmSchema(state) {
8556
+ for (const schema of state.schemas.values()) {
8557
+ for (const fns of schema.functions.values()) {
8558
+ if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGTRGM_BODY)) return schema.name;
8559
+ }
8560
+ }
8561
+ return null;
8562
+ }
8563
+ function pgTrgmInstalled(state) {
8564
+ return pgTrgmSchema(state) !== null;
8565
+ }
8566
+ function installPgTrgm(state, schemaName) {
8567
+ const schema = state.getSchema(schemaName);
8568
+ const existing = schema.functions.get("similarity") ?? [];
8569
+ const fn = {
8570
+ name: "similarity",
8571
+ schema: schema.name,
8572
+ argNames: [null, null],
8573
+ argTypes: ["text", "text"],
8574
+ argDefaults: [null, null],
8575
+ returns: "float4",
8576
+ returnsSet: false,
8577
+ returnsTable: null,
8578
+ language: "internal",
8579
+ body: null,
8580
+ rawBody: PGTRGM_BODY,
8581
+ strict: true,
8582
+ oid: state.nextOid()
8583
+ };
8584
+ existing.push(fn);
8585
+ schema.functions.set("similarity", existing);
8586
+ }
8587
+ function uninstallPgTrgm(state) {
8588
+ let removed = false;
8589
+ for (const schema of state.schemas.values()) {
8590
+ for (const [name, fns] of [...schema.functions.entries()]) {
8591
+ const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGTRGM_BODY);
8592
+ if (keep.length === fns.length) continue;
8593
+ removed = true;
8594
+ if (keep.length === 0) schema.functions.delete(name);
8595
+ else schema.functions.set(name, keep);
8596
+ }
8597
+ }
8598
+ return removed;
8599
+ }
8600
+ function evalPgTrgmFunction(ctx, fn, args2) {
8601
+ const ret = fn.returns ?? "float4";
8602
+ if (fn.strict && args2.some((arg) => arg.v === null)) return tv(ret, null);
8603
+ if (fn.name !== "similarity" || args2.length !== 2) {
8604
+ throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
8605
+ }
8606
+ const left = castTo(ctx, args2[0], "text");
8607
+ const right = castTo(ctx, args2[1], "text");
8608
+ if (left.v === null || right.v === null) return tv("float4", null);
8609
+ return tv("float4", trigramSimilarity(left.v, right.v));
8610
+ }
8611
+ function evalTrgmWordSimilar(ctx, left, right) {
8612
+ const missing = () => {
8613
+ throw pgError(
8614
+ "undefined_function",
8615
+ `operator does not exist: ${typeDisplayName(left.t)} <% ${typeDisplayName(right.t)}`
8616
+ );
8617
+ };
8618
+ if (!pgTrgmInstalled(ctx.state)) missing();
8619
+ if (!canImplicitCast(left.t, "text") || !canImplicitCast(right.t, "text")) missing();
8620
+ const a = castTo(ctx, left, "text");
8621
+ const b = castTo(ctx, right, "text");
8622
+ if (a.v === null || b.v === null) return tv("bool", null);
8623
+ const score = wordSimilarity(a.v, b.v);
8624
+ return tv("bool", score >= WORD_SIMILARITY_THRESHOLD);
8625
+ }
8626
+ function assertGinTrgmIndex(state, table, stmt) {
8627
+ const trgmCols = stmt.columns.filter((col2) => isGinTrgmOpclass(col2.opclass));
8628
+ if (trgmCols.length === 0) return;
8629
+ if (trgmCols.length !== stmt.columns.length || stmt.columns.length !== 1) {
8630
+ throw unsupported("multicolumn gin_trgm_ops");
8631
+ }
8632
+ const method = stmt.using ?? "btree";
8633
+ const col = trgmCols[0];
8634
+ const opclass = col.opclass;
8635
+ const label = opclass.join(".");
8636
+ if (method !== "gin" || !opclassVisible(state, opclass)) {
8637
+ throw pgError(
8638
+ "undefined_object",
8639
+ `operator class "${label}" does not exist for access method "${method}"`,
8640
+ "42704"
8641
+ );
8642
+ }
8643
+ if (stmt.unique) {
8644
+ throw pgError("feature_not_supported", `access method "gin" does not support unique indexes`, "0A000");
8645
+ }
8646
+ if (col.expr.type !== "colref" || col.expr.parts.length !== 1) {
8647
+ throw unsupported("expression gin_trgm_ops");
8648
+ }
8649
+ const index = table.columnIndex(col.expr.parts[0]);
8650
+ const typeId = table.columns[index].type.id;
8651
+ if (!GIN_TRGM_TYPES.has(typeId)) {
8652
+ throw pgError(
8653
+ "datatype_mismatch",
8654
+ `operator class "${label}" does not accept data type ${typeDisplayName(typeId)}`,
8655
+ "42804"
8656
+ );
8657
+ }
8658
+ }
8659
+ function isGinTrgmOpclass(opclass) {
8660
+ return opclass !== null && opclass[opclass.length - 1] === "gin_trgm_ops";
8661
+ }
8662
+ function opclassVisible(state, opclass) {
8663
+ const schema = pgTrgmSchema(state);
8664
+ if (schema === null || opclass[opclass.length - 1] !== "gin_trgm_ops") return false;
8665
+ if (opclass.length === 1) return state.effectiveSearchPath().includes(schema);
8666
+ if (opclass.length === 2) return opclass[0] === schema;
8667
+ return false;
8668
+ }
8669
+ function trigramSimilarity(left, right) {
8670
+ const a = uniqueSorted(orderedTrigrams(left));
8671
+ const b = uniqueSorted(orderedTrigrams(right));
8672
+ if (a.length === 0 || b.length === 0) return 0;
8673
+ let i = 0;
8674
+ let j = 0;
8675
+ let count = 0;
8676
+ while (i < a.length && j < b.length) {
8677
+ const cmp = a[i] < b[j] ? -1 : a[i] > b[j] ? 1 : 0;
8678
+ if (cmp < 0) i++;
8679
+ else if (cmp > 0) j++;
8680
+ else {
8681
+ count++;
8682
+ i++;
8683
+ j++;
8684
+ }
8685
+ }
8686
+ return calcSml(count, a.length, b.length);
8687
+ }
8688
+ function wordSimilarity(pattern, haystack) {
8689
+ const trg1 = orderedTrigrams(pattern);
8690
+ const trg2 = orderedTrigrams(haystack);
8691
+ const len1 = trg1.length;
8692
+ const len2 = trg2.length;
8693
+ if (len1 === 0 || len2 === 0) return 0;
8694
+ const positional = [];
8695
+ for (let i = 0; i < len1; i++) positional.push({ trg: trg1[i], index: -1 });
8696
+ for (let i = 0; i < len2; i++) positional.push({ trg: trg2[i], index: i });
8697
+ positional.sort((x, y) => x.trg < y.trg ? -1 : x.trg > y.trg ? 1 : x.index - y.index);
8698
+ const len = len1 + len2;
8699
+ const trg2indexes = new Array(len2);
8700
+ const found = new Array(len).fill(false);
8701
+ let ulen1 = 0;
8702
+ let group = 0;
8703
+ for (let i = 0; i < len; i++) {
8704
+ if (i > 0 && positional[i - 1].trg !== positional[i].trg) {
8705
+ if (found[group]) ulen1++;
8706
+ group++;
8707
+ }
8708
+ const index = positional[i].index;
8709
+ if (index >= 0) trg2indexes[index] = group;
8710
+ else found[group] = true;
8711
+ }
8712
+ if (found[group]) ulen1++;
8713
+ return iterateWordSimilarity(trg2indexes, found, ulen1, len2);
8714
+ }
8715
+ function orderedTrigrams(input) {
8716
+ const out = [];
8717
+ for (const match of input.matchAll(WORD_CHAR)) {
8718
+ const padded = ` ${match[0].toLowerCase()} `;
8719
+ for (let i = 0; i + 3 <= padded.length; i++) out.push(padded.slice(i, i + 3));
8720
+ }
8721
+ return out;
8722
+ }
8723
+ function uniqueSorted(trigrams) {
8724
+ return [...new Set(trigrams)].sort();
8725
+ }
8726
+ function calcSml(count, len1, len2) {
8727
+ const denom = len1 + len2 - count;
8728
+ if (denom <= 0) return 0;
8729
+ return Math.fround(count / denom);
8730
+ }
8731
+ function iterateWordSimilarity(trg2indexes, found, ulen1, len2) {
8732
+ const lastpos = new Array(found.length).fill(-1);
8733
+ let ulen2 = 0;
8734
+ let count = 0;
8735
+ let lower = -1;
8736
+ let smlrMax = 0;
8737
+ for (let i = 0; i < len2; i++) {
8738
+ const trgindex = trg2indexes[i];
8739
+ if (lower >= 0 || found[trgindex]) {
8740
+ if (lastpos[trgindex] < 0) {
8741
+ ulen2++;
8742
+ if (found[trgindex]) count++;
8743
+ }
8744
+ lastpos[trgindex] = i;
8745
+ }
8746
+ if (!found[trgindex]) continue;
8747
+ if (lower === -1) {
8748
+ lower = i;
8749
+ ulen2 = 1;
8750
+ }
8751
+ let smlrCur = calcSml(count, ulen1, ulen2);
8752
+ let tmpCount = count;
8753
+ let tmpUlen2 = ulen2;
8754
+ const prevLower = lower;
8755
+ for (let tmpLower = lower; tmpLower <= i; tmpLower++) {
8756
+ const smlrTmp = calcSml(tmpCount, ulen1, tmpUlen2);
8757
+ if (smlrTmp > smlrCur) {
8758
+ smlrCur = smlrTmp;
8759
+ ulen2 = tmpUlen2;
8760
+ lower = tmpLower;
8761
+ count = tmpCount;
8762
+ }
8763
+ const tmpTrgindex = trg2indexes[tmpLower];
8764
+ if (lastpos[tmpTrgindex] === tmpLower) {
8765
+ tmpUlen2--;
8766
+ if (found[tmpTrgindex]) tmpCount--;
8767
+ }
8768
+ }
8769
+ if (smlrCur > smlrMax) smlrMax = smlrCur;
8770
+ for (let tmpLower = prevLower; tmpLower < lower; tmpLower++) {
8771
+ const tmpTrgindex = trg2indexes[tmpLower];
8772
+ if (lastpos[tmpTrgindex] === tmpLower) lastpos[tmpTrgindex] = -1;
8773
+ }
8774
+ }
8775
+ return smlrMax;
8776
+ }
8777
+
8544
8778
  // src/expressions/operators.ts
8545
8779
  var NUM_LADDER = ["int2", "int4", "int8", "numeric", "float4", "float8"];
8546
8780
  function opNotExist(op, l, r) {
@@ -8684,6 +8918,8 @@ function evalBinary(ctx, op, l0, r0) {
8684
8918
  return evalJsonbDeletePath(ctx, l, r);
8685
8919
  case "@@":
8686
8920
  return evalTextSearchMatch(ctx, l, r);
8921
+ case "<%":
8922
+ return evalTrgmWordSimilar(ctx, l, r);
8687
8923
  default:
8688
8924
  opNotExist(op, l.t, r.t);
8689
8925
  }
@@ -10904,6 +11140,634 @@ function unifyAggType(a, b) {
10904
11140
  return unifyTypes(a, b) ?? a;
10905
11141
  }
10906
11142
 
11143
+ // src/functions/pgcrypto.ts
11144
+ var PGCRYPTO_BODY = "pgcrypto";
11145
+ var utf82 = new TextEncoder();
11146
+ var MD5_S = [
11147
+ 7,
11148
+ 12,
11149
+ 17,
11150
+ 22,
11151
+ 7,
11152
+ 12,
11153
+ 17,
11154
+ 22,
11155
+ 7,
11156
+ 12,
11157
+ 17,
11158
+ 22,
11159
+ 7,
11160
+ 12,
11161
+ 17,
11162
+ 22,
11163
+ 5,
11164
+ 9,
11165
+ 14,
11166
+ 20,
11167
+ 5,
11168
+ 9,
11169
+ 14,
11170
+ 20,
11171
+ 5,
11172
+ 9,
11173
+ 14,
11174
+ 20,
11175
+ 5,
11176
+ 9,
11177
+ 14,
11178
+ 20,
11179
+ 4,
11180
+ 11,
11181
+ 16,
11182
+ 23,
11183
+ 4,
11184
+ 11,
11185
+ 16,
11186
+ 23,
11187
+ 4,
11188
+ 11,
11189
+ 16,
11190
+ 23,
11191
+ 4,
11192
+ 11,
11193
+ 16,
11194
+ 23,
11195
+ 6,
11196
+ 10,
11197
+ 15,
11198
+ 21,
11199
+ 6,
11200
+ 10,
11201
+ 15,
11202
+ 21,
11203
+ 6,
11204
+ 10,
11205
+ 15,
11206
+ 21,
11207
+ 6,
11208
+ 10,
11209
+ 15,
11210
+ 21
11211
+ ];
11212
+ var MD5_K = new Uint32Array([
11213
+ 3614090360,
11214
+ 3905402710,
11215
+ 606105819,
11216
+ 3250441966,
11217
+ 4118548399,
11218
+ 1200080426,
11219
+ 2821735955,
11220
+ 4249261313,
11221
+ 1770035416,
11222
+ 2336552879,
11223
+ 4294925233,
11224
+ 2304563134,
11225
+ 1804603682,
11226
+ 4254626195,
11227
+ 2792965006,
11228
+ 1236535329,
11229
+ 4129170786,
11230
+ 3225465664,
11231
+ 643717713,
11232
+ 3921069994,
11233
+ 3593408605,
11234
+ 38016083,
11235
+ 3634488961,
11236
+ 3889429448,
11237
+ 568446438,
11238
+ 3275163606,
11239
+ 4107603335,
11240
+ 1163531501,
11241
+ 2850285829,
11242
+ 4243563512,
11243
+ 1735328473,
11244
+ 2368359562,
11245
+ 4294588738,
11246
+ 2272392833,
11247
+ 1839030562,
11248
+ 4259657740,
11249
+ 2763975236,
11250
+ 1272893353,
11251
+ 4139469664,
11252
+ 3200236656,
11253
+ 681279174,
11254
+ 3936430074,
11255
+ 3572445317,
11256
+ 76029189,
11257
+ 3654602809,
11258
+ 3873151461,
11259
+ 530742520,
11260
+ 3299628645,
11261
+ 4096336452,
11262
+ 1126891415,
11263
+ 2878612391,
11264
+ 4237533241,
11265
+ 1700485571,
11266
+ 2399980690,
11267
+ 4293915773,
11268
+ 2240044497,
11269
+ 1873313359,
11270
+ 4264355552,
11271
+ 2734768916,
11272
+ 1309151649,
11273
+ 4149444226,
11274
+ 3174756917,
11275
+ 718787259,
11276
+ 3951481745
11277
+ ]);
11278
+ var SHA256_K = new Uint32Array([
11279
+ 1116352408,
11280
+ 1899447441,
11281
+ 3049323471,
11282
+ 3921009573,
11283
+ 961987163,
11284
+ 1508970993,
11285
+ 2453635748,
11286
+ 2870763221,
11287
+ 3624381080,
11288
+ 310598401,
11289
+ 607225278,
11290
+ 1426881987,
11291
+ 1925078388,
11292
+ 2162078206,
11293
+ 2614888103,
11294
+ 3248222580,
11295
+ 3835390401,
11296
+ 4022224774,
11297
+ 264347078,
11298
+ 604807628,
11299
+ 770255983,
11300
+ 1249150122,
11301
+ 1555081692,
11302
+ 1996064986,
11303
+ 2554220882,
11304
+ 2821834349,
11305
+ 2952996808,
11306
+ 3210313671,
11307
+ 3336571891,
11308
+ 3584528711,
11309
+ 113926993,
11310
+ 338241895,
11311
+ 666307205,
11312
+ 773529912,
11313
+ 1294757372,
11314
+ 1396182291,
11315
+ 1695183700,
11316
+ 1986661051,
11317
+ 2177026350,
11318
+ 2456956037,
11319
+ 2730485921,
11320
+ 2820302411,
11321
+ 3259730800,
11322
+ 3345764771,
11323
+ 3516065817,
11324
+ 3600352804,
11325
+ 4094571909,
11326
+ 275423344,
11327
+ 430227734,
11328
+ 506948616,
11329
+ 659060556,
11330
+ 883997877,
11331
+ 958139571,
11332
+ 1322822218,
11333
+ 1537002063,
11334
+ 1747873779,
11335
+ 1955562222,
11336
+ 2024104815,
11337
+ 2227730452,
11338
+ 2361852424,
11339
+ 2428436474,
11340
+ 2756734187,
11341
+ 3204031479,
11342
+ 3329325298
11343
+ ]);
11344
+ var SHA512_K = [
11345
+ 0x428a2f98d728ae22n,
11346
+ 0x7137449123ef65cdn,
11347
+ 0xb5c0fbcfec4d3b2fn,
11348
+ 0xe9b5dba58189dbbcn,
11349
+ 0x3956c25bf348b538n,
11350
+ 0x59f111f1b605d019n,
11351
+ 0x923f82a4af194f9bn,
11352
+ 0xab1c5ed5da6d8118n,
11353
+ 0xd807aa98a3030242n,
11354
+ 0x12835b0145706fben,
11355
+ 0x243185be4ee4b28cn,
11356
+ 0x550c7dc3d5ffb4e2n,
11357
+ 0x72be5d74f27b896fn,
11358
+ 0x80deb1fe3b1696b1n,
11359
+ 0x9bdc06a725c71235n,
11360
+ 0xc19bf174cf692694n,
11361
+ 0xe49b69c19ef14ad2n,
11362
+ 0xefbe4786384f25e3n,
11363
+ 0x0fc19dc68b8cd5b5n,
11364
+ 0x240ca1cc77ac9c65n,
11365
+ 0x2de92c6f592b0275n,
11366
+ 0x4a7484aa6ea6e483n,
11367
+ 0x5cb0a9dcbd41fbd4n,
11368
+ 0x76f988da831153b5n,
11369
+ 0x983e5152ee66dfabn,
11370
+ 0xa831c66d2db43210n,
11371
+ 0xb00327c898fb213fn,
11372
+ 0xbf597fc7beef0ee4n,
11373
+ 0xc6e00bf33da88fc2n,
11374
+ 0xd5a79147930aa725n,
11375
+ 0x06ca6351e003826fn,
11376
+ 0x142929670a0e6e70n,
11377
+ 0x27b70a8546d22ffcn,
11378
+ 0x2e1b21385c26c926n,
11379
+ 0x4d2c6dfc5ac42aedn,
11380
+ 0x53380d139d95b3dfn,
11381
+ 0x650a73548baf63den,
11382
+ 0x766a0abb3c77b2a8n,
11383
+ 0x81c2c92e47edaee6n,
11384
+ 0x92722c851482353bn,
11385
+ 0xa2bfe8a14cf10364n,
11386
+ 0xa81a664bbc423001n,
11387
+ 0xc24b8b70d0f89791n,
11388
+ 0xc76c51a30654be30n,
11389
+ 0xd192e819d6ef5218n,
11390
+ 0xd69906245565a910n,
11391
+ 0xf40e35855771202an,
11392
+ 0x106aa07032bbd1b8n,
11393
+ 0x19a4c116b8d2d0c8n,
11394
+ 0x1e376c085141ab53n,
11395
+ 0x2748774cdf8eeb99n,
11396
+ 0x34b0bcb5e19b48a8n,
11397
+ 0x391c0cb3c5c95a63n,
11398
+ 0x4ed8aa4ae3418acbn,
11399
+ 0x5b9cca4f7763e373n,
11400
+ 0x682e6ff3d6b2b8a3n,
11401
+ 0x748f82ee5defb2fcn,
11402
+ 0x78a5636f43172f60n,
11403
+ 0x84c87814a1f0ab72n,
11404
+ 0x8cc702081a6439ecn,
11405
+ 0x90befffa23631e28n,
11406
+ 0xa4506cebde82bde9n,
11407
+ 0xbef9a3f7b2c67915n,
11408
+ 0xc67178f2e372532bn,
11409
+ 0xca273eceea26619cn,
11410
+ 0xd186b8c721c0c207n,
11411
+ 0xeada7dd6cde0eb1en,
11412
+ 0xf57d4f7fee6ed178n,
11413
+ 0x06f067aa72176fban,
11414
+ 0x0a637dc5a2c898a6n,
11415
+ 0x113f9804bef90daen,
11416
+ 0x1b710b35131c471bn,
11417
+ 0x28db77f523047d84n,
11418
+ 0x32caab7b40c72493n,
11419
+ 0x3c9ebe0a15c9bebcn,
11420
+ 0x431d67c49c100d4cn,
11421
+ 0x4cc5d4becb3e42b6n,
11422
+ 0x597f299cfc657e2an,
11423
+ 0x5fcb6fab3ad6faecn,
11424
+ 0x6c44198c4a475817n
11425
+ ];
11426
+ var MASK64 = (1n << 64n) - 1n;
11427
+ function rotl32(x, n) {
11428
+ return (x << n | x >>> 32 - n) >>> 0;
11429
+ }
11430
+ function rotr32(x, n) {
11431
+ return (x >>> n | x << 32 - n) >>> 0;
11432
+ }
11433
+ function rotr64(x, n) {
11434
+ return (x >> n | x << 64n - n) & MASK64;
11435
+ }
11436
+ function padBe(data, block, lengthBytes) {
11437
+ const bitLen = BigInt(data.length) * 8n;
11438
+ let len = data.length + 1 + lengthBytes;
11439
+ const rem = len % block;
11440
+ if (rem !== 0) len += block - rem;
11441
+ const out = new Uint8Array(len);
11442
+ out.set(data);
11443
+ out[data.length] = 128;
11444
+ const view = new DataView(out.buffer);
11445
+ view.setUint32(out.length - 8, Number(bitLen >> 32n & 0xffffffffn), false);
11446
+ view.setUint32(out.length - 4, Number(bitLen & 0xffffffffn), false);
11447
+ return out;
11448
+ }
11449
+ function padMd5(data) {
11450
+ const bitLen = BigInt(data.length) * 8n;
11451
+ let len = data.length + 1 + 8;
11452
+ const rem = len % 64;
11453
+ if (rem !== 0) len += 64 - rem;
11454
+ const out = new Uint8Array(len);
11455
+ out.set(data);
11456
+ out[data.length] = 128;
11457
+ const view = new DataView(out.buffer);
11458
+ view.setUint32(out.length - 8, Number(bitLen & 0xffffffffn), true);
11459
+ view.setUint32(out.length - 4, Number(bitLen >> 32n & 0xffffffffn), true);
11460
+ return out;
11461
+ }
11462
+ function md5(data) {
11463
+ const bytes = padMd5(data);
11464
+ const view = new DataView(bytes.buffer);
11465
+ let a0 = 1732584193;
11466
+ let b0 = 4023233417;
11467
+ let c0 = 2562383102;
11468
+ let d0 = 271733878;
11469
+ const m = new Uint32Array(16);
11470
+ for (let off = 0; off < bytes.length; off += 64) {
11471
+ for (let i = 0; i < 16; i++) m[i] = view.getUint32(off + i * 4, true);
11472
+ let a = a0;
11473
+ let b = b0;
11474
+ let c = c0;
11475
+ let d = d0;
11476
+ for (let i = 0; i < 64; i++) {
11477
+ let f;
11478
+ let g;
11479
+ if (i < 16) {
11480
+ f = b & c | ~b & d;
11481
+ g = i;
11482
+ } else if (i < 32) {
11483
+ f = d & b | ~d & c;
11484
+ g = (5 * i + 1) % 16;
11485
+ } else if (i < 48) {
11486
+ f = b ^ c ^ d;
11487
+ g = (3 * i + 5) % 16;
11488
+ } else {
11489
+ f = c ^ (b | ~d);
11490
+ g = 7 * i % 16;
11491
+ }
11492
+ const sum = f + a + MD5_K[i] + m[g] >>> 0;
11493
+ a = d;
11494
+ d = c;
11495
+ c = b;
11496
+ b = b + rotl32(sum, MD5_S[i]) >>> 0;
11497
+ }
11498
+ a0 = a0 + a >>> 0;
11499
+ b0 = b0 + b >>> 0;
11500
+ c0 = c0 + c >>> 0;
11501
+ d0 = d0 + d >>> 0;
11502
+ }
11503
+ const out = new Uint8Array(16);
11504
+ const ov = new DataView(out.buffer);
11505
+ ov.setUint32(0, a0, true);
11506
+ ov.setUint32(4, b0, true);
11507
+ ov.setUint32(8, c0, true);
11508
+ ov.setUint32(12, d0, true);
11509
+ return out;
11510
+ }
11511
+ function sha1(data) {
11512
+ const bytes = padBe(data, 64, 8);
11513
+ const view = new DataView(bytes.buffer);
11514
+ let h0 = 1732584193;
11515
+ let h1 = 4023233417;
11516
+ let h2 = 2562383102;
11517
+ let h3 = 271733878;
11518
+ let h4 = 3285377520;
11519
+ const w = new Uint32Array(80);
11520
+ for (let off = 0; off < bytes.length; off += 64) {
11521
+ for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
11522
+ for (let t = 16; t < 80; t++) w[t] = rotl32(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
11523
+ let a = h0;
11524
+ let b = h1;
11525
+ let c = h2;
11526
+ let d = h3;
11527
+ let e = h4;
11528
+ for (let t = 0; t < 80; t++) {
11529
+ let f;
11530
+ let k;
11531
+ if (t < 20) {
11532
+ f = b & c | ~b & d;
11533
+ k = 1518500249;
11534
+ } else if (t < 40) {
11535
+ f = b ^ c ^ d;
11536
+ k = 1859775393;
11537
+ } else if (t < 60) {
11538
+ f = b & c | b & d | c & d;
11539
+ k = 2400959708;
11540
+ } else {
11541
+ f = b ^ c ^ d;
11542
+ k = 3395469782;
11543
+ }
11544
+ const temp = rotl32(a, 5) + f + e + k + w[t] >>> 0;
11545
+ e = d;
11546
+ d = c;
11547
+ c = rotl32(b, 30);
11548
+ b = a;
11549
+ a = temp;
11550
+ }
11551
+ h0 = h0 + a >>> 0;
11552
+ h1 = h1 + b >>> 0;
11553
+ h2 = h2 + c >>> 0;
11554
+ h3 = h3 + d >>> 0;
11555
+ h4 = h4 + e >>> 0;
11556
+ }
11557
+ const out = new Uint8Array(20);
11558
+ const ov = new DataView(out.buffer);
11559
+ ov.setUint32(0, h0, false);
11560
+ ov.setUint32(4, h1, false);
11561
+ ov.setUint32(8, h2, false);
11562
+ ov.setUint32(12, h3, false);
11563
+ ov.setUint32(16, h4, false);
11564
+ return out;
11565
+ }
11566
+ function sha256(data, sha224) {
11567
+ const bytes = padBe(data, 64, 8);
11568
+ const view = new DataView(bytes.buffer);
11569
+ let h0 = sha224 ? 3238371032 : 1779033703;
11570
+ let h1 = sha224 ? 914150663 : 3144134277;
11571
+ let h2 = sha224 ? 812702999 : 1013904242;
11572
+ let h3 = sha224 ? 4144912697 : 2773480762;
11573
+ let h4 = sha224 ? 4290775857 : 1359893119;
11574
+ let h5 = sha224 ? 1750603025 : 2600822924;
11575
+ let h6 = sha224 ? 1694076839 : 528734635;
11576
+ let h7 = sha224 ? 3204075428 : 1541459225;
11577
+ const w = new Uint32Array(64);
11578
+ for (let off = 0; off < bytes.length; off += 64) {
11579
+ for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
11580
+ for (let t = 16; t < 64; t++) {
11581
+ const x15 = w[t - 15];
11582
+ const x2 = w[t - 2];
11583
+ const s0 = rotr32(x15, 7) ^ rotr32(x15, 18) ^ x15 >>> 3;
11584
+ const s1 = rotr32(x2, 17) ^ rotr32(x2, 19) ^ x2 >>> 10;
11585
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 >>> 0;
11586
+ }
11587
+ let a = h0;
11588
+ let b = h1;
11589
+ let c = h2;
11590
+ let d = h3;
11591
+ let e = h4;
11592
+ let f = h5;
11593
+ let g = h6;
11594
+ let h = h7;
11595
+ for (let t = 0; t < 64; t++) {
11596
+ const s1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
11597
+ const ch = e & f ^ ~e & g;
11598
+ const t1 = h + s1 + ch + SHA256_K[t] + w[t] >>> 0;
11599
+ const s0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22);
11600
+ const maj = a & b ^ a & c ^ b & c;
11601
+ const t2 = s0 + maj >>> 0;
11602
+ h = g;
11603
+ g = f;
11604
+ f = e;
11605
+ e = d + t1 >>> 0;
11606
+ d = c;
11607
+ c = b;
11608
+ b = a;
11609
+ a = t1 + t2 >>> 0;
11610
+ }
11611
+ h0 = h0 + a >>> 0;
11612
+ h1 = h1 + b >>> 0;
11613
+ h2 = h2 + c >>> 0;
11614
+ h3 = h3 + d >>> 0;
11615
+ h4 = h4 + e >>> 0;
11616
+ h5 = h5 + f >>> 0;
11617
+ h6 = h6 + g >>> 0;
11618
+ h7 = h7 + h >>> 0;
11619
+ }
11620
+ const words = sha224 ? 7 : 8;
11621
+ const out = new Uint8Array(words * 4);
11622
+ const ov = new DataView(out.buffer);
11623
+ const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
11624
+ for (let i = 0; i < words; i++) ov.setUint32(i * 4, hs[i], false);
11625
+ return out;
11626
+ }
11627
+ function readUint64BE(view, offset) {
11628
+ return BigInt(view.getUint32(offset, false)) << 32n | BigInt(view.getUint32(offset + 4, false));
11629
+ }
11630
+ function sha512(data, sha384) {
11631
+ const bytes = padBe(data, 128, 16);
11632
+ const view = new DataView(bytes.buffer);
11633
+ let h0 = sha384 ? 0xcbbb9d5dc1059ed8n : 0x6a09e667f3bcc908n;
11634
+ let h1 = sha384 ? 0x629a292a367cd507n : 0xbb67ae8584caa73bn;
11635
+ let h2 = sha384 ? 0x9159015a3070dd17n : 0x3c6ef372fe94f82bn;
11636
+ let h3 = sha384 ? 0x152fecd8f70e5939n : 0xa54ff53a5f1d36f1n;
11637
+ let h4 = sha384 ? 0x67332667ffc00b31n : 0x510e527fade682d1n;
11638
+ let h5 = sha384 ? 0x8eb44a8768581511n : 0x9b05688c2b3e6c1fn;
11639
+ let h6 = sha384 ? 0xdb0c2e0d64f98fa7n : 0x1f83d9abfb41bd6bn;
11640
+ let h7 = sha384 ? 0x47b5481dbefa4fa4n : 0x5be0cd19137e2179n;
11641
+ const w = new Array(80);
11642
+ for (let off = 0; off < bytes.length; off += 128) {
11643
+ for (let t = 0; t < 16; t++) w[t] = readUint64BE(view, off + t * 8);
11644
+ for (let t = 16; t < 80; t++) {
11645
+ const x15 = w[t - 15];
11646
+ const x2 = w[t - 2];
11647
+ const s0 = rotr64(x15, 1n) ^ rotr64(x15, 8n) ^ x15 >> 7n;
11648
+ const s1 = rotr64(x2, 19n) ^ rotr64(x2, 61n) ^ x2 >> 6n;
11649
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 & MASK64;
11650
+ }
11651
+ let a = h0;
11652
+ let b = h1;
11653
+ let c = h2;
11654
+ let d = h3;
11655
+ let e = h4;
11656
+ let f = h5;
11657
+ let g = h6;
11658
+ let h = h7;
11659
+ for (let t = 0; t < 80; t++) {
11660
+ const s1 = rotr64(e, 14n) ^ rotr64(e, 18n) ^ rotr64(e, 41n);
11661
+ const ch = e & f ^ ~e & g & MASK64;
11662
+ const t1 = h + s1 + ch + SHA512_K[t] + w[t] & MASK64;
11663
+ const s0 = rotr64(a, 28n) ^ rotr64(a, 34n) ^ rotr64(a, 39n);
11664
+ const maj = a & b ^ a & c ^ b & c;
11665
+ const t2 = s0 + maj & MASK64;
11666
+ h = g;
11667
+ g = f;
11668
+ f = e;
11669
+ e = d + t1 & MASK64;
11670
+ d = c;
11671
+ c = b;
11672
+ b = a;
11673
+ a = t1 + t2 & MASK64;
11674
+ }
11675
+ h0 = h0 + a & MASK64;
11676
+ h1 = h1 + b & MASK64;
11677
+ h2 = h2 + c & MASK64;
11678
+ h3 = h3 + d & MASK64;
11679
+ h4 = h4 + e & MASK64;
11680
+ h5 = h5 + f & MASK64;
11681
+ h6 = h6 + g & MASK64;
11682
+ h7 = h7 + h & MASK64;
11683
+ }
11684
+ const words = sha384 ? 6 : 8;
11685
+ const out = new Uint8Array(words * 8);
11686
+ const ov = new DataView(out.buffer);
11687
+ const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
11688
+ for (let i = 0; i < words; i++) {
11689
+ const word = hs[i];
11690
+ ov.setUint32(i * 8, Number(word >> 32n & 0xffffffffn), false);
11691
+ ov.setUint32(i * 8 + 4, Number(word & 0xffffffffn), false);
11692
+ }
11693
+ return out;
11694
+ }
11695
+ function digestBytes(data, algorithm) {
11696
+ switch (algorithm.toLowerCase().replaceAll("-", "")) {
11697
+ case "md5":
11698
+ return md5(data);
11699
+ case "sha1":
11700
+ return sha1(data);
11701
+ case "sha224":
11702
+ return sha256(data, true);
11703
+ case "sha256":
11704
+ return sha256(data, false);
11705
+ case "sha384":
11706
+ return sha512(data, true);
11707
+ case "sha512":
11708
+ return sha512(data, false);
11709
+ default:
11710
+ throw pgError("invalid_parameter_value", `Cannot use "${algorithm}": No such hash algorithm`, "22023");
11711
+ }
11712
+ }
11713
+ function pgcryptoInstalled(state) {
11714
+ for (const schema of state.schemas.values()) {
11715
+ for (const fns of schema.functions.values()) {
11716
+ if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGCRYPTO_BODY)) return true;
11717
+ }
11718
+ }
11719
+ return false;
11720
+ }
11721
+ function installPgcrypto(state, schemaName) {
11722
+ const schema = state.getSchema(schemaName);
11723
+ const existing = schema.functions.get("digest") ?? [];
11724
+ const arg0Types = ["text", "bytea"];
11725
+ for (const arg0 of arg0Types) {
11726
+ const fn = {
11727
+ name: "digest",
11728
+ schema: schema.name,
11729
+ argNames: [null, null],
11730
+ argTypes: [arg0, "text"],
11731
+ argDefaults: [null, null],
11732
+ returns: "bytea",
11733
+ returnsSet: false,
11734
+ returnsTable: null,
11735
+ language: "internal",
11736
+ body: null,
11737
+ rawBody: PGCRYPTO_BODY,
11738
+ strict: true,
11739
+ oid: state.nextOid()
11740
+ };
11741
+ existing.push(fn);
11742
+ }
11743
+ schema.functions.set("digest", existing);
11744
+ }
11745
+ function uninstallPgcrypto(state) {
11746
+ let removed = false;
11747
+ for (const schema of state.schemas.values()) {
11748
+ for (const [name, fns] of [...schema.functions.entries()]) {
11749
+ const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGCRYPTO_BODY);
11750
+ if (keep.length === fns.length) continue;
11751
+ removed = true;
11752
+ if (keep.length === 0) schema.functions.delete(name);
11753
+ else schema.functions.set(name, keep);
11754
+ }
11755
+ }
11756
+ return removed;
11757
+ }
11758
+ function evalPgcryptoFunction(ctx, fn, args2) {
11759
+ const ret = fn.returns ?? "bytea";
11760
+ if (fn.strict && args2.some((arg) => arg.v === null)) return tv(ret, null);
11761
+ if (fn.name !== "digest" || args2.length !== 2) {
11762
+ throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
11763
+ }
11764
+ const data = castTo(ctx, args2[0], fn.argTypes[0] ?? "bytea", {});
11765
+ const algorithm = castTo(ctx, args2[1], "text", {});
11766
+ if (data.v === null || algorithm.v === null) return tv("bytea", null);
11767
+ const bytes = fn.argTypes[0] === "bytea" ? data.v : utf82.encode(data.v);
11768
+ return tv("bytea", digestBytes(bytes, algorithm.v));
11769
+ }
11770
+
10907
11771
  // src/functions/srf.ts
10908
11772
  function jsonInput(ctx, a) {
10909
11773
  void ctx;
@@ -13097,10 +13961,32 @@ var Parser = class {
13097
13961
  }
13098
13962
  if (this.atKw("extension")) {
13099
13963
  this.pos++;
13100
- this.parseIfNotExists();
13964
+ const ifNotExists = this.parseIfNotExists();
13101
13965
  const name = this.ident(true);
13102
- while (!this.atPunct(";") && this.peek().type !== "eof") this.pos++;
13103
- throw unsupported(`extension "${name}"`);
13966
+ let schema = null;
13967
+ let version = null;
13968
+ let cascade = false;
13969
+ this.eatKw("with");
13970
+ while (!this.atPunct(";") && this.peek().type !== "eof") {
13971
+ if (this.eatKw("schema")) {
13972
+ schema = this.ident(true);
13973
+ continue;
13974
+ }
13975
+ if (this.eatKw("version")) {
13976
+ const token = this.next();
13977
+ if (token.type !== "string" && token.type !== "ident" && token.type !== "quoted_ident") {
13978
+ this.errorAt(token, "expected extension version");
13979
+ }
13980
+ version = token.value;
13981
+ continue;
13982
+ }
13983
+ if (this.eatKw("cascade")) {
13984
+ cascade = true;
13985
+ continue;
13986
+ }
13987
+ throw unsupported("CREATE EXTENSION option");
13988
+ }
13989
+ return { type: "create_extension", name, ifNotExists, schema, version, cascade };
13104
13990
  }
13105
13991
  if (this.atKw("database")) throw unsupported("CREATE DATABASE");
13106
13992
  if (this.atKw("role") || this.atKw("user") || this.atKw("group")) throw unsupported("roles");
@@ -13577,7 +14463,11 @@ var Parser = class {
13577
14463
  let opclass = null;
13578
14464
  const t = this.peek();
13579
14465
  if (t.type === "ident" && !RESERVED.has(t.value) && !["asc", "desc", "nulls", "with", "where", "include"].includes(t.value)) {
13580
- opclass = this.ident();
14466
+ opclass = [this.ident()];
14467
+ while (this.atPunct(".")) {
14468
+ this.pos++;
14469
+ opclass.push(this.ident(true));
14470
+ }
13581
14471
  }
13582
14472
  let dir = null;
13583
14473
  if (this.eatKw("asc")) dir = "asc";
@@ -15813,7 +16703,13 @@ function runStmt(env, stmt, vars, emit, tableNames) {
15813
16703
  return;
15814
16704
  }
15815
16705
  case "for": {
15816
- const forEnv = { ctx: env.ctx, params: env.params, ctes: env.ctes, outer: vars.scope() };
16706
+ const forEnv = {
16707
+ ctx: env.ctx,
16708
+ params: env.params,
16709
+ ctes: env.ctes,
16710
+ outer: vars.scope(),
16711
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
16712
+ };
15817
16713
  const rel2 = executeSelectStmt(forEnv, stmt.query);
15818
16714
  for (const row of rel2.rows) {
15819
16715
  for (let i = 0; i < stmt.targets.length; i++) {
@@ -15855,7 +16751,13 @@ function initVars(env, fn, bound, program) {
15855
16751
  if (fn.returnsTable) {
15856
16752
  for (const c of fn.returnsTable) vars.declare(c.name, c.type, null);
15857
16753
  }
15858
- const seed = () => ({ ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: vars.scope() });
16754
+ const seed = () => ({
16755
+ ctx: env.ctx,
16756
+ params: bound,
16757
+ ctes: /* @__PURE__ */ new Map(),
16758
+ outer: vars.scope(),
16759
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
16760
+ });
15859
16761
  for (const d of program.decls) {
15860
16762
  const id = resolveTypeName(env.ctx.state, d.typeName).column.id;
15861
16763
  let val = null;
@@ -15868,7 +16770,13 @@ function initVars(env, fn, bound, program) {
15868
16770
  return vars;
15869
16771
  }
15870
16772
  function plpgsqlEnv(env, bound, vars) {
15871
- return { ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: vars.scope() };
16773
+ return {
16774
+ ctx: env.ctx,
16775
+ params: bound,
16776
+ ctes: /* @__PURE__ */ new Map(),
16777
+ outer: vars.scope(),
16778
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
16779
+ };
15872
16780
  }
15873
16781
  function callPlpgsqlScalar(env, fn, args2) {
15874
16782
  const retT = fn.returns ?? "text";
@@ -16467,7 +17375,13 @@ function functionEnv(env, fn, args2) {
16467
17375
  bound.push(castTo(env.ctx, evalScalar(env, null, dflt), fn.argTypes[i], {}));
16468
17376
  }
16469
17377
  }
16470
- const fnEnv = { ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: null };
17378
+ const fnEnv = {
17379
+ ctx: env.ctx,
17380
+ params: bound,
17381
+ ctes: /* @__PURE__ */ new Map(),
17382
+ outer: null,
17383
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17384
+ };
16471
17385
  const namedCols = fn.argNames.map((n, i) => ({ n, i })).filter((x) => x.n !== null);
16472
17386
  if (namedCols.length > 0) {
16473
17387
  const cols = namedCols.map(({ n, i }) => ({ name: n, type: fn.argTypes[i] ?? "text", table: null }));
@@ -16493,6 +17407,10 @@ function callSqlFunctionScalar(env, fn, args2) {
16493
17407
  if (fn.language === "plpgsql") {
16494
17408
  return callPlpgsqlScalar(env, fn, args2);
16495
17409
  }
17410
+ if (fn.language === "internal") {
17411
+ if (fn.rawBody === "pg_trgm") return evalPgTrgmFunction(env.ctx, fn, args2);
17412
+ return evalPgcryptoFunction(env.ctx, fn, args2);
17413
+ }
16496
17414
  if (fn.language === "js") {
16497
17415
  if (!fn.jsImpl) {
16498
17416
  throw pgError("undefined_function", `JavaScript function ${fn.name} is not registered`, "42883");
@@ -16569,7 +17487,13 @@ function applyWith(env, w) {
16569
17487
  if (!w) return env;
16570
17488
  const ctes = new Map(env.ctes);
16571
17489
  for (const cte of w.ctes) {
16572
- const scopedEnv = { ctx: env.ctx, params: env.params, ctes, outer: env.outer };
17490
+ const scopedEnv = {
17491
+ ctx: env.ctx,
17492
+ params: env.params,
17493
+ ctes,
17494
+ outer: env.outer,
17495
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17496
+ };
16573
17497
  let rel2;
16574
17498
  if (w.recursive && cte.query.type === "select" && cte.query.body.type === "setop" && cte.query.body.op === "union" && referencesRelation(cte.query.body.right, cte.name)) {
16575
17499
  rel2 = executeRecursiveCte(scopedEnv, cte, cte.query);
@@ -16584,7 +17508,13 @@ function applyWith(env, w) {
16584
17508
  }
16585
17509
  ctes.set(cte.name, applyCteColumnNames(rel2, cte));
16586
17510
  }
16587
- return { ctx: env.ctx, params: env.params, ctes, outer: env.outer };
17511
+ return {
17512
+ ctx: env.ctx,
17513
+ params: env.params,
17514
+ ctes,
17515
+ outer: env.outer,
17516
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17517
+ };
16588
17518
  }
16589
17519
  var MAX_RECURSION_ITERATIONS = 2e5;
16590
17520
  var MAX_RECURSION_ROWS = 5e6;
@@ -16616,7 +17546,13 @@ function executeRecursiveCte(env, cte, query) {
16616
17546
  }
16617
17547
  const iterCtes = new Map(env.ctes);
16618
17548
  iterCtes.set(cte.name, { columns, rows: working });
16619
- const iterEnv = { ctx: env.ctx, params: env.params, ctes: iterCtes, outer: env.outer };
17549
+ const iterEnv = {
17550
+ ctx: env.ctx,
17551
+ params: env.params,
17552
+ ctes: iterCtes,
17553
+ outer: env.outer,
17554
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17555
+ };
16620
17556
  const step = executeBody(iterEnv, body.right);
16621
17557
  if (step.columns.length !== columns.length) {
16622
17558
  throw pgError("syntax", "each UNION query must have the same number of columns", "42601");
@@ -16688,7 +17624,7 @@ function materializeItem(env, item, scope) {
16688
17624
  if (view) {
16689
17625
  let rel2;
16690
17626
  if (view.materialized) {
16691
- if (view.matRows === null || view.matColumns === null) {
17627
+ if (view.matColumns === null || view.matRows === null && !env.allowUnpopulatedMatviews) {
16692
17628
  throw pgError(
16693
17629
  "object_not_in_prerequisite_state",
16694
17630
  `materialized view "${view.name}" has not been populated`,
@@ -16697,10 +17633,16 @@ function materializeItem(env, item, scope) {
16697
17633
  }
16698
17634
  rel2 = {
16699
17635
  columns: view.matColumns.map((c) => ({ name: c.name, type: c.type, table: label })),
16700
- rows: view.matRows
17636
+ rows: view.matRows ?? []
16701
17637
  };
16702
17638
  } else {
16703
- const viewEnv = { ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null };
17639
+ const viewEnv = {
17640
+ ctx: env.ctx,
17641
+ params: null,
17642
+ ctes: /* @__PURE__ */ new Map(),
17643
+ outer: null,
17644
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17645
+ };
16704
17646
  rel2 = executeSelectStmt(viewEnv, view.query);
16705
17647
  rel2 = {
16706
17648
  columns: rel2.columns.map((c, i) => ({
@@ -18835,7 +19777,13 @@ function applyUpdateSets(env, table, sets, scope, row) {
18835
19777
  });
18836
19778
  if (typeof set.value === "object" && "kind" in set.value && set.value.kind === "row_subquery") {
18837
19779
  const rel2 = executeSelectStmt(
18838
- { ctx: env.ctx, params: env.params, ctes: env.ctes, outer: scope },
19780
+ {
19781
+ ctx: env.ctx,
19782
+ params: env.params,
19783
+ ctes: env.ctes,
19784
+ outer: scope,
19785
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
19786
+ },
18839
19787
  set.value.query
18840
19788
  );
18841
19789
  if (rel2.rows.length > 1) {
@@ -20618,6 +21566,9 @@ function executeTrigger(env, table, trigger, _event, oldRow, newRow) {
20618
21566
  setTriggerExecutor(executeTrigger);
20619
21567
 
20620
21568
  // src/executor/ddl.ts
21569
+ function matviewQueryEnv(env, allowUnpopulatedMatviews) {
21570
+ return { ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null, allowUnpopulatedMatviews };
21571
+ }
20621
21572
  function targetSchema(env, parts) {
20622
21573
  const state = env.ctx.state;
20623
21574
  if (parts.length >= 2) {
@@ -21021,6 +21972,7 @@ function executeCreateIndex(env, stmt) {
21021
21972
  }
21022
21973
  }
21023
21974
  }
21975
+ assertGinTrgmIndex(state, table, stmt);
21024
21976
  schema.indexes.set(name, {
21025
21977
  name,
21026
21978
  schema: schema.name,
@@ -21065,7 +22017,7 @@ function executeCreateView(env, stmt) {
21065
22017
  let matRows = null;
21066
22018
  let matColumns = null;
21067
22019
  if (stmt.materialized) {
21068
- const rel2 = executeSelectStmt({ ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null }, stmt.query);
22020
+ const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), stmt.query);
21069
22021
  matColumns = rel2.columns.map((c, i) => ({
21070
22022
  name: stmt.columns?.[i] ?? c.name,
21071
22023
  type: c.type === UNKNOWN ? "text" : c.type
@@ -21091,7 +22043,7 @@ function executeRefreshMatView(env, stmt) {
21091
22043
  throw pgError("undefined_table", `materialized view "${stmt.name.join(".")}" does not exist`, "42P01");
21092
22044
  }
21093
22045
  const view = env.ctx.state.ensureWritableView(found);
21094
- const rel2 = executeSelectStmt({ ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null }, view.query);
22046
+ const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), view.query);
21095
22047
  view.matColumns = rel2.columns.map((c, i) => ({
21096
22048
  name: view.columns?.[i] ?? c.name,
21097
22049
  type: c.type === UNKNOWN ? "text" : c.type
@@ -21099,6 +22051,32 @@ function executeRefreshMatView(env, stmt) {
21099
22051
  view.matRows = stmt.withData ? rel2.rows : null;
21100
22052
  return commandResult("REFRESH MATERIALIZED VIEW", 0);
21101
22053
  }
22054
+ function executeCreateExtension(env, stmt) {
22055
+ const state = env.ctx.state;
22056
+ const schemaName = stmt.schema ?? state.currentSchema();
22057
+ const install = () => {
22058
+ if (stmt.name === "pgcrypto") {
22059
+ if (pgcryptoInstalled(state)) return "exists";
22060
+ installPgcrypto(state, state.getSchema(schemaName).name);
22061
+ return "installed";
22062
+ }
22063
+ if (stmt.name === "pg_trgm") {
22064
+ if (pgTrgmInstalled(state)) return "exists";
22065
+ installPgTrgm(state, state.getSchema(schemaName).name);
22066
+ return "installed";
22067
+ }
22068
+ return "unsupported";
22069
+ };
22070
+ const outcome = install();
22071
+ if (outcome === "unsupported") {
22072
+ throw pgError("feature_not_supported", `extension "${stmt.name}" is not available`, "0A000");
22073
+ }
22074
+ if (outcome === "exists") {
22075
+ if (stmt.ifNotExists) return commandResult("CREATE EXTENSION", 0);
22076
+ throw pgError("duplicate_object", `extension "${stmt.name}" already exists`, "42710");
22077
+ }
22078
+ return commandResult("CREATE EXTENSION", 0);
22079
+ }
21102
22080
  function executeCreateSchema(env, stmt) {
21103
22081
  const state = env.ctx.state;
21104
22082
  if (state.schemas.has(stmt.name)) {
@@ -21887,8 +22865,19 @@ function executeDrop(env, stmt) {
21887
22865
  table.triggers.splice(idx, 1);
21888
22866
  break;
21889
22867
  }
21890
- case "extension":
21891
- break;
22868
+ case "extension": {
22869
+ const ext = parts[parts.length - 1];
22870
+ if (ext === "pgcrypto" && pgcryptoInstalled(state)) {
22871
+ uninstallPgcrypto(state);
22872
+ break;
22873
+ }
22874
+ if (ext === "pg_trgm" && pgTrgmInstalled(state)) {
22875
+ uninstallPgTrgm(state);
22876
+ break;
22877
+ }
22878
+ if (stmt.ifExists) break;
22879
+ throw pgError("undefined_object", `extension "${ext}" does not exist`, "42704");
22880
+ }
21892
22881
  }
21893
22882
  }
21894
22883
  const label = stmt.kind === "materialized_view" ? "MATERIALIZED VIEW" : stmt.kind.toUpperCase();
@@ -21970,6 +22959,8 @@ function executeStatement(env, stmt) {
21970
22959
  return executeAlterSequence(env, stmt);
21971
22960
  case "create_schema":
21972
22961
  return executeCreateSchema(env, stmt);
22962
+ case "create_extension":
22963
+ return executeCreateExtension(env, stmt);
21973
22964
  case "create_enum":
21974
22965
  return executeCreateEnum(env, stmt);
21975
22966
  case "alter_enum":
@@ -22789,7 +23780,7 @@ var FrameParser = class {
22789
23780
  return { kind: "typed", type, body };
22790
23781
  }
22791
23782
  };
22792
- var utf82 = {
23783
+ var utf83 = {
22793
23784
  encode: (s) => encoder2.encode(s),
22794
23785
  decode: (b) => decoder.decode(b)
22795
23786
  };
@@ -23010,13 +24001,13 @@ var binaryValue = (type, text) => {
23010
24001
  return out;
23011
24002
  }
23012
24003
  default:
23013
- return TEXT_TYPES2.has(type) ? utf82.encode(text) : null;
24004
+ return TEXT_TYPES2.has(type) ? utf83.encode(text) : null;
23014
24005
  }
23015
24006
  };
23016
24007
  var binaryEncodable = (type) => TEXT_TYPES2.has(type) || ["bool", "int2", "int4", "oid", "int8", "float4", "float8", "bytea", "uuid"].includes(type);
23017
24008
  var decodeParam = (oid, format, bytes) => {
23018
24009
  if (bytes === null) return null;
23019
- if (format === 0) return utf82.decode(bytes);
24010
+ if (format === 0) return utf83.decode(bytes);
23020
24011
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
23021
24012
  switch (oid) {
23022
24013
  case 16:
@@ -23052,7 +24043,7 @@ var decodeParam = (oid, format, bytes) => {
23052
24043
  case 1042:
23053
24044
  case 1043:
23054
24045
  case 3802:
23055
- return utf82.decode(bytes);
24046
+ return utf83.decode(bytes);
23056
24047
  default:
23057
24048
  throw pgError2("internal", `binary parameter format is not supported for type oid ${oid}`, "0A000");
23058
24049
  }
@@ -23234,7 +24225,7 @@ var Connection = class {
23234
24225
  if (!this.scramStarted) {
23235
24226
  const mechanism = body.cstring();
23236
24227
  const length = body.i32();
23237
- const clientFirst = utf82.decode(body.take(length));
24228
+ const clientFirst = utf83.decode(body.take(length));
23238
24229
  const serverFirst = mechanism === "SCRAM-SHA-256" ? this.scram.first(clientFirst) : null;
23239
24230
  if (serverFirst === null) {
23240
24231
  this.fatal("28000", `SASL authentication with ${mechanism} is not supported or malformed`);
@@ -23244,7 +24235,7 @@ var Connection = class {
23244
24235
  this.write(backend.authenticationSASLContinue(serverFirst));
23245
24236
  return;
23246
24237
  }
23247
- const serverFinal = this.scram.final(utf82.decode(body.rest()));
24238
+ const serverFinal = this.scram.final(utf83.decode(body.rest()));
23248
24239
  if (serverFinal === null) {
23249
24240
  this.fatal("28P01", `password authentication failed for user "${this.user}"`);
23250
24241
  return;
@@ -23492,7 +24483,7 @@ var Connection = class {
23492
24483
  row.map((value, c) => {
23493
24484
  if (value === null) return null;
23494
24485
  const type = result.columnTypes[c];
23495
- return formats[c] === 1 ? binaryValue(type, value) ?? utf82.encode(value) : utf82.encode(value);
24486
+ return formats[c] === 1 ? binaryValue(type, value) ?? utf83.encode(value) : utf83.encode(value);
23496
24487
  })
23497
24488
  )
23498
24489
  );