@rocicorp/zero 1.11.0-canary.23 → 1.11.0-canary.24

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.
@@ -1 +1 @@
1
- {"version":3,"file":"pipeline-driver.js","names":["#tables","#pipelines","#rowSetSignatures","#lc","#snapshotter","#storage","#shardID","#logConfig","#config","#deferredWrites","#tableSpecs","#allTableNames","#costModels","#planWarningThresholds","#yieldThresholdMs","#advanceTime","#conflictRowsDeleted","#deferredWritesFallbacks","#inspectorDelegate","#queryStats","#metricsDelegate","#recordAdvancePush","#initAndResetCommon","#destroyPipeline","#primaryKeys","#streamer","#replicaVersion","#permissions","#disableCorrelatedPredicatePushdown","#getSource","#createStorage","#trackRowSetSignatures","#addQueryImpl","#logQueryPipelineLifecycle","#ensureCostModelExistsIfEnabled","#advanceContext","#hydrateContext","#totalRowsRead","#resolveScalarSubqueries","#warnIgnoredScalarHints","#logPlanWarnings","#streamPushed","#advance","#reserveDeferredWrites","#shouldAdvanceYieldMaybeAbortAdvance","#reserveSecondRow","#push","#holdPendingRows","#writeThrough","#logSlowAdvances","#logAdvanceTimeout","#releaseDeferredWrites","#recordAdvanceStats","#queryForLog","#advancementLogFields","#shouldYield","#throwSlowCurrentChangeReset","#throwProjectedAdvancementReset","#pushedRowChanges","#input","#queryHash","#transformationHash","#queryName","#output"],"sources":["../../../../../../zero-cache/src/services/view-syncer/pipeline-driver.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert, unreachable} from '../../../../shared/src/asserts.ts';\nimport {deepEqual, type JSONValue} from '../../../../shared/src/json.ts';\nimport {getOrInsertComputed} from '../../../../shared/src/map.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {assignProperty} from '../../../../shared/src/objects.ts';\nimport {randInt} from '../../../../shared/src/rand.ts';\nimport type {Writable} from '../../../../shared/src/writable.ts';\nimport type {AST, LiteralValue} from '../../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../../zero-protocol/src/client-schema.ts';\nimport type {Row} from '../../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../../zero-protocol/src/primary-key.ts';\nimport {buildPipeline} from '../../../../zql/src/builder/builder.ts';\nimport {\n Debug,\n runtimeDebugFlags,\n} from '../../../../zql/src/builder/debug-delegate.ts';\nimport {ChangeIndex} from '../../../../zql/src/ivm/change-index.ts';\nimport {ChangeType} from '../../../../zql/src/ivm/change-type.ts';\nimport type {Change} from '../../../../zql/src/ivm/change.ts';\nimport type {Node} from '../../../../zql/src/ivm/data.ts';\nimport {\n skipYields,\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Output,\n type Storage,\n} from '../../../../zql/src/ivm/operator.ts';\nimport type {SourceSchema} from '../../../../zql/src/ivm/schema.ts';\nimport {\n type Source,\n type SourceChange,\n type SourceInput,\n makeSourceChangeAdd,\n makeSourceChangeEdit,\n makeSourceChangeRemove,\n} from '../../../../zql/src/ivm/source.ts';\nimport type {Stream} from '../../../../zql/src/ivm/stream.ts';\nimport type {ConnectionCostModel} from '../../../../zql/src/planner/planner-connection.ts';\nimport type {\n PlanWarning,\n PlanWarningThresholds,\n} from '../../../../zql/src/planner/planner-warnings.ts';\nimport {MeasurePushOperator} from '../../../../zql/src/query/measure-push-operator.ts';\nimport type {\n MetricMap,\n MetricsDelegate,\n} from '../../../../zql/src/query/metrics-delegate.ts';\nimport type {ClientGroupStorage} from '../../../../zqlite/src/database-storage.ts';\nimport type {Database} from '../../../../zqlite/src/db.ts';\nimport {\n resolveSimpleScalarSubqueries,\n type CompanionSubquery,\n type IgnoredScalarHint,\n} from '../../../../zqlite/src/resolve-scalar-subqueries.ts';\nimport {createSQLiteCostModel} from '../../../../zqlite/src/sqlite-cost-model.ts';\nimport {TableSource} from '../../../../zqlite/src/table-source.ts';\nimport {\n reloadPermissionsIfChanged,\n type LoadedPermissions,\n} from '../../auth/load-permissions.ts';\nimport type {LogConfig, ZeroConfig} from '../../config/zero-config.ts';\nimport {computeZqlSpecs, mustGetTableSpec} from '../../db/lite-tables.ts';\nimport type {LiteAndZqlSpec, LiteTableSpec} from '../../db/specs.ts';\nimport {LogThrottle} from '../../observability/log-throttle.ts';\nimport {\n getOrCreateCounter,\n getOrCreateLatencyHistogram,\n} from '../../observability/metrics.ts';\nimport type {InspectorDelegate} from '../../server/inspector-delegate.ts';\nimport {type RowKey} from '../../types/row-key.ts';\nimport {type ShardID} from '../../types/shards.ts';\nimport {\n getSubscriptionState,\n ZERO_VERSION_COLUMN_NAME,\n} from '../replicator/schema/replication-state.ts';\nimport {checkClientSchema} from './client-schema.ts';\nimport type {DeferredWritesBudget} from './deferred-writes-budget.ts';\nimport {planWarningMessage} from './plan-warnings.ts';\nimport {queryShape, type QueryShape} from './query-shape.ts';\nimport type {QueryStats} from './query-stats.ts';\nimport {rowIDSignatureUnit} from './row-set-signature.ts';\nimport type {Snapshotter} from './snapshotter.ts';\nimport {ResetPipelinesSignal, type SnapshotDiff} from './snapshotter.ts';\n\ntype RowOp<Op extends Omit<ChangeType, ChangeType.CHILD>> = {\n readonly type: Op;\n readonly queryID: string;\n readonly table: string;\n readonly rowKey: Row;\n readonly row: Row;\n};\n\nexport type RowAdd = RowOp<ChangeType.ADD>;\n\nexport type RowRemove = RowOp<ChangeType.REMOVE>;\n\nexport type RowEdit = RowOp<ChangeType.EDIT>;\n\nexport type RowChange = RowAdd | RowRemove | RowEdit;\n\ntype CompanionPipeline = {\n readonly input: Input;\n readonly childField: string;\n readonly resolvedValue: LiteralValue | null | undefined;\n};\n\ntype Pipeline = {\n readonly input: Input;\n readonly hydrationTimeMs: number;\n readonly hydrationRowCount: number;\n readonly hydrationRowsRead: number;\n readonly planWarnings: readonly PlanWarning[];\n readonly hydrationReason: PipelineHydrationReason;\n readonly pipelineRunID: string;\n readonly pipelineReadyAtMs: number;\n readonly transformedAst: AST;\n readonly originalAst: AST;\n /** The {@link queryShape} of {@link originalAst}. */\n readonly shape: QueryShape;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n readonly companions: readonly CompanionPipeline[];\n};\n\nexport type QueryInfo = {\n readonly transformedAst: AST;\n readonly originalAst?: AST | undefined;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n};\n\nexport type HydrationStats = {\n /** The rows the hydration output. */\n readonly rowCount: number;\n /**\n * The rows the hydration read from the replica, including rows that were\n * then filtered out, e.g. by a filter that could not be pushed to SQLite or\n * by an EXISTS that did not match. Much greater than {@link rowCount} means\n * the query does a lot of work for the rows it returns.\n */\n readonly rowsRead: number;\n /** What the planner warned about the plan it chose for the query. */\n readonly planWarnings: readonly PlanWarning[];\n /** The {@link queryShape} of the query, computed when it was hydrated. */\n readonly shape: QueryShape;\n};\n\ntype QueryLogInfo = {\n readonly queryHash: string;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n};\n\ntype QueryPipelineLifecycleEvent =\n | 'query-pipeline-hydrate-start'\n | 'query-pipeline-hydrate-finish'\n | 'query-pipeline-hydrate-failed'\n | 'query-pipeline-hydrate-aborted'\n | 'query-pipeline-stop';\n\nexport type PipelineHydrationReason =\n | 'query-set-sync'\n | 'unchanged-query-rehydrate';\n\ntype PipelineStopReason =\n | 'replace-query'\n | 'remove-query'\n | 'reset'\n | 'destroy';\n\ntype QueryPipelineLifecycleLog = {\n readonly zeroEvent: QueryPipelineLifecycleEvent;\n readonly pipelineRunID: string;\n readonly queryHash: string;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n readonly hydrationReason?: PipelineHydrationReason | undefined;\n readonly stopReason?: PipelineStopReason | undefined;\n readonly hydrationTimeMs?: number | undefined;\n readonly hydrationRowCount?: number | undefined;\n readonly hydrationRowsRead?: number | undefined;\n readonly pipelineLifetimeMs?: number | undefined;\n};\n\nexport type AdvancementProgress = {\n /** Raw ChangeLog entries read, including skipped entries and the current change. */\n readonly changesScanned: number;\n /** Yielded source changes whose push processing completed. */\n readonly changesProcessed: number;\n /** Raw entries omitted by the snapshot diff. */\n readonly changesSkipped: number;\n};\n\ntype AdvancementLogFields = AdvancementProgress & {\n readonly advancementTimeMs: number;\n readonly queryPushTimeMs: number;\n readonly outsideQueryPushTimeMs: number;\n readonly totalHydrationTimeMs: number;\n readonly changesCompleted: number;\n};\n\ntype AdvanceContext = {\n readonly timer: Timer;\n readonly progress: {changesProcessed: number};\n readonly diff: SnapshotDiff;\n /** Raw entries completed, including skipped entries but excluding an in-flight push. */\n completedChanges: number;\n readonly totalHydrationTimeMs: number;\n readonly numChanges: number;\n /**\n * The rows reserved from the {@link DeferredWritesBudget} while the\n * advancement's changes are held in memory.\n */\n reservedRows: number | undefined;\n /** The bytes the advancement has added to the budget's held bytes. */\n heldBytes: number;\n currentChangeStartMs: number | undefined;\n pos: number;\n /** The table of the change being pushed. */\n currentTable: string | undefined;\n /** The processing time of each query's pushes, by query ID. */\n readonly queryStats: Map<string, QueryAdvanceStats>;\n};\n\n/**\n * Whether the rows an advancement holds in memory fit in the budget, or why\n * they do not: the bytes held, a change that needed more rows than were left\n * to reserve, or more rows held than reserved.\n */\ntype HeldRows = 'fits' | 'bytes' | 'rows' | 'row-overrun';\n\ntype QueryAdvanceStats = {\n /** The time spent processing pushes to the query. */\n timeMs: number;\n /** The number of changes pushed to the query. */\n changes: number;\n /** The position of the last change pushed to the query. */\n lastPos: number;\n /** {@link timeMs} by the table of the change pushed. */\n readonly timeMsByTable: Map<string, number>;\n};\n\n/** The most queries a log of an advancement timeout lists. */\nconst ADVANCE_TIMEOUT_LOG_MAX_QUERIES = 3;\n\ntype HydrateContext = {\n readonly timer: Timer;\n};\n\nexport type Timer = {\n elapsedLap: () => number;\n totalElapsed: () => number;\n};\n\n/**\n * No matter how fast hydration is, advancement is given at least this long to\n * complete before doing a pipeline reset.\n */\nconst MIN_ADVANCEMENT_TIME_LIMIT_MS = 50;\nconst MIN_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES = 8;\nconst PROJECTED_ADVANCEMENT_SAMPLE_FRACTION = 0.25;\nconst MAX_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES = 50;\nconst MIN_PROJECTED_ADVANCEMENT_SAMPLE_MS = 5;\nconst MIN_PROJECTED_ADVANCEMENT_CHANGES = 16;\nconst PROJECTED_ADVANCEMENT_RESET_MULTIPLIER = 1.5;\nconst LATE_ADVANCEMENT_FINISH_PROGRESS = 0.8;\n\n/**\n * The planner warns about a query each time it is planned, i.e. for every\n * client group that hydrates it, and the warnings only change when the data\n * does. So they are logged at most once per query shape per this window,\n * per process.\n */\nconst PLAN_WARNING_LOG_WINDOW_MS = 60 * 60_000;\n\n// Shared by all PipelineDrivers in the process, so that a query shape is\n// throttled across client groups.\nconst planWarningLogThrottle = new LogThrottle({\n windowMs: PLAN_WARNING_LOG_WINDOW_MS,\n});\n\n/**\n * Slow advancements of a query shape, and advancement timeouts led by it,\n * are logged at most once per this window per process.\n */\nconst SLOW_ADVANCE_LOG_WINDOW_MS = 5 * 60_000;\n\nconst slowAdvanceLogThrottle = new LogThrottle({\n windowMs: SLOW_ADVANCE_LOG_WINDOW_MS,\n});\n\nfunction randomID() {\n return randInt(1, Number.MAX_SAFE_INTEGER).toString(36);\n}\n\nfunction projectedAdvancementTimeMs(\n elapsedMs: number,\n processedChanges: number,\n numChanges: number,\n): number | undefined {\n if (processedChanges <= 0 || numChanges <= 0) {\n return undefined;\n }\n return (elapsedMs / processedChanges) * numChanges;\n}\n\nfunction advancementResetTimeLimitMs(totalHydrationTimeMs: number): number {\n return Math.max(totalHydrationTimeMs, 1);\n}\n\nfunction minProjectedAdvancementSampleChanges(numChanges: number): number {\n return Math.max(\n MIN_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES,\n Math.min(\n MAX_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES,\n Math.ceil(numChanges * PROJECTED_ADVANCEMENT_SAMPLE_FRACTION),\n ),\n );\n}\n\nfunction shouldResetProjectedAdvancement(\n elapsedMs: number,\n projectedTotalTimeMs: number | undefined,\n processedChanges: number,\n numChanges: number,\n totalHydrationTimeMs: number,\n): boolean {\n if (\n projectedTotalTimeMs === undefined ||\n numChanges < MIN_PROJECTED_ADVANCEMENT_CHANGES ||\n processedChanges < minProjectedAdvancementSampleChanges(numChanges) ||\n elapsedMs < MIN_PROJECTED_ADVANCEMENT_SAMPLE_MS\n ) {\n return false;\n }\n\n // The time already spent is spent whether or not the advancement is reset,\n // so a reset only saves the rest of it.\n return (\n projectedTotalTimeMs - elapsedMs >\n advancementResetTimeLimitMs(totalHydrationTimeMs) *\n PROJECTED_ADVANCEMENT_RESET_MULTIPLIER\n );\n}\n\nfunction shouldFinishLateAdvancement(\n processedChanges: number,\n numChanges: number,\n): boolean {\n return (\n numChanges > 0 &&\n processedChanges / numChanges >= LATE_ADVANCEMENT_FINISH_PROGRESS\n );\n}\n\nfunction shouldResetSlowCurrentChange(\n currentChangeElapsedMs: number,\n totalHydrationTimeMs: number,\n): boolean {\n return (\n currentChangeElapsedMs > MIN_ADVANCEMENT_TIME_LIMIT_MS &&\n currentChangeElapsedMs > advancementResetTimeLimitMs(totalHydrationTimeMs)\n );\n}\n\n/**\n * Manages the state of IVM pipelines for a given ViewSyncer (i.e. client group).\n */\nexport class PipelineDriver {\n // Retain sources until reset or destroy so getRow can serve catch-up reads.\n readonly #tables = new Map<string, TableSource>();\n // Query id to pipeline\n readonly #pipelines = new Map<string, Pipeline>();\n /**\n * XOR signature of the set of rows currently attached to each active\n * query, maintained as RowChanges are yielded from {@link addQuery} and\n * {@link advance}. ADDs / REMOVEs XOR the row's unit in (XOR is\n * self-inverse, so one op serves both directions); EDITs are no-ops.\n * Hydration implicitly reseeds from `0n` because {@link addQuery} calls\n * {@link removeQuery} first, which deletes the entry.\n */\n readonly #rowSetSignatures = new Map<string, bigint>();\n\n readonly #lc: LogContext;\n readonly #snapshotter: Snapshotter;\n readonly #storage: ClientGroupStorage;\n readonly #shardID: ShardID;\n readonly #logConfig: LogConfig;\n readonly #config: ZeroConfig | undefined;\n readonly #deferredWrites: DeferredWritesBudget | undefined;\n readonly #tableSpecs = new Map<string, LiteAndZqlSpec>();\n readonly #allTableNames = new Set<string>();\n readonly #costModels: WeakMap<Database, ConnectionCostModel> | undefined;\n readonly #planWarningThresholds: PlanWarningThresholds | undefined;\n readonly #yieldThresholdMs: () => number;\n #streamer: Streamer | null = null;\n /** The row changes of the push in progress, until #push yields them. */\n #pushedRowChanges: RowChange[] | null = null;\n #hydrateContext: HydrateContext | null = null;\n #advanceContext: AdvanceContext | null = null;\n #replicaVersion: string | null = null;\n #primaryKeys: Map<string, PrimaryKey> | null = null;\n #permissions: LoadedPermissions | null = null;\n\n readonly #advanceTime = getOrCreateLatencyHistogram(\n 'sync',\n 'ivm.advance-time',\n 'Time to advance all queries for a given client group in response to a single change.',\n );\n\n readonly #conflictRowsDeleted = getOrCreateCounter(\n 'sync',\n 'ivm.conflict-rows-deleted',\n 'Number of rows deleted because they conflicted with added row',\n );\n\n readonly #deferredWritesFallbacks = getOrCreateCounter(\n 'sync',\n 'ivm.deferred-writes-fallbacks',\n 'Number of advancements written through to the replica snapshot because ' +\n 'their changes did not fit in the deferred IVM writes budget, from the ' +\n 'start or partway (because of the bytes held, a change that needed ' +\n 'more rows than were left, or more rows held than reserved)',\n );\n\n readonly #inspectorDelegate: InspectorDelegate;\n readonly #queryStats: QueryStats | undefined;\n\n /**\n * Passes the pipelines' metrics on to the inspector, and accounts the time\n * spent pushing to each query to the advancement in progress.\n */\n readonly #metricsDelegate: MetricsDelegate = {\n addMetric: <K extends keyof MetricMap>(\n metric: K,\n value: number,\n ...args: MetricMap[K]\n ) => {\n this.#inspectorDelegate.addMetric(metric, value, ...args);\n if (metric === 'query-update-server') {\n this.#recordAdvancePush(args[0], value);\n }\n },\n };\n\n constructor(\n lc: LogContext,\n logConfig: LogConfig,\n snapshotter: Snapshotter,\n shardID: ShardID,\n storage: ClientGroupStorage,\n clientGroupID: string,\n inspectorDelegate: InspectorDelegate,\n yieldThresholdMs: () => number,\n enablePlanner?: boolean,\n config?: ZeroConfig,\n deferredWrites?: DeferredWritesBudget,\n queryStats?: QueryStats | undefined,\n ) {\n this.#lc = lc.withContext('clientGroupID', clientGroupID);\n this.#snapshotter = snapshotter;\n this.#storage = storage;\n this.#shardID = shardID;\n this.#logConfig = logConfig;\n this.#config = config;\n this.#deferredWrites = deferredWrites;\n this.#inspectorDelegate = inspectorDelegate;\n this.#costModels = enablePlanner ? new WeakMap() : undefined;\n const planWarningThresholds = {\n rows: logConfig.planWarningRowThreshold,\n cost: logConfig.planWarningCostThreshold,\n };\n this.#planWarningThresholds =\n planWarningThresholds.rows > 0 || planWarningThresholds.cost > 0\n ? planWarningThresholds\n : undefined;\n this.#yieldThresholdMs = yieldThresholdMs;\n this.#queryStats = queryStats;\n }\n\n /**\n * Initializes the PipelineDriver to the current head of the database.\n * Queries can then be added (i.e. hydrated) with {@link addQuery()}.\n *\n * Must only be called once.\n */\n init(clientSchema: ClientSchema) {\n assert(!this.#snapshotter.initialized(), 'Already initialized');\n this.#snapshotter.init();\n this.#initAndResetCommon(clientSchema);\n }\n\n /**\n * @returns Whether the PipelineDriver has been initialized.\n */\n initialized(): boolean {\n return this.#snapshotter.initialized();\n }\n\n /**\n * Clears the current pipelines and TableSources, returning the PipelineDriver\n * to its initial state. This should be called in response to a schema change,\n * as TableSources need to be recomputed.\n */\n reset(clientSchema: ClientSchema) {\n for (const [queryID, pipeline] of this.#pipelines) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, 'reset');\n }\n this.#tables.clear();\n this.#allTableNames.clear();\n this.#rowSetSignatures.clear();\n this.#initAndResetCommon(clientSchema);\n }\n\n #initAndResetCommon(clientSchema: ClientSchema) {\n const {db} = this.#snapshotter.current();\n const fullTables = new Map<string, LiteTableSpec>();\n computeZqlSpecs(\n this.#lc,\n db.db,\n {includeBackfillingColumns: false},\n this.#tableSpecs,\n fullTables,\n );\n checkClientSchema(\n this.#shardID,\n clientSchema,\n this.#tableSpecs,\n fullTables,\n );\n this.#allTableNames.clear();\n for (const table of fullTables.keys()) {\n this.#allTableNames.add(table);\n }\n const primaryKeys = this.#primaryKeys ?? new Map<string, PrimaryKey>();\n this.#primaryKeys = primaryKeys;\n primaryKeys.clear();\n for (const [table, spec] of this.#tableSpecs.entries()) {\n primaryKeys.set(table, spec.tableSpec.primaryKey);\n }\n buildPrimaryKeys(clientSchema, primaryKeys);\n this.#streamer ??= new Streamer(primaryKeys, this.#tableSpecs);\n const {replicaVersion} = getSubscriptionState(db);\n this.#replicaVersion = replicaVersion;\n }\n\n /** @returns The replica version. The PipelineDriver must have been initialized. */\n get replicaVersion(): string {\n return must(this.#replicaVersion, 'Not yet initialized');\n }\n\n /**\n * Returns the current version of the database. This will reflect the\n * latest version change when calling {@link advance()} once the\n * iteration has begun.\n */\n currentVersion(): string {\n assert(this.initialized(), 'Not yet initialized');\n return this.#snapshotter.current().version;\n }\n\n /**\n * Returns the current upstream {app}.permissions, or `null` if none are defined.\n */\n currentPermissions(): LoadedPermissions | null {\n assert(this.initialized(), 'Not yet initialized');\n const res = reloadPermissionsIfChanged(\n this.#lc,\n this.#snapshotter.current().db,\n this.#shardID.appID,\n this.#permissions,\n this.#config,\n );\n if (res.changed) {\n this.#permissions = res.permissions;\n this.#lc.debug?.(\n 'Reloaded permissions',\n JSON.stringify(this.#permissions),\n );\n }\n return this.#permissions;\n }\n\n /**\n * Advances the snapshot to the head of the database without diffing the\n * change log, in preparation for hydrating queries at head.\n *\n * Throws a {@link ResetPipelinesSignal} if the change log records a\n * schema change since the previous snapshot. The table specs (and any\n * TableSources built from them) were computed at or before that snapshot\n * and are stale with respect to the new head, so the caller must\n * {@link reset()} before hydrating. ({@link advance()} detects this when\n * the diff encounters the RESET op; this path skips the diff and so must\n * check explicitly.)\n */\n advanceWithoutDiff(): string {\n const {prev, curr} = this.#snapshotter.advanceWithoutDiff();\n if (curr.schemaChangedSince(prev.version)) {\n throw new ResetPipelinesSignal(\n `schema changed between ${prev.version} and ${curr.version}`,\n 'schema-change',\n );\n }\n for (const table of this.#tables.values()) {\n table.setDB(curr.db.db);\n }\n return curr.version;\n }\n\n #ensureCostModelExistsIfEnabled(db: Database) {\n let existing = this.#costModels?.get(db);\n if (existing) {\n return existing;\n }\n if (this.#costModels) {\n const costModel = createSQLiteCostModel(db, this.#tableSpecs);\n this.#costModels.set(db, costModel);\n return costModel;\n }\n return undefined;\n }\n\n /**\n * Clears storage used for the pipelines. Call this when the\n * PipelineDriver will no longer be used.\n */\n destroy() {\n for (const [queryID, pipeline] of this.#pipelines) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, 'destroy');\n }\n this.#tables.clear();\n this.#rowSetSignatures.clear();\n this.#storage.destroy();\n this.#snapshotter.destroy();\n }\n\n /** @return Map from query ID to PipelineInfo for all added queries. */\n queries(): ReadonlyMap<string, QueryInfo> {\n return this.#pipelines;\n }\n\n /**\n * Stats from the hydration of the pipeline for `queryID`, or `undefined` if\n * the query has no pipeline.\n */\n hydrationStats(queryID: string): HydrationStats | undefined {\n const pipeline = this.#pipelines.get(queryID);\n return pipeline\n ? {\n rowCount: pipeline.hydrationRowCount,\n rowsRead: pipeline.hydrationRowsRead,\n planWarnings: pipeline.planWarnings,\n shape: pipeline.shape,\n }\n : undefined;\n }\n\n /**\n * Logs what the planner warned about the plan it chose for `query`, at\n * most once per query shape per {@link PLAN_WARNING_LOG_WINDOW_MS}.\n */\n #logPlanWarnings(\n shape: QueryShape,\n {queryHash, transformationHash, queryName}: QueryLogInfo,\n warnings: readonly PlanWarning[],\n ): void {\n if (!this.#lc.warn) {\n return;\n }\n const suppressed = planWarningLogThrottle.admit(\n `${queryName ?? ''}:${shape.hash}`,\n );\n if (suppressed === undefined) {\n return;\n }\n const messages = warnings.map(planWarningMessage);\n this.#lc.warn(\n `Query plan warning${queryName === undefined ? '' : ` for ${queryName}`}: ` +\n messages.join(' '),\n {\n zeroEvent: 'query-plan-warning',\n queryHash,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n warnings,\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n zql: shape.zql,\n },\n );\n }\n\n #totalRowsRead(): number {\n let total = 0;\n for (const table of this.#tables.values()) {\n total += table.rowsRead;\n }\n return total;\n }\n\n totalHydrationTimeMs(): number {\n let total = 0;\n for (const pipeline of this.#pipelines.values()) {\n total += pipeline.hydrationTimeMs;\n }\n return total;\n }\n\n /**\n * The rows the sources hold in memory for the advancement in progress.\n * The advancement's reservation from the {@link DeferredWritesBudget}\n * bounds this.\n */\n get pendingRows(): number {\n let total = 0;\n for (const source of this.#tables.values()) {\n total += source.pendingRows;\n }\n return total;\n }\n\n /**\n * The estimated bytes of {@link pendingRows}, which are added to the\n * {@link DeferredWritesBudget}'s held bytes as the advancement goes.\n */\n get pendingBytes(): number {\n let total = 0;\n for (const source of this.#tables.values()) {\n total += source.pendingBytes;\n }\n return total;\n }\n\n #logQueryPipelineLifecycle({\n zeroEvent,\n pipelineRunID,\n queryHash,\n transformationHash,\n queryName,\n hydrationReason,\n stopReason,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n pipelineLifetimeMs,\n }: QueryPipelineLifecycleLog): void {\n let lc = this.#lc\n .withContext('zeroEvent', zeroEvent)\n .withContext('pipelineRunID', pipelineRunID)\n .withContext('queryHash', queryHash)\n .withContext('transformationHash', transformationHash);\n if (queryName !== undefined) {\n lc = lc.withContext('queryName', queryName);\n }\n if (hydrationReason !== undefined) {\n lc = lc.withContext('hydrationReason', hydrationReason);\n }\n if (stopReason !== undefined) {\n lc = lc.withContext('stopReason', stopReason);\n }\n if (hydrationTimeMs !== undefined) {\n lc = lc.withContext('hydrationTimeMs', hydrationTimeMs);\n }\n if (hydrationRowCount !== undefined) {\n lc = lc.withContext('hydrationRowCount', hydrationRowCount);\n }\n if (hydrationRowsRead !== undefined) {\n lc = lc.withContext('hydrationRowsRead', hydrationRowsRead);\n }\n if (pipelineLifetimeMs !== undefined) {\n lc = lc.withContext('pipelineLifetimeMs', pipelineLifetimeMs);\n }\n lc.info?.('query pipeline lifecycle');\n }\n\n /**\n * A `{scalar: true}` that cannot be honored degrades silently to a plain\n * EXISTS, so the author gets none of the plan they asked for and no signal\n * that they didn't. Say so, with the unique keys that were actually\n * available — the client schema knows only primary keys, so this is the only\n * place the advice can be correct.\n */\n #warnIgnoredScalarHints(queryID: string, hints: IgnoredScalarHint[]): void {\n for (const {table, uniqueKeys} of hints) {\n const keys = uniqueKeys.map(k => `(${k.join(', ')})`).join(', ');\n this.#lc.warn?.(\n `Ignoring {scalar: true} on the \"${table}\" subquery of query ` +\n `${queryID}: it does not constrain every column of any unique key ` +\n `${keys.length > 0 ? `[${keys}]` : '(none on this table)'} to a ` +\n `literal with \"=\", so it is not provably limited to one row. ` +\n `The gate runs as a plain EXISTS.`,\n );\n }\n }\n\n #disableCorrelatedPredicatePushdown(): boolean {\n return this.#config?.enableCorrelatedPredicatePushdown === false;\n }\n\n #resolveScalarSubqueries(ast: AST): {\n ast: AST;\n companionRows: {table: string; row: Row}[];\n companions: CompanionSubquery[];\n companionInputs: Input[];\n ignoredScalarHints: IgnoredScalarHint[];\n } {\n const companionRows: {table: string; row: Row}[] = [];\n const companionInputs: Input[] = [];\n\n const executor = (\n subqueryAST: AST,\n childField: string,\n ): LiteralValue | null | undefined => {\n const input = buildPipeline(\n subqueryAST,\n {\n disableCorrelatedPredicatePushdown:\n this.#disableCorrelatedPredicatePushdown(),\n getSource: name => this.#getSource(name),\n createStorage: () => this.#createStorage(),\n decorateSourceInput: (input: SourceInput): Input => input,\n decorateInput: input => input,\n addEdge() {},\n decorateFilterInput: input => input,\n },\n 'scalar-subquery',\n );\n // Tracked before it is fetched so that a failure in this or a later\n // subquery can tear it down below. A companion with no result is kept\n // alive too: it detects a future insert that creates the row.\n companionInputs.push(input);\n // Consume the full stream rather than using first() to avoid\n // triggering early return on Take's #initialFetch assertion.\n // The subquery AST already has limit: 1, so at most one row is produced.\n let node: Node | undefined;\n for (const n of skipYields(input.fetch({}))) {\n node ??= n;\n }\n if (!node) {\n return undefined;\n }\n companionRows.push({table: subqueryAST.table, row: node.row as Row});\n return (node.row[childField] as LiteralValue) ?? null;\n };\n\n let resolved: AST;\n let companions: CompanionSubquery[];\n let ignoredScalarHints: IgnoredScalarHint[];\n try {\n ({\n ast: resolved,\n companions,\n ignoredScalarHints,\n } = resolveSimpleScalarSubqueries(ast, this.#tableSpecs, executor));\n } catch (e) {\n for (const input of companionInputs) {\n input.destroy();\n }\n throw e;\n }\n return {\n ast: resolved,\n companionRows,\n companions,\n companionInputs,\n ignoredScalarHints,\n };\n }\n\n /**\n * Adds a pipeline for the query. The method will hydrate the query using the\n * driver's current snapshot of the database and return a stream of results.\n * Henceforth, updates to the query will be returned when the driver is\n * {@link advance}d. The query and its pipeline can be removed with\n * {@link removeQuery()}.\n *\n * If a query with the same queryID is already added, the existing pipeline\n * will be removed and destroyed before adding the new pipeline.\n *\n * @param timer The caller-controlled {@link Timer} used to determine the\n * final hydration time. (The caller may pause and resume the timer\n * when yielding the thread for time-slicing).\n * @return The rows from the initial hydration of the query.\n */\n addQuery(\n transformationHash: string,\n queryID: string,\n query: AST,\n timer: Timer,\n queryName?: string,\n hydrationReason: PipelineHydrationReason = 'query-set-sync',\n ): Iterable<RowChange | 'yield'> {\n return this.#trackRowSetSignatures(\n this.#addQueryImpl(\n transformationHash,\n queryID,\n query,\n timer,\n queryName,\n hydrationReason,\n ),\n );\n }\n\n *#addQueryImpl(\n transformationHash: string,\n queryID: string,\n query: AST,\n timer: Timer,\n queryName?: string,\n hydrationReason: PipelineHydrationReason = 'query-set-sync',\n ): Iterable<RowChange | 'yield'> {\n assert(\n this.initialized(),\n 'Pipeline driver must be initialized before adding queries',\n );\n this.removeQuery(queryID, 'replace-query');\n const pipelineRunID = randomID();\n const shape = queryShape(query);\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-start',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n });\n const debugDelegate = runtimeDebugFlags.trackRowsVended\n ? new Debug(true)\n : undefined;\n\n const costModel = this.#ensureCostModelExistsIfEnabled(\n this.#snapshotter.current().db.db,\n );\n\n assert(\n this.#advanceContext === null,\n 'Cannot hydrate while advance is in progress',\n );\n this.#hydrateContext = {\n timer,\n };\n // Hydration has the driver to itself, so the rows read by all of its\n // tables in the meantime are the rows read by this hydration. Tables\n // added by the hydration start from zero, which this also accounts for.\n const rowsReadAtStart = this.#totalRowsRead();\n let hydrationFinished = false;\n let hydrationFailed = false;\n let hydrationRowCount = 0;\n let planWarnings: readonly PlanWarning[] = [];\n const planWarningThresholds = this.#planWarningThresholds;\n // The inputs built so far, held outside the try so that a hydration that\n // does not finish (aborted by the consumer or failed) can tear them down.\n // Only a finished hydration hands them over to #pipelines.\n let builtInputs: Input[] = [];\n try {\n const {\n ast: resolvedQuery,\n companionRows,\n companions: companionMeta,\n companionInputs,\n ignoredScalarHints,\n } = this.#resolveScalarSubqueries(query);\n builtInputs = [...companionInputs];\n\n this.#warnIgnoredScalarHints(queryID, ignoredScalarHints);\n\n const input = buildPipeline(\n resolvedQuery,\n {\n debug: debugDelegate,\n enableNotExists: true, // Server-side can handle NOT EXISTS\n disableCorrelatedPredicatePushdown:\n this.#disableCorrelatedPredicatePushdown(),\n enablePlannerAwarePushdown:\n this.#config?.enablePlannerAwarePushdown !== false,\n getSource: name => this.#getSource(name),\n createStorage: () => this.#createStorage(),\n decorateSourceInput: (input: SourceInput, _queryID: string): Input =>\n new MeasurePushOperator(\n new QueryFailureLoggingOperator(\n this.#lc,\n input,\n queryID,\n transformationHash,\n queryName,\n ),\n queryID,\n this.#metricsDelegate,\n 'query-update-server',\n ),\n decorateInput: input => input,\n addEdge() {},\n decorateFilterInput: input => input,\n planWarnings: planWarningThresholds && {\n thresholds: planWarningThresholds,\n report: warnings => {\n planWarnings = warnings;\n },\n },\n },\n queryID,\n costModel,\n );\n builtInputs.push(input);\n if (planWarnings.length > 0) {\n this.#logPlanWarnings(\n shape,\n {queryHash: queryID, transformationHash, queryName},\n planWarnings,\n );\n }\n const schema = input.getSchema();\n input.setOutput({\n push: change => this.#streamPushed(queryID, schema, change),\n });\n\n for (const change of must(this.#streamer).streamNodes(\n queryID,\n schema,\n ChangeType.ADD,\n input.fetch({}),\n )) {\n if (change !== 'yield') {\n hydrationRowCount++;\n }\n yield change;\n }\n\n for (const {table, row} of companionRows) {\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, table);\n hydrationRowCount++;\n yield {\n type: ChangeType.ADD,\n queryID,\n table,\n rowKey: getRowKey(primaryKey, row),\n row,\n } as RowChange;\n }\n\n const hydrationTimeMs = timer.totalElapsed();\n const hydrationRowsRead = this.#totalRowsRead() - rowsReadAtStart;\n if (runtimeDebugFlags.trackRowCountsVended) {\n if (hydrationTimeMs > this.#logConfig.slowHydrateThreshold) {\n let totalRowsConsidered = 0;\n const lc = this.#lc\n .withContext('queryID', queryID)\n .withContext('hydrationTimeMs', hydrationTimeMs);\n for (const tableName of this.#tables.keys()) {\n const entries = Object.entries(\n debugDelegate?.getVendedRowCounts()[tableName] ?? {},\n );\n totalRowsConsidered += entries.reduce(\n (acc, entry) => acc + entry[1],\n 0,\n );\n lc.info?.(tableName + ' VENDED: ', entries);\n }\n lc.info?.(`Total rows considered: ${totalRowsConsidered}`);\n }\n }\n debugDelegate?.reset();\n\n // Set up live companion pipelines for reactive scalar subquery monitoring.\n const liveCompanions: CompanionPipeline[] = [];\n for (let i = 0; i < companionMeta.length; i++) {\n const meta = companionMeta[i];\n const companionInput = companionInputs[i];\n const companionSchema = companionInput.getSchema();\n const {childField, resolvedValue} = meta;\n companionInput.setOutput({\n push: (change: Change) => {\n let newValue: LiteralValue | null | undefined;\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.EDIT:\n newValue =\n (change[ChangeIndex.NODE].row[childField] as LiteralValue) ??\n null;\n break;\n case ChangeType.REMOVE:\n newValue = undefined;\n break;\n case ChangeType.CHILD:\n return [];\n }\n if (!scalarValuesEqual(newValue, resolvedValue)) {\n throw new ResetPipelinesSignal(\n `Scalar subquery value changed for ${meta.ast.table}: ` +\n `${String(resolvedValue)} -> ${String(newValue)}`,\n 'scalar-subquery',\n );\n }\n return this.#streamPushed(queryID, companionSchema, change);\n },\n });\n liveCompanions.push({input: companionInput, childField, resolvedValue});\n }\n\n // Note: This hydrationTime is a wall-clock overestimate, as it does\n // not take time slicing into account. The view-syncer resets this\n // to a more precise processing-time measurement with setHydrationTime().\n const pipelineReadyAtMs = Date.now();\n this.#pipelines.set(queryID, {\n input,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n planWarnings,\n hydrationReason,\n pipelineRunID,\n pipelineReadyAtMs,\n transformedAst: resolvedQuery,\n originalAst: query,\n shape,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n companions: liveCompanions,\n });\n hydrationFinished = true;\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {\n outcome: 'finished',\n timeMs: hydrationTimeMs,\n rowCount: hydrationRowCount,\n rowsRead: hydrationRowsRead,\n planWarnings,\n },\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-finish',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n });\n } catch (e) {\n hydrationFailed = true;\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {outcome: 'failed', timeMs: timer.totalElapsed()},\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-failed',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs: timer.totalElapsed(),\n hydrationRowCount,\n });\n logQueryFailure(\n this.#lc,\n {queryHash: queryID, transformationHash, queryName},\n 'query hydration failed',\n e,\n );\n throw e;\n } finally {\n if (!hydrationFinished && !hydrationFailed) {\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {outcome: 'aborted', timeMs: timer.totalElapsed()},\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-aborted',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs: timer.totalElapsed(),\n hydrationRowCount,\n });\n }\n if (!hydrationFinished) {\n for (const input of builtInputs) {\n input.destroy();\n }\n // Rows may already have been yielded through #trackRowSetSignatures,\n // and rowSetSignature() must not report a signature for a query\n // without an active pipeline.\n this.#rowSetSignatures.delete(queryID);\n }\n this.#hydrateContext = null;\n }\n }\n\n /**\n * Removes the pipeline for the query. This is a no-op if the query\n * was not added.\n */\n removeQuery(\n queryID: string,\n stopReason: PipelineStopReason = 'remove-query',\n ) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, stopReason);\n }\n this.#rowSetSignatures.delete(queryID);\n }\n\n #destroyPipeline(\n queryID: string,\n pipeline: Pipeline,\n stopReason: PipelineStopReason,\n ): void {\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-stop',\n pipelineRunID: pipeline.pipelineRunID,\n queryHash: queryID,\n transformationHash: pipeline.transformationHash,\n queryName: pipeline.queryName,\n hydrationReason: pipeline.hydrationReason,\n stopReason,\n hydrationTimeMs: pipeline.hydrationTimeMs,\n hydrationRowCount: pipeline.hydrationRowCount,\n pipelineLifetimeMs: Date.now() - pipeline.pipelineReadyAtMs,\n });\n pipeline.input.destroy();\n for (const companion of pipeline.companions) {\n companion.input.destroy();\n }\n }\n\n /**\n * Current XOR signature of the row-set attached to `queryID`, or\n * `undefined` if no pipeline for the query is currently active.\n * Maintained incrementally by {@link addQuery} and {@link advance}.\n */\n rowSetSignature(queryID: string): bigint | undefined {\n return this.#rowSetSignatures.get(queryID);\n }\n\n /**\n * Wraps an iterable of RowChanges, XORing each row's unit hash into the\n * query's signature (ADDs and REMOVEs share the same op; EDITs are no-ops).\n * Used to intercept the yield streams from {@link addQuery} and\n * {@link advance}.\n */\n *#trackRowSetSignatures(\n changes: Iterable<RowChange | 'yield'>,\n ): Iterable<RowChange | 'yield'> {\n for (const change of changes) {\n if (change !== 'yield' && change.type !== ChangeType.EDIT) {\n const cur = this.#rowSetSignatures.get(change.queryID) ?? 0n;\n const unit = rowIDSignatureUnit({\n schema: '',\n table: change.table,\n rowKey: change.rowKey as RowKey,\n });\n this.#rowSetSignatures.set(change.queryID, cur ^ unit);\n }\n yield change;\n }\n }\n\n /**\n * Returns the value of the row with the given primary key `pk`,\n * or `undefined` if there is no such row. The pipeline must have been\n * initialized.\n */\n getRow(table: string, pk: RowKey): Row | undefined {\n assert(this.initialized(), 'Not yet initialized');\n const source = must(this.#tables.get(table));\n return source.getRow(pk as Row);\n }\n\n /**\n * Advances to the new head of the database.\n *\n * @param timer The caller-controlled {@link Timer} that will be used to\n * measure the progress of the advancement and abort with a\n * {@link ResetPipelinesSignal} if it is estimated to take longer\n * than a hydration.\n * @return The resulting row changes for all added queries. Note that the\n * `changes` must be iterated over in their entirety in order to\n * advance the database snapshot.\n */\n advance(timer: Timer): {\n version: string;\n numChanges: number;\n changes: Iterable<RowChange | 'yield'>;\n /** Available throughout iteration and after completion or an abort. */\n progress: () => AdvancementProgress;\n } {\n assert(\n this.initialized(),\n 'Pipeline driver must be initialized before advancing',\n );\n const diff = this.#snapshotter.advance(\n this.#tableSpecs,\n this.#allTableNames,\n this.#tables,\n // Sources skip changes that none of this client group's pipelines can\n // observe, so a `prev` they write to diverges from other groups'.\n // #advance() overrides this if it holds the changes in memory.\n 'divergent',\n );\n const {prev, curr, changes} = diff;\n this.#lc.debug?.(\n `advance ${prev.version} => ${curr.version}: ${changes} changes`,\n );\n\n const progress = {changesProcessed: 0};\n return {\n version: curr.version,\n numChanges: changes,\n changes: this.#trackRowSetSignatures(\n this.#advance(diff, timer, changes, progress),\n ),\n progress: () => ({\n changesScanned: diff.changesScanned,\n changesProcessed: progress.changesProcessed,\n changesSkipped: diff.changesSkipped,\n }),\n };\n }\n\n *#advance(\n diff: SnapshotDiff,\n timer: Timer,\n numChanges: number,\n progress: {changesProcessed: number},\n ): Iterable<RowChange | 'yield'> {\n assert(\n this.#hydrateContext === null,\n 'Cannot advance while hydration is in progress',\n );\n const totalHydrationTimeMs = this.totalHydrationTimeMs();\n const advanceContext: AdvanceContext = {\n timer,\n progress,\n diff,\n completedChanges: 0,\n totalHydrationTimeMs,\n numChanges,\n reservedRows: undefined,\n heldBytes: 0,\n currentChangeStartMs: undefined,\n pos: 0,\n currentTable: undefined,\n queryStats: new Map(),\n };\n this.#advanceContext = advanceContext;\n // The reservation is made here rather than in advance(), so that the\n // finally below is guaranteed to release it: a generator that is never\n // started never runs its finally.\n let timedOut = false;\n try {\n advanceContext.reservedRows = this.#reserveDeferredWrites(numChanges);\n const deferWrites = advanceContext.reservedRows !== undefined;\n if (deferWrites) {\n // `prev` is not written, so all of its reads can be shared.\n diff.setPrevWrites('none');\n }\n for (const table of this.#tables.values()) {\n table.setDeferWrites(deferWrites);\n }\n this.#lc.debug?.(\n `starting pipeline advancement of ${numChanges} changes with an ` +\n `advancement time limited based on total hydration time of ` +\n `${totalHydrationTimeMs} ms (${deferWrites ? 'deferred' : 'write-through'}).`,\n );\n for (const {\n table,\n prevValues: probedPrevValues,\n nextValue,\n rowKey,\n } of diff) {\n // The diff counts every raw entry, whereas it only yields observed,\n // non-no-op changes. Match the projection's progress to its raw total,\n // but do not count the current change until its push has completed.\n advanceContext.completedChanges = diff.changesScanned - 1;\n // Advance progress is checked each time a row is fetched\n // from a TableSource during push processing, but some pushes\n // don't read any rows. Check progress here before processing\n // the next change.\n if (this.#shouldAdvanceYieldMaybeAbortAdvance()) {\n yield 'yield';\n }\n const start = timer.totalElapsed();\n advanceContext.currentChangeStartMs = start;\n advanceContext.currentTable = table;\n\n let holds: HeldRows = 'fits';\n let pushCompleted = false;\n try {\n try {\n const tableSource = this.#tables.get(table);\n if (!tableSource) {\n // no pipelines read from this table, so no need to process the change\n pushCompleted = true;\n continue;\n }\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, table);\n if (\n nextValue !== null &&\n !this.#reserveSecondRow(advanceContext, rowKey, primaryKey)\n ) {\n holds = 'rows';\n }\n // The diff probed the `prev` snapshot for the rows this change\n // collides with. If the source is deferring its writes, the\n // earlier changes of this advancement are not in that snapshot,\n // so the probe has to be reconciled against them. A no-op for a\n // write-through source, which has already applied them to `prev`.\n const prevValues = tableSource.reconcilePendingConflicts(\n probedPrevValues,\n nextValue,\n rowKey as Row,\n this.#tableSpecs.get(table)?.tableSpec.uniqueKeys ?? [],\n );\n let editOldRow: Row | undefined = undefined;\n for (const prevValue of prevValues) {\n if (\n nextValue &&\n deepEqual(\n getRowKey(primaryKey, prevValue as Row) as JSONValue,\n getRowKey(primaryKey, nextValue as Row) as JSONValue,\n )\n ) {\n editOldRow = prevValue;\n } else {\n if (nextValue) {\n this.#conflictRowsDeleted.add(1);\n }\n yield* this.#push(\n tableSource,\n makeSourceChangeRemove(prevValue as Row),\n );\n }\n }\n if (nextValue) {\n if (editOldRow) {\n yield* this.#push(\n tableSource,\n makeSourceChangeEdit(nextValue as Row, editOldRow),\n );\n } else {\n yield* this.#push(\n tableSource,\n makeSourceChangeAdd(nextValue as Row),\n );\n }\n }\n pushCompleted = true;\n } finally {\n advanceContext.pos++;\n if (pushCompleted) {\n progress.changesProcessed++;\n advanceContext.completedChanges = diff.changesScanned;\n }\n }\n\n this.#shouldAdvanceYieldMaybeAbortAdvance(false);\n if (holds === 'fits') {\n holds = this.#holdPendingRows(advanceContext);\n }\n } finally {\n advanceContext.currentChangeStartMs = undefined;\n }\n\n const elapsed = timer.totalElapsed() - start;\n this.#advanceTime.recordMs(elapsed, {\n table,\n });\n\n if (holds !== 'fits') {\n // Before the diff reads the next change from `prev`.\n yield* this.#writeThrough(advanceContext, diff, holds);\n }\n }\n\n // Include trailing skips, or an advancement that yielded no changes.\n advanceContext.completedChanges = diff.changesScanned;\n\n // Set the new snapshot on all TableSources.\n const {curr} = diff;\n for (const table of this.#tables.values()) {\n table.setDB(curr.db.db);\n }\n this.#ensureCostModelExistsIfEnabled(curr.db.db);\n this.#lc.debug?.(`Advanced to ${curr.version}`);\n this.#logSlowAdvances(advanceContext);\n } catch (e) {\n if (\n e instanceof ResetPipelinesSignal &&\n e.reason === 'advancement-timeout'\n ) {\n timedOut = true;\n this.#logAdvanceTimeout(advanceContext, e);\n }\n throw e;\n } finally {\n if (advanceContext.reservedRows !== undefined) {\n // An advancement that completed has moved its sources to `curr`,\n // which dropped what they held. One that was abandoned is followed by\n // a reset of the pipelines, but not right away, and what they hold\n // must not outlast its release from the budget.\n for (const table of this.#tables.values()) {\n table.discardPendingChanges();\n }\n }\n this.#releaseDeferredWrites(advanceContext);\n this.#advanceContext = null;\n this.#recordAdvanceStats(advanceContext, timedOut);\n }\n }\n\n /** Accounts the time each query took in an advancement to its shape. */\n #recordAdvanceStats({queryStats}: AdvanceContext, timedOut: boolean): void {\n if (this.#queryStats === undefined) {\n return;\n }\n for (const [queryID, {timeMs, changes}] of queryStats) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline !== undefined) {\n this.#queryStats.recordAdvance(pipeline, {timeMs, changes, timedOut});\n }\n }\n }\n\n /**\n * Decides how the sources apply an advancement's changes: in memory if\n * they fit in the worker's {@link DeferredWritesBudget}, and otherwise\n * written through to the `prev` snapshot. Returns the number of rows\n * reserved if they are held in memory.\n */\n #reserveDeferredWrites(numChanges: number): number | undefined {\n const budget = this.#deferredWrites;\n if (!budget) {\n return undefined;\n }\n // An entry sets or removes one row, and a row that it displaces has an\n // entry of its own. Entries of tables that no pipeline of this group reads\n // are counted too: excluding them would take a scan of the entries (see\n // #reserveSecondRow for the one kind of entry that can need two rows).\n if (budget.tryReserve(numChanges)) {\n return numChanges;\n }\n this.#deferredWritesFallbacks.add(1, {stage: 'start'});\n this.#lc.debug?.(\n `writing through ${numChanges} changes: ${budget.reservedRows} rows ` +\n `of the deferred writes budget are reserved`,\n );\n return undefined;\n }\n\n /**\n * If the change log identifies the rows of a table by a key other than its\n * primary key here, an entry that sets a row can change its primary key,\n * which is two rows to a source: the old and the new. (An entry that removes\n * a row removes the one row its key identifies.) Reserves the second row for\n * such an entry, if the advancement holds its changes in memory, and\n * returns whether it fit.\n */\n #reserveSecondRow(\n advanceContext: AdvanceContext,\n rowKey: RowKey,\n primaryKey: PrimaryKey,\n ): boolean {\n if (\n advanceContext.reservedRows === undefined ||\n isKeyedBy(rowKey, primaryKey)\n ) {\n return true;\n }\n if (!must(this.#deferredWrites).tryReserveMore(1)) {\n return false;\n }\n advanceContext.reservedRows++;\n return true;\n }\n\n /**\n * Writes the changes that an advancement holds in memory through to the\n * `prev` snapshot, and the rest of its changes after them, when they no\n * longer fit (see {@link HeldRows}). The advancement then continues as if\n * it had written through from the start.\n */\n *#writeThrough(\n advanceContext: AdvanceContext,\n diff: SnapshotDiff,\n reason: Exclude<HeldRows, 'fits'>,\n ): Iterable<'yield'> {\n const budget = must(this.#deferredWrites);\n this.#deferredWritesFallbacks.add(1, {stage: 'partway', reason});\n this.#lc.debug?.(\n `writing through (${reason}) at ${advanceContext.pos} of ` +\n `${advanceContext.numChanges} changes, holding ` +\n `${advanceContext.heldBytes} of the ${budget.heldBytes} estimated ` +\n `bytes held on this worker (budget: ${budget.maxBytes})`,\n );\n // `prev` is about to be written, so the reads that its writes can affect\n // can no longer be shared.\n diff.setPrevWrites('divergent');\n for (const source of this.#tables.values()) {\n yield* source.writePendingChanges();\n }\n this.#releaseDeferredWrites(advanceContext);\n }\n\n #releaseDeferredWrites(advanceContext: AdvanceContext) {\n const {reservedRows, heldBytes} = advanceContext;\n if (reservedRows !== undefined) {\n must(this.#deferredWrites).release(reservedRows, heldBytes);\n advanceContext.reservedRows = undefined;\n advanceContext.heldBytes = 0;\n }\n }\n\n #recordAdvancePush(queryID: string, timeMs: number): void {\n const advance = this.#advanceContext;\n if (advance === null) {\n return;\n }\n const stats = getOrInsertComputed(advance.queryStats, queryID, () => ({\n timeMs: 0,\n changes: 0,\n lastPos: -1,\n timeMsByTable: new Map(),\n }));\n stats.timeMs += timeMs;\n if (stats.lastPos !== advance.pos) {\n stats.lastPos = advance.pos;\n stats.changes++;\n }\n const table = advance.currentTable;\n if (table !== undefined) {\n stats.timeMsByTable.set(\n table,\n (stats.timeMsByTable.get(table) ?? 0) + timeMs,\n );\n }\n }\n\n /**\n * The identity of the query of the pipeline for `queryID` and its\n * {@link queryShape}, for logging.\n */\n #queryForLog(queryID: string) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline === undefined) {\n return undefined;\n }\n const {transformationHash, queryName, shape} = pipeline;\n return {\n queryName,\n shape,\n fields: {\n queryHash: queryID,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n },\n };\n }\n\n /** Compute once for an admitted log, without clocks or allocations per change. */\n #advancementLogFields({\n timer,\n diff,\n progress,\n completedChanges,\n queryStats,\n totalHydrationTimeMs,\n }: AdvanceContext): AdvancementLogFields {\n const advancementTimeMs = timer.totalElapsed();\n let queryPushTimeMs = 0;\n for (const stats of queryStats.values()) {\n queryPushTimeMs += stats.timeMs;\n }\n return {\n advancementTimeMs,\n queryPushTimeMs,\n // Time outside measured query output pushes includes upstream query\n // propagation, diff iteration, sources and consumer work/waits. This\n // approximate remainder is not a CPU or non-query-work attribution;\n // clamp it for timer precision/overlap.\n outsideQueryPushTimeMs: Math.max(0, advancementTimeMs - queryPushTimeMs),\n totalHydrationTimeMs,\n changesScanned: diff.changesScanned,\n changesCompleted: completedChanges,\n changesProcessed: progress.changesProcessed,\n changesSkipped: diff.changesSkipped,\n };\n }\n\n /**\n * Logs each query whose pushes took longer than the slow advance\n * threshold, at most once per query shape per\n * {@link SLOW_ADVANCE_LOG_WINDOW_MS}.\n */\n #logSlowAdvances(context: AdvanceContext): void {\n if (!this.#lc.warn) {\n return;\n }\n const {queryStats, numChanges} = context;\n let advancementFields: AdvancementLogFields | undefined;\n for (const [queryID, stats] of queryStats) {\n if (stats.timeMs <= this.#logConfig.slowAdvanceThreshold) {\n continue;\n }\n const query = this.#queryForLog(queryID);\n if (query === undefined) {\n continue;\n }\n const suppressed = slowAdvanceLogThrottle.admit(\n `${query.queryName ?? ''}:${query.shape.hash}`,\n );\n if (suppressed === undefined) {\n continue;\n }\n this.#lc.warn(\n `Slow query advancement${query.queryName === undefined ? '' : ` for ${query.queryName}`}: ` +\n `${Math.round(stats.timeMs)} ms for ${stats.changes} of ${numChanges} changes`,\n {\n zeroEvent: 'query-slow-advance',\n ...query.fields,\n advanceTimeMs: stats.timeMs,\n changes: stats.changes,\n advancementChanges: numChanges,\n ...(advancementFields ??= this.#advancementLogFields(context)),\n timeMsByTable: Object.fromEntries(stats.timeMsByTable),\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n zql: query.shape.zql,\n },\n );\n }\n }\n\n /**\n * Logs the queries that took the most time in an advancement that timed\n * out, since the reset that follows would otherwise not say what was slow.\n * Throttled per query shape of the slowest query, with a shared fallback\n * when the timeout happens before any query push timings are recorded.\n */\n #logAdvanceTimeout(\n context: AdvanceContext,\n reset: ResetPipelinesSignal,\n ): void {\n if (!this.#lc.warn) {\n return;\n }\n const {queryStats, numChanges} = context;\n const slowest = [...queryStats]\n .toSorted(([, a], [, b]) => b.timeMs - a.timeMs)\n .slice(0, ADVANCE_TIMEOUT_LOG_MAX_QUERIES)\n .flatMap(([queryID, stats]) => {\n const query = this.#queryForLog(queryID);\n return query ? [{query, stats}] : [];\n });\n const leadingQuery = slowest[0]?.query;\n const suppressed = slowAdvanceLogThrottle.admit(\n leadingQuery\n ? `timeout:${leadingQuery.queryName ?? ''}:${leadingQuery.shape.hash}`\n : 'timeout:outside-query-push',\n );\n if (suppressed === undefined) {\n return;\n }\n const summary = slowest\n .map(\n ({query, stats}) =>\n `${query.queryName ?? query.fields.queryHash} (${Math.round(stats.timeMs)} ms)`,\n )\n .join(', ');\n this.#lc.warn(\n `Advancement of ${numChanges} changes timed out. ` +\n (summary\n ? `The queries that took the most time: ${summary}`\n : 'No query push timings were recorded.'),\n {\n zeroEvent: 'query-advance-timeout',\n // Says where the advancement was when it timed out.\n reason: reset.message,\n advancementChanges: numChanges,\n ...this.#advancementLogFields(context),\n queries: slowest.map(({query, stats}) => ({\n ...query.fields,\n advanceTimeMs: stats.timeMs,\n changes: stats.changes,\n zql: query.shape.zql,\n })),\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n },\n );\n }\n\n /** Implements `BuilderDelegate.getSource()` */\n #getSource(tableName: string): Source {\n return getOrInsertComputed(this.#tables, tableName, tableName => {\n const tableSpec = mustGetTableSpec(this.#tableSpecs, tableName);\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, tableName);\n\n const {db} = this.#snapshotter.current();\n const source = new TableSource(\n this.#lc,\n this.#logConfig,\n db.db,\n tableName,\n tableSpec.zqlSpec,\n primaryKey,\n () => this.#shouldYield(),\n // Pipelines only read tables through their connections, and the\n // sources are moved to the next snapshot after every advancement.\n // How writes are applied is set per advancement, by #advance().\n {skipUnobservableChanges: true},\n );\n this.#lc.debug?.(`created TableSource for ${tableName}`);\n return source;\n });\n }\n\n #shouldYield(): boolean {\n if (this.#hydrateContext) {\n return this.#hydrateContext.timer.elapsedLap() > this.#yieldThresholdMs();\n }\n if (this.#advanceContext) {\n return this.#shouldAdvanceYieldMaybeAbortAdvance();\n }\n throw new Error('shouldYield called outside of hydration or advancement');\n }\n\n /**\n * Cancel advancement processing when either the whole batch projects to be\n * more expensive than hydration, or the current source change alone exceeds\n * the hydration budget. The late-finish exception only applies to batch-level\n * checks; a single pathological push always resets.\n */\n #shouldAdvanceYieldMaybeAbortAdvance(checkYield = true): boolean {\n const {\n currentChangeStartMs,\n completedChanges,\n numChanges,\n timer: advanceTimer,\n totalHydrationTimeMs,\n } = must(this.#advanceContext);\n const elapsed = advanceTimer.totalElapsed();\n const currentChangeElapsedMs =\n currentChangeStartMs === undefined\n ? undefined\n : elapsed - currentChangeStartMs;\n if (\n currentChangeElapsedMs !== undefined &&\n shouldResetSlowCurrentChange(currentChangeElapsedMs, totalHydrationTimeMs)\n ) {\n this.#throwSlowCurrentChangeReset(\n completedChanges,\n numChanges,\n elapsed,\n currentChangeElapsedMs,\n totalHydrationTimeMs,\n );\n }\n const projectedTotalTimeMs = projectedAdvancementTimeMs(\n elapsed,\n completedChanges,\n numChanges,\n );\n const shouldFinish = shouldFinishLateAdvancement(\n completedChanges,\n numChanges,\n );\n if (\n !shouldFinish &&\n shouldResetProjectedAdvancement(\n elapsed,\n projectedTotalTimeMs,\n completedChanges,\n numChanges,\n totalHydrationTimeMs,\n )\n ) {\n this.#throwProjectedAdvancementReset(\n completedChanges,\n numChanges,\n elapsed,\n projectedTotalTimeMs,\n totalHydrationTimeMs,\n );\n }\n if (\n !shouldFinish &&\n elapsed > MIN_ADVANCEMENT_TIME_LIMIT_MS &&\n elapsed > totalHydrationTimeMs / 2 &&\n // As above, only the rest of the advancement is saved by a reset.\n (projectedTotalTimeMs === undefined ||\n projectedTotalTimeMs - elapsed > totalHydrationTimeMs)\n ) {\n throw new ResetPipelinesSignal(\n `Advancement exceeded timeout at ${completedChanges} of ${numChanges} changes ` +\n `after ${elapsed} ms. Advancement time limited based on total ` +\n `hydration time of ${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n return checkYield && advanceTimer.elapsedLap() > this.#yieldThresholdMs();\n }\n\n /**\n * The rows an advancement holds in memory are bounded by its reservation,\n * but their width is not known in advance. So the bytes they are estimated\n * to hold are added to those held by the other advancements on this worker.\n * Says whether that takes them past the budget, or if the rows are not\n * bounded by the reservation after all.\n */\n #holdPendingRows(advanceContext: AdvanceContext): HeldRows {\n const {reservedRows, heldBytes, pos, numChanges} = advanceContext;\n if (reservedRows === undefined) {\n return 'fits';\n }\n const budget = must(this.#deferredWrites);\n const rows = this.pendingRows;\n const bytes = this.pendingBytes;\n // Recorded first, so that the bytes are released even if they do not fit.\n advanceContext.heldBytes = bytes;\n const bytesFit = budget.holdBytes(bytes - heldBytes);\n if (rows > reservedRows) {\n // The reservation is supposed to bound the rows held (see\n // DeferredWritesBudget), so this is a bug, but not one to crash on.\n budget.recordRowOverrun();\n this.#lc.error?.(\n `Advancement holds ${rows} rows at ${pos} of ${numChanges} changes, ` +\n `more than the ${reservedRows} it reserved. Writing through the rest.`,\n );\n return 'row-overrun';\n }\n return bytesFit ? 'fits' : 'bytes';\n }\n\n #throwSlowCurrentChangeReset(\n pos: number,\n numChanges: number,\n elapsed: number,\n currentChangeElapsedMs: number,\n totalHydrationTimeMs: number,\n ): never {\n throw new ResetPipelinesSignal(\n `Advancement exceeded timeout processing current change at ${pos} of ` +\n `${numChanges} changes after ${currentChangeElapsedMs} ms ` +\n `(${elapsed} ms total). Advancement time limited based on total ` +\n `hydration time of ${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n\n #throwProjectedAdvancementReset(\n pos: number,\n numChanges: number,\n elapsed: number,\n projectedTotalTimeMs: number | undefined,\n totalHydrationTimeMs: number,\n ): never {\n const projection =\n projectedTotalTimeMs === undefined\n ? ''\n : ` Projected total advancement time is ${projectedTotalTimeMs} ms.`;\n throw new ResetPipelinesSignal(\n `Advancement projected to exceed hydration time at ${pos} of ` +\n `${numChanges} changes after ${elapsed} ms.` +\n projection +\n ` Advancement time limited based on total hydration time of ` +\n `${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n\n /** Implements `BuilderDelegate.createStorage()` */\n #createStorage(): Storage {\n return this.#storage.createStorage();\n }\n\n *#push(\n source: TableSource,\n change: SourceChange,\n ): Iterable<RowChange | 'yield'> {\n assert(this.#pushedRowChanges === null, 'push already in progress');\n try {\n this.#pushedRowChanges = [];\n for (const val of source.genPush(change)) {\n if (val === 'yield') {\n yield 'yield';\n }\n const rowChanges = this.#pushedRowChanges;\n this.#pushedRowChanges = [];\n yield* rowChanges;\n }\n } finally {\n this.#pushedRowChanges = null;\n }\n }\n\n /**\n * Converts a change pushed out of a query pipeline into row changes during\n * the push. The relationships of a pushed node are lazy, and operators such\n * as Join compute them from the state of the push in progress: while a child\n * change is pushed to each matching parent in turn, parents not yet pushed\n * must not see it. Read after the push has moved on to the next parent, or\n * after it has finished, a node's relationships show the later state, and\n * rows that are also pushed separately are counted twice.\n */\n *#streamPushed(\n queryID: string,\n schema: SourceSchema,\n change: Change,\n ): Stream<'yield'> {\n assert(this.#pushedRowChanges, 'changes are only pushed by #push');\n for (const rowChange of must(this.#streamer).streamChange(\n queryID,\n schema,\n change,\n )) {\n if (rowChange === 'yield') {\n yield rowChange;\n continue;\n }\n // #push starts a new list after each 'yield', so add to the current one.\n must(this.#pushedRowChanges).push(rowChange);\n }\n }\n}\n\n/** Turns the output of query pipelines into the row changes sent to clients. */\nclass Streamer {\n readonly #primaryKeys: Map<string, PrimaryKey>;\n readonly #tableSpecs: Map<string, LiteAndZqlSpec>;\n\n constructor(\n primaryKeys: Map<string, PrimaryKey>,\n tableSpecs: Map<string, LiteAndZqlSpec>,\n ) {\n this.#primaryKeys = primaryKeys;\n this.#tableSpecs = tableSpecs;\n }\n\n /** The row changes for a change pushed out of the pipeline of `queryID`. */\n *streamChange(\n queryID: string,\n schema: SourceSchema,\n change: Change,\n ): Iterable<RowChange | 'yield'> {\n // We do not sync rows gathered by the permissions\n // system to the client.\n if (schema.system === 'permissions') {\n return;\n }\n\n const type = change[ChangeIndex.TYPE];\n switch (type) {\n case ChangeType.REMOVE:\n case ChangeType.ADD:\n yield* this.streamNodes(queryID, schema, type, [\n change[ChangeIndex.NODE],\n ]);\n break;\n case ChangeType.CHILD: {\n const child = change[ChangeIndex.CHILD_DATA];\n yield* this.streamChange(\n queryID,\n must(schema.relationships[child.relationshipName]),\n child.change,\n );\n break;\n }\n case ChangeType.EDIT:\n yield* this.streamNodes(queryID, schema, type, [\n {row: change[ChangeIndex.NODE].row, relationships: {}},\n ]);\n break;\n default:\n unreachable(change[ChangeIndex.TYPE]);\n }\n }\n\n /** A row change of type `op` for each of `nodes` and their descendants. */\n *streamNodes(\n queryID: string,\n schema: SourceSchema,\n op: ChangeType.ADD | ChangeType.REMOVE | ChangeType.EDIT,\n nodes: Iterable<Node | 'yield'>,\n ): Iterable<RowChange | 'yield'> {\n const {tableName: table, system} = schema;\n\n // We do not sync rows gathered by the permissions\n // system to the client.\n if (system === 'permissions') {\n return;\n }\n\n const primaryKey = must(this.#primaryKeys.get(table));\n const spec = must(this.#tableSpecs.get(table)).tableSpec;\n\n for (const node of nodes) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n const {relationships} = node;\n let {row} = node;\n const rowKey = getRowKey(primaryKey, row);\n if (op !== ChangeType.REMOVE) {\n const rowVersion = row[ZERO_VERSION_COLUMN_NAME];\n if (\n typeof rowVersion === 'string' &&\n rowVersion < (spec.minRowVersion ?? '00')\n ) {\n row = {...row, [ZERO_VERSION_COLUMN_NAME]: spec.minRowVersion};\n }\n }\n\n yield {\n type: op,\n queryID,\n table,\n rowKey,\n row: op === ChangeType.REMOVE ? undefined : row,\n } as RowChange;\n\n for (const relationship of Object.keys(relationships)) {\n const childSchema = must(schema.relationships[relationship]);\n // Permissions rows are not synced, so their relationship is not read.\n if (childSchema.system !== 'permissions') {\n yield* this.streamNodes(\n queryID,\n childSchema,\n op,\n relationships[relationship](),\n );\n }\n }\n }\n }\n}\n\nclass QueryFailureLoggingOperator implements Input, Output {\n readonly #lc: LogContext;\n readonly #input: Input;\n readonly #queryHash: string;\n readonly #transformationHash: string;\n readonly #queryName: string | undefined;\n #output: Output = throwOutput;\n\n constructor(\n lc: LogContext,\n input: Input,\n queryHash: string,\n transformationHash: string,\n queryName?: string,\n ) {\n this.#lc = lc;\n this.#input = input;\n this.#queryHash = queryHash;\n this.#transformationHash = transformationHash;\n this.#queryName = queryName;\n input.setOutput(this);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n fetch(req: FetchRequest): Iterable<Node | 'yield'> {\n return this.#input.fetch(req);\n }\n\n *push(change: Change): Iterable<'yield'> {\n try {\n yield* this.#output.push(change, this);\n } catch (e) {\n logQueryFailure(\n this.#lc,\n {\n queryHash: this.#queryHash,\n transformationHash: this.#transformationHash,\n queryName: this.#queryName,\n },\n 'query pipeline failed',\n e,\n );\n throw e;\n }\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n try {\n yield* this.#output.reconcile(this);\n } catch (e) {\n logQueryFailure(\n this.#lc,\n {\n queryHash: this.#queryHash,\n transformationHash: this.#transformationHash,\n queryName: this.#queryName,\n },\n 'query pipeline failed during reconcile',\n e,\n );\n throw e;\n }\n }\n }\n}\n\nfunction logQueryFailure(\n lc: LogContext,\n queryInfo: QueryLogInfo | undefined,\n message: string,\n error: unknown,\n): void {\n if (error instanceof ResetPipelinesSignal) {\n return;\n }\n let queryLC = lc;\n if (queryInfo) {\n queryLC = queryLC\n .withContext('queryHash', queryInfo.queryHash)\n .withContext('transformationHash', queryInfo.transformationHash);\n if (queryInfo.queryName !== undefined) {\n queryLC = queryLC.withContext('queryName', queryInfo.queryName);\n }\n }\n queryLC.error?.(message, error);\n}\n\nfunction getRowKey(cols: PrimaryKey, row: Row): RowKey {\n const rowKey: Writable<RowKey> = {};\n for (const col of cols) {\n // Not `rowKey[col] = ...`: for a column named `__proto__` that would call\n // the prototype setter instead of adding the key.\n assignProperty(rowKey, col, must(row[col]));\n }\n return rowKey;\n}\n\n/** Whether `rowKey` has exactly the columns of `primaryKey`. */\nfunction isKeyedBy(rowKey: RowKey, primaryKey: PrimaryKey): boolean {\n let cols = 0;\n for (const col in rowKey) {\n if (!primaryKey.includes(col)) {\n return false;\n }\n cols++;\n }\n return cols === primaryKey.length;\n}\n\n/**\n * Core hydration logic used by {@link PipelineDriver#addQuery}, extracted to a\n * function for reuse by the analyze-query RPC path so that analysis hydrates\n * queries the same way the view-syncer does in production.\n */\nexport function hydrate(\n input: Input,\n hash: string,\n clientSchema: ClientSchema,\n tableSpecs: Map<string, LiteAndZqlSpec>,\n): Iterable<RowChange | 'yield'> {\n return new Streamer(buildPrimaryKeys(clientSchema), tableSpecs).streamNodes(\n hash,\n input.getSchema(),\n ChangeType.ADD,\n input.fetch({}),\n );\n}\n\nfunction buildPrimaryKeys(\n clientSchema: ClientSchema,\n primaryKeys: Map<string, PrimaryKey> = new Map<string, PrimaryKey>(),\n) {\n for (const [tableName, {primaryKey}] of Object.entries(clientSchema.tables)) {\n primaryKeys.set(tableName, primaryKey as unknown as PrimaryKey);\n }\n return primaryKeys;\n}\n\nfunction mustGetPrimaryKey(\n primaryKeys: Map<string, PrimaryKey> | null,\n table: string,\n): PrimaryKey {\n const pKeys = must(primaryKeys, 'primaryKey map must be non-null');\n\n const rv = pKeys.get(table);\n assert(\n rv,\n () =>\n // oxlint-disable-next-line e18e/prefer-array-to-sorted\n `table '${table}' is not one of: ${JSON.stringify([...pKeys.keys()].sort())}. ` +\n `Check the spelling and ensure that the table has a primary key.`,\n );\n return rv;\n}\n\n/**\n * Compares two scalar subquery resolved values for equality.\n * Unlike `valuesEqual` in data.ts (which treats null != null for join\n * semantics), this uses identity semantics: undefined === undefined\n * (no row matched), null === null (row matched but field was NULL).\n */\nfunction scalarValuesEqual(\n a: LiteralValue | null | undefined,\n b: LiteralValue | null | undefined,\n): boolean {\n return a === b;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsPA,IAAM,kCAAkC;;;;;AAexC,IAAM,gCAAgC;AACtC,IAAM,2CAA2C;AACjD,IAAM,wCAAwC;AAC9C,IAAM,2CAA2C;AACjD,IAAM,sCAAsC;AAC5C,IAAM,oCAAoC;AAC1C,IAAM,yCAAyC;AAC/C,IAAM,mCAAmC;AAYzC,IAAM,yBAAyB,IAAI,YAAY,EAC7C,UALiC,KAAK,IAMxC,CAAC;AAQD,IAAM,yBAAyB,IAAI,YAAY,EAC7C,UAHiC,IAAI,IAIvC,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,UAAmC;CAEhE,OAAO;AACT;AAEA,SAAS,kBACP,aACA,OACY;CACZ,MAAM,QAAQ,KAAK,aAAa,iCAAiC;CAEjE,MAAM,KAAK,MAAM,IAAI,KAAK;CAC1B,OACE,UAGE,UAAU,MAAM,mBAAmB,KAAK,UAAU,CAAC,GAAG,MAAM,KAAK,CAAC,EAAE,KAAK,CAAC,EAAE,kEAEhF;CACA,OAAO;AACT;;;;;;;AAQA,SAAS,kBACP,GACA,GACS;CACT,OAAO,MAAM;AACf"}
1
+ {"version":3,"file":"pipeline-driver.js","names":["#tables","#pipelines","#rowSetSignatures","#lc","#snapshotter","#storage","#shardID","#logConfig","#config","#deferredWrites","#tableSpecs","#allTableNames","#costModels","#planWarningThresholds","#yieldThresholdMs","#advanceTime","#conflictRowsDeleted","#deferredWritesFallbacks","#inspectorDelegate","#queryStats","#metricsDelegate","#recordAdvancePush","#initAndResetCommon","#destroyPipeline","#primaryKeys","#streamer","#replicaVersion","#permissions","#disableCorrelatedPredicatePushdown","#getSource","#createStorage","#trackRowSetSignatures","#addQueryImpl","#logQueryPipelineLifecycle","#ensureCostModelExistsIfEnabled","#advanceContext","#hydrateContext","#totalRowsRead","#resolveScalarSubqueries","#warnIgnoredScalarHints","#logPlanWarnings","#streamPushed","#advance","#reserveDeferredWrites","#shouldAdvanceYieldMaybeAbortAdvance","#reserveSecondRow","#push","#holdPendingRows","#writeThrough","#logSlowAdvances","#logAdvanceTimeout","#releaseDeferredWrites","#recordAdvanceStats","#queryForLog","#advancementLogFields","#shouldYield","#throwSlowCurrentChangeReset","#throwProjectedAdvancementReset","#pushedRowChanges","#input","#queryHash","#transformationHash","#queryName","#output"],"sources":["../../../../../../zero-cache/src/services/view-syncer/pipeline-driver.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert, unreachable} from '../../../../shared/src/asserts.ts';\nimport {deepEqual, type JSONValue} from '../../../../shared/src/json.ts';\nimport {getOrInsertComputed} from '../../../../shared/src/map.ts';\nimport {must} from '../../../../shared/src/must.ts';\nimport {assignProperty} from '../../../../shared/src/objects.ts';\nimport {randInt} from '../../../../shared/src/rand.ts';\nimport type {Writable} from '../../../../shared/src/writable.ts';\nimport type {AST, LiteralValue} from '../../../../zero-protocol/src/ast.ts';\nimport type {ClientSchema} from '../../../../zero-protocol/src/client-schema.ts';\nimport type {Row} from '../../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../../zero-protocol/src/primary-key.ts';\nimport {buildPipeline} from '../../../../zql/src/builder/builder.ts';\nimport {\n Debug,\n runtimeDebugFlags,\n} from '../../../../zql/src/builder/debug-delegate.ts';\nimport {ChangeIndex} from '../../../../zql/src/ivm/change-index.ts';\nimport {ChangeType} from '../../../../zql/src/ivm/change-type.ts';\nimport type {Change} from '../../../../zql/src/ivm/change.ts';\nimport type {Node} from '../../../../zql/src/ivm/data.ts';\nimport {\n skipYields,\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Output,\n type Storage,\n} from '../../../../zql/src/ivm/operator.ts';\nimport type {SourceSchema} from '../../../../zql/src/ivm/schema.ts';\nimport {\n type Source,\n type SourceChange,\n type SourceInput,\n makeSourceChangeAdd,\n makeSourceChangeEdit,\n makeSourceChangeRemove,\n} from '../../../../zql/src/ivm/source.ts';\nimport type {Stream} from '../../../../zql/src/ivm/stream.ts';\nimport type {ConnectionCostModel} from '../../../../zql/src/planner/planner-connection.ts';\nimport type {\n PlanWarning,\n PlanWarningThresholds,\n} from '../../../../zql/src/planner/planner-warnings.ts';\nimport {MeasurePushOperator} from '../../../../zql/src/query/measure-push-operator.ts';\nimport type {\n MetricMap,\n MetricsDelegate,\n} from '../../../../zql/src/query/metrics-delegate.ts';\nimport type {ClientGroupStorage} from '../../../../zqlite/src/database-storage.ts';\nimport type {Database} from '../../../../zqlite/src/db.ts';\nimport {\n resolveSimpleScalarSubqueries,\n type CompanionSubquery,\n type IgnoredScalarHint,\n} from '../../../../zqlite/src/resolve-scalar-subqueries.ts';\nimport {createSQLiteCostModel} from '../../../../zqlite/src/sqlite-cost-model.ts';\nimport {TableSource} from '../../../../zqlite/src/table-source.ts';\nimport {\n reloadPermissionsIfChanged,\n type LoadedPermissions,\n} from '../../auth/load-permissions.ts';\nimport type {LogConfig, ZeroConfig} from '../../config/zero-config.ts';\nimport {computeZqlSpecs, mustGetTableSpec} from '../../db/lite-tables.ts';\nimport type {LiteAndZqlSpec, LiteTableSpec} from '../../db/specs.ts';\nimport {LogThrottle} from '../../observability/log-throttle.ts';\nimport {\n getOrCreateCounter,\n getOrCreateLatencyHistogram,\n} from '../../observability/metrics.ts';\nimport type {InspectorDelegate} from '../../server/inspector-delegate.ts';\nimport {type RowKey} from '../../types/row-key.ts';\nimport {type ShardID} from '../../types/shards.ts';\nimport {\n getSubscriptionState,\n ZERO_VERSION_COLUMN_NAME,\n} from '../replicator/schema/replication-state.ts';\nimport {checkClientSchema} from './client-schema.ts';\nimport type {DeferredWritesBudget} from './deferred-writes-budget.ts';\nimport {planWarningMessage} from './plan-warnings.ts';\nimport {queryShape, type QueryShape} from './query-shape.ts';\nimport type {QueryStats} from './query-stats.ts';\nimport {rowIDSignatureUnit} from './row-set-signature.ts';\nimport type {Snapshotter} from './snapshotter.ts';\nimport {ResetPipelinesSignal, type SnapshotDiff} from './snapshotter.ts';\n\ntype RowOp<Op extends Omit<ChangeType, ChangeType.CHILD>> = {\n readonly type: Op;\n readonly queryID: string;\n readonly table: string;\n readonly rowKey: Row;\n readonly row: Row;\n};\n\nexport type RowAdd = RowOp<ChangeType.ADD>;\n\nexport type RowRemove = RowOp<ChangeType.REMOVE>;\n\nexport type RowEdit = RowOp<ChangeType.EDIT>;\n\nexport type RowChange = RowAdd | RowRemove | RowEdit;\n\ntype CompanionPipeline = {\n readonly input: Input;\n readonly childField: string;\n readonly resolvedValue: LiteralValue | null | undefined;\n};\n\ntype Pipeline = {\n readonly input: Input;\n readonly hydrationTimeMs: number;\n readonly hydrationRowCount: number;\n readonly hydrationRowsRead: number;\n readonly planWarnings: readonly PlanWarning[];\n readonly hydrationReason: PipelineHydrationReason;\n readonly pipelineRunID: string;\n readonly pipelineReadyAtMs: number;\n readonly transformedAst: AST;\n readonly originalAst: AST;\n /** The {@link queryShape} of {@link originalAst}. */\n readonly shape: QueryShape;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n readonly companions: readonly CompanionPipeline[];\n};\n\nexport type QueryInfo = {\n readonly transformedAst: AST;\n readonly originalAst?: AST | undefined;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n};\n\nexport type HydrationStats = {\n /** The rows the hydration output. */\n readonly rowCount: number;\n /**\n * The rows the hydration read from the replica, including rows that were\n * then filtered out, e.g. by a filter that could not be pushed to SQLite or\n * by an EXISTS that did not match. Much greater than {@link rowCount} means\n * the query does a lot of work for the rows it returns.\n */\n readonly rowsRead: number;\n /** What the planner warned about the plan it chose for the query. */\n readonly planWarnings: readonly PlanWarning[];\n /** The {@link queryShape} of the query, computed when it was hydrated. */\n readonly shape: QueryShape;\n};\n\ntype QueryLogInfo = {\n readonly queryHash: string;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n};\n\ntype QueryPipelineLifecycleEvent =\n | 'query-pipeline-hydrate-start'\n | 'query-pipeline-hydrate-finish'\n | 'query-pipeline-hydrate-failed'\n | 'query-pipeline-hydrate-aborted'\n | 'query-pipeline-stop';\n\nexport type PipelineHydrationReason =\n | 'query-set-sync'\n | 'unchanged-query-rehydrate';\n\ntype PipelineStopReason =\n | 'replace-query'\n | 'remove-query'\n | 'reset'\n | 'destroy';\n\ntype QueryPipelineLifecycleLog = {\n readonly zeroEvent: QueryPipelineLifecycleEvent;\n readonly pipelineRunID: string;\n readonly queryHash: string;\n readonly transformationHash: string;\n readonly queryName?: string | undefined;\n readonly hydrationReason?: PipelineHydrationReason | undefined;\n readonly stopReason?: PipelineStopReason | undefined;\n readonly hydrationTimeMs?: number | undefined;\n readonly hydrationRowCount?: number | undefined;\n readonly hydrationRowsRead?: number | undefined;\n readonly pipelineLifetimeMs?: number | undefined;\n};\n\nexport type AdvancementProgress = {\n /** Raw ChangeLog entries read, including skipped entries and the current change. */\n readonly changesScanned: number;\n /** Yielded source changes whose push processing completed. */\n readonly changesProcessed: number;\n /** Raw entries omitted by the snapshot diff. */\n readonly changesSkipped: number;\n};\n\ntype AdvancementLogFields = AdvancementProgress & {\n readonly advancementTimeMs: number;\n readonly queryPushTimeMs: number;\n readonly outsideQueryPushTimeMs: number;\n readonly totalHydrationTimeMs: number;\n readonly changesCompleted: number;\n};\n\ntype AdvanceContext = {\n readonly timer: Timer;\n readonly progress: {changesProcessed: number};\n readonly diff: SnapshotDiff;\n /** Raw entries completed, including skipped entries but excluding an in-flight push. */\n completedChanges: number;\n readonly totalHydrationTimeMs: number;\n readonly numChanges: number;\n /**\n * The rows reserved from the {@link DeferredWritesBudget} while the\n * advancement's changes are held in memory.\n */\n reservedRows: number | undefined;\n /** The bytes the advancement has added to the budget's held bytes. */\n heldBytes: number;\n currentChangeStartMs: number | undefined;\n pos: number;\n /** The table of the change being pushed. */\n currentTable: string | undefined;\n /** The processing time of each query's pushes, by query ID. */\n readonly queryStats: Map<string, QueryAdvanceStats>;\n};\n\n/**\n * Whether the rows an advancement holds in memory fit in the budget, or why\n * they do not: the bytes held, a change that needed more rows than were left\n * to reserve, or more rows held than reserved.\n */\ntype HeldRows = 'fits' | 'bytes' | 'rows' | 'row-overrun';\n\ntype QueryAdvanceStats = {\n /** The time spent processing pushes to the query. */\n timeMs: number;\n /** The number of changes pushed to the query. */\n changes: number;\n /** The position of the last change pushed to the query. */\n lastPos: number;\n /** {@link timeMs} by the table of the change pushed. */\n readonly timeMsByTable: Map<string, number>;\n};\n\n/** The most queries a log of an advancement timeout lists. */\nconst ADVANCE_TIMEOUT_LOG_MAX_QUERIES = 3;\n\ntype HydrateContext = {\n readonly timer: Timer;\n};\n\nexport type Timer = {\n elapsedLap: () => number;\n totalElapsed: () => number;\n};\n\n/**\n * No matter how fast hydration is, advancement is given at least this long to\n * complete before doing a pipeline reset.\n */\nconst MIN_ADVANCEMENT_TIME_LIMIT_MS = 50;\nconst MIN_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES = 8;\nconst PROJECTED_ADVANCEMENT_SAMPLE_FRACTION = 0.25;\nconst MAX_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES = 50;\nconst MIN_PROJECTED_ADVANCEMENT_SAMPLE_MS = 5;\nconst MIN_PROJECTED_ADVANCEMENT_CHANGES = 16;\nconst PROJECTED_ADVANCEMENT_RESET_MULTIPLIER = 1.5;\nconst LATE_ADVANCEMENT_FINISH_PROGRESS = 0.8;\n\n/**\n * The planner warns about a query each time it is planned, i.e. for every\n * client group that hydrates it, and the warnings only change when the data\n * does. So they are logged at most once per query shape per this window,\n * per process.\n */\nconst PLAN_WARNING_LOG_WINDOW_MS = 60 * 60_000;\n\n// Shared by all PipelineDrivers in the process, so that a query shape is\n// throttled across client groups.\nconst planWarningLogThrottle = new LogThrottle({\n windowMs: PLAN_WARNING_LOG_WINDOW_MS,\n});\n\n/**\n * Slow advancements of a query shape, and advancement timeouts led by it,\n * are logged at most once per this window per process.\n */\nconst SLOW_ADVANCE_LOG_WINDOW_MS = 5 * 60_000;\n\nconst slowAdvanceLogThrottle = new LogThrottle({\n windowMs: SLOW_ADVANCE_LOG_WINDOW_MS,\n});\n\nfunction randomID() {\n return randInt(1, Number.MAX_SAFE_INTEGER).toString(36);\n}\n\nfunction projectedAdvancementTimeMs(\n elapsedMs: number,\n processedChanges: number,\n numChanges: number,\n): number | undefined {\n if (processedChanges <= 0 || numChanges <= 0) {\n return undefined;\n }\n return (elapsedMs / processedChanges) * numChanges;\n}\n\nfunction advancementResetTimeLimitMs(totalHydrationTimeMs: number): number {\n return Math.max(totalHydrationTimeMs, 1);\n}\n\nfunction minProjectedAdvancementSampleChanges(numChanges: number): number {\n return Math.max(\n MIN_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES,\n Math.min(\n MAX_PROJECTED_ADVANCEMENT_SAMPLE_CHANGES,\n Math.ceil(numChanges * PROJECTED_ADVANCEMENT_SAMPLE_FRACTION),\n ),\n );\n}\n\nfunction canProjectAdvancement(\n elapsedMs: number,\n projectedTotalTimeMs: number | undefined,\n processedChanges: number,\n numChanges: number,\n): boolean {\n return (\n projectedTotalTimeMs !== undefined &&\n numChanges >= MIN_PROJECTED_ADVANCEMENT_CHANGES &&\n processedChanges >= minProjectedAdvancementSampleChanges(numChanges) &&\n elapsedMs >= MIN_PROJECTED_ADVANCEMENT_SAMPLE_MS\n );\n}\n\nfunction shouldResetProjectedAdvancement(\n elapsedMs: number,\n projectedTotalTimeMs: number,\n totalHydrationTimeMs: number,\n): boolean {\n // The time already spent is spent whether or not the advancement is reset,\n // so a reset only saves the rest of it.\n return (\n projectedTotalTimeMs - elapsedMs >\n advancementResetTimeLimitMs(totalHydrationTimeMs) *\n PROJECTED_ADVANCEMENT_RESET_MULTIPLIER\n );\n}\n\nfunction shouldFinishLateAdvancement(\n processedChanges: number,\n numChanges: number,\n): boolean {\n return (\n numChanges > 0 &&\n processedChanges / numChanges >= LATE_ADVANCEMENT_FINISH_PROGRESS\n );\n}\n\nfunction shouldResetSlowCurrentChange(\n currentChangeElapsedMs: number,\n totalHydrationTimeMs: number,\n): boolean {\n return (\n currentChangeElapsedMs > MIN_ADVANCEMENT_TIME_LIMIT_MS &&\n currentChangeElapsedMs > advancementResetTimeLimitMs(totalHydrationTimeMs)\n );\n}\n\n/**\n * Manages the state of IVM pipelines for a given ViewSyncer (i.e. client group).\n */\nexport class PipelineDriver {\n // Retain sources until reset or destroy so getRow can serve catch-up reads.\n readonly #tables = new Map<string, TableSource>();\n // Query id to pipeline\n readonly #pipelines = new Map<string, Pipeline>();\n /**\n * XOR signature of the set of rows currently attached to each active\n * query, maintained as RowChanges are yielded from {@link addQuery} and\n * {@link advance}. ADDs / REMOVEs XOR the row's unit in (XOR is\n * self-inverse, so one op serves both directions); EDITs are no-ops.\n * Hydration implicitly reseeds from `0n` because {@link addQuery} calls\n * {@link removeQuery} first, which deletes the entry.\n */\n readonly #rowSetSignatures = new Map<string, bigint>();\n\n readonly #lc: LogContext;\n readonly #snapshotter: Snapshotter;\n readonly #storage: ClientGroupStorage;\n readonly #shardID: ShardID;\n readonly #logConfig: LogConfig;\n readonly #config: ZeroConfig | undefined;\n readonly #deferredWrites: DeferredWritesBudget | undefined;\n readonly #tableSpecs = new Map<string, LiteAndZqlSpec>();\n readonly #allTableNames = new Set<string>();\n readonly #costModels: WeakMap<Database, ConnectionCostModel> | undefined;\n readonly #planWarningThresholds: PlanWarningThresholds | undefined;\n readonly #yieldThresholdMs: () => number;\n #streamer: Streamer | null = null;\n /** The row changes of the push in progress, until #push yields them. */\n #pushedRowChanges: RowChange[] | null = null;\n #hydrateContext: HydrateContext | null = null;\n #advanceContext: AdvanceContext | null = null;\n #replicaVersion: string | null = null;\n #primaryKeys: Map<string, PrimaryKey> | null = null;\n #permissions: LoadedPermissions | null = null;\n\n readonly #advanceTime = getOrCreateLatencyHistogram(\n 'sync',\n 'ivm.advance-time',\n 'Time to advance all queries for a given client group in response to a single change.',\n );\n\n readonly #conflictRowsDeleted = getOrCreateCounter(\n 'sync',\n 'ivm.conflict-rows-deleted',\n 'Number of rows deleted because they conflicted with added row',\n );\n\n readonly #deferredWritesFallbacks = getOrCreateCounter(\n 'sync',\n 'ivm.deferred-writes-fallbacks',\n 'Number of advancements written through to the replica snapshot because ' +\n 'their changes did not fit in the deferred IVM writes budget, from the ' +\n 'start or partway (because of the bytes held, a change that needed ' +\n 'more rows than were left, or more rows held than reserved)',\n );\n\n readonly #inspectorDelegate: InspectorDelegate;\n readonly #queryStats: QueryStats | undefined;\n\n /**\n * Passes the pipelines' metrics on to the inspector, and accounts the time\n * spent pushing to each query to the advancement in progress.\n */\n readonly #metricsDelegate: MetricsDelegate = {\n addMetric: <K extends keyof MetricMap>(\n metric: K,\n value: number,\n ...args: MetricMap[K]\n ) => {\n this.#inspectorDelegate.addMetric(metric, value, ...args);\n if (metric === 'query-update-server') {\n this.#recordAdvancePush(args[0], value);\n }\n },\n };\n\n constructor(\n lc: LogContext,\n logConfig: LogConfig,\n snapshotter: Snapshotter,\n shardID: ShardID,\n storage: ClientGroupStorage,\n clientGroupID: string,\n inspectorDelegate: InspectorDelegate,\n yieldThresholdMs: () => number,\n enablePlanner?: boolean,\n config?: ZeroConfig,\n deferredWrites?: DeferredWritesBudget,\n queryStats?: QueryStats | undefined,\n ) {\n this.#lc = lc.withContext('clientGroupID', clientGroupID);\n this.#snapshotter = snapshotter;\n this.#storage = storage;\n this.#shardID = shardID;\n this.#logConfig = logConfig;\n this.#config = config;\n this.#deferredWrites = deferredWrites;\n this.#inspectorDelegate = inspectorDelegate;\n this.#costModels = enablePlanner ? new WeakMap() : undefined;\n const planWarningThresholds = {\n rows: logConfig.planWarningRowThreshold,\n cost: logConfig.planWarningCostThreshold,\n };\n this.#planWarningThresholds =\n planWarningThresholds.rows > 0 || planWarningThresholds.cost > 0\n ? planWarningThresholds\n : undefined;\n this.#yieldThresholdMs = yieldThresholdMs;\n this.#queryStats = queryStats;\n }\n\n /**\n * Initializes the PipelineDriver to the current head of the database.\n * Queries can then be added (i.e. hydrated) with {@link addQuery()}.\n *\n * Must only be called once.\n */\n init(clientSchema: ClientSchema) {\n assert(!this.#snapshotter.initialized(), 'Already initialized');\n this.#snapshotter.init();\n this.#initAndResetCommon(clientSchema);\n }\n\n /**\n * @returns Whether the PipelineDriver has been initialized.\n */\n initialized(): boolean {\n return this.#snapshotter.initialized();\n }\n\n /**\n * Clears the current pipelines and TableSources, returning the PipelineDriver\n * to its initial state. This should be called in response to a schema change,\n * as TableSources need to be recomputed.\n */\n reset(clientSchema: ClientSchema) {\n for (const [queryID, pipeline] of this.#pipelines) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, 'reset');\n }\n this.#tables.clear();\n this.#allTableNames.clear();\n this.#rowSetSignatures.clear();\n this.#initAndResetCommon(clientSchema);\n }\n\n #initAndResetCommon(clientSchema: ClientSchema) {\n const {db} = this.#snapshotter.current();\n const fullTables = new Map<string, LiteTableSpec>();\n computeZqlSpecs(\n this.#lc,\n db.db,\n {includeBackfillingColumns: false},\n this.#tableSpecs,\n fullTables,\n );\n checkClientSchema(\n this.#shardID,\n clientSchema,\n this.#tableSpecs,\n fullTables,\n );\n this.#allTableNames.clear();\n for (const table of fullTables.keys()) {\n this.#allTableNames.add(table);\n }\n const primaryKeys = this.#primaryKeys ?? new Map<string, PrimaryKey>();\n this.#primaryKeys = primaryKeys;\n primaryKeys.clear();\n for (const [table, spec] of this.#tableSpecs.entries()) {\n primaryKeys.set(table, spec.tableSpec.primaryKey);\n }\n buildPrimaryKeys(clientSchema, primaryKeys);\n this.#streamer ??= new Streamer(primaryKeys, this.#tableSpecs);\n const {replicaVersion} = getSubscriptionState(db);\n this.#replicaVersion = replicaVersion;\n }\n\n /** @returns The replica version. The PipelineDriver must have been initialized. */\n get replicaVersion(): string {\n return must(this.#replicaVersion, 'Not yet initialized');\n }\n\n /**\n * Returns the current version of the database. This will reflect the\n * latest version change when calling {@link advance()} once the\n * iteration has begun.\n */\n currentVersion(): string {\n assert(this.initialized(), 'Not yet initialized');\n return this.#snapshotter.current().version;\n }\n\n /**\n * Returns the current upstream {app}.permissions, or `null` if none are defined.\n */\n currentPermissions(): LoadedPermissions | null {\n assert(this.initialized(), 'Not yet initialized');\n const res = reloadPermissionsIfChanged(\n this.#lc,\n this.#snapshotter.current().db,\n this.#shardID.appID,\n this.#permissions,\n this.#config,\n );\n if (res.changed) {\n this.#permissions = res.permissions;\n this.#lc.debug?.(\n 'Reloaded permissions',\n JSON.stringify(this.#permissions),\n );\n }\n return this.#permissions;\n }\n\n /**\n * Advances the snapshot to the head of the database without diffing the\n * change log, in preparation for hydrating queries at head.\n *\n * Throws a {@link ResetPipelinesSignal} if the change log records a\n * schema change since the previous snapshot. The table specs (and any\n * TableSources built from them) were computed at or before that snapshot\n * and are stale with respect to the new head, so the caller must\n * {@link reset()} before hydrating. ({@link advance()} detects this when\n * the diff encounters the RESET op; this path skips the diff and so must\n * check explicitly.)\n */\n advanceWithoutDiff(): string {\n const {prev, curr} = this.#snapshotter.advanceWithoutDiff();\n if (curr.schemaChangedSince(prev.version)) {\n throw new ResetPipelinesSignal(\n `schema changed between ${prev.version} and ${curr.version}`,\n 'schema-change',\n );\n }\n for (const table of this.#tables.values()) {\n table.setDB(curr.db.db);\n }\n return curr.version;\n }\n\n #ensureCostModelExistsIfEnabled(db: Database) {\n let existing = this.#costModels?.get(db);\n if (existing) {\n return existing;\n }\n if (this.#costModels) {\n const costModel = createSQLiteCostModel(db, this.#tableSpecs);\n this.#costModels.set(db, costModel);\n return costModel;\n }\n return undefined;\n }\n\n /**\n * Clears storage used for the pipelines. Call this when the\n * PipelineDriver will no longer be used.\n */\n destroy() {\n for (const [queryID, pipeline] of this.#pipelines) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, 'destroy');\n }\n this.#tables.clear();\n this.#rowSetSignatures.clear();\n this.#storage.destroy();\n this.#snapshotter.destroy();\n }\n\n /** @return Map from query ID to PipelineInfo for all added queries. */\n queries(): ReadonlyMap<string, QueryInfo> {\n return this.#pipelines;\n }\n\n /**\n * Stats from the hydration of the pipeline for `queryID`, or `undefined` if\n * the query has no pipeline.\n */\n hydrationStats(queryID: string): HydrationStats | undefined {\n const pipeline = this.#pipelines.get(queryID);\n return pipeline\n ? {\n rowCount: pipeline.hydrationRowCount,\n rowsRead: pipeline.hydrationRowsRead,\n planWarnings: pipeline.planWarnings,\n shape: pipeline.shape,\n }\n : undefined;\n }\n\n /**\n * Logs what the planner warned about the plan it chose for `query`, at\n * most once per query shape per {@link PLAN_WARNING_LOG_WINDOW_MS}.\n */\n #logPlanWarnings(\n shape: QueryShape,\n {queryHash, transformationHash, queryName}: QueryLogInfo,\n warnings: readonly PlanWarning[],\n ): void {\n if (!this.#lc.warn) {\n return;\n }\n const suppressed = planWarningLogThrottle.admit(\n `${queryName ?? ''}:${shape.hash}`,\n );\n if (suppressed === undefined) {\n return;\n }\n const messages = warnings.map(planWarningMessage);\n this.#lc.warn(\n `Query plan warning${queryName === undefined ? '' : ` for ${queryName}`}: ` +\n messages.join(' '),\n {\n zeroEvent: 'query-plan-warning',\n queryHash,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n warnings,\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n zql: shape.zql,\n },\n );\n }\n\n #totalRowsRead(): number {\n let total = 0;\n for (const table of this.#tables.values()) {\n total += table.rowsRead;\n }\n return total;\n }\n\n totalHydrationTimeMs(): number {\n let total = 0;\n for (const pipeline of this.#pipelines.values()) {\n total += pipeline.hydrationTimeMs;\n }\n return total;\n }\n\n /**\n * The rows the sources hold in memory for the advancement in progress.\n * The advancement's reservation from the {@link DeferredWritesBudget}\n * bounds this.\n */\n get pendingRows(): number {\n let total = 0;\n for (const source of this.#tables.values()) {\n total += source.pendingRows;\n }\n return total;\n }\n\n /**\n * The estimated bytes of {@link pendingRows}, which are added to the\n * {@link DeferredWritesBudget}'s held bytes as the advancement goes.\n */\n get pendingBytes(): number {\n let total = 0;\n for (const source of this.#tables.values()) {\n total += source.pendingBytes;\n }\n return total;\n }\n\n #logQueryPipelineLifecycle({\n zeroEvent,\n pipelineRunID,\n queryHash,\n transformationHash,\n queryName,\n hydrationReason,\n stopReason,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n pipelineLifetimeMs,\n }: QueryPipelineLifecycleLog): void {\n let lc = this.#lc\n .withContext('zeroEvent', zeroEvent)\n .withContext('pipelineRunID', pipelineRunID)\n .withContext('queryHash', queryHash)\n .withContext('transformationHash', transformationHash);\n if (queryName !== undefined) {\n lc = lc.withContext('queryName', queryName);\n }\n if (hydrationReason !== undefined) {\n lc = lc.withContext('hydrationReason', hydrationReason);\n }\n if (stopReason !== undefined) {\n lc = lc.withContext('stopReason', stopReason);\n }\n if (hydrationTimeMs !== undefined) {\n lc = lc.withContext('hydrationTimeMs', hydrationTimeMs);\n }\n if (hydrationRowCount !== undefined) {\n lc = lc.withContext('hydrationRowCount', hydrationRowCount);\n }\n if (hydrationRowsRead !== undefined) {\n lc = lc.withContext('hydrationRowsRead', hydrationRowsRead);\n }\n if (pipelineLifetimeMs !== undefined) {\n lc = lc.withContext('pipelineLifetimeMs', pipelineLifetimeMs);\n }\n lc.info?.('query pipeline lifecycle');\n }\n\n /**\n * A `{scalar: true}` that cannot be honored degrades silently to a plain\n * EXISTS, so the author gets none of the plan they asked for and no signal\n * that they didn't. Say so, with the unique keys that were actually\n * available — the client schema knows only primary keys, so this is the only\n * place the advice can be correct.\n */\n #warnIgnoredScalarHints(queryID: string, hints: IgnoredScalarHint[]): void {\n for (const {table, uniqueKeys} of hints) {\n const keys = uniqueKeys.map(k => `(${k.join(', ')})`).join(', ');\n this.#lc.warn?.(\n `Ignoring {scalar: true} on the \"${table}\" subquery of query ` +\n `${queryID}: it does not constrain every column of any unique key ` +\n `${keys.length > 0 ? `[${keys}]` : '(none on this table)'} to a ` +\n `literal with \"=\", so it is not provably limited to one row. ` +\n `The gate runs as a plain EXISTS.`,\n );\n }\n }\n\n #disableCorrelatedPredicatePushdown(): boolean {\n return this.#config?.enableCorrelatedPredicatePushdown === false;\n }\n\n #resolveScalarSubqueries(ast: AST): {\n ast: AST;\n companionRows: {table: string; row: Row}[];\n companions: CompanionSubquery[];\n companionInputs: Input[];\n ignoredScalarHints: IgnoredScalarHint[];\n } {\n const companionRows: {table: string; row: Row}[] = [];\n const companionInputs: Input[] = [];\n\n const executor = (\n subqueryAST: AST,\n childField: string,\n ): LiteralValue | null | undefined => {\n const input = buildPipeline(\n subqueryAST,\n {\n disableCorrelatedPredicatePushdown:\n this.#disableCorrelatedPredicatePushdown(),\n getSource: name => this.#getSource(name),\n createStorage: () => this.#createStorage(),\n decorateSourceInput: (input: SourceInput): Input => input,\n decorateInput: input => input,\n addEdge() {},\n decorateFilterInput: input => input,\n },\n 'scalar-subquery',\n );\n // Tracked before it is fetched so that a failure in this or a later\n // subquery can tear it down below. A companion with no result is kept\n // alive too: it detects a future insert that creates the row.\n companionInputs.push(input);\n // Consume the full stream rather than using first() to avoid\n // triggering early return on Take's #initialFetch assertion.\n // The subquery AST already has limit: 1, so at most one row is produced.\n let node: Node | undefined;\n for (const n of skipYields(input.fetch({}))) {\n node ??= n;\n }\n if (!node) {\n return undefined;\n }\n companionRows.push({table: subqueryAST.table, row: node.row as Row});\n return (node.row[childField] as LiteralValue) ?? null;\n };\n\n let resolved: AST;\n let companions: CompanionSubquery[];\n let ignoredScalarHints: IgnoredScalarHint[];\n try {\n ({\n ast: resolved,\n companions,\n ignoredScalarHints,\n } = resolveSimpleScalarSubqueries(ast, this.#tableSpecs, executor));\n } catch (e) {\n for (const input of companionInputs) {\n input.destroy();\n }\n throw e;\n }\n return {\n ast: resolved,\n companionRows,\n companions,\n companionInputs,\n ignoredScalarHints,\n };\n }\n\n /**\n * Adds a pipeline for the query. The method will hydrate the query using the\n * driver's current snapshot of the database and return a stream of results.\n * Henceforth, updates to the query will be returned when the driver is\n * {@link advance}d. The query and its pipeline can be removed with\n * {@link removeQuery()}.\n *\n * If a query with the same queryID is already added, the existing pipeline\n * will be removed and destroyed before adding the new pipeline.\n *\n * @param timer The caller-controlled {@link Timer} used to determine the\n * final hydration time. (The caller may pause and resume the timer\n * when yielding the thread for time-slicing).\n * @return The rows from the initial hydration of the query.\n */\n addQuery(\n transformationHash: string,\n queryID: string,\n query: AST,\n timer: Timer,\n queryName?: string,\n hydrationReason: PipelineHydrationReason = 'query-set-sync',\n ): Iterable<RowChange | 'yield'> {\n return this.#trackRowSetSignatures(\n this.#addQueryImpl(\n transformationHash,\n queryID,\n query,\n timer,\n queryName,\n hydrationReason,\n ),\n );\n }\n\n *#addQueryImpl(\n transformationHash: string,\n queryID: string,\n query: AST,\n timer: Timer,\n queryName?: string,\n hydrationReason: PipelineHydrationReason = 'query-set-sync',\n ): Iterable<RowChange | 'yield'> {\n assert(\n this.initialized(),\n 'Pipeline driver must be initialized before adding queries',\n );\n this.removeQuery(queryID, 'replace-query');\n const pipelineRunID = randomID();\n const shape = queryShape(query);\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-start',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n });\n const debugDelegate = runtimeDebugFlags.trackRowsVended\n ? new Debug(true)\n : undefined;\n\n const costModel = this.#ensureCostModelExistsIfEnabled(\n this.#snapshotter.current().db.db,\n );\n\n assert(\n this.#advanceContext === null,\n 'Cannot hydrate while advance is in progress',\n );\n this.#hydrateContext = {\n timer,\n };\n // Hydration has the driver to itself, so the rows read by all of its\n // tables in the meantime are the rows read by this hydration. Tables\n // added by the hydration start from zero, which this also accounts for.\n const rowsReadAtStart = this.#totalRowsRead();\n let hydrationFinished = false;\n let hydrationFailed = false;\n let hydrationRowCount = 0;\n let planWarnings: readonly PlanWarning[] = [];\n const planWarningThresholds = this.#planWarningThresholds;\n // The inputs built so far, held outside the try so that a hydration that\n // does not finish (aborted by the consumer or failed) can tear them down.\n // Only a finished hydration hands them over to #pipelines.\n let builtInputs: Input[] = [];\n try {\n const {\n ast: resolvedQuery,\n companionRows,\n companions: companionMeta,\n companionInputs,\n ignoredScalarHints,\n } = this.#resolveScalarSubqueries(query);\n builtInputs = [...companionInputs];\n\n this.#warnIgnoredScalarHints(queryID, ignoredScalarHints);\n\n const input = buildPipeline(\n resolvedQuery,\n {\n debug: debugDelegate,\n enableNotExists: true, // Server-side can handle NOT EXISTS\n disableCorrelatedPredicatePushdown:\n this.#disableCorrelatedPredicatePushdown(),\n enablePlannerAwarePushdown:\n this.#config?.enablePlannerAwarePushdown !== false,\n getSource: name => this.#getSource(name),\n createStorage: () => this.#createStorage(),\n decorateSourceInput: (input: SourceInput, _queryID: string): Input =>\n new MeasurePushOperator(\n new QueryFailureLoggingOperator(\n this.#lc,\n input,\n queryID,\n transformationHash,\n queryName,\n ),\n queryID,\n this.#metricsDelegate,\n 'query-update-server',\n ),\n decorateInput: input => input,\n addEdge() {},\n decorateFilterInput: input => input,\n planWarnings: planWarningThresholds && {\n thresholds: planWarningThresholds,\n report: warnings => {\n planWarnings = warnings;\n },\n },\n },\n queryID,\n costModel,\n );\n builtInputs.push(input);\n if (planWarnings.length > 0) {\n this.#logPlanWarnings(\n shape,\n {queryHash: queryID, transformationHash, queryName},\n planWarnings,\n );\n }\n const schema = input.getSchema();\n input.setOutput({\n push: change => this.#streamPushed(queryID, schema, change),\n });\n\n for (const change of must(this.#streamer).streamNodes(\n queryID,\n schema,\n ChangeType.ADD,\n input.fetch({}),\n )) {\n if (change !== 'yield') {\n hydrationRowCount++;\n }\n yield change;\n }\n\n for (const {table, row} of companionRows) {\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, table);\n hydrationRowCount++;\n yield {\n type: ChangeType.ADD,\n queryID,\n table,\n rowKey: getRowKey(primaryKey, row),\n row,\n } as RowChange;\n }\n\n const hydrationTimeMs = timer.totalElapsed();\n const hydrationRowsRead = this.#totalRowsRead() - rowsReadAtStart;\n if (runtimeDebugFlags.trackRowCountsVended) {\n if (hydrationTimeMs > this.#logConfig.slowHydrateThreshold) {\n let totalRowsConsidered = 0;\n const lc = this.#lc\n .withContext('queryID', queryID)\n .withContext('hydrationTimeMs', hydrationTimeMs);\n for (const tableName of this.#tables.keys()) {\n const entries = Object.entries(\n debugDelegate?.getVendedRowCounts()[tableName] ?? {},\n );\n totalRowsConsidered += entries.reduce(\n (acc, entry) => acc + entry[1],\n 0,\n );\n lc.info?.(tableName + ' VENDED: ', entries);\n }\n lc.info?.(`Total rows considered: ${totalRowsConsidered}`);\n }\n }\n debugDelegate?.reset();\n\n // Set up live companion pipelines for reactive scalar subquery monitoring.\n const liveCompanions: CompanionPipeline[] = [];\n for (let i = 0; i < companionMeta.length; i++) {\n const meta = companionMeta[i];\n const companionInput = companionInputs[i];\n const companionSchema = companionInput.getSchema();\n const {childField, resolvedValue} = meta;\n companionInput.setOutput({\n push: (change: Change) => {\n let newValue: LiteralValue | null | undefined;\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.EDIT:\n newValue =\n (change[ChangeIndex.NODE].row[childField] as LiteralValue) ??\n null;\n break;\n case ChangeType.REMOVE:\n newValue = undefined;\n break;\n case ChangeType.CHILD:\n return [];\n }\n if (!scalarValuesEqual(newValue, resolvedValue)) {\n throw new ResetPipelinesSignal(\n `Scalar subquery value changed for ${meta.ast.table}: ` +\n `${String(resolvedValue)} -> ${String(newValue)}`,\n 'scalar-subquery',\n );\n }\n return this.#streamPushed(queryID, companionSchema, change);\n },\n });\n liveCompanions.push({input: companionInput, childField, resolvedValue});\n }\n\n // Note: This hydrationTime is a wall-clock overestimate, as it does\n // not take time slicing into account. The view-syncer resets this\n // to a more precise processing-time measurement with setHydrationTime().\n const pipelineReadyAtMs = Date.now();\n this.#pipelines.set(queryID, {\n input,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n planWarnings,\n hydrationReason,\n pipelineRunID,\n pipelineReadyAtMs,\n transformedAst: resolvedQuery,\n originalAst: query,\n shape,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n companions: liveCompanions,\n });\n hydrationFinished = true;\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {\n outcome: 'finished',\n timeMs: hydrationTimeMs,\n rowCount: hydrationRowCount,\n rowsRead: hydrationRowsRead,\n planWarnings,\n },\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-finish',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs,\n hydrationRowCount,\n hydrationRowsRead,\n });\n } catch (e) {\n hydrationFailed = true;\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {outcome: 'failed', timeMs: timer.totalElapsed()},\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-failed',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs: timer.totalElapsed(),\n hydrationRowCount,\n });\n logQueryFailure(\n this.#lc,\n {queryHash: queryID, transformationHash, queryName},\n 'query hydration failed',\n e,\n );\n throw e;\n } finally {\n if (!hydrationFinished && !hydrationFailed) {\n this.#queryStats?.recordHydration(\n {queryName, shape},\n {outcome: 'aborted', timeMs: timer.totalElapsed()},\n );\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-hydrate-aborted',\n pipelineRunID,\n queryHash: queryID,\n transformationHash,\n queryName,\n hydrationReason,\n hydrationTimeMs: timer.totalElapsed(),\n hydrationRowCount,\n });\n }\n if (!hydrationFinished) {\n for (const input of builtInputs) {\n input.destroy();\n }\n // Rows may already have been yielded through #trackRowSetSignatures,\n // and rowSetSignature() must not report a signature for a query\n // without an active pipeline.\n this.#rowSetSignatures.delete(queryID);\n }\n this.#hydrateContext = null;\n }\n }\n\n /**\n * Removes the pipeline for the query. This is a no-op if the query\n * was not added.\n */\n removeQuery(\n queryID: string,\n stopReason: PipelineStopReason = 'remove-query',\n ) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline) {\n this.#pipelines.delete(queryID);\n this.#destroyPipeline(queryID, pipeline, stopReason);\n }\n this.#rowSetSignatures.delete(queryID);\n }\n\n #destroyPipeline(\n queryID: string,\n pipeline: Pipeline,\n stopReason: PipelineStopReason,\n ): void {\n this.#logQueryPipelineLifecycle({\n zeroEvent: 'query-pipeline-stop',\n pipelineRunID: pipeline.pipelineRunID,\n queryHash: queryID,\n transformationHash: pipeline.transformationHash,\n queryName: pipeline.queryName,\n hydrationReason: pipeline.hydrationReason,\n stopReason,\n hydrationTimeMs: pipeline.hydrationTimeMs,\n hydrationRowCount: pipeline.hydrationRowCount,\n pipelineLifetimeMs: Date.now() - pipeline.pipelineReadyAtMs,\n });\n pipeline.input.destroy();\n for (const companion of pipeline.companions) {\n companion.input.destroy();\n }\n }\n\n /**\n * Current XOR signature of the row-set attached to `queryID`, or\n * `undefined` if no pipeline for the query is currently active.\n * Maintained incrementally by {@link addQuery} and {@link advance}.\n */\n rowSetSignature(queryID: string): bigint | undefined {\n return this.#rowSetSignatures.get(queryID);\n }\n\n /**\n * Wraps an iterable of RowChanges, XORing each row's unit hash into the\n * query's signature (ADDs and REMOVEs share the same op; EDITs are no-ops).\n * Used to intercept the yield streams from {@link addQuery} and\n * {@link advance}.\n */\n *#trackRowSetSignatures(\n changes: Iterable<RowChange | 'yield'>,\n ): Iterable<RowChange | 'yield'> {\n for (const change of changes) {\n if (change !== 'yield' && change.type !== ChangeType.EDIT) {\n const cur = this.#rowSetSignatures.get(change.queryID) ?? 0n;\n const unit = rowIDSignatureUnit({\n schema: '',\n table: change.table,\n rowKey: change.rowKey as RowKey,\n });\n this.#rowSetSignatures.set(change.queryID, cur ^ unit);\n }\n yield change;\n }\n }\n\n /**\n * Returns the value of the row with the given primary key `pk`,\n * or `undefined` if there is no such row. The pipeline must have been\n * initialized.\n */\n getRow(table: string, pk: RowKey): Row | undefined {\n assert(this.initialized(), 'Not yet initialized');\n const source = must(this.#tables.get(table));\n return source.getRow(pk as Row);\n }\n\n /**\n * Advances to the new head of the database.\n *\n * @param timer The caller-controlled {@link Timer} that will be used to\n * measure the progress of the advancement and abort with a\n * {@link ResetPipelinesSignal} if it is estimated to take longer\n * than a hydration.\n * @return The resulting row changes for all added queries. Note that the\n * `changes` must be iterated over in their entirety in order to\n * advance the database snapshot.\n */\n advance(timer: Timer): {\n version: string;\n numChanges: number;\n changes: Iterable<RowChange | 'yield'>;\n /** Available throughout iteration and after completion or an abort. */\n progress: () => AdvancementProgress;\n } {\n assert(\n this.initialized(),\n 'Pipeline driver must be initialized before advancing',\n );\n const diff = this.#snapshotter.advance(\n this.#tableSpecs,\n this.#allTableNames,\n this.#tables,\n // Sources skip changes that none of this client group's pipelines can\n // observe, so a `prev` they write to diverges from other groups'.\n // #advance() overrides this if it holds the changes in memory.\n 'divergent',\n );\n const {prev, curr, changes} = diff;\n this.#lc.debug?.(\n `advance ${prev.version} => ${curr.version}: ${changes} changes`,\n );\n\n const progress = {changesProcessed: 0};\n return {\n version: curr.version,\n numChanges: changes,\n changes: this.#trackRowSetSignatures(\n this.#advance(diff, timer, changes, progress),\n ),\n progress: () => ({\n changesScanned: diff.changesScanned,\n changesProcessed: progress.changesProcessed,\n changesSkipped: diff.changesSkipped,\n }),\n };\n }\n\n *#advance(\n diff: SnapshotDiff,\n timer: Timer,\n numChanges: number,\n progress: {changesProcessed: number},\n ): Iterable<RowChange | 'yield'> {\n assert(\n this.#hydrateContext === null,\n 'Cannot advance while hydration is in progress',\n );\n const totalHydrationTimeMs = this.totalHydrationTimeMs();\n const advanceContext: AdvanceContext = {\n timer,\n progress,\n diff,\n completedChanges: 0,\n totalHydrationTimeMs,\n numChanges,\n reservedRows: undefined,\n heldBytes: 0,\n currentChangeStartMs: undefined,\n pos: 0,\n currentTable: undefined,\n queryStats: new Map(),\n };\n this.#advanceContext = advanceContext;\n // The reservation is made here rather than in advance(), so that the\n // finally below is guaranteed to release it: a generator that is never\n // started never runs its finally.\n let timedOut = false;\n try {\n advanceContext.reservedRows = this.#reserveDeferredWrites(numChanges);\n const deferWrites = advanceContext.reservedRows !== undefined;\n if (deferWrites) {\n // `prev` is not written, so all of its reads can be shared.\n diff.setPrevWrites('none');\n }\n for (const table of this.#tables.values()) {\n table.setDeferWrites(deferWrites);\n }\n this.#lc.debug?.(\n `starting pipeline advancement of ${numChanges} changes with an ` +\n `advancement time limited based on total hydration time of ` +\n `${totalHydrationTimeMs} ms (${deferWrites ? 'deferred' : 'write-through'}).`,\n );\n for (const {\n table,\n prevValues: probedPrevValues,\n nextValue,\n rowKey,\n } of diff) {\n // The diff counts every raw entry, whereas it only yields observed,\n // non-no-op changes. Match the projection's progress to its raw total,\n // but do not count the current change until its push has completed.\n advanceContext.completedChanges = diff.changesScanned - 1;\n // Advance progress is checked each time a row is fetched\n // from a TableSource during push processing, but some pushes\n // don't read any rows. Check progress here before processing\n // the next change.\n if (this.#shouldAdvanceYieldMaybeAbortAdvance()) {\n yield 'yield';\n }\n const start = timer.totalElapsed();\n advanceContext.currentChangeStartMs = start;\n advanceContext.currentTable = table;\n\n let holds: HeldRows = 'fits';\n let pushCompleted = false;\n try {\n try {\n const tableSource = this.#tables.get(table);\n if (!tableSource) {\n // no pipelines read from this table, so no need to process the change\n pushCompleted = true;\n continue;\n }\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, table);\n // Early skip: If this change is an ADD of a row no connection can observe,\n // or an in-place EDIT where neither the old nor new row can be observed\n // and no secondary unique keys are displaced, skip immediately.\n const isUnobservableAdd =\n probedPrevValues.length === 0 &&\n nextValue !== null &&\n !tableSource.mayAcceptRow(nextValue as Row);\n const isUnobservableEdit =\n probedPrevValues.length === 1 &&\n nextValue !== null &&\n !tableSource.mayAcceptRow(nextValue as Row) &&\n !tableSource.mayAcceptRow(probedPrevValues[0] as Row) &&\n deepEqual(\n getRowKey(primaryKey, probedPrevValues[0] as Row) as JSONValue,\n rowKey as JSONValue,\n );\n if (isUnobservableAdd || isUnobservableEdit) {\n pushCompleted = true;\n continue;\n }\n\n if (\n nextValue !== null &&\n !this.#reserveSecondRow(advanceContext, rowKey, primaryKey)\n ) {\n holds = 'rows';\n }\n // The diff probed the `prev` snapshot for the rows this change\n // collides with. If the source is deferring its writes, the\n // earlier changes of this advancement are not in that snapshot,\n // so the probe has to be reconciled against them. A no-op for a\n // write-through source, which has already applied them to `prev`.\n const prevValues = tableSource.reconcilePendingConflicts(\n probedPrevValues,\n nextValue,\n rowKey as Row,\n this.#tableSpecs.get(table)?.tableSpec.uniqueKeys ?? [],\n );\n let editOldRow: Row | undefined = undefined;\n for (const prevValue of prevValues) {\n if (\n nextValue &&\n deepEqual(\n getRowKey(primaryKey, prevValue as Row) as JSONValue,\n getRowKey(primaryKey, nextValue as Row) as JSONValue,\n )\n ) {\n editOldRow = prevValue;\n } else {\n if (nextValue) {\n this.#conflictRowsDeleted.add(1);\n }\n yield* this.#push(\n tableSource,\n makeSourceChangeRemove(prevValue as Row),\n );\n }\n }\n if (nextValue) {\n if (editOldRow) {\n yield* this.#push(\n tableSource,\n makeSourceChangeEdit(nextValue as Row, editOldRow),\n );\n } else {\n yield* this.#push(\n tableSource,\n makeSourceChangeAdd(nextValue as Row),\n );\n }\n }\n pushCompleted = true;\n } finally {\n advanceContext.pos++;\n if (pushCompleted) {\n progress.changesProcessed++;\n advanceContext.completedChanges = diff.changesScanned;\n }\n }\n\n this.#shouldAdvanceYieldMaybeAbortAdvance(false);\n if (holds === 'fits') {\n holds = this.#holdPendingRows(advanceContext);\n }\n } finally {\n advanceContext.currentChangeStartMs = undefined;\n }\n\n const elapsed = timer.totalElapsed() - start;\n this.#advanceTime.recordMs(elapsed, {\n table,\n });\n\n if (holds !== 'fits') {\n // Before the diff reads the next change from `prev`.\n yield* this.#writeThrough(advanceContext, diff, holds);\n }\n }\n\n // Include trailing skips, or an advancement that yielded no changes.\n advanceContext.completedChanges = diff.changesScanned;\n\n // Set the new snapshot on all TableSources.\n const {curr} = diff;\n for (const table of this.#tables.values()) {\n table.setDB(curr.db.db);\n }\n this.#ensureCostModelExistsIfEnabled(curr.db.db);\n this.#lc.debug?.(`Advanced to ${curr.version}`);\n this.#logSlowAdvances(advanceContext);\n } catch (e) {\n if (\n e instanceof ResetPipelinesSignal &&\n e.reason === 'advancement-timeout'\n ) {\n timedOut = true;\n this.#logAdvanceTimeout(advanceContext, e);\n }\n throw e;\n } finally {\n if (advanceContext.reservedRows !== undefined) {\n // An advancement that completed has moved its sources to `curr`,\n // which dropped what they held. One that was abandoned is followed by\n // a reset of the pipelines, but not right away, and what they hold\n // must not outlast its release from the budget.\n for (const table of this.#tables.values()) {\n table.discardPendingChanges();\n }\n }\n this.#releaseDeferredWrites(advanceContext);\n this.#advanceContext = null;\n this.#recordAdvanceStats(advanceContext, timedOut);\n }\n }\n\n /** Accounts the time each query took in an advancement to its shape. */\n #recordAdvanceStats({queryStats}: AdvanceContext, timedOut: boolean): void {\n if (this.#queryStats === undefined) {\n return;\n }\n for (const [queryID, {timeMs, changes}] of queryStats) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline !== undefined) {\n this.#queryStats.recordAdvance(pipeline, {timeMs, changes, timedOut});\n }\n }\n }\n\n /**\n * Decides how the sources apply an advancement's changes: in memory if\n * they fit in the worker's {@link DeferredWritesBudget}, and otherwise\n * written through to the `prev` snapshot. Returns the number of rows\n * reserved if they are held in memory.\n */\n #reserveDeferredWrites(numChanges: number): number | undefined {\n const budget = this.#deferredWrites;\n if (!budget) {\n return undefined;\n }\n // An entry sets or removes one row, and a row that it displaces has an\n // entry of its own. Entries of tables that no pipeline of this group reads\n // are counted too: excluding them would take a scan of the entries (see\n // #reserveSecondRow for the one kind of entry that can need two rows).\n if (budget.tryReserve(numChanges)) {\n return numChanges;\n }\n this.#deferredWritesFallbacks.add(1, {stage: 'start'});\n this.#lc.debug?.(\n `writing through ${numChanges} changes: ${budget.reservedRows} rows ` +\n `of the deferred writes budget are reserved`,\n );\n return undefined;\n }\n\n /**\n * If the change log identifies the rows of a table by a key other than its\n * primary key here, an entry that sets a row can change its primary key,\n * which is two rows to a source: the old and the new. (An entry that removes\n * a row removes the one row its key identifies.) Reserves the second row for\n * such an entry, if the advancement holds its changes in memory, and\n * returns whether it fit.\n */\n #reserveSecondRow(\n advanceContext: AdvanceContext,\n rowKey: RowKey,\n primaryKey: PrimaryKey,\n ): boolean {\n if (\n advanceContext.reservedRows === undefined ||\n isKeyedBy(rowKey, primaryKey)\n ) {\n return true;\n }\n if (!must(this.#deferredWrites).tryReserveMore(1)) {\n return false;\n }\n advanceContext.reservedRows++;\n return true;\n }\n\n /**\n * Writes the changes that an advancement holds in memory through to the\n * `prev` snapshot, and the rest of its changes after them, when they no\n * longer fit (see {@link HeldRows}). The advancement then continues as if\n * it had written through from the start.\n */\n *#writeThrough(\n advanceContext: AdvanceContext,\n diff: SnapshotDiff,\n reason: Exclude<HeldRows, 'fits'>,\n ): Iterable<'yield'> {\n const budget = must(this.#deferredWrites);\n this.#deferredWritesFallbacks.add(1, {stage: 'partway', reason});\n this.#lc.debug?.(\n `writing through (${reason}) at ${advanceContext.pos} of ` +\n `${advanceContext.numChanges} changes, holding ` +\n `${advanceContext.heldBytes} of the ${budget.heldBytes} estimated ` +\n `bytes held on this worker (budget: ${budget.maxBytes})`,\n );\n // `prev` is about to be written, so the reads that its writes can affect\n // can no longer be shared.\n diff.setPrevWrites('divergent');\n for (const source of this.#tables.values()) {\n yield* source.writePendingChanges();\n }\n this.#releaseDeferredWrites(advanceContext);\n }\n\n #releaseDeferredWrites(advanceContext: AdvanceContext) {\n const {reservedRows, heldBytes} = advanceContext;\n if (reservedRows !== undefined) {\n must(this.#deferredWrites).release(reservedRows, heldBytes);\n advanceContext.reservedRows = undefined;\n advanceContext.heldBytes = 0;\n }\n }\n\n #recordAdvancePush(queryID: string, timeMs: number): void {\n const advance = this.#advanceContext;\n if (advance === null) {\n return;\n }\n const stats = getOrInsertComputed(advance.queryStats, queryID, () => ({\n timeMs: 0,\n changes: 0,\n lastPos: -1,\n timeMsByTable: new Map(),\n }));\n stats.timeMs += timeMs;\n if (stats.lastPos !== advance.pos) {\n stats.lastPos = advance.pos;\n stats.changes++;\n }\n const table = advance.currentTable;\n if (table !== undefined) {\n stats.timeMsByTable.set(\n table,\n (stats.timeMsByTable.get(table) ?? 0) + timeMs,\n );\n }\n }\n\n /**\n * The identity of the query of the pipeline for `queryID` and its\n * {@link queryShape}, for logging.\n */\n #queryForLog(queryID: string) {\n const pipeline = this.#pipelines.get(queryID);\n if (pipeline === undefined) {\n return undefined;\n }\n const {transformationHash, queryName, shape} = pipeline;\n return {\n queryName,\n shape,\n fields: {\n queryHash: queryID,\n transformationHash,\n ...(queryName !== undefined && {queryName}),\n queryShape: shape.hash,\n },\n };\n }\n\n /** Compute once for an admitted log, without clocks or allocations per change. */\n #advancementLogFields({\n timer,\n diff,\n progress,\n completedChanges,\n queryStats,\n totalHydrationTimeMs,\n }: AdvanceContext): AdvancementLogFields {\n const advancementTimeMs = timer.totalElapsed();\n let queryPushTimeMs = 0;\n for (const stats of queryStats.values()) {\n queryPushTimeMs += stats.timeMs;\n }\n return {\n advancementTimeMs,\n queryPushTimeMs,\n // Time outside measured query output pushes includes upstream query\n // propagation, diff iteration, sources and consumer work/waits. This\n // approximate remainder is not a CPU or non-query-work attribution;\n // clamp it for timer precision/overlap.\n outsideQueryPushTimeMs: Math.max(0, advancementTimeMs - queryPushTimeMs),\n totalHydrationTimeMs,\n changesScanned: diff.changesScanned,\n changesCompleted: completedChanges,\n changesProcessed: progress.changesProcessed,\n changesSkipped: diff.changesSkipped,\n };\n }\n\n /**\n * Logs each query whose pushes took longer than the slow advance\n * threshold, at most once per query shape per\n * {@link SLOW_ADVANCE_LOG_WINDOW_MS}.\n */\n #logSlowAdvances(context: AdvanceContext): void {\n if (!this.#lc.warn) {\n return;\n }\n const {queryStats, numChanges} = context;\n let advancementFields: AdvancementLogFields | undefined;\n for (const [queryID, stats] of queryStats) {\n if (stats.timeMs <= this.#logConfig.slowAdvanceThreshold) {\n continue;\n }\n const query = this.#queryForLog(queryID);\n if (query === undefined) {\n continue;\n }\n const suppressed = slowAdvanceLogThrottle.admit(\n `${query.queryName ?? ''}:${query.shape.hash}`,\n );\n if (suppressed === undefined) {\n continue;\n }\n this.#lc.warn(\n `Slow query advancement${query.queryName === undefined ? '' : ` for ${query.queryName}`}: ` +\n `${Math.round(stats.timeMs)} ms for ${stats.changes} of ${numChanges} changes`,\n {\n zeroEvent: 'query-slow-advance',\n ...query.fields,\n advanceTimeMs: stats.timeMs,\n changes: stats.changes,\n advancementChanges: numChanges,\n ...(advancementFields ??= this.#advancementLogFields(context)),\n timeMsByTable: Object.fromEntries(stats.timeMsByTable),\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n zql: query.shape.zql,\n },\n );\n }\n }\n\n /**\n * Logs the queries that took the most time in an advancement that timed\n * out, since the reset that follows would otherwise not say what was slow.\n * Throttled per query shape of the slowest query, with a shared fallback\n * when the timeout happens before any query push timings are recorded.\n */\n #logAdvanceTimeout(\n context: AdvanceContext,\n reset: ResetPipelinesSignal,\n ): void {\n if (!this.#lc.warn) {\n return;\n }\n const {queryStats, numChanges} = context;\n const slowest = [...queryStats]\n .toSorted(([, a], [, b]) => b.timeMs - a.timeMs)\n .slice(0, ADVANCE_TIMEOUT_LOG_MAX_QUERIES)\n .flatMap(([queryID, stats]) => {\n const query = this.#queryForLog(queryID);\n return query ? [{query, stats}] : [];\n });\n const leadingQuery = slowest[0]?.query;\n const suppressed = slowAdvanceLogThrottle.admit(\n leadingQuery\n ? `timeout:${leadingQuery.queryName ?? ''}:${leadingQuery.shape.hash}`\n : 'timeout:outside-query-push',\n );\n if (suppressed === undefined) {\n return;\n }\n const summary = slowest\n .map(\n ({query, stats}) =>\n `${query.queryName ?? query.fields.queryHash} (${Math.round(stats.timeMs)} ms)`,\n )\n .join(', ');\n this.#lc.warn(\n `Advancement of ${numChanges} changes timed out. ` +\n (summary\n ? `The queries that took the most time: ${summary}`\n : 'No query push timings were recorded.'),\n {\n zeroEvent: 'query-advance-timeout',\n // Says where the advancement was when it timed out.\n reason: reset.message,\n advancementChanges: numChanges,\n ...this.#advancementLogFields(context),\n queries: slowest.map(({query, stats}) => ({\n ...query.fields,\n advanceTimeMs: stats.timeMs,\n changes: stats.changes,\n zql: query.shape.zql,\n })),\n ...(suppressed > 0 && {suppressedSinceLastLog: suppressed}),\n },\n );\n }\n\n /** Implements `BuilderDelegate.getSource()` */\n #getSource(tableName: string): Source {\n return getOrInsertComputed(this.#tables, tableName, tableName => {\n const tableSpec = mustGetTableSpec(this.#tableSpecs, tableName);\n const primaryKey = mustGetPrimaryKey(this.#primaryKeys, tableName);\n\n const {db} = this.#snapshotter.current();\n const source = new TableSource(\n this.#lc,\n this.#logConfig,\n db.db,\n tableName,\n tableSpec.zqlSpec,\n primaryKey,\n () => this.#shouldYield(),\n // Pipelines only read tables through their connections, and the\n // sources are moved to the next snapshot after every advancement.\n // How writes are applied is set per advancement, by #advance().\n {skipUnobservableChanges: true},\n );\n this.#lc.debug?.(`created TableSource for ${tableName}`);\n return source;\n });\n }\n\n #shouldYield(): boolean {\n if (this.#hydrateContext) {\n return this.#hydrateContext.timer.elapsedLap() > this.#yieldThresholdMs();\n }\n if (this.#advanceContext) {\n return this.#shouldAdvanceYieldMaybeAbortAdvance();\n }\n throw new Error('shouldYield called outside of hydration or advancement');\n }\n\n /**\n * Cancel advancement processing when either the whole batch projects to be\n * more expensive than hydration, or the current source change alone exceeds\n * the hydration budget. The late-finish exception only applies to batch-level\n * checks; a single pathological push always resets.\n */\n #shouldAdvanceYieldMaybeAbortAdvance(checkYield = true): boolean {\n const {\n currentChangeStartMs,\n completedChanges,\n numChanges,\n timer: advanceTimer,\n totalHydrationTimeMs,\n } = must(this.#advanceContext);\n const elapsed = advanceTimer.totalElapsed();\n const currentChangeElapsedMs =\n currentChangeStartMs === undefined\n ? undefined\n : elapsed - currentChangeStartMs;\n if (\n currentChangeElapsedMs !== undefined &&\n shouldResetSlowCurrentChange(currentChangeElapsedMs, totalHydrationTimeMs)\n ) {\n this.#throwSlowCurrentChangeReset(\n completedChanges,\n numChanges,\n elapsed,\n currentChangeElapsedMs,\n totalHydrationTimeMs,\n );\n }\n const projectedTotalTimeMs = projectedAdvancementTimeMs(\n elapsed,\n completedChanges,\n numChanges,\n );\n const shouldFinish = shouldFinishLateAdvancement(\n completedChanges,\n numChanges,\n );\n if (!shouldFinish) {\n const canProject = canProjectAdvancement(\n elapsed,\n projectedTotalTimeMs,\n completedChanges,\n numChanges,\n );\n if (canProject) {\n const projectedTotal = must(projectedTotalTimeMs);\n if (\n shouldResetProjectedAdvancement(\n elapsed,\n projectedTotal,\n totalHydrationTimeMs,\n )\n ) {\n this.#throwProjectedAdvancementReset(\n completedChanges,\n numChanges,\n elapsed,\n projectedTotal,\n totalHydrationTimeMs,\n );\n }\n } else if (\n elapsed > MIN_ADVANCEMENT_TIME_LIMIT_MS &&\n elapsed > totalHydrationTimeMs\n ) {\n throw new ResetPipelinesSignal(\n `Advancement exceeded timeout at ${completedChanges} of ${numChanges} changes ` +\n `after ${elapsed} ms. Advancement time limited based on total ` +\n `hydration time of ${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n }\n return checkYield && advanceTimer.elapsedLap() > this.#yieldThresholdMs();\n }\n\n /**\n * The rows an advancement holds in memory are bounded by its reservation,\n * but their width is not known in advance. So the bytes they are estimated\n * to hold are added to those held by the other advancements on this worker.\n * Says whether that takes them past the budget, or if the rows are not\n * bounded by the reservation after all.\n */\n #holdPendingRows(advanceContext: AdvanceContext): HeldRows {\n const {reservedRows, heldBytes, pos, numChanges} = advanceContext;\n if (reservedRows === undefined) {\n return 'fits';\n }\n const budget = must(this.#deferredWrites);\n const rows = this.pendingRows;\n const bytes = this.pendingBytes;\n // Recorded first, so that the bytes are released even if they do not fit.\n advanceContext.heldBytes = bytes;\n const bytesFit = budget.holdBytes(bytes - heldBytes);\n if (rows > reservedRows) {\n // The reservation is supposed to bound the rows held (see\n // DeferredWritesBudget), so this is a bug, but not one to crash on.\n budget.recordRowOverrun();\n this.#lc.error?.(\n `Advancement holds ${rows} rows at ${pos} of ${numChanges} changes, ` +\n `more than the ${reservedRows} it reserved. Writing through the rest.`,\n );\n return 'row-overrun';\n }\n return bytesFit ? 'fits' : 'bytes';\n }\n\n #throwSlowCurrentChangeReset(\n pos: number,\n numChanges: number,\n elapsed: number,\n currentChangeElapsedMs: number,\n totalHydrationTimeMs: number,\n ): never {\n throw new ResetPipelinesSignal(\n `Advancement exceeded timeout processing current change at ${pos} of ` +\n `${numChanges} changes after ${currentChangeElapsedMs} ms ` +\n `(${elapsed} ms total). Advancement time limited based on total ` +\n `hydration time of ${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n\n #throwProjectedAdvancementReset(\n pos: number,\n numChanges: number,\n elapsed: number,\n projectedTotalTimeMs: number | undefined,\n totalHydrationTimeMs: number,\n ): never {\n const projection =\n projectedTotalTimeMs === undefined\n ? ''\n : ` Projected total advancement time is ${projectedTotalTimeMs} ms.`;\n throw new ResetPipelinesSignal(\n `Advancement projected to exceed hydration time at ${pos} of ` +\n `${numChanges} changes after ${elapsed} ms.` +\n projection +\n ` Advancement time limited based on total hydration time of ` +\n `${totalHydrationTimeMs} ms.`,\n 'advancement-timeout',\n );\n }\n\n /** Implements `BuilderDelegate.createStorage()` */\n #createStorage(): Storage {\n return this.#storage.createStorage();\n }\n\n *#push(\n source: TableSource,\n change: SourceChange,\n ): Iterable<RowChange | 'yield'> {\n assert(this.#pushedRowChanges === null, 'push already in progress');\n try {\n this.#pushedRowChanges = [];\n for (const val of source.genPush(change)) {\n if (val === 'yield') {\n yield 'yield';\n }\n const rowChanges = this.#pushedRowChanges;\n this.#pushedRowChanges = [];\n yield* rowChanges;\n }\n } finally {\n this.#pushedRowChanges = null;\n }\n }\n\n /**\n * Converts a change pushed out of a query pipeline into row changes during\n * the push. The relationships of a pushed node are lazy, and operators such\n * as Join compute them from the state of the push in progress: while a child\n * change is pushed to each matching parent in turn, parents not yet pushed\n * must not see it. Read after the push has moved on to the next parent, or\n * after it has finished, a node's relationships show the later state, and\n * rows that are also pushed separately are counted twice.\n */\n *#streamPushed(\n queryID: string,\n schema: SourceSchema,\n change: Change,\n ): Stream<'yield'> {\n assert(this.#pushedRowChanges, 'changes are only pushed by #push');\n for (const rowChange of must(this.#streamer).streamChange(\n queryID,\n schema,\n change,\n )) {\n if (rowChange === 'yield') {\n yield rowChange;\n continue;\n }\n // #push starts a new list after each 'yield', so add to the current one.\n must(this.#pushedRowChanges).push(rowChange);\n }\n }\n}\n\n/** Turns the output of query pipelines into the row changes sent to clients. */\nclass Streamer {\n readonly #primaryKeys: Map<string, PrimaryKey>;\n readonly #tableSpecs: Map<string, LiteAndZqlSpec>;\n\n constructor(\n primaryKeys: Map<string, PrimaryKey>,\n tableSpecs: Map<string, LiteAndZqlSpec>,\n ) {\n this.#primaryKeys = primaryKeys;\n this.#tableSpecs = tableSpecs;\n }\n\n /** The row changes for a change pushed out of the pipeline of `queryID`. */\n *streamChange(\n queryID: string,\n schema: SourceSchema,\n change: Change,\n ): Iterable<RowChange | 'yield'> {\n // We do not sync rows gathered by the permissions\n // system to the client.\n if (schema.system === 'permissions') {\n return;\n }\n\n const type = change[ChangeIndex.TYPE];\n switch (type) {\n case ChangeType.REMOVE:\n case ChangeType.ADD:\n yield* this.streamNodes(queryID, schema, type, [\n change[ChangeIndex.NODE],\n ]);\n break;\n case ChangeType.CHILD: {\n const child = change[ChangeIndex.CHILD_DATA];\n yield* this.streamChange(\n queryID,\n must(schema.relationships[child.relationshipName]),\n child.change,\n );\n break;\n }\n case ChangeType.EDIT:\n yield* this.streamNodes(queryID, schema, type, [\n {row: change[ChangeIndex.NODE].row, relationships: {}},\n ]);\n break;\n default:\n unreachable(change[ChangeIndex.TYPE]);\n }\n }\n\n /** A row change of type `op` for each of `nodes` and their descendants. */\n *streamNodes(\n queryID: string,\n schema: SourceSchema,\n op: ChangeType.ADD | ChangeType.REMOVE | ChangeType.EDIT,\n nodes: Iterable<Node | 'yield'>,\n ): Iterable<RowChange | 'yield'> {\n const {tableName: table, system} = schema;\n\n // We do not sync rows gathered by the permissions\n // system to the client.\n if (system === 'permissions') {\n return;\n }\n\n const primaryKey = must(this.#primaryKeys.get(table));\n const spec = must(this.#tableSpecs.get(table)).tableSpec;\n\n for (const node of nodes) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n const {relationships} = node;\n let {row} = node;\n const rowKey = getRowKey(primaryKey, row);\n if (op !== ChangeType.REMOVE) {\n const rowVersion = row[ZERO_VERSION_COLUMN_NAME];\n if (\n typeof rowVersion === 'string' &&\n rowVersion < (spec.minRowVersion ?? '00')\n ) {\n row = {...row, [ZERO_VERSION_COLUMN_NAME]: spec.minRowVersion};\n }\n }\n\n yield {\n type: op,\n queryID,\n table,\n rowKey,\n row: op === ChangeType.REMOVE ? undefined : row,\n } as RowChange;\n\n for (const relationship of Object.keys(relationships)) {\n const childSchema = must(schema.relationships[relationship]);\n // Permissions rows are not synced, so their relationship is not read.\n if (childSchema.system !== 'permissions') {\n yield* this.streamNodes(\n queryID,\n childSchema,\n op,\n relationships[relationship](),\n );\n }\n }\n }\n }\n}\n\nclass QueryFailureLoggingOperator implements Input, Output {\n readonly #lc: LogContext;\n readonly #input: Input;\n readonly #queryHash: string;\n readonly #transformationHash: string;\n readonly #queryName: string | undefined;\n #output: Output = throwOutput;\n\n constructor(\n lc: LogContext,\n input: Input,\n queryHash: string,\n transformationHash: string,\n queryName?: string,\n ) {\n this.#lc = lc;\n this.#input = input;\n this.#queryHash = queryHash;\n this.#transformationHash = transformationHash;\n this.#queryName = queryName;\n input.setOutput(this);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n fetch(req: FetchRequest): Iterable<Node | 'yield'> {\n return this.#input.fetch(req);\n }\n\n *push(change: Change): Iterable<'yield'> {\n try {\n yield* this.#output.push(change, this);\n } catch (e) {\n logQueryFailure(\n this.#lc,\n {\n queryHash: this.#queryHash,\n transformationHash: this.#transformationHash,\n queryName: this.#queryName,\n },\n 'query pipeline failed',\n e,\n );\n throw e;\n }\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n try {\n yield* this.#output.reconcile(this);\n } catch (e) {\n logQueryFailure(\n this.#lc,\n {\n queryHash: this.#queryHash,\n transformationHash: this.#transformationHash,\n queryName: this.#queryName,\n },\n 'query pipeline failed during reconcile',\n e,\n );\n throw e;\n }\n }\n }\n}\n\nfunction logQueryFailure(\n lc: LogContext,\n queryInfo: QueryLogInfo | undefined,\n message: string,\n error: unknown,\n): void {\n if (error instanceof ResetPipelinesSignal) {\n return;\n }\n let queryLC = lc;\n if (queryInfo) {\n queryLC = queryLC\n .withContext('queryHash', queryInfo.queryHash)\n .withContext('transformationHash', queryInfo.transformationHash);\n if (queryInfo.queryName !== undefined) {\n queryLC = queryLC.withContext('queryName', queryInfo.queryName);\n }\n }\n queryLC.error?.(message, error);\n}\n\nfunction getRowKey(cols: PrimaryKey, row: Row): RowKey {\n const rowKey: Writable<RowKey> = {};\n for (const col of cols) {\n // Not `rowKey[col] = ...`: for a column named `__proto__` that would call\n // the prototype setter instead of adding the key.\n assignProperty(rowKey, col, must(row[col]));\n }\n return rowKey;\n}\n\n/** Whether `rowKey` has exactly the columns of `primaryKey`. */\nfunction isKeyedBy(rowKey: RowKey, primaryKey: PrimaryKey): boolean {\n let cols = 0;\n for (const col in rowKey) {\n if (!primaryKey.includes(col)) {\n return false;\n }\n cols++;\n }\n return cols === primaryKey.length;\n}\n\n/**\n * Core hydration logic used by {@link PipelineDriver#addQuery}, extracted to a\n * function for reuse by the analyze-query RPC path so that analysis hydrates\n * queries the same way the view-syncer does in production.\n */\nexport function hydrate(\n input: Input,\n hash: string,\n clientSchema: ClientSchema,\n tableSpecs: Map<string, LiteAndZqlSpec>,\n): Iterable<RowChange | 'yield'> {\n return new Streamer(buildPrimaryKeys(clientSchema), tableSpecs).streamNodes(\n hash,\n input.getSchema(),\n ChangeType.ADD,\n input.fetch({}),\n );\n}\n\nfunction buildPrimaryKeys(\n clientSchema: ClientSchema,\n primaryKeys: Map<string, PrimaryKey> = new Map<string, PrimaryKey>(),\n) {\n for (const [tableName, {primaryKey}] of Object.entries(clientSchema.tables)) {\n primaryKeys.set(tableName, primaryKey as unknown as PrimaryKey);\n }\n return primaryKeys;\n}\n\nfunction mustGetPrimaryKey(\n primaryKeys: Map<string, PrimaryKey> | null,\n table: string,\n): PrimaryKey {\n const pKeys = must(primaryKeys, 'primaryKey map must be non-null');\n\n const rv = pKeys.get(table);\n assert(\n rv,\n () =>\n // oxlint-disable-next-line e18e/prefer-array-to-sorted\n `table '${table}' is not one of: ${JSON.stringify([...pKeys.keys()].sort())}. ` +\n `Check the spelling and ensure that the table has a primary key.`,\n );\n return rv;\n}\n\n/**\n * Compares two scalar subquery resolved values for equality.\n * Unlike `valuesEqual` in data.ts (which treats null != null for join\n * semantics), this uses identity semantics: undefined === undefined\n * (no row matched), null === null (row matched but field was NULL).\n */\nfunction scalarValuesEqual(\n a: LiteralValue | null | undefined,\n b: LiteralValue | null | undefined,\n): boolean {\n return a === b;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsPA,IAAM,kCAAkC;;;;;AAexC,IAAM,gCAAgC;AACtC,IAAM,2CAA2C;AACjD,IAAM,wCAAwC;AAC9C,IAAM,2CAA2C;AACjD,IAAM,sCAAsC;AAC5C,IAAM,oCAAoC;AAC1C,IAAM,yCAAyC;AAC/C,IAAM,mCAAmC;AAYzC,IAAM,yBAAyB,IAAI,YAAY,EAC7C,UALiC,KAAK,IAMxC,CAAC;AAQD,IAAM,yBAAyB,IAAI,YAAY,EAC7C,UAHiC,IAAI,IAIvC,CAAC;AAED,SAAS,WAAW;CAClB,OAAO,QAAQ,GAAG,OAAO,gBAAgB,EAAE,SAAS,EAAE;AACxD;AAEA,SAAS,2BACP,WACA,kBACA,YACoB;CACpB,IAAI,oBAAoB,KAAK,cAAc,GACzC;CAEF,OAAQ,YAAY,mBAAoB;AAC1C;AAEA,SAAS,4BAA4B,sBAAsC;CACzE,OAAO,KAAK,IAAI,sBAAsB,CAAC;AACzC;AAEA,SAAS,qCAAqC,YAA4B;CACxE,OAAO,KAAK,IACV,0CACA,KAAK,IACH,0CACA,KAAK,KAAK,aAAa,qCAAqC,CAC9D,CACF;AACF;AAEA,SAAS,sBACP,WACA,sBACA,kBACA,YACS;CACT,OACE,yBAAyB,KAAA,KACzB,cAAc,qCACd,oBAAoB,qCAAqC,UAAU,KACnE,aAAa;AAEjB;AAEA,SAAS,gCACP,WACA,sBACA,sBACS;CAGT,OACE,uBAAuB,YACvB,4BAA4B,oBAAoB,IAC9C;AAEN;AAE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AS,kBACP,GACA,GACS;CACT,OAAO,MAAM;AACf"}
@@ -1 +1 @@
1
- {"version":3,"file":"snapshotter.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAY,KAAK,SAAS,EAAC,MAAM,uCAAuC,CAAC;AAGhF,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,uCAAuC,CAAC;AAC/D,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,sCAAsC,CAAC;AAGrE,OAAO,KAAK,EACV,cAAc,EACd,+BAA+B,EAChC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,QAAQ,EACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,uBAAuB,CAAC;AAYjD,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,yBAAyB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,qBAAa,WAAW;;IAStB;;;;;OAKG;IACH,YACE,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAC,KAAK,EAAC,EAAE,KAAK,EACd,gBAAgB,CAAC,EAAE,MAAM,EACzB,QAAQ,CAAC,EAAE,gBAAgB,EAO5B;IAED;;;;OAIG;IACH,IAAI,IAAI,IAAI,CAUX;IAED,WAAW,IAAI,OAAO,CAErB;IAED,+EAA+E;IAC/E,OAAO,IAAI,QAAQ,CAGlB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACH,OAAO,CACL,cAAc,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,EAC3C,aAAa,EAAE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AI,KAAK,IAAI,OAAO,CAEnB;IAED,WAAW,IAAI,QAAQ,CAItB;CACF;AAiSD,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,YAAY,GAAG,EAAE,MAAM,EAEtB;CACF"}
1
+ {"version":3,"file":"snapshotter.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/view-syncer/snapshotter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAY,KAAK,SAAS,EAAC,MAAM,uCAAuC,CAAC;AAGhF,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,uCAAuC,CAAC;AAC/D,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,sCAAsC,CAAC;AAGrE,OAAO,KAAK,EACV,cAAc,EACd,+BAA+B,EAChC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAC,eAAe,EAAC,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAEL,KAAK,MAAM,EACX,KAAK,QAAQ,EACd,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,uBAAuB,CAAC;AAYjD,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,yBAAyB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,qBAAa,WAAW;;IAStB;;;;;OAKG;IACH,YACE,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,MAAM,EACd,EAAC,KAAK,EAAC,EAAE,KAAK,EACd,gBAAgB,CAAC,EAAE,MAAM,EACzB,QAAQ,CAAC,EAAE,gBAAgB,EAO5B;IAED;;;;OAIG;IACH,IAAI,IAAI,IAAI,CAUX;IAED,WAAW,IAAI,OAAO,CAErB;IAED,+EAA+E;IAC/E,OAAO,IAAI,QAAQ,CAGlB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;O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KAAK,CAAC,EAAE,gBAAgB,EACxB,GAAG,CAAC,EAAE,MAAM,SAwEb;IAiBD;;;;OAIG;IACH,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED,WAAW,IAAI,QAAQ,CAItB;CACF;AAqSD,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,YAAY,GAAG,EAAE,MAAM,EAEtB;CACF"}
@@ -292,20 +292,36 @@ var Snapshot = class Snapshot {
292
292
  * version(s) of the replica that the result depends on.
293
293
  */
294
294
  getRows(table, keys, row, cache, tag) {
295
- const validKeys = [];
296
- let mask = 0;
295
+ let validKeys = keys;
296
+ let mask = (1 << keys.length) - 1;
297
297
  for (let i = 0; i < keys.length; i++) {
298
298
  const key = keys[i];
299
- if (key.every((column) => row[column] !== null && row[column] !== void 0)) {
300
- validKeys.push(key);
301
- mask |= 1 << i;
299
+ for (let j = 0; j < key.length; j++) {
300
+ const val = row[key[j]];
301
+ if (val === null || val === void 0) {
302
+ validKeys = [];
303
+ mask = 0;
304
+ for (let k = 0; k < keys.length; k++) {
305
+ const candidate = keys[k];
306
+ if (candidate.every((col) => row[col] !== null && row[col] !== void 0)) {
307
+ validKeys.push(candidate);
308
+ mask |= 1 << k;
309
+ }
310
+ }
311
+ break;
312
+ }
302
313
  }
314
+ if (validKeys !== keys) break;
303
315
  }
304
316
  if (validKeys.length === 0) return [];
305
317
  const reads = readsFor(table);
306
318
  const buildSQL = () => reads.prefix + validKeys.map((key) => key.map((c) => `${id(c)}=?`).join(" AND ")).join(" OR ");
307
319
  const sql = keys === table.uniqueKeys && keys.length <= MAX_MEMOIZED_UNIQUE_KEYS ? getOrInsertComputed(reads.byUniqueKeys, mask, buildSQL) : buildSQL();
308
- const args = validKeys.flatMap((key) => key.map((column) => row[column]));
320
+ const args = [];
321
+ for (let i = 0; i < validKeys.length; i++) {
322
+ const key = validKeys[i];
323
+ for (let j = 0; j < key.length; j++) args.push(row[key[j]]);
324
+ }
309
325
  const read = () => this.db.statementCache.use(sql, (cached) => {
310
326
  cached.statement.safeIntegers(true);
311
327
  return cached.statement.all(args);
@@ -347,6 +363,8 @@ var Diff = class {
347
363
  #observedTables;
348
364
  #rowCache;
349
365
  #prevWrites;
366
+ #prevTagUniform;
367
+ #prevTagDivergent;
350
368
  #iterated = false;
351
369
  prev;
352
370
  curr;
@@ -361,6 +379,8 @@ var Diff = class {
361
379
  this.#observedTables = observedTables;
362
380
  this.prev = prev;
363
381
  this.curr = curr;
382
+ this.#prevTagUniform = `p:${prev.version}`;
383
+ this.#prevTagDivergent = `p:${prev.version}:${curr.version}`;
364
384
  this.changes = curr.numChangesSince(prev.version);
365
385
  this.#rowCache = rowCache && !curr.hasTableWideOpSince(prev.version) ? rowCache : void 0;
366
386
  }
@@ -416,7 +436,7 @@ var Diff = class {
416
436
  const cache = this.#rowCache;
417
437
  const prevDependsOnWrites = tableSpec.uniqueKeys.length > 1 && this.#prevWrites !== "none";
418
438
  const prevCache = prevDependsOnWrites && this.#prevWrites === "divergent" ? void 0 : cache;
419
- const prevTag = prevDependsOnWrites ? `p:${this.prev.version}:${this.curr.version}` : `p:${this.prev.version}`;
439
+ const prevTag = prevDependsOnWrites ? this.#prevTagDivergent : this.#prevTagUniform;
420
440
  const nextValue = op === "s" ? this.curr.getRow(tableSpec, rowKey, cache, `n:${stateVersion}`) : null;
421
441
  let prevValues;
422
442
  if (nextValue) prevValues = this.prev.getRows(tableSpec, tableSpec.uniqueKeys, nextValue, prevCache, prevTag);