@minnowdb/core 0.6.5 → 0.6.6

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 (43) hide show
  1. package/dist/engine/artifact-cache.d.ts +2 -1
  2. package/dist/engine/batch.d.ts +0 -1
  3. package/dist/engine/batch.js +1 -1
  4. package/dist/engine/buffered-writer.js +1 -12
  5. package/dist/engine/byte-estimates.d.ts +11 -0
  6. package/dist/engine/byte-estimates.js +25 -0
  7. package/dist/engine/database.js +58 -34
  8. package/dist/engine/fts.d.ts +0 -1
  9. package/dist/engine/fts.js +1 -1
  10. package/dist/engine/join-index.d.ts +0 -1
  11. package/dist/engine/optimizer.js +9 -2
  12. package/dist/engine/point-read.d.ts +1 -1
  13. package/dist/engine/point-read.js +3 -2
  14. package/dist/engine/query-cache.js +1 -12
  15. package/dist/engine/query.d.ts +20 -59
  16. package/dist/engine/query.js +280 -39
  17. package/dist/engine/result-wire.d.ts +2 -1
  18. package/dist/engine/schema.d.ts +18 -2
  19. package/dist/engine/schema.js +11 -2
  20. package/dist/engine/sort-keys.d.ts +2 -3
  21. package/dist/engine/sort-keys.js +1 -1
  22. package/dist/engine/sql-domains.d.ts +27 -2
  23. package/dist/engine/sql-domains.js +120 -6
  24. package/dist/engine/sql-json.d.ts +0 -2
  25. package/dist/engine/sql-json.js +1 -1
  26. package/dist/engine/sql-semantics.d.ts +2 -1
  27. package/dist/engine/vector.d.ts +7 -7
  28. package/dist/engine/vector.js +8 -4
  29. package/dist/engine/write-block-planner.d.ts +2 -1
  30. package/dist/plan/model.d.ts +15 -0
  31. package/dist/storage/opfs/leader.d.ts +0 -4
  32. package/dist/storage/opfs/leader.js +2 -2
  33. package/dist/storage/opfs/snapshot-ledger.d.ts +3 -2
  34. package/dist/storage/toolkit/index.d.ts +1 -1
  35. package/dist/storage/toolkit/record-core.d.ts +0 -1
  36. package/dist/storage/toolkit/record-core.js +0 -7
  37. package/dist/testing/opfs-shim.d.ts +2 -1
  38. package/dist/testing/simulator.js +3 -0
  39. package/dist/worker-protocol/index.d.ts +1 -1
  40. package/dist/worker-protocol/index.js +0 -1
  41. package/package.json +2 -1
  42. package/postgres-feature-profile.json +8 -3
  43. package/sql-feature-matrix.json +71 -11
@@ -19,7 +19,7 @@
19
19
  * next, so what a multi-term sort pays for its later terms is proportional to how many rows tie
20
20
  * on the earlier ones.
21
21
  */
