(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** sqlite3's value semantics: what the register VM ({!Vdbe}) compares, tests and computes by, and what the IR ({!Ir}) evaluates a comparison, a logical operator, a sort key and an aggregate by. The VM is {!Vdbe_core.Make} applied to this module. *) type affinity = Value_ops.affinity (** The type for a comparison's affinity: the one character {!Value_ops.compare_affinity} computes for a pair of operands. *) val truth : Catalog.Register.t -> bool (** [truth m] is the truth of [m] in a [WHERE] or [ON] test (SQLite lang_expr.html section 14): [m] cast to NUMERIC as a [CAST] would cast it, true unless that is zero. So a text or blob is true only when it leads with a non-zero number -- ["5x"] is true where ["abc"], [""] and ["0"] are false -- and NULL, which SQL leaves unknown, is false to a filter that has to decide. *) val compare : Catalog.Register.t -> Catalog.Register.t -> int (** [compare a b] orders [a] and [b] as sqlite3 orders values: NULL, then numbers by value, then text by bytes, then blobs. The caller handles NULL for three-valued logic. *) val compare_with : affinity option -> Catalog.Register.t -> Catalog.Register.t -> int (** [compare_with aff a b] is {!compare} with the comparison affinity [aff] applied to both registers first, as [OP_Eq]..[OP_Ge] apply their P5 (src/vdbe.c). It reads text as a number through {!Value_ops.whole_number} and orders it against a real exactly, never by widening the integer to a double, so a pair orders the same way whichever storage class each side happens to arrive in. *) val settles : affinity option -> bool (** [settles aff] is [true] iff [aff] is numeric, the affinities {!settle} converts under. *) val settle : affinity option -> Catalog.Register.t -> unit (** [settle aff r] makes a text [r] the number it spells ({!Value_ops.whole_number}) where [aff] is numeric, as applyNumericAffinity makes it before a comparison (src/vdbe.c), and leaves any other [r] as it is: what a TEXT affinity converts depends on the other operand. A text left as it is under a numeric affinity then spells no number. *) val compare_settled : affinity option -> Catalog.Register.t -> Catalog.Register.t -> int (** [compare_settled aff a b] is {!compare_with} [aff a b] for a [b] {!settle} [aff] has settled: a text [b] under a numeric affinity is known to spell no number, so only [a] is read as one. *) val arith : Semantics.arith -> Catalog.Register.t -> Catalog.Register.t -> Catalog.Register.t -> unit (** [arith op dst a b] sets [dst] to [a op b] as sqlite3 computes it: NULL when either is NULL, an integer kept integer unless it overflows to a real, a division by zero NULL, a NaN result NULL, and text or a blob read for its leading number. *) (** {1:values Values} *) type collation = Collation.t (** The type for collating sequences: sqlite3's [BINARY], [NOCASE] and [RTRIM]. *) (** The type for the binary operators the VM has no opcode for: [LIKE], [GLOB], [||] and the JSON accessors [->] and [->>]. *) type op = Like | Glob | Concat | Json_get | Json_get_text (** The type for what a trigger program's [RAISE()] does to the statement that fired it, as sqlite3 codes it into OP_Halt's P2 operand: [Ignore] abandons the trigger program and leaves the row alone, [Abort] undoes the statement, [Fail] stops at the row and keeps the ones before it, and [Rollback] backs out the transaction. *) type action = Ignore | Abort | Fail | Rollback val collate : collation -> Catalog.Value.t -> Catalog.Value.t -> int (** [collate c a b] is {!Value_ops.compare_values_coll}[ c a b]. *) val key : collation -> string -> string (** [key c s] is {!Collation.key}[ c s]. *) val compare_value : Semantics.cmp -> affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [compare_value op aff coll a b] is {!Value_op.compare_op}[ ?aff ?coll]: NULL if either operand is NULL, else [1] or [0]. *) val is_eq : affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [is_eq aff coll a b] is {!Value_ops.is_value}[ ?aff ?coll a b]. *) val in_list : affinity option -> collation option -> Catalog.Value.t -> Catalog.Value.t list -> Catalog.Value.t (** [in_list aff coll v cands] is {!Value_op.in_list_value}[ ?aff ?coll v cands]: sqlite3 takes a list's affinity from the left operand alone (src/expr.c). *) val and_ : Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [and_ a b] is the three-valued [a AND b], each operand read by {!Value_ops.tri_of}. *) val or_ : Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [or_ a b] is the three-valued [a OR b]. *) val not_ : Catalog.Value.t -> Catalog.Value.t (** [not_ v] is {!Value_ops.not_value}[ v]. *) val op_value : op -> Catalog.Value.t -> Catalog.Value.t -> Catalog.Value.t (** [op_value op a b] is the value of [a op b] ({!Value_op.binop_value}). *) val op_calls : op -> bool (** [op_calls op] is [true] for [LIKE], [GLOB], [->] and [->>], which sqlite3 codes as function calls, and [false] for [||], which has an opcode. *) val deterministic : string -> bool (** [deterministic name] is {!Func.deterministic}[ name]: [true] unless [name] is a function sqlite3 does not flag [SQLITE_FUNC_CONSTANT] or [SQLITE_FUNC_SLOCHNG]. *) val aggregate : string -> nargs:int -> coll:collation option -> Semantics.agg_acc (** [aggregate name ~nargs ~coll] is {!Json1.aggregate}: the JSON aggregates, and {!Value_ops.agg_acc} for every other name. *) (** {1:unboxed Unboxed paths} *) val by_class : affinity option -> Catalog.Register.kind -> bool (** [by_class aff k] is [true] iff two values of class [k] compare unboxed under [aff] ({!Closure_core.SEMANTICS.by_class}): integers, reals and blobs under every affinity, which never converts two values of one of those classes, and texts under none, [BLOB] or [TEXT], which leave both sides text. *) val above_numbers : affinity option -> string -> bool (** [above_numbers aff s] is [true] iff [aff] is numeric and [s] spells no whole number ({!Value_ops.whole_number}): {!compare_with} leaves [s] as it is and orders every number below it. *) val spells_number : string -> first:int -> length:int -> bool (** [spells_number] is {!Value_ops.spells_number}. *) val stores_reals : affinity -> bool (** [stores_reals aff] is [true] iff [aff] is [REAL]. *) module Sum = Value_ops.Sum (** The fold of [SUM], [TOTAL] and [AVG]. *) (** {1:reads Typed reads} *) val to_int64 : Catalog.Value.t -> int64 (** [to_int64 v] is [v] as [sqlite3_column_int64] reads it: NULL is [0], a real is truncated towards zero and held to the [int64] range ({!Value_ops.intify}), and text or a blob is its leading integer, [0] if there is none. *) val to_float : Catalog.Value.t -> float (** [to_float v] is [v] as [sqlite3_column_double] reads it: NULL is [0.], and text or a blob is its leading number, [0.] if there is none. *) val to_text : Catalog.Value.t -> string (** [to_text v] is [v] as [sqlite3_column_text] reads it: a number is its text, as [CAST(x AS TEXT)] spells it, and NULL is [""]. *) (** {1:joins Hash join keys} *) val numeric : string -> float option (** [numeric s] is the number numeric affinity reads the text or blob [s] as ({!Value_ops.whole_number}), as a float, or [None] when [s] spells none and stays text, which never equals a number. *)