Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Queries compiled to closures, over a dialect's value semantics.
A plan is turned, once, into a tree of OCaml closures, each node the closure that evaluates it over the closures of its children (Feeley and Lapalme, {e Using closures for code generation}, 1987), so a row costs direct calls and no opcode dispatch. Columns are read through {!Catalog.Cursor.reader}s, and a comparison or a [MIN]/[MAX] over integers or reals runs unboxed on the rows that hold that class, and a comparison with a text or a blob that the affinity leaves as it is reads the row's bytes where its source holds them, each where the semantics says the class orders so ({!SEMANTICS}). A row of any other class takes the path the VM takes, so the answer is the VM's.
The shapes compiled so far read one source, through a [WHERE] of comparisons, of [IS NULL] and [IS NOT NULL] and of a bare value, joined by [AND] and [OR]. Their operands, and the expressions a select projects, are columns, the rowid, literals, bound parameters, and the arithmetic, [CAST], comparison values, [IS], [IN], [NOT], [IS NULL] values and function calls over them; a function, a cast or a comparison value is the operation the VM's program applies, from the semantics or the run's {!Ir_core.S.type-hooks}, called as many times and in the same order. Two columns compared hold integers or reals compared unboxed. A select has a [LIMIT] and no [ORDER BY], [DISTINCT] or [OFFSET]. An aggregate is grouped by nothing or by one column under no collating sequence, with no bare column, and folds [COUNT] of the rows, and [COUNT], [MIN], [MAX], [SUM], [TOTAL] and [AVG] over a column, with no [DISTINCT], [FILTER] or [ORDER BY]. Every other plan runs on the VM. The compiled folds are the semantics' {!Semantics.S.aggregate}, whose [SUM], [TOTAL] and [AVG] are its {!SEMANTICS.Sum}. *)
(** The type for what the unboxed paths read of a dialect's semantics, beside its value semantics. *)module type SEMANTICS = sig include Semantics.S
val by_class : affinity option -> Catalog.Register.kind -> bool (** [by_class aff k] is [true] iff, under [aff], two values both of the storage class [k] compare as the class orders them unboxed: integers by [Int.compare], reals by [Float.compare], texts and blobs by their bytes. *)
val above_numbers : affinity option -> string -> bool (** [above_numbers aff s] is [true] iff, under [aff], the text [s] stays text and compares above every number, and a text compares below it exactly when {!spells_number} holds of it, by bytes otherwise. *)
val spells_number : string -> first:int -> length:int -> bool (** [spells_number s ~first ~length] is [true] iff the [length] bytes of [s] from [first] are a text a converting affinity reads as a number. *)
val stores_reals : affinity -> bool (** [stores_reals aff] is [true] iff a column of affinity [aff] holds its numbers as reals, which picks the class a [MIN] or [MAX] reads unboxed first. A wrong answer costs speed, never the answer. *)
(** The fold of [SUM], [TOTAL] and [AVG], the one {!Semantics.S.aggregate} runs for those names. *) module Sum : sig type t (** The type for one fold in progress. *)
val v : string -> t (** [v name] is an empty fold of [name]. *)
val add : t -> Catalog.Value.t -> unit (** [add s v] folds the non-NULL value [v] into [s]. *)
val add_real : t -> float -> unit (** [add_real s x] folds the real [x] into [s]. *)
val add_ints : t -> sum:int -> count:int -> unit (** [add_ints s ~sum ~count] folds [count] integers whose sum is [sum]. *)
val finish : t -> Catalog.Value.t (** [finish s] is the value of the fold. *) endend
(** The type for queries compiled to closures. *)module type S = sig type affinity (** The type for a column's affinity, the semantics'. *)
type hooks (** The type for a run's hooks ({!Ir_core.S.type-hooks}). *)
type query (** The type for a select ({!Ir_core.S.type-query}). *)
type grouped (** The type for a grouped select ({!Ir_core.S.type-grouped}). *)
type program (** The type for the VM's programs. *)
(** The storage classes an unboxed path reads, as witnesses of the OCaml type that holds them. *) type _ ty = Int : int ty | Real : float ty
val run_grouped : ?hooks:hooks -> ?arrival:bool -> ?params:Catalog.Value.t array -> ?first:bool -> ?keep:(Catalog.Value.t -> bool) -> ?affinity:(int * int -> affinity option) -> grouped -> Catalog.Cursor.t array -> Catalog.Value.t list list (** [run_grouped g cursors] is {!Ir_core.S.run_grouped}[ ?arrival ?first g cursors], compiled to closures when [g] has a shape this module compiles and [first] is not set, and run on the VM otherwise; the choice is made before a cursor is stepped. With [arrival] the rows of each group come one after another, as a loop that yields the GROUP BY's order reads them, and the compiled run holds one group at a time. [affinity (source, column)] is the affinity of a column, which picks the class a [MIN] or [MAX] reads unboxed first (default: none, integers). *)
type plan (** The type for a select made ready to run: where the select has a shape this module compiles, its closures, staged so that a run builds them without reading the query again, and its VM program, coded when a run first takes the VM. *)
val plan : ?hooks:hooks -> query -> plan (** [plan q] is the choice of the path a run of [q] takes, made once here: a select of a shape this module compiles runs as closures, any other on its VM program, {!Ir_core.S.precompile}[ q], coded here.
@raise Ir_core.S.Unsupported where [q] has no such shape and {!Ir_core.S.precompile} raises it. *)
val order : plan -> int list (** [order p] is the order the loop of [p] enters its cursors in ({!Ir_core.S.select.order}): a run of [p] is over cursors built in it. *)
val compiled : plan -> bool (** [compiled p] is [true] iff the runs of [p] over one cursor take the closures rather than the VM program. *)
val iter_rows : ?params:Catalog.Value.t array -> ?regs:(program -> Catalog.Register.t array) -> plan -> Catalog.Cursor.t array -> f:(Row_core.row -> unit) -> unit (** [iter_rows ?params ?regs p cursors ~f] is {!Ir_core.S.iter_rows}[ ?params ?regs:(regs prog) q prog cursors ~f] for the query [q] of [p] and its program [prog], run as closures where {!plan} chose them and on the VM otherwise. A compiled run hands each row to [f] as the scan reaches it, and ends at a [LIMIT]'s bound. *)end
(** [Make (Sem) (I)] compiles [I]'s queries to closures under [Sem]. *)module Make (Sem : SEMANTICS) (I : Ir_core.S with type affinity = Sem.affinity and type collation = Sem.collation and type op = Sem.op and type raise_action = Sem.action) : S with type affinity = Sem.affinity and type hooks = I.hooks and type query = I.query and type grouped = I.grouped and type program = I.program