import type { EscapedPropPath, QueryAST } from '@dxos/echo-protocol'; import type { EID, EntityId, URI } from '@dxos/keys'; export declare namespace QueryPlan { type TextSearchKind = 'full-text' | 'vector' | 'hybrid'; /** * A series of linear steps to execute a query. * Steps can potentially contain sub-plans in case of unions. * * The query executor will execute each step in sequence. * The plans start with a select step, which adds objects to the current working set. * Then the next steps will act on the current working set, preforming filters, traversals, etc. */ type Plan = { steps: Step[]; }; const Plan: Readonly<{ make: (steps: Step[]) => Plan; }>; type Step = ClearWorkingSetStep | SelectStep | FilterStep | FilterDeletedStep | TraverseStep | UnionStep | SetDifferenceStep | OrderStep | LimitStep | SkipStep | AggregateStep | SqlStep; /** * A contiguous run of steps compiled into a single SQLite statement over the index tables. * Emitted by `SqlPlanCompiler` in place of the steps it stands for, so the compiled path runs a * plan like any other rather than a separate execution mode. */ type SqlStep = { _tag: 'SqlStep'; /** Statement text with placeholders. */ sql: string; /** Values bound to the statement's placeholders, in order. */ params: readonly unknown[]; /** The steps the statement stands for, kept so traces and scope analysis still see them. */ steps: readonly Step[]; }; /** * Clear the current working set. */ type ClearWorkingSetStep = { _tag: 'ClearWorkingSetStep'; }; /** * Select objects based on id, typename, or other predicates. * Specifies the spaces and/or queues to select from. * The results from spaces and queues are unioned together. */ type SelectStep = { _tag: 'SelectStep'; /** * What to select from the scope. */ selector: Selector; /** * Where to select from. */ scope: readonly QueryAST.Scope[]; /** * Optional limit on the number of results to select. * When set, the index scan can be optimized to stop early. */ limit?: number; /** * Cursor range from a `feed-cursor` filter, both bounds exclusive. The scan returns only blocks * positioned inside it, in position order, so resuming a feed costs what is new rather than the * whole feed. Present (possibly with no bounds set) whenever the filter was, since a cursor read * covers positioned blocks only. */ feedCursorRange?: { begin?: string; end?: string; positions?: 'local' | 'edge'; }; /** * Set when the planner has proved the step's {@link limit} can be applied by the feed scan * itself — see `feedScanForLimit`. Absent means the limit must be applied downstream, after the * steps that would otherwise slice candidates out of an already-capped page. */ feedScan?: FeedScan; /** * Build bare items from index rows, carrying no document. Set by the planner only when no later * step reads anything but index fields: type, timestamps, deletion, parent and relation endpoints. */ bare?: boolean; }; /** * How a feed scan must run for a `limit` pushed into it to keep the same rows the unpushed plan * would have returned. */ type FeedScan = { /** Natural order the scan produces, matching the plan's `OrderStep`. */ direction: 'asc' | 'desc'; /** Deleted state the scan filters to, folding in the plan's `FilterDeletedStep`. */ deleted?: boolean; /** Rows the scan drops ahead of the step's `limit`, folding in the plan's `SkipStep`s. */ skip?: number; }; /** * Specifier to scan the database for objects. * Optimized to utilize database indexes. */ type Selector = WildcardSelector | IdSelector | TypeSelector | TextSelector | TimestampSelector | IncomingReferenceSelector | ChangesSelector; /** * Select Automerge changes (`Filter.changes`) rather than objects. The working set holds change * records, so only ordering, paging and aggregation may follow. */ type ChangesSelector = { _tag: 'ChangesSelector'; /** Entities whose documents to read; every document in scope when absent. */ targets?: readonly EID.EID[]; /** * `index` reads hourly buckets from the activity index, valid only for the aggregates it can * answer; `replay` lists each target document's change history. */ source: 'index' | 'replay'; }; type WildcardSelector = { _tag: 'WildcardSelector'; }; type IdSelector = { _tag: 'IdSelector'; objectIds: readonly EntityId[]; }; /** * Select objects by typename. * Supports passing an array of typenames and an optional inverse flag to optimize for index implementation. */ type TypeSelector = { _tag: 'TypeSelector'; typename: URI.URI[]; /** * If true, select objects that do not match the typename. */ inverted: boolean; }; /** * Select the objects holding a reference to `targetDXN`, straight off the reverse-reference index. * * Unlike an incoming {@link ReferenceTraversal} this needs no anchor in the working set, so it can * name an entity that is absent from the graph — a named entity, or one a migration has renamed away. */ type IncomingReferenceSelector = { _tag: 'IncomingReferenceSelector'; targetDXN: URI.URI; /** * Property path where the reference is located; null matches any property. */ property: EscapedPropPath | null; }; /** * Select objects by preforming a full-text or vector search. */ type TextSelector = { _tag: 'TextSelector'; text: string; searchKind: TextSearchKind; /** * Optionally select only objects of the given typenames. */ typename: URI.URI[] | null; }; /** * Select objects by timestamp range (createdAt / updatedAt). * Uses the EntityMetaIndex timestamp columns. */ type TimestampSelector = { _tag: 'TimestampSelector'; updatedAfter?: number; updatedBefore?: number; createdAfter?: number; createdBefore?: number; }; /** * Filter objects in the current working set based on a predicate. */ type FilterStep = { _tag: 'FilterStep'; filter: QueryAST.Filter; }; const FilterStep: Readonly<{ isNoop: (step: FilterStep) => boolean; }>; /** * Filter objects in the current working set based on the deleted state. */ type FilterDeletedStep = { _tag: 'FilterDeletedStep'; mode: 'only-deleted' | 'only-non-deleted'; }; /** * Traverse the object graph, starting from objects in the current working set. */ type TraverseStep = { _tag: 'TraverseStep'; traversal: Traversal; }; /** * Describes a traversal of the object graph. */ type Traversal = ReferenceTraversal | RelationTraversal | HierarchyTraversal; /** * Traverse a reference connection. */ type ReferenceTraversal = { _tag: 'ReferenceTraversal'; /** * Property path where the reference is located. * If null, matches references from any property (only valid for incoming direction). */ property: EscapedPropPath | null; /** * outgoing: the reference points from the anchor object to the target. * incoming: the reference points to the anchor object from the target. */ direction: 'outgoing' | 'incoming'; }; /** * Traverse a relation. */ type RelationTraversal = { _tag: 'RelationTraversal'; /** * The direction of the traversal. * There are for variants since each relation has two connectors (source & target) and there are two directions to traverse each. */ direction: 'source-to-relation' | 'relation-to-source' | 'target-to-relation' | 'relation-to-target'; }; /** * Traverse parent-child hierarchy. */ type HierarchyTraversal = { _tag: 'HierarchyTraversal'; /** * to-parent: traverse from child to parent. * to-children: traverse from parent to children. */ direction: 'to-parent' | 'to-children'; }; /** * Combine results from multiple plans. * Each of the plans starts with a copy of the current working set. * This supports plans where we first perform a selection, then traverse in different directions, and then combine the results. */ type UnionStep = { _tag: 'UnionStep'; plans: Plan[]; }; /** * Subtract the results of one plan from another. */ type SetDifferenceStep = { _tag: 'SetDifferenceStep'; source: Plan; exclude: Plan; }; /** * Order the results of the plan. */ type OrderStep = { _tag: 'OrderStep'; order: readonly QueryAST.Order[]; /** * Optional limit on the number of results to return after ordering. * When set, the sorting can be optimized to only track the top N elements. */ limit?: number; }; /** * Limit the number of results. */ type LimitStep = { _tag: 'LimitStep'; limit: number; }; /** * Skip a number of results (offset). */ type SkipStep = { _tag: 'SkipStep'; skip: number; }; /** * Aggregate the working set. `group`-kind entries partition it into contiguous groups by scalar * property values (with none, the whole set forms one group), ordered by the first occurrence of * their key in the incoming (already-ordered) working set — this lets a preceding `OrderStep` also * control group order. Non-`group` aggregates are stamped on each member so a following group-level * `OrderStep` can order by them. * * The runtime grouping/pagination algorithms live in `./group-by` (executor-side), not here. */ type AggregateStep = { _tag: 'AggregateStep'; /** Aggregate declarations; `group`-kind entries define the grouping keys. */ aggregates: readonly QueryAST.GroupAggregate[]; }; } //# sourceMappingURL=plan.d.ts.map