arrow2/scalar/
map.rs

1use std::any::Any;
2
3use crate::{array::*, datatypes::DataType};
4
5use super::Scalar;
6
7/// The scalar equivalent of [`MapArray`]. Like [`MapArray`], this struct holds a dynamically-typed
8/// [`Array`]. The only difference is that this has only one element.
9#[derive(Debug, Clone)]
10pub struct MapScalar {
11    values: Box<dyn Array>,
12    is_valid: bool,
13    data_type: DataType,
14}
15
16impl PartialEq for MapScalar {
17    fn eq(&self, other: &Self) -> bool {
18        (self.data_type == other.data_type)
19            && (self.is_valid == other.is_valid)
20            && ((!self.is_valid) | (self.values.as_ref() == other.values.as_ref()))
21    }
22}
23
24impl MapScalar {
25    /// returns a new [`MapScalar`]
26    /// # Panics
27    /// iff
28    /// * the `data_type` is not `Map`
29    /// * the child of the `data_type` is not equal to the `values`
30    #[inline]
31    pub fn new(data_type: DataType, values: Option<Box<dyn Array>>) -> Self {
32        let inner_field = MapArray::try_get_field(&data_type).unwrap();
33        let inner_data_type = inner_field.data_type();
34        let (is_valid, values) = match values {
35            Some(values) => {
36                assert_eq!(inner_data_type, values.data_type());
37                (true, values)
38            }
39            None => (false, new_empty_array(inner_data_type.clone())),
40        };
41        Self {
42            values,
43            is_valid,
44            data_type,
45        }
46    }
47
48    /// The values of the [`MapScalar`]
49    pub fn values(&self) -> &Box<dyn Array> {
50        &self.values
51    }
52}
53
54impl Scalar for MapScalar {
55    fn as_any(&self) -> &dyn Any {
56        self
57    }
58
59    fn is_valid(&self) -> bool {
60        self.is_valid
61    }
62
63    fn data_type(&self) -> &DataType {
64        &self.data_type
65    }
66}