datatypes/types/
list_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, Field};
18use serde::{Deserialize, Serialize};
19
20use crate::data_type::{ConcreteDataType, DataType};
21use crate::type_id::LogicalTypeId;
22use crate::value::{ListValue, Value};
23use crate::vectors::{ListVectorBuilder, MutableVector};
24
25/// Used to represent the List datatype.
26#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
27pub struct ListType {
28    /// The type of List's item.
29    // Use Box to avoid recursive dependency, as enum ConcreteDataType depends on ListType.
30    item_type: Box<ConcreteDataType>,
31}
32
33impl Default for ListType {
34    fn default() -> Self {
35        ListType::new(ConcreteDataType::null_datatype())
36    }
37}
38
39impl ListType {
40    /// Create a new `ListType` whose item's data type is `item_type`.
41    pub fn new(item_type: ConcreteDataType) -> Self {
42        ListType {
43            item_type: Box::new(item_type),
44        }
45    }
46
47    /// Returns the item data type.
48    #[inline]
49    pub fn item_type(&self) -> &ConcreteDataType {
50        &self.item_type
51    }
52}
53
54impl DataType for ListType {
55    fn name(&self) -> String {
56        format!("List<{}>", self.item_type.name())
57    }
58
59    fn logical_type_id(&self) -> LogicalTypeId {
60        LogicalTypeId::List
61    }
62
63    fn default_value(&self) -> Value {
64        Value::List(ListValue::new(vec![], *self.item_type.clone()))
65    }
66
67    fn as_arrow_type(&self) -> ArrowDataType {
68        let field = Arc::new(Field::new("item", self.item_type.as_arrow_type(), true));
69        ArrowDataType::List(field)
70    }
71
72    fn create_mutable_vector(&self, capacity: usize) -> Box<dyn MutableVector> {
73        Box::new(ListVectorBuilder::with_type_capacity(
74            *self.item_type.clone(),
75            capacity,
76        ))
77    }
78
79    fn try_cast(&self, from: Value) -> Option<Value> {
80        match from {
81            Value::List(v) => Some(Value::List(v)),
82            _ => None,
83        }
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90    use crate::value::ListValue;
91
92    #[test]
93    fn test_list_type() {
94        let t = ListType::new(ConcreteDataType::boolean_datatype());
95        assert_eq!("List<Boolean>", t.name());
96        assert_eq!(LogicalTypeId::List, t.logical_type_id());
97        assert_eq!(
98            Value::List(ListValue::new(vec![], ConcreteDataType::boolean_datatype())),
99            t.default_value()
100        );
101        assert_eq!(
102            ArrowDataType::List(Arc::new(Field::new("item", ArrowDataType::Boolean, true))),
103            t.as_arrow_type()
104        );
105        assert_eq!(ConcreteDataType::boolean_datatype(), *t.item_type());
106    }
107}