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
//! Typed representation of a single generated value.
//!
//! Generators produce [`GeneratedValue`] rather than writing SQL literals
//! directly, so downstream stages (formatting, redaction interop, planner
//! arithmetic) can inspect and combine values without re-parsing text. Money
//! and other fixed-point quantities are represented as integer minor units
//! (e.g. cents) plus a scale, so planner arithmetic never touches floats.
use std::fmt;
use crate::diagnostic::{Diagnostic, DiagnosticBag, DiagnosticDefinition};
use crate::synthetic::schema::SqlTypeFamily;
/// A single value produced by a generator, independent of any target SQL
/// dialect's literal syntax.
#[derive(Debug, Clone, PartialEq)]
pub enum GeneratedValue {
/// SQL `NULL`.
Null,
/// The column's `DEFAULT` expression, left for the writer to emit.
Default,
Boolean(bool),
/// Any whole-number value, wide enough for `BIGINT` and beyond.
Integer(i128),
/// A fixed-point number as integer minor units (e.g. cents) plus scale,
/// e.g. `{ minor: 1050, scale: 2 }` for `10.50`.
Decimal {
minor: i128,
scale: u32,
},
Text(String),
Bytes(Vec<u8>),
/// A pre-formatted date/time/timestamp literal.
DateTime(String),
/// A pre-serialized JSON document.
Json(String),
}
impl GeneratedValue {
/// Whether this value represents SQL `NULL`.
pub fn is_null(&self) -> bool {
matches!(self, GeneratedValue::Null)
}
/// Borrow the value as a boolean, or a [`GenerateError::TypeMismatch`]
/// if it isn't one.
pub fn as_boolean(&self) -> Result<bool, GenerateError> {
match self {
GeneratedValue::Boolean(b) => Ok(*b),
other => Err(GenerateError::type_mismatch("Boolean", other)),
}
}
/// Borrow the value as an integer, or a [`GenerateError::TypeMismatch`]
/// if it isn't one.
pub fn as_integer(&self) -> Result<i128, GenerateError> {
match self {
GeneratedValue::Integer(i) => Ok(*i),
other => Err(GenerateError::type_mismatch("Integer", other)),
}
}
/// Borrow the value as `(minor units, scale)`, or a
/// [`GenerateError::TypeMismatch`] if it isn't a decimal.
pub fn as_decimal(&self) -> Result<(i128, u32), GenerateError> {
match self {
GeneratedValue::Decimal { minor, scale } => Ok((*minor, *scale)),
other => Err(GenerateError::type_mismatch("Decimal", other)),
}
}
/// Borrow the value as text, or a [`GenerateError::TypeMismatch`] if it
/// isn't one.
pub fn as_text(&self) -> Result<&str, GenerateError> {
match self {
GeneratedValue::Text(s) => Ok(s.as_str()),
other => Err(GenerateError::type_mismatch("Text", other)),
}
}
/// Borrow the value as bytes, or a [`GenerateError::TypeMismatch`] if it
/// isn't one.
pub fn as_bytes(&self) -> Result<&[u8], GenerateError> {
match self {
GeneratedValue::Bytes(b) => Ok(b.as_slice()),
other => Err(GenerateError::type_mismatch("Bytes", other)),
}
}
/// Borrow the value as a pre-formatted date/time literal, or a
/// [`GenerateError::TypeMismatch`] if it isn't one.
pub fn as_date_time(&self) -> Result<&str, GenerateError> {
match self {
GeneratedValue::DateTime(s) => Ok(s.as_str()),
other => Err(GenerateError::type_mismatch("DateTime", other)),
}
}
/// Borrow the value as a pre-serialized JSON document, or a
/// [`GenerateError::TypeMismatch`] if it isn't one.
pub fn as_json(&self) -> Result<&str, GenerateError> {
match self {
GeneratedValue::Json(s) => Ok(s.as_str()),
other => Err(GenerateError::type_mismatch("Json", other)),
}
}
/// Whether this value is a legal payload for a column of `family`,
/// independent of any particular generator. `Null` and `Default` are
/// legal for every family; every other variant is checked against the
/// one or two families it can represent (e.g. a `Uuid`-family column is
/// represented as `Text`, since [`GeneratedValue`] has no dedicated
/// UUID variant).
pub fn compatible_with(&self, family: &SqlTypeFamily) -> bool {
match self {
GeneratedValue::Null | GeneratedValue::Default => true,
GeneratedValue::Boolean(_) => matches!(family, SqlTypeFamily::Boolean),
GeneratedValue::Integer(_) => {
matches!(family, SqlTypeFamily::Integer | SqlTypeFamily::BigInteger)
}
GeneratedValue::Decimal { .. } => matches!(family, SqlTypeFamily::Decimal),
GeneratedValue::Text(_) => matches!(
family,
SqlTypeFamily::Text | SqlTypeFamily::Uuid | SqlTypeFamily::Other
),
GeneratedValue::Bytes(_) => matches!(family, SqlTypeFamily::Bytes),
GeneratedValue::DateTime(_) => matches!(family, SqlTypeFamily::DateTime),
GeneratedValue::Json(_) => matches!(family, SqlTypeFamily::Json),
}
}
/// The name of this value's variant, for error messages built outside
/// this module (e.g. a modifier reporting a type mismatch it detected
/// itself, rather than through one of the `as_*` accessors above).
pub fn type_name(&self) -> &'static str {
self.kind_name()
}
/// The name of this value's variant, used in error messages.
fn kind_name(&self) -> &'static str {
match self {
GeneratedValue::Null => "Null",
GeneratedValue::Default => "Default",
GeneratedValue::Boolean(_) => "Boolean",
GeneratedValue::Integer(_) => "Integer",
GeneratedValue::Decimal { .. } => "Decimal",
GeneratedValue::Text(_) => "Text",
GeneratedValue::Bytes(_) => "Bytes",
GeneratedValue::DateTime(_) => "DateTime",
GeneratedValue::Json(_) => "Json",
}
}
}
/// Errors returned while running a compiled generator or modifier.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GenerateError {
/// A typed accessor was called on a value of a different variant.
TypeMismatch {
expected: &'static str,
found: &'static str,
},
/// A generator's internal state (e.g. a `sequence`'s counter) could not
/// advance without wrapping past its representable range.
Overflow(String),
/// A modifier (e.g. `unique`) could not satisfy its constraint within
/// its configured attempt budget.
Exhausted(String),
/// A generator could not make sense of a value it depends on at row
/// time (e.g. `before`/`after` reading an unparsable source column).
InvalidInput(String),
/// Structured diagnostics from loading, merging, or compiling a model,
/// surfaced through the public [`crate::generate::Generate`] API (e.g.
/// `ModelCompiler::compile` errors, or `ModelMerger::merge` errors).
/// Carries the full bag, including any warnings, so a caller can report
/// every problem rather than just the first.
Diagnostics(DiagnosticBag),
/// One structured runtime diagnostic. Unlike [`Self::Diagnostics`], this
/// variant represents a single failure discovered after compilation.
Diagnostic(Box<Diagnostic>),
}
impl GenerateError {
fn type_mismatch(expected: &'static str, found: &GeneratedValue) -> Self {
GenerateError::TypeMismatch {
expected,
found: found.kind_name(),
}
}
/// Construct a structured built-in runtime failure.
pub fn diagnostic(
definition: &'static DiagnosticDefinition,
path: impl Into<String>,
message: impl Into<String>,
) -> Self {
GenerateError::Diagnostic(Box::new(Diagnostic::error(definition, path, message)))
}
}
impl fmt::Display for GenerateError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GenerateError::TypeMismatch { expected, found } => {
write!(f, "expected a {expected} value, found {found}")
}
GenerateError::Overflow(message) => write!(f, "{message}"),
GenerateError::Exhausted(message) => write!(f, "{message}"),
GenerateError::InvalidInput(message) => write!(f, "{message}"),
GenerateError::Diagnostics(bag) => write!(f, "{bag}"),
GenerateError::Diagnostic(diagnostic) => write!(f, "{diagnostic}"),
}
}
}
impl std::error::Error for GenerateError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accessors_return_the_matching_variant() {
assert_eq!(GeneratedValue::Boolean(true).as_boolean(), Ok(true));
assert_eq!(GeneratedValue::Integer(7).as_integer(), Ok(7));
assert_eq!(
GeneratedValue::Decimal {
minor: 1050,
scale: 2
}
.as_decimal(),
Ok((1050, 2))
);
assert_eq!(GeneratedValue::Text("hi".into()).as_text(), Ok("hi"));
assert_eq!(
GeneratedValue::Bytes(vec![1, 2, 3]).as_bytes(),
Ok([1u8, 2, 3].as_slice())
);
assert_eq!(
GeneratedValue::DateTime("2024-01-01".into()).as_date_time(),
Ok("2024-01-01")
);
assert_eq!(GeneratedValue::Json("{}".into()).as_json(), Ok("{}"));
}
#[test]
fn accessors_return_type_mismatch_instead_of_panicking() {
let value = GeneratedValue::Null;
assert_eq!(
value.as_integer(),
Err(GenerateError::TypeMismatch {
expected: "Integer",
found: "Null",
})
);
}
#[test]
fn is_null_reports_only_the_null_variant() {
assert!(GeneratedValue::Null.is_null());
assert!(!GeneratedValue::Default.is_null());
assert!(!GeneratedValue::Integer(0).is_null());
}
#[test]
fn compatible_with_accepts_null_and_default_for_every_family() {
assert!(GeneratedValue::Null.compatible_with(&SqlTypeFamily::Integer));
assert!(GeneratedValue::Default.compatible_with(&SqlTypeFamily::Json));
}
#[test]
fn compatible_with_matches_variant_to_family() {
assert!(GeneratedValue::Integer(1).compatible_with(&SqlTypeFamily::BigInteger));
assert!(!GeneratedValue::Integer(1).compatible_with(&SqlTypeFamily::Text));
assert!(GeneratedValue::Text("id".into()).compatible_with(&SqlTypeFamily::Uuid));
assert!(!GeneratedValue::Json("{}".into()).compatible_with(&SqlTypeFamily::Text));
}
}