|
| 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 | + |
| 15 | +use arrow_buffer::i256; |
| 16 | +use chrono::{DateTime, FixedOffset}; |
| 17 | +use chrono_tz::Tz; |
| 18 | +use databend_client::schema::{DataType, DecimalDataType, DecimalSize, NumberDataType}; |
| 19 | + |
| 20 | +// Thu 1970-01-01 is R.D. 719163 |
| 21 | +pub(crate) const DAYS_FROM_CE: i32 = 719_163; |
| 22 | +pub(crate) const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f"; |
| 23 | +pub(crate) const TIMESTAMP_TIMEZONE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f %z"; |
| 24 | + |
| 25 | +#[derive(Clone, Debug, PartialEq)] |
| 26 | +pub enum NumberValue { |
| 27 | + Int8(i8), |
| 28 | + Int16(i16), |
| 29 | + Int32(i32), |
| 30 | + Int64(i64), |
| 31 | + UInt8(u8), |
| 32 | + UInt16(u16), |
| 33 | + UInt32(u32), |
| 34 | + UInt64(u64), |
| 35 | + Float32(f32), |
| 36 | + Float64(f64), |
| 37 | + Decimal128(i128, DecimalSize), |
| 38 | + Decimal256(i256, DecimalSize), |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Clone, Debug, PartialEq)] |
| 42 | +pub enum Value { |
| 43 | + Null, |
| 44 | + EmptyArray, |
| 45 | + EmptyMap, |
| 46 | + Boolean(bool), |
| 47 | + Binary(Vec<u8>), |
| 48 | + String(String), |
| 49 | + Number(NumberValue), |
| 50 | + /// Microseconds from 1970-01-01 00:00:00 UTC |
| 51 | + Timestamp(i64, Tz), |
| 52 | + TimestampTz(DateTime<FixedOffset>), |
| 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 | + |
| 65 | +impl 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::Decimal128(_, s) => DataType::Decimal(DecimalDataType::Decimal128(*s)), |
| 86 | + NumberValue::Decimal256(_, s) => DataType::Decimal(DecimalDataType::Decimal256(*s)), |
| 87 | + }, |
| 88 | + Self::Timestamp(_, _) => DataType::Timestamp, |
| 89 | + Self::TimestampTz(_) => DataType::TimestampTz, |
| 90 | + Self::Date(_) => DataType::Date, |
| 91 | + Self::Interval(_) => DataType::Interval, |
| 92 | + Self::Array(vals) => { |
| 93 | + if vals.is_empty() { |
| 94 | + DataType::EmptyArray |
| 95 | + } else { |
| 96 | + DataType::Array(Box::new(vals[0].get_type())) |
| 97 | + } |
| 98 | + } |
| 99 | + Self::Map(kvs) => { |
| 100 | + if kvs.is_empty() { |
| 101 | + DataType::EmptyMap |
| 102 | + } else { |
| 103 | + let inner_ty = DataType::Tuple(vec![kvs[0].0.get_type(), kvs[0].1.get_type()]); |
| 104 | + DataType::Map(Box::new(inner_ty)) |
| 105 | + } |
| 106 | + } |
| 107 | + Self::Tuple(vals) => { |
| 108 | + let inner_tys = vals.iter().map(|v| v.get_type()).collect::<Vec<_>>(); |
| 109 | + DataType::Tuple(inner_tys) |
| 110 | + } |
| 111 | + Self::Bitmap(_) => DataType::Bitmap, |
| 112 | + Self::Variant(_) => DataType::Variant, |
| 113 | + Self::Geometry(_) => DataType::Geometry, |
| 114 | + Self::Geography(_) => DataType::Geography, |
| 115 | + Self::Vector(v) => DataType::Vector(v.len() as u64), |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments