Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
module Register = Catalog.Registermodule Value = Catalog.Value
(* Every row stands on registers: the VM's own for a streamed row, fresh ones for a row of values another path computed. *)type row = { mutable live : bool; mutable regs : Register.t array; mutable base : int; mutable count : int;}
let v () = { live = false; regs = [||]; base = 0; count = 0 }
let[@inline never] refuse r i = if not r.live then invalid_arg "Sql.Row: a row read outside its call" else Fmt.invalid_arg "Sql.Row: column %d of a row of %d" i r.count
(* The register of column [i]. [base + count] is within [regs], as the VM hands its result span over, so the check on [i] is the bounds check. *)let[@inline] mem r i = if r.live && i >= 0 && i < r.count then Array.unsafe_get r.regs (r.base + i) else refuse r i
let length r = if r.live then r.count else refuse r 0let value r i = Register.to_value (mem r i)
let to_list r = let n = length r in let rec take k acc = if k < 0 then acc else take (k - 1) (value r k :: acc) in take (n - 1) []
(* The row goes dead however [f] ends, so a row kept past its call is refused rather than read from registers the scan has since overwritten. *)let with_regs r regs base count f = r.live <- true; r.regs <- regs; r.base <- base; r.count <- count; match f r with | () -> r.live <- false | exception e -> let bt = Printexc.get_raw_backtrace () in r.live <- false; Printexc.raise_with_backtrace e bt
let with_values vs f = let regs = Array.of_list (List.map (fun v -> let m = Register.v () in Register.set_value m v; m) vs) in with_regs (v ()) regs 0 (Array.length regs) f
module type SEMANTICS = sig val to_int64 : Value.t -> int64 val to_float : Value.t -> float val to_text : Value.t -> stringend
module type S = sig type t = row type kind = [ `Null | `Integer | `Real | `Text | `Blob ]
val length : t -> int val kind : t -> int -> kind val int64 : t -> int -> int64 val int : t -> int -> int val float : t -> int -> float val text : t -> int -> string val bytes : t -> int -> int val blit : t -> int -> off:int -> Bytes.t -> pos:int -> len:int -> unit val value : t -> int -> Value.t val to_list : t -> Value.t list val v : unit -> t val with_regs : t -> Register.t array -> int -> int -> (t -> unit) -> unit val with_values : Value.t list -> (t -> unit) -> unitend
module Make (Sem : SEMANTICS) = struct type t = row type kind = [ `Null | `Integer | `Real | `Text | `Blob ]
let length = length let value = value let to_list = to_list let v = v let with_regs = with_regs let with_values = with_values
let kind r i : kind = match (mem r i).kind with | Null -> `Null | Int | Bigint -> `Integer | Real -> `Real | Text -> `Text | Blob -> `Blob
let int64 r i = let m = mem r i in match m.kind with | Int -> Int64.of_int m.i | Bigint -> m.big | Real | Null | Text | Blob -> Sem.to_int64 (Register.to_value m)
let[@inline never] outside n = Fmt.invalid_arg "Sql.Row.int: %Ld is outside the native range" n
let int r i = let m = mem r i in match m.kind with | Int -> m.i | _ -> let n = int64 r i in if Int64.compare n (Int64.of_int min_int) < 0 || Int64.compare n (Int64.of_int max_int) > 0 then outside n else Int64.to_int n
let float r i = let m = mem r i in match m.kind with | Real -> m.r | Int -> Float.of_int m.i | Bigint -> Int64.to_float m.big | Null | Text | Blob -> Sem.to_float (Register.to_value m)
let text r i = let m = mem r i in match m.kind with | Text | Blob -> Register.contents m | Null | Int | Bigint | Real -> Sem.to_text (Register.to_value m)
(* a text or a blob is measured and copied out where its register holds it; another kind is the text it renders as *) let bytes r i = let m = mem r i in match m.kind with | Text | Blob -> Register.length m | Null | Int | Bigint | Real -> String.length (text r i)
let blit r i ~off dst ~pos ~len = let m = mem r i in let s, n = match m.kind with | Text | Blob -> (Register.view m, Register.length m) | Null | Int | Bigint | Real -> let s = text r i in (s, String.length s) in if off < 0 || len < 0 || pos < 0 || off > n - len || pos > Bytes.length dst - len then invalid_arg "Sql.Row.blit: range outside the cell or the buffer"; Bytes.blit_string s off dst pos lenend[@@inline]