polars_arrow/scalar/
map.rs

1use std::any::Any;
2
3use super::Scalar;
4use crate::array::*;
5use crate::datatypes::ArrowDataType;
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    dtype: ArrowDataType,
14}
15
16impl PartialEq for MapScalar {
17    fn eq(&self, other: &Self) -> bool {
18        (self.dtype == other.dtype)
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 `dtype` is not `Map`
29    /// * the child of the `dtype` is not equal to the `values`
30    #[inline]
31    pub fn new(dtype: ArrowDataType, values: Option<Box<dyn Array>>) -> Self {
32        let inner_field = MapArray::try_get_field(&dtype).unwrap();
33        let inner_dtype = inner_field.dtype();
34        let (is_valid, values) = match values {
35            Some(values) => {
36                assert_eq!(inner_dtype, values.dtype());
37                (true, values)
38            },
39            None => (false, new_empty_array(inner_dtype.clone())),
40        };
41        Self {
42            values,
43            is_valid,
44            dtype,
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 dtype(&self) -> &ArrowDataType {
64        &self.dtype
65    }
66}