Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
open Astopen Value_ops
(* A comparison's value: NULL if either operand is NULL, else 1/0 for the ordering the two operands take under the comparison's affinity and collating sequence ({!Value_ops.compare_operands}). *)let compare_op ?aff ?coll op a b = match (a, b) with | Null, _ | _, Null -> Null | _ -> ( let c = compare_operands ?aff ?coll a b in match op with | Eq -> bool (c = 0) | Neq -> bool (c <> 0) | Lt -> bool (c < 0) | Le -> bool (c <= 0) | Gt -> bool (c > 0) | Ge -> bool (c >= 0) | And | Or | Like | Glob | Add | Sub | Mul | Div | Mod | Bit_and | Bit_or | Shl | Shr | Concat | Json_get | Json_get_text -> assert false)
(* [v IN (candidates)] as a three-valued membership: an empty set is always false (so [NOT IN ()] is always true, even for a NULL left operand); else a NULL left operand is NULL; else 1 if any candidate compares equal, NULL if none match but a NULL candidate is present, 0 otherwise. *)let in_eval ?aff ?coll v candidates = if candidates = [] then Int 0L else if equal_value v Null then Null else let cmp = List.map (fun x -> compare_op ?aff ?coll Eq v x) candidates in if List.exists (fun r -> equal_value r (Int 1L)) cmp then Int 1L else if List.exists (fun r -> equal_value r Null) cmp then Null else Int 0L
(* The value of a binary operator with no per-element opcode and no affinity: the three-valued AND/OR (both operands evaluated -- SQLite's AND/OR are pure 3VL, not short-circuit), LIKE/GLOB pattern tests, [||] concatenation, and the JSON [->]/[->>] accessors. Comparison goes through {!compare_op}, and arithmetic through the register VM, so neither reaches here. *)let binop_value op va vb = match op with | And -> value_of_tri (tri_and (tri_of va) (tri_of vb)) | Or -> value_of_tri (tri_or (tri_of va) (tri_of vb)) | Like -> like_op va vb | Glob -> glob_op va vb | Concat -> concat_op va vb | Json_get -> Func.json_arrow ~as_text:false va vb | Json_get_text -> Func.json_arrow ~as_text:true va vb | Eq | Neq | Lt | Le | Gt | Ge | Add | Sub | Mul | Div | Mod | Bit_and | Bit_or | Shl | Shr -> assert false
(* The value of [v IN (cands)]: each candidate is compared to [v] under the membership's affinity and sequence, then the three-valued {!in_eval} decides. *)let in_list_value ?aff ?coll v cands = in_eval ?aff ?coll v cands
(* [v IN (candidates)] answered by binary search rather than by comparing [v] with each candidate, for a list one query asks of every row. Every affinity but TEXT converts each operand on its own ({!Value_ops.coerce_compare}), so the candidates are converted once and sorted under the comparison's own order, and an equal candidate is one the search lands on. TEXT renders a number as text only against a text operand: a text [v] is looked up among the candidates rendered, and a number among them as they are, then as the text it spells. A search needs a total order and a NaN promises none, so a list holding one, or a NaN [v], is compared in turn by {!in_eval}. *)type in_set = { aff : affinity option; coll : Collation.t option; candidates : value list; has_null : bool; ordered : value array; rendered : value array Lazy.t; has_nan : bool;}
let is_nan = function Float f -> Float.is_nan f | _ -> false
let in_set_order coll = match coll with Some c -> compare_values_coll c | None -> compare_values
let sorted_by order vs = let a = Array.of_list vs in Array.stable_sort order a; a
let in_set ?aff ?coll candidates = let order = in_set_order coll in let present = List.filter (fun v -> not (equal_value v Null)) candidates in let own v = match aff with Some (`Numeric | `Integer | `Real) -> num_coerce v | _ -> v in let render = function | (Int _ | Float _) as v -> Text (text_of_value v) | v -> v in { aff; coll; candidates; has_null = List.length present <> List.length candidates; ordered = sorted_by order (List.map own present); rendered = lazy (sorted_by order (List.map render present)); has_nan = List.exists is_nan present; }
(* Whether [sorted] holds a value [order] puts level with [v]. *)let holds order sorted v = let rec go lo hi = if lo >= hi then false else let mid = lo + ((hi - lo) / 2) in let c = order sorted.(mid) v in if c = 0 then true else if c < 0 then go (mid + 1) hi else go lo mid in go 0 (Array.length sorted)
let in_set_mem s v = if s.candidates = [] then Int 0L else if equal_value v Null then Null else if s.has_nan || is_nan v then in_eval ?aff:s.aff ?coll:s.coll v s.candidates else let order = in_set_order s.coll in let found = match (s.aff, v) with | Some (`Numeric | `Integer | `Real), _ -> holds order s.ordered (num_coerce v) | Some `Text, Text _ -> holds order (Lazy.force s.rendered) v | Some `Text, (Int _ | Float _) -> holds order s.ordered v || holds order s.ordered (Text (text_of_value v)) | _ -> holds order s.ordered v in if found then Int 1L else if s.has_null then Null else Int 0L