Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/scalar/list.rs
6939 views
1
use std::any::Any;
2
3
use super::Scalar;
4
use crate::array::*;
5
use crate::datatypes::ArrowDataType;
6
use crate::offset::Offset;
7
8
/// The scalar equivalent of [`ListArray`]. Like [`ListArray`], this struct holds a dynamically-typed
9
/// [`Array`]. The only difference is that this has only one element.
10
#[derive(Debug, Clone)]
11
pub struct ListScalar<O: Offset> {
12
values: Box<dyn Array>,
13
is_valid: bool,
14
phantom: std::marker::PhantomData<O>,
15
dtype: ArrowDataType,
16
}
17
18
impl<O: Offset> PartialEq for ListScalar<O> {
19
fn eq(&self, other: &Self) -> bool {
20
(self.dtype == other.dtype)
21
&& (self.is_valid == other.is_valid)
22
&& ((!self.is_valid) | (self.values.as_ref() == other.values.as_ref()))
23
}
24
}
25
26
impl<O: Offset> ListScalar<O> {
27
/// returns a new [`ListScalar`]
28
/// # Panics
29
/// iff
30
/// * the `dtype` is not `List` or `LargeList` (depending on this scalar's offset `O`)
31
/// * the child of the `dtype` is not equal to the `values`
32
#[inline]
33
pub fn new(dtype: ArrowDataType, values: Option<Box<dyn Array>>) -> Self {
34
let inner_dtype = ListArray::<O>::get_child_type(&dtype);
35
let (is_valid, values) = match values {
36
Some(values) => {
37
assert_eq!(inner_dtype, values.dtype());
38
(true, values)
39
},
40
None => (false, new_empty_array(inner_dtype.clone())),
41
};
42
Self {
43
values,
44
is_valid,
45
phantom: std::marker::PhantomData,
46
dtype,
47
}
48
}
49
50
/// The values of the [`ListScalar`]
51
pub fn values(&self) -> &Box<dyn Array> {
52
&self.values
53
}
54
}
55
56
impl<O: Offset> Scalar for ListScalar<O> {
57
fn as_any(&self) -> &dyn Any {
58
self
59
}
60
61
fn is_valid(&self) -> bool {
62
self.is_valid
63
}
64
65
fn dtype(&self) -> &ArrowDataType {
66
&self.dtype
67
}
68
}
69
70