diesel-aip-160 0.1.0-beta.8

Parse AIP-160 filters and compile typed, parameterized Diesel predicates
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Parse and validate literals before binding them to SQL.

use super::{Bound, Kind, Type};
use crate::parser::Value;
#[cfg(not(feature = "postgres"))]
use anyhow::bail;
use anyhow::{Result, ensure};

pub(super) fn parse(ty: &Type, value: &Value) -> Result<Bound> {
    let text = &value.text;
    ensure!(
        !ty.unsigned || !text.starts_with('-'),
        "unsigned value cannot be negative"
    );
    match &ty.kind {
        Kind::Signed(bits) => {
            ensure!(
                !value.quoted,
                "requires an unquoted signed {bits}-bit integer"
            );
            let n: i64 = text.parse().map_err(|error| {
                anyhow::anyhow!("requires a signed {bits}-bit integer: {error}")
            })?;
            ensure!(
                *bits == 64 || (n >= -(1i64 << (bits - 1)) && n < (1i64 << (bits - 1))),
                "signed {bits}-bit integer overflow"
            );
            // PostgreSQL must retain exact narrow types inside records.
            #[cfg(feature = "postgres")]
            {
                Ok(match bits {
                    32 => Bound::Int32(n as i32),
                    64 => Bound::Int(n),
                    _ => Bound::Text(n.to_string()),
                })
            }
            #[cfg(not(feature = "postgres"))]
            {
                Ok(Bound::Int(n))
            }
        }
        Kind::Unsigned(bits) => {
            ensure!(
                !value.quoted,
                "requires an unquoted unsigned {bits}-bit integer"
            );
            let n: u64 = text.parse()?;
            ensure!(*bits == 64 || n < (1u64 << bits), "integer overflow");
            #[cfg(feature = "mysql")]
            {
                Ok(Bound::UInt(n))
            }
            #[cfg(not(feature = "mysql"))]
            {
                Ok(Bound::Text(n.to_string()))
            }
        }
        Kind::Bool => {
            ensure!(
                !value.quoted && matches!(text.as_str(), "true" | "false"),
                "requires unquoted true or false"
            );
            #[cfg(feature = "postgres")]
            {
                Ok(Bound::Text(text.clone()))
            }
            #[cfg(not(feature = "postgres"))]
            {
                Ok(Bound::Int(i64::from(text == "true")))
            }
        }
        Kind::Float(bits) => {
            ensure!(!value.quoted, "requires an unquoted float");
            let n: f64 = text.parse()?;
            ensure!(
                n.is_finite() && (*bits != 32 || (n as f32).is_finite()),
                "float must be finite and within range"
            );
            #[cfg(feature = "postgres")]
            {
                Ok(Bound::Text(text.clone()))
            }
            #[cfg(feature = "mysql")]
            {
                Ok(if *bits == 32 {
                    Bound::Float32(n as f32)
                } else {
                    Bound::Float(n)
                })
            }
            #[cfg(feature = "sqlite")]
            {
                Ok(Bound::Float(n))
            }
        }
        Kind::Numeric => {
            ensure!(!value.quoted, "requires an unquoted decimal");
            validate_decimal(text)?;
            #[cfg(feature = "numeric")]
            {
                let _: bigdecimal::BigDecimal = text.parse()?;
            }
            #[cfg(feature = "sqlite")]
            {
                let n: f64 = text.parse()?;
                ensure!(n.is_finite(), "numeric out of range");
                Ok(Bound::Float(n))
            }
            #[cfg(not(feature = "sqlite"))]
            {
                Ok(Bound::Text(text.clone()))
            }
        }
        Kind::Binary => {
            ensure!(value.quoted, "requires quoted hexadecimal bytes (0x...)");
            let hex = text
                .strip_prefix("0x")
                .or_else(|| text.strip_prefix("\\x"))
                .ok_or_else(|| anyhow::anyhow!("expected 0x hexadecimal prefix"))?;
            ensure!(
                hex.len() % 2 == 0 && hex.bytes().all(|b| b.is_ascii_hexdigit()),
                "invalid hexadecimal bytes"
            );
            Ok(Bound::Bytes(
                hex.as_bytes()
                    .chunks(2)
                    .map(|v| u8::from_str_radix(std::str::from_utf8(v).unwrap(), 16).unwrap())
                    .collect(),
            ))
        }
        Kind::Json => {
            let _: serde_json::Value = serde_json::from_str(text)?;
            Ok(Bound::Text(text.clone()))
        }
        Kind::Record(types) => {
            ensure!(value.quoted, "requires a quoted PostgreSQL record literal");
            let fields = record_fields(text)?;
            ensure!(
                fields.len() == types.len(),
                "record requires {} fields",
                types.len()
            );
            Ok(Bound::Record(
                types
                    .iter()
                    .cloned()
                    .zip(fields)
                    .map(|(ty, field)| {
                        let value = match field {
                            None => Bound::Null,
                            Some(text) => parse(
                                &ty,
                                &Value {
                                    text,
                                    quoted: !matches!(
                                        ty.kind,
                                        Kind::Signed(_)
                                            | Kind::Unsigned(_)
                                            | Kind::Float(_)
                                            | Kind::Numeric
                                            | Kind::Bool
                                    ),
                                },
                            )?,
                        };
                        Ok((ty, value))
                    })
                    .collect::<Result<_>>()?,
            ))
        }
        Kind::Date | Kind::Time | Kind::Timestamp | Kind::Timestamptz => {
            ensure!(value.quoted, "requires a quoted ISO date/time");
            validate_temporal(&ty.kind, text)?;
            Ok(Bound::Text(text.clone()))
        }
        Kind::Uuid => {
            ensure!(value.quoted, "requires a quoted UUID");
            ensure!(
                text.len() == 36
                    && text
                        .char_indices()
                        .all(|(i, c)| if [8, 13, 18, 23].contains(&i) {
                            c == '-'
                        } else {
                            c.is_ascii_hexdigit()
                        }),
                "invalid UUID"
            );
            #[cfg(feature = "uuid")]
            {
                let _: uuid::Uuid = text.parse()?;
            }
            Ok(Bound::Text(text.clone()))
        }
        Kind::Network => {
            ensure!(value.quoted, "requires a quoted network address");
            let (ip, prefix) = text
                .split_once('/')
                .map_or((text.as_str(), None), |(a, b)| (a, Some(b)));
            let ip: std::net::IpAddr = ip.parse()?;
            if let Some(prefix) = prefix {
                let prefix: u8 = prefix.parse()?;
                ensure!(
                    prefix <= if ip.is_ipv4() { 32 } else { 128 },
                    "invalid network prefix"
                );
            }
            Ok(Bound::Text(text.clone()))
        }
        Kind::Mac(size) => {
            ensure!(value.quoted, "requires a quoted MAC address");
            let digits: String = text
                .chars()
                .filter(|c| !matches!(c, ':' | '-' | '.'))
                .collect();
            ensure!(
                digits.len() == size * 2 && digits.bytes().all(|b| b.is_ascii_hexdigit()),
                "invalid MAC address"
            );
            Ok(Bound::Text(
                digits
                    .as_bytes()
                    .chunks(2)
                    .map(|b| std::str::from_utf8(b).unwrap())
                    .collect::<Vec<_>>()
                    .join(":"),
            ))
        }
        Kind::Lsn => {
            ensure!(value.quoted, "requires a quoted WAL position");
            let (hi, lo) = text
                .split_once('/')
                .ok_or_else(|| anyhow::anyhow!("invalid WAL position"))?;
            u32::from_str_radix(hi, 16)?;
            u32::from_str_radix(lo, 16)?;
            Ok(Bound::Text(text.clone()))
        }
        Kind::Char => {
            #[cfg(feature = "postgres")]
            {
                let byte = if value.quoted {
                    ensure!(
                        text.len() == 1,
                        "requires one quoted byte or an unquoted unsigned 8-bit integer"
                    );
                    text.as_bytes()[0]
                } else {
                    text.parse::<u8>()?
                };
                Ok(Bound::CChar(byte))
            }
            #[cfg(not(feature = "postgres"))]
            {
                bail!("CChar is PostgreSQL-only");
            }
        }
        Kind::Array(_) | Kind::Range(_) | Kind::Multirange(_) | Kind::Other => {
            ensure!(value.quoted, "requires a quoted PostgreSQL native literal");
            Ok(Bound::Text(text.clone()))
        }
        Kind::Text | Kind::Jsonb => Ok(Bound::Text(text.clone())),
    }
}
fn validate_decimal(s: &str) -> Result<()> {
    let s = s.strip_prefix(['-', '+']).unwrap_or(s);
    let (mantissa, exp) = s
        .split_once(['e', 'E'])
        .map_or((s, None), |(a, b)| (a, Some(b)));
    let mut dot = false;
    let mut digits = 0;
    for b in mantissa.bytes() {
        if b == b'.' && !dot {
            dot = true;
        } else {
            ensure!(b.is_ascii_digit(), "invalid decimal");
            digits += 1;
        }
    }
    ensure!(digits > 0, "invalid decimal");
    if let Some(exp) = exp {
        let _: i32 = exp.parse()?;
    }
    Ok(())
}
fn validate_temporal(kind: &Kind, text: &str) -> Result<()> {
    #[cfg(feature = "chrono")]
    {
        match kind {
            Kind::Date => {
                chrono::NaiveDate::parse_from_str(text, "%Y-%m-%d")?;
            }
            Kind::Time => {
                chrono::NaiveTime::parse_from_str(text, "%H:%M:%S%.f")?;
            }
            Kind::Timestamp => {
                chrono::NaiveDateTime::parse_from_str(text, "%Y-%m-%dT%H:%M:%S%.f").or_else(
                    |_| chrono::NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M:%S%.f"),
                )?;
            }
            Kind::Timestamptz => {
                chrono::DateTime::parse_from_rfc3339(text)?;
            }
            _ => unreachable!(),
        }
    }
    #[cfg(all(feature = "time", not(feature = "chrono")))]
    {
        use time::format_description::well_known::{Iso8601, Rfc3339};
        match kind {
            Kind::Date => {
                time::Date::parse(text, &Iso8601::DEFAULT)?;
            }
            Kind::Time => {
                time::Time::parse(text, &Iso8601::DEFAULT)?;
            }
            Kind::Timestamp => {
                time::PrimitiveDateTime::parse(text, &Iso8601::DEFAULT)?;
            }
            Kind::Timestamptz => {
                time::OffsetDateTime::parse(text, &Rfc3339)?;
            }
            _ => unreachable!(),
        }
    }
    #[cfg(not(any(feature = "chrono", feature = "time")))]
    {
        let _ = kind;
        ensure!(!text.is_empty(), "empty date/time literal");
    }
    Ok(())
}
// PostgreSQL composite text format: empty unquoted fields are NULL, doubled
// quotes/backslashes escape characters inside a quoted field.
fn record_fields(text: &str) -> Result<Vec<Option<String>>> {
    let body = text
        .strip_prefix('(')
        .and_then(|s| s.strip_suffix(')'))
        .ok_or_else(|| anyhow::anyhow!("invalid record literal"))?;
    let mut fields = Vec::new();
    let mut value = String::new();
    let mut quoted = false;
    let mut was_quoted = false;
    let mut chars = body.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '\\' => value.push(
                chars
                    .next()
                    .ok_or_else(|| anyhow::anyhow!("unfinished record escape"))?,
            ),
            '"' if quoted && chars.peek() == Some(&'"') => {
                chars.next();
                value.push('"');
            }
            '"' => {
                quoted = !quoted;
                was_quoted = true;
            }
            ',' if !quoted => {
                fields.push(if value.is_empty() && !was_quoted {
                    None
                } else {
                    Some(std::mem::take(&mut value))
                });
                was_quoted = false;
            }
            c => value.push(c),
        }
    }
    ensure!(!quoted, "unterminated record string");
    fields.push(if value.is_empty() && !was_quoted {
        None
    } else {
        Some(value)
    });
    Ok(fields)
}

