(** Internal select source operations. *) module Ast = Sql.Ast module Query = Sql.Private.Query module Engine = Sql.Private.Engine module Resolve = Sql.Private.Resolve module Plan = Sql.Private.Plan module Lower = Sql.Private.Lower module Value_ops = Sql.Value_ops module Value_op = Sql.Private.Value_op module Register = Catalog.Register module Ident = Catalog.Ident module Seek = Seek module Cursor = Cursor module Catalog_view = Catalog_view module Fast_path = Fast_path type value = Ast.value = | Null | Int of int64 | Float of float | Blob of string | Text of string module type S = Select_intf.S module Make (R : S) : sig val catalog : R.t -> Sql.Private.Query.catalog (** See {!Select_intf.S.catalog}. *) val gtable : R.t -> string -> Model.generic_table (** See {!Select_intf.S.gtable}. *) val plain_rowid_gtable : R.t -> string -> Model.generic_table option (** See {!Select_intf.S.plain_rowid_gtable}. *) val table_column_names : R.t -> string -> string list (** See {!Select_intf.S.column_names}. *) val view : R.t -> string -> Sql.Ast.create_view option (** See {!Select_intf.S.view}. *) val view_columns : R.t -> Sql.Ast.create_view -> string list (** See {!Select_intf.S.view_columns}. *) val master_rows : R.t -> (int64 * Sql.Ast.value list) list (** See {!Select_intf.S.master_rows}. *) val access_cursors : R.t -> Sql.Ast.value array -> Cursor.source list -> Sql.Private.Plan.access list -> paths:R.seek_path option array -> unmatched:(int * Sql.Private.Plan.access * R.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 (** See {!Select_intf.S.accesses}. *) val seek_paths : R.t -> ?where:Sql.Ast.expr -> Sql.Private.Plan.access list -> reads:(string -> int list) -> R.seek_path option array (** See {!Select_intf.S.seek_paths}. *) val is_master_name : string -> bool (** Tests whether a name denotes the schema table. *) val master_column_names : string list (** The names of the schema table columns. *) val ident_equal : Ident.t -> Ident.t -> bool (** Compares SQL identifiers. *) val mem_ident : Ident.t -> Ident.t list -> bool (** Tests identifier membership with SQL name equality. *) val materialised_cursor : R.t -> Sql.Ast.value list list -> unit -> Catalog.Cursor.t (** Creates a cursor over already materialised rows. *) val streamed : Ast.select -> string option (** The [WITH] table of [s] its select may read as the table is made: a recursive one that select's lone source, which nothing else in [s] reads, as sqlite3 runs a recursive CTE as a co-routine of the select reading it where that select reads it once (select.c, the co-routine conditions of sqlite3Select). *) val stream_cursor : Catalog__.Value.t list Seq.t -> again:(unit -> Catalog__.Value.t list list) -> unit -> Catalog.Cursor.t (** A cursor over [rows] as they are produced: the first rewind reads them as they come, holding one at a time, and a later one reads [again ()], the relation whole. Counted as a materialised relation is, when it was made. *) val synthetic_scan_cursor : R.t -> Sql.Ast.value list list -> unit -> Catalog.Cursor.t (** Creates a cursor over synthetic table rows. *) val literal_cursor : R.t -> Sql.Ast.value list list -> unit -> Catalog.Cursor.t (** Creates a cursor over literal rows. *) val vdbe_table_info : R.t -> Ast.table_ref -> Cursor.source option (** The {!Cursor.source} for one VM-scanned base table, or [None] for the schema table, a view, or a name no table answers to. A secondary index does not divert the scan: a bare [SELECT] over an indexed table reads in table order. *) val schema_unchanged : R.t -> Model.generic_table list * Model.raw_master_entry list -> bool (** A cached plan is valid only while the schema it compiled against is unchanged. Every DDL, schema reload, and rollback replaces these lists wholesale, so physical equality with the handle's current lists is exact and O(1). *) val rerun_cache : R.t -> 'a array -> R.t option (** The handle, whose cache keeps the plan of a select run with [params], where that select may run again as it is: one reading a bound value, which its caller runs again with others, or a step of a trigger program, run for each row it fires on. Any other select holds its literals alone and meets its like only where its text runs again, and a text is prepared again at each run, as sqlite3_exec prepares it: looking such a select up and keeping it would hash it twice for nothing. *) val infos_affinity : R.t -> ?cat:Sql.Private.Query.catalog -> Sql.Ast.select -> (string * string list * 'a) list -> int * int -> Plan.affinity option (** What each scanned column can hold, for the lowering that gives every comparison its affinity: the declared type of the column on the table that position's cursor reads. A source with no schema to read -- a derived table, a VALUES list, a relation the caller bound itself -- answers for none, and a comparison against one converts neither operand. *) val cat_membership : Sql.Private.Query.catalog -> Sql.Ast.value array -> Sql.Ast.select -> Query.membership (** What each [IN (SELECT ...)] compares against: the affinity the subquery's own result column states, and its rows where it reads nothing of the query around it. Not folded into the AST before the lowering sees it, because an [IN] over a value list states no affinity on the right and the membership has to read one from that end. *) val emit_values : (Sql.Row.t -> unit) -> Catalog.Value.t list list -> unit (** Rows another path computed, handed to a row consumer one at a time. *) val catalog_run_with_infos : R.t -> ?cache:R.t -> ?prepared:Handle.prepared -> ?kept:Sql.Closure.plan option ref -> ?run_subquery: ([> `Exists | `In of Sql.Ir.comparison * Sql.Ast.value | `Scalar ] -> Sql.Ast.select -> (string * (string * Catalog.Value.t) list) list -> Sql.Ast.value) -> ?regs:Catalog.Register.t array option ref -> ?order:int list -> cat:Query.catalog -> scan:(unit -> Catalog.Cursor.t array) option -> affinity:(int * int -> Sql.Value_ops.affinity option) -> membership:(Sql.Ast.select -> Sql.Private.Query.membership) -> params:Sql.Ast.value array -> (string * string list * (unit -> Catalog.Cursor.t)) list -> Ast.select -> emit:(Sql.Private.Row_core.row -> unit) -> bool (** Attempts VM execution over the supplied sources, declining before emission if compilation fails. *) val catalog_aggregate : R.t -> ?run_subquery: ([> `Exists | `In of Sql.Ir.comparison * Sql.Ast.value | `Scalar ] -> Sql.Ast.select -> (string * (string * Catalog.Value.t) list) list -> Sql.Ast.value) -> ?first:bool -> ?arrival:bool -> ?order:int list -> cat:Query.catalog -> scan:(unit -> Catalog.Cursor.t array) option -> affinity:(int * int -> Sql.Closure.affinity option) -> membership:(Sql.Ast.select -> Sql.Private.Query.membership) -> params:Catalog.Value.t array -> (string * string list * (unit -> Catalog.Cursor.t)) list -> Ast.select -> emit:(Sql.Row.t -> unit) -> bool (** [scan] is required for the reason given on {!catalog_run_with_infos}. *) val values_info : R.t -> Sql.Ast.value array -> Sql.Ast.expr list list -> string * string list * (unit -> Catalog.Cursor.t) (** A VALUES row source materialised into a cursor: each tuple's cells are constant expressions, bound under the unnamed alias [""] with columns [column1..columnN], the names {!Engine.run_select} gives them. *) val columns : Plan.key list -> string list (** The columns [keys] equates, and each with its value. *) val named : Where_loop.source -> (int * Where_loop.sought) list -> Plan.key list (** The keys of a step over [s], by the names the schema spells their columns with. *) val sorted : Where_loop.t -> Plan.access -> Where_loop.kind -> Plan.access * Plan.sort option (** [a] reading its rows in the order of loop [kind], and the sort of [q]'s rows after it. *) val rowid_backwards : Plan.access -> bool (** Whether [a] is a range of the rowid walked backwards, which the span cursor walks forwards: its rows come in ascending rowid order. *) val ordered : R.t -> ?where:Sql.Ast.expr -> reads:(string -> int list) -> string -> Ast.select -> Plan.access -> Plan.access * Plan.sort option * Plan.min_max option (** {!ordered_loop}, with a min/max query's order weighed for the loop it reads and none left to sort: the loop stops at its first row where it yields the argument's order, except through an IN list ({!keyed_by_list}). *) val join_conjuncts : Ast.select -> Plan.layout -> (Ast.expr * [> `Inner_on of int | `On of int | `Where ]) list (** The conjuncts of the WHERE of [s] and of its ONs, each with where it applies: an outer join's ON to its own source; where a RIGHT or FULL join is one of them, an inner join's ON to its own source too, which sqlite3 marks EP_InnerON; any other to the whole join. A NATURAL or USING join is planned as the ON it stands for, as sqlite3 plans it (sqlite3ProcessJoin). A written ON is a term whatever it holds, an ON 1 included, and a comma join adds none. *) val backwards : Plan.access -> Plan.access (** [a] walking its relation backwards, as an ORDER BY a loop answers in descending order needs: a walk of the rowid answers a key no two rows share, so its rows come in the same order whichever way it goes. *) val result_exprs : Ast.select -> Plan.layout -> Ast.expr list (** The result columns of [s] over [layout], a star standing for the columns it names. *) type join_order = { keys : (Ast.expr * bool) list; mode : Where_loop.mode; want_distinct : bool; limit : int option; checks : ((Ast.expr * bool) list * Where_loop.mode) list; finish : Where_loop.nested -> Plan.sort option * [ `Loop | `Temp of [ `Distinct | `Group_by ] ] option * int list; } val order_by : Ast.select -> (Ast.expr * bool) list (** The ordering terms of the select, or an empty list. *) val join_order : Ast.select -> Plan.layout -> join_order (** {!rows_order}, but for a min/max query, whose loop is asked for the order of its argument in place of an ORDER BY, under no LIMIT (select.c, minMaxQuery), and leaves nothing to sort. *) val outer_reads : Resolve.env -> Plan.layout -> Ast.expr -> int list (** The sources of [layout] [e] reads, through the subqueries it holds too: a column a subquery's own FROM does not bind is one of [layout]'s. *) val plan_facts : R.t -> ?where:Sql.Ast.expr -> reads:(string -> int list) -> read:int list array -> unit -> Plan.facts (** Builds source accesses and ordering facts for the select. *) type reads = { read : int list array; tables : string option list } val reads : R.t -> Ast.select -> Plan.layout -> reads (** The column positions read from each source of the select. *) val table_reads : reads -> Ident.t -> int list (** What the query reads from each table, as positions in that table's own columns. A path is offered per table and not per source, so two sources over one table share an answer: the union is what the index has to hold, which declines a covering read one of them alone could have had and never claims one neither can. *) val bound_keys : R.t -> ?where:Sql.Ast.expr -> reads:(string -> int list) -> Plan.access -> Plan.access (** [a] with the keys its path does not bind made inert, the path being the one the cursor reads ({!seek_index}): an IN key is dropped, since a seek walks the values of each IN it keys on and one it does not key on is a conjunct the loop tests, and a key answers no conjunct ({!Plan.key.answers}), since the rows the path yields need not satisfy it. *) val distinct_lines : R.t -> Ast.select -> plan:Plan.t option -> Eqp.t list (** The b-tree sqlite3 opens for each DISTINCT aggregate of [s] as it resets its accumulators, USE TEMP B-TREE FOR f(DISTINCT), in the order it analyses the aggregates, each once: the result columns', the ORDER BY's, the HAVING's (select.c, resetAccumulator). The lone aggregate of a select with no GROUP BY keeps none where the loop yields its argument unique or in order (fixDistinctOpenEph): the argument is the rowid of the one table read, or the next key of the index [plan] walks or seeks through. *) val with_outer_ctes : Ast.cte list -> first:int -> Ast.select -> int * Ast.select (** The subquery [sub] of a select whose WITH tables are [ctes], numbered from [first], with those tables as its own where it reads one it does not bind itself, as sqlite3 resolves a name against the WITH of each enclosing select (select.c, sqlite3WithPush): explained on its own, it would read an unknown table. The numbering starts that much earlier, so the subquery's own selects keep their numbers. *) val values_line : 'a list -> string (** The line sqlite3 scans a VALUES list of [rows] under: one row is a CONSTANT ROW, more a VALUES clause (parse.y, sqlite3MultiValues). *) val values_clause : Ast.select -> string option (** The VALUES clause [q] is where it is a bare list of more than one row, which sqlite3 reads in place of a subquery of it. *) val from_subqueries : Ast.select -> (int * string option * Ast.select) list (** The FROM subqueries of [s], each with its place among the items, the alias it is read under and its select; a table-valued function a join reads is held as a subquery of it and is none, and so is a VALUES clause the FROM reads. *) end