razeri_plugin_contract/sid_spelling.rs
1// Copyright (C) 2026 Gabriel Shea
2// Licensed under either of the Apache License, Version 2.0 or the MIT
3// license, at your option.
4
5//! **How this engine spells a surrogate key** — the one thing about the key a
6//! dialect decides.
7//!
8//! Razeri derives every surrogate key one way:
9//!
10//! ```text
11//! guard( hash( normalize(part₁) ‖ '|' ‖ … ‖ normalize(partₙ) ) )
12//! ```
13//!
14//! That derivation is Razeri's and is composed in one place. What varies
15//! between engines is only the **vocabulary** — what this engine calls `LOWER`,
16//! what it calls `TRIM`, how it joins strings, how it casts a value to text,
17//! and how it spells a 64-bit hash. [`SidSpelling`] is that vocabulary, and a
18//! dialect states it by overriding
19//! [`SqlRenderer::sid_spelling`](crate::SqlRenderer::sid_spelling).
20//!
21//! Two expressions built from this struct differ **only** in these fields,
22//! which is what makes *"provably the same key on every engine"* a proof
23//! rather than an assumption.
24//!
25//! # If your engine has no 64-bit hash
26//!
27//! Say so rather than reaching for the nearest-looking function. A hash that is
28//! deterministic *within* one warehouse and equal to nothing outside it is the
29//! exact lock-in a portable key exists to end — a warehouse keyed with it
30//! reconciles against nothing. The honest answers are an artifact your dialect
31//! installs (name it in [`SidSpelling::hash`] and emit its definition from your
32//! prerequisites), or a refusal that says what the reader should do instead.
33
34/// The [`SidSpelling::platform`] of a dialect that has not stated its spelling.
35///
36/// The default [`SidSpelling`] is ANSI's vocabulary, which is a **guess** and
37/// not a claim about any platform. It has to exist — a dialect must render
38/// *something* — and it has to be distinguishable from the agnostic renderer's
39/// answer, which is ANSI on purpose. Otherwise an unspelled platform gets ANSI
40/// everywhere, every producer agrees, and Razeri's cross-checks pass over a
41/// platform nobody has spelled: a check that cannot see its own blind spot.
42///
43/// **A dialect that leaves this value in place is telling Razeri it has not
44/// decided**, and the conformance kit reports it as an unanswered obligation.
45pub const UNKNOWN_PLATFORM: &str = "unknown";
46
47/// How a dialect spells the canonical lowercase.
48///
49/// The canonical lowercase is fixed as **locale-independent simple case
50/// mapping, one code point to one code point, with no combining mark introduced
51/// by the operation**, and it renders as the engine's own `LOWER` over a value
52/// whose two divergent code points have already been substituted away:
53///
54/// ```text
55/// <function>(<replace>(<replace>(x, 'İ', 'I'), 'Σ', 'σ'))
56/// ```
57///
58/// # Why the substitution is unconditional
59///
60/// Whether an engine's `LOWER` is the simple mapping or the **full** one is not
61/// a property of the engine. It is a property of the engine **and its collation
62/// provider**, and the provider is chosen when a database is created — so the
63/// same server answers both ways. Measured on one `PostgreSQL` 17.9 instance:
64/// under a libc collation `lower('İ')` is a bare `i`, and in a database created
65/// with the ICU provider it is `i` + U+0307 and `ΟΔΟΣ` lowercases with a final
66/// `ς`. Classifying the engine therefore classifies the wrong thing, and the
67/// engine that is classified wrong keys one value to two different numbers.
68///
69/// So the substitution is applied **everywhere**, which costs nothing because
70/// it is a no-op wherever the lowercase is already simple: `'İ'` → `'I'`
71/// lowercases to `i` either way, and `'Σ'` → `'σ'` is already lowercase and is
72/// left alone. That makes the composed key **collation-independent** rather
73/// than correct-on-the-configurations-somebody-tried.
74///
75/// # The two code points, and why they are two facts
76///
77/// - **`İ` (U+0130) — the unconditional divergence.** Its full lowercase adds a
78/// combining mark the canonical mapping excludes by name (`i` followed by
79/// U+0307), and it is the **only** code point whose simple and full mappings
80/// differ with no context at all. Replacing it with `I` first makes the
81/// composition exactly the simple mapping;
82/// - **`Σ` (U+03A3) — the conditional divergence.** Its full lowercase is `σ`
83/// *except* word-finally, where Unicode's `Final_Sigma` condition makes it
84/// `ς` (U+03C2). No enumeration of unconditional mappings can see this: a
85/// different quantifier hides a different letter. Replacing every `Σ` with an
86/// already-lowercase `σ` first removes the context the rule reads, and
87/// `LOWER` then leaves it alone.
88///
89/// Note the direction: `ς` is never folded into `σ`. Its own simple lowercase
90/// is itself, so a substitution that merged the two would be a *third*
91/// normalization rather than a repair.
92///
93/// # What a dialect has to answer
94///
95/// Two function names, and — where the engine matches a replacement pattern by
96/// something other than the code point — how to pin that match. An engine whose
97/// replacement is collation-sensitive is the one to watch: under a
98/// case-insensitive collation `REPLACE(x, 'Σ', 'σ')` matches `ς` as well, and
99/// silently performs the third normalization. Measured on SQL Server 2022 with
100/// its default collation, cured by [`Lower::match_collation`].
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub struct Lower {
103 /// The platform's lowercase function, in its own casing — `LOWER` on most
104 /// engines, `lowerUTF8` on one.
105 pub function: &'static str,
106 /// The platform's three-argument string-replacement function — `REPLACE` on
107 /// most engines, `replaceAll` on one.
108 pub replace: &'static str,
109 /// **How this engine spells a Unicode string literal** — what goes in front
110 /// of the opening quote, where that differs from an ordinary literal. Empty
111 /// on every engine whose ordinary literal is already Unicode; `"N"` on
112 /// T-SQL, where a bare literal is narrowed to the database's code page and
113 /// the two Greek letters do not survive it.
114 ///
115 /// The substitution's two patterns are the reason the field exists, and they
116 /// are not the only literal that has to survive: the equality gate
117 /// substitutes probe values where a column reference stands, and a bare
118 /// `'ΟΔΟΣ'` reaches a T-SQL hash as `???S`. Anything that writes one of the
119 /// corpus's values into this engine as a literal reads this field.
120 pub pattern_literal_prefix: &'static str,
121 /// The collation the substitution's subject is pinned to, where this engine
122 /// matches a replacement pattern **under the subject's collation** rather
123 /// than by code point.
124 ///
125 /// `None` on every engine whose replacement is already by code point.
126 /// Name a **binary** collation where it is not: under an accent- or
127 /// case-insensitive one, replacing `Σ` also replaces `ς`, which moves a
128 /// Greek key that was already correct.
129 pub match_collation: Option<&'static str>,
130}
131
132/// How a dialect casts one call-site argument to text.
133///
134/// The hash is a hash **of a string** and a call-site argument is a raw column
135/// of its own declared type, so the cast is not optional.
136#[derive(Debug, Clone, Copy, PartialEq, Eq)]
137pub enum TextCast {
138 /// `CAST(x AS <type>)` — the cast propagates NULL, so Razeri's NULL
139 /// sentinel is reached with the statement intact.
140 Direct(&'static str),
141 /// `assumeNotNull(CAST(x AS Nullable(<type>)))` — `ClickHouse`, whose
142 /// `CAST` to a non-`Nullable` type **raises** instead of propagating
143 /// (*"Cannot convert NULL to a non-nullable type"*).
144 ///
145 /// `ClickHouse` evaluates both arms of a `CASE`, so the sentinel cannot
146 /// save the statement: the emitted key expression failed outright on any
147 /// NULL natural-key part rather than returning the sentinel. That is not a
148 /// wrong key — it is a dimension load that does not run. The wrapper makes
149 /// the unreachable arm total; which value it would produce is irrelevant,
150 /// because the sentinel has already answered.
151 ClickHouseNullTolerant(&'static str),
152}
153
154/// How a dialect spells `TRIM`.
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum Trim {
157 /// `TRIM(x)`, in the platform's own casing.
158 Function(&'static str),
159 /// `LTRIM(RTRIM(x))` — T-SQL, whose `TRIM` requires SQL Server 2017. The
160 /// pair removes the same leading and trailing spaces.
161 LtrimRtrim,
162 /// `TRIM(x, ' ')` — the engines whose **bare** `TRIM` removes every
163 /// whitespace character rather than the ASCII space alone.
164 ///
165 /// The canonical trim is the ASCII space (`U+0020`) and nothing else, which
166 /// is why a tab-padded and a newline-padded probe are in the corpus and are
167 /// *not* expected to normalize away. `BigQuery` is the measured member:
168 /// `LENGTH(TRIM('\tBoston\t'))` is 6 there — the tab gone and the key moved
169 /// — while `LENGTH(TRIM('\tBoston\t', ' '))` is 8, and a non-breaking space
170 /// survives the second form and not the first.
171 ///
172 /// The explicit set is not a repair of the engine; it is the canonical
173 /// clause written in an engine whose default set is wider. `TRIM(x, ' ')`
174 /// and `TRIM(x)` are the same function on every platform whose bare form
175 /// already strips the space alone, so the variant states a difference
176 /// rather than introducing one.
177 ///
178 /// **Only a tab-padded probe on your engine tells you which variant is
179 /// yours** — run the corpus on the engine itself. An emulator can give the
180 /// opposite answer: the community `BigQuery` emulator strips the space
181 /// alone.
182 FunctionWithSpaceSet(&'static str),
183}
184
185/// How a dialect joins the normalized parts of a composite key.
186#[derive(Debug, Clone, Copy, PartialEq, Eq)]
187pub enum Concat {
188 /// `CONCAT(a, '|', b)`, in the platform's own casing.
189 Function(&'static str),
190 /// `CONCAT(CONCAT(a, '|'), b)` — the engines whose `CONCAT` takes
191 /// **exactly two arguments**, in the platform's own casing.
192 ///
193 /// Rendered by nesting pairwise, left to right, one call per pair: three
194 /// parts read `CONCAT(CONCAT(a, b), c)` where a variadic engine reads
195 /// `CONCAT(a, b, c)`. Db2 is the engine that needs it, and older Oracle
196 /// releases are the same shape.
197 ///
198 /// The distinction is not cosmetic. On such an engine the variadic
199 /// spelling is not a wider call to the same function, it is a call to a
200 /// function that is not there, so the first composite key stops the load
201 /// rather than reading oddly in review.
202 ///
203 /// The nesting joins the same parts with the same separator in the same
204 /// order, so the string that reaches the hash — and therefore the key — is
205 /// the one every other engine hashes. What differs is only how many calls
206 /// it takes to spell it.
207 FunctionBinary(&'static str),
208 /// `a + '|' + b` — T-SQL's string operator. Equal to `CONCAT` for non-NULL
209 /// operands, which Razeri's NULL sentinel is what guarantees: `+`
210 /// propagates NULL where `CONCAT` swallows it, and a swallowed NULL
211 /// silently collapses `('boston', NULL)` and `('boston', '')` into one
212 /// member.
213 Plus,
214 /// `a || '|' || b` — the engines that join strings with an operator, in
215 /// the operator's own spelling. `||` is the standard's, and the one an
216 /// engine with no `CONCAT` function usually takes.
217 ///
218 /// Rendered left to right, the operator between each part and the
219 /// separator: `Operator("||")` over three parts reads
220 /// `a || '|' || b || '|' || c`. One expression, whatever the number of
221 /// parts, and no function the warehouse has to hold.
222 ///
223 /// Like [`Plus`](Self::Plus), the operator propagates NULL where `CONCAT`
224 /// swallows it, and Razeri's NULL sentinel is what makes that safe: a key
225 /// with a NULL part is answered before the join is reached.
226 Operator(&'static str),
227}
228
229/// One platform's vocabulary for the surrogate-key derivation — the only thing
230/// that varies between engines.
231#[derive(Debug, Clone, Copy)]
232pub struct SidSpelling {
233 /// The name this dialect answers under. [`UNKNOWN_PLATFORM`] means the
234 /// dialect has not stated a spelling and is taking ANSI's guess.
235 pub platform: &'static str,
236 /// The canonical lowercase — this engine's own `LOWER` and the
237 /// substitution that runs in front of it.
238 pub lower: Lower,
239 /// The `TRIM` function, or the pair that stands in for it.
240 pub trim: Trim,
241 /// The join used for composite keys.
242 pub concat: Concat,
243 /// The platform's hash of an already-normalized text expression, as the
244 /// text before and after it. `("CAST(xxHash64(", ") AS Int64)")` reads
245 /// `CAST(xxHash64(x) AS Int64)`.
246 pub hash: (&'static str, &'static str),
247 /// The CAST that turns one argument into text. The hash is a hash OF A
248 /// STRING, and at the inline call sites the argument is a raw column of its
249 /// own declared type — `TRIM(DATE)` is a binder error, not a value.
250 pub cast_type: TextCast,
251 /// Whether the load path reaches the hash through Razeri's emitted helper
252 /// function instead of inlining it. Inlining is forced where a UDF cannot
253 /// call another UDF (`DuckDB` macros, `ClickHouse` lambdas) and chosen
254 /// where the platform's own function is already the derivation.
255 pub through_helper: bool,
256}
257
258/// **ANSI's vocabulary** — the spelling a dialect inherits until it states its
259/// own, and the agnostic renderer's answer on purpose.
260///
261/// A dialect building its own arm should start from this and change only the
262/// fields its engine actually spells differently:
263///
264/// ```
265/// use razeri_plugin_contract::{ANSI_SID_SPELLING, SidSpelling, TextCast};
266///
267/// const MY_ENGINE: SidSpelling = SidSpelling {
268/// platform: "my-engine",
269/// hash: ("my_xxhash64(", ")"),
270/// cast_type: TextCast::Direct("STRING"),
271/// through_helper: false,
272/// ..ANSI_SID_SPELLING
273/// };
274/// ```
275pub const ANSI_SID_SPELLING: SidSpelling = SidSpelling {
276 platform: "ansi",
277 lower: Lower {
278 function: "LOWER",
279 replace: "REPLACE",
280 pattern_literal_prefix: "",
281 match_collation: None,
282 },
283 trim: Trim::Function("TRIM"),
284 concat: Concat::Function("CONCAT"),
285 hash: ("xxhash64(", ")"),
286 cast_type: TextCast::Direct("TEXT"),
287 through_helper: true,
288};
289
290/// **The language a surrogate-key helper's body is written in**, where that
291/// body is not SQL.
292///
293/// The two named languages are the ones Razeri's function renderers already
294/// spell a `LANGUAGE` clause for. Anything else is [`Named`](Self::Named),
295/// which carries the keyword through unchanged.
296#[derive(Debug, Clone, Copy, PartialEq, Eq)]
297pub enum SidHelperLanguage {
298 /// JavaScript.
299 JavaScript,
300 /// Python.
301 Python,
302 /// A language keyword this contract does not name, spelled exactly as the
303 /// engine's `LANGUAGE` clause takes it.
304 Named(&'static str),
305}
306
307/// **The whole body of the surrogate-key helper, supplied by the dialect.**
308///
309/// Razeri normally builds the helper's body itself: it is the one derivation,
310/// composed from this engine's [`SidSpelling`] in this engine's own SQL. A
311/// dialect answers with this type only when that is impossible — the engine has
312/// no 64-bit hash in any SQL spelling, so there is no vocabulary to compose
313/// from and the definition has to be written in another language.
314///
315/// Answering here does **not** change the key. The body must compute XXH64 with
316/// seed 0 over the UTF-8 bytes of its single already-normalized argument, and
317/// return it as a signed 64-bit integer — the same number every other engine
318/// returns for the same input. A body that computes something else keys a
319/// warehouse to numbers nothing else reproduces, which is the lock-in a
320/// portable key exists to end. Measure it against the canonical number before
321/// you ship it.
322///
323/// The body is the function's inner text only: Razeri writes the `CREATE
324/// FUNCTION` envelope, the parameter, the return type and the `LANGUAGE`
325/// clause around it.
326#[derive(Debug, Clone, Copy, PartialEq, Eq)]
327pub struct SidHelperBody {
328 /// What the body is written in.
329 pub language: SidHelperLanguage,
330 /// The body itself.
331 pub body: &'static str,
332}
333
334/// The spelling a dialect that has decided nothing renders — ANSI's vocabulary
335/// under the name that says it is a guess.
336pub const UNSPELLED_SID: SidSpelling =
337 SidSpelling { platform: UNKNOWN_PLATFORM, ..ANSI_SID_SPELLING };
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342
343 #[test]
344 fn the_unspelled_default_differs_from_ansi_only_in_its_name() {
345 assert_eq!(UNSPELLED_SID.platform, UNKNOWN_PLATFORM);
346 assert_eq!(UNSPELLED_SID.hash, ANSI_SID_SPELLING.hash);
347 assert_eq!(UNSPELLED_SID.lower, ANSI_SID_SPELLING.lower);
348 assert_eq!(UNSPELLED_SID.trim, ANSI_SID_SPELLING.trim);
349 assert_eq!(UNSPELLED_SID.concat, ANSI_SID_SPELLING.concat);
350 assert_eq!(UNSPELLED_SID.cast_type, ANSI_SID_SPELLING.cast_type);
351 assert_eq!(UNSPELLED_SID.through_helper, ANSI_SID_SPELLING.through_helper);
352 }
353}