Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416(* A row a statement writes over the in-memory store takes what its table declares before the store sees it, as sqlite3's VDBE applies it before its b-tree. Each expected row 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 run cat sql = match Sql.exec (Sql.v cat) sql with Sql.Rows r -> r | Sql.Done -> []
let store stmts = let cat = Catalog.of_mem [] in List.iter (fun s -> ignore (run cat s)) stmts; cat
(* datatype3.html section 3: an INTEGER column holds '01' as 1, a REAL column holds 2 as 2.0, a TEXT column holds 3 as '3'. sqlite3 answers 1|integer|2.0|real|3|text. *)let test_a_written_value_takes_its_column_affinity () = let cat = store [ "CREATE TABLE t (a INTEGER, b REAL, c TEXT)"; "INSERT INTO t VALUES ('01', 2, 3)"; ] in Alcotest.check vrows "each value under its column's affinity" [ [ Value.Int 1L; Value.Text "integer"; Value.Float 2.; Value.Text "real"; Value.Text "3"; Value.Text "text"; ]; ] (run cat "SELECT a, typeof(a), b, typeof(b), c, typeof(c) FROM t")
(* lang_createtable.html section 3.2: a column the INSERT leaves out takes its DEFAULT, a constant or a parenthesised expression, and NOT NULL then reads the default. sqlite3 answers 1|7|3. *)let test_an_omitted_column_takes_its_default () = let cat = store [ "CREATE TABLE t (a, b NOT NULL DEFAULT 7, c DEFAULT (1 + 2))"; "INSERT INTO t (a) VALUES (1)"; ] in Alcotest.check vrows "the defaults" [ [ Value.Int 1L; Value.Int 7L; Value.Int 3L ] ] (run cat "SELECT * FROM t")
(* gencol.html section 2: an INSERT without a column list supplies the columns that are not generated, and both kinds are computed from them, a STORED one reading a VIRTUAL one declared after it. sqlite3 answers 5|10|11 and 6|12|13. *)let test_generated_columns_are_computed () = let cat = store [ "CREATE TABLE t (a INT, b AS (c - 1) STORED, c AS (a * 2 + 1))"; "INSERT INTO t (a) VALUES (5)"; "INSERT INTO t VALUES (6)"; ] in Alcotest.check vrows "computed in the order they read each other" [ [ Value.Int 5L; Value.Int 10L; Value.Int 11L ]; [ Value.Int 6L; Value.Int 12L; Value.Int 13L ]; ] (run cat "SELECT * FROM t")
(* An UPDATE stores its row as an INSERT does: '7' under INTEGER affinity is 7, and the generated columns are computed again. sqlite3 answers 7|integer|14. *)let test_an_update_stores_as_an_insert_does () = let cat = store [ "CREATE TABLE t (a INTEGER, b AS (a * 2))"; "INSERT INTO t VALUES (1)"; "UPDATE t SET a = '7'"; ] in Alcotest.check vrows "affinity, then the generated column" [ [ Value.Int 7L; Value.Text "integer"; Value.Int 14L ] ] (run cat "SELECT a, typeof(a), b FROM t")
(* stricttables.html section 3: a STRICT column holds only its type once its affinity is applied, so '42' goes into an INT column as 42 and 'x' is refused. sqlite3 answers "cannot store TEXT value in INT column t.a". *)let test_a_strict_column_refuses_another_type () = let cat = store [ "CREATE TABLE t (a INT) STRICT"; "INSERT INTO t VALUES ('42')" ] in Alcotest.check vrows "converted first" [ [ Value.Int 42L ] ] (run cat "SELECT a FROM t"); Alcotest.check_raises "then refused" (Catalog.Error (Constraint (Datatype { table = "t"; column = "a"; declared = "INT"; stored = "TEXT" }))) (fun () -> ignore (run cat "INSERT INTO t VALUES ('x')"))
let i n = Value.Int nlet t s = Value.Text s
(* lang_update.html and lang_delete.html: a WHERE and a SET hold any expression, a subquery among them, and a WITH binds its tables for both. sqlite3 answers 1|x 2|3y 3|3z, then 1|x 3|3z, then 3|w. *)let test_a_subquery_and_a_with_reach_update_and_delete () = let db = Sql.v (Catalog.of_mem []) in let run sql = match Sql.exec db sql with Sql.Rows r -> r | Sql.Done -> [] in ignore (run "CREATE TABLE t(a, b); INSERT INTO t VALUES (1, 'x'), (2, 'y'), (3, \ 'z'); CREATE TABLE u(k); INSERT INTO u VALUES (2), (3)"); ignore (run "UPDATE t SET b = (SELECT max(k) FROM u) || b WHERE a IN (SELECT k FROM \ u)"); Alcotest.check vrows "a subquery in the SET and the WHERE" [ [ i 1L; t "x" ]; [ i 2L; t "3y" ]; [ i 3L; t "3z" ] ] (run "SELECT * FROM t"); ignore (run "DELETE FROM t WHERE a = (SELECT min(k) FROM u)"); Alcotest.check vrows "a subquery in a DELETE's WHERE" [ [ i 1L; t "x" ]; [ i 3L; t "3z" ] ] (run "SELECT * FROM t"); ignore (run "WITH c(n) AS (SELECT 3) UPDATE t SET b = 'w' WHERE a IN (SELECT n FROM \ c)"); ignore (run "WITH c(n) AS (SELECT 1) DELETE FROM t WHERE a IN (SELECT n FROM c)"); Alcotest.check vrows "a WITH over an UPDATE and a DELETE" [ [ i 3L; t "w" ] ] (run "SELECT * FROM t")
(* lang_createtable.html section 2.6: rowid, oid and _rowid_ name the key of a table that declares no column of that name, in a WHERE and a SET as in a SELECT. sqlite3 answers 1|p 3|s3. *)let test_a_write_reads_the_rowid () = let cat = store [ "CREATE TABLE r(a)"; "INSERT INTO r VALUES ('p'), ('q'), ('s')"; "DELETE FROM r WHERE rowid = 2"; "UPDATE r SET a = a || oid WHERE _rowid_ > 1"; ] in Alcotest.check vrows "the key under each of its names" [ [ i 1L; t "p" ]; [ i 3L; t "s3" ] ] (run cat "SELECT rowid, a FROM r")
(* lang_update.html section 2.2: UPDATE ... FROM joins the target with the FROM sources and rewrites each target row the join matches once. sqlite3 answers 1|10 2|20 3|0. *)let test_an_update_reads_its_from () = let cat = store [ "CREATE TABLE p(id, v)"; "INSERT INTO p VALUES (1, 10), (2, 20)"; "CREATE TABLE q(id, w)"; "INSERT INTO q VALUES (1, 0), (2, 0), (3, 0)"; "UPDATE q SET w = p.v FROM p WHERE p.id = q.id"; ] in Alcotest.check vrows "each matched row takes its join's value" [ [ i 1L; i 10L ]; [ i 2L; i 20L ]; [ i 3L; i 0L ] ] (run cat "SELECT * FROM q")
(* lang_returning.html: an INSERT, an UPDATE and a DELETE answer the rows they wrote, an UPDATE's as rewritten and a DELETE's as they were, and each counts what it changed. sqlite3 answers 3|60 4|80, then 3|3|31 4|4|41, then 10, then 1. *)let test_a_write_returns_its_rows () = let db = Sql.v (Catalog.of_mem []) in let run sql = match Sql.exec db sql with Sql.Rows r -> r | Sql.Done -> [] in ignore (run "CREATE TABLE p(id, v); INSERT INTO p VALUES (1, 10), (2, 20)"); Alcotest.check vrows "an INSERT's rows" [ [ i 3L; i 60L ]; [ i 4L; i 80L ] ] (run "INSERT INTO p VALUES (3, 30), (4, 40) RETURNING id, v * 2"); Alcotest.check vrows "an UPDATE's rows, rewritten" [ [ i 3L; i 3L; i 31L ]; [ i 4L; i 4L; i 41L ] ] (run "UPDATE p SET v = v + 1 WHERE id > 2 RETURNING rowid, *"); Alcotest.check vrows "a DELETE's rows, as they were" [ [ i 10L ] ] (run "DELETE FROM p WHERE id = 1 RETURNING v"); Alcotest.(check int64) "the DELETE counted its row" 1L (Sql.changes db)
(* select.c codes a VALUES list of several rows as a co-routine no group folds, so an aggregate in it is refused while the statement is prepared, before any row is written. sqlite3 answers "misuse of aggregate: count()" and leaves t empty. *)let test_an_aggregate_in_values_is_misused () = let cat = store [ "CREATE TABLE t(a)" ] in Alcotest.check_raises "refused as prepared" (Catalog.Error (Prepare "misuse of aggregate: count()")) (fun () -> ignore (run cat "INSERT INTO t VALUES (1), (count(*))")); Alcotest.check vrows "nothing written" [] (run cat "SELECT * FROM t")
(* insert.c, sqlite3Insert: a SELECT source that reads no table of the statement's target runs as a co-routine, each row stored before the next is computed, so the NULL of the first row is refused before the second row's integer overflow is reached. sqlite3 answers "NOT NULL constraint failed: w.a". *)let test_insert_select_stores_row_by_row () = let cat = store [ "CREATE TABLE w(a NOT NULL)" ] in Alcotest.check_raises "the first row's refusal" (Catalog.Error (Constraint (Not_null { table = "w"; column = "a" }))) (fun () -> ignore (run cat "INSERT INTO w SELECT NULL UNION ALL SELECT \ abs(-9223372036854775807-1)"))
(* lang_createtable.html section 3.7: a CHECK is evaluated on every row a write stores, fails only where its predicate is false, and takes the statement's OR clause, ABORT by default. sqlite3 answers "CHECK constraint failed: c0 < 5" with 1,2,3 kept, then 1,2,3,4,0. *)let test_a_check_holds_over_any_store () = let cat = store [ "CREATE TABLE t(c0, CHECK (c0 < 5))"; "INSERT INTO t VALUES (1), (2), (3)"; ] in Alcotest.check_raises "the second row's CHECK undoes the statement" (Catalog.Error (Constraint (Check "c0 < 5"))) (fun () -> ignore (run cat "INSERT INTO t VALUES (4), (5)")); Alcotest.check vrows "the statement undone" [ [ t "1,2,3" ] ] (run cat "SELECT group_concat(c0) FROM t"); ignore (run cat "INSERT OR IGNORE INTO t VALUES (4), (9), (0)"); Alcotest.check vrows "IGNORE skips the failing row" [ [ t "1,2,3,4,0" ] ] (run cat "SELECT group_concat(c0) FROM t")
(* lang_conflict.html: a NOT NULL ON CONFLICT REPLACE puts the column's DEFAULT in, OR IGNORE skips the row, and a NOT NULL with no clause aborts. sqlite3 answers "NOT NULL constraint failed: n.c" and keeps 1|d|1. *)let test_not_null_takes_its_resolution () = let cat = store [ "CREATE TABLE n(a, b NOT NULL ON CONFLICT REPLACE DEFAULT 'd', c NOT \ NULL)"; "INSERT INTO n VALUES (1, NULL, 1)"; "INSERT OR IGNORE INTO n VALUES (2, 2, NULL)"; ] in Alcotest.check_raises "no clause aborts" (Catalog.Error (Constraint (Not_null { table = "n"; column = "c" }))) (fun () -> ignore (run cat "INSERT INTO n VALUES (3, 3, NULL)")); Alcotest.check vrows "REPLACE's default, IGNORE's skip" [ [ i 1L; t "d"; i 1L ] ] (run cat "SELECT * FROM n")
(* lang_conflict.html: OR IGNORE skips a row in the way of a UNIQUE key, and OR REPLACE deletes the row in the way before it stores its own; under OR FAIL the rows before the refused one stay. sqlite3 answers 1|a 2|y 3|c, then "UNIQUE constraint failed: f.a" keeping 1|1 2|2. *)let test_ignore_replace_and_fail_resolve_a_unique_key () = let cat = store [ "CREATE TABLE u(k UNIQUE, v)"; "INSERT INTO u VALUES (1, 'a'), (2, 'b')"; "INSERT OR IGNORE INTO u VALUES (1, 'x'), (3, 'c')"; "INSERT OR REPLACE INTO u VALUES (2, 'y')"; ] in Alcotest.check vrows "IGNORE skipped, REPLACE replaced" [ [ i 1L; t "a" ]; [ i 2L; t "y" ]; [ i 3L; t "c" ] ] (run cat "SELECT * FROM u ORDER BY k"); ignore (run cat "CREATE TABLE f(a UNIQUE, b); INSERT INTO f VALUES (1, 1)"); Alcotest.check_raises "FAIL refuses the second row" (Catalog.Error (Constraint (Unique { table = "f"; columns = [ "a" ] }))) (fun () -> ignore (run cat "INSERT OR FAIL INTO f VALUES (2, 2), (1, 3), (4, 4)")); Alcotest.check vrows "and keeps the first" [ [ i 1L; i 1L ]; [ i 2L; i 2L ] ] (run cat "SELECT * FROM f")
(* insert.c, sqlite3GenerateConstraintChecks: a constraint's own ON CONFLICT holds where the statement names none, and a REPLACE constraint is checked after the others, so an IGNORE one met on the same row skips it first. sqlite3 answers 1|1 2|2. *)let test_a_declared_resolution_is_checked_in_sqlite3_order () = let cat = store [ "CREATE TABLE r(a UNIQUE ON CONFLICT REPLACE, b UNIQUE ON CONFLICT \ IGNORE)"; "INSERT INTO r VALUES (1, 1), (2, 2)"; "INSERT INTO r VALUES (1, 2)"; ] in Alcotest.check vrows "IGNORE met before REPLACE" [ [ i 1L; i 1L ]; [ i 2L; i 2L ] ] (run cat "SELECT * FROM r")
(* lang_upsert.html: ON CONFLICT (k) DO UPDATE rewrites the row in the way with excluded the row that was refused, under its WHERE; DO NOTHING skips the row; a target naming no unique key is refused as prepared. sqlite3 answers 1|ap 2|b 4|q, then "ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint". *)let test_an_upsert_runs_over_any_store () = let cat = store [ "CREATE TABLE u(k UNIQUE, v)"; "INSERT INTO u VALUES (1, 'a'), (2, 'b')"; "INSERT INTO u VALUES (1, 'p'), (4, 'q') ON CONFLICT(k) DO UPDATE SET \ v = v || excluded.v WHERE excluded.v <> 'q'"; "INSERT INTO u VALUES (4, 'z') ON CONFLICT DO NOTHING"; ] in Alcotest.check vrows "updated, inserted, skipped" [ [ i 1L; t "ap" ]; [ i 2L; t "b" ]; [ i 4L; t "q" ] ] (run cat "SELECT * FROM u ORDER BY k"); Alcotest.check_raises "a target naming no key" (Catalog.Error (Prepare "ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE \ constraint")) (fun () -> ignore (run cat "INSERT INTO u VALUES (1, 'z') ON CONFLICT(v) DO NOTHING"))
(* sqlite3 draws the key of a row naming none after the row's BEFORE triggers and before its conflicts are resolved (insert.c, sqlite3Insert: OP_NewRowid ahead of sqlite3GenerateConstraintChecks): the table's largest rowid plus one, as it stands then. A REPLACE that deletes that largest row does not lower the key, and a row a conflict skips has still drawn it, which an AUTOINCREMENT table never hands out again. sqlite3 answers 3|3, 2|1, and 1|1 then 3|2. *)let test_a_row_draws_its_key_before_its_conflicts () = let rows stmts query = run (store stmts) query in let int n = Value.Int (Int64.of_int n) in Alcotest.check vrows "a REPLACE of the largest row keeps the key past it" [ [ int 3; int 3 ] ] (rows [ "CREATE TABLE t (a, u UNIQUE)"; "INSERT INTO t VALUES (1, 1)"; "INSERT OR REPLACE INTO t VALUES (2, 1)"; "INSERT OR REPLACE INTO t VALUES (3, 1)"; ] "SELECT rowid, a FROM t"); Alcotest.check vrows "and so does an INTEGER PRIMARY KEY written NULL" [ [ int 2; int 1 ] ] (rows [ "CREATE TABLE t (id INTEGER PRIMARY KEY, u UNIQUE)"; "INSERT INTO t (u) VALUES (1)"; "INSERT OR REPLACE INTO t (u) VALUES (1)"; ] "SELECT id, u FROM t"); Alcotest.check vrows "an AUTOINCREMENT key a skipped row drew is spent" [ [ int 1; int 1 ]; [ int 3; int 2 ] ] (rows [ "CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, u UNIQUE)"; "INSERT INTO t (u) VALUES (1)"; "INSERT OR IGNORE INTO t (u) VALUES (1)"; "INSERT INTO t (u) VALUES (2)"; ] "SELECT id, u FROM t")
let suite = ( "write", [ Alcotest.test_case "a row draws its key before its conflicts" `Quick test_a_row_draws_its_key_before_its_conflicts; Alcotest.test_case "a written value takes its column affinity" `Quick test_a_written_value_takes_its_column_affinity; Alcotest.test_case "an omitted column takes its default" `Quick test_an_omitted_column_takes_its_default; Alcotest.test_case "generated columns are computed" `Quick test_generated_columns_are_computed; Alcotest.test_case "an update stores as an insert does" `Quick test_an_update_stores_as_an_insert_does; Alcotest.test_case "a strict column refuses another type" `Quick test_a_strict_column_refuses_another_type; Alcotest.test_case "a subquery and a WITH reach UPDATE and DELETE" `Quick test_a_subquery_and_a_with_reach_update_and_delete; Alcotest.test_case "a write reads the rowid" `Quick test_a_write_reads_the_rowid; Alcotest.test_case "an UPDATE reads its FROM" `Quick test_an_update_reads_its_from; Alcotest.test_case "a write returns its rows" `Quick test_a_write_returns_its_rows; Alcotest.test_case "an aggregate in VALUES is misused" `Quick test_an_aggregate_in_values_is_misused; Alcotest.test_case "INSERT ... SELECT stores row by row" `Quick test_insert_select_stores_row_by_row; Alcotest.test_case "a CHECK holds over any store" `Quick test_a_check_holds_over_any_store; Alcotest.test_case "NOT NULL takes its resolution" `Quick test_not_null_takes_its_resolution; Alcotest.test_case "IGNORE, REPLACE and FAIL resolve a unique key" `Quick test_ignore_replace_and_fail_resolve_a_unique_key; Alcotest.test_case "a declared resolution is checked in sqlite3 order" `Quick test_a_declared_resolution_is_checked_in_sqlite3_order; Alcotest.test_case "an upsert runs over any store" `Quick test_an_upsert_runs_over_any_store; ] )