22
- export interface SortKeyColumn {
22
+ interface SortKeyColumn {
23
23
  /**
24
24
  * Ascending comparison with NULL smallest. This is the column's raw comparison, not SQL's
25
25
  * default placement: the sorter applies PostgreSQL's NULLS LAST for ASC and NULLS FIRST for
@@ -45,8 +45,6 @@ export interface SortKeyTerm {
45
45
  readonly descending: boolean;
46
46
  readonly nulls: "first" | "last" | undefined;
47
47
  }
48
- /** Unwraps a datetime to the number the comparisons use; every other value passes through. */
49
- export declare function comparableSortValue(value: unknown): unknown;
50
48
  /**
51
49
  * Extracts one term's values into a comparison-ready column. `valueAt` is called once per row,
52
50
  * so a caller that reads through a row object or a vector pays that cost once rather than once
@@ -72,3 +70,4 @@ export declare function buildSortKeyColumn(count: number, valueAt: (index: numbe
72
70
  * is already in order before doing anything else, since a scan often arrives sorted.
73
71
  */
74
72
  export declare function sortKeyIndexes(count: number, terms: readonly SortKeyTerm[]): Uint32Array;
73
+ export {};
@@ -1,7 +1,7 @@
1
1
  import { dateMilliseconds } from "../date-value.js";
2
2
  import { compareSqlStrings, compareSqlValues } from "./sql-semantics.js";
3
3
  /** Unwraps a datetime to the number the comparisons use; every other value passes through. */
4
- export function comparableSortValue(value) {
4
+ function comparableSortValue(value) {
5
5
  return value instanceof Date ? dateMilliseconds(value) : value;
6
6
  }
7
7
  /**
@@ -9,7 +9,24 @@ export declare function protectedSqlTextValue(value: string): string;
9
9
  export declare function externalSqlTextValue(value: unknown): unknown;
10
10
  export declare function isExactNumeric(value: unknown): value is string;
11
11
  export declare function exactNumericValue(value: unknown, precision?: number, scale?: number): string | null;
12
- export declare function exactNumericBinary(operator: "+" | "-" | "*" | "/" | "%", left: unknown, right: unknown): string | null | undefined;
12
+ /**
13
+ * Tags a SQL numeric constant with its exact digits, as written. Unlike `exactNumericValue`
14
+ * this does not canonicalize: trailing fractional zeros are PostgreSQL display scale, and
15
+ * division selects its result scale from the operands' scales, so a literal's digits are part
16
+ * of its meaning. Scientific notation is the exception — PostgreSQL expands the exponent when
17
+ * it parses the literal, so `1.5e2` is `150` and `1e400` is the full digit string, and the
18
+ * display scale comes from that expansion. The text is still validated and bounded.
19
+ */
20
+ export declare function exactNumericLiteral(text: string): string;
21
+ /**
22
+ * The Float64 this exact numeric value reads back identically from — the nearest float's
23
+ * canonical rendering re-parses to the same decimal value, so `0.1` qualifies while
24
+ * `9007199254740993` does not — or undefined when the trip through a number would visibly
25
+ * round. A value that survives stays an ordinary number so every number-typed path keeps
26
+ * running; only visibly rounded values stay tagged NUMERIC.
27
+ */
28
+ export declare function exactNumericAsNumber(value: string): number | undefined;
29
+ export declare function exactNumericBinary(operator: "+" | "-" | "*" | "/" | "%", left: unknown, right: unknown, minimumQuotientScale?: number, canonicalize?: boolean): string | null | undefined;
13
30
  export declare function exactNumericCompare(left: unknown, right: unknown): number | undefined;
14
31
  /**
15
32
  * Serializes a JSON value while bounding traversal, nesting, and output before concatenation.
@@ -33,7 +50,6 @@ export declare function isDateDomainValue(value: unknown): value is string;
33
50
  export declare function timeDomainValue(value: unknown): string | null;
34
51
  export declare function intervalDomainValue(value: unknown): string | null;
35
52
  export declare function arrayDomainValue(values: readonly unknown[]): string;
36
- export declare function enumDomainValue(value: unknown, name: string, values: readonly string[]): string | null;
37
53
  export declare function enumDomainCompare(left: unknown, right: unknown): number | undefined;
38
54
  export declare function normalizeSqlDomainValue(domain: SqlDomain, value: unknown): string | null;
39
55
  export declare function collatedDomainValue(value: unknown, collation: unknown): string | null;
@@ -48,4 +64,13 @@ export declare function collatedDomainCompare(left: unknown, right: unknown): nu
48
64
  */
49
65
  export declare function externalSqlDomainColumnValue(value: unknown, domain: SqlDomain | null | undefined): unknown;
50
66
  export declare function externalSqlDomainValue(value: unknown): unknown;
67
+ /**
68
+ * SQL `||` over internal string values, shared by both executors and constant folding so the
69
+ * three paths cannot disagree. PostgreSQL resolves `||` to text concatenation only when one
70
+ * side is text: its array and JSONB `||` operators are structural concatenation, which this
71
+ * engine does not implement, and two non-text operands have no `||` operator at all. Refusing
72
+ * those shapes keeps `||` from inventing a text concatenation PostgreSQL does not have —
73
+ * and from ever concatenating internal domain encodings.
74
+ */
75
+ export declare function concatenatedSqlValue(left: string, right: string): string;
51
76
  export declare function isSqlDomainValue(value: unknown): value is string;
@@ -53,6 +53,9 @@ function decimalParts(value) {
53
53
  let coefficient = BigInt(digits || "0") * (match[1] === "-" ? -1n : 1n);
54
54
  let scale = fraction.length - exponent;
55
55
  if (scale < 0) {
56
+ if (-scale > 100_000) {
57
+ throw new RangeError(`NUMERIC exponent is outside the supported range: ${source}`);
58
+ }
56
59
  coefficient *= pow10(-scale);
57
60
  scale = 0;
58
61
  }
@@ -122,7 +125,79 @@ export function exactNumericValue(value, precision, scale) {
122
125
  }
123
126
  return boundedTaggedDomainValue(NUMERIC, formatDecimal(normalizeDecimal(adjusted)), "NUMERIC value");
124
127
  }
125
- export function exactNumericBinary(operator, left, right) {
128
+ /**
129
+ * Tags a SQL numeric constant with its exact digits, as written. Unlike `exactNumericValue`
130
+ * this does not canonicalize: trailing fractional zeros are PostgreSQL display scale, and
131
+ * division selects its result scale from the operands' scales, so a literal's digits are part
132
+ * of its meaning. Scientific notation is the exception — PostgreSQL expands the exponent when
133
+ * it parses the literal, so `1.5e2` is `150` and `1e400` is the full digit string, and the
134
+ * display scale comes from that expansion. The text is still validated and bounded.
135
+ */
136
+ export function exactNumericLiteral(text) {
137
+ const parts = decimalParts(text);
138
+ const plain = /[eE]/.test(text) ? formatDecimal(parts) : text;
139
+ return boundedTaggedDomainValue(NUMERIC, plain, "NUMERIC literal");
140
+ }
141
+ /**
142
+ * The Float64 this exact numeric value reads back identically from — the nearest float's
143
+ * canonical rendering re-parses to the same decimal value, so `0.1` qualifies while
144
+ * `9007199254740993` does not — or undefined when the trip through a number would visibly
145
+ * round. A value that survives stays an ordinary number so every number-typed path keeps
146
+ * running; only visibly rounded values stay tagged NUMERIC.
147
+ */
148
+ export function exactNumericAsNumber(value) {
149
+ const parts = taggedDecimalParts(value);
150
+ if (parts === undefined)
151
+ return undefined;
152
+ const candidate = Number(value.slice(NUMERIC.length));
153
+ if (!Number.isFinite(candidate))
154
+ return undefined;
155
+ const roundTrip = normalizeDecimal(decimalParts(String(candidate)));
156
+ const exact = normalizeDecimal(parts);
157
+ return roundTrip.coefficient === exact.coefficient && roundTrip.scale === exact.scale
158
+ ? candidate
159
+ : undefined;
160
+ }
161
+ /**
162
+ * The leading base-10000 digit of a decimal, aligned at the decimal point the way PostgreSQL's
163
+ * NUMERIC digit array is: group k covers the decimal digits at 10^(4k)..10^(4k+3). `weight` is
164
+ * the group index of the first nonzero group and `firstDigit` its value (1..9999); zero reports
165
+ * both as 0, matching PostgreSQL's defaults when no nonzero digit exists.
166
+ */
167
+ function nbaseLeading(parts) {
168
+ const magnitude = parts.coefficient < 0n ? -parts.coefficient : parts.coefficient;
169
+ if (magnitude === 0n)
170
+ return { weight: 0, firstDigit: 0 };
171
+ const fractionGroups = Math.ceil(parts.scale / 4);
172
+ const aligned = magnitude * pow10(fractionGroups * 4 - parts.scale);
173
+ const groups = Math.ceil(aligned.toString().length / 4);
174
+ return {
175
+ weight: groups - 1 - fractionGroups,
176
+ firstDigit: Number(aligned / pow10((groups - 1) * 4)),
177
+ };
178
+ }
179
+ /**
180
+ * PostgreSQL's `select_div_scale`: the fractional digits a NUMERIC quotient is computed and
181
+ * rounded to. The estimated quotient weight guarantees roughly sixteen significant digits, and
182
+ * the result never displays fewer fractional digits than either operand. `minimumScale` stands
183
+ * in for display scale the canonical encoding cannot carry: PostgreSQL floors this selection at
184
+ * each operand's dscale, which for an AVG over a declared-scale column is that declared scale.
185
+ */
186
+ function quotientScale(a, b, minimumScale) {
187
+ const leftLeading = nbaseLeading(a);
188
+ const rightLeading = nbaseLeading(b);
189
+ let estimatedWeight = leftLeading.weight - rightLeading.weight;
190
+ if (leftLeading.firstDigit <= rightLeading.firstDigit)
191
+ estimatedWeight -= 1;
192
+ const scale = Math.max(16 - estimatedWeight * 4, a.scale, b.scale, minimumScale, 0);
193
+ return Math.min(scale, 1000);
194
+ }
195
+ export function exactNumericBinary(operator, left, right, minimumQuotientScale = 0,
196
+ // Stored and returned values are canonical (trailing fractional zeros stripped). Constant
197
+ // folding keeps the computed scale instead: PostgreSQL carries display scale through each
198
+ // step — max for +, -, %, the operands' sum for *, the selected scale for / — and a later
199
+ // division selects its own scale from it, so folding must not strip what execution never sees.
200
+ canonicalize = true) {
126
201
  const leftTagged = taggedDecimalParts(left);
127
202
  const rightTagged = taggedDecimalParts(right);
128
203
  if (leftTagged === undefined && rightTagged === undefined)
@@ -149,9 +224,10 @@ export function exactNumericBinary(operator, left, right) {
149
224
  };
150
225
  }
151
226
  else {
152
- // PostgreSQL NUMERIC division is arbitrary precision. Twenty fractional digits gives a
153
- // deterministic exact decimal rounding boundary without ever crossing binary Float64.
154
- const scale = Math.max(20, a.scale, b.scale);
227
+ // PostgreSQL NUMERIC division computes to its selected result scale and rounds the final
228
+ // digit half away from zero; matching the scale selection keeps every rendered digit in
229
+ // agreement, deterministically and without ever crossing binary Float64.
230
+ const scale = quotientScale(a, b, minimumQuotientScale);
155
231
  const numerator = a.coefficient * pow10(scale + b.scale);
156
232
  const denominator = b.coefficient * pow10(a.scale);
157
233
  let coefficient = numerator / denominator;
@@ -162,7 +238,7 @@ export function exactNumericBinary(operator, left, right) {
162
238
  }
163
239
  result = { coefficient, scale };
164
240
  }
165
- return boundedTaggedDomainValue(NUMERIC, formatDecimal(normalizeDecimal(result)), "NUMERIC result");
241
+ return boundedTaggedDomainValue(NUMERIC, formatDecimal(canonicalize ? normalizeDecimal(result) : result), "NUMERIC result");
166
242
  }
167
243
  export function exactNumericCompare(left, right) {
168
244
  const leftTagged = taggedDecimalParts(left);
@@ -470,7 +546,7 @@ export function intervalDomainValue(value) {
470
546
  export function arrayDomainValue(values) {
471
547
  return boundedTaggedDomainValue(ARRAY_VALUE, boundedJsonText(values.map(externalSqlDomainValue), true, "ARRAY value"), "ARRAY value");
472
548
  }
473
- export function enumDomainValue(value, name, values) {
549
+ function enumDomainValue(value, name, values) {
474
550
  if (value === null || value === undefined)
475
551
  return null;
476
552
  if (typeof value !== "string")
@@ -667,6 +743,44 @@ export function externalSqlDomainValue(value) {
667
743
  }
668
744
  return value;
669
745
  }
746
+ /**
747
+ * The domain kind carried by an internal string value, or undefined for ordinary text. The
748
+ * protected-TEXT wrapper and collated text are both ordinary text to PostgreSQL's operator
749
+ * resolution, so they report no kind.
750
+ */
751
+ function concatenationDomainKind(value) {
752
+ if (!value.startsWith(PREFIX))
753
+ return undefined;
754
+ if (value.startsWith(TEXT_VALUE) || value.startsWith(COLLATION_VALUE))
755
+ return undefined;
756
+ const rest = value.slice(PREFIX.length);
757
+ const colon = rest.indexOf(":");
758
+ return colon === -1 ? undefined : rest.slice(0, colon);
759
+ }
760
+ /**
761
+ * SQL `||` over internal string values, shared by both executors and constant folding so the
762
+ * three paths cannot disagree. PostgreSQL resolves `||` to text concatenation only when one
763
+ * side is text: its array and JSONB `||` operators are structural concatenation, which this
764
+ * engine does not implement, and two non-text operands have no `||` operator at all. Refusing
765
+ * those shapes keeps `||` from inventing a text concatenation PostgreSQL does not have —
766
+ * and from ever concatenating internal domain encodings.
767
+ */
768
+ export function concatenatedSqlValue(left, right) {
769
+ const leftKind = concatenationDomainKind(left);
770
+ const rightKind = concatenationDomainKind(right);
771
+ for (const kind of [leftKind, rightKind]) {
772
+ if (kind === "array") {
773
+ throw new TypeError("PostgreSQL array concatenation (||) is not supported");
774
+ }
775
+ if (kind === "jsonb") {
776
+ throw new TypeError("PostgreSQL JSONB concatenation (||) is not supported");
777
+ }
778
+ }
779
+ if (leftKind !== undefined && rightKind !== undefined) {
780
+ throw new TypeError(`No || operator for ${leftKind} and ${rightKind} values`);
781
+ }
782
+ return protectedSqlTextValue(String(externalSqlDomainValue(left)) + String(externalSqlDomainValue(right)));
783
+ }
670
784
  export function isSqlDomainValue(value) {
671
785
  return typeof value === "string" && value.startsWith(PREFIX);
672
786
  }
@@ -29,8 +29,6 @@ export declare function jsonArrowStep(document: unknown, key: unknown, caller: s
29
29
  };
30
30
  /** Whether a value is JSON text of the requested shape (T825). */
31
31
  export declare function jsonIsValid(document: unknown, kind: string): boolean;
32
- /** A SQL value as its JSON counterpart: datetimes serialize as ISO text, like every cast. */
33
- export declare function jsonValueOf(value: unknown): unknown;
34
32
  /**
35
33
  * JSON_ARRAY(v, ...) and JSON_OBJECT(k, v, ...) (T811/T812). The omitted null-handling clause is
36
34
  * `NULL ON NULL`: SQL NULL becomes a JSON null. `ABSENT ON NULL` is a separate spelling rather
@@ -143,7 +143,7 @@ export function jsonIsValid(document, kind) {
143
143
  }
144
144
  }
145
145
  /** A SQL value as its JSON counterpart: datetimes serialize as ISO text, like every cast. */
146
- export function jsonValueOf(value) {
146
+ function jsonValueOf(value) {
147
147
  const external = externalSqlDomainValue(value);
148
148
  return external instanceof Date ? dateIsoString(external) : external;
149
149
  }
@@ -19,7 +19,7 @@ export declare function encodeSqlEqualityValue(value: unknown): readonly unknown
19
19
  */
20
20
  export declare function roundSqlNumber(value: number, precision?: number): number;
21
21
  /** A whole-string SQL pattern matcher. Unlike RegExp, test never coerces its input. */
22
- export interface SqlPatternMatcher {
22
+ interface SqlPatternMatcher {
23
23
  test(value: string): boolean;
24
24
  }
25
25
  /** Compiles SQL LIKE without exposing input to the host regular-expression engine. */
@@ -34,3 +34,4 @@ export declare function defineSqlResultProperty(target: Record<string, unknown>,
34
34
  * a silent coercion here would make `LENGTH(42)` answer instead of failing.
35
35
  */
36
36
  export declare function stringArgument(name: string, value: unknown): string;
37
+ export {};
@@ -2,7 +2,7 @@ import type { DatabaseRow } from "./database.js";
2
2
  import type { CompiledQuery, QueryResult, QueryValue } from "./query.js";
3
3
  import { type FtsStats } from "./fts.js";
4
4
  import { QueryMemoryContext, type QueryMemoryUsage } from "./memory.js";
5
- export type VectorType = "boolean" | "number" | "string" | "datetime";
5
+ type VectorType = "boolean" | "number" | "string" | "datetime";
6
6
  /**
7
7
  * A resident slice of a logically longer streamed column. Window arrays index rows relative to
8
8
  * `start`; `length` on the vector remains the logical table length.
@@ -16,25 +16,25 @@ interface VectorBase {
16
16
  readonly length: number;
17
17
  readonly window?: VectorWindow;
18
18
  }
19
- export interface BooleanVector extends VectorBase {
19
+ interface BooleanVector extends VectorBase {
20
20
  readonly kind: "boolean";
21
21
  readonly values: Uint8Array;
22
22
  }
23
- export interface NumberVector extends VectorBase {
23
+ interface NumberVector extends VectorBase {
24
24
  readonly kind: "number";
25
25
  readonly values: Float64Array;
26
26
  }
27
- export interface DateTimeVector extends VectorBase {
27
+ interface DateTimeVector extends VectorBase {
28
28
  readonly kind: "datetime";
29
29
  readonly values: Float64Array;
30
30
  }
31
- export interface StringVector extends VectorBase {
31
+ interface StringVector extends VectorBase {
32
32
  readonly kind: "string";
33
33
  readonly codes: Uint32Array;
34
34
  readonly dictionary: readonly string[];
35
35
  }
36
36
  export type ColumnVector = BooleanVector | NumberVector | DateTimeVector | StringVector;
37
- export interface ColumnarColumnInput {
37
+ interface ColumnarColumnInput {
38
38
  readonly type: VectorType;
39
39
  readonly values: readonly QueryValue[];
40
40
  }
@@ -91,7 +91,7 @@ export interface QueryBatchExecutionOptions extends AsyncQueryExecutionOptions {
91
91
  /** Maximum result rows handed to the consumer at once. */
92
92
  readonly batchRows: number;
93
93
  }
94
- export interface PrepareVectorQueryOptions {
94
+ interface PrepareVectorQueryOptions {
95
95
  readonly memoryContext?: QueryMemoryContext;
96
96
  /**
97
97
  * Exact BM25 corpus statistics served by the persisted full-text index, keyed by each
@@ -9,7 +9,7 @@ import { ByteJoinIndex } from "./join-index.js";
9
9
  import { UnknownTableError } from "./errors.js";
10
10
  import { QueryMemoryBudgetError, QueryMemoryContext, } from "./memory.js";
11
11
  import { compareSqlValues, compileSimilarPattern, defineSqlResultProperty, } from "./sql-semantics.js";
12
- import { exactNumericBinary, externalSqlDomainValue, isDateDomainValue, isExactNumeric, preservedJsonDomainValue, protectedSqlTextValue, } from "./sql-domains.js";
12
+ import { concatenatedSqlValue, exactNumericBinary, externalSqlDomainValue, isDateDomainValue, isExactNumeric, preservedJsonDomainValue, protectedSqlTextValue, } from "./sql-domains.js";
13
13
  import { buildSortKeyColumn, sortKeyIndexes } from "./sort-keys.js";
14
14
  const DEFAULT_BATCH_ROWS = 2_048;
15
15
  /** Above this, locating each IN member separately costs more than scanning between them. */
@@ -892,6 +892,9 @@ function bindExpression(expression, sources, aggregateSpecs, aggregateIndexes, m
892
892
  ...(expression.distinct === true ? { distinct: true } : {}),
893
893
  ...(rawDatetime === undefined ? {} : { rawDatetime }),
894
894
  ...(rawNumber === undefined ? {} : { rawNumber }),
895
+ ...(expression.avgArgumentScale === undefined
896
+ ? {}
897
+ : { avgScale: expression.avgArgumentScale }),
895
898
  });
896
899
  }
897
900
  return {
@@ -4029,8 +4032,9 @@ function evaluateFinalExpression(plan, expression, group) {
4029
4032
  if (expression.name === "SUM")
4030
4033
  return isExactNumeric(value) ? value : (group.sums[aggregateIndex] ?? 0);
4031
4034
  if (expression.name === "AVG") {
4032
- if (isExactNumeric(value))
4033
- return exactNumericBinary("/", value, count);
4035
+ if (isExactNumeric(value)) {
4036
+ return exactNumericBinary("/", value, count, plan.aggregates[aggregateIndex]?.avgScale);
4037
+ }
4034
4038
  return (group.sums[aggregateIndex] ?? 0) / count;
4035
4039
  }
4036
4040
  // Raw-millisecond datetime extremes re-box into a Date only here, once per surviving group.
@@ -4764,7 +4768,7 @@ function binaryValue(operator, left, right) {
4764
4768
  if (typeof left !== "string" || typeof right !== "string") {
4765
4769
  throw new TypeError("|| requires string operands");
4766
4770
  }
4767
- return protectedSqlTextValue(String(externalSqlDomainValue(left)) + String(externalSqlDomainValue(right)));
4771
+ return concatenatedSqlValue(left, right);
4768
4772
  }
4769
4773
  const exact = exactNumericBinary(operator, left, right);
4770
4774
  if (exact !== undefined)
@@ -4,7 +4,7 @@ export interface WriteColumnValues {
4
4
  readonly values: readonly unknown[];
5
5
  readonly stringByteLengths?: readonly number[];
6
6
  }
7
- export interface WriteBlockRange {
7
+ interface WriteBlockRange {
8
8
  readonly start: number;
9
9
  readonly end: number;
10
10
  }
@@ -16,3 +16,4 @@ export interface WriteBlockRange {
16
16
  export declare function planAlignedWriteBlockRanges(columns: readonly WriteColumnValues[], rowCount: number, maximumRows: number, targetBytes: number, measureString?: (value: string) => number): WriteBlockRange[];
17
17
  /** Conservative first estimate used by both physical compaction planners. */
18
18
  export declare function estimateCompactionRowsPerOutput(targetBlockBytes: number, maximumEncodedBytesPerRow: number): number;
19
+ export {};
@@ -40,6 +40,14 @@ export type Expression = {
40
40
  value: QueryValue;
41
41
  internalSqlValue?: true;
42
42
  sqlDomain?: SqlDomain;
43
+ /**
44
+ * A numeric constant's exact source digits, kept when the value survives the number
45
+ * boundary but the spelling carries more (`1.000000000000000000000000` is the number 1
46
+ * with display scale 24).
47
+ * Constant folding reads these so seeded arithmetic runs in exact decimal space, the way
48
+ * PostgreSQL types every decimal constant NUMERIC before evaluating it.
49
+ */
50
+ exactText?: string;
43
51
  }
44
52
  /** A `?` or `$n` placeholder; `index` is 0-based. Replaced by a literal at bind time. */
45
53
  | {
@@ -68,6 +76,13 @@ export type Expression = {
68
76
  direction: "asc" | "desc";
69
77
  nulls?: "first" | "last";
70
78
  }>;
79
+ /**
80
+ * AVG over a declared-scale NUMERIC column: the argument's declared scale, annotated by a
81
+ * schema-aware pass before execution. PostgreSQL floors its quotient scale at the summed
82
+ * operand's display scale; canonical NUMERIC encoding strips trailing zeros, so without
83
+ * this the internal division cannot know the digits the declared scale will render.
84
+ */
85
+ avgArgumentScale?: number;
71
86
  } | {
72
87
  kind: "list";
73
88
  items: Expression[];
@@ -4,8 +4,6 @@ import { type Placement } from "../toolkit/extents.js";
4
4
  /** A failed checkpoint may defer compaction, but the recovery log itself stays bounded. */
5
5
  export declare const MAX_OPFS_WAL_BYTES: number;
6
6
  export declare const MAX_OPFS_CHECKPOINT_BYTES: number;
7
- /** Failed physical reclamation cannot permit byte-growing work forever. */
8
- export declare const MAX_OPFS_CLEANUP_DEBT_BYTES: number;
9
7
  interface IdPlacement {
10
8
  id: string;
11
9
  placement: Placement;
@@ -459,6 +457,4 @@ export declare class OpfsLeader {
459
457
  export declare function assertBlockReadBatchByteLimit(placements: ReadonlyArray<{
460
458
  length: number;
461
459
  } | undefined>): void;
462
- /** Conservative retained-heap model for one decoded postings chunk and its nested arrays. */
463
- export declare function modeledFtsChunkBytes(chunk: readonly FtsPosting[]): number;
464
460
  export {};
@@ -17,7 +17,7 @@ const CHECKPOINT_ENTRIES = 1024;
17
17
  export const MAX_OPFS_WAL_BYTES = 256 * 1024 * 1024;
18
18
  export const MAX_OPFS_CHECKPOINT_BYTES = 256 * 1024 * 1024;
19
19
  /** Failed physical reclamation cannot permit byte-growing work forever. */
20
- export const MAX_OPFS_CLEANUP_DEBT_BYTES = 64 * 1024 * 1024;
20
+ const MAX_OPFS_CLEANUP_DEBT_BYTES = 64 * 1024 * 1024;
21
21
  /** Decoded full-text base cache: bounded primarily by modeled retained heap, count secondarily. */
22
22
  const FTS_CHUNK_CACHE_BYTES = 16 * 1024 * 1024;
23
23
  const FTS_CHUNK_CACHE_SIZE = 64;
@@ -5035,7 +5035,7 @@ function samePlacement(left, right) {
5035
5035
  left.checksum === right.checksum);
5036
5036
  }
5037
5037
  /** Conservative retained-heap model for one decoded postings chunk and its nested arrays. */
5038
- export function modeledFtsChunkBytes(chunk) {
5038
+ function modeledFtsChunkBytes(chunk) {
5039
5039
  let bytes = 64;
5040
5040
  for (const posting of chunk) {
5041
5041
  bytes += 64 + posting.term.length * 2 + posting.rowIds.length * 40;
@@ -1,8 +1,8 @@
1
1
  import { type SnapshotFrame, type SnapshotFrameKind } from "../types.js";
2
2
  import { type Placement } from "../toolkit/extents.js";
3
3
  import { OpfsTree } from "./files.js";
4
- export type SnapshotLedgerKind = "export" | "import" | "completed";
5
- export interface SnapshotLedgerRecord {
4
+ type SnapshotLedgerKind = "export" | "import" | "completed";
5
+ interface SnapshotLedgerRecord {
6
6
  readonly sequence: number;
7
7
  readonly kind: SnapshotFrameKind;
8
8
  readonly itemCount: number;
@@ -38,3 +38,4 @@ export declare class SnapshotFrameLedger {
38
38
  adoptLength(length: number): void;
39
39
  close(): void;
40
40
  }
41
+ export {};
@@ -26,4 +26,4 @@ export { readFully, writeFully, type SyncFileHandle } from "./sync-file.js";
26
26
  export { RecordCore, type PhysicalBlocks, type RecordCoreState } from "./record-core.js";
27
27
  export { MAX_WAL_FRAME_BYTES, WalWriter, iterateWalFrames, replayWalFrames, type ReplayedWalFrame, } from "./wal.js";
28
28
  export { ExtentPool, assertValidExtentMeta, assertValidPlacement, extentPath, validPlacement, type ExtentBatchMark, type ExtentFiles, type ExtentMeta, type ExtentPoolOptions, type Placement, } from "./extents.js";
29
- export { LOG_FORMAT_VERSION, decodeChunk, decodePostingChunk, decodeRecordJson, decodeSyncCheckpoint, encodeChunk, encodePostingChunk, encodeRecordJson, encodeSyncCheckpoint, } from "./wire.js";
29
+ export { LOG_FORMAT_VERSION, decodeChunk, decodePostingChunk, decodeRecordJson, decodeSyncCheckpoint, encodeChunk, encodePostingChunk, encodeRecordJson, encodeSyncCheckpoint, type PostingChunkEntry, } from "./wire.js";
@@ -373,7 +373,6 @@ export declare function validateFtsPostingChunks(chunks: unknown, label: string)
373
373
  export declare function validateSegmentRuntimeRecord(value: unknown, label: string): SegmentRecord;
374
374
  export declare function validateAutoIncrementReservation(count: number, atLeast: bigint | undefined): void;
375
375
  export declare function validateBeginTransactionInput(input: BeginTransactionInput): void;
376
- export declare function validateLeaseExpiration(expiresAt: string): void;
377
376
  export declare function validateTempRunPage(page: TempRunPage): void;
378
377
  export declare function validateTempRunPageIdentity(ownerId: string, runId: string, pageIndex: number): void;
379
378
  export {};
@@ -6382,13 +6382,6 @@ function validateTempOwnerRecord(record) {
6382
6382
  throw new RangeError("Temp owner record must be created at revision zero");
6383
6383
  }
6384
6384
  }
6385
- export function validateLeaseExpiration(expiresAt) {
6386
- if (typeof expiresAt !== "string" ||
6387
- expiresAt.length === 0 ||
6388
- !Number.isFinite(Date.parse(expiresAt))) {
6389
- throw new TypeError("Lease expiration must be valid");
6390
- }
6391
- }
6392
6385
  function isCanonicalTimestamp(value) {
6393
6386
  const timestamp = Date.parse(value);
6394
6387
  return Number.isFinite(timestamp) && dateIsoString(new Date(timestamp)) === value;
@@ -21,7 +21,7 @@
21
21
  * toolkit's `SyncFileHandle` surface, in plain vitest, with crashes and quota failures on tap.
22
22
  */
23
23
  export type WriteFault = (path: string, phase: "create" | "write" | "flush") => void;
24
- export type DeleteFault = (path: string) => void;
24
+ type DeleteFault = (path: string) => void;
25
25
  export type TransferLimit = (path: string, operation: "read" | "write", requestedBytes: number, at: number) => number | undefined;
26
26
  export declare class MemoryOpfs {
27
27
  #private;
@@ -43,3 +43,4 @@ export declare class MemoryOpfs {
43
43
  /** Test-side in-place corruption: existing sync handles observe the changed byte. */
44
44
  corruptFileByte(path: string, offset: number): void;
45
45
  }
46
+ export {};
@@ -572,6 +572,9 @@ function summarizeCollection(result) {
572
572
  retainedTransactions: result.retainedTransactionCount,
573
573
  };
574
574
  }
575
+ // Stream-identical copy of `mulberry32` in ./seeds.ts. This module ships in the tarball and
576
+ // seeds.ts does not (it reads MINNOW_SEED and the unpublished regression-seeds.json), so the
577
+ // published simulator cannot import the canonical copy.
575
578
  function mulberry32(seed) {
576
579
  let state = seed >>> 0;
577
580
  return () => {
@@ -1,7 +1,7 @@
1
1
  export declare const protocolVersion: 3;
2
2
  /** Outstanding request/response pairs retained by either side of one database RPC connection. */
3
3
  export declare const MAX_DATABASE_RPC_IN_FLIGHT = 256;
4
- export type WorkerOperation = "benchmark" | "cancelBenchmark" | "memorySample" | "datasetList" | "datasetCreate" | "datasetDelete" | "runQuery" | "suiteReference" | "suiteWrite" | "suiteFeatureMatrix" | "suiteLive";
4
+ export type WorkerOperation = "benchmark" | "cancelBenchmark" | "datasetList" | "datasetCreate" | "datasetDelete" | "runQuery" | "suiteReference" | "suiteWrite" | "suiteFeatureMatrix" | "suiteLive";
5
5
  export interface WorkerRequest<T = unknown> {
6
6
  version: typeof protocolVersion;
7
7
  requestId: string;
@@ -105,7 +105,6 @@ function isOperation(value) {
105
105
  return [
106
106
  "benchmark",
107
107
  "cancelBenchmark",
108
- "memorySample",
109
108
  "datasetList",
110
109
  "datasetCreate",
111
110
  "datasetDelete",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minnowdb/core",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "A columnar SQL database for the browser: PostgreSQL-style SQL over durable IndexedDB or OPFS data, with no server or WebAssembly module.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Wilhite",
@@ -125,6 +125,7 @@
125
125
  "!dist/storage/fixture-shape.*",
126
126
  "!dist/engine/storage-test-helpers.*",
127
127
  "!dist/testing/seeds.*",
128
+ "!dist/testing/oracle.*",
128
129
  "!dist/*.tsbuildinfo",
129
130
  "sql-feature-matrix.json",
130
131
  "postgres-feature-profile.json"
@@ -111,7 +111,7 @@
111
111
  {
112
112
  "id": "function.string-extended",
113
113
  "classification": "different",
114
- "reason": "The bundled form includes INSTR; PostgreSQL spells that function strpos with reversed arguments."
114
+ "reason": "The bundled form includes INSTR; PostgreSQL has no INSTR and spells the same (string, substring) lookup STRPOS, with the arguments in the same order."
115
115
  },
116
116
  {
117
117
  "id": "function.trim-multi-character",
@@ -134,10 +134,15 @@
134
134
  "classification": "different",
135
135
  "reason": "Both engines accept the correlated JSON aggregate and agree on its JSON value, but Minnow returns JSON text while PostgreSQL returns a native JSON value."
136
136
  },
137
+ {
138
+ "id": "literal.scientific",
139
+ "classification": "different",
140
+ "reason": "PostgreSQL types every scientific-notation constant NUMERIC and renders it as text. Minnow evaluates it exactly but returns a number whenever the value reads back identically from one, keeping ordinary constants number-typed at the JavaScript boundary; a constant that stays exact renders fully expanded, as PostgreSQL renders it."
141
+ },
137
142
  {
138
143
  "id": "type.exact-numeric",
139
144
  "classification": "different",
140
- "reason": "Minnow preserves exact decimals at the JavaScript boundary as strings; PGlite's default decoder returns this NUMERIC value as a number. A declared scale renders at exactly that scale as PostgreSQL does; a bare NUMERIC column, a derived arithmetic result, and a value cast or concatenated to text render canonically, without the trailing fractional zeros PostgreSQL preserves. A non-terminating AVG quotient renders at Minnow's internal precision, which keeps more fractional digits than PostgreSQL's rounding; the value agrees to every digit PostgreSQL renders."
145
+ "reason": "Minnow preserves exact decimals at the JavaScript boundary as strings, as PGlite's default decoder also does. A declared scale renders at exactly that scale as PostgreSQL does; a bare NUMERIC column, a derived arithmetic result, and a value cast or concatenated to text render canonically, without the trailing fractional zeros PostgreSQL preserves. Division and AVG select their result scale the way PostgreSQL does, so quotient digits agree — including AVG over a column whose declared scale exceeds the selection. The canonical encoding does drop a stored value's display scale, so an explicit arithmetic quotient (such as SUM(v) / COUNT(v)) over a column declared with more than about twenty fractional digits can carry fewer digits than PostgreSQL, which floors the selection at the operand's display scale. An arithmetic or comparison expression mixing a float column with a constant Float64 cannot represent stays exact, where PostgreSQL casts the constant to float8 and rounds it before evaluating."
141
146
  },
142
147
  {
143
148
  "id": "type.json-jsonb",
@@ -182,7 +187,7 @@
182
187
  {
183
188
  "id": "json.array",
184
189
  "classification": "different",
185
- "reason": "The JSON value agrees, but Minnow returns compact JSON text while PostgreSQL's text rendering includes spaces."
190
+ "reason": "Minnow defaults JSON_ARRAY to NULL ON NULL, so a NULL argument becomes a JSON null where PostgreSQL's ABSENT ON NULL default drops it; Minnow also returns compact JSON text while PostgreSQL's rendering includes spaces."
186
191
  },
187
192
  {
188
193
  "id": "trigger.create-after",