polars_arrow/scalar/
dictionary.rs

1use std::any::Any;
2
3use super::Scalar;
4use crate::array::*;
5use crate::datatypes::ArrowDataType;
6
7/// The [`DictionaryArray`] equivalent of [`Array`] for [`Scalar`].
8#[derive(Debug, Clone)]
9pub struct DictionaryScalar<K: DictionaryKey> {
10    value: Option<Box<dyn Scalar>>,
11    phantom: std::marker::PhantomData<K>,
12    dtype: ArrowDataType,
13}
14
15impl<K: DictionaryKey> PartialEq for DictionaryScalar<K> {
16    fn eq(&self, other: &Self) -> bool {
17        (self.dtype == other.dtype) && (self.value.as_ref() == other.value.as_ref())
18    }
19}
20
21impl<K: DictionaryKey> DictionaryScalar<K> {
22    /// returns a new [`DictionaryScalar`]
23    /// # Panics
24    /// iff
25    /// * the `dtype` is not `List` or `LargeList` (depending on this scalar's offset `O`)
26    /// * the child of the `dtype` is not equal to the `values`
27    #[inline]
28    pub fn new(dtype: ArrowDataType, value: Option<Box<dyn Scalar>>) -> Self {
29        Self {
30            value,
31            phantom: std::marker::PhantomData,
32            dtype,
33        }
34    }
35
36    /// The values of the [`DictionaryScalar`]
37    pub fn value(&self) -> Option<&Box<dyn Scalar>> {
38        self.value.as_ref()
39    }
40}
41
42impl<K: DictionaryKey> Scalar for DictionaryScalar<K> {
43    fn as_any(&self) -> &dyn Any {
44        self
45    }
46
47    fn is_valid(&self) -> bool {
48        self.value.is_some()
49    }
50
51    fn dtype(&self) -> &ArrowDataType {
52        &self.dtype
53    }
54}