@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/unstable.js CHANGED
@@ -11037,10 +11037,32 @@ var Parser = class {
11037
11037
  }
11038
11038
  if (this.atKw("extension")) {
11039
11039
  this.pos++;
11040
- this.parseIfNotExists();
11040
+ const ifNotExists = this.parseIfNotExists();
11041
11041
  const name = this.ident(true);
11042
- while (!this.atPunct(";") && this.peek().type !== "eof") this.pos++;
11043
- throw unsupported(`extension "${name}"`);
11042
+ let schema = null;
11043
+ let version = null;
11044
+ let cascade = false;
11045
+ this.eatKw("with");
11046
+ while (!this.atPunct(";") && this.peek().type !== "eof") {
11047
+ if (this.eatKw("schema")) {
11048
+ schema = this.ident(true);
11049
+ continue;
11050
+ }
11051
+ if (this.eatKw("version")) {
11052
+ const token = this.next();
11053
+ if (token.type !== "string" && token.type !== "ident" && token.type !== "quoted_ident") {
11054
+ this.errorAt(token, "expected extension version");
11055
+ }
11056
+ version = token.value;
11057
+ continue;
11058
+ }
11059
+ if (this.eatKw("cascade")) {
11060
+ cascade = true;
11061
+ continue;
11062
+ }
11063
+ throw unsupported("CREATE EXTENSION option");
11064
+ }
11065
+ return { type: "create_extension", name, ifNotExists, schema, version, cascade };
11044
11066
  }
11045
11067
  if (this.atKw("database")) throw unsupported("CREATE DATABASE");
11046
11068
  if (this.atKw("role") || this.atKw("user") || this.atKw("group")) throw unsupported("roles");
@@ -11517,7 +11539,11 @@ var Parser = class {
11517
11539
  let opclass = null;
11518
11540
  const t = this.peek();
11519
11541
  if (t.type === "ident" && !RESERVED.has(t.value) && !["asc", "desc", "nulls", "with", "where", "include"].includes(t.value)) {
11520
- opclass = this.ident();
11542
+ opclass = [this.ident()];
11543
+ while (this.atPunct(".")) {
11544
+ this.pos++;
11545
+ opclass.push(this.ident(true));
11546
+ }
11521
11547
  }
11522
11548
  let dir = null;
11523
11549
  if (this.eatKw("asc")) dir = "asc";
@@ -13442,7 +13468,13 @@ var RowScope = class {
13442
13468
  }
13443
13469
  };
13444
13470
  function childEnv(env, outer) {
13445
- return { ctx: env.ctx, params: env.params, ctes: env.ctes, outer };
13471
+ return {
13472
+ ctx: env.ctx,
13473
+ params: env.params,
13474
+ ctes: env.ctes,
13475
+ outer,
13476
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
13477
+ };
13446
13478
  }
13447
13479
  function relationResult(rel2, command) {
13448
13480
  const visible = rel2.columns.map((c, i) => ({ i, c })).filter(({ c }) => !c.hidden);
@@ -13567,6 +13599,234 @@ function datumToJs(t, v, ctx, int8 = "bigint") {
13567
13599
  }
13568
13600
  }
13569
13601
 