#[cfg(feature = "mysql")]
pub(super) fn mysql_decimal_type(text: &str) -> Result<String> {
    validate_decimal(text)?;
    let text = text.trim_start_matches(['-', '+']);
    let (mantissa, exp) = text
        .split_once(['e', 'E'])
        .map_or((text, 0), |(a, b)| (a, b.parse::<i32>().unwrap()));
    let (whole, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));
    let scale = (fraction.len() as i64 - i64::from(exp)).max(0);
    let integers = (whole.trim_start_matches('0').len() as i64 + i64::from(exp)).max(0);
    ensure!(
        scale <= 30 && integers + scale <= 65,
        "decimal exceeds MySQL DECIMAL precision/scale"
    );
    Ok(format!("DECIMAL(65,{scale})"))
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn records_preserve_null_empty_strings_delimiters_and_escapes() {
        assert_eq!(
            record_fields(r#"(,"", "a,b","a""b","a\\b")"#).unwrap(),
            vec![
                None,
                Some("".into()),
                Some(" a,b".into()),
                Some("a\"b".into()),
                Some("a\\b".into())
            ]
        );
        assert!(record_fields("(\"unfinished)").is_err());
        assert!(record_fields("a,b").is_err());
    }
    #[test]
    fn decimals_validate_without_float_conversion() {
        for text in ["0", "-0.01", "1e+100", "12345678901234567890.123456789"] {
            validate_decimal(text).unwrap();
        }
        for text in [
            "",
            ".",
            "NaN",
            "inf",
            "1.2.3",
            "1e",
            "1e9999999999",
            "1; DROP TABLE x",
        ] {
            assert!(validate_decimal(text).is_err(), "{text}");
        }
    }
    #[cfg(any(feature = "chrono", feature = "time"))]
    #[test]
    fn temporal_formats_are_validated_with_either_integration() {
        for (kind, text) in [
            (Kind::Date, "2026-10-17"),
            (Kind::Time, "12:34:56.123456"),
            (Kind::Timestamp, "2026-10-17T12:34:56"),
            (Kind::Timestamptz, "2026-10-17T12:34:56+08:00"),
        ] {
            validate_temporal(&kind, text).unwrap();
        }
        assert!(validate_temporal(&Kind::Date, "2026-02-30").is_err());
    }
}