Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
type t = Binary | Nocase | Rtrim | Binary_utf16le | Binary_utf16be
let default = Binary
let binary = function | Encoding.Utf8 -> Binary | Encoding.Utf16le -> Binary_utf16le | Encoding.Utf16be -> Binary_utf16be
let is_binary = function | Binary | Binary_utf16le | Binary_utf16be -> true | Nocase | Rtrim -> false
let equal (a : t) b = a = b
let to_string = function | Binary | Binary_utf16le | Binary_utf16be -> "BINARY" | Nocase -> "NOCASE" | Rtrim -> "RTRIM"
let pp ppf c = Fmt.string ppf (to_string c)
let of_string name = match String.uppercase_ascii name with | "BINARY" -> Some Binary | "NOCASE" -> Some Nocase | "RTRIM" -> Some Rtrim | _ -> None
(* The length of [s.(off..off+len)] with its trailing spaces dropped. *)let rtrim_len s off len = let n = ref len in while !n > 0 && String.unsafe_get s (off + !n - 1) = ' ' do decr n done; !n
let key c s = match c with | Binary -> s | Binary_utf16le -> Encoding.of_utf8 Encoding.Utf16le s | Binary_utf16be -> Encoding.of_utf8 Encoding.Utf16be s | Nocase -> String.lowercase_ascii s | Rtrim -> String.sub s 0 (rtrim_len s 0 (String.length s))
(* The shared-prefix walks below take their operands rather than closing over them: written as a local [let rec] each builds a closure on the heap once per call, and these are the comparisons an index b-tree descends under -- once per level, once per rewind of a seek cursor -- where the whole point of reading a slice in place is that the comparison allocates nothing. *)let rec bytes_from_nocase a aoff b boff n i = if i >= n then 0 else let c = Char.compare (Char.lowercase_ascii (String.unsafe_get a (aoff + i))) (Char.lowercase_ascii (String.unsafe_get b (boff + i))) in if c <> 0 then c else bytes_from_nocase a aoff b boff n (i + 1)
(* A word read with no bounds check, and as big-endian order reads it: the callers below check each slice once, not each word. *)external get64u : string -> int -> int64 = "%caml_string_get64u"external bswap64 : int64 -> int64 = "%bswap_int64"
let[@inline] word_be s i = let w = get64u s i in if Sys.big_endian then w else bswap64 w
(* A slice the comparisons read lies inside its string; the exception is built once so that raising it is a jump, which keeps the hot paths free of a call. *)let outside = Invalid_argument "Sql.Collation: a slice outside its string"
let[@inline] check_slice s off len = if off < 0 || len < 0 || off > String.length s - len then raise outside
(* The [k < 8] bytes at [a.(aoff)] and [b.(boff)] that follow the slices' shared whole words, then their lengths [alen] and [blen]. *)let[@inline] compare_tail a aoff b boff k alen blen = if k = 0 then Int.compare alen blen else if aoff + 8 <= String.length a && boff + 8 <= String.length b then let shift = 64 - (8 * k) in let x = Int64.shift_right_logical (word_be a aoff) shift and y = Int64.shift_right_logical (word_be b boff) shift in if Int64.equal x y then Int.compare alen blen else Int64.unsigned_compare x y else begin let i = ref 0 in while !i < k && String.unsafe_get a (aoff + !i) = String.unsafe_get b (boff + !i) do incr i done; if !i < k then Char.compare (String.unsafe_get a (aoff + !i)) (String.unsafe_get b (boff + !i)) else Int.compare alen blen end
(* [String.compare] semantics on two slices, without materialising either. The shared prefix is compared a word a step as the bytes lie, and the word that differs answers by its unsigned order read big-endian, the order of its bytes. A tail of [k < 8] bytes is one word too, its low bytes shifted out, when both slices lie at least eight bytes before the end of their strings, as a record column on a b-tree page almost always does; else byte by byte. Then the lengths. Loops rather than recursion, so the comparison inlines into its caller -- an index descent's column compare -- and makes no call. *)let[@inline] compare_bytes a aoff alen b boff blen = check_slice a aoff alen; check_slice b boff blen; let n = if alen < blen then alen else blen in let i = ref 0 in while !i + 8 <= n && Int64.equal (get64u a (aoff + !i)) (get64u b (boff + !i)) do i := !i + 8 done; if !i + 8 <= n then Int64.unsigned_compare (word_be a (aoff + !i)) (word_be b (boff + !i)) else compare_tail a (aoff + !i) b (boff + !i) (n - !i) alen blen
(* NOCASE folds ASCII case, which preserves length, so folding each byte as it is read gives the same answer as comparing the two folded strings. *)let compare_bytes_nocase a aoff alen b boff blen = check_slice a aoff alen; check_slice b boff blen; let c = bytes_from_nocase a aoff b boff (if alen < blen then alen else blen) 0 in if c <> 0 then c else Int.compare alen blen
let compare_sub c a aoff alen b boff blen = match c with | Binary -> compare_bytes a aoff alen b boff blen | Nocase -> compare_bytes_nocase a aoff alen b boff blen | Rtrim -> check_slice a aoff alen; check_slice b boff blen; compare_bytes a aoff (rtrim_len a aoff alen) b boff (rtrim_len b boff blen) | (Binary_utf16le | Binary_utf16be) as c -> check_slice a aoff alen; check_slice b boff blen; String.compare (key c (String.sub a aoff alen)) (key c (String.sub b boff blen))
let compare_text c x y = compare_sub c x 0 (String.length x) y 0 (String.length y)