datatypes/types/
binary_type.rs1use 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}