vortex_sampling_compressor/compressors/
zigzag.rs1use vortex_array::aliases::hash_set::HashSet;
2use vortex_array::array::PrimitiveArray;
3use vortex_array::stats::Stat;
4use vortex_array::variants::PrimitiveArrayTrait;
5use vortex_array::{Array, Encoding, EncodingId, IntoArray};
6use vortex_error::VortexResult;
7use vortex_zigzag::{zigzag_encode, ZigZagArray, ZigZagEncoding};
8
9use crate::compressors::{CompressedArray, CompressionTree, EncodingCompressor};
10use crate::{constants, SamplingCompressor};
11
12#[derive(Debug)]
13pub struct ZigZagCompressor;
14
15impl EncodingCompressor for ZigZagCompressor {
16 fn id(&self) -> &str {
17 ZigZagEncoding::ID.as_ref()
18 }
19
20 fn cost(&self) -> u8 {
21 constants::ZIGZAG_COST
22 }
23
24 fn can_compress(&self, array: &Array) -> Option<&dyn EncodingCompressor> {
25 let parray = PrimitiveArray::maybe_from(array)?;
27
28 if !parray.ptype().is_signed_int() {
30 return None;
31 }
32
33 parray
36 .statistics()
37 .compute_as::<i64>(Stat::Min)
38 .filter(|&min| min < 0)
39 .map(|_| self as &dyn EncodingCompressor)
40 }
41
42 fn compress<'a>(
43 &'a self,
44 array: &Array,
45 like: Option<CompressionTree<'a>>,
46 ctx: SamplingCompressor<'a>,
47 ) -> VortexResult<CompressedArray<'a>> {
48 let encoded = zigzag_encode(PrimitiveArray::try_from(array.clone())?)?;
49 let compressed =
50 ctx.compress(&encoded.encoded(), like.as_ref().and_then(|l| l.child(0)))?;
51 Ok(CompressedArray::compressed(
52 ZigZagArray::try_new(compressed.array)?.into_array(),
53 Some(CompressionTree::new(self, vec![compressed.path])),
54 array,
55 ))
56 }
57
58 fn used_encodings(&self) -> HashSet<EncodingId> {
59 HashSet::from([ZigZagEncoding::ID])
60 }
61}