databend_driver_core/value/
base.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use databend_client::schema::{DataType, DecimalDataType, DecimalSize, NumberDataType};
16use ethnum::i256;
17use jiff::Zoned;
18
19// Thu 1970-01-01 is R.D. 719163
20pub(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    Decimal128(i128, DecimalSize),
37    Decimal256(i256, DecimalSize),
38}
39
40#[derive(Clone, Debug, PartialEq)]
41pub enum Value {
42    Null,
43    EmptyArray,
44    EmptyMap,
45    Boolean(bool),
46    Binary(Vec<u8>),
47    String(String),
48    Number(NumberValue),
49    /// Microseconds from 1970-01-01 00:00:00 UTC
50    Timestamp(Zoned),
51    TimestampTz(Zoned),
52    Date(i32),
53    Array(Vec<Value>),
54    Map(Vec<(Value, Value)>),
55    Tuple(Vec<Value>),
56    Bitmap(String),
57    Variant(String),
58    Geometry(String),
59    Geography(String),
60    Interval(String),
61    Vector(Vec<f32>),
62}
63
64impl Value {
65    pub fn get_type(&self) -> DataType {
66        match self {
67            Self::Null => DataType::Null,
68            Self::EmptyArray => DataType::EmptyArray,
69            Self::EmptyMap => DataType::EmptyMap,
70            Self::Boolean(_) => DataType::Boolean,
71            Self::Binary(_) => DataType::Binary,
72            Self::String(_) => DataType::String,
73            Self::Number(n) => match n {
74                NumberValue::Int8(_) => DataType::Number(NumberDataType::Int8),
75                NumberValue::Int16(_) => DataType::Number(NumberDataType::Int16),
76                NumberValue::Int32(_) => DataType::Number(NumberDataType::Int32),
77                NumberValue::Int64(_) => DataType::Number(NumberDataType::Int64),
78                NumberValue::UInt8(_) => DataType::Number(NumberDataType::UInt8),
79                NumberValue::UInt16(_) => DataType::Number(NumberDataType::UInt16),
80                NumberValue::UInt32(_) => DataType::Number(NumberDataType::UInt32),
81                NumberValue::UInt64(_) => DataType::Number(NumberDataType::UInt64),
82                NumberValue::Float32(_) => DataType::Number(NumberDataType::Float32),
83                NumberValue::Float64(_) => DataType::Number(NumberDataType::Float64),
84                NumberValue::Decimal128(_, s) => DataType::Decimal(DecimalDataType::Decimal128(*s)),
85                NumberValue::Decimal256(_, s) => DataType::Decimal(DecimalDataType::Decimal256(*s)),
86            },
87            Self::Timestamp(_) => DataType::Timestamp,
88            Self::TimestampTz(_) => DataType::TimestampTz,
89            Self::Date(_) => DataType::Date,
90            Self::Interval(_) => DataType::Interval,
91            Self::Array(vals) => {
92                if vals.is_empty() {
93                    DataType::EmptyArray
94                } else {
95                    DataType::Array(Box::new(vals[0].get_type()))
96                }
97            }
98            Self::Map(kvs) => {
99                if kvs.is_empty() {
100                    DataType::EmptyMap
101                } else {
102                    let inner_ty = DataType::Tuple(vec![kvs[0].0.get_type(), kvs[0].1.get_type()]);
103                    DataType::Map(Box::new(inner_ty))
104                }
105            }
106            Self::Tuple(vals) => {
107                let inner_tys = vals.iter().map(|v| v.get_type()).collect::<Vec<_>>();
108                DataType::Tuple(inner_tys)
109            }
110            Self::Bitmap(_) => DataType::Bitmap,
111            Self::Variant(_) => DataType::Variant,
112            Self::Geometry(_) => DataType::Geometry,
113            Self::Geography(_) => DataType::Geography,
114            Self::Vector(v) => DataType::Vector(v.len() as u64),
115        }
116    }
117}