foldkit-entity 0.16.0 → 0.18.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/README.md +582 -240
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +30 -4
- package/dist/conformance/index.js.map +1 -1
- package/dist/conformance/operator.d.ts.map +1 -1
- package/dist/conformance/operator.js +13 -4
- package/dist/conformance/operator.js.map +1 -1
- package/dist/descriptor.d.ts +5 -1
- package/dist/descriptor.d.ts.map +1 -1
- package/dist/descriptor.js +6 -2
- package/dist/descriptor.js.map +1 -1
- package/dist/entity.d.ts +16 -3
- package/dist/entity.d.ts.map +1 -1
- package/dist/entity.js +44 -14
- package/dist/entity.js.map +1 -1
- package/dist/enumerate.d.ts +44 -0
- package/dist/enumerate.d.ts.map +1 -0
- package/dist/enumerate.js +128 -0
- package/dist/enumerate.js.map +1 -0
- package/dist/evaluate.d.ts +12 -6
- package/dist/evaluate.d.ts.map +1 -1
- package/dist/evaluate.js +48 -23
- package/dist/evaluate.js.map +1 -1
- package/dist/expr.d.ts +78 -22
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +86 -57
- package/dist/expr.js.map +1 -1
- package/dist/footprint.d.ts +120 -0
- package/dist/footprint.d.ts.map +1 -0
- package/dist/footprint.js +41 -0
- package/dist/footprint.js.map +1 -0
- package/dist/held.js +11 -3
- package/dist/held.js.map +1 -1
- package/dist/index.d.ts +24 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -6
- package/dist/index.js.map +1 -1
- package/dist/invariant.d.ts +41 -11
- package/dist/invariant.d.ts.map +1 -1
- package/dist/invariant.js +102 -51
- package/dist/invariant.js.map +1 -1
- package/dist/law.d.ts.map +1 -1
- package/dist/law.js +3 -9
- package/dist/law.js.map +1 -1
- package/dist/lawCore.d.ts +42 -0
- package/dist/lawCore.d.ts.map +1 -0
- package/dist/lawCore.js +12 -0
- package/dist/lawCore.js.map +1 -0
- package/dist/lawIds.d.ts +15 -0
- package/dist/lawIds.d.ts.map +1 -0
- package/dist/lawIds.js +13 -0
- package/dist/lawIds.js.map +1 -0
- package/dist/laws.d.ts +308 -0
- package/dist/laws.d.ts.map +1 -0
- package/dist/laws.js +566 -0
- package/dist/laws.js.map +1 -0
- package/dist/memoryTable.d.ts +11 -0
- package/dist/memoryTable.d.ts.map +1 -0
- package/dist/memoryTable.js +69 -0
- package/dist/memoryTable.js.map +1 -0
- package/dist/operators.d.ts +1 -1
- package/dist/operators.d.ts.map +1 -1
- package/dist/operators.js +2 -66
- package/dist/operators.js.map +1 -1
- package/dist/refine.d.ts +64 -0
- package/dist/refine.d.ts.map +1 -0
- package/dist/refine.js +104 -0
- package/dist/refine.js.map +1 -0
- package/dist/rows.d.ts +15 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +23 -0
- package/dist/rows.js.map +1 -0
- package/dist/sets.js.map +1 -1
- package/dist/uncollated.d.ts +10 -0
- package/dist/uncollated.d.ts.map +1 -0
- package/dist/uncollated.js +18 -0
- package/dist/uncollated.js.map +1 -0
- package/dist/write.d.ts +65 -7
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +128 -30
- package/dist/write.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Data, Match, Option, Schema, SchemaAST } from "effect";
|
|
2
|
+
import { own, put } from "./record.js";
|
|
3
|
+
import { heldRow } from "./rows.js";
|
|
4
|
+
import { Judge } from "./evaluate.js";
|
|
5
|
+
import { Query } from "./expr.js";
|
|
6
|
+
import { kernelOperators } from "./memoryTable.js";
|
|
7
|
+
// src/enumerate.ts
|
|
8
|
+
/**
|
|
9
|
+
* The enumerator `Laws.check` and a write's `becomes` share: every row a
|
|
10
|
+
* declared write can start from, over the finite members asked about, with the
|
|
11
|
+
* inputs it can be given and the row it leaves. No solver: a member that is not
|
|
12
|
+
* finite, or that the write changes otherwise than by setting it from its
|
|
13
|
+
* input, makes the question undecided, with the reason.
|
|
14
|
+
*/
|
|
15
|
+
/** The most rows (rows before, times inputs) one question is enumerated over. */
|
|
16
|
+
const maxRows = 4096;
|
|
17
|
+
/**
|
|
18
|
+
* The values a stored form can hold, when they are few: a literal, a union of
|
|
19
|
+
* literals, a boolean, each with or without null. None for anything else.
|
|
20
|
+
*/
|
|
21
|
+
const finiteValues = (schema) => valuesOf(Schema.toEncoded(schema).ast);
|
|
22
|
+
const valuesOf = (ast) => {
|
|
23
|
+
if (SchemaAST.isLiteral(ast)) return Option.some([ast.literal]);
|
|
24
|
+
if (SchemaAST.isNull(ast)) return Option.some([null]);
|
|
25
|
+
if (SchemaAST.isBoolean(ast)) return Option.some([true, false]);
|
|
26
|
+
if (SchemaAST.isUnion(ast)) return Option.map(Option.all(ast.types.map(valuesOf)), (each) => [...new Set(each.flat())]);
|
|
27
|
+
return Option.none();
|
|
28
|
+
};
|
|
29
|
+
const Steps = Data.taggedEnum();
|
|
30
|
+
const Change = Data.taggedEnum();
|
|
31
|
+
/** The values an input key gives a field, encoded as the field stores them: its own values when the input's are not few. */
|
|
32
|
+
const setValues = (write, key, field) => {
|
|
33
|
+
const schema = write.input.schema.fields[key];
|
|
34
|
+
const takes = Schema.is(Schema.toType(schema));
|
|
35
|
+
const optional = SchemaAST.isOptional(schema.ast) || takes(void 0) || takes(Option.none());
|
|
36
|
+
const encode = Schema.encodeUnknownOption(field.schema);
|
|
37
|
+
const fromInput = Option.map(valuesOf(Schema.toType(schema).ast), (values) => values.flatMap((value) => Option.toArray(encode(value))));
|
|
38
|
+
return Option.map(Option.orElse(fromInput, () => finiteValues(field.schema)), (values) => ({
|
|
39
|
+
values,
|
|
40
|
+
optional
|
|
41
|
+
}));
|
|
42
|
+
};
|
|
43
|
+
/** How `write` changes the member `key` of its row. */
|
|
44
|
+
const changeOf = (write, key) => {
|
|
45
|
+
const set = write.sets.find((each) => each.field.key === key);
|
|
46
|
+
if (set !== void 0) return Option.match(setValues(write, set.key, set.field), {
|
|
47
|
+
onNone: () => Change.Other({ reason: `${key} is set to any value its input holds` }),
|
|
48
|
+
onSome: ({ values, optional }) => Change.Set({
|
|
49
|
+
key: set.key,
|
|
50
|
+
values,
|
|
51
|
+
optional
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
if (write.increments.some((each) => each.field.key === key)) return Change.Other({ reason: `${key} is incremented` });
|
|
55
|
+
if (write.assignments.some((each) => each.field.key === key)) return Change.Other({ reason: `${key} is computed` });
|
|
56
|
+
if (write.links.some((each) => each.relation.key === key)) return Change.Other({ reason: `${key} is linked to another row` });
|
|
57
|
+
if (write.context.some((each) => each.member.key === key)) return Change.Other({ reason: `${key} is set by the write's context` });
|
|
58
|
+
if (write.expect?.field.key === key) return Change.Other({ reason: `${key} is the revision it expects, moved on where it lands` });
|
|
59
|
+
return Match.value(write).pipe(Match.tag("Insert", () => Change.Other({ reason: `${key} is not written by the insert, so the store decides it` })), Match.orElse(() => Change.Keep()));
|
|
60
|
+
};
|
|
61
|
+
/** Every combination of one value per key. */
|
|
62
|
+
const combinations = (choices) => choices.reduce((rows, [key, values]) => rows.flatMap((row) => values.map((value) => {
|
|
63
|
+
const next = { ...row };
|
|
64
|
+
put(next, key, value);
|
|
65
|
+
return next;
|
|
66
|
+
})), [{}]);
|
|
67
|
+
/** Absent from an input: the key the write is given nothing for. */
|
|
68
|
+
const absent = Symbol("absent");
|
|
69
|
+
/**
|
|
70
|
+
* Every step `write` can take over the members `members` names (field keys of
|
|
71
|
+
* its Entity): each row before over them and the finite members its `when`
|
|
72
|
+
* reads, dropping a row the `when` definitely refuses, with each input value
|
|
73
|
+
* of the keys that set them. Rows are not filtered by the Entity's
|
|
74
|
+
* invariants: a row may be invalid before a write.
|
|
75
|
+
*/
|
|
76
|
+
const steps = (write, members, options = {}) => {
|
|
77
|
+
const entity = write.input.entity;
|
|
78
|
+
const fields = entity.fields;
|
|
79
|
+
const insert = write._tag === "Insert";
|
|
80
|
+
const changes = /* @__PURE__ */ new Map();
|
|
81
|
+
for (const key of members) {
|
|
82
|
+
if (own(fields, key) === void 0) return Steps.Undecided({ reason: `${key} is a link, not a field` });
|
|
83
|
+
const change = changeOf(write, key);
|
|
84
|
+
if (change._tag === "Other") return Steps.Undecided({ reason: change.reason });
|
|
85
|
+
changes.set(key, change);
|
|
86
|
+
}
|
|
87
|
+
const infinite = insert ? void 0 : members.find((key) => Option.isNone(finiteValues(own(fields, key).schema)));
|
|
88
|
+
if (infinite !== void 0) return Steps.Undecided({ reason: `${infinite} is not finite` });
|
|
89
|
+
const condition = Option.filter(write.when, () => !insert);
|
|
90
|
+
const read = Option.map(condition, (body) => Query.dependencies(body));
|
|
91
|
+
const whenKeys = Option.match(read, {
|
|
92
|
+
onNone: () => [],
|
|
93
|
+
onSome: (dependencies) => dependencies.predicate.map((field) => field.key).filter((key) => !changes.has(key) && Option.isSome(Option.flatMap(Option.fromUndefinedOr(own(fields, key)), (field) => finiteValues(field.schema))))
|
|
94
|
+
});
|
|
95
|
+
const beforeChoices = insert ? [] : [.../* @__PURE__ */ new Set([...changes.keys(), ...whenKeys])].map((key) => [key, Option.getOrThrow(finiteValues(own(fields, key).schema))]);
|
|
96
|
+
const setKeys = [...changes.values()].filter(Change.$is("Set"));
|
|
97
|
+
const inputChoices = [...new Map(setKeys.map((set) => [set.key, set])).values()].map((set) => [set.key, set.optional ? [...set.values, absent] : set.values]);
|
|
98
|
+
if ([...beforeChoices, ...inputChoices].reduce((product, [, values]) => product * values.length, 1) > 4096) return Steps.Undecided({ reason: `more than ${maxRows} rows` });
|
|
99
|
+
const operators = options.operators ?? kernelOperators;
|
|
100
|
+
const judged = Option.filter(Option.zipWith(condition, read, (body, dependencies) => ({
|
|
101
|
+
body,
|
|
102
|
+
dependencies
|
|
103
|
+
})), ({ dependencies }) => dependencies.inputs.every((key) => inputChoices.some(([given]) => given === key)) && !dependencies.operations.some((operation) => operation === "viewer" || operation === "now"));
|
|
104
|
+
const result = [];
|
|
105
|
+
for (const before of combinations(beforeChoices)) for (const given of combinations(inputChoices)) {
|
|
106
|
+
const input = {};
|
|
107
|
+
for (const [key, value] of Object.entries(given)) if (value !== absent) put(input, key, value);
|
|
108
|
+
if (Option.match(judged, {
|
|
109
|
+
onNone: () => false,
|
|
110
|
+
onSome: ({ body }) => {
|
|
111
|
+
const row = heldRow(entity, before);
|
|
112
|
+
const verdict = Judge.query(body, input, row, { operators });
|
|
113
|
+
return verdict._tag === "Known" && !verdict.truth;
|
|
114
|
+
}
|
|
115
|
+
})) continue;
|
|
116
|
+
const after = { ...before };
|
|
117
|
+
for (const [key, change] of changes) if (change._tag === "Set" && Object.hasOwn(input, change.key)) put(after, key, own(input, change.key));
|
|
118
|
+
result.push({
|
|
119
|
+
before: insert ? {} : before,
|
|
120
|
+
input,
|
|
121
|
+
after
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return Steps.Steps({ steps: result });
|
|
125
|
+
};
|
|
126
|
+
export { Steps, finiteValues, maxRows, steps };
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=enumerate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enumerate.js","names":[],"sources":["../src/enumerate.ts"],"sourcesContent":["/**\n * The enumerator `Laws.check` and a write's `becomes` share: every row a\n * declared write can start from, over the finite members asked about, with the\n * inputs it can be given and the row it leaves. No solver: a member that is not\n * finite, or that the write changes otherwise than by setting it from its\n * input, makes the question undecided, with the reason.\n */\nimport { Data, Match, Option, Schema, SchemaAST } from 'effect'\nimport { Judge, type Row } from './evaluate.js'\nimport { Query } from './expr.js'\nimport type { AnyEntity, EntityField } from './index.js'\nimport { kernelOperators } from './memoryTable.js'\nimport type { MemoryOperators } from './operators.js'\nimport { own, put } from './record.js'\nimport { heldRow } from './rows.js'\nimport type { AnyWrite } from './write.js'\n\ntype AnyField = EntityField<string, string, Schema.Constraint>\n\n/** The most rows (rows before, times inputs) one question is enumerated over. */\nexport const maxRows = 4096\n\n/**\n * The values a stored form can hold, when they are few: a literal, a union of\n * literals, a boolean, each with or without null. None for anything else.\n */\nexport const finiteValues = (schema: Schema.Top): Option.Option<ReadonlyArray<unknown>> =>\n valuesOf(Schema.toEncoded(schema).ast)\n\nconst valuesOf = (ast: SchemaAST.AST): Option.Option<ReadonlyArray<unknown>> => {\n if (SchemaAST.isLiteral(ast)) return Option.some([ast.literal])\n if (SchemaAST.isNull(ast)) return Option.some([null])\n if (SchemaAST.isBoolean(ast)) return Option.some([true, false])\n if (SchemaAST.isUnion(ast)) {\n return Option.map(Option.all(ast.types.map(valuesOf)), each => [...new Set(each.flat())])\n }\n return Option.none()\n}\n\n/** One row a write can start from, an input it can be given, and the row it leaves. */\nexport interface Step {\n readonly before: Row\n readonly input: Row\n readonly after: Row\n}\n\n/** What enumerating a write comes to: every step, or why there is no list of them. */\nexport type Steps = Data.TaggedEnum<{\n Steps: { readonly steps: ReadonlyArray<Step> }\n Undecided: { readonly reason: string }\n}>\nexport const Steps = Data.taggedEnum<Steps>()\n\n/** How a write changes one member: leaves it, sets it to one of a few values, or otherwise. */\ntype Change = Data.TaggedEnum<{\n Keep: {}\n Set: { readonly key: string; readonly values: ReadonlyArray<unknown>; readonly optional: boolean }\n Other: { readonly reason: string }\n}>\nconst Change = Data.taggedEnum<Change>()\n\n/** The values an input key gives a field, encoded as the field stores them: its own values when the input's are not few. */\nconst setValues = (\n write: AnyWrite,\n key: string,\n field: AnyField,\n): Option.Option<{ readonly values: ReadonlyArray<unknown>; readonly optional: boolean }> => {\n const schema = write.input.schema.fields[key] as unknown as Schema.Top\n const takes = Schema.is(Schema.toType(schema) as Schema.Codec<unknown>)\n // A key that may be absent, or an `Option` holding nothing, writes nothing.\n const optional = SchemaAST.isOptional(schema.ast) || takes(undefined) || takes(Option.none())\n const encode = Schema.encodeUnknownOption(\n field.schema as unknown as Schema.Codec<unknown, unknown>,\n )\n const fromInput = Option.map(valuesOf(Schema.toType(schema).ast), values =>\n values.flatMap(value => Option.toArray(encode(value))),\n )\n return Option.map(\n Option.orElse(fromInput, () => finiteValues(field.schema as unknown as Schema.Top)),\n values => ({ values, optional }),\n )\n}\n\n/** How `write` changes the member `key` of its row. */\nconst changeOf = (write: AnyWrite, key: string): Change => {\n const set = write.sets.find(each => each.field.key === key)\n if (set !== undefined) {\n return Option.match(setValues(write, set.key, set.field), {\n onNone: () => Change.Other({ reason: `${key} is set to any value its input holds` }),\n onSome: ({ values, optional }) => Change.Set({ key: set.key, values, optional }),\n })\n }\n if (write.increments.some(each => each.field.key === key))\n return Change.Other({ reason: `${key} is incremented` })\n if (write.assignments.some(each => each.field.key === key))\n return Change.Other({ reason: `${key} is computed` })\n if (write.links.some(each => each.relation.key === key))\n return Change.Other({ reason: `${key} is linked to another row` })\n if (write.context.some(each => each.member.key === key))\n return Change.Other({ reason: `${key} is set by the write's context` })\n if (write.expect?.field.key === key)\n return Change.Other({ reason: `${key} is the revision it expects, moved on where it lands` })\n return Match.value(write).pipe(\n Match.tag('Insert', () =>\n Change.Other({ reason: `${key} is not written by the insert, so the store decides it` }),\n ),\n Match.orElse(() => Change.Keep()),\n )\n}\n\n/** Every combination of one value per key. */\nconst combinations = (\n choices: ReadonlyArray<readonly [string, ReadonlyArray<unknown>]>,\n): ReadonlyArray<Row> =>\n choices.reduce<ReadonlyArray<Row>>(\n (rows, [key, values]) =>\n rows.flatMap(row =>\n values.map(value => {\n const next: Record<string, unknown> = { ...row }\n put(next, key, value)\n return next\n }),\n ),\n [{}],\n )\n\n/** Absent from an input: the key the write is given nothing for. */\nconst absent: unique symbol = Symbol('absent')\n\n/**\n * Every step `write` can take over the members `members` names (field keys of\n * its Entity): each row before over them and the finite members its `when`\n * reads, dropping a row the `when` definitely refuses, with each input value\n * of the keys that set them. Rows are not filtered by the Entity's\n * invariants: a row may be invalid before a write.\n */\nexport const steps = (\n write: AnyWrite,\n members: ReadonlyArray<string>,\n options: { readonly operators?: MemoryOperators | undefined } = {},\n): Steps => {\n const entity: AnyEntity = write.input.entity\n const fields = entity.fields as Readonly<Record<string, AnyField>>\n const insert = write._tag === 'Insert'\n const changes = new Map<string, Change>()\n // How the write changes a member says more than that the member is not finite, so it is asked first.\n for (const key of members) {\n if (own(fields, key) === undefined)\n return Steps.Undecided({ reason: `${key} is a link, not a field` })\n const change = changeOf(write, key)\n if (change._tag === 'Other') return Steps.Undecided({ reason: change.reason })\n changes.set(key, change)\n }\n const infinite = insert\n ? undefined\n : members.find(key =>\n Option.isNone(finiteValues(own(fields, key)!.schema as unknown as Schema.Top)),\n )\n if (infinite !== undefined) return Steps.Undecided({ reason: `${infinite} is not finite` })\n // What the `when` reads joins the rows when it is finite, so a row it refuses is dropped.\n const condition = Option.filter(write.when, () => !insert)\n const read = Option.map(condition, body => Query.dependencies(body))\n const whenKeys = Option.match(read, {\n onNone: () => [],\n onSome: dependencies =>\n dependencies.predicate\n .map(field => field.key)\n .filter(\n key =>\n !changes.has(key) &&\n Option.isSome(\n Option.flatMap(Option.fromUndefinedOr(own(fields, key)), field =>\n finiteValues(field.schema as unknown as Schema.Top),\n ),\n ),\n ),\n })\n const beforeChoices = insert\n ? []\n : [...new Set([...changes.keys(), ...whenKeys])].map(\n key =>\n [\n key,\n Option.getOrThrow(finiteValues(own(fields, key)!.schema as unknown as Schema.Top)),\n ] as const,\n )\n const setKeys = [...changes.values()].filter(Change.$is('Set'))\n const inputChoices = [...new Map(setKeys.map(set => [set.key, set])).values()].map(\n set => [set.key, set.optional ? [...set.values, absent] : set.values] as const,\n )\n const size = [...beforeChoices, ...inputChoices].reduce(\n (product, [, values]) => product * values.length,\n 1,\n )\n if (size > maxRows) return Steps.Undecided({ reason: `more than ${maxRows} rows` })\n const operators = options.operators ?? kernelOperators\n // The `when` is judged only when what it reads is held: an input it reads that is\n // not enumerated, or the write's context, would read as nothing and refuse wrongly.\n const judged = Option.filter(\n Option.zipWith(condition, read, (body, dependencies) => ({ body, dependencies })),\n ({ dependencies }) =>\n dependencies.inputs.every(key => inputChoices.some(([given]) => given === key)) &&\n !dependencies.operations.some(operation => operation === 'viewer' || operation === 'now'),\n )\n const result: Array<Step> = []\n for (const before of combinations(beforeChoices)) {\n for (const given of combinations(inputChoices)) {\n const input: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(given)) if (value !== absent) put(input, key, value)\n const refused = Option.match(judged, {\n onNone: () => false,\n onSome: ({ body }) => {\n const row = heldRow(entity, before)\n const verdict = Judge.query(body, input, row, { operators })\n return verdict._tag === 'Known' && !verdict.truth\n },\n })\n if (refused) continue\n const after: Record<string, unknown> = { ...before }\n for (const [key, change] of changes) {\n if (change._tag === 'Set' && Object.hasOwn(input, change.key))\n put(after, key, own(input, change.key))\n }\n result.push({ before: insert ? {} : before, input, after })\n }\n }\n return Steps.Steps({ steps: result })\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoBA,MAAa,UAAU;;;;;AAMvB,MAAa,gBAAgB,WAC3B,SAAS,OAAO,UAAU,MAAM,CAAC,CAAC,GAAG;AAEvC,MAAM,YAAY,QAA8D;CAC9E,IAAI,UAAU,UAAU,GAAG,GAAG,OAAO,OAAO,KAAK,CAAC,IAAI,OAAO,CAAC;CAC9D,IAAI,UAAU,OAAO,GAAG,GAAG,OAAO,OAAO,KAAK,CAAC,IAAI,CAAC;CACpD,IAAI,UAAU,UAAU,GAAG,GAAG,OAAO,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;CAC9D,IAAI,UAAU,QAAQ,GAAG,GACvB,OAAO,OAAO,IAAI,OAAO,IAAI,IAAI,MAAM,IAAI,QAAQ,CAAC,IAAG,SAAQ,CAAC,GAAG,IAAI,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC;CAE1F,OAAO,OAAO,KAAK;AACrB;AAcA,MAAa,QAAQ,KAAK,WAAkB;AAQ5C,MAAM,SAAS,KAAK,WAAmB;;AAGvC,MAAM,aACJ,OACA,KACA,UAC2F;CAC3F,MAAM,SAAS,MAAM,MAAM,OAAO,OAAO;CACzC,MAAM,QAAQ,OAAO,GAAG,OAAO,OAAO,MAAM,CAA0B;CAEtE,M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AAa,EAAE,IAC/E,EAAE,mBACD,aAAa,OAAO,OAAM,QAAO,aAAa,MAAM,CAAC,WAAW,UAAU,GAAG,CAAC,KAC9E,CAAC,aAAa,WAAW,MAAK,cAAa,cAAc,YAAY,cAAc,KAAK,CAC5F;CACA,MAAM,SAAsB,CAAC;CAC7B,KAAK,MAAM,UAAU,aAAa,aAAa,GAC7C,KAAK,MAAM,SAAS,aAAa,YAAY,GAAG;EAC9C,MAAM,QAAiC,CAAC;EACxC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,KAAK,GAAG,IAAI,UAAU,QAAQ,IAAI,OAAO,KAAK,KAAK;EAS7F,IARgB,OAAO,MAAM,QAAQ;GACnC,cAAc;GACd,SAAS,EAAE,WAAW;IACpB,MAAM,MAAM,QAAQ,QAAQ,MAAM;IAClC,MAAM,UAAU,MAAM,MAAM,MAAM,OAAO,KAAK,EAAE,UAAU,CAAC;IAC3D,OAAO,QAAQ,SAAS,WAAW,CAAC,QAAQ;GAC9C;EACF,CACU,GAAG;EACb,MAAM,QAAiC,EAAE,GAAG,OAAO;EACnD,KAAK,MAAM,CAAC,KAAK,WAAW,SAC1B,IAAI,OAAO,SAAS,SAAS,OAAO,OAAO,OAAO,OAAO,GAAG,GAC1D,IAAI,OAAO,KAAK,IAAI,OAAO,OAAO,GAAG,CAAC;EAE1C,OAAO,KAAK;GAAE,QAAQ,SAAS,CAAC,IAAI;GAAQ;GAAO;EAAM,CAAC;CAC5D;CAEF,OAAO,MAAM,MAAM,EAAE,OAAO,OAAO,CAAC;AACtC"}
|
package/dist/evaluate.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Option } from "effect";
|
|
2
|
-
import { Collation } from "./collation.js";
|
|
3
2
|
import { AnyQuery, Operandish, Operation, OperatorDescriptor, OperatorTable, OrderTerm, Predicate } from "./expr.js";
|
|
3
|
+
import { Collation } from "./collation.js";
|
|
4
4
|
import { HeldRow, HeldRows, Pinned } from "./held.js";
|
|
5
|
-
import { MemoryOperators } from "./operators.js";
|
|
6
5
|
import { ExprContext, NowExpr, ViewerExpr } from "./context.js";
|
|
6
|
+
import { MemoryOperators } from "./operators.js";
|
|
7
7
|
/** A row as this interpreter reads one: values by field key. */
|
|
8
8
|
export type Row = Readonly<Record<string, unknown>>;
|
|
9
9
|
export declare class QueryEvaluateError extends Error {
|
|
@@ -144,9 +144,9 @@ export declare const Judge: {
|
|
|
144
144
|
* as `evaluate` sorts: negative when it comes first. A call among them runs
|
|
145
145
|
* over each row with `input` and `operators` (the kernel's when not given).
|
|
146
146
|
* Refuses what `evaluate` refuses, for these two rows: a term over neither a
|
|
147
|
-
* field nor a call, a locale collation,
|
|
148
|
-
* operator the table lacks. For a client placing
|
|
149
|
-
* holds.
|
|
147
|
+
* field nor a call, a locale collation, text with no declared collation, keys
|
|
148
|
+
* of different kinds, or an operator the table lacks. For a client placing
|
|
149
|
+
* one row among others it holds.
|
|
150
150
|
*/
|
|
151
151
|
export declare const compareRows: (terms: ReadonlyArray<OrderTerm>, left: Row, right: Row, query: string, options?: {
|
|
152
152
|
readonly input?: Row | undefined;
|
|
@@ -173,6 +173,10 @@ export declare const assertSupported: (body: AnyQuery, operators?: MemoryOperato
|
|
|
173
173
|
* `input` holds the values this run was given, by the keys the body's
|
|
174
174
|
* placeholders were built with — the same record the query would be called
|
|
175
175
|
* with anywhere else.
|
|
176
|
+
*
|
|
177
|
+
* The order is part of the answer, so an order over text with no declared
|
|
178
|
+
* collation is refused (`Query.placement`'s reason): declare one on the field,
|
|
179
|
+
* or on the term.
|
|
176
180
|
*/
|
|
177
181
|
export declare const evaluate: (body: AnyQuery, input: Row, rows: ReadonlyArray<Row>, options?: {
|
|
178
182
|
readonly operators?: MemoryOperators | undefined;
|
|
@@ -193,7 +197,9 @@ export interface Evaluated {
|
|
|
193
197
|
* What no single row causes still refuses the run: an operation or operator
|
|
194
198
|
* the table lacks, an order by a locale collation, an input the body reads
|
|
195
199
|
* holding a NUL (every row would refuse, and no row is the cause), and order
|
|
196
|
-
* keys of different kinds across rows.
|
|
200
|
+
* keys of different kinds across rows. It judges membership, which no order
|
|
201
|
+
* decides, so an order over text with no declared collation does not refuse it:
|
|
202
|
+
* `matched` is then in code point order, which no other reader need follow.
|
|
197
203
|
*/
|
|
198
204
|
export declare const evaluateEach: (body: AnyQuery, input: Row, rows: ReadonlyArray<Row>, options?: {
|
|
199
205
|
readonly operators?: MemoryOperators | undefined;
|
package/dist/evaluate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evaluate.d.ts","names":[],"sources":["../src/evaluate.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"evaluate.d.ts","names":[],"sources":["../src/evaluate.ts"],"mappings":";;;;;;;YAmDY,MAAM,SAAS;qBAEd,2BAA2B;EACtC,YAAY;;;;;;;qBAWD,6BAA6B;WAC/B,UAAU;;WAEV;;EAET,YAAY,UAAU,oBAAoB,YAAW;;;;;;qBAyC1C,4BAA4B;EACvC,YAAY,MAAM,sBAAsB,kBAAkB;;;iBAyF3C;WACN,MAAM;WACN;;;;;;;qBAiIE,yBAAyB;EACpC,YAAY;;;;;;;qBA+GD,gBAAa,UACd,oBAAkB,QACpB,wBAAsB;WAEnB,WAAW,OAAO,OAAO;WACzB,WAAW;;;KA+BnB;;;;;;;;qBAoDQ,iBAAc,MACnB,YAAU;WAEL,QAAQ;WACR,YAAY;WACZ,UAAU;;;YAsMX,iBAAiB;;;;;;;;qBAYhB,UAAO,MACZ,WAAS,OACR,KAAG,YACC,iBAAe,UAChB,gBACT,OAAO,OAAO;;qBA8BJ,YAAS,MACd,WAAS,KACV,KAAG,QACD,KAAG,OACJ,KAAG,YACE,oBACV;;;;;;YAiBS,QAAQ,IAAI;WACT;WAAwB,OAAO;;WAC/B;WAA0B;;;UAM/B;WACC,QAAQ;WACR,YAAY;;WAEZ,UAAU;;;;;WAKV,OAAO;;;;;;;;;qBAqBL;;EAEX,YAAS,WACI,WAAS,KACf,SAAO,UACH;aAA0B,OAAO;QACzC;;;;;;;;;EAqBH,QAAK,MACG,UAAQ,OACP,KAAG,KACL,SAAO,UACH,KAAK;aAAoC;QACjD;;;;;;;;EAgCH,OAAI,YACU,cAAc,YAAU;aAChB,QAAQ;aAA0B,UAAU;QAC/D;EAEH,QAAQ,GAAG,GAAC,SACD,QAAQ,IAAE;aACD,UAAU,OAAO,MAAM;aAAY,YAAY,mBAAmB;QACnF;;;;;;;;;;;qBAqMQ,cAAW,OACf,cAAc,YAAU,MACzB,KAAG,OACF,KAAG,eACG;WAEF,QAAQ;WACR,YAAY;WACZ,UAAU;;;;;;;;qBAoBV,WAAW,cAAc;;;;;;qBAmBzB,kBAAe,MACpB,UAAQ,YACH;;;;;;;;;;;;;qBAsBA,WAAQ,MACb,UAAQ,OACP,KAAG,MACJ,cAAc,MAAI;WAEb,YAAY;;WAEZ,UAAU;MAEpB,cAAc;;iBAaA;WACN,SAAS,cAAc;WACvB,SAAS,cAAc;;;;;;;;;;;;;;;qBAgBrB,eAAY,MACjB,UAAQ,OACP,KAAG,MACJ,cAAc,MAAI;WAEb,YAAY;;WAEZ,UAAU;MAEpB"}
|
package/dist/evaluate.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { Match, Option, Schema, SchemaAST } from "effect";
|
|
2
2
|
import { own } from "./record.js";
|
|
3
|
+
import { Expr, Query, dependenciesOf, isPredicate, missingName, noOperators } from "./expr.js";
|
|
4
|
+
import { kernelOperators } from "./memoryTable.js";
|
|
3
5
|
import { absurd } from "effect/Function";
|
|
4
6
|
import { Collation } from "./collation.js";
|
|
5
|
-
import {
|
|
7
|
+
import { uncollatedText } from "./uncollated.js";
|
|
6
8
|
import { Held } from "./held.js";
|
|
7
|
-
import { Operators } from "./operators.js";
|
|
8
9
|
import { contextValue } from "./context.js";
|
|
9
10
|
// src/evaluate.ts
|
|
10
11
|
/**
|
|
@@ -134,12 +135,7 @@ const reached = (from, via) => {
|
|
|
134
135
|
/** A value two `eq`s agree on exactly, on both sides of the wire: never null, NaN or a structure. */
|
|
135
136
|
const pinnable = (value) => typeof value === "string" || typeof value === "boolean" || typeof value === "bigint" || typeof value === "number" && !Number.isNaN(value);
|
|
136
137
|
/** An `eq` of a field of the row and something else, the field first. */
|
|
137
|
-
const fieldAndSide = (
|
|
138
|
-
if (predicate._tag !== "Eq") return Option.none();
|
|
139
|
-
if (predicate.left._tag === "Field") return Option.some([predicate.left, predicate.right]);
|
|
140
|
-
if (predicate.right._tag === "Field") return Option.some([predicate.right, predicate.left]);
|
|
141
|
-
return Option.none();
|
|
142
|
-
};
|
|
138
|
+
const fieldAndSide = Match.type().pipe(Match.tag("Eq", ({ left, right }) => Option.orElse(Option.map(fieldOf(left), (field) => [field, right]), () => Option.map(fieldOf(right), (field) => [field, left]))), Match.orElse(() => Option.none()));
|
|
143
139
|
const pinsOf = (predicates, input, context) => {
|
|
144
140
|
const pins = /* @__PURE__ */ new Map();
|
|
145
141
|
let whole = true;
|
|
@@ -163,6 +159,9 @@ const pinsOf = (predicates, input, context) => {
|
|
|
163
159
|
whole
|
|
164
160
|
};
|
|
165
161
|
};
|
|
162
|
+
const fieldOf = Match.type().pipe(Match.tag("Field", (field) => Option.some(field)), Match.orElse(() => Option.none()));
|
|
163
|
+
/** A filter's conjuncts; built once, since `exists` asks for every row judged. */
|
|
164
|
+
const operandsOf = Match.type().pipe(Match.tag("And", ({ operands }) => operands), Match.orElse((single) => [single]));
|
|
166
165
|
/**
|
|
167
166
|
* Whether some row pointing at the one `via` reaches holds the filter: true
|
|
168
167
|
* as soon as a held one does, false only when every one is held and none
|
|
@@ -175,7 +174,7 @@ const exists = (node, scope) => {
|
|
|
175
174
|
if (isUnheld(anchor)) return anchor;
|
|
176
175
|
const pinned = Option.match(node.where, {
|
|
177
176
|
onNone: () => /* @__PURE__ */ new Map(),
|
|
178
|
-
onSome: (where) => pinsOf(where
|
|
177
|
+
onSome: (where) => pinsOf(operandsOf(where), scope.input, scope.context).pins
|
|
179
178
|
});
|
|
180
179
|
const { rows, missing } = anchor.pointing(node.relation, pinned);
|
|
181
180
|
let unheld = Option.map(missing, (reason) => new Unheld(reason));
|
|
@@ -331,7 +330,7 @@ const valueOfOperand = (node, options = {}) => {
|
|
|
331
330
|
row: noRow,
|
|
332
331
|
input: options.input ?? {},
|
|
333
332
|
next: noRow,
|
|
334
|
-
operators: options.operators ??
|
|
333
|
+
operators: options.operators ?? kernelOperators,
|
|
335
334
|
judging: false,
|
|
336
335
|
find: noRows,
|
|
337
336
|
context: options.context
|
|
@@ -394,6 +393,7 @@ const holds = (node, scope) => {
|
|
|
394
393
|
case "In": {
|
|
395
394
|
const among = valueOf(node.among, scope);
|
|
396
395
|
if (isUnheld(among)) return among;
|
|
396
|
+
if (isNull(among)) return "unknown";
|
|
397
397
|
if (!Array.isArray(among)) throw new QueryEvaluateError("a membership test was given a list that is not a list");
|
|
398
398
|
if (among.length === 0) return false;
|
|
399
399
|
const value = valueOf(node.value, scope);
|
|
@@ -449,7 +449,7 @@ const readsBeyond = /* @__PURE__ */ new WeakMap();
|
|
|
449
449
|
* predicate before it builds anything: `isNull($status)` is known before any
|
|
450
450
|
* row is. Refuses what `evaluate` refuses (text holding a NUL).
|
|
451
451
|
*/
|
|
452
|
-
const decided = (node, input, operators =
|
|
452
|
+
const decided = (node, input, operators = kernelOperators, context) => {
|
|
453
453
|
let reads = readsBeyond.get(node);
|
|
454
454
|
if (reads === void 0) {
|
|
455
455
|
const read = dependenciesOf(node);
|
|
@@ -470,7 +470,7 @@ const decided = (node, input, operators = Operators.kernel, context) => {
|
|
|
470
470
|
})));
|
|
471
471
|
};
|
|
472
472
|
/** A predicate's truth over one row (and, for an invariant, the row after a write): SQL's three values. */
|
|
473
|
-
const truthOver = (node, row, input = {}, next = {}, operators =
|
|
473
|
+
const truthOver = (node, row, input = {}, next = {}, operators = kernelOperators) => settled(holds(node, {
|
|
474
474
|
row: flat(row),
|
|
475
475
|
input,
|
|
476
476
|
next: flat(next),
|
|
@@ -508,7 +508,7 @@ const Judge = {
|
|
|
508
508
|
row: viewOf(row),
|
|
509
509
|
input: options.input ?? {},
|
|
510
510
|
next: options.next === void 0 ? noRow : viewOf(options.next),
|
|
511
|
-
operators: options.operators ??
|
|
511
|
+
operators: options.operators ?? kernelOperators,
|
|
512
512
|
judging: true,
|
|
513
513
|
context: options.context,
|
|
514
514
|
find: finder(options.rows)
|
|
@@ -529,7 +529,7 @@ const Judge = {
|
|
|
529
529
|
row: viewOf(row),
|
|
530
530
|
input,
|
|
531
531
|
next: noRow,
|
|
532
|
-
operators: options.operators ??
|
|
532
|
+
operators: options.operators ?? kernelOperators,
|
|
533
533
|
judging: true,
|
|
534
534
|
context: options.context,
|
|
535
535
|
find: finder(options.rows)
|
|
@@ -548,7 +548,12 @@ const Judge = {
|
|
|
548
548
|
* whose filter fixes the same values counts.
|
|
549
549
|
*/
|
|
550
550
|
pins: (predicates, options = {}) => pinsOf(predicates, options.input ?? {}, options.context),
|
|
551
|
-
match: (verdict, cases) =>
|
|
551
|
+
match: (verdict, cases) => {
|
|
552
|
+
switch (verdict._tag) {
|
|
553
|
+
case "Known": return cases.onKnown(verdict.truth);
|
|
554
|
+
case "Unknown": return cases.onUnknown(verdict.reason);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
552
557
|
};
|
|
553
558
|
/** Every predicate must hold: the list is the conjunction, as it is in a `Query`. */
|
|
554
559
|
const matches = (body, row, input, operators, context) => body.where.map((node) => settled(holds(node, {
|
|
@@ -623,6 +628,18 @@ const checkOrder = (keys, terms, query) => terms.forEach((term, index) => {
|
|
|
623
628
|
if (kind !== first || !comparable.has(kind)) throw new QueryEvaluateError(`query "${query}" orders by values this interpreter cannot compare (${first} and ${kind})`);
|
|
624
629
|
}
|
|
625
630
|
});
|
|
631
|
+
/**
|
|
632
|
+
* Refuses an order over text with no declared collation: JavaScript would
|
|
633
|
+
* answer by code point where SQLite compares bytes and Postgres follows its
|
|
634
|
+
* locale. Asked wherever the answer depends on the order, and not of a
|
|
635
|
+
* membership judged alone (`evaluateEach`), which no order decides.
|
|
636
|
+
*/
|
|
637
|
+
const checkCollated = (terms, query) => {
|
|
638
|
+
for (const term of terms) {
|
|
639
|
+
const uncollated = uncollatedText(term);
|
|
640
|
+
if (Option.isSome(uncollated)) throw new QueryEvaluateError(`query "${query}" cannot be ordered: ${uncollated.value}`);
|
|
641
|
+
}
|
|
642
|
+
};
|
|
626
643
|
/** Two rows' keys by checked terms: negative when `left` sorts first. */
|
|
627
644
|
const byTerms = (terms, left, right) => {
|
|
628
645
|
for (const [index, term] of terms.entries()) {
|
|
@@ -639,6 +656,7 @@ const byTerms = (terms, left, right) => {
|
|
|
639
656
|
};
|
|
640
657
|
const ordered = (rows, terms, query, scope) => {
|
|
641
658
|
if (terms.length === 0) return [...rows];
|
|
659
|
+
checkCollated(terms, query);
|
|
642
660
|
const keyed = rows.map((row) => ({
|
|
643
661
|
row,
|
|
644
662
|
keys: keysOf(row, terms, scope)
|
|
@@ -651,17 +669,18 @@ const ordered = (rows, terms, query, scope) => {
|
|
|
651
669
|
* as `evaluate` sorts: negative when it comes first. A call among them runs
|
|
652
670
|
* over each row with `input` and `operators` (the kernel's when not given).
|
|
653
671
|
* Refuses what `evaluate` refuses, for these two rows: a term over neither a
|
|
654
|
-
* field nor a call, a locale collation,
|
|
655
|
-
* operator the table lacks. For a client placing
|
|
656
|
-
* holds.
|
|
672
|
+
* field nor a call, a locale collation, text with no declared collation, keys
|
|
673
|
+
* of different kinds, or an operator the table lacks. For a client placing
|
|
674
|
+
* one row among others it holds.
|
|
657
675
|
*/
|
|
658
676
|
const compareRows = (terms, left, right, query, options = {}) => {
|
|
659
677
|
const scope = {
|
|
660
678
|
input: options.input ?? {},
|
|
661
|
-
operators: options.operators ??
|
|
679
|
+
operators: options.operators ?? kernelOperators,
|
|
662
680
|
context: options.context
|
|
663
681
|
};
|
|
664
682
|
const keys = [keysOf(left, terms, scope), keysOf(right, terms, scope)];
|
|
683
|
+
checkCollated(terms, query);
|
|
665
684
|
checkOrder(keys, terms, query);
|
|
666
685
|
return byTerms(terms, keys[0], keys[1]);
|
|
667
686
|
};
|
|
@@ -689,7 +708,7 @@ const supported = [
|
|
|
689
708
|
* than skipping it and answering a different question. Called by `evaluate`;
|
|
690
709
|
* call it directly to check a body once instead of on every run.
|
|
691
710
|
*/
|
|
692
|
-
const assertSupported = (body, operators =
|
|
711
|
+
const assertSupported = (body, operators = kernelOperators) => {
|
|
693
712
|
const missing = Query.unsupported(body, supported, operators);
|
|
694
713
|
if (missing.length > 0) throw new QueryEvaluateError(`this interpreter does not run ${missing.join(", ")}, which query "${body.entity.name}" needs`);
|
|
695
714
|
};
|
|
@@ -700,9 +719,13 @@ const assertSupported = (body, operators = Operators.kernel) => {
|
|
|
700
719
|
* `input` holds the values this run was given, by the keys the body's
|
|
701
720
|
* placeholders were built with — the same record the query would be called
|
|
702
721
|
* with anywhere else.
|
|
722
|
+
*
|
|
723
|
+
* The order is part of the answer, so an order over text with no declared
|
|
724
|
+
* collation is refused (`Query.placement`'s reason): declare one on the field,
|
|
725
|
+
* or on the term.
|
|
703
726
|
*/
|
|
704
727
|
const evaluate = (body, input, rows, options = {}) => {
|
|
705
|
-
const operators = options.operators ??
|
|
728
|
+
const operators = options.operators ?? kernelOperators;
|
|
706
729
|
const { context } = options;
|
|
707
730
|
assertSupported(body, operators);
|
|
708
731
|
return ordered(rows.filter((row) => matches(body, row, input, operators, context)), Query.orderFor(body, input), body.entity.name, {
|
|
@@ -720,10 +743,12 @@ const evaluate = (body, input, rows, options = {}) => {
|
|
|
720
743
|
* What no single row causes still refuses the run: an operation or operator
|
|
721
744
|
* the table lacks, an order by a locale collation, an input the body reads
|
|
722
745
|
* holding a NUL (every row would refuse, and no row is the cause), and order
|
|
723
|
-
* keys of different kinds across rows.
|
|
746
|
+
* keys of different kinds across rows. It judges membership, which no order
|
|
747
|
+
* decides, so an order over text with no declared collation does not refuse it:
|
|
748
|
+
* `matched` is then in code point order, which no other reader need follow.
|
|
724
749
|
*/
|
|
725
750
|
const evaluateEach = (body, input, rows, options = {}) => {
|
|
726
|
-
const operators = options.operators ??
|
|
751
|
+
const operators = options.operators ?? kernelOperators;
|
|
727
752
|
const { context } = options;
|
|
728
753
|
assertSupported(body, operators);
|
|
729
754
|
const terms = Query.orderFor(body, input);
|