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