@crvouga/postgres-mem 0.1.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.
Files changed (109) hide show
  1. package/AGENTS.md +159 -0
  2. package/COMPATIBILITY-AUDIT.md +155 -0
  3. package/COMPATIBILITY.md +87 -0
  4. package/LICENSE +21 -0
  5. package/README.md +325 -0
  6. package/compat/coverage.json +1081 -0
  7. package/compat/divergences.json +191 -0
  8. package/compat/gate-report.json +80 -0
  9. package/compat/requirements.json +1105 -0
  10. package/compat/requirements.raw.html +484 -0
  11. package/compat/scenario-types.ts +98 -0
  12. package/compat/scenarios.ts +98 -0
  13. package/compat/sections/agg.ts +45 -0
  14. package/compat/sections/api.ts +28 -0
  15. package/compat/sections/arr.ts +45 -0
  16. package/compat/sections/cat.ts +47 -0
  17. package/compat/sections/con.ts +32 -0
  18. package/compat/sections/cpy.ts +24 -0
  19. package/compat/sections/cte.ts +29 -0
  20. package/compat/sections/dat.ts +60 -0
  21. package/compat/sections/ddl.ts +55 -0
  22. package/compat/sections/det.ts +20 -0
  23. package/compat/sections/dml.ts +37 -0
  24. package/compat/sections/eco.ts +16 -0
  25. package/compat/sections/err.ts +16 -0
  26. package/compat/sections/exp.ts +53 -0
  27. package/compat/sections/fun.ts +58 -0
  28. package/compat/sections/fzz.ts +36 -0
  29. package/compat/sections/guc.ts +27 -0
  30. package/compat/sections/joi.ts +33 -0
  31. package/compat/sections/jsn.ts +46 -0
  32. package/compat/sections/lim.ts +13 -0
  33. package/compat/sections/par.ts +61 -0
  34. package/compat/sections/pre.ts +26 -0
  35. package/compat/sections/sch.ts +31 -0
  36. package/compat/sections/sel.ts +43 -0
  37. package/compat/sections/seq.ts +31 -0
  38. package/compat/sections/snp.ts +21 -0
  39. package/compat/sections/tok.ts +53 -0
  40. package/compat/sections/trg.ts +57 -0
  41. package/compat/sections/tsr.ts +37 -0
  42. package/compat/sections/txn.ts +39 -0
  43. package/compat/sections/typ.ts +55 -0
  44. package/compat/sections/uni.ts +13 -0
  45. package/compat/sections/win.ts +43 -0
  46. package/compat/smoke-baseline.json +3 -0
  47. package/compat/unsupported-register.json +2526 -0
  48. package/dist/api/bind.d.ts +10 -0
  49. package/dist/api/database.d.ts +69 -0
  50. package/dist/api/statement.d.ts +51 -0
  51. package/dist/ast/nodes.d.ts +810 -0
  52. package/dist/constraints/enforce.d.ts +39 -0
  53. package/dist/errors/error.d.ts +24 -0
  54. package/dist/executor/ddl.d.ts +23 -0
  55. package/dist/executor/dml.d.ts +10 -0
  56. package/dist/executor/execute.d.ts +11 -0
  57. package/dist/executor/relation.d.ts +60 -0
  58. package/dist/executor/select.d.ts +41 -0
  59. package/dist/executor/session.d.ts +21 -0
  60. package/dist/executor/triggers-exec.d.ts +1 -0
  61. package/dist/executor/triggers.d.ts +16 -0
  62. package/dist/executor/window.d.ts +13 -0
  63. package/dist/expressions/context.d.ts +23 -0
  64. package/dist/expressions/eval.d.ts +38 -0
  65. package/dist/expressions/operators.d.ts +8 -0
  66. package/dist/expressions/pattern.d.ts +20 -0
  67. package/dist/functions/aggregates.d.ts +17 -0
  68. package/dist/functions/array-fns.d.ts +2 -0
  69. package/dist/functions/datetime-fns.d.ts +26 -0
  70. package/dist/functions/datetime-registry.d.ts +2 -0
  71. package/dist/functions/json-fns.d.ts +7 -0
  72. package/dist/functions/math-fns.d.ts +2 -0
  73. package/dist/functions/misc-fns.d.ts +8 -0
  74. package/dist/functions/scalar.d.ts +10 -0
  75. package/dist/functions/srf.d.ts +12 -0
  76. package/dist/functions/string-fns.d.ts +4 -0
  77. package/dist/functions/tsearch-fns.d.ts +2 -0
  78. package/dist/functions/util.d.ts +14 -0
  79. package/dist/functions/window.d.ts +7 -0
  80. package/dist/index.d.ts +28 -0
  81. package/dist/index.js +19591 -0
  82. package/dist/index.js.map +7 -0
  83. package/dist/lexer/tokenize.d.ts +8 -0
  84. package/dist/parser/index.d.ts +1 -0
  85. package/dist/parser/parser.d.ts +96 -0
  86. package/dist/runtime/clock.d.ts +14 -0
  87. package/dist/runtime/index.d.ts +3 -0
  88. package/dist/runtime/options.d.ts +25 -0
  89. package/dist/runtime/prng.d.ts +48 -0
  90. package/dist/schema/catalog-tables.d.ts +1 -0
  91. package/dist/schema/catalog.d.ts +12 -0
  92. package/dist/serialization/codec.d.ts +29 -0
  93. package/dist/serialization/index.d.ts +1 -0
  94. package/dist/storage/database-state.d.ts +216 -0
  95. package/dist/transactions/manager.d.ts +25 -0
  96. package/dist/tsearch/stem.d.ts +5 -0
  97. package/dist/tsearch/tsearch.d.ts +59 -0
  98. package/dist/types/cast.d.ts +26 -0
  99. package/dist/types/compare.d.ts +18 -0
  100. package/dist/types/datetime.d.ts +68 -0
  101. package/dist/types/jsonb.d.ts +45 -0
  102. package/dist/types/numeric.d.ts +69 -0
  103. package/dist/types/resolve.d.ts +15 -0
  104. package/dist/types/timezone.d.ts +9 -0
  105. package/dist/types/value.d.ts +99 -0
  106. package/dist/unstable.d.ts +15 -0
  107. package/dist/unstable.js +19153 -0
  108. package/dist/unstable.js.map +7 -0
  109. package/package.json +120 -0
