databend_driver_core/value/
base.rs1use databend_client::schema::{DataType, DecimalDataType, DecimalSize, NumberDataType};
16use ethnum::i256;
17use jiff::Zoned;
18
19pub(crate) const DAYS_FROM_CE: i32 = 719_163;
21pub(crate) const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f";
22pub(crate) const TIMESTAMP_TIMEZONE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f %z";
23
24#[derive(Clone, Debug, PartialEq)]
25pub enum NumberValue {
26 Int8(i8),
27 Int16(i16),
28 Int32(i32),
29 Int64(i64),
30 UInt8(u8),
31 UInt16(u16),
32 UInt32(u32),
33 UInt64(u64),
34 Float32(f32),
35 Float64(f64),
36 Decimal64(i64, DecimalSize),
37 Decimal128(i128, DecimalSize),
38 Decimal256(i256, DecimalSize),
39}
40
41#[derive(Clone, Debug, PartialEq)]
42pub enum Value {
43 Null,
44 EmptyArray,
45 EmptyMap,
46 Boolean(bool),
47 Binary(Vec<u8>),
48 String(String),
49 Number(NumberValue),
50 Timestamp(Zoned),
52 TimestampTz(Zoned),
53 Date(i32),
54 Array(Vec<Value>),
55 Map(Vec<(Value, Value)>),
56 Tuple(Vec<Value>),
57 Bitmap(String),
58 Variant(String),
59 Geometry(String),
60 Geography(String),
61 Interval(String),
62 Vector(Vec<f32>),
63}
64
65impl Value {
66 pub fn get_type(&self) -> DataType {
67 match self {
68 Self::Null => DataType::Null,
69 Self::EmptyArray => DataType::EmptyArray,
70 Self::EmptyMap => DataType::EmptyMap,
71 Self::Boolean(_) => DataType::Boolean,
72 Self::Binary(_) => DataType::Binary,
73 Self::String(_) => DataType::String,
74 Self::Number(n) => match n {
75 NumberValue::Int8(_) => DataType::Number(NumberDataType::Int8),
76 NumberValue::Int16(_) => DataType::Number(NumberDataType::Int16),
77 NumberValue::Int32(_) => DataType::Number(NumberDataType::Int32),
78 NumberValue::Int64(_) => DataType::Number(NumberDataType::Int64),
79 NumberValue::UInt8(_) => DataType::Number(NumberDataType::UInt8),
80 NumberValue::UInt16(_) => DataType::Number(NumberDataType::UInt16),
81 NumberValue::UInt32(_) => DataType::Number(NumberDataType::UInt32),
82 NumberValue::UInt64(_) => DataType::Number(NumberDataType::UInt64),
83 NumberValue::Float32(_) => DataType::Number(NumberDataType::Float32),
84 NumberValue::Float64(_) => DataType::Number(NumberDataType::Float64),
85 NumberValue::Decimal64(_, s) => DataType::Decimal(DecimalDataType::Decimal64(*s)),
86 NumberValue::Decimal128(_, s) => DataType::Decimal(DecimalDataType::Decimal128(*s)),
87 NumberValue::Decimal256(_, s) => DataType::Decimal(DecimalDataType::Decimal256(*s)),
88 },
89 Self::Timestamp(_) => DataType::Timestamp,
90 Self::TimestampTz(_) => DataType::TimestampTz,
91 Self::Date(_) => DataType::Date,
92 Self::Interval(_) => DataType::Interval,
93 Self::Array(vals) => {
94 if vals.is_empty() {
95 DataType::EmptyArray
96 } else {
97 DataType::Array(Box::new(vals[0].get_type()))
98 }
99 }
100 Self::Map(kvs) => {
101 if kvs.is_empty() {
102 DataType::EmptyMap
103 } else {
104 let inner_ty = DataType::Tuple(vec![kvs[0].0.get_type(), kvs[0].1.get_type()]);
105 DataType::Map(Box::new(inner_ty))
106 }
107 }
108 Self::Tuple(vals) => {
109 let inner_tys = vals.iter().map(|v| v.get_type()).collect::<Vec<_>>();
110 DataType::Tuple(inner_tys)
111 }
112 Self::Bitmap(_) => DataType::Bitmap,
113 Self::Variant(_) => DataType::Variant,
114 Self::Geometry(_) => DataType::Geometry,
115 Self::Geography(_) => DataType::Geography,
116 Self::Vector(v) => DataType::Vector(v.len() as u64),
117 }
118 }
119}