duckdb/types/
value.rs

1use super::{Null, OrderedMap, TimeUnit, Type};
2use rust_decimal::prelude::*;
3
4/// Owning [dynamic type value](https://duckdb.org/docs/stable/sql/data_types/overview.html).
5/// Value's type is typically dictated by DuckDB (not by the caller).
6///
7/// See [`ValueRef`](crate::types::ValueRef) for a non-owning dynamic type
8/// value.
9#[derive(Clone, Debug, PartialEq)]
10pub enum Value {
11    /// The value is a `NULL` value.
12    Null,
13    /// The value is a boolean.
14    Boolean(bool),
15    /// The value is a signed tiny integer.
16    TinyInt(i8),
17    /// The value is a signed small integer.
18    SmallInt(i16),
19    /// The value is a signed integer.
20    Int(i32),
21    /// The value is a signed big integer.
22    BigInt(i64),
23    /// The value is a signed huge integer.
24    HugeInt(i128),
25    /// The value is a unsigned tiny integer.
26    UTinyInt(u8),
27    /// The value is a unsigned small integer.
28    USmallInt(u16),
29    /// The value is a unsigned integer.
30    UInt(u32),
31    /// The value is a unsigned big integer.
32    UBigInt(u64),
33    /// The value is a f32.
34    Float(f32),
35    /// The value is a f64.
36    Double(f64),
37    /// The value is a Decimal.
38    Decimal(Decimal),
39    /// The value is a timestamp.
40    Timestamp(TimeUnit, i64),
41    /// The value is a text string.
42    Text(String),
43    /// The value is a blob of data
44    Blob(Vec<u8>),
45    /// The value is a date32
46    ///
47    /// The `i32` represents the number of days since the Unix epoch (1970-01-01).
48    ///
49    /// Enable the `chrono` feature for easy conversion to proper date types like `NaiveDate`.
50    Date32(i32),
51    /// The value is a time64
52    Time64(TimeUnit, i64),
53    /// The value is an interval (month, day, nano)
54    Interval {
55        /// months
56        months: i32,
57        /// days
58        days: i32,
59        /// nanos
60        nanos: i64,
61    },
62    /// The value is a list
63    List(Vec<Value>),
64    /// The value is an enum
65    Enum(String),
66    /// The value is a struct
67    Struct(OrderedMap<String, Value>),
68    /// The value is an array
69    Array(Vec<Value>),
70    /// The value is a map
71    Map(OrderedMap<Value, Value>),
72    /// The value is a union
73    Union(Box<Value>),
74}
75
76impl From<Null> for Value {
77    #[inline]
78    fn from(_: Null) -> Self {
79        Self::Null
80    }
81}
82
83impl From<bool> for Value {
84    #[inline]
85    fn from(i: bool) -> Self {
86        Self::Boolean(i)
87    }
88}
89
90impl From<usize> for Value {
91    #[inline]
92    fn from(i: usize) -> Self {
93        Self::UBigInt(i as u64)
94    }
95}
96
97impl From<isize> for Value {
98    #[inline]
99    fn from(i: isize) -> Self {
100        Self::BigInt(i as i64)
101    }
102}
103
104#[cfg(feature = "uuid")]
105impl From<uuid::Uuid> for Value {
106    #[inline]
107    fn from(id: uuid::Uuid) -> Self {
108        Self::Text(id.to_string())
109    }
110}
111
112impl From<i8> for Value {
113    #[inline]
114    fn from(i: i8) -> Self {
115        Self::TinyInt(i)
116    }
117}
118
119impl From<i16> for Value {
120    #[inline]
121    fn from(i: i16) -> Self {
122        Self::SmallInt(i)
123    }
124}
125
126impl From<i32> for Value {
127    #[inline]
128    fn from(i: i32) -> Self {
129        Self::Int(i)
130    }
131}
132
133impl From<i64> for Value {
134    #[inline]
135    fn from(i: i64) -> Self {
136        Self::BigInt(i)
137    }
138}
139
140impl From<u8> for Value {
141    #[inline]
142    fn from(i: u8) -> Self {
143        Self::UTinyInt(i)
144    }
145}
146
147impl From<u16> for Value {
148    #[inline]
149    fn from(i: u16) -> Self {
150        Self::USmallInt(i)
151    }
152}
153
154impl From<u32> for Value {
155    #[inline]
156    fn from(i: u32) -> Self {
157        Self::UInt(i)
158    }
159}
160
161impl From<u64> for Value {
162    #[inline]
163    fn from(i: u64) -> Self {
164        Self::UBigInt(i)
165    }
166}
167
168impl From<i128> for Value {
169    #[inline]
170    fn from(i: i128) -> Self {
171        Self::HugeInt(i)
172    }
173}
174
175impl From<f32> for Value {
176    #[inline]
177    fn from(f: f32) -> Self {
178        Self::Float(f)
179    }
180}
181
182impl From<f64> for Value {
183    #[inline]
184    fn from(f: f64) -> Self {
185        Self::Double(f)
186    }
187}
188
189impl From<String> for Value {
190    #[inline]
191    fn from(s: String) -> Self {
192        Self::Text(s)
193    }
194}
195
196impl From<Vec<u8>> for Value {
197    #[inline]
198    fn from(v: Vec<u8>) -> Self {
199        Self::Blob(v)
200    }
201}
202
203impl<T> From<Option<T>> for Value
204where
205    T: Into<Self>,
206{
207    #[inline]
208    fn from(v: Option<T>) -> Self {
209        match v {
210            Some(x) => x.into(),
211            None => Self::Null,
212        }
213    }
214}
215
216impl Value {
217    /// Returns DuckDB fundamental datatype.
218    #[inline]
219    pub fn data_type(&self) -> Type {
220        match *self {
221            Self::Null => Type::Null,
222            Self::Boolean(_) => Type::Boolean,
223            Self::TinyInt(_) => Type::TinyInt,
224            Self::SmallInt(_) => Type::SmallInt,
225            Self::Int(_) => Type::Int,
226            Self::BigInt(_) => Type::BigInt,
227            Self::HugeInt(_) => Type::HugeInt,
228            Self::UTinyInt(_) => Type::UTinyInt,
229            Self::USmallInt(_) => Type::USmallInt,
230            Self::UInt(_) => Type::UInt,
231            Self::UBigInt(_) => Type::UBigInt,
232            Self::Float(_) => Type::Float,
233            Self::Double(_) => Type::Double,
234            Self::Decimal(_) => Type::Decimal,
235            Self::Timestamp(_, _) => Type::Timestamp,
236            Self::Text(_) => Type::Text,
237            Self::Blob(_) => Type::Blob,
238            Self::Date32(_) => Type::Date32,
239            Self::Time64(..) => Type::Time64,
240            Self::Interval { .. } => Type::Interval,
241            Self::Union(..) | Self::Struct(..) | Self::List(..) | Self::Array(..) | Self::Map(..) => todo!(),
242            Self::Enum(..) => Type::Enum,
243        }
244    }
245}