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