13602
+ // src/functions/pgtrgm.ts
13603
+ var PGTRGM_BODY = "pg_trgm";
13604
+ var WORD_SIMILARITY_THRESHOLD = Math.fround(0.6);
13605
+ var GIN_TRGM_TYPES = /* @__PURE__ */ new Set(["text", "varchar"]);
13606
+ var WORD_CHAR = /[\p{L}\p{N}]+/gu;
13607
+ function pgTrgmSchema(state) {
13608
+ for (const schema of state.schemas.values()) {
13609
+ for (const fns of schema.functions.values()) {
13610
+ if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGTRGM_BODY)) return schema.name;
13611
+ }
13612
+ }
13613
+ return null;
13614
+ }
13615
+ function pgTrgmInstalled(state) {
13616
+ return pgTrgmSchema(state) !== null;
13617
+ }
13618
+ function installPgTrgm(state, schemaName) {
13619
+ const schema = state.getSchema(schemaName);
13620
+ const existing = schema.functions.get("similarity") ?? [];
13621
+ const fn = {
13622
+ name: "similarity",
13623
+ schema: schema.name,
13624
+ argNames: [null, null],
13625
+ argTypes: ["text", "text"],
13626
+ argDefaults: [null, null],
13627
+ returns: "float4",
13628
+ returnsSet: false,
13629
+ returnsTable: null,
13630
+ language: "internal",
13631
+ body: null,
13632
+ rawBody: PGTRGM_BODY,
13633
+ strict: true,
13634
+ oid: state.nextOid()
13635
+ };
13636
+ existing.push(fn);
13637
+ schema.functions.set("similarity", existing);
13638
+ }
13639
+ function uninstallPgTrgm(state) {
13640
+ let removed = false;
13641
+ for (const schema of state.schemas.values()) {
13642
+ for (const [name, fns] of [...schema.functions.entries()]) {
13643
+ const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGTRGM_BODY);
13644
+ if (keep.length === fns.length) continue;
13645
+ removed = true;
13646
+ if (keep.length === 0) schema.functions.delete(name);
13647
+ else schema.functions.set(name, keep);
13648
+ }
13649
+ }
13650
+ return removed;
13651
+ }
13652
+ function evalPgTrgmFunction(ctx, fn, args) {
13653
+ const ret = fn.returns ?? "float4";
13654
+ if (fn.strict && args.some((arg) => arg.v === null)) return tv(ret, null);
13655
+ if (fn.name !== "similarity" || args.length !== 2) {
13656
+ throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
13657
+ }
13658
+ const left = castTo(ctx, args[0], "text");
13659
+ const right = castTo(ctx, args[1], "text");
13660
+ if (left.v === null || right.v === null) return tv("float4", null);
13661
+ return tv("float4", trigramSimilarity(left.v, right.v));
13662
+ }
13663
+ function evalTrgmWordSimilar(ctx, left, right) {
13664
+ const missing = () => {
13665
+ throw pgError(
13666
+ "undefined_function",
13667
+ `operator does not exist: ${typeDisplayName(left.t)} <% ${typeDisplayName(right.t)}`
13668
+ );
13669
+ };
13670
+ if (!pgTrgmInstalled(ctx.state)) missing();
13671
+ if (!canImplicitCast(left.t, "text") || !canImplicitCast(right.t, "text")) missing();
13672
+ const a = castTo(ctx, left, "text");
13673
+ const b = castTo(ctx, right, "text");
13674
+ if (a.v === null || b.v === null) return tv("bool", null);
13675
+ const score = wordSimilarity(a.v, b.v);
13676
+ return tv("bool", score >= WORD_SIMILARITY_THRESHOLD);
13677
+ }
13678
+ function assertGinTrgmIndex(state, table, stmt) {
13679
+ const trgmCols = stmt.columns.filter((col2) => isGinTrgmOpclass(col2.opclass));
13680
+ if (trgmCols.length === 0) return;
13681
+ if (trgmCols.length !== stmt.columns.length || stmt.columns.length !== 1) {
13682
+ throw unsupported("multicolumn gin_trgm_ops");
13683
+ }
13684
+ const method = stmt.using ?? "btree";
13685
+ const col = trgmCols[0];
13686
+ const opclass = col.opclass;
13687
+ const label = opclass.join(".");
13688
+ if (method !== "gin" || !opclassVisible(state, opclass)) {
13689
+ throw pgError(
13690
+ "undefined_object",
13691
+ `operator class "${label}" does not exist for access method "${method}"`,
13692
+ "42704"
13693
+ );
13694
+ }
13695
+ if (stmt.unique) {
13696
+ throw pgError("feature_not_supported", `access method "gin" does not support unique indexes`, "0A000");
13697
+ }
13698
+ if (col.expr.type !== "colref" || col.expr.parts.length !== 1) {
13699
+ throw unsupported("expression gin_trgm_ops");
13700
+ }
13701
+ const index = table.columnIndex(col.expr.parts[0]);
13702
+ const typeId = table.columns[index].type.id;
13703
+ if (!GIN_TRGM_TYPES.has(typeId)) {
13704
+ throw pgError(
13705
+ "datatype_mismatch",
13706
+ `operator class "${label}" does not accept data type ${typeDisplayName(typeId)}`,
13707
+ "42804"
13708
+ );
13709
+ }
13710
+ }
13711
+ function isGinTrgmOpclass(opclass) {
13712
+ return opclass !== null && opclass[opclass.length - 1] === "gin_trgm_ops";
13713
+ }
13714
+ function opclassVisible(state, opclass) {
13715
+ const schema = pgTrgmSchema(state);
13716
+ if (schema === null || opclass[opclass.length - 1] !== "gin_trgm_ops") return false;
13717
+ if (opclass.length === 1) return state.effectiveSearchPath().includes(schema);
13718
+ if (opclass.length === 2) return opclass[0] === schema;
13719
+ return false;
13720
+ }
13721
+ function trigramSimilarity(left, right) {
13722
+ const a = uniqueSorted(orderedTrigrams(left));
13723
+ const b = uniqueSorted(orderedTrigrams(right));
13724
+ if (a.length === 0 || b.length === 0) return 0;
13725
+ let i = 0;
13726
+ let j = 0;
13727
+ let count = 0;
13728
+ while (i < a.length && j < b.length) {
13729
+ const cmp = a[i] < b[j] ? -1 : a[i] > b[j] ? 1 : 0;
13730
+ if (cmp < 0) i++;
13731
+ else if (cmp > 0) j++;
13732
+ else {
13733
+ count++;
13734
+ i++;
13735
+ j++;
13736
+ }
13737
+ }
13738
+ return calcSml(count, a.length, b.length);
13739
+ }
13740
+ function wordSimilarity(pattern, haystack) {
13741
+ const trg1 = orderedTrigrams(pattern);
13742
+ const trg2 = orderedTrigrams(haystack);
13743
+ const len1 = trg1.length;
13744
+ const len2 = trg2.length;
13745
+ if (len1 === 0 || len2 === 0) return 0;
13746
+ const positional = [];
13747
+ for (let i = 0; i < len1; i++) positional.push({ trg: trg1[i], index: -1 });
13748
+ for (let i = 0; i < len2; i++) positional.push({ trg: trg2[i], index: i });
13749
+ positional.sort((x, y) => x.trg < y.trg ? -1 : x.trg > y.trg ? 1 : x.index - y.index);
13750
+ const len = len1 + len2;
13751
+ const trg2indexes = new Array(len2);
13752
+ const found = new Array(len).fill(false);
13753
+ let ulen1 = 0;
13754
+ let group = 0;
13755
+ for (let i = 0; i < len; i++) {
13756
+ if (i > 0 && positional[i - 1].trg !== positional[i].trg) {
13757
+ if (found[group]) ulen1++;
13758
+ group++;
13759
+ }
13760
+ const index = positional[i].index;
13761
+ if (index >= 0) trg2indexes[index] = group;
13762
+ else found[group] = true;
13763
+ }
13764
+ if (found[group]) ulen1++;
13765
+ return iterateWordSimilarity(trg2indexes, found, ulen1, len2);
13766
+ }
13767
+ function orderedTrigrams(input) {
13768
+ const out = [];
13769
+ for (const match of input.matchAll(WORD_CHAR)) {
13770
+ const padded = ` ${match[0].toLowerCase()} `;
13771
+ for (let i = 0; i + 3 <= padded.length; i++) out.push(padded.slice(i, i + 3));
13772
+ }
13773
+ return out;
13774
+ }
13775
+ function uniqueSorted(trigrams) {
13776
+ return [...new Set(trigrams)].sort();
13777
+ }
13778
+ function calcSml(count, len1, len2) {
13779
+ const denom = len1 + len2 - count;
13780
+ if (denom <= 0) return 0;
13781
+ return Math.fround(count / denom);
13782
+ }
13783
+ function iterateWordSimilarity(trg2indexes, found, ulen1, len2) {
13784
+ const lastpos = new Array(found.length).fill(-1);
13785
+ let ulen2 = 0;
13786
+ let count = 0;
13787
+ let lower = -1;
13788
+ let smlrMax = 0;
13789
+ for (let i = 0; i < len2; i++) {
13790
+ const trgindex = trg2indexes[i];
13791
+ if (lower >= 0 || found[trgindex]) {
13792
+ if (lastpos[trgindex] < 0) {
13793
+ ulen2++;
13794
+ if (found[trgindex]) count++;
13795
+ }
13796
+ lastpos[trgindex] = i;
13797
+ }
13798
+ if (!found[trgindex]) continue;
13799
+ if (lower === -1) {
13800
+ lower = i;
13801
+ ulen2 = 1;
13802
+ }
13803
+ let smlrCur = calcSml(count, ulen1, ulen2);
13804
+ let tmpCount = count;
13805
+ let tmpUlen2 = ulen2;
13806
+ const prevLower = lower;
13807
+ for (let tmpLower = lower; tmpLower <= i; tmpLower++) {
13808
+ const smlrTmp = calcSml(tmpCount, ulen1, tmpUlen2);
13809
+ if (smlrTmp > smlrCur) {
13810
+ smlrCur = smlrTmp;
13811
+ ulen2 = tmpUlen2;
13812
+ lower = tmpLower;
13813
+ count = tmpCount;
13814
+ }
13815
+ const tmpTrgindex = trg2indexes[tmpLower];
13816
+ if (lastpos[tmpTrgindex] === tmpLower) {
13817
+ tmpUlen2--;
13818
+ if (found[tmpTrgindex]) tmpCount--;
13819
+ }
13820
+ }
13821
+ if (smlrCur > smlrMax) smlrMax = smlrCur;
13822
+ for (let tmpLower = prevLower; tmpLower < lower; tmpLower++) {
13823
+ const tmpTrgindex = trg2indexes[tmpLower];
13824
+ if (lastpos[tmpTrgindex] === tmpLower) lastpos[tmpTrgindex] = -1;
13825
+ }
13826
+ }
13827
+ return smlrMax;
13828
+ }
13829
+
13570
13830
  // src/expressions/operators.ts
