1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright (C) 2026 Gabriel Shea
// Licensed under either of the Apache License, Version 2.0 or the MIT
// license, at your option.
//! **How this engine spells a surrogate key** — the one thing about the key a
//! dialect decides.
//!
//! Razeri derives every surrogate key one way:
//!
//! ```text
//! guard( hash( normalize(part₁) ‖ '|' ‖ … ‖ normalize(partₙ) ) )
//! ```
//!
//! That derivation is Razeri's and is composed in one place. What varies
//! between engines is only the **vocabulary** — what this engine calls `LOWER`,
//! what it calls `TRIM`, how it joins strings, how it casts a value to text,
//! and how it spells a 64-bit hash. [`SidSpelling`] is that vocabulary, and a
//! dialect states it by overriding
//! [`SqlRenderer::sid_spelling`](crate::SqlRenderer::sid_spelling).
//!
//! Two expressions built from this struct differ **only** in these fields,
//! which is what makes *"provably the same key on every engine"* a proof
//! rather than an assumption.
//!
//! # If your engine has no 64-bit hash
//!
//! Say so rather than reaching for the nearest-looking function. A hash that is
//! deterministic *within* one warehouse and equal to nothing outside it is the
//! exact lock-in a portable key exists to end — a warehouse keyed with it
//! reconciles against nothing. The honest answers are an artifact your dialect
//! installs (name it in [`SidSpelling::hash`] and emit its definition from your
//! prerequisites), or a refusal that says what the reader should do instead.
/// The [`SidSpelling::platform`] of a dialect that has not stated its spelling.
///
/// The default [`SidSpelling`] is ANSI's vocabulary, which is a **guess** and
/// not a claim about any platform. It has to exist — a dialect must render
/// *something* — and it has to be distinguishable from the agnostic renderer's
/// answer, which is ANSI on purpose. Otherwise an unspelled platform gets ANSI
/// everywhere, every producer agrees, and Razeri's cross-checks pass over a
/// platform nobody has spelled: a check that cannot see its own blind spot.
///
/// **A dialect that leaves this value in place is telling Razeri it has not
/// decided**, and the conformance kit reports it as an unanswered obligation.
pub const UNKNOWN_PLATFORM: &str = "unknown";
/// How a dialect spells the canonical lowercase.
///
/// The canonical lowercase is fixed as **locale-independent simple case
/// mapping, one code point to one code point, with no combining mark introduced
/// by the operation**, and it renders as the engine's own `LOWER` over a value
/// whose two divergent code points have already been substituted away:
///
/// ```text
/// <function>(<replace>(<replace>(x, 'İ', 'I'), 'Σ', 'σ'))
/// ```
///
/// # Why the substitution is unconditional
///
/// Whether an engine's `LOWER` is the simple mapping or the **full** one is not
/// a property of the engine. It is a property of the engine **and its collation
/// provider**, and the provider is chosen when a database is created — so the
/// same server answers both ways. Measured on one `PostgreSQL` 17.9 instance:
/// under a libc collation `lower('İ')` is a bare `i`, and in a database created
/// with the ICU provider it is `i` + U+0307 and `ΟΔΟΣ` lowercases with a final
/// `ς`. Classifying the engine therefore classifies the wrong thing, and the
/// engine that is classified wrong keys one value to two different numbers.
///
/// So the substitution is applied **everywhere**, which costs nothing because
/// it is a no-op wherever the lowercase is already simple: `'İ'` → `'I'`
/// lowercases to `i` either way, and `'Σ'` → `'σ'` is already lowercase and is
/// left alone. That makes the composed key **collation-independent** rather
/// than correct-on-the-configurations-somebody-tried.
///
/// # The two code points, and why they are two facts
///
/// - **`İ` (U+0130) — the unconditional divergence.** Its full lowercase adds a
/// combining mark the canonical mapping excludes by name (`i` followed by
/// U+0307), and it is the **only** code point whose simple and full mappings
/// differ with no context at all. Replacing it with `I` first makes the
/// composition exactly the simple mapping;
/// - **`Σ` (U+03A3) — the conditional divergence.** Its full lowercase is `σ`
/// *except* word-finally, where Unicode's `Final_Sigma` condition makes it
/// `ς` (U+03C2). No enumeration of unconditional mappings can see this: a
/// different quantifier hides a different letter. Replacing every `Σ` with an
/// already-lowercase `σ` first removes the context the rule reads, and
/// `LOWER` then leaves it alone.
///
/// Note the direction: `ς` is never folded into `σ`. Its own simple lowercase
/// is itself, so a substitution that merged the two would be a *third*
/// normalization rather than a repair.
///
/// # What a dialect has to answer
///
/// Two function names, and — where the engine matches a replacement pattern by
/// something other than the code point — how to pin that match. An engine whose
/// replacement is collation-sensitive is the one to watch: under a
/// case-insensitive collation `REPLACE(x, 'Σ', 'σ')` matches `ς` as well, and
/// silently performs the third normalization. Measured on SQL Server 2022 with
/// its default collation, cured by [`Lower::match_collation`].
/// How a dialect casts one call-site argument to text.
///
/// The hash is a hash **of a string** and a call-site argument is a raw column
/// of its own declared type, so the cast is not optional.
/// How a dialect spells `TRIM`.
/// How a dialect joins the normalized parts of a composite key.
/// One platform's vocabulary for the surrogate-key derivation — the only thing
/// that varies between engines.
/// **ANSI's vocabulary** — the spelling a dialect inherits until it states its
/// own, and the agnostic renderer's answer on purpose.
///
/// A dialect building its own arm should start from this and change only the
/// fields its engine actually spells differently:
///
/// ```
/// use razeri_plugin_contract::{ANSI_SID_SPELLING, SidSpelling, TextCast};
///
/// const MY_ENGINE: SidSpelling = SidSpelling {
/// platform: "my-engine",
/// hash: ("my_xxhash64(", ")"),
/// cast_type: TextCast::Direct("STRING"),
/// through_helper: false,
/// ..ANSI_SID_SPELLING
/// };
/// ```
pub const ANSI_SID_SPELLING: SidSpelling = SidSpelling ;
/// **The language a surrogate-key helper's body is written in**, where that
/// body is not SQL.
///
/// The two named languages are the ones Razeri's function renderers already
/// spell a `LANGUAGE` clause for. Anything else is [`Named`](Self::Named),
/// which carries the keyword through unchanged.
/// **The whole body of the surrogate-key helper, supplied by the dialect.**
///
/// Razeri normally builds the helper's body itself: it is the one derivation,
/// composed from this engine's [`SidSpelling`] in this engine's own SQL. A
/// dialect answers with this type only when that is impossible — the engine has
/// no 64-bit hash in any SQL spelling, so there is no vocabulary to compose
/// from and the definition has to be written in another language.
///
/// Answering here does **not** change the key. The body must compute XXH64 with
/// seed 0 over the UTF-8 bytes of its single already-normalized argument, and
/// return it as a signed 64-bit integer — the same number every other engine
/// returns for the same input. A body that computes something else keys a
/// warehouse to numbers nothing else reproduces, which is the lock-in a
/// portable key exists to end. Measure it against the canonical number before
/// you ship it.
///
/// The body is the function's inner text only: Razeri writes the `CREATE
/// FUNCTION` envelope, the parameter, the return type and the `LANGUAGE`
/// clause around it.
/// The spelling a dialect that has decided nothing renders — ANSI's vocabulary
/// under the name that says it is a guess.
pub const UNSPELLED_SID: SidSpelling =
SidSpelling ;