Skip to main content

typed_arrow/bridge/
null_type.rs

1//! Arrow `Null` type binding.
2
3#[cfg(feature = "views")]
4use arrow_array::Array;
5use arrow_array::{NullArray, builder::NullBuilder};
6use arrow_schema::DataType;
7
8use super::ArrowBinding;
9#[cfg(feature = "views")]
10use super::ArrowBindingView;
11
12/// Marker type for Arrow `DataType::Null` columns.
13///
14/// A `Null` column contains only nulls. Appending a value or a null both append
15/// a null slot. This maps to `arrow_array::NullArray` and uses `NullBuilder`.
16#[derive(Debug, Clone, PartialEq)]
17pub struct Null;
18
19impl ArrowBinding for Null {
20    type Builder = NullBuilder;
21    type Array = NullArray;
22    const NULLABLE: bool = true;
23    fn data_type() -> DataType {
24        DataType::Null
25    }
26    fn new_builder(_capacity: usize) -> Self::Builder {
27        NullBuilder::new()
28    }
29    fn append_value(b: &mut Self::Builder, _v: &Self) {
30        b.append_null();
31    }
32    fn append_null(b: &mut Self::Builder) {
33        b.append_null();
34    }
35    fn finish(mut b: Self::Builder) -> Self::Array {
36        b.finish()
37    }
38}
39
40#[cfg(feature = "views")]
41impl ArrowBindingView for Null {
42    type Array = NullArray;
43    type View<'a> = Null;
44
45    fn get_view(
46        array: &Self::Array,
47        index: usize,
48    ) -> Result<Self::View<'_>, crate::schema::ViewAccessError> {
49        if index >= array.len() {
50            return Err(crate::schema::ViewAccessError::OutOfBounds {
51                index,
52                len: array.len(),
53                field_name: None,
54            });
55        }
56        // NullArray has no non-null values; treat the marker as the value.
57        Ok(Null)
58    }
59}
60
61// Serialize/Deserialize implementation forwards to that for the empty tuple, ().
62#[cfg(feature = "serde")]
63impl<'de> serde::de::Deserialize<'de> for Null {
64    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
65    where
66        D: serde::de::Deserializer<'de>,
67    {
68        <()>::deserialize(deserializer).map(|_| Null)
69    }
70}
71
72#[cfg(feature = "serde")]
73impl serde::Serialize for Null {
74    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
75        serializer.serialize_unit()
76    }
77}