13571
13831
  var NUM_LADDER = ["int2", "int4", "int8", "numeric", "float4", "float8"];
13572
13832
  function opNotExist(op, l, r) {
@@ -13710,6 +13970,8 @@ function evalBinary(ctx, op, l0, r0) {
13710
13970
  return evalJsonbDeletePath(ctx, l, r);
13711
13971
  case "@@":
13712
13972
  return evalTextSearchMatch(ctx, l, r);
13973
+ case "<%":
13974
+ return evalTrgmWordSimilar(ctx, l, r);
13713
13975
  default:
13714
13976
  opNotExist(op, l.t, r.t);
13715
13977
  }
@@ -15392,6 +15654,634 @@ function evalLazyFunc(ctx, scope, name, argExprs) {
15392
15654
  }
15393
15655
  }
15394
15656
 
15657
+ // src/functions/pgcrypto.ts
15658
+ var PGCRYPTO_BODY = "pgcrypto";
15659
+ var utf82 = new TextEncoder();
15660
+ var MD5_S = [
15661
+ 7,
15662
+ 12,
15663
+ 17,
15664
+ 22,
15665
+ 7,
15666
+ 12,
15667
+ 17,
15668
+ 22,
15669
+ 7,
15670
+ 12,
15671
+ 17,
15672
+ 22,
15673
+ 7,
15674
+ 12,
15675
+ 17,
15676
+ 22,
15677
+ 5,
15678
+ 9,
15679
+ 14,
15680
+ 20,
15681
+ 5,
15682
+ 9,
15683
+ 14,
15684
+ 20,
15685
+ 5,
15686
+ 9,
15687
+ 14,
15688
+ 20,
15689
+ 5,
15690
+ 9,
15691
+ 14,
15692
+ 20,
15693
+ 4,
15694
+ 11,
15695
+ 16,
15696
+ 23,
15697
+ 4,
15698
+ 11,
15699
+ 16,
15700
+ 23,
15701
+ 4,
15702
+ 11,
15703
+ 16,
15704
+ 23,
15705
+ 4,
15706
+ 11,
15707
+ 16,
15708
+ 23,
15709
+ 6,
15710
+ 10,
15711
+ 15,
15712
+ 21,
15713
+ 6,
15714
+ 10,
15715
+ 15,
15716
+ 21,
15717
+ 6,
15718
+ 10,
15719
+ 15,
15720
+ 21,
15721
+ 6,
15722
+ 10,
15723
+ 15,
15724
+ 21
15725
+ ];
15726
+ var MD5_K = new Uint32Array([
15727
+ 3614090360,
15728
+ 3905402710,
15729
+ 606105819,
15730
+ 3250441966,
15731
+ 4118548399,
15732
+ 1200080426,
15733
+ 2821735955,
15734
+ 4249261313,
15735
+ 1770035416,
15736
+ 2336552879,
15737
+ 4294925233,
15738
+ 2304563134,
15739
+ 1804603682,
15740
+ 4254626195,
15741
+ 2792965006,
15742
+ 1236535329,
15743
+ 4129170786,
15744
+ 3225465664,
15745
+ 643717713,
15746
+ 3921069994,
15747
+ 3593408605,
15748
+ 38016083,
15749
+ 3634488961,
15750
+ 3889429448,
15751
+ 568446438,
15752
+ 3275163606,
15753
+ 4107603335,
15754
+ 1163531501,
15755
+ 2850285829,
15756
+ 4243563512,
15757
+ 1735328473,
15758
+ 2368359562,
15759
+ 4294588738,
15760
+ 2272392833,
15761
+ 1839030562,
15762
+ 4259657740,
15763
+ 2763975236,
15764
+ 1272893353,
15765
+ 4139469664,
15766
+ 3200236656,
15767
+ 681279174,
15768
+ 3936430074,
15769
+ 3572445317,
15770
+ 76029189,
15771
+ 3654602809,
15772
+ 3873151461,
15773
+ 530742520,
15774
+ 3299628645,
15775
+ 4096336452,
15776
+ 1126891415,
15777
+ 2878612391,
15778
+ 4237533241,
15779
+ 1700485571,
15780
+ 2399980690,
15781
+ 4293915773,
15782
+ 2240044497,
15783
+ 1873313359,
15784
+ 4264355552,
15785
+ 2734768916,
15786
+ 1309151649,
15787
+ 4149444226,
15788
+ 3174756917,
15789
+ 718787259,
15790
+ 3951481745
15791
+ ]);
15792
+ var SHA256_K = new Uint32Array([
15793
+ 1116352408,
15794
+ 1899447441,
15795
+ 3049323471,
15796
+ 3921009573,
15797
+ 961987163,
15798
+ 1508970993,
15799
+ 2453635748,
15800
+ 2870763221,
15801
+ 3624381080,
15802
+ 310598401,
15803
+ 607225278,
15804
+ 1426881987,
15805
+ 1925078388,
15806
+ 2162078206,
15807
+ 2614888103,
15808
+ 3248222580,
15809
+ 3835390401,
15810
+ 4022224774,
15811
+ 264347078,
15812
+ 604807628,
15813
+ 770255983,
15814
+ 1249150122,
15815
+ 1555081692,
15816
+ 1996064986,
15817
+ 2554220882,
15818
+ 2821834349,
15819
+ 2952996808,
15820
+ 3210313671,
15821
+ 3336571891,
15822
+ 3584528711,
15823
+ 113926993,
15824
+ 338241895,
15825
+ 666307205,
15826
+ 773529912,
15827
+ 1294757372,
15828
+ 1396182291,
15829
+ 1695183700,
15830
+ 1986661051,
15831
+ 2177026350,
15832
+ 2456956037,
15833
+ 2730485921,
15834
+ 2820302411,
15835
+ 3259730800,
15836
+ 3345764771,
15837
+ 3516065817,
15838
+ 3600352804,
15839
+ 4094571909,
15840
+ 275423344,
15841
+ 430227734,
15842
+ 506948616,
15843
+ 659060556,
15844
+ 883997877,
15845
+ 958139571,
15846
+ 1322822218,
15847
+ 1537002063,
15848
+ 1747873779,
15849
+ 1955562222,
15850
+ 2024104815,
15851
+ 2227730452,
15852
+ 2361852424,
15853
+ 2428436474,
15854
+ 2756734187,
15855
+ 3204031479,
15856
+ 3329325298
15857
+ ]);
15858
+ var SHA512_K = [
15859
+ 0x428a2f98d728ae22n,
15860
+ 0x7137449123ef65cdn,
15861
+ 0xb5c0fbcfec4d3b2fn,
15862
+ 0xe9b5dba58189dbbcn,
15863
+ 0x3956c25bf348b538n,
15864
+ 0x59f111f1b605d019n,
15865
+ 0x923f82a4af194f9bn,
15866
+ 0xab1c5ed5da6d8118n,
15867
+ 0xd807aa98a3030242n,
15868
+ 0x12835b0145706fben,
15869
+ 0x243185be4ee4b28cn,
15870
+ 0x550c7dc3d5ffb4e2n,
15871
+ 0x72be5d74f27b896fn,
15872
+ 0x80deb1fe3b1696b1n,
15873
+ 0x9bdc06a725c71235n,
15874
+ 0xc19bf174cf692694n,
15875
+ 0xe49b69c19ef14ad2n,
15876
+ 0xefbe4786384f25e3n,
15877
+ 0x0fc19dc68b8cd5b5n,
15878
+ 0x240ca1cc77ac9c65n,
15879
+ 0x2de92c6f592b0275n,
15880
+ 0x4a7484aa6ea6e483n,
15881
+ 0x5cb0a9dcbd41fbd4n,
15882
+ 0x76f988da831153b5n,
15883
+ 0x983e5152ee66dfabn,
15884
+ 0xa831c66d2db43210n,
15885
+ 0xb00327c898fb213fn,
15886
+ 0xbf597fc7beef0ee4n,
15887
+ 0xc6e00bf33da88fc2n,
15888
+ 0xd5a79147930aa725n,
15889
+ 0x06ca6351e003826fn,
15890
+ 0x142929670a0e6e70n,
15891
+ 0x27b70a8546d22ffcn,
15892
+ 0x2e1b21385c26c926n,
15893
+ 0x4d2c6dfc5ac42aedn,
15894
+ 0x53380d139d95b3dfn,
15895
+ 0x650a73548baf63den,
15896
+ 0x766a0abb3c77b2a8n,
15897
+ 0x81c2c92e47edaee6n,
15898
+ 0x92722c851482353bn,
15899
+ 0xa2bfe8a14cf10364n,
15900
+ 0xa81a664bbc423001n,
15901
+ 0xc24b8b70d0f89791n,
15902
+ 0xc76c51a30654be30n,
15903
+ 0xd192e819d6ef5218n,
15904
+ 0xd69906245565a910n,
15905
+ 0xf40e35855771202an,
15906
+ 0x106aa07032bbd1b8n,
15907
+ 0x19a4c116b8d2d0c8n,
15908
+ 0x1e376c085141ab53n,
15909
+ 0x2748774cdf8eeb99n,
15910
+ 0x34b0bcb5e19b48a8n,
15911
+ 0x391c0cb3c5c95a63n,
15912
+ 0x4ed8aa4ae3418acbn,
15913
+ 0x5b9cca4f7763e373n,
15914
+ 0x682e6ff3d6b2b8a3n,
15915
+ 0x748f82ee5defb2fcn,
15916
+ 0x78a5636f43172f60n,
15917
+ 0x84c87814a1f0ab72n,
15918
+ 0x8cc702081a6439ecn,
15919
+ 0x90befffa23631e28n,
15920
+ 0xa4506cebde82bde9n,
15921
+ 0xbef9a3f7b2c67915n,
15922
+ 0xc67178f2e372532bn,
15923
+ 0xca273eceea26619cn,
15924
+ 0xd186b8c721c0c207n,
15925
+ 0xeada7dd6cde0eb1en,
15926
+ 0xf57d4f7fee6ed178n,
15927
+ 0x06f067aa72176fban,
15928
+ 0x0a637dc5a2c898a6n,
15929
+ 0x113f9804bef90daen,
15930
+ 0x1b710b35131c471bn,
15931
+ 0x28db77f523047d84n,
15932
+ 0x32caab7b40c72493n,
15933
+ 0x3c9ebe0a15c9bebcn,
15934
+ 0x431d67c49c100d4cn,
15935
+ 0x4cc5d4becb3e42b6n,
15936
+ 0x597f299cfc657e2an,
15937
+ 0x5fcb6fab3ad6faecn,
15938
+ 0x6c44198c4a475817n
15939
+ ];
15940
+ var MASK64 = (1n << 64n) - 1n;
15941
+ function rotl32(x, n) {
15942
+ return (x << n | x >>> 32 - n) >>> 0;
15943
+ }
15944
+ function rotr32(x, n) {
15945
+ return (x >>> n | x << 32 - n) >>> 0;
15946
+ }
15947
+ function rotr64(x, n) {
15948
+ return (x >> n | x << 64n - n) & MASK64;
15949
+ }
15950
+ function padBe(data, block, lengthBytes) {
15951
+ const bitLen = BigInt(data.length) * 8n;
15952
+ let len = data.length + 1 + lengthBytes;
15953
+ const rem = len % block;
15954
+ if (rem !== 0) len += block - rem;
15955
+ const out = new Uint8Array(len);
15956
+ out.set(data);
15957
+ out[data.length] = 128;
15958
+ const view = new DataView(out.buffer);
15959
+ view.setUint32(out.length - 8, Number(bitLen >> 32n & 0xffffffffn), false);
15960
+ view.setUint32(out.length - 4, Number(bitLen & 0xffffffffn), false);
15961
+ return out;
15962
+ }
15963
+ function padMd5(data) {
15964
+ const bitLen = BigInt(data.length) * 8n;
15965
+ let len = data.length + 1 + 8;
15966
+ const rem = len % 64;
15967
+ if (rem !== 0) len += 64 - rem;
15968
+ const out = new Uint8Array(len);
15969
+ out.set(data);
15970
+ out[data.length] = 128;
15971
+ const view = new DataView(out.buffer);
15972
+ view.setUint32(out.length - 8, Number(bitLen & 0xffffffffn), true);
15973
+ view.setUint32(out.length - 4, Number(bitLen >> 32n & 0xffffffffn), true);
15974
+ return out;
15975
+ }
15976
+ function md5(data) {
15977
+ const bytes = padMd5(data);
15978
+ const view = new DataView(bytes.buffer);
15979
+ let a0 = 1732584193;
15980
+ let b0 = 4023233417;
15981
+ let c0 = 2562383102;
15982
+ let d0 = 271733878;
15983
+ const m = new Uint32Array(16);
15984
+ for (let off = 0; off < bytes.length; off += 64) {
15985
+ for (let i = 0; i < 16; i++) m[i] = view.getUint32(off + i * 4, true);
15986
+ let a = a0;
15987
+ let b = b0;
15988
+ let c = c0;
15989
+ let d = d0;
15990
+ for (let i = 0; i < 64; i++) {
15991
+ let f;
15992
+ let g;
15993
+ if (i < 16) {
15994
+ f = b & c | ~b & d;
15995
+ g = i;
15996
+ } else if (i < 32) {
15997
+ f = d & b | ~d & c;
15998
+ g = (5 * i + 1) % 16;
15999
+ } else if (i < 48) {
16000
+ f = b ^ c ^ d;
16001
+ g = (3 * i + 5) % 16;
16002
+ } else {
16003
+ f = c ^ (b | ~d);
16004
+ g = 7 * i % 16;
16005
+ }
16006
+ const sum = f + a + MD5_K[i] + m[g] >>> 0;
16007
+ a = d;
16008
+ d = c;
16009
+ c = b;
16010
+ b = b + rotl32(sum, MD5_S[i]) >>> 0;
16011
+ }
16012
+ a0 = a0 + a >>> 0;
16013
+ b0 = b0 + b >>> 0;
16014
+ c0 = c0 + c >>> 0;
16015
+ d0 = d0 + d >>> 0;
16016
+ }
16017
+ const out = new Uint8Array(16);
16018
+ const ov = new DataView(out.buffer);
16019
+ ov.setUint32(0, a0, true);
16020
+ ov.setUint32(4, b0, true);
16021
+ ov.setUint32(8, c0, true);
16022
+ ov.setUint32(12, d0, true);
16023
+ return out;
16024
+ }
16025
+ function sha1(data) {
16026
+ const bytes = padBe(data, 64, 8);
16027
+ const view = new DataView(bytes.buffer);
16028
+ let h0 = 1732584193;
16029
+ let h1 = 4023233417;
16030
+ let h2 = 2562383102;
16031
+ let h3 = 271733878;
16032
+ let h4 = 3285377520;
16033
+ const w = new Uint32Array(80);
16034
+ for (let off = 0; off < bytes.length; off += 64) {
16035
+ for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
16036
+ for (let t = 16; t < 80; t++) w[t] = rotl32(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
16037
+ let a = h0;
16038
+ let b = h1;
16039
+ let c = h2;
16040
+ let d = h3;
16041
+ let e = h4;
16042
+ for (let t = 0; t < 80; t++) {
16043
+ let f;
16044
+ let k;
16045
+ if (t < 20) {
16046
+ f = b & c | ~b & d;
16047
+ k = 1518500249;
16048
+ } else if (t < 40) {
16049
+ f = b ^ c ^ d;
16050
+ k = 1859775393;
16051
+ } else if (t < 60) {
16052
+ f = b & c | b & d | c & d;
16053
+ k = 2400959708;
16054
+ } else {
16055
+ f = b ^ c ^ d;
16056
+ k = 3395469782;
16057
+ }
16058
+ const temp = rotl32(a, 5) + f + e + k + w[t] >>> 0;
16059
+ e = d;
16060
+ d = c;
16061
+ c = rotl32(b, 30);
16062
+ b = a;
16063
+ a = temp;
16064
+ }
16065
+ h0 = h0 + a >>> 0;
16066
+ h1 = h1 + b >>> 0;
16067
+ h2 = h2 + c >>> 0;
16068
+ h3 = h3 + d >>> 0;
16069
+ h4 = h4 + e >>> 0;
16070
+ }
16071
+ const out = new Uint8Array(20);
16072
+ const ov = new DataView(out.buffer);
16073
+ ov.setUint32(0, h0, false);
16074
+ ov.setUint32(4, h1, false);
16075
+ ov.setUint32(8, h2, false);
16076
+ ov.setUint32(12, h3, false);
16077
+ ov.setUint32(16, h4, false);
16078
+ return out;
16079
+ }
16080
+ function sha256(data, sha224) {
16081
+ const bytes = padBe(data, 64, 8);
16082
+ const view = new DataView(bytes.buffer);
16083
+ let h0 = sha224 ? 3238371032 : 1779033703;
16084
+ let h1 = sha224 ? 914150663 : 3144134277;
16085
+ let h2 = sha224 ? 812702999 : 1013904242;
16086
+ let h3 = sha224 ? 4144912697 : 2773480762;
16087
+ let h4 = sha224 ? 4290775857 : 1359893119;
16088
+ let h5 = sha224 ? 1750603025 : 2600822924;
16089
+ let h6 = sha224 ? 1694076839 : 528734635;
16090
+ let h7 = sha224 ? 3204075428 : 1541459225;
16091
+ const w = new Uint32Array(64);
16092
+ for (let off = 0; off < bytes.length; off += 64) {
16093
+ for (let t = 0; t < 16; t++) w[t] = view.getUint32(off + t * 4, false);
16094
+ for (let t = 16; t < 64; t++) {
16095
+ const x15 = w[t - 15];
16096
+ const x2 = w[t - 2];
16097
+ const s0 = rotr32(x15, 7) ^ rotr32(x15, 18) ^ x15 >>> 3;
16098
+ const s1 = rotr32(x2, 17) ^ rotr32(x2, 19) ^ x2 >>> 10;
16099
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 >>> 0;
16100
+ }
16101
+ let a = h0;
16102
+ let b = h1;
16103
+ let c = h2;
16104
+ let d = h3;
16105
+ let e = h4;
16106
+ let f = h5;
16107
+ let g = h6;
16108
+ let h = h7;
16109
+ for (let t = 0; t < 64; t++) {
16110
+ const s1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25);
16111
+ const ch = e & f ^ ~e & g;
16112
+ const t1 = h + s1 + ch + SHA256_K[t] + w[t] >>> 0;
16113
+ const s0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22);
16114
+ const maj = a & b ^ a & c ^ b & c;
16115
+ const t2 = s0 + maj >>> 0;
16116
+ h = g;
16117
+ g = f;
16118
+ f = e;
16119
+ e = d + t1 >>> 0;
16120
+ d = c;
16121
+ c = b;
16122
+ b = a;
16123
+ a = t1 + t2 >>> 0;
16124
+ }
16125
+ h0 = h0 + a >>> 0;
16126
+ h1 = h1 + b >>> 0;
16127
+ h2 = h2 + c >>> 0;
16128
+ h3 = h3 + d >>> 0;
16129
+ h4 = h4 + e >>> 0;
16130
+ h5 = h5 + f >>> 0;
16131
+ h6 = h6 + g >>> 0;
16132
+ h7 = h7 + h >>> 0;
16133
+ }
16134
+ const words = sha224 ? 7 : 8;
16135
+ const out = new Uint8Array(words * 4);
16136
+ const ov = new DataView(out.buffer);
16137
+ const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
16138
+ for (let i = 0; i < words; i++) ov.setUint32(i * 4, hs[i], false);
16139
+ return out;
16140
+ }
16141
+ function readUint64BE(view, offset) {
16142
+ return BigInt(view.getUint32(offset, false)) << 32n | BigInt(view.getUint32(offset + 4, false));
16143
+ }
16144
+ function sha512(data, sha384) {
16145
+ const bytes = padBe(data, 128, 16);
16146
+ const view = new DataView(bytes.buffer);
16147
+ let h0 = sha384 ? 0xcbbb9d5dc1059ed8n : 0x6a09e667f3bcc908n;
16148
+ let h1 = sha384 ? 0x629a292a367cd507n : 0xbb67ae8584caa73bn;
16149
+ let h2 = sha384 ? 0x9159015a3070dd17n : 0x3c6ef372fe94f82bn;
16150
+ let h3 = sha384 ? 0x152fecd8f70e5939n : 0xa54ff53a5f1d36f1n;
16151
+ let h4 = sha384 ? 0x67332667ffc00b31n : 0x510e527fade682d1n;
16152
+ let h5 = sha384 ? 0x8eb44a8768581511n : 0x9b05688c2b3e6c1fn;
16153
+ let h6 = sha384 ? 0xdb0c2e0d64f98fa7n : 0x1f83d9abfb41bd6bn;
16154
+ let h7 = sha384 ? 0x47b5481dbefa4fa4n : 0x5be0cd19137e2179n;
16155
+ const w = new Array(80);
16156
+ for (let off = 0; off < bytes.length; off += 128) {
16157
+ for (let t = 0; t < 16; t++) w[t] = readUint64BE(view, off + t * 8);
16158
+ for (let t = 16; t < 80; t++) {
16159
+ const x15 = w[t - 15];
16160
+ const x2 = w[t - 2];
16161
+ const s0 = rotr64(x15, 1n) ^ rotr64(x15, 8n) ^ x15 >> 7n;
16162
+ const s1 = rotr64(x2, 19n) ^ rotr64(x2, 61n) ^ x2 >> 6n;
16163
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 & MASK64;
16164
+ }
16165
+ let a = h0;
16166
+ let b = h1;
16167
+ let c = h2;
16168
+ let d = h3;
16169
+ let e = h4;
16170
+ let f = h5;
16171
+ let g = h6;
16172
+ let h = h7;
16173
+ for (let t = 0; t < 80; t++) {
16174
+ const s1 = rotr64(e, 14n) ^ rotr64(e, 18n) ^ rotr64(e, 41n);
16175
+ const ch = e & f ^ ~e & g & MASK64;
16176
+ const t1 = h + s1 + ch + SHA512_K[t] + w[t] & MASK64;
16177
+ const s0 = rotr64(a, 28n) ^ rotr64(a, 34n) ^ rotr64(a, 39n);
16178
+ const maj = a & b ^ a & c ^ b & c;
16179
+ const t2 = s0 + maj & MASK64;
16180
+ h = g;
16181
+ g = f;
16182
+ f = e;
16183
+ e = d + t1 & MASK64;
16184
+ d = c;
16185
+ c = b;
16186
+ b = a;
16187
+ a = t1 + t2 & MASK64;
16188
+ }
16189
+ h0 = h0 + a & MASK64;
16190
+ h1 = h1 + b & MASK64;
16191
+ h2 = h2 + c & MASK64;
16192
+ h3 = h3 + d & MASK64;
16193
+ h4 = h4 + e & MASK64;
16194
+ h5 = h5 + f & MASK64;
16195
+ h6 = h6 + g & MASK64;
16196
+ h7 = h7 + h & MASK64;
16197
+ }
16198
+ const words = sha384 ? 6 : 8;
16199
+ const out = new Uint8Array(words * 8);
16200
+ const ov = new DataView(out.buffer);
16201
+ const hs = [h0, h1, h2, h3, h4, h5, h6, h7];
16202
+ for (let i = 0; i < words; i++) {
16203
+ const word = hs[i];
16204
+ ov.setUint32(i * 8, Number(word >> 32n & 0xffffffffn), false);
16205
+ ov.setUint32(i * 8 + 4, Number(word & 0xffffffffn), false);
16206
+ }
16207
+ return out;
16208
+ }
16209
+ function digestBytes(data, algorithm) {
16210
+ switch (algorithm.toLowerCase().replaceAll("-", "")) {
16211
+ case "md5":
16212
+ return md5(data);
16213
+ case "sha1":
16214
+ return sha1(data);
16215
+ case "sha224":
16216
+ return sha256(data, true);
16217
+ case "sha256":
16218
+ return sha256(data, false);
16219
+ case "sha384":
16220
+ return sha512(data, true);
16221
+ case "sha512":
16222
+ return sha512(data, false);
16223
+ default:
16224
+ throw pgError("invalid_parameter_value", `Cannot use "${algorithm}": No such hash algorithm`, "22023");
16225
+ }
16226
+ }
16227
+ function pgcryptoInstalled(state) {
16228
+ for (const schema of state.schemas.values()) {
16229
+ for (const fns of schema.functions.values()) {
16230
+ if (fns.some((fn) => fn.language === "internal" && fn.rawBody === PGCRYPTO_BODY)) return true;
16231
+ }
16232
+ }
16233
+ return false;
16234
+ }
16235
+ function installPgcrypto(state, schemaName) {
16236
+ const schema = state.getSchema(schemaName);
16237
+ const existing = schema.functions.get("digest") ?? [];
16238
+ const arg0Types = ["text", "bytea"];
16239
+ for (const arg0 of arg0Types) {
16240
+ const fn = {
16241
+ name: "digest",
16242
+ schema: schema.name,
16243
+ argNames: [null, null],
16244
+ argTypes: [arg0, "text"],
16245
+ argDefaults: [null, null],
16246
+ returns: "bytea",
16247
+ returnsSet: false,
16248
+ returnsTable: null,
16249
+ language: "internal",
16250
+ body: null,
16251
+ rawBody: PGCRYPTO_BODY,
16252
+ strict: true,
16253
+ oid: state.nextOid()
16254
+ };
16255
+ existing.push(fn);
16256
+ }
16257
+ schema.functions.set("digest", existing);
16258
+ }
16259
+ function uninstallPgcrypto(state) {
16260
+ let removed = false;
16261
+ for (const schema of state.schemas.values()) {
16262
+ for (const [name, fns] of [...schema.functions.entries()]) {
16263
+ const keep = fns.filter((fn) => fn.language !== "internal" || fn.rawBody !== PGCRYPTO_BODY);
16264
+ if (keep.length === fns.length) continue;
16265
+ removed = true;
16266
+ if (keep.length === 0) schema.functions.delete(name);
16267
+ else schema.functions.set(name, keep);
16268
+ }
16269
+ }
16270
+ return removed;
16271
+ }
16272
+ function evalPgcryptoFunction(ctx, fn, args) {
16273
+ const ret = fn.returns ?? "bytea";
16274
+ if (fn.strict && args.some((arg) => arg.v === null)) return tv(ret, null);
16275
+ if (fn.name !== "digest" || args.length !== 2) {
16276
+ throw pgError("undefined_function", `function ${fn.schema}.${fn.name} is not implemented`, "42883");
16277
+ }
16278
+ const data = castTo(ctx, args[0], fn.argTypes[0] ?? "bytea", {});
16279
+ const algorithm = castTo(ctx, args[1], "text", {});
16280
+ if (data.v === null || algorithm.v === null) return tv("bytea", null);
16281
+ const bytes = fn.argTypes[0] === "bytea" ? data.v : utf82.encode(data.v);
16282
+ return tv("bytea", digestBytes(bytes, algorithm.v));
16283
+ }
16284
+
15395
16285
  // src/indexes/index.ts
