module J = Sql.Join let algorithm = Alcotest.testable J.pp J.equal (* The choice weighs two numbers, not one: the descents a seek pays against the rows a hash would hold. The same pair of options therefore goes one way for a pinned outer and the other for a scanned one -- and, against the same rewind count, one way for a small inner relation and the other for a large one. Covering by default, which is the shape a join on an indexed key takes when it reads nothing but that key: the entry the descent lands on is the answer. The last case below is the same numbers when it is not. *) let choose ?(seek_yield = 10) ?(covering = true) ~rewinds ~inner_rows () = J.choose ~rewinds ~inner_rows ~seek_yield ~covering ~seek:true ~hash:true let test_choose_from_cardinality () = Alcotest.check algorithm "a pinned outer seeks" J.Seek (choose ~rewinds:1 ~inner_rows:500 ()); (* one rewind per row of a relation the same size: the seek re-reads about ten rows for each row the hash would hold, so the hash wins *) Alcotest.check algorithm "an outer that rewinds per inner row hashes" J.Hash (choose ~rewinds:5_000 ~inner_rows:500 ()); (* the crossover moves with the inner relation, which is what a bound on [rewinds] alone could not express: 600 rewinds of a 600-row relation is a seek, and of a 60-row one a hash *) Alcotest.check algorithm "a large inner relation is worth seeking" J.Seek (choose ~rewinds:600 ~inner_rows:600 ()); Alcotest.check algorithm "a small one is not worth holding" J.Hash (choose ~rewinds:600 ~inner_rows:60 ()); (* and it moves with what one key selects: a seek that addresses a single row is kept far longer than one that reads back a bucket *) Alcotest.check algorithm "a bucket of ten hashes here" J.Hash (choose ~rewinds:1_000 ~inner_rows:500 ()); Alcotest.check algorithm "a unique key still seeks" J.Seek (choose ~seek_yield:1 ~rewinds:1_000 ~inner_rows:500 ()); (* and it moves with what reading one of those rows costs: the same 600 rewinds of the same 600-row relation go the other way where the path names the row rather than holding it, because each of the ten rows a key selects is then fetched again *) Alcotest.check algorithm "a path that must fetch its rows does not" J.Hash (choose ~covering:false ~rewinds:600 ~inner_rows:600 ()) (* With one option there is no choice to make, whatever the cardinality: a relation no index covers is hashed however large the outer, and one too large to hash is sought however often it is rewound. Neither falls back to the nested loop, which is the failure this is guarding -- a scan per outer row is the product. *) let test_choose_with_one_option () = let one ~seek ~hash = J.choose ~rewinds:1_000_000 ~inner_rows:10 ~seek_yield:10 ~covering:false ~seek ~hash in Alcotest.check algorithm "no index, large outer" J.Hash (one ~seek:false ~hash:true); Alcotest.check algorithm "no index, small outer" J.Hash (J.choose ~rewinds:1 ~inner_rows:10 ~seek_yield:10 ~covering:false ~seek:false ~hash:true); Alcotest.check algorithm "too large to hash" J.Seek (one ~seek:true ~hash:false); Alcotest.check algorithm "neither is available" J.Scan (one ~seek:false ~hash:false) (* Counting an inner relation costs leaf pages, so it stops where the answer does: far enough that no further row can turn the choice from a hash into a seek, and, where a source follows, far enough to estimate how often that one is rewound. *) let test_count_bound_is_where_the_answer_settles () = let settles ~next rewinds = let n = J.count_bound ~rewinds ~next in let at inner = J.choose ~rewinds ~inner_rows:inner ~seek_yield:10 ~covering:false ~seek:true ~hash:true in (* counted to the bound the choice is already the seek, so no further row can move it *) Alcotest.check algorithm (Fmt.str "%d rewinds settle by %d rows" rewinds n) J.Seek (at n); Alcotest.check algorithm (Fmt.str "%d rewinds are not settled one row short" rewinds) J.Hash (at (n - 1)) in List.iter (settles ~next:true) [ 500; 600; 2_000 ]; List.iter (settles ~next:false) [ 1; 500; 600; 2_000 ]; Alcotest.(check int) "a shallow loop still counts far enough to size the next source" J.max_rewinds (J.count_bound ~rewinds:1 ~next:true); (* the last source sizes nothing after it: one rewind settles at two rows, where (inner_rows + 1) * build = 36 first covers the 31 a ten-row non-covering seek costs *) Alcotest.(check int) "the last source of a shallow loop is counted two rows deep" 2 (J.count_bound ~rewinds:1 ~next:false); Alcotest.(check int) "and a rewind count of zero is a rewind count of one" (J.count_bound ~rewinds:1 ~next:true) (J.count_bound ~rewinds:0 ~next:true) (* Either limit alone stops a build, so a wide table is bounded by its cells and a table of large blobs by its bytes -- a row bound would let both through. *) let test_budget () = let b = J.budget in Alcotest.(check bool) "an empty build is within budget" false (J.over b ~cells:0 ~bytes:0); Alcotest.(check bool) "at the cell limit is still within" false (J.over b ~cells:b.J.cells ~bytes:0); Alcotest.(check bool) "one cell past it is over" true (J.over b ~cells:(b.J.cells + 1) ~bytes:0); Alcotest.(check bool) "one byte past the payload limit is over" true (J.over b ~cells:0 ~bytes:(b.J.bytes + 1)); (* a wide table: twenty thousand rows of 158 cells each *) Alcotest.(check bool) "a wide table is bounded by cells well under a million rows" true (J.over b ~cells:(20_000 * 158) ~bytes:0) let test_row_bytes () = Alcotest.(check int) "only text and blobs carry payload" 7 (J.row_bytes [ Catalog.Value.Int 1L; Text "abcd"; Null; Blob "xyz"; Float 1.5 ]); Alcotest.(check int) "an empty row costs nothing" 0 (J.row_bytes []) module C = Catalog.Spi open Catalog.Value let users : (string * C.Mem.table) list = [ ( "users", { C.Mem.columns = [ "id"; "name" ]; rows = [ [ Int 1L; Text "alice" ]; [ Int 2L; Text "bob" ]; [ Int 3L; Null ] ]; } ); ] let cat = Catalog.of_mem users (* An outer join's null-fill re-enters its own body with its cursor already off the end, so the fall-through out of that body reaches the loop's [Next] a second time. Every cursor has to answer false again rather than run off its rows or wake back up, or a preserving level either loops forever or reports a row twice. Each constructor the catalog offers is drained and then stepped twice more. *) let test_next_past_end () = let rows = [ [ Int 1L; Text "a" ]; [ Int 1L; Text "b" ] ] in let drain name (c : Catalog.Cursor.t) = let n = ref (if c.rewind () then 1 else 0) in while c.next () do incr n done; Alcotest.(check bool) (name ^ ": still exhausted one step past the end") false (c.next ()); Alcotest.(check bool) (name ^ ": and two steps past it") false (c.next ()); !n in ignore (drain "list_cursor" (C.list_cursor rows)); ignore (drain "table cursor" (C.cursor cat "users")); ignore (drain "empty list_cursor" (C.list_cursor [])); let outer = C.list_cursor rows in ignore (outer.rewind ()); ignore (drain "Join.probe" (Sql.Join.probe ~outer ~outer_key:0 ~inner:rows ~inner_key:0 ())) (* The tags a probe cursor offers, in the order it offers them. *) let offered (c : Catalog.Cursor.t) = let m = Catalog.Register.v () in let tag () = c.column 3 m; match Catalog.Register.to_value m with Text s -> s | _ -> "?" in if not (c.rewind ()) then [] else let out = ref [ tag () ] in while c.next () do out := tag () :: !out done; List.rev !out (* Stand [c] on its row [i]. *) let stand (c : Catalog.Cursor.t) i = ignore (c.rewind ()); for _ = 1 to i do ignore (c.next ()) done (* One repository's rows: the first key has one value, so only the further keys tell the rows apart. Each further key keeps its own rules: [a] compares under NOCASE and [b] under BINARY, integers equal the reals of their value, a text that spells a number is offered for it, and NULL matches nothing. *) let multi_inner = [ [ Text "r"; Text "ABC"; Text "k"; Text "n1" ]; [ Text "r"; Text "abc"; Text "K"; Text "n2" ]; [ Text "r"; Int 1L; Float 2.0; Text "n3" ]; [ Text "r"; Float 1.0; Text "2"; Text "n4" ]; [ Text "r"; Null; Int 2L; Text "n5" ]; [ Null; Int 1L; Int 2L; Text "n6" ]; [ Text "r"; Int 1L; Int 2L; Text "n7" ]; [ Text "r"; Int 1L; Int 3L; Text "n8" ]; ] let multi_outer = [ ([ Text "r"; Text "abc"; Text "k" ], [ "n1" ]); ([ Text "r"; Int 1L; Int 2L ], [ "n3"; "n4"; "n7" ]); ([ Text "r"; Float 1.0; Float 3.0 ], [ "n8" ]); ([ Text "r"; Null; Int 2L ], []); ([ Null; Int 1L; Int 2L ], []); ([ Text "s"; Int 1L; Int 2L ], []); ] let test_equi_join_probe_every_key () = let outer = C.list_cursor (List.map fst multi_outer) in let key outer_key collation = { Sql.Join.outer; outer_key; inner_key = outer_key; collation; nulls = false; } in let probe = Sql.Join.probe ~outer ~outer_key:0 ~inner:multi_inner ~inner_key:0 ~rest:[ key 1 (Some Sql.Collation.Nocase); key 2 None ] () in (* twice over, so the second pass reads the levels the first one built *) for pass = 1 to 2 do List.iteri (fun i (row, expected) -> stand outer i; Alcotest.(check (list string)) (Fmt.str "pass %d: %a" pass Fmt.(list ~sep:(any ", ") Catalog.Value.pp) row) expected (offered probe)) multi_outer done (* Each further key reads its value from its own cursor, which is how a join on columns of two earlier sources hashes on both. *) let test_equi_join_probe_keys_from_two_cursors () = let first = C.list_cursor [ [ Text "r"; Int 1L ] ] in let second = C.list_cursor [ [ Int 2L ]; [ Int 3L ]; [ Null ] ] in let probe = Sql.Join.probe ~outer:first ~outer_key:0 ~inner:multi_inner ~inner_key:0 ~rest: [ { Sql.Join.outer = first; outer_key = 1; inner_key = 1; collation = None; nulls = false; }; { Sql.Join.outer = second; outer_key = 0; inner_key = 2; collation = None; nulls = false; }; ] () in stand first 0; List.iteri (fun i expected -> stand second i; Alcotest.(check (list string)) (Fmt.str "second cursor on row %d" i) expected (offered probe)) [ [ "n3"; "n4"; "n7" ]; [ "n8" ]; [] ] (* Without further keys the probe is the single-key one: the first key alone decides, so every row of the one repository is offered. *) let test_equi_join_probe_one_key () = let outer = C.list_cursor [ [ Text "r" ] ] in stand outer 0; Alcotest.(check (list string)) "every row carrying the key, in table order" [ "n1"; "n2"; "n3"; "n4"; "n5"; "n7"; "n8" ] (offered (Sql.Join.probe ~outer ~outer_key:0 ~inner:multi_inner ~inner_key:0 ())) (* Join.ordered puts each bucket a probe offers in the order of its [by] columns, then of its rows, as sqlite3's automatic index holds a key's entries (where.c, constructAutomaticIndex). A sort of each key's rows that kept the scan's order, or one bucket sorted before another's rows, offers them otherwise. *) let ordered_inner = [ [ Int 2L; Text "b"; Null; Text "r1" ]; [ Int 1L; Text "c"; Null; Text "r2" ]; [ Int 2L; Text "a"; Null; Text "r3" ]; [ Int 1L; Text "a"; Null; Text "r4" ]; [ Float 2.0; Text "a"; Null; Text "r5" ]; [ Text "X"; Text "b"; Null; Text "r6" ]; [ Text "x"; Text "a"; Null; Text "r7" ]; ] let probe_ordered ?collation inner keys = let outer = C.list_cursor (List.map (fun k -> [ k ]) keys) in let probe = Sql.Join.probe ?collation ~outer ~outer_key:0 ~inner:(Sql.Join.ordered ?collation ~key:(Some 0) ~by:[ 1 ] inner) ~inner_key:0 () in List.mapi (fun i _ -> stand outer i; offered probe) keys let test_ordered_buckets () = Alcotest.(check (list (list string))) "each key's rows by the column, then in place order" [ [ "r3"; "r5"; "r1" ]; [ "r4"; "r2" ]; [ "r7"; "r6" ] ] (probe_ordered ~collation:Sql.Collation.Nocase ordered_inner [ Int 2L; Int 1L; Text "X" ]); (* a text that spells 2 is offered with 2's rows: the bucket of both is in the column's order too *) let inner = ordered_inner @ [ [ Text "2"; Text "a"; Null; Text "r8" ] ] in Alcotest.(check (list (list string))) "a number's rows with the texts that spell it" [ [ "r3"; "r5"; "r8"; "r1" ]; [ "r3"; "r5"; "r8"; "r1" ] ] (probe_ordered inner [ Int 2L; Text "2" ]); Alcotest.(check (list string)) "with no key, every row is one bucket" [ "r3"; "r4"; "r5"; "r7"; "r1"; "r6"; "r2" ] (List.map (fun row -> match List.nth row 3 with Catalog.Value.Text s -> s | _ -> "?") (Sql.Join.ordered ~key:None ~by:[ 1 ] ordered_inner)) let suite = ( "join", [ Alcotest.test_case "the algorithm follows the cardinality" `Quick test_choose_from_cardinality; Alcotest.test_case "one option leaves no choice" `Quick test_choose_with_one_option; Alcotest.test_case "count_bound is where the answer settles" `Quick test_count_bound_is_where_the_answer_settles; Alcotest.test_case "the build budget binds on either limit" `Quick test_budget; Alcotest.test_case "row_bytes counts payload" `Quick test_row_bytes; Alcotest.test_case "a cursor stays exhausted past its end" `Quick test_next_past_end; Alcotest.test_case "Join.probe on one key" `Quick test_equi_join_probe_one_key; Alcotest.test_case "Join.probe on every key" `Quick test_equi_join_probe_every_key; Alcotest.test_case "Join.probe keys from two cursors" `Quick test_equi_join_probe_keys_from_two_cursors; Alcotest.test_case "Join.ordered orders each bucket" `Quick test_ordered_buckets; ] )