vortex_array/arrays/constant/
mod.rs1use vortex_dtype::DType;
2use vortex_error::VortexResult;
3use vortex_mask::Mask;
4use vortex_scalar::Scalar;
5
6use crate::array::ArrayValidityImpl;
7use crate::stats::{ArrayStats, StatsSet, StatsSetRef};
8use crate::vtable::VTableRef;
9use crate::{Array, ArrayImpl, ArrayRef, ArrayStatisticsImpl, EmptyMetadata, Encoding};
10
11mod canonical;
12mod compute;
13mod serde;
14mod variants;
15
16#[derive(Clone, Debug)]
17pub struct ConstantArray {
18 scalar: Scalar,
19 len: usize,
20 stats_set: ArrayStats,
21}
22
23pub struct ConstantEncoding;
24impl Encoding for ConstantEncoding {
25 type Array = ConstantArray;
26 type Metadata = EmptyMetadata;
27}
28
29impl ConstantArray {
30 pub fn new<S>(scalar: S, len: usize) -> Self
31 where
32 S: Into<Scalar>,
33 {
34 let scalar = scalar.into();
35 let stats = StatsSet::constant(scalar.clone(), len);
36 Self {
37 scalar,
38 len,
39 stats_set: ArrayStats::from(stats),
40 }
41 }
42
43 pub fn scalar(&self) -> &Scalar {
45 &self.scalar
46 }
47}
48
49impl ArrayImpl for ConstantArray {
50 type Encoding = ConstantEncoding;
51
52 fn _len(&self) -> usize {
53 self.len
54 }
55
56 fn _dtype(&self) -> &DType {
57 self.scalar.dtype()
58 }
59
60 fn _vtable(&self) -> VTableRef {
61 VTableRef::new_ref(&ConstantEncoding)
62 }
63
64 fn _with_children(&self, _children: &[ArrayRef]) -> VortexResult<Self> {
65 Ok(self.clone())
66 }
67}
68
69impl ArrayValidityImpl for ConstantArray {
70 fn _is_valid(&self, _index: usize) -> VortexResult<bool> {
71 Ok(!self.scalar().is_null())
72 }
73
74 fn _all_valid(&self) -> VortexResult<bool> {
75 Ok(!self.scalar().is_null())
76 }
77
78 fn _all_invalid(&self) -> VortexResult<bool> {
79 Ok(self.scalar().is_null())
80 }
81
82 fn _validity_mask(&self) -> VortexResult<Mask> {
83 Ok(match self.scalar().is_null() {
84 true => Mask::AllFalse(self.len()),
85 false => Mask::AllTrue(self.len()),
86 })
87 }
88}
89
90impl ArrayStatisticsImpl for ConstantArray {
91 fn _stats_ref(&self) -> StatsSetRef<'_> {
92 self.stats_set.to_ref(self)
93 }
94}