(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** SQLite built-in functions. [Func] applies the built-in scalar and table-valued functions to {!Catalog.Value.t} values. The register VM and every frontend that evaluates an expression use it. The aggregate folds are {!Json1.aggregate}. *) val random_bits : unit -> int64 (** [random_bits ()] is 64 pseudo-random bits from the generator of [random()] and [randomblob()]. There is one generator per domain, seeded by the operating system on first use. An engine uses it for its own random values, such as a free rowid, as sqlite3 uses [sqlite3_randomness]. *) val exists : string -> bool (** [exists name] is [true] iff [name], in lower case, is a scalar function that {!apply} implements at some argument count. A table-valued function ({!table_function}) is not a scalar function. *) val takes : string -> nargs:int -> bool (** [takes name ~nargs] is [true] iff {!apply} accepts a call of the scalar function [name], in lower case, with [nargs] arguments. A resolver uses it to refuse a wrong count when the statement is prepared, with the message {!unresolved} gives. A name that {!exists} does not know, or one this package does not implement, accepts every count. *) val window_name : string -> bool (** [window_name name] is [true] iff [name] is a built-in window function at some argument count. *) val window : string -> nargs:int -> bool (** [window name ~nargs] is [true] iff the window function [name], in lower case, takes [nargs] arguments: none for [row_number], [rank], [dense_rank], [percent_rank] and [cume_dist], one for [ntile], [first_value] and [last_value], two for [nth_value], and one to three for [lag] and [lead]. It is [false] for a name {!window_name} does not know. *) val window_misuse : string -> string (** [window_misuse name] is the sentence sqlite3 refuses a call of the window function [name] with where no window applies: ["misuse of window function name()"]. *) val apply : ?coll:Collation.t -> string -> Catalog.Value.t list -> Catalog.Value.t (** [apply ~coll name args] is the scalar function [name], such as [length], [substr], [round], [json_extract] or [printf], applied to [args]. [coll] is the collating sequence that [min], [max] and [nullif] compare their arguments under (defaults to BINARY); the other functions ignore it. @raise Catalog.Error on a domain error, such as a bad [ESCAPE]. @raise Catalog.Error with ["no such function: f"] for an unknown name, and ["wrong number of arguments to function f()"] for a wrong argument count. *) val date_call : ?now:int64 -> ?zone:(int64 -> int) -> string -> (Catalog.Value.t list -> Catalog.Value.t) option (** [date_call ~now ~zone name] is [Some f] with [f] the date and time function [name] under the given clock, or [None] if [name] is not one. The names are [date], [time], [datetime], [julianday], [unixepoch] and [strftime], and the three [CURRENT_*] keywords, which take no argument. [now] and [zone] are as in {!Datetime.call}, and are all these functions read beyond their arguments. Without them, [f] is as {!apply}: ['now'] is [Null] and the time zone is UTC. *) val unresolved : string -> 'a (** [unresolved name] raises the error {!apply} raises for a call it cannot apply: ["no such function: f"] for an unknown name, ["function f() is not supported yet"] for a function this package does not implement, and ["wrong number of arguments to function f()"] for any other name, aggregates and window functions included. @raise Catalog.Error always. *) val reads_the_clock : string -> Catalog.Value.t list -> bool (** [reads_the_clock name args] is [true] iff applying [name] to [args] reads the clock or the time zone of the machine ({!Datetime.reads_the_clock}). Only date and time functions do, and only with some arguments. A stored expression refuses such a call, as SQLite does (src/date.c). *) val deterministic : string -> bool (** [deterministic name] is [true] iff [name] returns the same value for the same arguments. Only such a function may appear in an index key, a partial index predicate or a generated column. As in SQLite (src/func.c), the exceptions are [random], [randomblob], [changes], [total_changes], [last_insert_rowid], [sqlite_version] and [sqlite_source_id]. Date and time functions and unknown names are deterministic. *) val aggregate : string -> nargs:int -> bool (** [aggregate name ~nargs] is [true] iff a call of [name] with [nargs] arguments is an aggregate over a group, not a scalar over a row. The count matters: [max(v)] is an aggregate and [max(a, b)] a scalar, as in SQLite. A star [count] takes no argument, [group_concat] one or two, [string_agg] two, and every other aggregate one. *) val json_arrow : as_text:bool -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [json_arrow ~as_text doc path] is the value at [path] in the JSON [doc], as the [->] operator returns it if [as_text] is [false] and the [->>] operator if it is [true]. *) val json_arguments : string -> Ast.expr list -> Ast.expr list (** [json_arguments name args] is the arguments of a call of [name] with each one whose value sqlite3 tags with the JSON subtype, and which [name] reads as JSON for that tag, rewritten to answer its JSON as JSONB ({!Json1.embed}). The tag reaches an argument from a call of [json], [json_array], [json_extract] (an object or array), [json_object], [json_quote] or an editor, or from [->], through the branches of a [CASE], an [iif], a [coalesce] or an [ifnull]; a [json_group_array] or [json_group_object] there becomes its [jsonb_] form. Every other argument is unchanged. *) val table_functions : string list (** [table_functions] is the names of the table-valued functions {!table_function} answers. *) type table_function = { columns : string list; (** The columns a [*] lists, in order. *) hidden : string list; (** The HIDDEN columns after them, one per argument in argument order, which a name reads and a [*] leaves out. *) rows : Catalog.Value.t list -> Catalog.Value.t list list; (** [rows args] is the rows of a call with [args], over {!field-columns}. *) arguments : Catalog.Value.t list -> Catalog.Value.t list; (** [arguments args] is the value of each {!field-hidden} column in the rows of a call with [args]: the argument as the function reads it, with its default where the call left it out. *) } (** The type for table-valued functions. *) val table_function : string -> table_function (** [table_function name] is the table-valued function [name], such as [generate_series], whose hidden columns are [start], [stop] and [step]. @raise Catalog.Error if there is none of that name. *) val find_table_function : string -> table_function option (** [find_table_function name] is the table-valued function [name], compared as an identifier, or [None] if there is none of that name. *)