(* A table-valued function over the in-memory store, as a source of a join and as an eponymous table. A function with no alias is a source named after itself, in the leading FROM as in a join, so its star expands under that name (select.c, selectExpander); and a function named with no arguments is the eponymous table, whose hidden columns a WHERE equality binds (series.c, seriesBestIndex). Each expected answer is the sqlite3 CLI's (3.54.1) for the same statements. *) module Value = Catalog.Value let vrows = Alcotest.list (Alcotest.testable (Fmt.Dump.list Value.pp) (List.equal Value.equal)) let i n = Value.Int n let run db sql = match Sql.exec db sql with Sql.Rows r -> r | Sql.Done -> [] let fresh () = let db = Sql.v (Catalog.of_mem []) in ignore (run db "CREATE TABLE t(a); INSERT INTO t VALUES (1)"); db let test_a_joined_function_names_its_star () = let db = fresh () in Alcotest.check vrows "its own name" [ [ i 1L; i 4L ] ] (run db "SELECT t.*, generate_series.* FROM t, generate_series(4, 4)"); Alcotest.check vrows "beside every other kind of source" [ [ i 1L; i 1L; i 2L; i 3L; i 4L ] ] (run db "SELECT T.*, x.*, s.*, g.*, generate_series.* FROM t, t AS x, (SELECT \ 2) AS s, generate_series(3, 3) AS g, generate_series(4, 4)"); Alcotest.check vrows "a qualified column" [ [ i 5L ]; [ i 6L ] ] (run db "SELECT generate_series.value FROM t JOIN generate_series(5,6) ON 1") (* A hidden column is a column a name reads, wherever the select reading it nests. *) let test_the_eponymous_function () = let db = fresh () in Alcotest.check vrows "its arguments bound by the WHERE" [ [ i 3L ]; [ i 4L ]; [ i 5L ] ] (run db "SELECT value FROM generate_series WHERE start = 3 AND stop = 5"); Alcotest.check vrows "a hidden column read by a subquery" [ [ i 7L ] ] (run db "SELECT (SELECT stop FROM generate_series(1, 7) LIMIT 1)") let suite = ( "table_function", [ Alcotest.test_case "a joined function names its star" `Quick test_a_joined_function_names_its_star; Alcotest.test_case "the eponymous function" `Quick test_the_eponymous_function; ] )