Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198(** Catalog binding, query validation and recursive relation preparation. *)
include module type of Query_intf
type binding = string * (string * Ast.value) list(** The named rows visible in an enclosing expression scope. *)
val default_recursion_cap : int(** [default_recursion_cap] is the cap on a recursive CTE a catalog takes when its caller names none, the one {!Connection.v} applies. *)
val closed : encoding:Encoding.t -> string -> catalog(** [closed ~encoding what] is a catalog with no relation, over text stored in [encoding], for an expression that may read none: a [DEFAULT], a generated column, a [CHECK]. Every lookup of a relation raises [Catalog.Error (Runtime _)] naming [what]; it has no function of its own and takes {!default_recursion_cap}. *)
val of_catalog : recursion_cap:int -> subquery: (catalog -> (string * (string * Catalog.Value.t) list) list -> Ast.select -> Catalog.Value.t array -> Catalog.Value.t list list) -> Catalog.Spi.t -> catalog(** [of_catalog ~recursion_cap ~subquery cat] is the catalog of [cat], with [subquery] as its runner, usually from {!Engine.run_select}, and the positive [recursion_cap] as its {!field-recursion_cap}. Its {!field-encoding} is UTF-8, the text a {!Catalog.Spi.S} backend holds. A backend such as Irmin or Parquet implements {!Catalog.Spi.S} and adapts it here. The catalog has no functions of its own. *)
val table_function : catalog -> string -> Func.table_function(** [table_function cat name] is the table-valued function [name]: the one of the backend ({!field-table_function}) if it has one, and the one of {!Func} otherwise.
@raise Catalog.Error if neither has [name]. *)
val call : catalog -> ?coll:Collation.t -> string -> Catalog.Value.t list -> Catalog.Value.t(** [call cat ~coll name args] is the scalar function [name] applied to [args]: the one of the backend ({!field-scalar_function}) if it has one, and [Func.apply ~coll name args] otherwise. Every evaluator calls functions through it.
@raise Catalog.Error as {!Func.apply} does. *)
val resolve_env : catalog -> Resolve.env(** [resolve_env cat] is the schema of [cat] as {!Resolve} reads it, without rows, indexes or runner. *)
val is_rowid_name : string -> bool(** [is_rowid_name n] is [true] iff [n] is [rowid], [oid] or [_rowid_], without regard to case (lang_createtable.html section 2.6). A declared column of that name comes first. *)
val is_agg : string -> 'a list -> bool(** [is_agg name args] is [true] iff [name] with the arguments [args] is an aggregate, such as the argument count, [sum] or [avg], or [min] and [max] with at most one argument. *)
val is_agg_expr : Ast.expr -> bool(** [is_agg_expr e] is [true] iff [e] is an aggregate call, with or without [DISTINCT], [FILTER] or [ORDER BY] inside the call. It does not look into the operands of [e]. *)
val is_aggregate : Ast.result_col -> bool(** [is_aggregate rc] recognises a result column containing an aggregate. *)
val is_aggregate_query : Ast.select -> bool(** [is_aggregate_query s] is [true] iff [s] has a [GROUP BY], a [HAVING] or an aggregate in its select list. Its [LIMIT] cannot move into the scan. *)
val called_name : Ast.expr -> string option(** [called_name e] is the name of the function [e] calls, under its [FILTER], [ORDER BY] and [OVER] decorations, or [None] if [e] is no call. *)
val check_calls : ?explain:bool -> catalog -> Ast.select -> unit(** [check_calls ~explain cat s] accepts [s] if its stars expand ({!Resolve.check_stars}) and every call in its select list, [WHERE], [GROUP BY], [HAVING], [ORDER BY] and join conditions names a function that {!val-call} accepts with that number of arguments, in a clause that admits it, or raises. As in sqlite3, stars and calls are checked when the statement is prepared, so a bad name fails even over an empty table. A function of the backend accepts any number of arguments. Subqueries are checked elsewhere. [explain] (defaults to [false]) accepts unknown names but still checks argument counts, as sqlite3 does for [EXPLAIN].
A clause admits what resolve.c lets it hold: the select list and the [ORDER BY] an aggregate and a window function, the [GROUP BY] and the [HAVING] an aggregate, a [WHERE] and a join condition an aggregate only where [s] folds groups ({!is_aggregate_query}), and neither in the arguments, [FILTER] or [ORDER BY] of an aggregate; the arguments and keys of a window admit no window. The rows of a VALUES list admit both, but one of several rows that holds an aggregate, in a list that leads no compound, is refused as select.c codes it, ["misuse of aggregate: f()"] naming the last such call.
@raise Catalog.Error as {!Resolve.check_stars} does, then as {!Func.apply} does: ["no such function: f"] for an unknown name, and ["wrong number of arguments to function f()"] for a wrong count. A built-in scalar function at the head of a window fails with ["f() may not be used as a window function"], and a window function called without one, or where its clause admits none, with {!Func.window_misuse}'s sentence. An aggregate where its clause admits none fails with ["misuse of aggregate function f()"], and one in the [GROUP BY] with ["aggregate functions are not allowed in the GROUP BY clause"]. A subquery of [n] columns fails with ["row value misused"] as one side of a comparison whose other side differs in width, and with ["sub-select returns n columns - expected 1"] where one value is read. *)
val check_select : catalog -> Ast.select -> unit(** [check_select cat s] is {!check_calls} over each arm of [s], and {!Resolve.check_subqueries} with {!check_calls} over every select each arm holds: what sqlite3 resolves of a top-level select before it reads a row, so that a subquery no row reaches, or whose select list an [EXISTS] never computes ({!exists_body}), is resolved all the same.
@raise Catalog.Error as those do. *)
val check_expr_calls : catalog -> Ast.expr -> unit(** [check_expr_calls cat e] is {!check_calls} over the calls of [e] and of every select [e] holds, as sqlite3 checks an [INSERT]'s, an [UPDATE]'s or a [DELETE]'s expressions when it prepares the statement: [e] itself admits neither an aggregate nor a window function.
@raise Catalog.Error as {!check_calls} does. *)
val reads_table : ?view:(string -> Ast.select option) -> string -> Ast.select -> bool(** [reads_table ~view name s] is [true] iff [s] reads the relation [name], without regard to case, in its [FROM], a join, a compound arm, a nested [WITH] body or a subquery, or in the body [view v] of a view [v] it reads. [view] defaults to no view. *)
val ctes_are_cyclic : Ast.cte list -> bool(** [ctes_are_cyclic cs] detects a cycle between WITH relations, permitting a recursive CTE to reference itself. *)
val set_op : ?colls:Collation.t option list -> Ast.set_op -> Catalog.Value.t list list -> Catalog.Value.t list list -> Catalog.Value.t list list(** [set_op ~colls op left right] combines the rows of two compound arms. [UNION ALL] keeps every row in order. [UNION], [EXCEPT] and [INTERSECT] give distinct rows in key order, as sqlite3 does with an index on the row. [colls] is the collating sequence of each column ({!Resolve.compound_collations}); [None] or a missing entry is BINARY. Of rows with equal keys, the first one is kept. *)
val window_exprs : Ast.result_col list -> Ast.expr list(** [window_exprs cols] is the distinct [Window] nodes of [cols], in order of first appearance. It does not look into a [Window] node. *)
val count : catalog -> Catalog.Value.t array -> Ast.expr -> Ast.expr(** [count cat params e] evaluates a LIMIT or OFFSET count under the bound parameters. *)
val counts : catalog -> Catalog.Value.t array -> Ast.select -> Ast.select(** [counts cat params s] is [s] with a [LIMIT] or an [OFFSET] that is neither a literal nor a bound parameter replaced by its value, computed once over [cat] as sqlite3 computes it before the select runs. A [WITH] table's own counts are {!materialize_ctes}'s.
@raise Catalog.Error if one reads a column, which nothing binds there, or folds an aggregate.*)
val conjuncts : Ast.expr -> Ast.expr list(** [conjuncts e] splits an AND expression into its conjuncts. *)
val table_arguments : catalog -> Catalog.Value.t array -> Ast.select -> Ast.select(** [table_arguments cat params s] is [s] with the hidden columns of each table-valued function in a FROM read, in [s] and the selects it nests: an argument the call left out is the value a WHERE equality gives its hidden column, as sqlite3's xBestIndex takes it, and a reference to a hidden column is the value it holds ({!Func.type-table_function}). A name that no table holds and a table-valued function answers is that function, called with no argument. A function whose arguments read a column is left as it is. *)
val materialize_ctes : catalog -> Catalog.Value.t array -> Ast.cte list -> catalog(** [materialize_ctes cat params ctes] is [cat] with the rows of each [WITH] table of [ctes] bound under its name, a recursive one computed by semi-naive evaluation. It serves the [WITH] of an [UPDATE] or a [DELETE]. *)