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(
28 Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Default,
29)]
30pub enum BinaryReprType {
31 #[default]
32 Binary,
33 BinaryView,
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
37pub struct BinaryType {
38 #[serde(default)]
39 repr_type: BinaryReprType,
40}
41
42impl<'de> serde::Deserialize<'de> for BinaryType {
44 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
45 where
46 D: serde::Deserializer<'de>,
47 {
48 #[derive(serde::Deserialize)]
49 struct Helper {
50 #[serde(default)]
51 repr_type: BinaryReprType,
52 }
53
54 let opt = Option::<Helper>::deserialize(deserializer)?;
55 Ok(match opt {
56 Some(helper) => Self {
57 repr_type: helper.repr_type,
58 },
59 None => Self::default(),
60 })
61 }
62}
63
64impl Default for BinaryType {
65 fn default() -> Self {
66 Self {
67 repr_type: BinaryReprType::Binary,
68 }
69 }
70}
71
72impl BinaryType {
73 pub fn binary() -> Self {
74 Self {
75 repr_type: BinaryReprType::Binary,
76 }
77 }
78
79 pub fn binary_view() -> Self {
80 Self {
81 repr_type: BinaryReprType::BinaryView,
82 }
83 }
84
85 pub fn is_view(&self) -> bool {
86 matches!(self.repr_type, BinaryReprType::BinaryView)
87 }
88
89 pub fn arc() -> DataTypeRef {
90 Arc::new(Self::default())
91 }
92
93 pub fn view_arc() -> DataTypeRef {
94 Arc::new(Self::binary_view())
95 }
96}
97
98impl DataType for BinaryType {
99 fn name(&self) -> String {
100 "Binary".to_string()
101 }
102
103 fn logical_type_id(&self) -> LogicalTypeId {
104 LogicalTypeId::Binary
105 }
106
107 fn default_value(&self) -> Value {
108 Bytes::default().into()
109 }
110
111 fn as_arrow_type(&self) -> ArrowDataType {
112 match self.repr_type {
113 BinaryReprType::Binary => ArrowDataType::Binary,
114 BinaryReprType::BinaryView => ArrowDataType::BinaryView,
115 }
116 }
117
118 fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
119 match self.repr_type {
120 BinaryReprType::Binary => Box::new(BinaryVectorBuilder::with_capacity(capacity)),
121 BinaryReprType::BinaryView => {
122 Box::new(BinaryVectorBuilder::with_view_capacity(capacity))
123 }
124 }
125 }
126
127 fn try_cast(&self, from: Value) -> Option<Value> {
128 match from {
129 Value::Binary(v) => Some(Value::Binary(v)),
130 Value::String(v) => Some(Value::Binary(Bytes::from(v.as_utf8().as_bytes()))),
131 _ => None,
132 }
133 }
134}