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
354
355
356
357
358
// 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( part₁ ) ) one column
//! guard( hash( escape(part₁) ‖ '|' ‖ … ‖ escape(partₙ) ) ) two or more
//! ```
//!
//! That derivation is Razeri's and is composed in one place
//! ([`crate::sid`]). What varies between engines is only the **vocabulary** —
//! how this engine writes a replacement and a Unicode literal, 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 this engine writes a replacement and a Unicode literal**, and what
/// it calls `LOWER`.
///
/// The key changes no letter: it is the hash of the value as the table stores
/// it. These words are read where a value's characters are replaced or
/// matched one by one — the escape inside a composite key
/// ([`crate::sid::escape_part`]), a column's `clean: lowercase` and
/// `clean: uppercase` steps (through [`AsciiFold::Replacements`]), and the
/// reading of an amount the source wrote its own way.
///
/// # A replacement that matches by collation
///
/// An engine whose replacement matches a pattern under the value's own
/// collation, rather than by the character, has to say so: under a
/// case-insensitive collation a replacement of one letter also replaces its
/// other case. 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 parts of a composite key.
/// **How a column's `clean:` step changes the case of a letter** — on this
/// engine, for the twenty-six ASCII letters and nothing else.
///
/// A fold is never the engine's `LOWER` or `UPPER`: what they do to a letter
/// outside `a` to `z` belongs to the database's collation, so the same value
/// would be stored two ways on two databases. The default spells the fold in
/// plain SQL that no collation reads, twenty-six nested replacements. An engine
/// that holds a function doing exactly that fold names it here, and the step is
/// one call.
/// 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 argument, as given, 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 ;