datatypes/types/
string_type.rs

1// Copyright 2023 Greptime Team
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 std::sync::Arc;
16
17use arrow::datatypes::DataType as ArrowDataType;
18use common_base::bytes::StringBytes;
19use serde::{Deserialize, Serialize};
20
21use crate::data_type::{DataType, DataTypeRef};
22use crate::prelude::ScalarVectorBuilder;
23use crate::type_id::LogicalTypeId;
24use crate::value::Value;
25use crate::vectors::{MutableVector, StringVectorBuilder};
26
27#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
28pub struct StringType;
29
30impl StringType {
31    pub fn arc() -> DataTypeRef {
32        Arc::new(Self)
33    }
34}
35
36impl DataType for StringType {
37    fn name(&self) -> String {
38        "String".to_string()
39    }
40
41    fn logical_type_id(&self) -> LogicalTypeId {
42        LogicalTypeId::String
43    }
44
45    fn default_value(&self) -> Value {
46        StringBytes::default().into()
47    }
48
49    fn as_arrow_type(&self) -> ArrowDataType {
50        ArrowDataType::Utf8
51    }
52
53    fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
54        Box::new(StringVectorBuilder::with_capacity(capacity))
55    }
56
57    fn try_cast(&self, from: Value) -> Option<Value> {
58        if from.logical_type_id() == self.logical_type_id() {
59            return Some(from);
60        }
61
62        match from {
63            Value::Null => Some(Value::String(StringBytes::from("null".to_string()))),
64
65            Value::Boolean(v) => Some(Value::String(StringBytes::from(v.to_string()))),
66            Value::UInt8(v) => Some(Value::String(StringBytes::from(v.to_string()))),
67            Value::UInt16(v) => Some(Value::String(StringBytes::from(v.to_string()))),
68            Value::UInt32(v) => Some(Value::String(StringBytes::from(v.to_string()))),
69            Value::UInt64(v) => Some(Value::String(StringBytes::from(v.to_string()))),
70            Value::Int8(v) => Some(Value::String(StringBytes::from(v.to_string()))),
71            Value::Int16(v) => Some(Value::String(StringBytes::from(v.to_string()))),
72            Value::Int32(v) => Some(Value::String(StringBytes::from(v.to_string()))),
73            Value::Int64(v) => Some(Value::String(StringBytes::from(v.to_string()))),
74            Value::Float32(v) => Some(Value::String(StringBytes::from(v.to_string()))),
75            Value::Float64(v) => Some(Value::String(StringBytes::from(v.to_string()))),
76            Value::String(v) => Some(Value::String(v)),
77            Value::Date(v) => Some(Value::String(StringBytes::from(v.to_string()))),
78            Value::Timestamp(v) => Some(Value::String(StringBytes::from(v.to_iso8601_string()))),
79            Value::Time(v) => Some(Value::String(StringBytes::from(v.to_iso8601_string()))),
80            Value::IntervalYearMonth(v) => {
81                Some(Value::String(StringBytes::from(v.to_iso8601_string())))
82            }
83            Value::IntervalDayTime(v) => {
84                Some(Value::String(StringBytes::from(v.to_iso8601_string())))
85            }
86            Value::IntervalMonthDayNano(v) => {
87                Some(Value::String(StringBytes::from(v.to_iso8601_string())))
88            }
89            Value::Duration(v) => Some(Value::String(StringBytes::from(v.to_string()))),
90            Value::Decimal128(v) => Some(Value::String(StringBytes::from(v.to_string()))),
91
92            // StringBytes is only support for utf-8, Value::Binary is not allowed.
93            Value::Binary(_) | Value::List(_) => None,
94        }
95    }
96}