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
use crate::datatypes::DataType;

use super::Scalar;

/// A single entry of a [`crate::array::StructArray`].
#[derive(Debug, Clone)]
pub struct StructScalar {
    values: Vec<Box<dyn Scalar>>,
    is_valid: bool,
    data_type: DataType,
}

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

impl StructScalar {
    /// Returns a new [`StructScalar`]
    #[inline]
    pub fn new(data_type: DataType, values: Option<Vec<Box<dyn Scalar>>>) -> Self {
        let is_valid = values.is_some();
        Self {
            values: values.unwrap_or_default(),
            is_valid,
            data_type,
        }
    }

    /// Returns the values irrespectively of the validity.
    #[inline]
    pub fn values(&self) -> &[Box<dyn Scalar>] {
        &self.values
    }
}

impl Scalar for StructScalar {
    #[inline]
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    #[inline]
    fn is_valid(&self) -> bool {
        self.is_valid
    }

    #[inline]
    fn data_type(&self) -> &DataType {
        &self.data_type
    }
}