@atscript/db-memory 0.1.167 → 0.1.168
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/index.cjs +107 -15
- package/dist/index.d.cts +13 -1
- package/dist/index.d.mts +13 -1
- package/dist/index.mjs +108 -16
- package/package.json +9 -9
package/dist/index.cjs
CHANGED
|
@@ -103,7 +103,7 @@ function valuesEqual(a, b) {
|
|
|
103
103
|
return a === b;
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
|
-
* Mirror of `mongo-
|
|
106
|
+
* Mirror of `mongo-view-expr.ts`'s `parseRegexString`: normalize a `$regex` value
|
|
107
107
|
* (or a bare `RegExp`) into a `{ pattern, flags }` pair. Accepts a `RegExp`
|
|
108
108
|
* instance, a `/pattern/flags` string literal, or a plain string (treated as
|
|
109
109
|
* the literal pattern with no flags).
|
|
@@ -194,10 +194,12 @@ function toOrdinal(v) {
|
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
196
196
|
* `$gt`/`$gte`/`$lt`/`$lte`. A missing or `null` field never matches an
|
|
197
|
-
* ordering comparison
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
197
|
+
* ordering comparison, and neither does a `null` operand — the visitor
|
|
198
|
+
* compiles that to "no match" before reaching here (SQL's UNKNOWN, MongoDB's
|
|
199
|
+
* type bracketing — JS would read it as `0`). `Date` operands are
|
|
200
|
+
* normalized to epoch millis; all other comparisons use plain JS ordering
|
|
201
|
+
* (numbers numerically, strings lexicographically) — NO collation or locale
|
|
202
|
+
* awareness. This intentionally differs from SQL engines' collated ordering.
|
|
201
203
|
*/
|
|
202
204
|
function evalRelational(fieldValue, op, value) {
|
|
203
205
|
if (fieldValue === void 0 || fieldValue === null) return false;
|
|
@@ -246,7 +248,9 @@ const memoryVisitor = {
|
|
|
246
248
|
case "$gt":
|
|
247
249
|
case "$gte":
|
|
248
250
|
case "$lt":
|
|
249
|
-
case "$lte":
|
|
251
|
+
case "$lte":
|
|
252
|
+
if (value == null) return () => false;
|
|
253
|
+
return (row) => evalRelational(read(row), op, value);
|
|
250
254
|
case "$in": {
|
|
251
255
|
const isIn = compileIn(value);
|
|
252
256
|
return (row) => isIn(read(row));
|
|
@@ -746,7 +750,16 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
746
750
|
* `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
|
|
747
751
|
* - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
|
|
748
752
|
* the `$sort` comparator; `countDistinct` counts distinct non-null values
|
|
749
|
-
* by group identity (below)
|
|
753
|
+
* by group identity (below) — over a field list (since 0.1.168), distinct
|
|
754
|
+
* combinations, a row with any null / missing field not counted; any other
|
|
755
|
+
* `$fn` is `INVALID_QUERY`;
|
|
756
|
+
* - a conditional aggregate (`$where`, since 0.1.168) feeds its accumulator
|
|
757
|
+
* only the rows its condition matches — the adapter's own filter predicate
|
|
758
|
+
* ({@link buildMemoryPredicate}), so it reads like the query filter;
|
|
759
|
+
* - a two-level aggregate (`controls.$from`, since 0.1.168) groups the rows
|
|
760
|
+
* of its inner level (this function over `$from`) by the outer controls —
|
|
761
|
+
* every inner column (an inner `$groupBy` key at its path, an inner alias
|
|
762
|
+
* at the top) is read like a field of a table row;
|
|
750
763
|
* - group identity is type-tagged: `1` and `"1"` never share a group (a
|
|
751
764
|
* number and a bigint of one value do), `Date`s group by instant, JSON
|
|
752
765
|
* values by a key-order-independent serialization;
|
|
@@ -755,16 +768,17 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
755
768
|
* object-valued leaves copied so no store-owned value escapes.
|
|
756
769
|
*/
|
|
757
770
|
function aggregateRows(rows, controls) {
|
|
771
|
+
if (controls.$from) rows = aggregateRows(rows, controls.$from);
|
|
758
772
|
const $select = controls.$select;
|
|
759
773
|
const groupBy = controls.$groupBy ?? [];
|
|
760
774
|
const entries = [
|
|
761
775
|
...($select?.aggregates ?? []).map((expr) => ({
|
|
762
776
|
alias: (0, _atscript_db_agg.resolveAlias)(expr),
|
|
763
|
-
create: accumulatorFactory(expr)
|
|
777
|
+
create: conditional(accumulatorFactory(expr), expr.$where)
|
|
764
778
|
})),
|
|
765
779
|
...($select?.exprAggregates ?? []).map((e) => ({
|
|
766
780
|
alias: e.alias,
|
|
767
|
-
create: exprAccumulatorFactory(e)
|
|
781
|
+
create: conditional(exprAccumulatorFactory(e), e.where)
|
|
768
782
|
})),
|
|
769
783
|
...($select?.firstLast ?? []).map((fl) => ({
|
|
770
784
|
alias: fl.alias,
|
|
@@ -872,6 +886,23 @@ function numericValue(value) {
|
|
|
872
886
|
return Number.isNaN(n) ? void 0 : n;
|
|
873
887
|
}
|
|
874
888
|
}
|
|
889
|
+
/**
|
|
890
|
+
* An accumulator fed only the rows `where` matches (a conditional aggregate,
|
|
891
|
+
* since 0.1.168) — `create` unchanged without a condition.
|
|
892
|
+
*/
|
|
893
|
+
function conditional(create, where) {
|
|
894
|
+
if (where === void 0) return create;
|
|
895
|
+
const matches = buildMemoryPredicate(where);
|
|
896
|
+
return () => {
|
|
897
|
+
const acc = create();
|
|
898
|
+
return {
|
|
899
|
+
add: (row) => {
|
|
900
|
+
if (matches(row)) acc.add(row);
|
|
901
|
+
},
|
|
902
|
+
result: () => acc.result()
|
|
903
|
+
};
|
|
904
|
+
};
|
|
905
|
+
}
|
|
875
906
|
/** `sum` / `avg` / `min` / `max` over a per-row arithmetic expression; NULL results are skipped like NULL fields. */
|
|
876
907
|
function exprAccumulatorFactory(e) {
|
|
877
908
|
const readers = new Map(e.names.map((name) => [name, pathReader(name)]));
|
|
@@ -942,12 +973,39 @@ function fnAccumulator(fn, read) {
|
|
|
942
973
|
};
|
|
943
974
|
}
|
|
944
975
|
/**
|
|
976
|
+
* `countDistinct` over a field list: distinct combinations by their
|
|
977
|
+
* length-prefixed {@link identityToken}s (type-tagged — `1` and `"1"`
|
|
978
|
+
* differ; no value forges a separator); a row with any null / missing field
|
|
979
|
+
* is not counted (SQL's `COUNT(DISTINCT a, b)`).
|
|
980
|
+
*/
|
|
981
|
+
function distinctListFactory(fields) {
|
|
982
|
+
const readers = fields.map((field) => pathReader(field));
|
|
983
|
+
return () => {
|
|
984
|
+
const seen = /* @__PURE__ */ new Set();
|
|
985
|
+
return {
|
|
986
|
+
add: (row) => {
|
|
987
|
+
let identity = "";
|
|
988
|
+
for (const read of readers) {
|
|
989
|
+
const v = read(row);
|
|
990
|
+
if (v == null) return;
|
|
991
|
+
const token = identityToken(v);
|
|
992
|
+
identity += `${token.length}:${token}`;
|
|
993
|
+
}
|
|
994
|
+
seen.add(identity);
|
|
995
|
+
},
|
|
996
|
+
result: () => seen.size
|
|
997
|
+
};
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
945
1001
|
* Resolves a plain aggregate (`first` / `last` are {@link firstLastFactory}'s)
|
|
946
|
-
* to an accumulator factory (one accumulator per group
|
|
947
|
-
* so an unknown `$fn` is
|
|
1002
|
+
* to an accumulator factory (one accumulator per group; a `$where` is applied
|
|
1003
|
+
* by {@link conditional}). Validated up front, so an unknown `$fn` is
|
|
1004
|
+
* rejected even when there are no rows.
|
|
948
1005
|
*/
|
|
949
1006
|
function accumulatorFactory(expr) {
|
|
950
1007
|
(0, _atscript_db_agg.assertAggregateFn)(expr.$fn);
|
|
1008
|
+
if (typeof expr.$field !== "string") return distinctListFactory((0, _atscript_db.aggregateColumns)(expr));
|
|
951
1009
|
const field = expr.$field;
|
|
952
1010
|
const read = pathReader(field);
|
|
953
1011
|
if (expr.$fn === "count") {
|
|
@@ -1728,7 +1786,9 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1728
1786
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
1729
1787
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
1730
1788
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
1731
|
-
* that survive `$having`.
|
|
1789
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
1790
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
1791
|
+
* the engine too (since 0.1.168).
|
|
1732
1792
|
*
|
|
1733
1793
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
1734
1794
|
* rejects a grouped search before dispatch.
|
|
@@ -1752,6 +1812,18 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1752
1812
|
supportsAggregateExpressions() {
|
|
1753
1813
|
return true;
|
|
1754
1814
|
}
|
|
1815
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
1816
|
+
supportsConditionalAggregates() {
|
|
1817
|
+
return true;
|
|
1818
|
+
}
|
|
1819
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
1820
|
+
supportsDistinctFieldLists() {
|
|
1821
|
+
return true;
|
|
1822
|
+
}
|
|
1823
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
1824
|
+
supportsNestedAggregates() {
|
|
1825
|
+
return true;
|
|
1826
|
+
}
|
|
1755
1827
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
1756
1828
|
supportsNullsPlacement() {
|
|
1757
1829
|
return true;
|
|
@@ -1819,16 +1891,36 @@ var MemoryAdapter = class MemoryAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1819
1891
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
1820
1892
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
1821
1893
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
1894
|
+
*
|
|
1895
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
1896
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
1897
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
1822
1898
|
*/
|
|
1823
1899
|
async syncIndexes() {
|
|
1900
|
+
const state = this._state();
|
|
1824
1901
|
const uniqueIndexes = [];
|
|
1902
|
+
const broken = [];
|
|
1825
1903
|
for (const index of this._table.indexes.values()) {
|
|
1826
1904
|
if (index.type !== "unique") continue;
|
|
1827
|
-
|
|
1905
|
+
const recorded = recordUniqueIndex(index.name, index.fields.map((f) => f.name), new Set(index.fields.filter((f) => f.optional).map((f) => f.name)));
|
|
1906
|
+
let holds = true;
|
|
1907
|
+
for (const [key, row] of state.rows) {
|
|
1908
|
+
const tuple = uniqueTupleKey(recorded, row);
|
|
1909
|
+
if (tuple === void 0) continue;
|
|
1910
|
+
if (recorded.entries.has(tuple)) {
|
|
1911
|
+
holds = false;
|
|
1912
|
+
break;
|
|
1913
|
+
}
|
|
1914
|
+
recorded.entries.set(tuple, new Set([key]));
|
|
1915
|
+
}
|
|
1916
|
+
(holds ? uniqueIndexes : broken).push(recorded);
|
|
1828
1917
|
}
|
|
1829
|
-
const state = this._state();
|
|
1830
1918
|
state.uniqueIndexes = uniqueIndexes;
|
|
1831
|
-
|
|
1919
|
+
const [first] = broken;
|
|
1920
|
+
if (first) throw (0, _atscript_db.constraintViolation)("CONFLICT", {
|
|
1921
|
+
name: first.name,
|
|
1922
|
+
columns: first.fields
|
|
1923
|
+
}, `Cannot create unique index ${broken.map((i) => `"${i.name}"`).join(", ")}: stored rows hold duplicate values`);
|
|
1832
1924
|
}
|
|
1833
1925
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
|
1834
1926
|
async ensureTable() {
|
package/dist/index.d.cts
CHANGED
|
@@ -443,7 +443,9 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
443
443
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
444
444
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
445
445
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
446
|
-
* that survive `$having`.
|
|
446
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
447
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
448
|
+
* the engine too (since 0.1.168).
|
|
447
449
|
*
|
|
448
450
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
449
451
|
* rejects a grouped search before dispatch.
|
|
@@ -459,6 +461,12 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
459
461
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
460
462
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
461
463
|
supportsAggregateExpressions(): boolean;
|
|
464
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
465
|
+
supportsConditionalAggregates(): boolean;
|
|
466
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
467
|
+
supportsDistinctFieldLists(): boolean;
|
|
468
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
469
|
+
supportsNestedAggregates(): boolean;
|
|
462
470
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
463
471
|
supportsNullsPlacement(): boolean;
|
|
464
472
|
/**
|
|
@@ -479,6 +487,10 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
479
487
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
480
488
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
481
489
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
490
|
+
*
|
|
491
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
492
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
493
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
482
494
|
*/
|
|
483
495
|
syncIndexes(): Promise<void>;
|
|
484
496
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
package/dist/index.d.mts
CHANGED
|
@@ -443,7 +443,9 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
443
443
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
444
444
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
445
445
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
446
|
-
* that survive `$having`.
|
|
446
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
447
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
448
|
+
* the engine too (since 0.1.168).
|
|
447
449
|
*
|
|
448
450
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
449
451
|
* rejects a grouped search before dispatch.
|
|
@@ -459,6 +461,12 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
459
461
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
460
462
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
461
463
|
supportsAggregateExpressions(): boolean;
|
|
464
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
465
|
+
supportsConditionalAggregates(): boolean;
|
|
466
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
467
|
+
supportsDistinctFieldLists(): boolean;
|
|
468
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
469
|
+
supportsNestedAggregates(): boolean;
|
|
462
470
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
463
471
|
supportsNullsPlacement(): boolean;
|
|
464
472
|
/**
|
|
@@ -479,6 +487,10 @@ declare class MemoryAdapter extends BaseDbAdapter {
|
|
|
479
487
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
480
488
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
481
489
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
490
|
+
*
|
|
491
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
492
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
493
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
482
494
|
*/
|
|
483
495
|
syncIndexes(): Promise<void>;
|
|
484
496
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, INTEGER_REGEX_OP, constraintViolation, containsRelationFilter, deletePath, evaluateExpr, forEachResolvedRelation, getPath, isAtscriptDbView, isConflict, isPlainObject, isResolvedRelationFilter, stableJson, walkFilter } from "@atscript/db";
|
|
1
|
+
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, INTEGER_REGEX_OP, aggregateColumns, constraintViolation, containsRelationFilter, deletePath, evaluateExpr, forEachResolvedRelation, getPath, isAtscriptDbView, isConflict, isPlainObject, isResolvedRelationFilter, stableJson, walkFilter } from "@atscript/db";
|
|
2
2
|
import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
|
|
3
3
|
import { bucketer } from "@uniqu/core";
|
|
4
4
|
//#region src/lib/memory-clone.ts
|
|
@@ -102,7 +102,7 @@ function valuesEqual(a, b) {
|
|
|
102
102
|
return a === b;
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
105
|
-
* Mirror of `mongo-
|
|
105
|
+
* Mirror of `mongo-view-expr.ts`'s `parseRegexString`: normalize a `$regex` value
|
|
106
106
|
* (or a bare `RegExp`) into a `{ pattern, flags }` pair. Accepts a `RegExp`
|
|
107
107
|
* instance, a `/pattern/flags` string literal, or a plain string (treated as
|
|
108
108
|
* the literal pattern with no flags).
|
|
@@ -193,10 +193,12 @@ function toOrdinal(v) {
|
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
195
|
* `$gt`/`$gte`/`$lt`/`$lte`. A missing or `null` field never matches an
|
|
196
|
-
* ordering comparison
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
196
|
+
* ordering comparison, and neither does a `null` operand — the visitor
|
|
197
|
+
* compiles that to "no match" before reaching here (SQL's UNKNOWN, MongoDB's
|
|
198
|
+
* type bracketing — JS would read it as `0`). `Date` operands are
|
|
199
|
+
* normalized to epoch millis; all other comparisons use plain JS ordering
|
|
200
|
+
* (numbers numerically, strings lexicographically) — NO collation or locale
|
|
201
|
+
* awareness. This intentionally differs from SQL engines' collated ordering.
|
|
200
202
|
*/
|
|
201
203
|
function evalRelational(fieldValue, op, value) {
|
|
202
204
|
if (fieldValue === void 0 || fieldValue === null) return false;
|
|
@@ -245,7 +247,9 @@ const memoryVisitor = {
|
|
|
245
247
|
case "$gt":
|
|
246
248
|
case "$gte":
|
|
247
249
|
case "$lt":
|
|
248
|
-
case "$lte":
|
|
250
|
+
case "$lte":
|
|
251
|
+
if (value == null) return () => false;
|
|
252
|
+
return (row) => evalRelational(read(row), op, value);
|
|
249
253
|
case "$in": {
|
|
250
254
|
const isIn = compileIn(value);
|
|
251
255
|
return (row) => isIn(read(row));
|
|
@@ -745,7 +749,16 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
745
749
|
* `@uniqu/core` kernel label (the SQLite UDF runs the same kernel);
|
|
746
750
|
* - `sum` / `avg` over no numeric value are `null`; `min` / `max` order with
|
|
747
751
|
* the `$sort` comparator; `countDistinct` counts distinct non-null values
|
|
748
|
-
* by group identity (below)
|
|
752
|
+
* by group identity (below) — over a field list (since 0.1.168), distinct
|
|
753
|
+
* combinations, a row with any null / missing field not counted; any other
|
|
754
|
+
* `$fn` is `INVALID_QUERY`;
|
|
755
|
+
* - a conditional aggregate (`$where`, since 0.1.168) feeds its accumulator
|
|
756
|
+
* only the rows its condition matches — the adapter's own filter predicate
|
|
757
|
+
* ({@link buildMemoryPredicate}), so it reads like the query filter;
|
|
758
|
+
* - a two-level aggregate (`controls.$from`, since 0.1.168) groups the rows
|
|
759
|
+
* of its inner level (this function over `$from`) by the outer controls —
|
|
760
|
+
* every inner column (an inner `$groupBy` key at its path, an inner alias
|
|
761
|
+
* at the top) is read like a field of a table row;
|
|
749
762
|
* - group identity is type-tagged: `1` and `"1"` never share a group (a
|
|
750
763
|
* number and a bigint of one value do), `Date`s group by instant, JSON
|
|
751
764
|
* values by a key-order-independent serialization;
|
|
@@ -754,16 +767,17 @@ function tupleTaken(index, tuple, excludeKey) {
|
|
|
754
767
|
* object-valued leaves copied so no store-owned value escapes.
|
|
755
768
|
*/
|
|
756
769
|
function aggregateRows(rows, controls) {
|
|
770
|
+
if (controls.$from) rows = aggregateRows(rows, controls.$from);
|
|
757
771
|
const $select = controls.$select;
|
|
758
772
|
const groupBy = controls.$groupBy ?? [];
|
|
759
773
|
const entries = [
|
|
760
774
|
...($select?.aggregates ?? []).map((expr) => ({
|
|
761
775
|
alias: resolveAlias(expr),
|
|
762
|
-
create: accumulatorFactory(expr)
|
|
776
|
+
create: conditional(accumulatorFactory(expr), expr.$where)
|
|
763
777
|
})),
|
|
764
778
|
...($select?.exprAggregates ?? []).map((e) => ({
|
|
765
779
|
alias: e.alias,
|
|
766
|
-
create: exprAccumulatorFactory(e)
|
|
780
|
+
create: conditional(exprAccumulatorFactory(e), e.where)
|
|
767
781
|
})),
|
|
768
782
|
...($select?.firstLast ?? []).map((fl) => ({
|
|
769
783
|
alias: fl.alias,
|
|
@@ -871,6 +885,23 @@ function numericValue(value) {
|
|
|
871
885
|
return Number.isNaN(n) ? void 0 : n;
|
|
872
886
|
}
|
|
873
887
|
}
|
|
888
|
+
/**
|
|
889
|
+
* An accumulator fed only the rows `where` matches (a conditional aggregate,
|
|
890
|
+
* since 0.1.168) — `create` unchanged without a condition.
|
|
891
|
+
*/
|
|
892
|
+
function conditional(create, where) {
|
|
893
|
+
if (where === void 0) return create;
|
|
894
|
+
const matches = buildMemoryPredicate(where);
|
|
895
|
+
return () => {
|
|
896
|
+
const acc = create();
|
|
897
|
+
return {
|
|
898
|
+
add: (row) => {
|
|
899
|
+
if (matches(row)) acc.add(row);
|
|
900
|
+
},
|
|
901
|
+
result: () => acc.result()
|
|
902
|
+
};
|
|
903
|
+
};
|
|
904
|
+
}
|
|
874
905
|
/** `sum` / `avg` / `min` / `max` over a per-row arithmetic expression; NULL results are skipped like NULL fields. */
|
|
875
906
|
function exprAccumulatorFactory(e) {
|
|
876
907
|
const readers = new Map(e.names.map((name) => [name, pathReader(name)]));
|
|
@@ -941,12 +972,39 @@ function fnAccumulator(fn, read) {
|
|
|
941
972
|
};
|
|
942
973
|
}
|
|
943
974
|
/**
|
|
975
|
+
* `countDistinct` over a field list: distinct combinations by their
|
|
976
|
+
* length-prefixed {@link identityToken}s (type-tagged — `1` and `"1"`
|
|
977
|
+
* differ; no value forges a separator); a row with any null / missing field
|
|
978
|
+
* is not counted (SQL's `COUNT(DISTINCT a, b)`).
|
|
979
|
+
*/
|
|
980
|
+
function distinctListFactory(fields) {
|
|
981
|
+
const readers = fields.map((field) => pathReader(field));
|
|
982
|
+
return () => {
|
|
983
|
+
const seen = /* @__PURE__ */ new Set();
|
|
984
|
+
return {
|
|
985
|
+
add: (row) => {
|
|
986
|
+
let identity = "";
|
|
987
|
+
for (const read of readers) {
|
|
988
|
+
const v = read(row);
|
|
989
|
+
if (v == null) return;
|
|
990
|
+
const token = identityToken(v);
|
|
991
|
+
identity += `${token.length}:${token}`;
|
|
992
|
+
}
|
|
993
|
+
seen.add(identity);
|
|
994
|
+
},
|
|
995
|
+
result: () => seen.size
|
|
996
|
+
};
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
944
1000
|
* Resolves a plain aggregate (`first` / `last` are {@link firstLastFactory}'s)
|
|
945
|
-
* to an accumulator factory (one accumulator per group
|
|
946
|
-
* so an unknown `$fn` is
|
|
1001
|
+
* to an accumulator factory (one accumulator per group; a `$where` is applied
|
|
1002
|
+
* by {@link conditional}). Validated up front, so an unknown `$fn` is
|
|
1003
|
+
* rejected even when there are no rows.
|
|
947
1004
|
*/
|
|
948
1005
|
function accumulatorFactory(expr) {
|
|
949
1006
|
assertAggregateFn(expr.$fn);
|
|
1007
|
+
if (typeof expr.$field !== "string") return distinctListFactory(aggregateColumns(expr));
|
|
950
1008
|
const field = expr.$field;
|
|
951
1009
|
const read = pathReader(field);
|
|
952
1010
|
if (expr.$fn === "count") {
|
|
@@ -1727,7 +1785,9 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1727
1785
|
* pure {@link aggregateRows} engine, so provider (read-through) mode works
|
|
1728
1786
|
* unchanged. SQL-adapter semantics: null and missing group together,
|
|
1729
1787
|
* `sum` / `avg` over no numeric value are `null`, `$count` counts the groups
|
|
1730
|
-
* that survive `$having`.
|
|
1788
|
+
* that survive `$having`. Conditional aggregates, `countDistinct` field
|
|
1789
|
+
* lists and two-level aggregates (`$from`, over the same snapshot) run in
|
|
1790
|
+
* the engine too (since 0.1.168).
|
|
1731
1791
|
*
|
|
1732
1792
|
* `$search` never reaches here: this adapter is not searchable, so the core
|
|
1733
1793
|
* rejects a grouped search before dispatch.
|
|
@@ -1751,6 +1811,18 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1751
1811
|
supportsAggregateExpressions() {
|
|
1752
1812
|
return true;
|
|
1753
1813
|
}
|
|
1814
|
+
/** Conditional aggregates (`$where`) — the memory filter predicate over each row. @since 0.1.168 */
|
|
1815
|
+
supportsConditionalAggregates() {
|
|
1816
|
+
return true;
|
|
1817
|
+
}
|
|
1818
|
+
/** `countDistinct` over a field list — distinct combinations by type-tagged identity. @since 0.1.168 */
|
|
1819
|
+
supportsDistinctFieldLists() {
|
|
1820
|
+
return true;
|
|
1821
|
+
}
|
|
1822
|
+
/** Two-level aggregates (`$from`) — the grouping engine over its own output rows. @since 0.1.168 */
|
|
1823
|
+
supportsNestedAggregates() {
|
|
1824
|
+
return true;
|
|
1825
|
+
}
|
|
1754
1826
|
/** NULL placement (`$nulls`, `@db.sort.nulls`) — `sortRows` honours it per key. */
|
|
1755
1827
|
supportsNullsPlacement() {
|
|
1756
1828
|
return true;
|
|
@@ -1818,16 +1890,36 @@ var MemoryAdapter = class MemoryAdapter extends BaseDbAdapter {
|
|
|
1818
1890
|
* Records the model's `unique` indexes for insert-time enforcement. Idempotent
|
|
1819
1891
|
* (replaces the recorded set). Non-unique index types are ignored — an
|
|
1820
1892
|
* in-memory scan needs no plain/fulltext/geo index to answer queries.
|
|
1893
|
+
*
|
|
1894
|
+
* A unique index the stored rows already break is not recorded and the call
|
|
1895
|
+
* throws `CONFLICT` naming it — as `CREATE UNIQUE INDEX` fails over
|
|
1896
|
+
* duplicates on SQL — after recording the indexes that hold.
|
|
1821
1897
|
*/
|
|
1822
1898
|
async syncIndexes() {
|
|
1899
|
+
const state = this._state();
|
|
1823
1900
|
const uniqueIndexes = [];
|
|
1901
|
+
const broken = [];
|
|
1824
1902
|
for (const index of this._table.indexes.values()) {
|
|
1825
1903
|
if (index.type !== "unique") continue;
|
|
1826
|
-
|
|
1904
|
+
const recorded = recordUniqueIndex(index.name, index.fields.map((f) => f.name), new Set(index.fields.filter((f) => f.optional).map((f) => f.name)));
|
|
1905
|
+
let holds = true;
|
|
1906
|
+
for (const [key, row] of state.rows) {
|
|
1907
|
+
const tuple = uniqueTupleKey(recorded, row);
|
|
1908
|
+
if (tuple === void 0) continue;
|
|
1909
|
+
if (recorded.entries.has(tuple)) {
|
|
1910
|
+
holds = false;
|
|
1911
|
+
break;
|
|
1912
|
+
}
|
|
1913
|
+
recorded.entries.set(tuple, new Set([key]));
|
|
1914
|
+
}
|
|
1915
|
+
(holds ? uniqueIndexes : broken).push(recorded);
|
|
1827
1916
|
}
|
|
1828
|
-
const state = this._state();
|
|
1829
1917
|
state.uniqueIndexes = uniqueIndexes;
|
|
1830
|
-
|
|
1918
|
+
const [first] = broken;
|
|
1919
|
+
if (first) throw constraintViolation("CONFLICT", {
|
|
1920
|
+
name: first.name,
|
|
1921
|
+
columns: first.fields
|
|
1922
|
+
}, `Cannot create unique index ${broken.map((i) => `"${i.name}"`).join(", ")}: stored rows hold duplicate values`);
|
|
1831
1923
|
}
|
|
1832
1924
|
/** Creates the table's (empty) state when it does not exist. Idempotent. */
|
|
1833
1925
|
async ensureTable() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-memory",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.168",
|
|
4
4
|
"description": "In-memory adapter for atscript-db.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -36,16 +36,16 @@
|
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@atscript/core": "^0.2.
|
|
40
|
-
"@atscript/typescript": "^0.2.
|
|
41
|
-
"@uniqu/core": "^0.
|
|
42
|
-
"unplugin-atscript": "^0.2.
|
|
39
|
+
"@atscript/core": "^0.2.4",
|
|
40
|
+
"@atscript/typescript": "^0.2.4",
|
|
41
|
+
"@uniqu/core": "^0.2.0",
|
|
42
|
+
"unplugin-atscript": "^0.2.4"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@atscript/core": "^0.2.
|
|
46
|
-
"@atscript/typescript": "^0.2.
|
|
47
|
-
"@uniqu/core": "^0.
|
|
48
|
-
"@atscript/db": "^0.1.
|
|
45
|
+
"@atscript/core": "^0.2.4",
|
|
46
|
+
"@atscript/typescript": "^0.2.4",
|
|
47
|
+
"@uniqu/core": "^0.2.0",
|
|
48
|
+
"@atscript/db": "^0.1.168"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "vp pack",
|