@@ -0,0 +1,45 @@
1
+ import { type Numeric } from "./numeric.js";
2
+ /**
3
+ * jsonb value tree. Numbers are kept as `numeric` (PG stores jsonb numbers
4
+ * as numeric — `1.50::jsonb` keeps its scale). Object keys are unique;
5
+ * output order is (length, byte order), matching PG jsonb.
6
+ */
7
+ export type JsonbValue = {
8
+ j: "null";
9
+ } | {
10
+ j: "bool";
11
+ v: boolean;
12
+ } | {
13
+ j: "num";
14
+ v: Numeric;
15
+ } | {
16
+ j: "str";
17
+ v: string;
18
+ } | {
19
+ j: "arr";
20
+ v: JsonbValue[];
21
+ } | {
22
+ j: "obj";
23
+ v: Map<string, JsonbValue>;
24
+ };
25
+ export declare const JSONB_NULL: JsonbValue;
26
+ export declare function jsonbBool(v: boolean): JsonbValue;
27
+ export declare function jsonbNum(v: Numeric): JsonbValue;
28
+ export declare function jsonbStr(v: string): JsonbValue;
29
+ export declare function jsonbArr(v: JsonbValue[]): JsonbValue;
30
+ export declare function jsonbObj(entries: Iterable<[string, JsonbValue]>): JsonbValue;
31
+ export declare function parseJsonText(text: string): JsonbValue;
32
+ /** Validate json text without canonicalizing (for `json` type input). */
33
+ export declare function validateJsonText(text: string): void;
34
+ /** jsonb key sort: length first, then byte order (PG lexCompareJsonbStringValue). */
35
+ export declare function jsonbKeyCompare(a: string, b: string): number;
36
+ /** Render jsonb in PG's canonical text form: `{"a": 1, "b": [1, 2]}` */
37
+ export declare function jsonbText(v: JsonbValue): string;
38
+ /** Render as compact json text with insertion order (row_to_json / to_json / composite_to_json). */
39
+ export declare function jsonbCompactText(v: JsonbValue): string;
40
+ /** Structural equality (jsonb = jsonb). */
41
+ export declare function jsonbEquals(a: JsonbValue, b: JsonbValue): boolean;
42
+ /** Total order over jsonb (matches jsonb btree comparison semantics). */
43
+ export declare function jsonbCompare(a: JsonbValue, b: JsonbValue): number;
44
+ /** jsonb containment (@>). */
45
+ export declare function jsonbContains(a: JsonbValue, b: JsonbValue): boolean;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Arbitrary-precision decimal mirroring PostgreSQL's `numeric`.
3
+ *
4
+ * Value = (nan/inf special) or coefficient * 10^-dscale where `coef` is a
5
+ * signed bigint and `dscale` (display scale) is the number of digits after
6
+ * the decimal point. Display scale is significant: `2.50` renders as "2.50".
7
+ */
8
+ export interface Numeric {
9
+ readonly kind: "numeric";
10
+ /** signed coefficient; value = coef * 10^-dscale */
11
+ readonly coef: bigint;
12
+ /** display scale (digits after decimal point), >= 0 */
13
+ readonly dscale: number;
14
+ readonly special: "nan" | "inf" | "-inf" | null;
15
+ }
16
+ export declare function makeNumeric(coef: bigint, dscale: number): Numeric;
17
+ export declare const NUMERIC_NAN: Numeric;
18
+ export declare const NUMERIC_PINF: Numeric;
19
+ export declare const NUMERIC_NINF: Numeric;
20
+ export declare function isNumeric(v: unknown): v is Numeric;
21
+ export declare function numericFromBigInt(v: bigint): Numeric;
22
+ export declare function numericFromNumber(v: number): Numeric;
23
+ /** Parse text into numeric. Throws 22P02 on bad syntax (like numeric_in). */
24
+ export declare function parseNumeric(text: string): Numeric;
25
+ export declare function numericRescale(v: Numeric, dscale: number): Numeric;
26
+ export declare function numericText(v: Numeric): string;
27
+ export declare function numericIsZero(v: Numeric): boolean;
28
+ export declare function numericSign(v: Numeric): number;
29
+ /** Compare (NaN sorts greater than everything, like PG). Returns -1|0|1. */
30
+ export declare function numericCmp(a: Numeric, b: Numeric): number;
31
+ export declare function numericAdd(a: Numeric, b: Numeric): Numeric;
32
+ export declare function numericNeg(a: Numeric): Numeric;
33
+ export declare function numericSub(a: Numeric, b: Numeric): Numeric;
34
+ export declare function numericMul(a: Numeric, b: Numeric): Numeric;
35
+ /** Result scale for division, mirroring numeric.c select_div_scale. */
36
+ export declare function selectDivScale(a: Numeric, b: Numeric): number;
37
+ /** Divide with explicit result scale, rounding half away from zero. */
38
+ export declare function numericDivScaled(a: Numeric, b: Numeric, rscale: number): Numeric;
39
+ export declare function numericDiv(a: Numeric, b: Numeric): Numeric;
40
+ /** Truncated integer division (div(a,b) function semantics). */
41
+ export declare function numericDivTrunc(a: Numeric, b: Numeric): Numeric;
42
+ /** Modulo like numeric_mod: result has scale max(s1, s2), sign of dividend. */
43
+ export declare function numericMod(a: Numeric, b: Numeric): Numeric;
44
+ export declare function numericAbs(a: Numeric): Numeric;
45
+ /** round(v, s): round half away from zero; result dscale = max(s, 0). */
46
+ export declare function numericRound(a: Numeric, s: number): Numeric;
47
+ /** trunc(v, s): truncate toward zero. */
48
+ export declare function numericTrunc(a: Numeric, s: number): Numeric;
49
+ export declare function numericFloor(a: Numeric): Numeric;
50
+ export declare function numericCeil(a: Numeric): Numeric;
51
+ export declare function numericToNumber(a: Numeric): number;
52
+ /** Exact bigint if integral after rounding to scale 0 (half away from zero). */
53
+ export declare function numericToBigInt(a: Numeric): bigint;
54
+ /** sqrt via integer Newton iteration at extended scale. */
55
+ export declare function numericSqrt(a: Numeric): Numeric;
56
+ /** power for numeric with numeric result: ports PG's power_var/power_var_int in fixed point. */
57
+ export declare function numericPower(a: Numeric, b: Numeric): Numeric;
58
+ /** exp(numeric) in fixed point with PG's exp_var display-scale rule. */
59
+ export declare function numericExp(a: Numeric): Numeric;
60
+ /** ln(numeric) in fixed point with PG's numeric_ln display-scale rule. */
61
+ export declare function numericLn(a: Numeric): Numeric;
62
+ /** log(base, x) = ln(x)/ln(base) in fixed point with PG's log_var scale rule. */
63
+ export declare function numericLogBase(base: Numeric, x: Numeric): Numeric;
64
+ /** log10(numeric) in fixed point. */
65
+ export declare function numericLog10(a: Numeric): Numeric;
66
+ /** Apply numeric(precision, scale) typmod constraint. */
67
+ export declare function applyNumericTypmod(v: Numeric, precision: number, scale: number): Numeric;
68
+ /** Strip trailing fractional zeros (used by jsonb number canonicalization is NOT this; PG keeps scale). */
69
+ export declare function numericStripTrailingZeros(v: Numeric): Numeric;
@@ -0,0 +1,15 @@
1
+ import type { TypeName } from "../ast/nodes.js";
2
+ import type { DatabaseState } from "../storage/database-state.js";
3
+ import type { ColumnType } from "./value.js";
4
+ export interface ResolvedType {
5
+ readonly column: ColumnType;
6
+ /** "schema.name" when the name resolved to a domain */
7
+ readonly domain: string | null;
8
+ /** serial pseudo-types need sequence creation in DDL */
9
+ readonly serial: "int2" | "int4" | "int8" | null;
10
+ }
11
+ /**
12
+ * Resolve a parsed type name against the catalog: builtin alias, enum, or
13
+ * domain (searched along search_path). Throws 42704 when nothing matches.
14
+ */
15
+ export declare function resolveTypeName(state: DatabaseState, tn: TypeName): ResolvedType;
@@ -0,0 +1,9 @@
1
+ /** Offset (seconds east of UTC) of `zone` at the given UTC instant. */
2
+ export declare function zoneOffsetAtUtc(zone: string, utcMicros: bigint): number;
3
+ /**
4
+ * Offset for interpreting a naive local timestamp in `zone`
5
+ * (two-pass refinement over {@link zoneOffsetAtUtc}).
6
+ */
7
+ export declare function zoneOffsetForNaive(zone: string, naiveMicros: bigint): number;
8
+ /** Validate a zone string (throws when unrecognized). */
9
+ export declare function validateZone(zone: string): void;
@@ -0,0 +1,99 @@
1
+ import { type Interval } from "./datetime.js";
2
+ import { type JsonbValue } from "./jsonb.js";
3
+ import { type Numeric } from "./numeric.js";
4
+ /**
5
+ * Canonical type ids are lowercase PG internal names: "int4", "text",
6
+ * "timestamptz", … Arrays are `elem + "[]"`. Enums are `"enum:" + qualified
7
+ * name`. "unknown" is the type of untyped string literals; "null" never
8
+ * appears (null datums carry whatever type context provides).
9
+ */
10
+ export type TypeId = string;
11
+ export type Datum = null | boolean | number | bigint | string | Uint8Array | Numeric | Interval | PgArray | PgRecord | JsonbWrap;
12
+ export interface PgArray {
13
+ readonly kind: "pgarray";
14
+ readonly elem: TypeId;
15
+ /** dimension lengths, outermost first; empty array => [] */
16
+ readonly dims: number[];
17
+ /** lower bounds per dimension (default 1) */
18
+ readonly lbs: number[];
19
+ /** row-major flattened items */
20
+ readonly items: Datum[];
21
+ }
22
+ export interface PgRecord {
23
+ readonly kind: "pgrecord";
24
+ readonly types: TypeId[];
25
+ readonly values: Datum[];
26
+ readonly names?: string[];
27
+ }
28
+ /** jsonb datums are wrapped so `typeof` dispatch stays unambiguous. */
29
+ export interface JsonbWrap {
30
+ readonly kind: "jsonb";
31
+ readonly value: JsonbValue;
32
+ }
33
+ export declare function wrapJsonb(value: JsonbValue): JsonbWrap;
34
+ export declare function isJsonbWrap(v: unknown): v is JsonbWrap;
35
+ export declare function isPgArray(v: unknown): v is PgArray;
36
+ export declare function isPgRecord(v: unknown): v is PgRecord;
37
+ export declare function makeArray(elem: TypeId, items: Datum[], dims?: number[], lbs?: number[]): PgArray;
38
+ export interface TypedValue {
39
+ readonly t: TypeId;
40
+ readonly v: Datum;
41
+ }
42
+ export declare function tv(t: TypeId, v: Datum): TypedValue;
43
+ export declare const UNKNOWN = "unknown";
44
+ export interface TypeMod {
45
+ /** varchar(n)/bpchar(n): max chars; numeric: precision; time/timestamp: fractional digits */
46
+ readonly a?: number;
47
+ /** numeric scale */
48
+ readonly b?: number;
49
+ }
50
+ export interface ColumnType {
51
+ readonly id: TypeId;
52
+ readonly mod: TypeMod | null;
53
+ }
54
+ export declare function normalizeTypeName(name: string): TypeId | null;
55
+ export declare function isArrayType(t: TypeId): boolean;
56
+ export declare function arrayElemType(t: TypeId): TypeId;
57
+ export declare function arrayTypeOf(elem: TypeId): TypeId;
58
+ export declare function isEnumType(t: TypeId): boolean;
59
+ export declare function enumTypeName(t: TypeId): string;
60
+ /** SQL-facing type name (format_type / pg_typeof style). */
61
+ export declare function typeDisplayName(t: TypeId): string;
62
+ export declare const TYPE_OIDS: Record<string, number>;
63
+ export declare function typeOid(t: TypeId): number;
64
+ export declare function float8Text(v: number): string;
65
+ export declare function float4Text(v: number): string;
66
+ export declare function parseFloatText(text: string, type: "float4" | "float8"): number;
67
+ export declare const INT2_MIN = -32768;
68
+ export declare const INT2_MAX = 32767;
69
+ export declare const INT4_MIN = -2147483648;
70
+ export declare const INT4_MAX = 2147483647;
71
+ export declare const INT8_MIN = -9223372036854775808n;
72
+ export declare const INT8_MAX = 9223372036854775807n;
73
+ export declare function checkInt2(v: number): number;
74
+ export declare function checkInt4(v: number): number;
75
+ export declare function checkInt8(v: bigint): bigint;
76
+ export declare function parseBoolText(text: string): boolean;
77
+ export declare function parseByteaText(text: string): Uint8Array;
78
+ export declare function byteaText(v: Uint8Array): string;
79
+ export declare function parseUuidText(text: string): string;
80
+ export declare function parseMoneyText(text: string): bigint;
81
+ export declare function moneyText(cents: bigint): string;
82
+ export interface OutputCtx {
83
+ /** offset seconds east of UTC for rendering timestamptz at given UTC micros */
84
+ zoneOffsetAt(utcMicros: bigint): number;
85
+ }
86
+ export declare const UTC_OUTPUT: OutputCtx;
87
+ export declare function arrayText(arr: PgArray, ctx: OutputCtx): string;
88
+ export declare function recordText(rec: PgRecord, ctx: OutputCtx): string;
89
+ /** Render a non-null datum of type `t` to PG wire text. */
90
+ export declare function datumText(t: TypeId, v: Datum, ctx: OutputCtx): string;
91
+ export interface InputCtx {
92
+ /** session zone offset lookup for naive timestamptz input */
93
+ zoneOffsetForNaive(naiveMicros: bigint): number;
94
+ /** enum label validation: returns true when label is valid for the enum type */
95
+ enumHasLabel?(enumType: TypeId, label: string): boolean;
96
+ }
97
+ export declare const UTC_INPUT: InputCtx;
98
+ /** Parse PG wire text into a datum of type `t` (typinput). */
99
+ export declare function datumFromText(t: TypeId, text: string, ctx: InputCtx): Datum;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Unstable internals of `@crvouga/postgres-mem`. Everything exported here is
4
+ * exempt from semver — pin an exact version if you import from this module.
5
+ *
6
+ * @module
7
+ */
8
+ export { executeStatement, makeEnv } from "./executor/execute.js";
9
+ export { EngineCtx } from "./expressions/context.js";
10
+ export { tokenize } from "./lexer/tokenize.js";
11
+ export { parse, parseSingle } from "./parser/index.js";
12
+ export { type Clock, DEFAULT_NOW, fixedClock, systemClock } from "./runtime/clock.js";
13
+ export { deriveSeed, OsEntropy, Prng } from "./runtime/prng.js";
14
+ export { DatabaseState } from "./storage/database-state.js";
15
+ export { type Datum, datumText, type TypedValue, type TypeId } from "./types/value.js";