Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250(* Price a bulk load through the typed insert: one transaction of rows into a table of 5 or of 14 columns, in a file whose schema holds many other tables and indexes, as a loader of a wide database runs it. Prints the CPU and the minor words a row costs and a digest of the file written, which two builds of the engine must agree on. Usage: typed_insert.exe SHAPE [rows] [door] [tables] [path]; SHAPE is flows (5 columns, one index) or brackets (14 columns, a four-column primary key); door is typed (Sqlite.insert, the default) or values (Sqlite.Table.insert); tables is how many other tables the schema holds (default 58, every second one indexed). MEMTRACE=ins.ctf ... then trace_top.exe ins.ctf. *)
let () = Observe.Memtrace.trace_if_requested ()
let arg i fallback = if Array.length Sys.argv <= i then fallback else match int_of_string_opt Sys.argv.(i) with | Some n -> n | None -> invalid_arg ("typed_insert: not a count: " ^ Sys.argv.(i))
let word i fallback = if Array.length Sys.argv <= i then fallback else Sys.argv.(i)
let shape = word 1 "flows"let rows = arg 2 1_000_000let door = word 3 "typed"let others = arg 4 58
type flow = { edition : int64; cog : int64; from_code : string; to_code : string; workers : float option;}
let flows = let open Sql in Table.map "flows" (fun edition cog from_code to_code workers -> { edition; cog; from_code; to_code; workers }) |> Table.col (Col.v "edition" int (fun r -> r.edition)) |> Table.col (Col.v "cog" int (fun r -> r.cog)) |> Table.col (Col.v "from_code" text (fun r -> r.from_code)) |> Table.col (Col.v "to_code" text (fun r -> r.to_code)) |> Table.col (Col.v "workers" (nullable float) (fun r -> r.workers)) |> Table.seal
let flows_sql = [ "CREATE TABLE flows (edition INTEGER NOT NULL, cog INTEGER NOT NULL, \ from_code TEXT NOT NULL, to_code TEXT NOT NULL, workers REAL)"; "CREATE INDEX flows_edition ON flows (edition)"; ]
let code i = Fmt.str "%05d" (i mod 35_000)
let flow i = { edition = Int64.of_int (2015 + (i / 500_000)); cog = 2024L; from_code = code i; to_code = code (i * 7); workers = (if i mod 11 = 0 then None else Some (float (i mod 977) /. 4.)); }
let flow_values (r : flow) = Sqlite. [ Int r.edition; Int r.cog; Text r.from_code; Text r.to_code; (match r.workers with Some f -> Float f | None -> Null); ]
type bracket = { code : string; bracket_set : string; year : int64; lower : int64; upper : int64 option; households : int64 option; taxed : int64 option; reference_income : float option; net_tax : float option; taxed_income : float option; salaries_households : int64 option; salaries : float option; pensions_households : int64 option; pensions : float option;}
let bracket_of code bracket_set year lower upper households taxed reference_income net_tax taxed_income salaries_households salaries pensions_households pensions = { code; bracket_set; year; lower; upper; households; taxed; reference_income; net_tax; taxed_income; salaries_households; salaries; pensions_households; pensions; }
let brackets = let open Sql in let int_opt name get = Col.v name (nullable int) get and float_opt name get = Col.v name (nullable float) get in Table.map "brackets" bracket_of |> Table.col (Col.v "code" text (fun r -> r.code)) |> Table.col (Col.v "bracket_set" text (fun r -> r.bracket_set)) |> Table.col (Col.v "year" int (fun r -> r.year)) |> Table.col (Col.v "lower" int (fun r -> r.lower)) |> Table.col (int_opt "upper" (fun r -> r.upper)) |> Table.col (int_opt "households" (fun r -> r.households)) |> Table.col (int_opt "taxed" (fun r -> r.taxed)) |> Table.col (float_opt "reference_income" (fun r -> r.reference_income)) |> Table.col (float_opt "net_tax" (fun r -> r.net_tax)) |> Table.col (float_opt "taxed_income" (fun r -> r.taxed_income)) |> Table.col (int_opt "salaries_households" (fun r -> r.salaries_households)) |> Table.col (float_opt "salaries" (fun r -> r.salaries)) |> Table.col (int_opt "pensions_households" (fun r -> r.pensions_households)) |> Table.col (float_opt "pensions" (fun r -> r.pensions)) |> Table.seal
let brackets_sql = [ "CREATE TABLE brackets (code TEXT NOT NULL, bracket_set TEXT NOT NULL, \ year INTEGER NOT NULL, lower INTEGER NOT NULL, upper INTEGER, households \ INTEGER, taxed INTEGER, reference_income REAL, net_tax REAL, taxed_income \ REAL, salaries_households INTEGER, salaries REAL, pensions_households \ INTEGER, pensions REAL, PRIMARY KEY (code, bracket_set, year, lower))"; ]
let bracket i = let some_int k = if i mod 13 = 0 then None else Some (Int64.of_int k) and some_float k = if i mod 13 = 0 then None else Some (float k /. 8.) in { code = code (i / 8); bracket_set = (if i / 280_000 mod 2 = 0 then "fine" else "coarse"); year = Int64.of_int (2011 + (i / 560_000)); lower = Int64.of_int (i mod 8 * 10_000); upper = (if i mod 8 = 7 then None else Some (Int64.of_int ((i mod 8) + 1))); households = some_int (i mod 4_001); taxed = some_int (i mod 1_999); reference_income = some_float (i mod 90_001); net_tax = some_float (i mod 7_001); taxed_income = some_float (i mod 60_013); salaries_households = some_int (i mod 3_001); salaries = some_float (i mod 50_021); pensions_households = some_int (i mod 1_009); pensions = some_float (i mod 20_011); }
let bracket_values (r : bracket) = let int = function Some i -> Sqlite.Int i | None -> Sqlite.Null and float = function Some f -> Sqlite.Float f | None -> Sqlite.Null in Sqlite. [ Text r.code; Text r.bracket_set; Int r.year; Int r.lower; int r.upper; int r.households; int r.taxed; float r.reference_income; float r.net_tax; float r.taxed_income; int r.salaries_households; float r.salaries; int r.pensions_households; float r.pensions; ]
(* The rest of the schema: tables no row of this load goes to, every second one with an index. *)let others_sql = List.concat (List.init others (fun i -> Fmt.str "CREATE TABLE other_%02d (code TEXT NOT NULL, year INTEGER NOT \ NULL, a REAL, b REAL, c INTEGER, d TEXT)" i :: (if i mod 2 = 0 then [ Fmt.str "CREATE INDEX other_%02d_code ON other_%02d (code)" i i ] else [])))
let load db = let each f = for i = 0 to rows - 1 do f i done in match (shape, door) with | "flows", "typed" -> each (fun i -> Sqlite.insert db flows (flow i)) | "brackets", "typed" -> each (fun i -> Sqlite.insert db brackets (bracket i)) | "flows", "values" -> each (fun i -> ignore (Sqlite.Table.insert db ~table:"flows" (flow_values (flow i)) : int64 option)) | "brackets", "values" -> each (fun i -> ignore (Sqlite.Table.insert db ~table:"brackets" (bracket_values (bracket i)) : int64 option)) | _ -> invalid_arg "typed_insert: SHAPE is flows or brackets, door typed or values"
let () = Eio_main.run @@ fun env -> let path = if Array.length Sys.argv > 5 then Sys.argv.(5) else Filename.temp_file "typed_insert" ".db" in if Sys.file_exists path then Sys.remove path; let words, cpu = Eio.Switch.run @@ fun sw -> let db = Sqlite.open_ ~sw ~synchronous:`Off Eio.Path.(Eio.Stdenv.fs env / path) in List.iter (fun sql -> Sqlite.Raw.exec db sql ~params:[]) (others_sql @ flows_sql @ brackets_sql); let w0 = Gc.minor_words () and c0 = Unix.times () in Sqlite.with_transaction db (fun () -> load db); let w1 = Gc.minor_words () and c1 = Unix.times () in Sqlite.close db; (w1 -. w0, c1.tms_utime -. c0.tms_utime +. (c1.tms_stime -. c0.tms_stime)) in let n = float rows in Fmt.pr "%s %s rows=%d others=%d cpu=%.2fs us/row=%.2f minor_words/row=%.0f@." shape door rows others cpu (cpu *. 1e6 /. n) (words /. n); Fmt.pr "file %s@." Digest.BLAKE256.(to_hex (file path)); if Array.length Sys.argv <= 5 then Sys.remove path