(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* Tests for {!Sqlite.Private.Where_loop}: the loop sqlite3 3.54.1 chooses for [CREATE TABLE t (a, b, c, UNIQUE (b))], as its EXPLAIN QUERY PLAN prints it for each select below, and the ORDER BY terms that loop yields in order. *) module W = Sqlite.Private.Where_loop module Model = Sqlite.Private.Model module Decl = Sqlite.Private.Decl let gt = let pager = Btree.Pager.mem ~page_size:4096 () in let schema = Decl.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a, b, c, UNIQUE (b))" in { Model.store = Model.Rowid_rows (Btree.Table.v pager); schema; master_rowid = 1L; indexes = Sqlite.Private.Index.autoindexes pager ~tbl_name:"t" ~first_rowid:2L ~trailing:(fun _ -> Some Model.Rowid_ref) (List.filter_map Result.to_option (Decl.parse_unique_constraints schema)); unusable = []; explicit = []; } let select sql = match Sql.Private.Lexer.parse_statements sql with | Ok [ Sql.Ast.Select s ] -> s | _ -> Alcotest.failf "not one select: %s" sql let kind = function | W.Scan -> "scan" | W.One_row -> "one row" | W.Rowid_in -> "rowid in" | W.Rowid_range _ -> "rowid range" | W.Full idx -> "walk " ^ idx.Model.name | W.Search { index; _ } -> "search " ^ index.Model.name | W.Pk_search _ -> "search the primary key" let loop ?(covers = false) sql = match W.v gt ~names:[ "a"; "b"; "c" ] ~alias:"t" ~covers:(fun _ -> covers) ~subquery:(fun _ -> Some None) (select sql) with | Some t -> t | None -> Alcotest.failf "not modelled: %s" sql let check ?covers sql ~best ~sorted ~reverse = let t = loop ?covers sql in let b = W.best t in Alcotest.(check string) (sql ^ ": loop") best (kind b); let o = W.order t b in Alcotest.(check int) (sql ^ ": terms in order") sorted o.sorted; Alcotest.(check bool) (sql ^ ": backwards") reverse o.reverse let index = "sqlite_autoindex_t_1" (* "SCAN t USING INDEX sqlite_autoindex_t_1": the walk costs less than the scan and its sorter, so the index serves the ORDER BY, backwards for DESC *) let test_order_by_walks () = check "SELECT a FROM t ORDER BY b" ~best:("walk " ^ index) ~sorted:1 ~reverse:false; check "SELECT a FROM t ORDER BY b DESC" ~best:("walk " ^ index) ~sorted:1 ~reverse:true (* "USE TEMP B-TREE FOR LAST TERM OF ORDER BY": a UNIQUE key holds NULLs more than once, so it orders the rows only up to its own term *) let test_nullable_unique_orders_a_prefix () = check "SELECT a FROM t ORDER BY b DESC, a" ~best:("walk " ^ index) ~sorted:1 ~reverse:true (* "SCAN t" then "USE TEMP B-TREE FOR ORDER BY" *) let test_no_index_sorts () = check "SELECT a FROM t ORDER BY a" ~best:"scan" ~sorted:0 ~reverse:false (* "SCAN t USING COVERING INDEX sqlite_autoindex_t_1": narrower than the table, the entries are the answer *) let test_covering_walk () = check ~covers:true "SELECT b FROM t" ~best:("walk " ^ index) ~sorted:0 ~reverse:false (* "SEARCH t USING INDEX sqlite_autoindex_t_1 (b=?)" and no sorter: one row is in every order *) let test_one_row () = check "SELECT a FROM t WHERE b = 1 ORDER BY a" ~best:"one row" ~sorted:1 ~reverse:false (* build.c, estimateTableWidth and estimateIndexWidth: three untyped columns and the rowid weigh LogEst(16), the key and the rowid LogEst(8) *) let test_widths () = Alcotest.(check int) "table" 40 (W.table_width gt); Alcotest.(check int) "index" 30 (W.index_width gt (List.hd gt.indexes)) (* sqlite3 3.54.1: "SEARCH t USING INDEX sqlite_autoindex_t_1 (b=?)" with no sorter for an IN list on [b], either direction, since the seek walks the list's values in the index's order; "SEARCH t USING INTEGER PRIMARY KEY (rowid=?)" for one on the rowid; an uncorrelated IN (SELECT ...) is a seek too, and one on an unindexed column a conjunct a walk for the ORDER BY tests. A model that skipped an IN column as it skips an equality's would sort none of these orders; one that left IN unmodelled would refuse them. *) let test_in_seeks () = check "SELECT a FROM t WHERE b IN (1, 2) ORDER BY b" ~best:("search " ^ index) ~sorted:1 ~reverse:false; check "SELECT a FROM t WHERE b IN (1, 2) ORDER BY b DESC" ~best:("search " ^ index) ~sorted:1 ~reverse:true; check "SELECT a FROM t WHERE rowid IN (3, 1) ORDER BY rowid" ~best:"rowid in" ~sorted:1 ~reverse:false; check "SELECT a FROM t WHERE b IN (SELECT x FROM u) ORDER BY a" ~best:("search " ^ index) ~sorted:0 ~reverse:false; check "SELECT a FROM t WHERE c IN (SELECT x FROM u) ORDER BY b" ~best:("walk " ^ index) ~sorted:1 ~reverse:false let suite = ( "where_loop", [ Alcotest.test_case "an ORDER BY walks its index" `Quick test_order_by_walks; Alcotest.test_case "a nullable UNIQUE key orders a prefix" `Quick test_nullable_unique_orders_a_prefix; Alcotest.test_case "no index, a sort" `Quick test_no_index_sorts; Alcotest.test_case "a narrower covering walk" `Quick test_covering_walk; Alcotest.test_case "one row needs no sort" `Quick test_one_row; Alcotest.test_case "row widths" `Quick test_widths; Alcotest.test_case "an IN seeks its values in order" `Quick test_in_seeks; ] )