@rocicorp/zero 0.25.0-canary.23 → 0.25.0-canary.25
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/out/analyze-query/src/run-ast.d.ts +1 -1
- package/out/analyze-query/src/run-ast.d.ts.map +1 -1
- package/out/analyze-query/src/run-ast.js +7 -1
- package/out/analyze-query/src/run-ast.js.map +1 -1
- package/out/otel/src/log-options.d.ts +1 -1
- package/out/otel/src/log-options.d.ts.map +1 -1
- package/out/otel/src/log-options.js +0 -1
- package/out/otel/src/log-options.js.map +1 -1
- package/out/shared/src/options-types.d.ts +113 -0
- package/out/shared/src/options-types.d.ts.map +1 -0
- package/out/shared/src/options.d.ts +2 -111
- package/out/shared/src/options.d.ts.map +1 -1
- package/out/shared/src/options.js.map +1 -1
- package/out/zero/package.json.js +1 -1
- package/out/zero/src/pg.js +1 -2
- package/out/zero/src/server.js +1 -2
- package/out/zero/src/zero-cache-dev.js +11 -5
- package/out/zero/src/zero-cache-dev.js.map +1 -1
- package/out/zero/src/zero.js +0 -2
- package/out/zero/src/zero.js.map +1 -1
- package/out/zero-cache/src/auth/read-authorizer.js +1 -1
- package/out/zero-cache/src/auth/read-authorizer.js.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.js +1 -1
- package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +5 -3
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/scripts/deploy-permissions.js +6 -3
- package/out/zero-cache/src/scripts/deploy-permissions.js.map +1 -1
- package/out/zero-cache/src/scripts/permissions.d.ts.map +1 -1
- package/out/zero-cache/src/scripts/permissions.js +11 -13
- package/out/zero-cache/src/scripts/permissions.js.map +1 -1
- package/out/zero-client/src/client/crud.d.ts +3 -2
- package/out/zero-client/src/client/crud.d.ts.map +1 -1
- package/out/zero-client/src/client/crud.js +7 -3
- package/out/zero-client/src/client/crud.js.map +1 -1
- package/out/zero-client/src/client/custom.d.ts +3 -2
- package/out/zero-client/src/client/custom.d.ts.map +1 -1
- package/out/zero-client/src/client/custom.js +2 -2
- package/out/zero-client/src/client/custom.js.map +1 -1
- package/out/zero-client/src/client/make-mutate-property.d.ts +1 -1
- package/out/zero-client/src/client/make-mutate-property.d.ts.map +1 -1
- package/out/zero-client/src/client/make-mutate-property.js +2 -2
- package/out/zero-client/src/client/make-mutate-property.js.map +1 -1
- package/out/zero-client/src/client/mutator-proxy.js +6 -7
- package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero.d.ts +14 -3
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +19 -6
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-client/src/mod.d.ts +3 -4
- package/out/zero-client/src/mod.d.ts.map +1 -1
- package/out/zero-schema/src/compiled-permissions.d.ts +22 -2
- package/out/zero-schema/src/compiled-permissions.d.ts.map +1 -1
- package/out/zero-schema/src/compiled-permissions.js +7 -6
- package/out/zero-schema/src/compiled-permissions.js.map +1 -1
- package/out/zero-schema/src/permissions.d.ts.map +1 -1
- package/out/zero-schema/src/permissions.js.map +1 -1
- package/out/zero-schema/src/schema-config.d.ts +0 -5
- package/out/zero-schema/src/schema-config.d.ts.map +1 -1
- package/out/zero-schema/src/schema-config.js +1 -1
- package/out/zero-schema/src/schema-config.js.map +1 -1
- package/out/zero-server/src/custom.d.ts +5 -14
- package/out/zero-server/src/custom.d.ts.map +1 -1
- package/out/zero-server/src/custom.js +8 -18
- package/out/zero-server/src/custom.js.map +1 -1
- package/out/zql/src/mutate/crud.d.ts +3 -26
- package/out/zql/src/mutate/crud.d.ts.map +1 -1
- package/out/zql/src/mutate/crud.js +14 -26
- package/out/zql/src/mutate/crud.js.map +1 -1
- package/out/zql/src/mutate/custom.d.ts +7 -8
- package/out/zql/src/mutate/custom.d.ts.map +1 -1
- package/out/zql/src/mutate/custom.js.map +1 -1
- package/out/zql/src/planner/planner-join.d.ts.map +1 -1
- package/out/zql/src/planner/planner-join.js +3 -1
- package/out/zql/src/planner/planner-join.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { recordProxy } from "../../../shared/src/record-proxy.js";
|
|
2
2
|
const CRUD_KINDS = ["insert", "upsert", "update", "delete"];
|
|
3
|
-
function
|
|
3
|
+
function makeCRUDMutate(schema, addSchemaCRUD, executor) {
|
|
4
4
|
const mutate = (request) => {
|
|
5
5
|
const { table, kind, args } = request;
|
|
6
6
|
return executor(table, kind, args);
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
{
|
|
9
9
|
for (const tableName of Object.keys(schema.tables)) {
|
|
10
10
|
mutate[tableName] = void 0;
|
|
11
11
|
}
|
|
@@ -14,40 +14,28 @@ function makeMutateCRUDFunction(schema, executor) {
|
|
|
14
14
|
(_value, tableName) => makeTableCRUD(tableName, executor)
|
|
15
15
|
);
|
|
16
16
|
}
|
|
17
|
-
return mutate;
|
|
18
17
|
}
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
function makeTransactionMutate(schema, executor) {
|
|
19
|
+
const target = {};
|
|
20
|
+
for (const tableName of Object.keys(schema.tables)) {
|
|
21
|
+
target[tableName] = void 0;
|
|
22
|
+
}
|
|
23
|
+
return recordProxy(
|
|
24
|
+
target,
|
|
25
|
+
(_value, tableName) => makeTableCRUD(tableName, executor)
|
|
25
26
|
);
|
|
26
27
|
}
|
|
27
|
-
function
|
|
28
|
+
function makeTableCRUD(tableName, executor) {
|
|
28
29
|
return Object.fromEntries(
|
|
29
30
|
CRUD_KINDS.map((kind) => [
|
|
30
31
|
kind,
|
|
31
|
-
(
|
|
32
|
+
(value) => executor(tableName, kind, value)
|
|
32
33
|
])
|
|
33
34
|
);
|
|
34
35
|
}
|
|
35
|
-
function createCRUDBuilder(schema) {
|
|
36
|
-
return recordProxy(
|
|
37
|
-
schema.tables,
|
|
38
|
-
(_tableSchema, tableName) => makeTableCRUDRequestBuilder(
|
|
39
|
-
schema,
|
|
40
|
-
tableName
|
|
41
|
-
),
|
|
42
|
-
(prop) => {
|
|
43
|
-
throw new Error(`Table ${prop} does not exist in schema`);
|
|
44
|
-
}
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
36
|
export {
|
|
48
37
|
CRUD_KINDS,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
makeTableCRUDRequestBuilder
|
|
38
|
+
makeCRUDMutate,
|
|
39
|
+
makeTransactionMutate
|
|
52
40
|
};
|
|
53
41
|
//# sourceMappingURL=crud.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crud.js","sources":["../../../../../zql/src/mutate/crud.ts"],"sourcesContent":["import type {Expand} from '../../../shared/src/expand.ts';\nimport {recordProxy} from '../../../shared/src/record-proxy.ts';\nimport type {SchemaValueToTSType} from '../../../zero-types/src/schema-value.ts';\nimport type {Schema, TableSchema} from '../../../zero-types/src/schema.ts';\nimport type {MutateCRUD} from './custom.ts';\n\nexport type SchemaCRUD<S extends Schema> = {\n [Table in keyof S['tables']]: TableCRUD<S['tables'][Table]>;\n};\n\nexport type TableCRUD<S extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\n * Non-primary-key fields that are 'optional' can be omitted or set to\n * `undefined`. Such fields will be assigned the value `null` optimistically\n * and then the default value as defined by the server.\n */\n insert: (value: InsertValue<S>) => Promise<void>;\n\n /**\n * Writes a row unconditionally, overwriting any existing row with the same\n * primary key. Non-primary-key fields that are 'optional' can be omitted or\n * set to `undefined`. Such fields will be assigned the value `null`\n * optimistically and then the default value as defined by the server.\n */\n upsert: (value: UpsertValue<S>) => Promise<void>;\n\n /**\n * Updates a row with the same primary key. If no such row exists, this\n * function does nothing. All non-primary-key fields can be omitted or set to\n * `undefined`. Such fields will be left unchanged from previous value.\n */\n update: (value: UpdateValue<S>) => Promise<void>;\n\n /**\n * Deletes the row with the specified primary key. If no such row exists, this\n * function does nothing.\n */\n delete: (id: DeleteID<S>) => Promise<void>;\n};\n\nexport type CRUDKind = keyof TableCRUD<TableSchema>;\n\nexport const CRUD_KINDS = ['insert', 'upsert', 'update', 'delete'] as const;\n\nexport type DeleteID<S extends TableSchema> = Expand<PrimaryKeyFields<S>>;\n\ntype PrimaryKeyFields<S extends TableSchema> = {\n [K in Extract<\n S['primaryKey'][number],\n keyof S['columns']\n >]: SchemaValueToTSType<S['columns'][K]>;\n};\n\nexport type InsertValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? K\n : never]?: SchemaValueToTSType<S['columns'][K]> | undefined;\n } & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? never\n : K]: SchemaValueToTSType<S['columns'][K]>;\n }\n>;\n\nexport type UpsertValue<S extends TableSchema> = InsertValue<S>;\n\nexport type UpdateValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns']]?:\n | SchemaValueToTSType<S['columns'][K]>\n | undefined;\n }\n>;\n\n/**\n * This is the type of the generated mutate.<name>.<verb> function.\n */\nexport type TableMutator<TS extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\n * Non-primary-key fields that are 'optional' can be omitted or set to\n * `undefined`. Such fields will be assigned the value `null` optimistically\n * and then the default value as defined by the server.\n */\n insert: (value: InsertValue<TS>) => Promise<void>;\n\n /**\n * Writes a row unconditionally, overwriting any existing row with the same\n * primary key. Non-primary-key fields that are 'optional' can be omitted or\n * set to `undefined`. Such fields will be assigned the value `null`\n * optimistically and then the default value as defined by the server.\n */\n upsert: (value: UpsertValue<TS>) => Promise<void>;\n\n /**\n * Updates a row with the same primary key. If no such row exists, this\n * function does nothing. All non-primary-key fields can be omitted or set to\n * `undefined`. Such fields will be left unchanged from previous value.\n */\n update: (value: UpdateValue<TS>) => Promise<void>;\n\n /**\n * Deletes the row with the specified primary key. If no such row exists, this\n * function does nothing.\n */\n delete: (id: DeleteID<TS>) => Promise<void>;\n};\n\n/**\n * A function that executes a CRUD operation.\n * Client and server provide different implementations.\n */\nexport type CRUDExecutor = (\n table: string,\n kind: CRUDKind,\n args: unknown,\n) => Promise<void>;\n\n/**\n * Creates a MutateCRUD function from a schema and executor.\n * This is the shared implementation used by both client and server.\n *\n * @param schema - The Zero schema\n * @param executor - A function that executes CRUD operations\n * @returns A MutateCRUD function that can be called with CRUDMutateRequest objects\n */\nexport function
|
|
1
|
+
{"version":3,"file":"crud.js","sources":["../../../../../zql/src/mutate/crud.ts"],"sourcesContent":["import type {Expand} from '../../../shared/src/expand.ts';\nimport {recordProxy} from '../../../shared/src/record-proxy.ts';\nimport type {SchemaValueToTSType} from '../../../zero-types/src/schema-value.ts';\nimport type {Schema, TableSchema} from '../../../zero-types/src/schema.ts';\nimport type {MutateCRUD} from './custom.ts';\n\nexport type SchemaCRUD<S extends Schema> = {\n [Table in keyof S['tables']]: TableCRUD<S['tables'][Table]>;\n};\n\nexport type TransactionMutate<S extends Schema> = SchemaCRUD<S>;\n\nexport type TableCRUD<S extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\n * Non-primary-key fields that are 'optional' can be omitted or set to\n * `undefined`. Such fields will be assigned the value `null` optimistically\n * and then the default value as defined by the server.\n */\n insert: (value: InsertValue<S>) => Promise<void>;\n\n /**\n * Writes a row unconditionally, overwriting any existing row with the same\n * primary key. Non-primary-key fields that are 'optional' can be omitted or\n * set to `undefined`. Such fields will be assigned the value `null`\n * optimistically and then the default value as defined by the server.\n */\n upsert: (value: UpsertValue<S>) => Promise<void>;\n\n /**\n * Updates a row with the same primary key. If no such row exists, this\n * function does nothing. All non-primary-key fields can be omitted or set to\n * `undefined`. Such fields will be left unchanged from previous value.\n */\n update: (value: UpdateValue<S>) => Promise<void>;\n\n /**\n * Deletes the row with the specified primary key. If no such row exists, this\n * function does nothing.\n */\n delete: (id: DeleteID<S>) => Promise<void>;\n};\n\nexport type CRUDKind = keyof TableCRUD<TableSchema>;\n\nexport const CRUD_KINDS = ['insert', 'upsert', 'update', 'delete'] as const;\n\nexport type DeleteID<S extends TableSchema> = Expand<PrimaryKeyFields<S>>;\n\ntype PrimaryKeyFields<S extends TableSchema> = {\n [K in Extract<\n S['primaryKey'][number],\n keyof S['columns']\n >]: SchemaValueToTSType<S['columns'][K]>;\n};\n\nexport type InsertValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? K\n : never]?: SchemaValueToTSType<S['columns'][K]> | undefined;\n } & {\n [K in keyof S['columns'] as S['columns'][K] extends {optional: true}\n ? never\n : K]: SchemaValueToTSType<S['columns'][K]>;\n }\n>;\n\nexport type UpsertValue<S extends TableSchema> = InsertValue<S>;\n\nexport type UpdateValue<S extends TableSchema> = Expand<\n PrimaryKeyFields<S> & {\n [K in keyof S['columns']]?:\n | SchemaValueToTSType<S['columns'][K]>\n | undefined;\n }\n>;\n\n/**\n * This is the type of the generated mutate.<name>.<verb> function.\n */\nexport type TableMutator<TS extends TableSchema> = {\n /**\n * Writes a row if a row with the same primary key doesn't already exist.\n * Non-primary-key fields that are 'optional' can be omitted or set to\n * `undefined`. Such fields will be assigned the value `null` optimistically\n * and then the default value as defined by the server.\n */\n insert: (value: InsertValue<TS>) => Promise<void>;\n\n /**\n * Writes a row unconditionally, overwriting any existing row with the same\n * primary key. Non-primary-key fields that are 'optional' can be omitted or\n * set to `undefined`. Such fields will be assigned the value `null`\n * optimistically and then the default value as defined by the server.\n */\n upsert: (value: UpsertValue<TS>) => Promise<void>;\n\n /**\n * Updates a row with the same primary key. If no such row exists, this\n * function does nothing. All non-primary-key fields can be omitted or set to\n * `undefined`. Such fields will be left unchanged from previous value.\n */\n update: (value: UpdateValue<TS>) => Promise<void>;\n\n /**\n * Deletes the row with the specified primary key. If no such row exists, this\n * function does nothing.\n */\n delete: (id: DeleteID<TS>) => Promise<void>;\n};\n\n/**\n * A function that executes a CRUD operation.\n * Client and server provide different implementations.\n */\nexport type CRUDExecutor = (\n table: string,\n kind: CRUDKind,\n args: unknown,\n) => Promise<void>;\n\n/**\n * Creates a MutateCRUD function from a schema and executor.\n * This is the shared implementation used by both client and server.\n *\n * @param schema - The Zero schema\n * @param executor - A function that executes CRUD operations\n * @returns A MutateCRUD function that can be called with CRUDMutateRequest objects\n */\nexport function makeCRUDMutate<\n TSchema extends Schema,\n TAddSchemaCRUD extends boolean,\n>(\n schema: TSchema,\n addSchemaCRUD: TAddSchemaCRUD,\n executor: CRUDExecutor,\n): MutateCRUD<TSchema, TAddSchemaCRUD> {\n // Create a callable function that accepts CRUDMutateRequest\n const mutate = (request: AnyCRUDMutateRequest) => {\n const {table, kind, args} = request;\n return executor(table, kind, args);\n };\n\n // Only add table properties when enableLegacyMutators is true\n if (addSchemaCRUD) {\n // Add table names as keys so the proxy can discover them\n for (const tableName of Object.keys(schema.tables)) {\n (mutate as unknown as Record<string, undefined>)[tableName] = undefined;\n }\n\n // Wrap in proxy that lazily creates and caches table CRUD objects\n return recordProxy(\n mutate as unknown as Record<string, undefined>,\n (_value, tableName) => makeTableCRUD(tableName, executor),\n ) as unknown as MutateCRUD<TSchema, TAddSchemaCRUD>;\n }\n\n return mutate as MutateCRUD<TSchema, TAddSchemaCRUD>;\n}\n\nexport function makeTransactionMutate<TSchema extends Schema>(\n schema: TSchema,\n executor: CRUDExecutor,\n): TransactionMutate<TSchema> {\n const target: Record<string, undefined> = {};\n for (const tableName of Object.keys(schema.tables)) {\n target[tableName] = undefined;\n }\n\n return recordProxy(target, (_value, tableName) =>\n makeTableCRUD(tableName, executor),\n ) as SchemaCRUD<TSchema>;\n}\n\n/**\n * Creates a TableCRUD object that delegates to the executor.\n */\nfunction makeTableCRUD(\n tableName: string,\n executor: CRUDExecutor,\n): TableCRUD<TableSchema> {\n return Object.fromEntries(\n CRUD_KINDS.map(kind => [\n kind,\n (value: unknown) => executor(tableName, kind, value),\n ]),\n ) as TableCRUD<TableSchema>;\n}\n\nexport type CRUDMutator<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'] & string,\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = {\n (args: TArgs): CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;\n\n /**\n * Type-only phantom property to surface mutator types in a covariant position.\n */\n ['~']: Expand<CRUDMutatorTypes<TSchema, TTable, TKind, TArgs>>;\n};\n\nexport type CRUDMutatorTypes<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'] & string,\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = 'CRUDMutator' & CRUDMutateRequest<TSchema, TTable, TKind, TArgs>;\n\nexport type CRUDMutateRequest<\n TSchema extends Schema,\n TTable extends keyof TSchema['tables'],\n TKind extends keyof TableMutator<TSchema['tables'][TTable]>,\n TArgs extends Parameters<TableMutator<TSchema['tables'][TTable]>[TKind]>[0],\n> = {\n readonly schema: TSchema;\n readonly table: TTable;\n readonly kind: TKind;\n readonly args: TArgs;\n};\n\n// oxlint-disable-next-line no-explicit-any\nexport type AnyCRUDMutateRequest = CRUDMutateRequest<any, any, CRUDKind, any>;\n"],"names":[],"mappings":";AA6CO,MAAM,aAAa,CAAC,UAAU,UAAU,UAAU,QAAQ;AAqF1D,SAAS,eAId,QACA,eACA,UACqC;AAErC,QAAM,SAAS,CAAC,YAAkC;AAChD,UAAM,EAAC,OAAO,MAAM,KAAA,IAAQ;AAC5B,WAAO,SAAS,OAAO,MAAM,IAAI;AAAA,EACnC;AAGmB;AAEjB,eAAW,aAAa,OAAO,KAAK,OAAO,MAAM,GAAG;AACjD,aAAgD,SAAS,IAAI;AAAA,IAChE;AAGA,WAAO;AAAA,MACL;AAAA,MACA,CAAC,QAAQ,cAAc,cAAc,WAAW,QAAQ;AAAA,IAAA;AAAA,EAE5D;AAGF;AAEO,SAAS,sBACd,QACA,UAC4B;AAC5B,QAAM,SAAoC,CAAA;AAC1C,aAAW,aAAa,OAAO,KAAK,OAAO,MAAM,GAAG;AAClD,WAAO,SAAS,IAAI;AAAA,EACtB;AAEA,SAAO;AAAA,IAAY;AAAA,IAAQ,CAAC,QAAQ,cAClC,cAAc,WAAW,QAAQ;AAAA,EAAA;AAErC;AAKA,SAAS,cACP,WACA,UACwB;AACxB,SAAO,OAAO;AAAA,IACZ,WAAW,IAAI,CAAA,SAAQ;AAAA,MACrB;AAAA,MACA,CAAC,UAAmB,SAAS,WAAW,MAAM,KAAK;AAAA,IAAA,CACpD;AAAA,EAAA;AAEL;"}
|
|
@@ -5,8 +5,7 @@ import type { ServerSchema } from '../../../zero-types/src/server-schema.ts';
|
|
|
5
5
|
import type { Format } from '../ivm/view.ts';
|
|
6
6
|
import type { HumanReadable, Query, RunOptions } from '../query/query.ts';
|
|
7
7
|
import type { SchemaQuery } from '../query/schema-query.ts';
|
|
8
|
-
import type { CRUDMutateRequest, SchemaCRUD } from './crud.ts';
|
|
9
|
-
export type { DeleteID, InsertValue, SchemaCRUD, TableCRUD, UpdateValue, UpsertValue, } from './crud.ts';
|
|
8
|
+
import type { CRUDMutateRequest, SchemaCRUD, TransactionMutate } from './crud.ts';
|
|
10
9
|
type ClientID = string;
|
|
11
10
|
/**
|
|
12
11
|
* A base transaction interface that any Transaction<S, T> is assignable to.
|
|
@@ -32,7 +31,7 @@ export interface TransactionBase<S extends Schema> {
|
|
|
32
31
|
* The reason for the transaction.
|
|
33
32
|
*/
|
|
34
33
|
readonly reason: TransactionReason;
|
|
35
|
-
readonly mutate:
|
|
34
|
+
readonly mutate: TransactionMutate<S>;
|
|
36
35
|
/**
|
|
37
36
|
* @deprecated Use {@linkcode createBuilder} with `tx.run(zql.table.where(...))` instead.
|
|
38
37
|
*/
|
|
@@ -68,12 +67,12 @@ interface Queryable {
|
|
|
68
67
|
query: (query: string, args: unknown[]) => Promise<Iterable<Row>>;
|
|
69
68
|
}
|
|
70
69
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* 2. When `enableLegacyMutators` is true, also an object with CRUD methods per table
|
|
70
|
+
* A callable mutate shape with optional table helpers used by helper factories
|
|
71
|
+
* like `makeMutateCRUD`. Transactions expose `SchemaCRUD` instead of this type.
|
|
74
72
|
*/
|
|
75
|
-
export type MutateCRUD<S extends Schema> = {
|
|
73
|
+
export type MutateCRUD<S extends Schema, AddSchemaCRUD extends boolean> = {
|
|
76
74
|
(request: CRUDMutateRequest<S, any, any, any>): Promise<void>;
|
|
77
|
-
} & (
|
|
75
|
+
} & (AddSchemaCRUD extends true ? SchemaCRUD<S> : {});
|
|
78
76
|
export declare function customMutatorKey(sep: string, parts: string[]): string;
|
|
77
|
+
export {};
|
|
79
78
|
//# sourceMappingURL=custom.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../../../../zql/src/mutate/custom.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,mCAAmC,CAAC;AAC3D,OAAO,KAAK,EACV,aAAa,EACb,yBAAyB,EAC1B,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,mCAAmC,CAAC;AAC9D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,0CAA0C,CAAC;AAC3E,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,gBAAgB,CAAC;AAC3C,OAAO,KAAK,EAAC,aAAa,EAAE,KAAK,EAAE,UAAU,EAAC,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAC,iBAAiB,EAAE,UAAU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AW,EAAE,CAAC,CAAC,EACb,EAAE,EAAE,CAAC,EAAE,EAAE,aAAa,CAAC,mBAAmB,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,KACvD,OAAO,CAAC,CAAC,CAAC,CAAC;CACjB;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,CAAE,SAAQ,SAAS;IACjD,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAC;IAC/B,QAAQ,CAAC,OAAO,EACd,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACzB,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;CACpC;AAED,UAAU,SAAS;IACjB,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;CACnE;AAED;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,aAAa,SAAS,OAAO,IAAI;IAExE,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/D,GAAG,CAAC,aAAa,SAAS,IAAI,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;AAEtD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAQ5D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom.js","sources":["../../../../../zql/src/mutate/custom.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {\n DefaultSchema,\n DefaultWrappedTransaction,\n} from '../../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {Format} from '../ivm/view.ts';\nimport type {HumanReadable, Query, RunOptions} from '../query/query.ts';\nimport type {SchemaQuery} from '../query/schema-query.ts';\nimport type {CRUDMutateRequest, SchemaCRUD
|
|
1
|
+
{"version":3,"file":"custom.js","sources":["../../../../../zql/src/mutate/custom.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {AST} from '../../../zero-protocol/src/ast.ts';\nimport type {\n DefaultSchema,\n DefaultWrappedTransaction,\n} from '../../../zero-types/src/default-types.ts';\nimport type {Schema} from '../../../zero-types/src/schema.ts';\nimport type {ServerSchema} from '../../../zero-types/src/server-schema.ts';\nimport type {Format} from '../ivm/view.ts';\nimport type {HumanReadable, Query, RunOptions} from '../query/query.ts';\nimport type {SchemaQuery} from '../query/schema-query.ts';\nimport type {CRUDMutateRequest, SchemaCRUD, TransactionMutate} from './crud.ts';\n\ntype ClientID = string;\n\n/**\n * A base transaction interface that any Transaction<S, T> is assignable to.\n * Used in places where the schema type doesn't need to be preserved,\n * like the public signature of Mutator.fn.\n */\nexport interface AnyTransaction {\n readonly location: Location;\n readonly clientID: string;\n readonly mutationID: number;\n readonly reason: TransactionReason;\n}\n\nexport type Location = 'client' | 'server';\nexport type TransactionReason = 'optimistic' | 'rebase' | 'authoritative';\n\nexport interface TransactionBase<S extends Schema> {\n readonly location: Location;\n readonly clientID: ClientID;\n /**\n * The ID of the mutation that is being applied.\n */\n readonly mutationID: number;\n\n /**\n * The reason for the transaction.\n */\n readonly reason: TransactionReason;\n\n readonly mutate: TransactionMutate<S>;\n /**\n * @deprecated Use {@linkcode createBuilder} with `tx.run(zql.table.where(...))` instead.\n */\n readonly query: SchemaQuery<S>;\n\n run<TTable extends keyof S['tables'] & string, TReturn>(\n query: Query<TTable, S, TReturn>,\n options?: RunOptions,\n ): Promise<HumanReadable<TReturn>>;\n}\n\nexport type Transaction<\n S extends Schema = DefaultSchema,\n TWrappedTransaction = DefaultWrappedTransaction,\n> = ServerTransaction<S, TWrappedTransaction> | ClientTransaction<S>;\n\nexport interface ServerTransaction<\n S extends Schema = DefaultSchema,\n TWrappedTransaction = DefaultWrappedTransaction,\n> extends TransactionBase<S> {\n readonly location: 'server';\n readonly reason: 'authoritative';\n readonly dbTransaction: DBTransaction<TWrappedTransaction>;\n}\n\n/**\n * An instance of this is passed to custom mutator implementations and\n * allows reading and writing to the database and IVM at the head at which the\n * mutator is being applied.\n */\nexport interface ClientTransaction<S extends Schema = DefaultSchema>\n extends TransactionBase<S> {\n readonly location: 'client';\n readonly reason: 'optimistic' | 'rebase';\n}\n\nexport interface Row {\n [column: string]: unknown;\n}\n\nexport interface DBConnection<TWrappedTransaction> {\n transaction: <T>(\n cb: (tx: DBTransaction<TWrappedTransaction>) => Promise<T>,\n ) => Promise<T>;\n}\n\nexport interface DBTransaction<T> extends Queryable {\n readonly wrappedTransaction: T;\n runQuery<TReturn>(\n ast: AST,\n format: Format,\n schema: Schema,\n serverSchema: ServerSchema,\n ): Promise<HumanReadable<TReturn>>;\n}\n\ninterface Queryable {\n query: (query: string, args: unknown[]) => Promise<Iterable<Row>>;\n}\n\n/**\n * A callable mutate shape with optional table helpers used by helper factories\n * like `makeMutateCRUD`. Transactions expose `SchemaCRUD` instead of this type.\n */\nexport type MutateCRUD<S extends Schema, AddSchemaCRUD extends boolean> = {\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n (request: CRUDMutateRequest<S, any, any, any>): Promise<void>;\n} & (AddSchemaCRUD extends true ? SchemaCRUD<S> : {});\n\nexport function customMutatorKey(sep: string, parts: string[]) {\n for (const part of parts) {\n assert(\n !part.includes(sep),\n `mutator names/namespaces must not include a ${sep}`,\n );\n }\n return parts.join(sep);\n}\n"],"names":[],"mappings":";AAiHO,SAAS,iBAAiB,KAAa,OAAiB;AAC7D,aAAW,QAAQ,OAAO;AACxB;AAAA,MACE,CAAC,KAAK,SAAS,GAAG;AAAA,MAClB,+CAA+C,GAAG;AA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,gBAAgB;IAIvC,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAWtC,IAAI,IAAI,IAAI;IAUZ,IAAI,IAAI,IAAI,MAAM,GAAG,SAAS,CAE7B;IACD,WAAW,IAAI,OAAO;IAItB;;;;;;;;;;;;;;OAcG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;OAKG;IACH,+BAA+B,IAAI,IAAI;IAIvC,oBAAoB,CAClB,aAAa,EAAE,MAAM,EAAE,EACvB,UAAU,EAAE,iBAAiB,GAAG,SAAS,EACzC,IAAI,CAAC,EAAE,WAAW,EAClB,YAAY,CAAC,EAAE,YAAY,GAC1B,IAAI;IAuDP,KAAK,IAAI,IAAI;IAIb,YAAY;IACV;;;;;;;;OAQG;IACH,0BAA0B,EAAE,MAAM;IAClC;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,aAAa,EAAE,MAAM,EAAE,EACvB,YAAY,CAAC,EAAE,YAAY,GAC1B,YAAY;IA8Gf;;;OAGG;IACH,OAAO,IAAI,MAAM;IAMjB;;OAEG;IACH,YAAY,IAAI;QACd,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC;KAChB;CAOF;AAED,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAI5B"}
|
|
@@ -178,7 +178,9 @@ class PlannerJoin {
|
|
|
178
178
|
downstreamChildSelectivity === 0 ? 0 : parent.limit / downstreamChildSelectivity
|
|
179
179
|
),
|
|
180
180
|
cost: child.cost + child.scanEst * (parent.startupCost + parent.cost + parent.scanEst),
|
|
181
|
-
|
|
181
|
+
// the child selectivity is not relevant here because it has already been taken into account via the flipping.
|
|
182
|
+
// I.e., `child.returnedRows` is the estimated number of rows produced by the child _after_ taking filtering into account.
|
|
183
|
+
returnedRows: parent.returnedRows * child.returnedRows,
|
|
182
184
|
selectivity: parent.selectivity * child.selectivity,
|
|
183
185
|
limit: parent.limit,
|
|
184
186
|
fanout: parent.fanout
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner-join.js","sources":["../../../../../zql/src/planner/planner-join.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {\n mergeConstraints,\n type PlannerConstraint,\n} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport {omitFanout} from './planner-node.ts';\nimport type {\n CostEstimate,\n JoinOrConnection,\n PlannerNode,\n} from './planner-node.ts';\nimport type {PlannerTerminus} from './planner-terminus.ts';\n\n/**\n * Translate constraints for a flipped join from parent space to child space.\n * Matches the runtime behavior of FlippedJoin.fetch() which translates\n * parent constraints to child constraints using index-based key mapping.\n *\n * Example:\n * parentConstraint = {issueID: undefined, projectID: undefined}\n * childConstraint = {id: undefined, projectID: undefined}\n * incomingConstraint = {issueID: 5}\n * result = {id: 5} // issueID at index 0 maps to id at index 0\n */\nfunction translateConstraintsForFlippedJoin(\n incomingConstraint: PlannerConstraint | undefined,\n parentConstraint: PlannerConstraint,\n childConstraint: PlannerConstraint,\n): PlannerConstraint | undefined {\n if (!incomingConstraint) return undefined;\n\n const parentKeys = Object.keys(parentConstraint);\n const childKeys = Object.keys(childConstraint);\n const translated: PlannerConstraint = {};\n\n for (const [key, value] of Object.entries(incomingConstraint)) {\n const index = parentKeys.indexOf(key);\n if (index !== -1) {\n // Found this key in parent at position `index`\n // Map to child key at same position\n translated[childKeys[index]] = value;\n }\n }\n\n return Object.keys(translated).length > 0 ? translated : undefined;\n}\n\n/**\n * Semi-join overhead multiplier.\n *\n * Semi-joins represent correlated subqueries (EXISTS checks) which have\n * execution overhead compared to flipped joins, even when logical row counts\n * are identical. This overhead comes from:\n * - Need to execute a separate correlation check for each parent row\n * - Cannot leverage combined constraint checking as effectively as flipped joins\n *\n * A multiplier of 1.5 means semi-joins are estimated to be ~50% more expensive\n * than equivalent flipped joins, which empirically matches observed performance\n * differences in production workloads (e.g., 1.7x in zbugs benchmarks).\n *\n * Flipped joins have a different overhead in that they become unlimited. This\n * is accounted for when propagating unlimits rather than here.\n */\n// const SEMI_JOIN_OVERHEAD_MULTIPLIER = 1.5;\n\n/**\n * Represents a join between two data streams (parent and child).\n *\n * # Dual-State Pattern\n * Like all planner nodes, PlannerJoin separates:\n * 1. IMMUTABLE STRUCTURE: Parent/child nodes, constraints, flippability\n * 2. MUTABLE STATE: Join type (semi/flipped), pinned status\n *\n * # Join Flipping\n * A join can be in two states:\n * - 'semi': Parent is outer loop, child is inner (semi-join for EXISTS)\n * - 'flipped': Child is outer loop, parent is inner\n *\n * Flipping is the key optimization: choosing which table scans first.\n * NOT EXISTS joins cannot be flipped (#flippable = false).\n *\n * # Constraint Propagation\n * - Semi-join: Sends childConstraint to child, forwards received constraints to parent\n * - Flipped join: Sends undefined to child, merges parentConstraint with received to parent\n * - Unpinned join: Only forwards constraints to parent (doesn't constrain child yet)\n *\n * # Lifecycle\n * 1. Construct with immutable structure (parent, child, constraints, flippability)\n * 2. Wire to output node during graph construction\n * 3. Planning calls flipIfNeeded() based on connection selection order\n * 4. pin() locks the join type once chosen\n * 5. reset() clears mutable state (type → 'semi', pinned → false)\n */\nexport class PlannerJoin {\n readonly kind = 'join' as const;\n\n readonly #parent: Exclude<PlannerNode, PlannerTerminus>;\n readonly #child: Exclude<PlannerNode, PlannerTerminus>;\n readonly #parentConstraint: PlannerConstraint;\n readonly #childConstraint: PlannerConstraint;\n readonly #flippable: boolean;\n readonly planId: number;\n #output?: PlannerNode | undefined; // Set once during graph construction\n\n // Reset between planning attempts\n #type: 'semi' | 'flipped';\n readonly #initialType: 'semi' | 'flipped';\n\n constructor(\n parent: Exclude<PlannerNode, PlannerTerminus>,\n child: Exclude<PlannerNode, PlannerTerminus>,\n parentConstraint: PlannerConstraint,\n childConstraint: PlannerConstraint,\n flippable: boolean,\n planId: number,\n initialType: 'semi' | 'flipped' = 'semi',\n ) {\n this.#type = initialType;\n this.#initialType = initialType;\n this.#parent = parent;\n this.#child = child;\n this.#childConstraint = childConstraint;\n this.#parentConstraint = parentConstraint;\n this.#flippable = flippable;\n this.planId = planId;\n }\n\n setOutput(node: PlannerNode): void {\n this.#output = node;\n }\n\n get output(): PlannerNode {\n assert(this.#output !== undefined, 'Output not set');\n return this.#output;\n }\n\n closestJoinOrSource(): JoinOrConnection {\n return 'join';\n }\n\n flipIfNeeded(input: PlannerNode): void {\n if (input === this.#child) {\n this.flip();\n } else {\n assert(\n input === this.#parent,\n 'Can only flip a join from one of its inputs',\n );\n }\n }\n\n flip(): void {\n assert(this.#type === 'semi', 'Can only flip a semi-join');\n if (!this.#flippable) {\n throw new UnflippableJoinError(\n 'Cannot flip a non-flippable join (e.g., NOT EXISTS)',\n );\n }\n this.#type = 'flipped';\n }\n\n get type(): 'semi' | 'flipped' {\n return this.#type;\n }\n isFlippable(): boolean {\n return this.#flippable;\n }\n\n /**\n * Propagate unlimiting when this join is flipped.\n * When a join is flipped:\n * 1. Child becomes outer loop → produces all rows (unlimited)\n * 2. Parent is fetched once per child row → effectively unlimited\n *\n * Example: If child produces 896 rows, parent is fetched 896 times.\n * Even if each fetch returns 1 row, parent produces 896 total rows.\n *\n * Propagation rules:\n * - Connection: call unlimit()\n * - Semi-join: continue to parent (outer loop)\n * - Flipped join: stop (already unlimited when it was flipped)\n * - Fan-out/Fan-in: propagate to all inputs\n */\n propagateUnlimit(): void {\n assert(this.#type === 'flipped', 'Can only unlimit a flipped join');\n // Parent stays limited; child becomes unlimited\n this.#child.propagateUnlimitFromFlippedJoin(); // Up the child chain\n }\n\n /**\n * Called when a parent join is flipped and this join is part of its child subgraph.\n * Continue propagation to parent (the outer loop).\n * If we are hitting a semi-join, the parent drives.\n * If we are hitting a flip-join, well now we have to unlimit its parent too!\n */\n propagateUnlimitFromFlippedJoin(): void {\n this.#parent.propagateUnlimitFromFlippedJoin();\n }\n\n propagateConstraints(\n branchPattern: number[],\n constraint: PlannerConstraint | undefined,\n from?: PlannerNode,\n planDebugger?: PlanDebugger,\n ): void {\n planDebugger?.log({\n type: 'node-constraint',\n nodeType: 'join',\n node: this.getName(),\n branchPattern,\n constraint,\n from: from ? getNodeName(from) : 'unknown',\n });\n\n if (this.#type === 'semi') {\n // A semi-join always has constraints for its child.\n // They are defined by the correlation between parent and child.\n this.#child.propagateConstraints(\n branchPattern,\n this.#childConstraint,\n this,\n planDebugger,\n );\n // A semi-join forwards constraints to its parent.\n this.#parent.propagateConstraints(\n branchPattern,\n constraint,\n this,\n planDebugger,\n );\n } else if (this.#type === 'flipped') {\n // A flipped join translates constraints from parent space to child space.\n // This matches FlippedJoin.fetch() runtime behavior where parent constraints\n // on join keys are translated to child constraints.\n // Example: If parent has {issueID: 5} and join maps issueID→id,\n // child gets {id: 5} allowing index usage.\n const translatedConstraint = translateConstraintsForFlippedJoin(\n constraint,\n this.#parentConstraint,\n this.#childConstraint,\n );\n this.#child.propagateConstraints(\n branchPattern,\n translatedConstraint,\n this,\n planDebugger,\n );\n // A flipped join will have constraints to send to its parent.\n // - The constraints its output sent\n // - The constraints its child creates\n this.#parent.propagateConstraints(\n branchPattern,\n mergeConstraints(constraint, this.#parentConstraint),\n this,\n planDebugger,\n );\n }\n }\n\n reset(): void {\n this.#type = this.#initialType;\n }\n\n estimateCost(\n /**\n * This argument is to deal with consecutive `andExists` statements.\n * Each one will constrain how often a parent row passes all constraints.\n * This means that we have to scan more and more parent rows the more\n * constraints we add.\n *\n * DownstreamChildSelectivity factors in fanout factor\n * from parent -> child\n */\n downstreamChildSelectivity: number,\n /**\n * branchPattern uniquely identifies OR branches in the graph.\n * Each path through an OR will have unique constraints to apply to the source\n * connection.\n * branchPattern allows us to correlate a path through the graph\n * to the constraints that should be applied for that path.\n *\n * Example graph:\n * UFO\n * / \\\n * J1 J2\n * \\ /\n * UFI\n *\n * J1 and J2 are joins inside an OR (FO).\n * branchPattern [0] = path through J1\n * branchPattern [1] = path through J2\n *\n * If many ORs are nested, branchPattern will have multiple elements\n * representing each level of OR.\n *\n * If no joins are flipped within the `OR`, then only a single\n * branchPattern element will be needed, as FO represents all sub-joins\n * as a single path.\n */\n branchPattern: number[],\n planDebugger?: PlanDebugger,\n ): CostEstimate {\n /**\n * downstreamChildSelectivity accumulates up a parent chain, not\n * up child chains. Child chains represent independent sub-graphs.\n * So we pass 1 for `downstreamChildSelectivity` when estimating child cost.\n * Put another way, downstreamChildSelectivity impacts how many parent\n * rows are returned.\n */\n const child = this.#child.estimateCost(1, branchPattern, planDebugger);\n\n const fanoutFactor = child.fanout(Object.keys(this.#childConstraint));\n // Factor in how many child rows match a parent row.\n // E.g., if an issue has 10 comments on average then we're more\n // likely to hit a comment compared to if an issue has 1 comment on average.\n // If an index is all nulls (no parents match any children)\n // this will collapse to 0.\n const scaledChildSelectivity =\n 1 - Math.pow(1 - child.selectivity, fanoutFactor.fanout);\n\n /**\n * How selective is the graph from this point forward?\n * If we are _very_ selective then we must scan more parent rows\n * before finding a match.\n * E.g., if childSelectivity = 0.1 and downstreamChildSelectivity = 0.5\n * then we only pass 5% of parent rows (0.1 * 0.5 = 0.05).\n *\n * This is used to estimate how many rows will be pulled from the parent\n * when trying to satisfy downstream constraints and a limit.\n *\n * NOTE: We do not know if the probabilities are correlated so we assume independence.\n * This is a fundamental limitation of the planner.\n */\n const parent = this.#parent.estimateCost(\n // Selectivity flows up the graph from child to parent\n // so we can determine the total selectivity of all ANDed exists checks.\n this.#type === 'flipped'\n ? 1 * downstreamChildSelectivity\n : scaledChildSelectivity * downstreamChildSelectivity,\n branchPattern,\n planDebugger,\n );\n\n let costEstimate: CostEstimate;\n\n if (this.type === 'semi') {\n costEstimate = {\n startupCost: parent.startupCost,\n scanEst:\n parent.limit === undefined\n ? parent.returnedRows\n : Math.min(\n parent.returnedRows,\n downstreamChildSelectivity === 0\n ? 0\n : parent.limit / downstreamChildSelectivity,\n ),\n cost:\n parent.cost +\n parent.scanEst * (child.startupCost + child.cost + child.scanEst),\n returnedRows: parent.returnedRows * child.selectivity,\n selectivity: child.selectivity * parent.selectivity,\n limit: parent.limit,\n fanout: parent.fanout,\n };\n } else {\n costEstimate = {\n startupCost: child.startupCost,\n scanEst:\n parent.limit === undefined\n ? parent.returnedRows * child.returnedRows\n : Math.min(\n parent.returnedRows * child.returnedRows,\n downstreamChildSelectivity === 0\n ? 0\n : parent.limit / downstreamChildSelectivity,\n ),\n cost:\n child.cost +\n child.scanEst * (parent.startupCost + parent.cost + parent.scanEst),\n returnedRows:\n parent.returnedRows * child.returnedRows * child.selectivity,\n selectivity: parent.selectivity * child.selectivity,\n limit: parent.limit,\n fanout: parent.fanout,\n };\n }\n\n if (planDebugger) {\n planDebugger.log({\n type: 'node-cost',\n nodeType: 'join',\n node: this.getName(),\n branchPattern,\n downstreamChildSelectivity,\n costEstimate: omitFanout(costEstimate),\n joinType: this.#type,\n });\n }\n\n return costEstimate;\n }\n\n /**\n * Get a human-readable name for this join for debugging.\n * Format: \"parentName ⋈ childName\"\n */\n getName(): string {\n const parentName = getNodeName(this.#parent);\n const childName = getNodeName(this.#child);\n return `${parentName} ⋈ ${childName}`;\n }\n\n /**\n * Get debug information about this join's state.\n */\n getDebugInfo(): {\n name: string;\n type: 'semi' | 'flipped';\n planId: number;\n } {\n return {\n name: this.getName(),\n type: this.#type,\n planId: this.planId,\n };\n }\n}\n\nexport class UnflippableJoinError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'UnflippableJoinError';\n }\n}\n\n/**\n * Get a human-readable name for any planner node.\n * Used for debugging and tracing.\n */\nfunction getNodeName(node: PlannerNode): string {\n switch (node.kind) {\n case 'connection':\n return node.name;\n case 'join':\n return node.getName();\n case 'fan-out':\n return 'FO';\n case 'fan-in':\n return 'FI';\n case 'terminus':\n return 'terminus';\n }\n}\n"],"names":[],"mappings":";;;AAyBA,SAAS,mCACP,oBACA,kBACA,iBAC+B;AAC/B,MAAI,CAAC,mBAAoB,QAAO;AAEhC,QAAM,aAAa,OAAO,KAAK,gBAAgB;AAC/C,QAAM,YAAY,OAAO,KAA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AAAA,MAAA,CAChB;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAkB;AAChB,UAAM,aAAa,YAAY,KAAK,OAAO;AAC3C,UAAM,YAAY,YAAY,KAAK,MAAM;AACzC,WAAO,GAAG,UAAU,MAAM,SAAS;AAAA,EACrC;AAAA;AAAA;AAAA;AAAA,EAKA,eAIE;AACA,WAAO;AAAA,MACL,MAAM,KAAK,QAAA;AAAA,MACX,MAAM,KAAK;AAAA,MACX,QAAQ,KAAK;AAAA,IAAA;AAAA,EAEjB;AACF;AAEO,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAMA,SAAS,YAAY,MAA2B;AAC9C,UAAQ,KAAK,MAAA;AAAA,IACX,KAAK;AACH,aAAO,KAAK;AAAA,IACd,KAAK;AACH,aAAO,KAAK,QAAA;AAAA,IACd,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,EAAA;AAEb;"}
|
|
1
|
+
{"version":3,"file":"planner-join.js","sources":["../../../../../zql/src/planner/planner-join.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {\n mergeConstraints,\n type PlannerConstraint,\n} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport {omitFanout} from './planner-node.ts';\nimport type {\n CostEstimate,\n JoinOrConnection,\n PlannerNode,\n} from './planner-node.ts';\nimport type {PlannerTerminus} from './planner-terminus.ts';\n\n/**\n * Translate constraints for a flipped join from parent space to child space.\n * Matches the runtime behavior of FlippedJoin.fetch() which translates\n * parent constraints to child constraints using index-based key mapping.\n *\n * Example:\n * parentConstraint = {issueID: undefined, projectID: undefined}\n * childConstraint = {id: undefined, projectID: undefined}\n * incomingConstraint = {issueID: 5}\n * result = {id: 5} // issueID at index 0 maps to id at index 0\n */\nfunction translateConstraintsForFlippedJoin(\n incomingConstraint: PlannerConstraint | undefined,\n parentConstraint: PlannerConstraint,\n childConstraint: PlannerConstraint,\n): PlannerConstraint | undefined {\n if (!incomingConstraint) return undefined;\n\n const parentKeys = Object.keys(parentConstraint);\n const childKeys = Object.keys(childConstraint);\n const translated: PlannerConstraint = {};\n\n for (const [key, value] of Object.entries(incomingConstraint)) {\n const index = parentKeys.indexOf(key);\n if (index !== -1) {\n // Found this key in parent at position `index`\n // Map to child key at same position\n translated[childKeys[index]] = value;\n }\n }\n\n return Object.keys(translated).length > 0 ? translated : undefined;\n}\n\n/**\n * Semi-join overhead multiplier.\n *\n * Semi-joins represent correlated subqueries (EXISTS checks) which have\n * execution overhead compared to flipped joins, even when logical row counts\n * are identical. This overhead comes from:\n * - Need to execute a separate correlation check for each parent row\n * - Cannot leverage combined constraint checking as effectively as flipped joins\n *\n * A multiplier of 1.5 means semi-joins are estimated to be ~50% more expensive\n * than equivalent flipped joins, which empirically matches observed performance\n * differences in production workloads (e.g., 1.7x in zbugs benchmarks).\n *\n * Flipped joins have a different overhead in that they become unlimited. This\n * is accounted for when propagating unlimits rather than here.\n */\n// const SEMI_JOIN_OVERHEAD_MULTIPLIER = 1.5;\n\n/**\n * Represents a join between two data streams (parent and child).\n *\n * # Dual-State Pattern\n * Like all planner nodes, PlannerJoin separates:\n * 1. IMMUTABLE STRUCTURE: Parent/child nodes, constraints, flippability\n * 2. MUTABLE STATE: Join type (semi/flipped), pinned status\n *\n * # Join Flipping\n * A join can be in two states:\n * - 'semi': Parent is outer loop, child is inner (semi-join for EXISTS)\n * - 'flipped': Child is outer loop, parent is inner\n *\n * Flipping is the key optimization: choosing which table scans first.\n * NOT EXISTS joins cannot be flipped (#flippable = false).\n *\n * # Constraint Propagation\n * - Semi-join: Sends childConstraint to child, forwards received constraints to parent\n * - Flipped join: Sends undefined to child, merges parentConstraint with received to parent\n * - Unpinned join: Only forwards constraints to parent (doesn't constrain child yet)\n *\n * # Lifecycle\n * 1. Construct with immutable structure (parent, child, constraints, flippability)\n * 2. Wire to output node during graph construction\n * 3. Planning calls flipIfNeeded() based on connection selection order\n * 4. pin() locks the join type once chosen\n * 5. reset() clears mutable state (type → 'semi', pinned → false)\n */\nexport class PlannerJoin {\n readonly kind = 'join' as const;\n\n readonly #parent: Exclude<PlannerNode, PlannerTerminus>;\n readonly #child: Exclude<PlannerNode, PlannerTerminus>;\n readonly #parentConstraint: PlannerConstraint;\n readonly #childConstraint: PlannerConstraint;\n readonly #flippable: boolean;\n readonly planId: number;\n #output?: PlannerNode | undefined; // Set once during graph construction\n\n // Reset between planning attempts\n #type: 'semi' | 'flipped';\n readonly #initialType: 'semi' | 'flipped';\n\n constructor(\n parent: Exclude<PlannerNode, PlannerTerminus>,\n child: Exclude<PlannerNode, PlannerTerminus>,\n parentConstraint: PlannerConstraint,\n childConstraint: PlannerConstraint,\n flippable: boolean,\n planId: number,\n initialType: 'semi' | 'flipped' = 'semi',\n ) {\n this.#type = initialType;\n this.#initialType = initialType;\n this.#parent = parent;\n this.#child = child;\n this.#childConstraint = childConstraint;\n this.#parentConstraint = parentConstraint;\n this.#flippable = flippable;\n this.planId = planId;\n }\n\n setOutput(node: PlannerNode): void {\n this.#output = node;\n }\n\n get output(): PlannerNode {\n assert(this.#output !== undefined, 'Output not set');\n return this.#output;\n }\n\n closestJoinOrSource(): JoinOrConnection {\n return 'join';\n }\n\n flipIfNeeded(input: PlannerNode): void {\n if (input === this.#child) {\n this.flip();\n } else {\n assert(\n input === this.#parent,\n 'Can only flip a join from one of its inputs',\n );\n }\n }\n\n flip(): void {\n assert(this.#type === 'semi', 'Can only flip a semi-join');\n if (!this.#flippable) {\n throw new UnflippableJoinError(\n 'Cannot flip a non-flippable join (e.g., NOT EXISTS)',\n );\n }\n this.#type = 'flipped';\n }\n\n get type(): 'semi' | 'flipped' {\n return this.#type;\n }\n isFlippable(): boolean {\n return this.#flippable;\n }\n\n /**\n * Propagate unlimiting when this join is flipped.\n * When a join is flipped:\n * 1. Child becomes outer loop → produces all rows (unlimited)\n * 2. Parent is fetched once per child row → effectively unlimited\n *\n * Example: If child produces 896 rows, parent is fetched 896 times.\n * Even if each fetch returns 1 row, parent produces 896 total rows.\n *\n * Propagation rules:\n * - Connection: call unlimit()\n * - Semi-join: continue to parent (outer loop)\n * - Flipped join: stop (already unlimited when it was flipped)\n * - Fan-out/Fan-in: propagate to all inputs\n */\n propagateUnlimit(): void {\n assert(this.#type === 'flipped', 'Can only unlimit a flipped join');\n // Parent stays limited; child becomes unlimited\n this.#child.propagateUnlimitFromFlippedJoin(); // Up the child chain\n }\n\n /**\n * Called when a parent join is flipped and this join is part of its child subgraph.\n * Continue propagation to parent (the outer loop).\n * If we are hitting a semi-join, the parent drives.\n * If we are hitting a flip-join, well now we have to unlimit its parent too!\n */\n propagateUnlimitFromFlippedJoin(): void {\n this.#parent.propagateUnlimitFromFlippedJoin();\n }\n\n propagateConstraints(\n branchPattern: number[],\n constraint: PlannerConstraint | undefined,\n from?: PlannerNode,\n planDebugger?: PlanDebugger,\n ): void {\n planDebugger?.log({\n type: 'node-constraint',\n nodeType: 'join',\n node: this.getName(),\n branchPattern,\n constraint,\n from: from ? getNodeName(from) : 'unknown',\n });\n\n if (this.#type === 'semi') {\n // A semi-join always has constraints for its child.\n // They are defined by the correlation between parent and child.\n this.#child.propagateConstraints(\n branchPattern,\n this.#childConstraint,\n this,\n planDebugger,\n );\n // A semi-join forwards constraints to its parent.\n this.#parent.propagateConstraints(\n branchPattern,\n constraint,\n this,\n planDebugger,\n );\n } else if (this.#type === 'flipped') {\n // A flipped join translates constraints from parent space to child space.\n // This matches FlippedJoin.fetch() runtime behavior where parent constraints\n // on join keys are translated to child constraints.\n // Example: If parent has {issueID: 5} and join maps issueID→id,\n // child gets {id: 5} allowing index usage.\n const translatedConstraint = translateConstraintsForFlippedJoin(\n constraint,\n this.#parentConstraint,\n this.#childConstraint,\n );\n this.#child.propagateConstraints(\n branchPattern,\n translatedConstraint,\n this,\n planDebugger,\n );\n // A flipped join will have constraints to send to its parent.\n // - The constraints its output sent\n // - The constraints its child creates\n this.#parent.propagateConstraints(\n branchPattern,\n mergeConstraints(constraint, this.#parentConstraint),\n this,\n planDebugger,\n );\n }\n }\n\n reset(): void {\n this.#type = this.#initialType;\n }\n\n estimateCost(\n /**\n * This argument is to deal with consecutive `andExists` statements.\n * Each one will constrain how often a parent row passes all constraints.\n * This means that we have to scan more and more parent rows the more\n * constraints we add.\n *\n * DownstreamChildSelectivity factors in fanout factor\n * from parent -> child\n */\n downstreamChildSelectivity: number,\n /**\n * branchPattern uniquely identifies OR branches in the graph.\n * Each path through an OR will have unique constraints to apply to the source\n * connection.\n * branchPattern allows us to correlate a path through the graph\n * to the constraints that should be applied for that path.\n *\n * Example graph:\n * UFO\n * / \\\n * J1 J2\n * \\ /\n * UFI\n *\n * J1 and J2 are joins inside an OR (FO).\n * branchPattern [0] = path through J1\n * branchPattern [1] = path through J2\n *\n * If many ORs are nested, branchPattern will have multiple elements\n * representing each level of OR.\n *\n * If no joins are flipped within the `OR`, then only a single\n * branchPattern element will be needed, as FO represents all sub-joins\n * as a single path.\n */\n branchPattern: number[],\n planDebugger?: PlanDebugger,\n ): CostEstimate {\n /**\n * downstreamChildSelectivity accumulates up a parent chain, not\n * up child chains. Child chains represent independent sub-graphs.\n * So we pass 1 for `downstreamChildSelectivity` when estimating child cost.\n * Put another way, downstreamChildSelectivity impacts how many parent\n * rows are returned.\n */\n const child = this.#child.estimateCost(1, branchPattern, planDebugger);\n\n const fanoutFactor = child.fanout(Object.keys(this.#childConstraint));\n // Factor in how many child rows match a parent row.\n // E.g., if an issue has 10 comments on average then we're more\n // likely to hit a comment compared to if an issue has 1 comment on average.\n // If an index is all nulls (no parents match any children)\n // this will collapse to 0.\n const scaledChildSelectivity =\n 1 - Math.pow(1 - child.selectivity, fanoutFactor.fanout);\n\n // Why do we not need fanout in the other direction?\n // E.g., for an `inventory -> film` flipped-join, if each film has 100 inventories (100 copies)\n // then we're more likely to hit an inventory row compared to if each film has 1 inventory.\n // Flipped-join already accounts for this because the child selectivity is implicitly accounted\n // for. The returned row estimate of the child is already representative of how many\n // rows the child will have post-filtering.\n\n /**\n * How selective is the graph from this point forward?\n * If we are _very_ selective then we must scan more parent rows\n * before finding a match.\n * E.g., if childSelectivity = 0.1 and downstreamChildSelectivity = 0.5\n * then we only pass 5% of parent rows (0.1 * 0.5 = 0.05).\n *\n * This is used to estimate how many rows will be pulled from the parent\n * when trying to satisfy downstream constraints and a limit.\n *\n * NOTE: We do not know if the probabilities are correlated so we assume independence.\n * This is a fundamental limitation of the planner.\n */\n const parent = this.#parent.estimateCost(\n // Selectivity flows up the graph from child to parent\n // so we can determine the total selectivity of all ANDed exists checks.\n this.#type === 'flipped'\n ? 1 * downstreamChildSelectivity\n : scaledChildSelectivity * downstreamChildSelectivity,\n branchPattern,\n planDebugger,\n );\n\n let costEstimate: CostEstimate;\n\n if (this.type === 'semi') {\n costEstimate = {\n startupCost: parent.startupCost,\n scanEst:\n parent.limit === undefined\n ? parent.returnedRows\n : Math.min(\n parent.returnedRows,\n downstreamChildSelectivity === 0\n ? 0\n : parent.limit / downstreamChildSelectivity,\n ),\n cost:\n parent.cost +\n parent.scanEst * (child.startupCost + child.cost + child.scanEst),\n returnedRows: parent.returnedRows * child.selectivity,\n selectivity: child.selectivity * parent.selectivity,\n limit: parent.limit,\n fanout: parent.fanout,\n };\n } else {\n costEstimate = {\n startupCost: child.startupCost,\n scanEst:\n parent.limit === undefined\n ? parent.returnedRows * child.returnedRows\n : Math.min(\n parent.returnedRows * child.returnedRows,\n downstreamChildSelectivity === 0\n ? 0\n : parent.limit / downstreamChildSelectivity,\n ),\n cost:\n child.cost +\n child.scanEst * (parent.startupCost + parent.cost + parent.scanEst),\n // the child selectivity is not relevant here because it has already been taken into account via the flipping.\n // I.e., `child.returnedRows` is the estimated number of rows produced by the child _after_ taking filtering into account.\n returnedRows: parent.returnedRows * child.returnedRows,\n selectivity: parent.selectivity * child.selectivity,\n limit: parent.limit,\n fanout: parent.fanout,\n };\n }\n\n if (planDebugger) {\n planDebugger.log({\n type: 'node-cost',\n nodeType: 'join',\n node: this.getName(),\n branchPattern,\n downstreamChildSelectivity,\n costEstimate: omitFanout(costEstimate),\n joinType: this.#type,\n });\n }\n\n return costEstimate;\n }\n\n /**\n * Get a human-readable name for this join for debugging.\n * Format: \"parentName ⋈ childName\"\n */\n getName(): string {\n const parentName = getNodeName(this.#parent);\n const childName = getNodeName(this.#child);\n return `${parentName} ⋈ ${childName}`;\n }\n\n /**\n * Get debug information about this join's state.\n */\n getDebugInfo(): {\n name: string;\n type: 'semi' | 'flipped';\n planId: number;\n } {\n return {\n name: this.getName(),\n type: this.#type,\n planId: this.planId,\n };\n }\n}\n\nexport class UnflippableJoinError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'UnflippableJoinError';\n }\n}\n\n/**\n * Get a human-readable name for any planner node.\n * Used for debugging and tracing.\n */\nfunction getNodeName(node: PlannerNode): string {\n switch (node.kind) {\n case 'connection':\n return node.name;\n case 'join':\n return node.getName();\n case 'fan-out':\n return 'FO';\n case 'fan-in':\n return 'FI';\n case 'terminus':\n return 'terminus';\n }\n}\n"],"names":[],"mappings":";;;AAyBA,SAAS,mCACP,oBACA,kBACA,iBAC+B;AAC/B,MAAI,CAAC,mBAAoB,QAAO;AAEhC,QAAM,aAAa,OAAO,KAAK,gBAAgB;AAC/C,QAAM,YAAY,OAAO,KAAK,eAAe;AAC7C,QAAM,aAAgC,CAAA;AAEtC,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,kBAAkB,GAAG;AAC7D,UAAM,QAAQ,WAAW,QAAQ,GAAG;AACpC,QAAI,UAAU,IAAI;AAGhB,iBAAW,UAAU,KAAK,CAAC,IAAI;AAAA,IACjC;AAAA,EACF;AAEA,SAAO,OAAO,KAAK,UAAU,EAAE,SAAS,IAAI,aAAa;AAC3D;AAgDO,MAAM,YAAY;AAAA,EACd,OAAO;AAAA,E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,QAAQ,KAAK;AAAA,IAAA;AAAA,EAEjB;AACF;AAEO,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAMA,SAAS,YAAY,MAA2B;AAC9C,UAAQ,KAAK,MAAA;AAAA,IACX,KAAK;AACH,aAAO,KAAK;AAAA,IACd,KAAK;AACH,aAAO,KAAK,QAAA;AAAA,IACd,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,EAAA;AAEb;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rocicorp/zero",
|
|
3
|
-
"version": "0.25.0-canary.
|
|
3
|
+
"version": "0.25.0-canary.25",
|
|
4
4
|
"description": "Zero is a web framework for serverless web development.",
|
|
5
5
|
"author": "Rocicorp, Inc.",
|
|
6
6
|
"repository": {
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@op-engineering/op-sqlite": ">=15",
|
|
81
81
|
"@rocicorp/prettier-config": "^0.4.0",
|
|
82
|
-
"@vitest/runner": "
|
|
82
|
+
"@vitest/runner": "4.0.15",
|
|
83
83
|
"analyze-query": "0.0.0",
|
|
84
84
|
"ast-to-zql": "0.0.0",
|
|
85
85
|
"expo-sqlite": ">=15",
|
|
@@ -88,8 +88,8 @@
|
|
|
88
88
|
"typedoc": "^0.28.2",
|
|
89
89
|
"typedoc-plugin-markdown": "^4.6.1",
|
|
90
90
|
"typescript": "~5.9.3",
|
|
91
|
-
"vite": "
|
|
92
|
-
"vitest": "
|
|
91
|
+
"vite": "7.2.7",
|
|
92
|
+
"vitest": "4.0.15",
|
|
93
93
|
"zero-cache": "0.0.0",
|
|
94
94
|
"zero-client": "0.0.0",
|
|
95
95
|
"zero-pg": "0.0.0",
|