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 Ast
(* The comparison operators of [Ast.binop] -- the ones datatype3.html sec.7.1 gives a collating sequence of their own. Repeated from {!Lower} rather than taken from it: this module sits below the lowering, and the list is the sec.7.1 operator set, not the lowering's opcode grouping. *)let is_cmp : binop -> bool = function | Eq | Neq | Lt | Le | Gt | Ge -> true | _ -> false
(* {!explicit}, {!carried} and {!strip} name every constructor and none falls through a wildcard or {!Ast.map_expr}: which node passes a sequence on is a decision per constructor, so a new one is a match error here until it is made. *)let rec explicit e = let first = List.find_map explicit in match e with | Collate (_, c) -> Some c | Cast (e, _) | Affinity (e, _) | Unary_plus e | Unary_minus e | Unop_not e | Is_null e | Is_not_null e | Distinct_agg (_, e) -> explicit e | Binary (_, a, b) | Is (a, b) -> first [ a; b ] | In_list (a, l) -> first (a :: l) | In_select (a, _) -> explicit a | Func (_, args) -> first args | Filter (agg, _) | Ordered_agg (agg, _) -> explicit agg | Window (f, _) -> explicit f | Case (base, branches, els) -> first (Option.to_list base @ List.concat_map (fun (c, r) -> [ c; r ]) branches @ Option.to_list els) (* RAISE contributes no value to a comparison: it either aborts or abandons the row, so a COLLATE in its message names nothing here. *) | Lit _ | Truth _ | Numeral _ | Param _ | Named_param _ | Col _ | Star | Qualified_star _ | Scalar_select _ | Exists _ | Raise _ | Schema_col _ -> None
(* The sequence a name denotes. sqlite3 looks the name up where it codes the operation that reads it (sqlite3LocateCollSeq, src/callback.c) and stops the statement when nothing carries it, so a misspelt sequence is an error and never a silent BINARY. The three built-ins are the whole vocabulary. *)let resolve name = match Collation.of_string name with | Some c -> c | None -> Catalog.Error.fail_prepare "no such collation sequence: %s" name
(* The scalar functions that compare their arguments, so an explicit COLLATE on one names the sequence that comparison runs under. sqlite3 marks exactly these three SQLITE_FUNC_NEEDCOLL (src/func.c). *)let compares_its_arguments = function | "min" | "max" | "nullif" -> true | _ -> false
let rec carried e = let first = List.find_map carried in match e with | Collate (_, c) -> Some c | Cast (e, _) | Affinity (e, _) | Unary_plus e | Unary_minus e | Unop_not e | Is_null e | Is_not_null e -> carried e (* each of these compares under a sequence of its own, so an assignment below one names that comparison's and not this operand's *) | Binary (op, _, _) when is_cmp op -> None | Is _ | In_list _ | In_select _ -> None | Scalar_select _ | Exists _ -> None | Distinct_agg _ | Filter _ | Ordered_agg _ | Window _ -> None | Func (n, _) when compares_its_arguments n -> None | Binary (_, a, b) -> first [ a; b ] | Func (_, args) -> first args (* a simple CASE compares its base against each branch value and a searched one reads each condition as a predicate, so both are comparisons of their own; only the branch results are values this operand contributes *) | Case (_, branches, els) -> first (List.map snd branches @ Option.to_list els) | Lit _ | Truth _ | Numeral _ | Param _ | Named_param _ | Col _ | Star | Qualified_star _ | Raise _ | Schema_col _ -> None
let sequence e = Option.map resolve (carried e)
let call_sequence args = match List.find_map (fun a -> Option.map resolve (explicit a)) args with | Some Collation.Binary | None -> None | c -> c
(* Remove every wrapper the {!carried} search above consumed, and no other. The stopping cases are the operations that read a sequence of their own when they are lowered: peeling theirs here would leave them comparing under BINARY with nothing said, where keeping the wrapper leaves a node the lowering has no value for and the statement is refused instead. *)let rec strip e = let go = strip in match e with (* a plus over a literal carries no sequence, and keeps the literal from being the constant truth an AND or OR leaves its other operand for *) | Unary_plus (Lit _ | Numeral _) -> e | Collate (e, _) | Unary_plus e -> go e | Binary (op, _, _) when is_cmp op -> e | Is _ | In_list _ | In_select _ -> e | Scalar_select _ | Exists _ -> e | Distinct_agg _ | Filter _ | Ordered_agg _ | Window _ -> e | Func (n, _) when compares_its_arguments n -> e | Cast (e, t) -> Cast (go e, t) | Affinity (e, a) -> Affinity (go e, a) | Binary (op, a, b) -> Binary (op, go a, go b) | Unop_not a -> Unop_not (go a) | Unary_minus a -> Unary_minus (go a) | Is_null a -> Is_null (go a) | Is_not_null a -> Is_not_null (go a) | Func (n, args) -> Func (n, List.map go args) (* A simple CASE compares its base to each condition and a searched one lowers each condition as a predicate, so both read their own sequences; only the branch results are values this operand contributes. *) | Case (base, branches, els) -> Case (base, List.map (fun (c, r) -> (c, go r)) branches, Option.map go els) | Lit _ | Truth _ | Numeral _ | Param _ | Named_param _ | Col _ | Star | Qualified_star _ | Raise _ | Schema_col _ -> e
(* Which aggregate folds compare their argument values, and so read a collating sequence of their own. sqlite3 marks [min] and [max] SQLITE_FUNC_NEEDCOLL in src/func.c and nothing else among the built-in aggregates: every other fold counts, sums or joins its arguments without ordering them. *)let fold_compares name = match String.lowercase_ascii name with "min" | "max" -> true | _ -> false