Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Query cursors.
A query reads each source through a cursor, and a nested-loop join rewinds the inner cursors once per outer row. A full scan steps a counter, so the number of rows scanned shows the access path. A cursor reads the database only through {!S}, and resolves a seek key again on every rewind. *)
type source = string * string list * (unit -> Catalog.Cursor.t)(** The type for query sources: the alias, the column names the query is compiled against, and a function that makes a new cursor over the rows. *)
(** The type for how a scan over a list of rows is counted. *)type policy = | Materialised (** A relation built by the statement, such as a subquery in [FROM] or a CTE. It is counted when built, not when scanned. *) | Synthetic (** A relation the engine generates, such as the schema table or a table-valued function. A scan counts as a full scan. *) | Literal (** Rows written in the statement, such as a [VALUES] list or the one row of a [SELECT] without [FROM]. They are not counted. *)
type set_walk = { compared : Sql.Value_ops.affinity list; (** The affinities each value of the set takes, in turn, before it is sought. *) order : Sql.Order.t; (** The sequence and the direction of the column in the b-tree the seek walks. *) rank : int; (** The place of the column in that b-tree. *)}(** The type for how a seek walks the values of an IN set on one column, as sqlite3 walks the ephemeral index it writes them into (wherecode.c, codeINTerm): each value once, in the column's order, so the rows come in the order of the b-tree. *)
(** {1:db Databases} *)
(** The signature of the database reads a cursor needs. *)module type S = sig type t (** The type for databases. *)
val counter : t -> Catalog.Cursor.counter (** [counter db] is the counter that full scans step, and seeks do not. *)
val table : t -> string -> Model.generic_table (** [table db name] is the table [name]. *)
val columns : t -> string -> string list (** [columns db name] is the declared columns of the base table [name], in order. *)
val encoding : t -> Sql.Encoding.t (** [encoding db] is the encoding of the database's text. *)
val affinity : t -> string -> string -> Sql.Value_ops.affinity option (** [affinity db name col] is the affinity of the column [col] of the table or view [name], or [None]. *)
val seek_plan : t -> ?where:Sql.Ast.expr -> ?index:string -> ?affinity:(string -> Sql.Value_ops.affinity list) -> ?nulls:(string -> bool) -> string -> string list -> read:int list -> (string * (reverse:bool -> Seek.rows)) option (** [seek_plan db ~where name cols ~read] is {!Seek.plan} for an equality on [cols] of table [name] in a query filtered by [where]: the text [EXPLAIN] prints after ["USING"], and the maker of its rows ({!Seek.rows}), which a cursor calls once. *)
val covering_plan : t -> ?where:Sql.Ast.expr -> ?index:string -> ?affinity:(string -> Sql.Value_ops.affinity list) -> ?nulls:(string -> bool) -> string -> string list -> read:int list -> (Model.index * (reverse:bool -> Seek.entries)) option (** [covering_plan db ~where name cols ~read] is {!Seek.covering_plan} for the same equality, for a cursor that reads the index entries only. *)
val primary_plan : t -> ?where:Sql.Ast.expr -> ?affinity:(string -> Sql.Value_ops.affinity list) -> string -> string list -> read:int list -> (Model.pk_rows * (Sql.Ast.value array -> string list)) option (** [primary_plan db ~where name cols ~read] is {!Seek.primary_plan} for the same equality, for a cursor that reads the rows of a [WITHOUT ROWID] table down its [PRIMARY KEY]. *)
val budget : t -> Sql.Join.budget (** [budget db] is the largest relation a hash join builds before it seeks instead. *)end
(** Cursors over a database. *)module Make (S : S) : sig val counted : S.t -> Catalog.Cursor.t -> Catalog.Cursor.t (** [counted db c] is [c] with each step stepping the counter of [db]: a pass over [n] rows adds [n - 1], since a rewind is not a step. *)
val of_rows : S.t -> policy -> Sql.Ast.value list list -> unit -> Catalog.Cursor.t (** [of_rows db policy rows] makes cursors over [rows], counted as [policy] says. A column past the end of a row reads as NULL. *)
val row_reader : S.t -> string -> Model.generic_table -> Btree.Table.Cursor.t -> (unit -> unit) * (int -> Catalog.Register.t -> unit) (** [row_reader db name gt c] is [(moved, read)]. [read j m] decodes column [j] of the row the table cursor [c] stands on into [m], and keeps where it found the columns only while [c] stands where it stood ({!Btree.Table.Cursor.watch}). The rowid slot and an [INTEGER PRIMARY KEY] column read the row's key, which is decoded for no other column, and a column past the end of the record reads its [DEFAULT]. [moved ()] tells [read] that the source has stepped. *)
val table_info : S.t -> Sql.Ast.table_ref -> Model.generic_table -> source (** [table_info db tr gt] is the source for the base table [tr] with model [gt]. A [WITHOUT ROWID] table scans its index b-tree in [PRIMARY KEY] order, and a rowid table scans its table b-tree. *)
type seek_path (** The type for how a {!Sql.Private.Plan.Seek} reads: the index its equality goes through, chosen once for a plan. *)
val seek_path : ?index:string -> S.t -> where:Sql.Ast.expr option -> table:string -> keys:Sql.Private.Plan.key list -> read:int list -> covering:bool -> seek_path (** [seek_path db ~index ~where ~table ~keys ~read ~covering] is how a seek of the rows of [table] whose columns equal [keys] reads, each value taking its key's {!Sql.Private.Plan.key.affinity}, for a query filtered by [where], which admits a partial index, and reading the columns [read]: the rows of a [WITHOUT ROWID] table {!S.primary_plan} seeks, else the entries of the index {!S.covering_plan} finds when [covering] holds and one does, else the rows {!S.seek_plan} names. The index is the one named [index] where given. *)
val seek : S.t -> table:string -> path:seek_path -> reverse:bool -> values:(unit -> Seek.key_value array list option) -> scan:(unit -> Catalog.Cursor.t) -> Catalog.Cursor.t (** [seek db ~table ~path ~reverse ~values ~scan] is a cursor that seeks, through [path], the rows of [table] that equal one of the keys of [values ()], each row once, the rows of a key in the order of the path's b-tree or, if [reverse], in the opposite order. If the path has no plan, the plan declines a key, or [values ()] is [None], it reads [scan ()] instead. *)
val seek_paths : S.t -> ?where:Sql.Ast.expr -> Sql.Private.Plan.access list -> reads:(string -> int list) -> seek_path option array (** [seek_paths db ~where plan ~reads] is, at each position of [plan] that is a {!Sql.Private.Plan.Seek}, its {!seek_path}, [reads t] being the columns read from table [t]. It reads the schema alone, so a plan kept between runs keeps it. *)
val drain : budget:Sql.Join.budget -> cols:int list -> (unit -> Catalog.Cursor.t) -> Sql.Ast.value list list option (** [drain ~budget ~cols mk] is the columns [cols] of every row of a cursor made by [mk], in the order of [cols], or [None] if they exceed [budget]. It stops reading as soon as they do. A row with no columns counts as one cell. *)
val set_walk : S.t -> table:string -> index:string option -> Sql.Private.Plan.key -> set_walk (** [set_walk db ~table ~index k] is how a seek of [table] through the index named [index], or through the table's own b-tree where it is [None], walks the values of an IN set on the column of [k], each converted as [k] says. *)
val key_values : Sql.Ast.value array -> cursor:(int -> Catalog.Cursor.t) -> scratch:Catalog.Register.t -> into:Seek.key_value array -> walk:(Sql.Private.Plan.key -> set_walk) -> set:(Sql.Ast.select -> Sql.Ast.value list option) -> Sql.Private.Plan.key list -> unit -> Seek.key_value array list option (** [key_values params ~cursor ~scratch ~into ~walk ~set keys ()] is the seek keys, each with one value per column in the order of [keys]: a {!Sql.Private.Plan.Const} from [params], a {!Sql.Private.Plan.Outer} as the reader of [cursor]'s column, which the seek reads on the outer row, and each distinct value of a {!Sql.Private.Plan.In_list} from [params] or of the rows [set] answers for a {!Sql.Private.Plan.In_select} in turn, in the order [walk] of its column gives, every combination of them once, the sets nested in the order of their columns' ranks, an [Outer] column read for them. Without an IN list it is [Some [into]], with [into] filled in at the first call and kept. It is [None] if a value is missing. *)
val index_scan : S.t -> table:string -> index:Model.index -> reverse:bool -> covering:bool -> Catalog.Cursor.t (** [index_scan db ~table ~index ~reverse ~covering] is a full scan of the rowid table [table] in the order of the entries of [index], descending if [reverse]. A row is read out of its entry if [covering], which holds every column read, and else out of the row the entry's rowid names. It counts as a full scan. *)
val accesses : S.t -> Sql.Ast.value array -> source list -> Sql.Private.Plan.access list -> paths:seek_path option array -> unmatched:(int * Sql.Private.Plan.access * seek_path option) list -> held:(int -> int list) -> relation:(int -> string option) -> set:(Sql.Ast.select -> Sql.Ast.value list option) -> order:int list -> Catalog.Cursor.t array (** [accesses db params infos plan ~paths ~unmatched ~held ~relation ~set ~order] is the cursors of [plan] over the sources [infos], built in [order] so that a seek or a probe reads its key from a cursor already built, then a cursor for each of [unmatched], the source at a scan position read through an access and its seek's path: the loop over the rows a RIGHT or FULL join kept unmatched ({!Sql.Private.Plan.t.unmatched}). [paths] is {!seek_paths} of [plan], each seek a {!seek} and each walk of an index an {!index_scan}. [held i] is the columns read from the source at scan position [i], as {!Sql.Private.Plan.read_columns} counts them, and [relation i] the table that source reads, if it reads one. [set sub] is the rows of the subquery [sub] of an {!Sql.Private.Plan.In_select} key, the first column of each, or [None] where it has none to give.
A hash holds the columns read and its keys, and counts its budget on them; a column it does not hold reads as NULL. Two probes of one table under the same predicates, whatever their aliases, share one hash, which holds the columns either reads and is drained once. A {!Sql.Private.Plan.Probe} seeks instead when its build exceeds the budget of [db]. *)end