Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341module Ast = Sql.Astmodule Lexer = Sql.Private.Lexermodule Self_ref = Sql.Private.Self_ref
(* The predicate of [CREATE INDEX i ON t (a) WHERE <e>], over a table of a and b, so the columns in scope are exactly [a; b]. *)let predicate sql = match Fmt.kstr Lexer.parse_statements "CREATE INDEX i ON t (a) WHERE %s" sql with | Ok [ Ast.Create_index { where = Some e; _ } ] -> e | Ok _ -> Fmt.failwith "not an indexed WHERE: %s" sql | Error e -> Fmt.failwith "parse error in %S: %s" sql e
let check ?(what = Self_ref.Partial_index) sql = Self_ref.check what ~table:"t" ~cols:[ "a"; "b" ] (predicate sql)
(* The key expression of [CREATE INDEX i ON t (<e>)] and the generation of [CREATE TABLE t (a, b, c AS (<e>))]. Neither production drops a schema qualifier the way [predicate] above does -- the partial index WHERE is one of the two clauses that ignore one -- so these read an expression in the shape those two contexts actually see. *)let index_key sql = match Fmt.kstr Lexer.parse_statements "CREATE INDEX i ON t (%s)" sql with | Ok [ Ast.Create_index { keys; _ } ] -> ( match Ast.index_key_expressions keys with | [ e ] -> e | _ -> Fmt.failwith "not one expression key: %s" sql) | Ok _ -> Fmt.failwith "not a CREATE INDEX: %s" sql | Error e -> Fmt.failwith "parse error in %S: %s" sql e
let generation sql = match Fmt.kstr Lexer.parse_statements "CREATE TABLE t (a, b, c AS (%s))" sql with | Ok [ Ast.Create_table { generated = [ (_, g) ]; _ } ] -> g.Ast.expr | Ok _ -> Fmt.failwith "not one generated column: %s" sql | Error e -> Fmt.failwith "parse error in %S: %s" sql e
(* The [ON CONFLICT (a) WHERE <e>] target, which {!Self_ref.check_names} reads and which keeps its qualifier too. *)let target_where sql = match Fmt.kstr Lexer.parse_statements "INSERT INTO t VALUES (1, 2) ON CONFLICT (a) WHERE %s DO NOTHING" sql with | Ok [ Ast.Insert { upsert = Some { target_where = Some e; _ }; _ } ] -> e | Ok _ -> Fmt.failwith "not an upsert target WHERE: %s" sql | Error e -> Fmt.failwith "parse error in %S: %s" sql e
(* [run] the expression [read] out of [sql], and say what came of it. *)let outcome read run sql = try run (read sql); None with Catalog.Error (Prepare m) -> Some m
let refuses_in read run sql msg = Alcotest.(check (option string)) sql (Some msg) (outcome read run sql)
let accepts_in read run sql = Alcotest.(check (option string)) sql None (outcome read run sql)
(* [refuses sql msg]: [sql] is refused, with exactly [msg]. The message is compared in full because it is sqlite3's verbatim, and a reader diffing two engines has no way to tell a differently worded refusal from a different one. *)let refuses ?what sql msg = match check ?what sql with | () -> Alcotest.failf "%S was accepted; expected %S" sql msg | exception Catalog.Error (Prepare m) -> Alcotest.(check string) sql msg m
let accepts ?what sql = match check ?what sql with | () -> () | exception Catalog.Error (Prepare m) -> Alcotest.failf "%S was refused: %s" sql m
let test_subqueries () = let msg = "subqueries prohibited in partial index WHERE clauses" in refuses "b IN (SELECT b FROM u)" msg; refuses "b = (SELECT max(b) FROM u)" msg; refuses "EXISTS (SELECT 1 FROM u)" msg; (* the whole expression is resolved, not its top node *) refuses "b > (SELECT 1) OR b < 2" msg; refuses "CASE WHEN EXISTS (SELECT 1 FROM u) THEN 1 ELSE 0 END" msg
let test_parameters () = let msg = "parameters prohibited in partial index WHERE clauses" in refuses "b > ?" msg; refuses "abs(b) > abs(?)" msg
(* SQLite's non-constant builtins, whether or not this engine implements them: the schema is shared with sqlite3, so the set has to be. *)let test_non_deterministic () = let msg = "non-deterministic functions prohibited in partial index WHERE clauses" in List.iter (fun call -> refuses (call ^ " > 1") msg) [ "random()"; "randomblob(4)"; "changes()"; "total_changes()"; "last_insert_rowid()"; "sqlite_version()"; "sqlite_source_id()"; ]
let test_current_time_keywords () = List.iter (fun kw -> let sql = Fmt.str "b > %s" kw in accepts ~what:Self_ref.Check_constraint sql; refuses ~what:Self_ref.Partial_index sql "non-deterministic functions prohibited in partial index WHERE clauses"; refuses ~what:Self_ref.Index_expression sql "non-deterministic functions prohibited in index expressions"; refuses ~what:Self_ref.Generated_column sql "non-deterministic functions prohibited in generated columns") [ "CURRENT_TIMESTAMP"; "CURRENT_DATE"; "CURRENT_TIME" ]
let test_schema_qualifier_is_ignored () = List.iter (fun what -> accepts ~what "main.t.b > 1"; accepts ~what "bogus.t.b > 1") [ Self_ref.Check_constraint; Self_ref.Partial_index ]; (* the table half is still resolved: the schema is what is ignored *) refuses ~what:Self_ref.Check_constraint "bogus.u.b > 1" "no such column: u.b"; refuses ~what:Self_ref.Check_constraint "bogus.t.nosuchcol > 1" "no such column: t.nosuchcol"; (* and the other two, read through their own grammar, which keeps it *) let run what e = Self_ref.check what ~table:"t" ~cols:[ "a"; "b" ] e in List.iter (fun (read, what, dot) -> refuses_in read (run what) "main.t.b" dot; refuses_in read (run what) "t.b" dot; refuses_in read (run what) "bogus.t.b" "no such column: bogus.t.b") [ ( index_key, Self_ref.Index_expression, "the \".\" operator prohibited in index expressions" ); ( generation, Self_ref.Generated_column, "the \".\" operator prohibited in generated columns" ); ]
let test_target_where_resolves_the_schema () = let names e = Self_ref.check_names ~table:"t" ~cols:[ "a"; "b" ] e in accepts_in target_where names "b > 5"; accepts_in target_where names "t.b > 5"; refuses_in target_where names "bogus.t.b > 5" "no such column: bogus.t.b"; refuses_in target_where names "u.b > 5" "no such column: u.b"; (* the same predicate inside the index it would name is accepted *) accepts ~what:Self_ref.Partial_index "bogus.t.b > 5"
let test_raise_is_stored_not_refused () = List.iter (fun what -> accepts ~what "RAISE(IGNORE)"; accepts ~what "RAISE(ABORT,'no')"; accepts ~what "b > 1 OR RAISE(FAIL,'no')") [ Self_ref.Partial_index; Self_ref.Check_constraint; Self_ref.Index_expression; Self_ref.Generated_column; ]; (* the message is an ordinary expression, and is resolved like one *) refuses ~what:Self_ref.Check_constraint "RAISE(ABORT, nosuchcol)" "no such column: nosuchcol"
(* A CHECK constraint is the one context that admits them: SQLite allows it there and refuses it in the other three. *)let test_non_deterministic_allowed_in_check () = accepts ~what:Self_ref.Check_constraint "b > randomblob(4)"; refuses ~what:Self_ref.Index_expression "b > randomblob(4)" "non-deterministic functions prohibited in index expressions"; refuses ~what:Self_ref.Generated_column "b > randomblob(4)" "non-deterministic functions prohibited in generated columns"; (* a subquery is refused in a CHECK constraint all the same *) refuses ~what:Self_ref.Check_constraint "b IN (SELECT b FROM u)" "subqueries prohibited in CHECK constraints"
(* The message names the function, and it is the aggregate spelling that is refused: max of one argument aggregates, of two it is the scalar maximum. *)let test_aggregates () = refuses "count(b) > 1" "misuse of aggregate function count()"; refuses "max(b) > 1" "misuse of aggregate function max()"; refuses "sum(b) > 1" "misuse of aggregate function sum()"; refuses "count(DISTINCT b) > 1" "misuse of aggregate function count()"; accepts "max(a,b) > 1"; accepts "min(a,b) > 1"
(* The predicate resolves against its own table alone, so another table's name is no more of a qualifier than an unknown column is a column. *)let test_columns () = accepts "a > 1"; accepts "b > 1"; accepts "t.b > 1"; accepts "T.B > 1" (* SQL identifiers are case-insensitive *); refuses "nosuchcol > 1" "no such column: nosuchcol"; refuses "t.nosuchcol > 1" "no such column: t.nosuchcol"; refuses "u.b > 1" "no such column: u.b"; refuses "abs(nosuchcol) > 1" "no such column: nosuchcol"
let test_dot_operator () = accepts ~what:Self_ref.Check_constraint "t.b > 1"; accepts ~what:Self_ref.Partial_index "t.b > 1"; refuses ~what:Self_ref.Index_expression "t.b > 1" "the \".\" operator prohibited in index expressions"; refuses ~what:Self_ref.Generated_column "t.b > 1" "the \".\" operator prohibited in generated columns"; (* the operator, not the name it resolves to: an unknown table qualifier is the same refusal, and it comes before the column lookup *) refuses ~what:Self_ref.Index_expression "u.b > 1" "the \".\" operator prohibited in index expressions"; refuses ~what:Self_ref.Generated_column "t.nosuchcol > 1" "the \".\" operator prohibited in generated columns"; (* nested, since it is the whole expression that is resolved *) refuses ~what:Self_ref.Index_expression "abs(t.b) > 1" "the \".\" operator prohibited in index expressions"
let test_unknown_functions () = List.iter (fun what -> refuses ~what "b > nosuchfunc(1)" "no such function: nosuchfunc"; refuses ~what "nosuchfunc(a) > b" "no such function: nosuchfunc"; refuses ~what "b > generate_series(1)" "no such function: generate_series") [ Self_ref.Partial_index; Self_ref.Check_constraint; Self_ref.Index_expression; Self_ref.Generated_column; ]; (* a builtin sqlite3 has and this package does not evaluate is still a function: it is refused for being non-deterministic where that rule applies, and admitted by a CHECK constraint, which is sqlite3's answer *) accepts ~what:Self_ref.Check_constraint "b > random()"; refuses ~what:Self_ref.Partial_index "b > random()" "non-deterministic functions prohibited in partial index WHERE clauses"
(* A window function is about a frame of rows, so it is no more answerable from one row than an aggregate is, and sqlite3 words it the same way:
CREATE TABLE t (a INT, b INT CHECK (row_number() OVER () > 1)) misuse of window function row_number() *)let test_window_functions () = refuses "row_number() OVER () > 1" "misuse of window function row_number()"; refuses ~what:Self_ref.Check_constraint "sum(b) OVER () > 1" "misuse of window function sum()"
(* What is left is what these expressions are for. The date and time functions are among it: SQLite declares them PURE_DATE, fixing 'now' for the duration of one statement. *)let test_row_local_expressions () = List.iter accepts [ "b > 3"; "b IN (1,2,3)"; "b > abs(-2)"; "b > 5 AND a IS NOT NULL"; "CASE WHEN b > 5 THEN 1 ELSE 0 END"; "b > 3 COLLATE NOCASE"; "CAST(b AS INTEGER) > 3"; "NOT (b > 3)"; "b IS NULL"; "b > julianday('now')"; "b > date('now')"; "length(a) > 2"; ]
let test_pp () = Alcotest.(check string) "partial index" "partial index WHERE clauses" (Fmt.to_to_string Self_ref.pp Self_ref.Partial_index); Alcotest.(check string) "check" "CHECK constraints" (Fmt.to_to_string Self_ref.pp Self_ref.Check_constraint); Alcotest.(check string) "index expression" "index expressions" (Fmt.to_to_string Self_ref.pp Self_ref.Index_expression); Alcotest.(check string) "generated column" "generated columns" (Fmt.to_to_string Self_ref.pp Self_ref.Generated_column)
let test_columns_fold_ascii_only () = let accented sql = Self_ref.check Self_ref.Partial_index ~table:"té" ~cols:[ "é" ] (predicate sql) in let accepts sql = match accented sql with | () -> () | exception Catalog.Error (Prepare m) -> Alcotest.failf "%S was refused: %s" sql m in let refuses sql msg = match accented sql with | () -> Alcotest.failf "%S was accepted; expected %S" sql msg | exception Catalog.Error (Prepare m) -> Alcotest.(check string) sql msg m in accepts "\"é\" > 1"; accepts "\"té\".\"é\" > 1"; refuses "\"É\" > 1" "no such column: É"; refuses "\"TÉ\".\"é\" > 1" "no such column: TÉ.é"
let suite = ( "self_ref", [ Alcotest.test_case "self_ref: subqueries" `Quick test_subqueries; Alcotest.test_case "self_ref: columns fold ASCII only" `Quick test_columns_fold_ascii_only; Alcotest.test_case "self_ref: parameters" `Quick test_parameters; Alcotest.test_case "self_ref: non-deterministic functions" `Quick test_non_deterministic; Alcotest.test_case "self_ref: the CURRENT_* keywords" `Quick test_current_time_keywords; Alcotest.test_case "self_ref: RAISE is stored, not refused" `Quick test_raise_is_stored_not_refused; Alcotest.test_case "self_ref: a schema qualifier is ignored" `Quick test_schema_qualifier_is_ignored; Alcotest.test_case "self_ref: an upsert target resolves the schema" `Quick test_target_where_resolves_the_schema; Alcotest.test_case "self_ref: a CHECK constraint admits them" `Quick test_non_deterministic_allowed_in_check; Alcotest.test_case "self_ref: aggregates" `Quick test_aggregates; Alcotest.test_case "self_ref: columns of the table alone" `Quick test_columns; Alcotest.test_case "self_ref: the \".\" operator" `Quick test_dot_operator; Alcotest.test_case "self_ref: unknown functions" `Quick test_unknown_functions; Alcotest.test_case "self_ref: window functions" `Quick test_window_functions; Alcotest.test_case "self_ref: row-local expressions" `Quick test_row_local_expressions; Alcotest.test_case "self_ref: pp" `Quick test_pp; ] )