datatypes/types/
list_type.rs1use 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#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
27pub struct ListType {
28 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 pub fn new(item_type: ConcreteDataType) -> Self {
42 ListType {
43 item_type: Box::new(item_type),
44 }
45 }
46
47 #[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}