datatypes/types/
null_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;
18use serde::{Deserialize, Serialize};
19
20use crate::data_type::{DataType, DataTypeRef};
21use crate::type_id::LogicalTypeId;
22use crate::value::Value;
23use crate::vectors::{MutableVector, NullVectorBuilder};
24
25#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
26pub struct NullType;
27
28impl NullType {
29    pub fn arc() -> DataTypeRef {
30        Arc::new(NullType)
31    }
32}
33
34impl DataType for NullType {
35    fn name(&self) -> String {
36        "Null".to_string()
37    }
38
39    fn logical_type_id(&self) -> LogicalTypeId {
40        LogicalTypeId::Null
41    }
42
43    fn default_value(&self) -> Value {
44        Value::Null
45    }
46
47    fn as_arrow_type(&self) -> ArrowDataType {
48        ArrowDataType::Null
49    }
50
51    fn create_mutable_vector(&self, _capacity: usize) -> Box<dyn MutableVector> {
52        Box::<NullVectorBuilder>::default()
53    }
54
55    // Unconditional cast other type to Value::Null
56    fn try_cast(&self, _from: Value) -> Option<Value> {
57        Some(Value::Null)
58    }
59}