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
23#[derive(Debug)]
24pub struct ConstantEncoding;
25impl Encoding for ConstantEncoding {
26 type Array = ConstantArray;
27 type Metadata = EmptyMetadata;
28}
29
30impl ConstantArray {
31 pub fn new<S>(scalar: S, len: usize) -> Self
32 where
33 S: Into<Scalar>,
34 {
35 let scalar = scalar.into();
36 let stats = StatsSet::constant(scalar.clone(), len);
37 Self {
38 scalar,
39 len,
40 stats_set: ArrayStats::from(stats),
41 }
42 }
43
44 pub fn scalar(&self) -> &Scalar {
46 &self.scalar
47 }
48}
49
50impl ArrayImpl for ConstantArray {
51 type Encoding = ConstantEncoding;
52
53 fn _len(&self) -> usize {
54 self.len
55 }
56
57 fn _dtype(&self) -> &DType {
58 self.scalar.dtype()
59 }
60
61 fn _vtable(&self) -> VTableRef {
62 VTableRef::new_ref(&ConstantEncoding)
63 }
64
65 fn _with_children(&self, _children: &[ArrayRef]) -> VortexResult<Self> {
66 Ok(self.clone())
67 }
68}
69
70impl ArrayValidityImpl for ConstantArray {
71 fn _is_valid(&self, _index: usize) -> VortexResult<bool> {
72 Ok(!self.scalar().is_null())
73 }
74
75 fn _all_valid(&self) -> VortexResult<bool> {
76 Ok(!self.scalar().is_null())
77 }
78
79 fn _all_invalid(&self) -> VortexResult<bool> {
80 Ok(self.scalar().is_null())
81 }
82
83 fn _validity_mask(&self) -> VortexResult<Mask> {
84 Ok(match self.scalar().is_null() {
85 true => Mask::AllFalse(self.len()),
86 false => Mask::AllTrue(self.len()),
87 })
88 }
89}
90
91impl ArrayStatisticsImpl for ConstantArray {
92 fn _stats_ref(&self) -> StatsSetRef<'_> {
93 self.stats_set.to_ref(self)
94 }
95}