15396
16286
  var IndexStore = class _IndexStore {
15397
16287
  name;
@@ -16296,7 +17186,13 @@ function functionEnv(env, fn, args) {
16296
17186
  bound.push(castTo(env.ctx, evalScalar(env, null, dflt), fn.argTypes[i], {}));
16297
17187
  }
16298
17188
  }
16299
- const fnEnv = { ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: null };
17189
+ const fnEnv = {
17190
+ ctx: env.ctx,
17191
+ params: bound,
17192
+ ctes: /* @__PURE__ */ new Map(),
17193
+ outer: null,
17194
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17195
+ };
16300
17196
  const namedCols = fn.argNames.map((n, i) => ({ n, i })).filter((x) => x.n !== null);
16301
17197
  if (namedCols.length > 0) {
16302
17198
  const cols = namedCols.map(({ n, i }) => ({ name: n, type: fn.argTypes[i] ?? "text", table: null }));
@@ -16322,6 +17218,10 @@ function callSqlFunctionScalar(env, fn, args) {
16322
17218
  if (fn.language === "plpgsql") {
16323
17219
  return callPlpgsqlScalar(env, fn, args);
16324
17220
  }
17221
+ if (fn.language === "internal") {
17222
+ if (fn.rawBody === "pg_trgm") return evalPgTrgmFunction(env.ctx, fn, args);
17223
+ return evalPgcryptoFunction(env.ctx, fn, args);
17224
+ }
16325
17225
  if (fn.language === "js") {
16326
17226
  if (!fn.jsImpl) {
16327
17227
  throw pgError("undefined_function", `JavaScript function ${fn.name} is not registered`, "42883");
@@ -16398,7 +17298,13 @@ function applyWith(env, w) {
16398
17298
  if (!w) return env;
16399
17299
  const ctes = new Map(env.ctes);
16400
17300
  for (const cte of w.ctes) {
16401
- const scopedEnv = { ctx: env.ctx, params: env.params, ctes, outer: env.outer };
17301
+ const scopedEnv = {
17302
+ ctx: env.ctx,
17303
+ params: env.params,
17304
+ ctes,
17305
+ outer: env.outer,
17306
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17307
+ };
16402
17308
  let rel2;
16403
17309
  if (w.recursive && cte.query.type === "select" && cte.query.body.type === "setop" && cte.query.body.op === "union" && referencesRelation(cte.query.body.right, cte.name)) {
16404
17310
  rel2 = executeRecursiveCte(scopedEnv, cte, cte.query);
@@ -16413,7 +17319,13 @@ function applyWith(env, w) {
16413
17319
  }
16414
17320
  ctes.set(cte.name, applyCteColumnNames(rel2, cte));
16415
17321
  }
16416
- return { ctx: env.ctx, params: env.params, ctes, outer: env.outer };
17322
+ return {
17323
+ ctx: env.ctx,
17324
+ params: env.params,
17325
+ ctes,
17326
+ outer: env.outer,
17327
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17328
+ };
16417
17329
  }
16418
17330
  var MAX_RECURSION_ITERATIONS = 2e5;
16419
17331
  var MAX_RECURSION_ROWS = 5e6;
@@ -16445,7 +17357,13 @@ function executeRecursiveCte(env, cte, query) {
16445
17357
  }
16446
17358
  const iterCtes = new Map(env.ctes);
16447
17359
  iterCtes.set(cte.name, { columns, rows: working });
16448
- const iterEnv = { ctx: env.ctx, params: env.params, ctes: iterCtes, outer: env.outer };
17360
+ const iterEnv = {
17361
+ ctx: env.ctx,
17362
+ params: env.params,
17363
+ ctes: iterCtes,
17364
+ outer: env.outer,
17365
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17366
+ };
16449
17367
  const step = executeBody(iterEnv, body.right);
16450
17368
  if (step.columns.length !== columns.length) {
16451
17369
  throw pgError("syntax", "each UNION query must have the same number of columns", "42601");
@@ -16517,7 +17435,7 @@ function materializeItem(env, item, scope) {
16517
17435
  if (view) {
16518
17436
  let rel2;
16519
17437
  if (view.materialized) {
16520
- if (view.matRows === null || view.matColumns === null) {
17438
+ if (view.matColumns === null || view.matRows === null && !env.allowUnpopulatedMatviews) {
16521
17439
  throw pgError(
16522
17440
  "object_not_in_prerequisite_state",
16523
17441
  `materialized view "${view.name}" has not been populated`,
@@ -16526,10 +17444,16 @@ function materializeItem(env, item, scope) {
16526
17444
  }
16527
17445
  rel2 = {
16528
17446
  columns: view.matColumns.map((c) => ({ name: c.name, type: c.type, table: label })),
16529
- rows: view.matRows
17447
+ rows: view.matRows ?? []
16530
17448
  };
16531
17449
  } else {
16532
- const viewEnv = { ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null };
17450
+ const viewEnv = {
17451
+ ctx: env.ctx,
17452
+ params: null,
17453
+ ctes: /* @__PURE__ */ new Map(),
17454
+ outer: null,
17455
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
17456
+ };
16533
17457
  rel2 = executeSelectStmt(viewEnv, view.query);
16534
17458
  rel2 = {
16535
17459
  columns: rel2.columns.map((c, i) => ({
@@ -18598,7 +19522,13 @@ function runStmt(env, stmt, vars, emit, tableNames) {
18598
19522
  return;
18599
19523
  }
18600
19524
  case "for": {
18601
- const forEnv = { ctx: env.ctx, params: env.params, ctes: env.ctes, outer: vars.scope() };
19525
+ const forEnv = {
19526
+ ctx: env.ctx,
19527
+ params: env.params,
19528
+ ctes: env.ctes,
19529
+ outer: vars.scope(),
19530
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
19531
+ };
18602
19532
  const rel2 = executeSelectStmt(forEnv, stmt.query);
18603
19533
  for (const row of rel2.rows) {
18604
19534
  for (let i = 0; i < stmt.targets.length; i++) {
@@ -18640,7 +19570,13 @@ function initVars(env, fn, bound, program) {
18640
19570
  if (fn.returnsTable) {
18641
19571
  for (const c of fn.returnsTable) vars.declare(c.name, c.type, null);
18642
19572
  }
18643
- const seed = () => ({ ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: vars.scope() });
19573
+ const seed = () => ({
19574
+ ctx: env.ctx,
19575
+ params: bound,
19576
+ ctes: /* @__PURE__ */ new Map(),
19577
+ outer: vars.scope(),
19578
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
19579
+ });
18644
19580
  for (const d of program.decls) {
18645
19581
  const id = resolveTypeName(env.ctx.state, d.typeName).column.id;
18646
19582
  let val = null;
@@ -18653,7 +19589,13 @@ function initVars(env, fn, bound, program) {
18653
19589
  return vars;
18654
19590
  }
18655
19591
  function plpgsqlEnv(env, bound, vars) {
18656
- return { ctx: env.ctx, params: bound, ctes: /* @__PURE__ */ new Map(), outer: vars.scope() };
19592
+ return {
19593
+ ctx: env.ctx,
19594
+ params: bound,
19595
+ ctes: /* @__PURE__ */ new Map(),
19596
+ outer: vars.scope(),
19597
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
19598
+ };
18657
19599
  }
18658
19600
  function callPlpgsqlScalar(env, fn, args) {
18659
19601
  const retT = fn.returns ?? "text";
@@ -19341,6 +20283,9 @@ var DatabaseState = class _DatabaseState {
19341
20283
  };
19342
20284
 
19343
20285
  // src/executor/ddl.ts
20286
+ function matviewQueryEnv(env, allowUnpopulatedMatviews) {
20287
+ return { ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null, allowUnpopulatedMatviews };
20288
+ }
19344
20289
  function targetSchema(env, parts) {
19345
20290
  const state = env.ctx.state;
19346
20291
  if (parts.length >= 2) {
@@ -19744,6 +20689,7 @@ function executeCreateIndex(env, stmt) {
19744
20689
  }
19745
20690
  }
19746
20691
  }
20692
+ assertGinTrgmIndex(state, table, stmt);
19747
20693
  schema.indexes.set(name, {
19748
20694
  name,
19749
20695
  schema: schema.name,
@@ -19788,7 +20734,7 @@ function executeCreateView(env, stmt) {
19788
20734
  let matRows = null;
19789
20735
  let matColumns = null;
19790
20736
  if (stmt.materialized) {
19791
- const rel2 = executeSelectStmt({ ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null }, stmt.query);
20737
+ const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), stmt.query);
19792
20738
  matColumns = rel2.columns.map((c, i) => ({
19793
20739
  name: stmt.columns?.[i] ?? c.name,
19794
20740
  type: c.type === UNKNOWN ? "text" : c.type
@@ -19814,7 +20760,7 @@ function executeRefreshMatView(env, stmt) {
19814
20760
  throw pgError("undefined_table", `materialized view "${stmt.name.join(".")}" does not exist`, "42P01");
19815
20761
  }
19816
20762
  const view = env.ctx.state.ensureWritableView(found);
19817
- const rel2 = executeSelectStmt({ ctx: env.ctx, params: null, ctes: /* @__PURE__ */ new Map(), outer: null }, view.query);
20763
+ const rel2 = executeSelectStmt(matviewQueryEnv(env, !stmt.withData), view.query);
19818
20764
  view.matColumns = rel2.columns.map((c, i) => ({
19819
20765
  name: view.columns?.[i] ?? c.name,
19820
20766
  type: c.type === UNKNOWN ? "text" : c.type
@@ -19822,6 +20768,32 @@ function executeRefreshMatView(env, stmt) {
19822
20768
  view.matRows = stmt.withData ? rel2.rows : null;
19823
20769
  return commandResult("REFRESH MATERIALIZED VIEW", 0);
19824
20770
  }
20771
+ function executeCreateExtension(env, stmt) {
20772
+ const state = env.ctx.state;
20773
+ const schemaName = stmt.schema ?? state.currentSchema();
20774
+ const install = () => {
20775
+ if (stmt.name === "pgcrypto") {
20776
+ if (pgcryptoInstalled(state)) return "exists";
20777
+ installPgcrypto(state, state.getSchema(schemaName).name);
20778
+ return "installed";
20779
+ }
20780
+ if (stmt.name === "pg_trgm") {
20781
+ if (pgTrgmInstalled(state)) return "exists";
20782
+ installPgTrgm(state, state.getSchema(schemaName).name);
20783
+ return "installed";
20784
+ }
20785
+ return "unsupported";
20786
+ };
20787
+ const outcome = install();
20788
+ if (outcome === "unsupported") {
20789
+ throw pgError("feature_not_supported", `extension "${stmt.name}" is not available`, "0A000");
20790
+ }
20791
+ if (outcome === "exists") {
20792
+ if (stmt.ifNotExists) return commandResult("CREATE EXTENSION", 0);
20793
+ throw pgError("duplicate_object", `extension "${stmt.name}" already exists`, "42710");
20794
+ }
20795
+ return commandResult("CREATE EXTENSION", 0);
20796
+ }
19825
20797
  function executeCreateSchema(env, stmt) {
19826
20798
  const state = env.ctx.state;
19827
20799
  if (state.schemas.has(stmt.name)) {
@@ -20610,8 +21582,19 @@ function executeDrop(env, stmt) {
20610
21582
  table.triggers.splice(idx, 1);
20611
21583
  break;
20612
21584
  }
20613
- case "extension":
20614
- break;
21585
+ case "extension": {
21586
+ const ext = parts[parts.length - 1];
21587
+ if (ext === "pgcrypto" && pgcryptoInstalled(state)) {
21588
+ uninstallPgcrypto(state);
21589
+ break;
21590
+ }
21591
+ if (ext === "pg_trgm" && pgTrgmInstalled(state)) {
21592
+ uninstallPgTrgm(state);
21593
+ break;
21594
+ }
21595
+ if (stmt.ifExists) break;
21596
+ throw pgError("undefined_object", `extension "${ext}" does not exist`, "42704");
21597
+ }
20615
21598
  }
20616
21599
  }
20617
21600
  const label = stmt.kind === "materialized_view" ? "MATERIALIZED VIEW" : stmt.kind.toUpperCase();
@@ -20983,7 +21966,13 @@ function applyUpdateSets(env, table, sets, scope, row) {
20983
21966
  });
20984
21967
  if (typeof set.value === "object" && "kind" in set.value && set.value.kind === "row_subquery") {
20985
21968
  const rel2 = executeSelectStmt(
20986
- { ctx: env.ctx, params: env.params, ctes: env.ctes, outer: scope },
21969
+ {
21970
+ ctx: env.ctx,
21971
+ params: env.params,
21972
+ ctes: env.ctes,
21973
+ outer: scope,
21974
+ allowUnpopulatedMatviews: env.allowUnpopulatedMatviews
21975
+ },
20987
21976
  set.value.query
20988
21977
  );
20989
21978
  if (rel2.rows.length > 1) {
@@ -21722,6 +22711,8 @@ function executeStatement(env, stmt) {
21722
22711
  return executeAlterSequence(env, stmt);
21723
22712
  case "create_schema":
21724
22713
  return executeCreateSchema(env, stmt);
22714
+ case "create_extension":
22715
+ return executeCreateExtension(env, stmt);
21725
22716
  case "create_enum":
21726
22717
  return executeCreateEnum(env, stmt);
21727
22718
  case "alter_enum":