Skip to main content

inillucent_sql/bind/
collation.rs

1//! Which collation a comparison, a sort or a grouping uses, and where an
2//! explicit `COLLATE` comes from.
3//!
4//! Invariant: **every collation the binder decides comes from the rules in
5//! this file, and they are SQLite's `sqlite3ExprCollSeq` and
6//! `sqlite3BinaryCompareCollSeq`, graded against the pinned 3.53.4 shell.**
7//! The executor's `expression_collation` used to be a second copy of them
8//! that looked only at the top node, and the binder's own two helpers did the
9//! same, so an explicit `COLLATE` inside an operand of `||` reached neither a
10//! comparison nor a `GROUP BY` (task-2089). One copy, here, is what keeps a
11//! comparison, an `ORDER BY`, a `DISTINCT` and a `PARTITION BY` agreeing
12//! about which values are equal. The cases that grade these rules are
13//! `crates/inillucent-compat/tests/corpora/differential-part8/task2089.cases`.
14
15use inillucent_value::{Affinity, Collation};
16
17use super::BoundExpr;
18use crate::ast::UnaryOp;
19
20impl BoundExpr {
21    /// Returns the collation this expression carries, if it has one.
22    ///
23    /// SQLite's `sqlite3ExprCollSeq`, in its order: a column has its declared
24    /// collation, a `CAST` and a unary `+` have their operand's, and any other
25    /// expression has the explicit collation of an operand, if one has one.
26    /// So `CAST(n AS TEXT) = 'A'` on a NOCASE column `n` compares with NOCASE,
27    /// and `n || '' = 'A'` compares with BINARY. Measured against 3.53.4
28    /// (task-2089): `CAST(n AS TEXT) = 'A'` and `+n = 'A'` answered 0 here
29    /// where SQLite answers 1.
30    pub fn collation(&self) -> Option<Collation> {
31        match self {
32            BoundExpr::Column { collation, .. } => Some(*collation),
33            BoundExpr::Cast { operand, .. }
34            | BoundExpr::Unary {
35                op: UnaryOp::Identity,
36                operand,
37            } => operand.collation(),
38            other => other.explicit_collation(),
39        }
40    }
41
42    /// Returns the collation an explicit `COLLATE` forced on this expression.
43    ///
44    /// This is *not* the same question as [`BoundExpr::collation`]. A column
45    /// declared `COLLATE NOCASE` has an implicit collation; `x COLLATE BINARY`
46    /// has an explicit one, and an explicit collation on either side of a
47    /// comparison beats an implicit one on the other side.
48    ///
49    /// **An explicit collation reaches up through every operator and function
50    /// argument (task-2089).** SQLite marks a node `EP_Collate` when any
51    /// operand has the mark, and reads the collation from the first operand
52    /// that has it, left first. This used to look only at the top node, so
53    /// `('a' COLLATE NOCASE || 'x') = 'AX'` compared with BINARY and answered
54    /// 0 where 3.53.4 answers 1, and `'a' COLLATE BINARY || 'b' COLLATE
55    /// NOCASE` has to answer BINARY because the left operand is asked first.
56    /// [`BoundExpr::children`] lists operands in SQLite's order for every node
57    /// whose value is text. A scalar subquery has no children here, and SQLite
58    /// does not carry a `COLLATE` out of one either. An aggregate or window
59    /// call has no children here either, because its arguments live in the
60    /// block's lists, so its reference carries the answer for them: see
61    /// [`explicit_argument_collation`].
62    pub fn explicit_collation(&self) -> Option<Collation> {
63        match self {
64            BoundExpr::Collate { collation, .. } => Some(*collation),
65            BoundExpr::Aggregate { collation, .. } | BoundExpr::WindowRef { collation, .. } => {
66                *collation
67            }
68            other => other
69                .children()
70                .into_iter()
71                .find_map(BoundExpr::explicit_collation),
72        }
73    }
74}
75
76/// Returns the explicit collation a call's arguments carry, left first.
77///
78/// This is what an aggregate or a window reference answers for
79/// [`BoundExpr::explicit_collation`]. SQLite reads an aggregate call's
80/// collation from its first argument with `EP_Collate`, so
81/// `max(s COLLATE NOCASE) = 'C'` and `group_concat(s, ',' COLLATE NOCASE) =
82/// 'A,B,C,A,B,C'` both compare with NOCASE and 3.53.4 answers each with 1.
83/// Before task-2094 the reference hid its arguments and both answered 0.
84///
85/// @param arguments - the call's bound arguments, in the order written
86pub(super) fn explicit_argument_collation(arguments: &[BoundExpr]) -> Option<Collation> {
87    arguments.iter().find_map(BoundExpr::explicit_collation)
88}
89
90/// Returns the collation a result column compares with.
91///
92/// `DISTINCT` and `GROUP BY` compare result values, and a NOCASE column makes
93/// `blue` and `Blue` the same value for both. Comparing them with BINARY
94/// instead returns more rows than SQLite does, which looks like a duplicate
95/// rather than like a bug.
96pub fn result_collation(expr: &BoundExpr) -> Collation {
97    expr.explicit_collation()
98        .or_else(|| expr.collation())
99        .unwrap_or(Collation::Binary)
100}
101
102/// Returns the affinity and collation a comparison between two operands uses.
103///
104/// SQLite's rule, in order: if either side has a column affinity the comparison
105/// applies it, with the left side winning. The collation is an explicit one on
106/// the left operand, then an explicit one on the right, then the left
107/// operand's implicit one, then the right's, and otherwise BINARY. "On an
108/// operand" includes anywhere inside it: see [`BoundExpr::explicit_collation`].
109///
110/// @param left - the comparison's left operand
111/// @param right - the comparison's right operand
112pub fn comparison_rules(left: &BoundExpr, right: &BoundExpr) -> (Option<Affinity>, Collation) {
113    let affinity = match (left.affinity(), right.affinity()) {
114        (Some(left), Some(right)) => inillucent_value::compare::comparison_affinity(left, right),
115        (Some(left), None) => Some(left),
116        (None, Some(right)) => Some(right),
117        (None, None) => None,
118    };
119    let collation = left
120        .explicit_collation()
121        .or_else(|| right.explicit_collation())
122        .or_else(|| left.collation())
123        .or_else(|| right.collation())
124        .unwrap_or(Collation::Binary);
125    (affinity, collation)
126}
127
128/// Wraps an expression in the collation an explicit `COLLATE` names.
129///
130/// **A `COLLATE` above a comparison does not reach the comparison (task-1979,
131/// F5).** `a = b COLLATE NOCASE` parses as `a = (b COLLATE NOCASE)`, because
132/// `COLLATE` binds tighter than `=`, and the comparison then reads NOCASE off
133/// its own right operand through [`comparison_rules`]. `(a = b) COLLATE
134/// NOCASE` is the other tree: the comparison is finished and NOCASE applies to
135/// the integer it produced, where a text collation does nothing. This function
136/// used to stamp the collation onto a `BoundExpr::Compare` it was handed, which
137/// made the two trees answer the same and made the outer name win over the
138/// inner one: measured against 3.53.4, `SELECT ('B'<'a') COLLATE NOCASE`
139/// answered 0 where SQLite answers 1, and
140/// `SELECT ('a' = 'A' COLLATE NOCASE) COLLATE BINARY` answered 0 where SQLite
141/// answers 1 because the inner NOCASE is the comparison's and the outer BINARY
142/// is the result's.
143///
144/// The wrapper is what carries the collation onward: [`comparison_rules`] asks
145/// an operand for its [`BoundExpr::explicit_collation`], so a `COLLATE` on a
146/// literal still reaches the comparison that uses it.
147///
148/// @param expr - the operand the `COLLATE` was written on
149/// @param collation - the collation it names
150pub(super) fn apply_collation(expr: BoundExpr, collation: Collation) -> BoundExpr {
151    BoundExpr::Collate {
152        operand: Box::new(expr),
153        collation,
154    }
155}
156
157#[cfg(test)]
158mod tests;