datatypes/types/
binary_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::Bytes;
19use serde::{Deserialize, Serialize};
20
21use crate::data_type::{DataType, DataTypeRef};
22use crate::scalars::ScalarVectorBuilder;
23use crate::type_id::LogicalTypeId;
24use crate::value::Value;
25use crate::vectors::{BinaryVectorBuilder, MutableVector};
26
27#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
28pub struct BinaryType;
29
30impl BinaryType {
31    pub fn arc() -> DataTypeRef {
32        Arc::new(Self)
33    }
34}
35
36impl DataType for BinaryType {
37    fn name(&self) -> String {
38        "Binary".to_string()
39    }
40
41    fn logical_type_id(&self) -> LogicalTypeId {
42        LogicalTypeId::Binary
43    }
44
45    fn default_value(&self) -> Value {
46        Bytes::default().into()
47    }
48
49    fn as_arrow_type(&self) -> ArrowDataType {
50        ArrowDataType::Binary
51    }
52
53    fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
54        Box::new(BinaryVectorBuilder::with_capacity(capacity))
55    }
56
57    fn try_cast(&self, from: Value) -> Option<Value> {
58        match from {
59            Value::Binary(v) => Some(Value::Binary(v)),
60            Value::String(v) => Some(Value::Binary(Bytes::from(v.as_utf8().as_bytes()))),
61            _ => None,
62        }
63    }
64}