1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::any::Any;

use super::Scalar;
use crate::array::*;
use crate::datatypes::ArrowDataType;

/// The scalar equivalent of [`MapArray`]. Like [`MapArray`], this struct holds a dynamically-typed
/// [`Array`]. The only difference is that this has only one element.
#[derive(Debug, Clone)]
pub struct MapScalar {
    values: Box<dyn Array>,
    is_valid: bool,
    data_type: ArrowDataType,
}

impl PartialEq for MapScalar {
    fn eq(&self, other: &Self) -> bool {
        (self.data_type == other.data_type)
            && (self.is_valid == other.is_valid)
            && ((!self.is_valid) | (self.values.as_ref() == other.values.as_ref()))
    }
}

impl MapScalar {
    /// returns a new [`MapScalar`]
    /// # Panics
    /// iff
    /// * the `data_type` is not `Map`
    /// * the child of the `data_type` is not equal to the `values`
    #[inline]
    pub fn new(data_type: ArrowDataType, values: Option<Box<dyn Array>>) -> Self {
        let inner_field = MapArray::try_get_field(&data_type).unwrap();
        let inner_data_type = inner_field.data_type();
        let (is_valid, values) = match values {
            Some(values) => {
                assert_eq!(inner_data_type, values.data_type());
                (true, values)
            },
            None => (false, new_empty_array(inner_data_type.clone())),
        };
        Self {
            values,
            is_valid,
            data_type,
        }
    }

    /// The values of the [`MapScalar`]
    pub fn values(&self) -> &Box<dyn Array> {
        &self.values
    }
}

impl Scalar for MapScalar {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn is_valid(&self) -> bool {
        self.is_valid
    }

    fn data_type(&self) -> &ArrowDataType {
        &self.data_type
    }
}