datatypes/types/
dictionary_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 arrow::datatypes::DataType as ArrowDataType;
16use serde::{Deserialize, Serialize};
17
18use crate::data_type::{ConcreteDataType, DataType};
19use crate::type_id::LogicalTypeId;
20use crate::value::Value;
21use crate::vectors::MutableVector;
22
23/// Used to represent the Dictionary datatype.
24#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
25pub struct DictionaryType {
26    // Use Box to avoid recursive dependency, as enum ConcreteDataType depends on DictionaryType.
27    /// The type of Dictionary key.
28    key_type: Box<ConcreteDataType>,
29    /// The type of Dictionary value.
30    value_type: Box<ConcreteDataType>,
31}
32
33impl Default for DictionaryType {
34    fn default() -> Self {
35        DictionaryType::new(
36            ConcreteDataType::null_datatype(),
37            ConcreteDataType::null_datatype(),
38        )
39    }
40}
41
42impl DictionaryType {
43    /// Create a new `DictionaryType` whose item's data type is `item_type`.
44    pub fn new(key_type: ConcreteDataType, value_type: ConcreteDataType) -> Self {
45        DictionaryType {
46            key_type: Box::new(key_type),
47            value_type: Box::new(value_type),
48        }
49    }
50
51    /// Returns the key data type.
52    #[inline]
53    pub fn key_type(&self) -> &ConcreteDataType {
54        &self.key_type
55    }
56
57    /// Returns the value data type.
58    #[inline]
59    pub fn value_type(&self) -> &ConcreteDataType {
60        &self.value_type
61    }
62}
63
64impl DataType for DictionaryType {
65    fn name(&self) -> String {
66        format!(
67            "Dictionary<{}, {}>",
68            self.key_type.name(),
69            self.value_type.name()
70        )
71    }
72
73    fn logical_type_id(&self) -> LogicalTypeId {
74        LogicalTypeId::Dictionary
75    }
76
77    fn default_value(&self) -> Value {
78        unimplemented!()
79    }
80
81    fn as_arrow_type(&self) -> ArrowDataType {
82        ArrowDataType::Dictionary(
83            Box::new(self.key_type.as_arrow_type()),
84            Box::new(self.value_type.as_arrow_type()),
85        )
86    }
87
88    fn create_mutable_vector(&self, _capacity: usize) -> Box<dyn MutableVector> {
89        unimplemented!()
90    }
91
92    fn try_cast(&self, _: Value) -> Option<Value> {
93        None
94    }
95}