use std::any::Any;
use std::fmt;
use crate::array::{Array, ArrayData, ArrayDataRef};
use crate::datatypes::*;
pub struct NullArray {
data: ArrayDataRef,
}
impl NullArray {
pub fn new(length: usize) -> Self {
let array_data = ArrayData::builder(DataType::Null).len(length).build();
NullArray::from(array_data)
}
}
impl Array for NullArray {
fn as_any(&self) -> &Any {
self
}
fn data(&self) -> ArrayDataRef {
self.data.clone()
}
fn data_ref(&self) -> &ArrayDataRef {
&self.data
}
fn is_null(&self, _index: usize) -> bool {
true
}
fn is_valid(&self, _index: usize) -> bool {
false
}
fn null_count(&self) -> usize {
self.data().len()
}
}
impl From<ArrayDataRef> for NullArray {
fn from(data: ArrayDataRef) -> Self {
assert_eq!(
data.buffers().len(),
0,
"NullArray data should contain 0 buffers"
);
assert!(
data.null_buffer().is_none(),
"NullArray data should not contain a null buffer, as no buffers are required"
);
Self { data }
}
}
impl fmt::Debug for NullArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NullArray")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_null_array() {
let array1 = NullArray::new(32);
assert_eq!(array1.len(), 32);
assert_eq!(array1.null_count(), 32);
assert_eq!(array1.is_valid(0), false);
}
#[test]
fn test_null_array_slice() {
let array1 = NullArray::new(32);
let array2 = array1.slice(8, 16);
assert_eq!(array2.len(), 16);
assert_eq!(array2.null_count(), 16);
assert_eq!(array2.offset(), 8);
}
}