Skip to main content

turnloop_postgres/
types.rs

1//! Common PostgreSQL codecs. `Int8` and `Numeric` remain exact: pg's default
2//! JS parsers use strings. JSON parsing and local-time JS Date construction
3//! belong to the host; neither reads a clock here.
4use crate::{Error, Result, wire::Cursor};
5use std::{borrow::Cow, fmt::Write};
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum Value<'a> {
9    Null,
10    Bool(bool),
11    Int(i32),
12    Int8(i64),
13    Float(f64),
14    Text(Cow<'a, str>),
15    Numeric(Cow<'a, str>),
16    Bytes(Cow<'a, [u8]>),
17    Json(Cow<'a, str>),
18    Uuid(Cow<'a, str>),
19    /// ISO DateStyle text; preserves infinity and local-time timestamp meaning.
20    TemporalText {
21        oid: u32,
22        text: Cow<'a, str>,
23    },
24    /// PostgreSQL epoch: days after 2000-01-01. i32 extrema are infinities.
25    Date(i32),
26    /// Microseconds after 2000-01-01; timestamptz=true means UTC.
27    Timestamp {
28        microseconds: i64,
29        timestamptz: bool,
30    },
31    Array(Vec<Value<'a>>),
32    /// Unknown binary types are deliberately not interpreted as UTF-8.
33    Raw {
34        oid: u32,
35        bytes: Cow<'a, [u8]>,
36    },
37}
38impl Value<'_> {
39    pub fn into_owned(self) -> Value<'static> {
40        match self {
41            Self::Null => Value::Null,
42            Self::Bool(v) => Value::Bool(v),
43            Self::Int(v) => Value::Int(v),
44            Self::Int8(v) => Value::Int8(v),
45            Self::Float(v) => Value::Float(v),
46            Self::Text(v) => Value::Text(Cow::Owned(v.into_owned())),
47            Self::Numeric(v) => Value::Numeric(Cow::Owned(v.into_owned())),
48            Self::Bytes(v) => Value::Bytes(Cow::Owned(v.into_owned())),
49            Self::Json(v) => Value::Json(Cow::Owned(v.into_owned())),
50            Self::Uuid(v) => Value::Uuid(Cow::Owned(v.into_owned())),
51            Self::TemporalText { oid, text } => Value::TemporalText {
52                oid,
53                text: Cow::Owned(text.into_owned()),
54            },
55            Self::Date(v) => Value::Date(v),
56            Self::Timestamp {
57                microseconds,
58                timestamptz,
59            } => Value::Timestamp {
60                microseconds,
61                timestamptz,
62            },
63            Self::Array(v) => Value::Array(v.into_iter().map(Value::into_owned).collect()),
64            Self::Raw { oid, bytes } => Value::Raw {
65                oid,
66                bytes: Cow::Owned(bytes.into_owned()),
67            },
68        }
69    }
70}
71fn text(b: &[u8]) -> Result<&str> {
72    std::str::from_utf8(b).map_err(|_| Error::Protocol("invalid UTF-8 value"))
73}
74fn number<T: std::str::FromStr>(s: &str) -> Result<T> {
75    s.parse()
76        .map_err(|_| Error::Protocol("invalid numeric value"))
77}
78fn array_element(oid: u32) -> Option<u32> {
79    Some(match oid {
80        1000 => 16,
81        1001 => 17,
82        1005 => 21,
83        1007 => 23,
84        1009 => 25,
85        1014 => 1042,
86        1015 => 1043,
87        1016 => 20,
88        1021 => 700,
89        1022 => 701,
90        1115 => 1114,
91        1182 => 1082,
92        1185 => 1184,
93        1231 => 1700,
94        199 => 114,
95        2951 => 2950,
96        3807 => 3802,
97        _ => return None,
98    })
99}
100/// Decode one field. Borrowed text/binary rows remain allocation-free; arrays,
101/// bytea unescaping, binary numeric and binary UUID allocate their result only.
102pub fn decode(oid: u32, format: i16, bytes: Option<&[u8]>) -> Result<Value<'_>> {
103    let Some(b) = bytes else {
104        return Ok(Value::Null);
105    };
106    if !matches!(format, 0 | 1) {
107        return Err(Error::Protocol("invalid value format"));
108    }
109    if let Some(element) = array_element(oid) {
110        return if format == 0 {
111            text_array(element, text(b)?)
112        } else {
113            binary_array(element, b)
114        };
115    }
116    if format == 0 {
117        let s = text(b)?;
118        return Ok(match oid {
119            16 => Value::Bool(match s {
120                "t" => true,
121                "f" => false,
122                _ => return Err(Error::Protocol("invalid boolean")),
123            }),
124            21 | 23 => Value::Int(number(s)?),
125            20 => Value::Int8(number(s)?),
126            700 | 701 => Value::Float(number(s)?),
127            1700 => Value::Numeric(s.into()),
128            17 => Value::Bytes(Cow::Owned(bytea(s)?)),
129            114 | 3802 => Value::Json(s.into()),
130            2950 => Value::Uuid(s.into()),
131            1082 | 1114 | 1184 => Value::TemporalText {
132                oid,
133                text: s.into(),
134            },
135            _ => Value::Text(s.into()),
136        });
137    }
138    let mut c = Cursor(b);
139    let value = match oid {
140        16 => Value::Bool(match c.u8()? {
141            0 => false,
142            1 => true,
143            _ => return Err(Error::Protocol("invalid boolean")),
144        }),
145        21 => Value::Int(c.i16()? as i32),
146        23 => Value::Int(c.i32()?),
147        20 => Value::Int8(i64::from_be_bytes(c.take(8)?.try_into().unwrap())),
148        700 => Value::Float(f32::from_be_bytes(c.take(4)?.try_into().unwrap()) as f64),
149        701 => Value::Float(f64::from_be_bytes(c.take(8)?.try_into().unwrap())),
150        17 => {
151            c.take(b.len())?;
152            Value::Bytes(b.into())
153        }
154        25 | 1042 | 1043 | 19 => {
155            c.take(b.len())?;
156            Value::Text(text(b)?.into())
157        }
158        114 => {
159            c.take(b.len())?;
160            Value::Json(text(b)?.into())
161        }
162        3802 => {
163            if c.u8()? != 1 {
164                return Err(Error::Protocol("unsupported jsonb version"));
165            }
166            let s = text(c.0)?;
167            c.take(c.0.len())?;
168            Value::Json(s.into())
169        }
170        1082 => Value::Date(c.i32()?),
171        1114 | 1184 => Value::Timestamp {
172            microseconds: i64::from_be_bytes(c.take(8)?.try_into().unwrap()),
173            timestamptz: oid == 1184,
174        },
175        2950 => {
176            let bytes = c.take(16)?;
177            let mut s = String::with_capacity(36);
178            for (i, b) in bytes.iter().enumerate() {
179                if matches!(i, 4 | 6 | 8 | 10) {
180                    s.push('-');
181                }
182                write!(s, "{b:02x}").unwrap();
183            }
184            Value::Uuid(s.into())
185        }
186        1700 => {
187            c.take(b.len())?;
188            Value::Numeric(numeric(b)?.into())
189        }
190        _ => {
191            c.take(b.len())?;
192            Value::Raw {
193                oid,
194                bytes: b.into(),
195            }
196        }
197    };
198    c.end()?;
199    Ok(value)
200}
201fn bytea(s: &str) -> Result<Vec<u8>> {
202    let mut out = Vec::with_capacity(s.len() / 2);
203    if let Some(hex) = s.strip_prefix("\\x") {
204        if hex.len() % 2 != 0 {
205            return Err(Error::Protocol("odd bytea hex length"));
206        }
207        for pair in hex.as_bytes().as_chunks::<2>().0 {
208            let h = |b: u8| {
209                (b as char)
210                    .to_digit(16)
211                    .map(|v| v as u8)
212                    .ok_or(Error::Protocol("invalid bytea hex"))
213            };
214            out.push(h(pair[0])? * 16 + h(pair[1])?);
215        }
216    } else {
217        let mut c = Cursor(s.as_bytes());
218        while !c.0.is_empty() {
219            let b = c.u8()?;
220            if b != b'\\' {
221                out.push(b);
222            } else if c.0.first() == Some(&b'\\') {
223                c.u8()?;
224                out.push(b'\\');
225            } else {
226                let b = c.take(3)?;
227                if b[0] > b'3' || b.iter().any(|v| !(b'0'..=b'7').contains(v)) {
228                    return Err(Error::Protocol("invalid bytea escape"));
229                }
230                out.push((b[0] - b'0') * 64 + (b[1] - b'0') * 8 + b[2] - b'0');
231            }
232        }
233    }
234    Ok(out)
235}
236fn numeric(b: &[u8]) -> Result<String> {
237    let mut c = Cursor(b);
238    let count = c.u16()? as usize;
239    let weight = c.i16()? as i32;
240    let sign = c.u16()?;
241    let scale = c.u16()? as usize;
242    let digits = c.take(count.checked_mul(2).ok_or(Error::Limit)?)?;
243    c.end()?;
244    if count > 32767 || scale > 16383 {
245        return Err(Error::Protocol("invalid numeric scale"));
246    }
247    for d in digits.as_chunks::<2>().0 {
248        if u16::from_be_bytes(*d) >= 10000 {
249            return Err(Error::Protocol("invalid numeric digit"));
250        }
251    }
252    if let Some(s) = match sign {
253        0xc000 => Some("NaN"),
254        0xd000 => Some("Infinity"),
255        0xf000 => Some("-Infinity"),
256        0 | 0x4000 => None,
257        _ => return Err(Error::Protocol("invalid numeric sign")),
258    } {
259        return Ok(s.into());
260    }
261    let digit = |power: i32| {
262        let i = weight - power;
263        if i < 0 || i as usize >= count {
264            0
265        } else {
266            u16::from_be_bytes(
267                digits[i as usize * 2..i as usize * 2 + 2]
268                    .try_into()
269                    .unwrap(),
270            )
271        }
272    };
273    let mut s = String::new();
274    if sign == 0x4000 {
275        s.push('-');
276    }
277    if weight < 0 {
278        s.push('0');
279    } else {
280        for power in (0..=weight).rev() {
281            if power == weight {
282                write!(s, "{}", digit(power)).unwrap();
283            } else {
284                write!(s, "{:04}", digit(power)).unwrap();
285            }
286        }
287    }
288    if scale > 0 {
289        s.push('.');
290        let start = s.len();
291        for i in 1..=scale.div_ceil(4) {
292            write!(s, "{:04}", digit(-(i as i32))).unwrap();
293        }
294        s.truncate(start + scale);
295    }
296    Ok(s)
297}
298fn binary_array(element: u32, b: &[u8]) -> Result<Value<'_>> {
299    let mut c = Cursor(b);
300    let dimensions = c.i32()?;
301    let nulls = c.i32()?;
302    let actual = c.u32()?;
303    if !(0..=6).contains(&dimensions) || !matches!(nulls, 0 | 1) || actual != element {
304        return Err(Error::Protocol("invalid array header"));
305    }
306    let mut shape = [0usize; 6];
307    let mut count = if dimensions == 0 { 0 } else { 1usize };
308    for n in &mut shape[..dimensions as usize] {
309        let len = c.i32()?;
310        c.i32()?;
311        if len < 0 {
312            return Err(Error::Protocol("negative array length"));
313        }
314        *n = len as usize;
315        count = count.checked_mul(*n).ok_or(Error::Limit)?;
316    }
317    if count > c.0.len() / 4 {
318        return Err(Error::Protocol("truncated array elements"));
319    }
320    fn values<'a>(c: &mut Cursor<'a>, shape: &[usize], element: u32) -> Result<Value<'a>> {
321        let mut out = Vec::with_capacity(shape.first().copied().unwrap_or(0));
322        if let Some(&n) = shape.first() {
323            for _ in 0..n {
324                out.push(if shape.len() > 1 {
325                    values(c, &shape[1..], element)?
326                } else {
327                    let len = c.i32()?;
328                    let bytes = match len {
329                        -1 => None,
330                        0.. => Some(c.take(len as usize)?),
331                        _ => return Err(Error::Protocol("invalid array element length")),
332                    };
333                    decode(element, 1, bytes)?
334                });
335            }
336        }
337        Ok(Value::Array(out))
338    }
339    let v = values(&mut c, &shape[..dimensions as usize], element)?;
340    c.end()?;
341    Ok(v)
342}
343fn text_array(element: u32, s: &str) -> Result<Value<'_>> {
344    let mut c = Cursor(s.as_bytes());
345    let has_bounds = c.0.first() == Some(&b'[');
346    // pg drops custom lower bounds in the JS array. Validate the prefix grammar.
347    while c.0.first() == Some(&b'[') {
348        c.u8()?;
349        let end =
350            c.0.iter()
351                .position(|b| *b == b']')
352                .ok_or(Error::Protocol("invalid array bounds"))?;
353        let bounds = text(c.take(end)?)?;
354        let (low, high) = bounds
355            .split_once(':')
356            .ok_or(Error::Protocol("invalid array bounds"))?;
357        let _: i32 = number(low)?;
358        let _: i32 = number(high)?;
359        c.u8()?;
360    }
361    if has_bounds && c.u8()? != b'=' {
362        return Err(Error::Protocol("array bounds require equals"));
363    }
364    fn array<'a>(c: &mut Cursor<'a>, element: u32, depth: usize) -> Result<Value<'a>> {
365        if depth > 6 || c.u8()? != b'{' {
366            return Err(Error::Protocol("invalid array nesting"));
367        }
368        let mut values = Vec::new();
369        if c.0.first() == Some(&b'}') {
370            c.u8()?;
371            return Ok(Value::Array(values));
372        }
373        loop {
374            if c.0.first() == Some(&b'{') {
375                values.push(array(c, element, depth + 1)?);
376            } else {
377                let quoted = c.0.first() == Some(&b'"');
378                if quoted {
379                    c.u8()?;
380                }
381                let start = c.0;
382                let mut end = 0;
383                let mut escaped = false;
384                loop {
385                    let b = *c.0.get(end).ok_or(Error::Protocol("unterminated array"))?;
386                    if b == b'\\' {
387                        escaped = true;
388                        end += 2;
389                        if end > c.0.len() {
390                            return Err(Error::Protocol("truncated array escape"));
391                        }
392                        continue;
393                    }
394                    if (quoted && b == b'"') || (!quoted && matches!(b, b',' | b'}')) {
395                        break;
396                    }
397                    end += 1;
398                }
399                c.take(end)?;
400                if quoted {
401                    c.u8()?;
402                }
403                let raw = &start[..end];
404                if !quoted && raw.is_empty() {
405                    return Err(Error::Protocol("empty unquoted array element"));
406                }
407                if !quoted && raw == b"NULL" {
408                    values.push(Value::Null);
409                } else if escaped {
410                    let mut buf = Vec::with_capacity(raw.len());
411                    let mut i = 0;
412                    while i < raw.len() {
413                        if raw[i] == b'\\' {
414                            i += 1;
415                        }
416                        buf.push(raw[i]);
417                        i += 1;
418                    }
419                    values.push(decode(element, 0, Some(&buf))?.into_owned());
420                } else {
421                    values.push(decode(element, 0, Some(raw))?);
422                }
423            }
424            match c.u8()? {
425                b',' => {}
426                b'}' => break,
427                _ => return Err(Error::Protocol("invalid array separator")),
428            }
429        }
430        Ok(Value::Array(values))
431    }
432    let v = array(&mut c, element, 1)?;
433    c.end()?;
434    Ok(v)
435}
436
437#[cfg(test)]
438mod tests {
439    use super::*;
440    #[test]
441    fn common_types_and_arrays() {
442        assert_eq!(
443            decode(20, 0, Some(b"9223372036854775807")).unwrap(),
444            Value::Int8(i64::MAX)
445        );
446        assert_eq!(
447            decode(1700, 0, Some(b"1.2300")).unwrap(),
448            Value::Numeric("1.2300".into())
449        );
450        assert_eq!(
451            decode(17, 0, Some(b"\\x00ff")).unwrap(),
452            Value::Bytes(vec![0, 255].into())
453        );
454        assert_eq!(
455            decode(1009, 0, Some(br#"{"hello, world","NULL",NULL,"a\\b"}"#)).unwrap(),
456            Value::Array(vec![
457                Value::Text("hello, world".into()),
458                Value::Text("NULL".into()),
459                Value::Null,
460                Value::Text("a\\b".into())
461            ])
462        );
463        assert_eq!(
464            decode(1007, 0, Some(b"[2:3]={1,2}")).unwrap(),
465            Value::Array(vec![Value::Int(1), Value::Int(2)])
466        );
467        assert!(decode(1007, 0, Some(b"{1,}")).is_err());
468        let numeric = [0, 2, 0, 0, 0, 0, 0, 4, 0, 1, 8, 252];
469        assert_eq!(
470            decode(1700, 1, Some(&numeric)).unwrap(),
471            Value::Numeric("1.2300".into())
472        );
473        assert_eq!(
474            decode(3802, 1, Some(b"\x01{\"x\":1}")).unwrap(),
475            Value::Json("{\"x\":1}".into())
476        );
477        assert!(decode(23, 1, Some(&[1])).is_err());
478        assert_eq!(
479            decode(2950, 1, Some(&[0; 16])).unwrap(),
480            Value::Uuid("00000000-0000-0000-0000-000000000000".into())
481        );
482        let mut array = Vec::new();
483        for n in [1i32, 1, 23, 2, 1, 4, 42, -1] {
484            array.extend_from_slice(&n.to_be_bytes());
485        }
486        assert_eq!(
487            decode(1007, 1, Some(&array)).unwrap(),
488            Value::Array(vec![Value::Int(42), Value::Null])
489        );
490    }
491}