Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111(** SQLite interop: the text a REAL renders to, and the double a literal reads as.
Exact interop with SQLite is the point of {!Sql.Decimal}, and the last place either direction can be checked is the decimal itself: SQLite renders a REAL through neither the platform's [printf] nor the shortest decimal that reads back, and reads a literal through neither the platform's [strtod] nor a correct rounding, so agreeing on a few round numbers proves nothing. Every row carries a double as its IEEE 754 bit pattern, so no parser sits between the value and the oracle.
- [traces/reals.txt] and [traces/subnormals.txt] hold one double per row and the text [sqlite3_snprintf("%!.17g", r)] renders it as -- the call [sqlite3VdbeMemStringify] makes, so the text is what a query returns. Subnormals get their own population because that is where SQLite's decimal decode, biased low by up to [2 ** -64] relative, and a correctly rounded one pick different 17th digits. - [traces/literals.txt] holds one REAL literal per row and the double [sqlite3_column_double] over [SELECT <literal>] reads it as. A literal wider than the 64-bit significand [sqlite3AtoF] accumulates into pins the truncation SQLite does there, where a correct rounding lands one ulp away.
Oracle: the SQLite amalgamation pinned by URL and SHA-256 in [scripts/generate.sh], whose version and source id the regeneration records in [traces/sqlite-version.txt].
Regenerate the traces with [REGEN=1 dune build @traces]. *)
type row = { line : int; left : string; right : string }
(* Every trace is two space-separated fields, input then expected. *)let rows path = let text = Eio.Path.load path in let lines = String.split_on_char '\n' text in List.concat (List.mapi (fun n l -> if String.length l = 0 || l.[0] = '#' then [] else match String.index_opt l ' ' with | None -> Alcotest.failf "%a:%d: no space in %S" Eio.Path.pp path (n + 1) l | Some i -> let left = String.sub l 0 i in let right = String.sub l (i + 1) (String.length l - i - 1) in [ { line = n + 1; left; right } ]) lines)
let bits_of_hex s = Int64.of_string ("0x" ^ s)
(* The rendering direction: the trace's bit pattern, rendered by us, must be the text the oracle rendered it as. *)let test_rendering traces name ~least () = let path = Eio.Path.(traces / name) in let rows = rows path in Alcotest.(check bool) (Fmt.str "%a has at least %d rows" Eio.Path.pp path least) true (List.length rows >= least); List.iter (fun { line; left; right } -> let bits = bits_of_hex left in Alcotest.(check string) (Fmt.str "%a:%d %016Lx" Eio.Path.pp path line bits) right (Sql.Value_ops.text_of_value (Catalog.Value.Float (Int64.float_of_bits bits)))) rows
(* The reading direction: the trace's literal, read by us, must be the double the oracle read it as -- compared as bit patterns, so a sign of zero and a one-ulp miss both show up. *)let test_literals traces () = let path = Eio.Path.(traces / "literals.txt") in let rows = rows path in Alcotest.(check bool) (Fmt.str "%a has at least %d rows" Eio.Path.pp path 1900) true (List.length rows >= 1900); List.iter (fun { line; left; right } -> let where = Fmt.str "%a:%d %s" Eio.Path.pp path line left in match Sql.Value_ops.leading_number left with | Catalog.Value.Float f -> Alcotest.(check string) where (Fmt.str "%016Lx" (bits_of_hex right)) (Fmt.str "%016Lx" (Int64.bits_of_float f)) | v -> Alcotest.failf "%s: read as %a, not a REAL" where Catalog.Value.pp v) rows
let () = Eio_main.run @@ fun env -> let traces = Eio.Path.(Eio.Stdenv.cwd env / "traces") in Alcotest.run "sql-sqlite3-interop" [ ( "reals", [ Alcotest.test_case "rendering matches sqlite3" `Quick (test_rendering traces "reals.txt" ~least:3000); Alcotest.test_case "subnormal rendering matches sqlite3" `Quick (test_rendering traces "subnormals.txt" ~least:1900); ] ); ( "literals", [ Alcotest.test_case "reading matches sqlite3" `Quick (test_literals traces); ] ); ]