Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(* A port of sqlite/src/analyze.c's StatAccum under SQLITE_ENABLE_STAT4: statInit, statPush, samplePushPrevious, sampleInsert, sampleIsBetter and the STAT_GET_ROWID walk of statGet. Counts are tRowcnt (u64) there; an OCaml int holds every count an index can reach. *)
type 'row slot = { eq : int array; lt : int array; dlt : int array; mutable row : 'row option; mutable periodic : bool; mutable col : int; (** the column whose prefix count put it here *) mutable hash : int; (** a u32 tie-breaker *)}
type 'row t = { ncol : int; periodic_every : int; mutable prn : int; mutable nrow : int; current : 'row slot; best : 'row slot array; mutable a : 'row slot array; (** the first [nsample] are kept *) mutable nsample : int; mutable imin : int; mutable max_eq_zero : int;}
type 'row sample = { neq : int array; nlt : int array; ndlt : int array; row : 'row;}
(* SQLITE_STAT4_SAMPLES *)let max_samples = 24let u32 x = x land 0xffff_ffff
let slot ncol col = { eq = Array.make ncol 0; lt = Array.make ncol 0; dlt = Array.make ncol 0; row = None; periodic = false; col; hash = 0; }
let copy ncol ~src ~dst = dst.periodic <- src.periodic; dst.col <- src.col; dst.hash <- src.hash; Array.blit src.eq 0 dst.eq 0 ncol; Array.blit src.lt 0 dst.lt 0 ncol; Array.blit src.dlt 0 dst.dlt 0 ncol; dst.row <- src.row
let clone ncol s = let d = slot ncol 0 in copy ncol ~src:s ~dst:d; d
(* statInit: nEst is the exact entry count (OP_Count with P3 0 under STAT4), and the seed reads it through sqlite3_value_int, its low 32 bits. *)let v ~columns ~entries = let ncol = columns in { ncol; periodic_every = (entries / ((max_samples / 3) + 1)) + 1; prn = u32 (0x689e962d * ncol lxor (0xd0944565 * u32 entries)); nrow = 0; current = slot ncol 0; best = Array.init ncol (fun i -> slot ncol i); a = [||]; nsample = 0; imin = 0; max_eq_zero = 0; }
(* sampleIsBetterPost: equal on [col], prefer more repeats on the longer prefixes, then the larger hash. *)let better_post t n o = let rec go i = if i >= t.ncol then n.hash > o.hash else if n.eq.(i) > o.eq.(i) then true else if n.eq.(i) < o.eq.(i) then false else go (i + 1) in go (n.col + 1)
(* sampleIsBetter *)let better t n o = let neq = n.eq.(n.col) and oeq = o.eq.(o.col) in if neq > oeq then true else if neq = oeq then if n.col < o.col then true else n.col = o.col && better_post t n o else false
(* The kept sample the next insertion evicts: the worst of those not periodic, once all [max_samples] are kept. *)let settle_min t = if t.nsample >= max_samples then begin let imin = ref (-1) in for i = 0 to max_samples - 1 do if not t.a.(i).periodic then if !imin < 0 || better t t.a.(!imin) t.a.(i) then imin := i done; t.imin <- !imin end
(* What sampleInsert does with [s], not periodic: a kept sample sharing the prefix that ends at [s.col] is promoted rather than [s] added, the best of them; and [s] is dropped if a periodic one shares it. *)let promotion t s = let rec go i up = if i < 0 then match up with Some u -> `Upgrade u | None -> `Insert else let o = t.a.(i) in if o.eq.(s.col) <> 0 then go (i - 1) up else if o.periodic then `Drop else match up with | Some u when not (better t o u) -> go (i - 1) up | _ -> go (i - 1) (Some o) in go (t.nsample - 1) None
(* sampleInsert *)let insert t (s : 'row slot) eq_zero = if eq_zero > t.max_eq_zero then t.max_eq_zero <- eq_zero; match if s.periodic then `Insert else promotion t s with | `Drop -> () | `Upgrade u -> u.col <- s.col; u.eq.(u.col) <- s.eq.(u.col); settle_min t | `Insert -> if t.nsample >= max_samples then begin (* drop sample [imin], keeping the others in order *) let im = t.imin in let dropped = t.a.(im) in Array.blit t.a (im + 1) t.a im (t.nsample - im - 1); t.a.(t.nsample - 1) <- dropped; t.nsample <- max_samples - 1 end; let d = t.a.(t.nsample) in copy t.ncol ~src:s ~dst:d; t.nsample <- t.nsample + 1; Array.fill d.eq 0 eq_zero 0; settle_min t
(* samplePushPrevious: column [chng] of the entry just pushed differs from the previous one, whose counts from [chng] on are now final. *)let push_previous t chng = for i = t.ncol - 2 downto chng do let b = t.best.(i) in b.eq.(i) <- t.current.eq.(i); if t.nsample < max_samples || better t b t.a.(t.imin) then insert t b i done; if chng < t.max_eq_zero then begin for i = t.nsample - 1 downto 0 do let s = t.a.(i) in for j = chng to t.ncol - 1 do if s.eq.(j) = 0 then s.eq.(j) <- t.current.eq.(j) done done; t.max_eq_zero <- chng end
(* statPush *)let push t ~changed row = let c = t.current in (* the first entry is pushed as changed at column 0 *) let changed = if t.nrow = 0 then 0 else changed in if Array.length t.a = 0 then t.a <- Array.init max_samples (fun _ -> slot t.ncol 0); if t.nrow = 0 then Array.fill c.eq 0 t.ncol 1 else begin push_previous t changed; for i = 0 to changed - 1 do c.eq.(i) <- c.eq.(i) + 1 done; for i = changed to t.ncol - 1 do c.dlt.(i) <- c.dlt.(i) + 1; c.lt.(i) <- c.lt.(i) + c.eq.(i); c.eq.(i) <- 1 done end; t.nrow <- t.nrow + 1; c.row <- Some row; t.prn <- u32 ((t.prn * 1103515245) + 12345); c.hash <- t.prn; let nlt = c.lt.(t.ncol - 1) in if nlt / t.periodic_every <> (nlt + 1) / t.periodic_every then begin c.periodic <- true; c.col <- 0; insert t c (t.ncol - 1); c.periodic <- false end; for i = 0 to t.ncol - 2 do c.col <- i; if i >= changed || better_post t c t.best.(i) then copy t.ncol ~src:c ~dst:t.best.(i) done
let samples t = if t.nrow = 0 then [] else begin push_previous t 0; List.init t.nsample (fun i -> let s = clone t.ncol t.a.(i) in match s.row with | Some row -> { neq = s.eq; nlt = s.lt; ndlt = s.dlt; row } | None -> invalid_arg "Stat4.samples: a sample names no row") end
let counts a = String.concat " " (Array.to_list (Array.map string_of_int a))