vortex_fastlanes/for/array/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::ArrayRef;
5use vortex_array::stats::ArrayStats;
6use vortex_dtype::PType;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_scalar::Scalar;
10
11pub mod for_compress;
12pub mod for_decompress;
13
14/// Frame of Reference (FoR) encoded array.
15///
16/// This encoding stores values as offsets from a reference value, which can significantly reduce
17/// storage requirements when values are clustered around a specific point.
18#[derive(Clone, Debug)]
19pub struct FoRArray {
20    pub(super) encoded: ArrayRef,
21    pub(super) reference: Scalar,
22    pub(super) stats_set: ArrayStats,
23}
24
25impl FoRArray {
26    pub fn try_new(encoded: ArrayRef, reference: Scalar) -> VortexResult<Self> {
27        if reference.is_null() {
28            vortex_bail!("Reference value cannot be null");
29        }
30        let reference = reference.cast(
31            &reference
32                .dtype()
33                .with_nullability(encoded.dtype().nullability()),
34        )?;
35
36        Ok(Self {
37            encoded,
38            reference,
39            stats_set: Default::default(),
40        })
41    }
42
43    pub(crate) unsafe fn new_unchecked(encoded: ArrayRef, reference: Scalar) -> Self {
44        Self {
45            encoded,
46            reference,
47            stats_set: Default::default(),
48        }
49    }
50
51    #[inline]
52    pub fn ptype(&self) -> PType {
53        self.dtype().as_ptype()
54    }
55
56    #[inline]
57    pub fn encoded(&self) -> &ArrayRef {
58        &self.encoded
59    }
60
61    #[inline]
62    pub fn reference_scalar(&self) -> &Scalar {
63        &self.reference
64    }
65
66    #[inline]
67    pub(crate) fn stats_set(&self) -> &ArrayStats {
68        &self.stats_set
69    }
70}