Skip to main content

vortex_btrblocks/schemes/
decimal.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Decimal compression scheme using byte-part decomposition.
5
6use vortex_array::ArrayRef;
7use vortex_array::Canonical;
8use vortex_array::IntoArray;
9use vortex_array::ToCanonical;
10use vortex_array::arrays::PrimitiveArray;
11use vortex_array::arrays::decimal::narrowed_decimal;
12use vortex_array::dtype::DecimalType;
13use vortex_compressor::estimate::CompressionEstimate;
14use vortex_decimal_byte_parts::DecimalByteParts;
15use vortex_error::VortexResult;
16
17use crate::ArrayAndStats;
18use crate::CascadingCompressor;
19use crate::CompressorContext;
20use crate::Scheme;
21use crate::SchemeExt;
22
23/// Compression scheme for decimal arrays via byte-part decomposition.
24///
25/// Narrows the decimal to the smallest integer type, compresses the underlying primitive, and wraps
26/// the result in a `DecimalBytePartsArray`.
27#[derive(Debug, Copy, Clone, PartialEq, Eq)]
28pub struct DecimalScheme;
29
30impl Scheme for DecimalScheme {
31    fn scheme_name(&self) -> &'static str {
32        "vortex.decimal.byte_parts"
33    }
34
35    fn matches(&self, canonical: &Canonical) -> bool {
36        matches!(canonical, Canonical::Decimal(_))
37    }
38
39    /// Children: primitive=0.
40    fn num_children(&self) -> usize {
41        1
42    }
43
44    fn expected_compression_ratio(
45        &self,
46        _data: &mut ArrayAndStats,
47        _ctx: CompressorContext,
48    ) -> CompressionEstimate {
49        // Decimal compression is almost always beneficial (narrowing + primitive compression).
50        CompressionEstimate::AlwaysUse
51    }
52
53    fn compress(
54        &self,
55        compressor: &CascadingCompressor,
56        data: &mut ArrayAndStats,
57        ctx: CompressorContext,
58    ) -> VortexResult<ArrayRef> {
59        // TODO(joe): add support splitting i128/256 buffers into chunks of primitive values
60        // for compression. 2 for i128 and 4 for i256.
61        let decimal = data.array().clone().to_decimal();
62        let decimal = narrowed_decimal(decimal);
63        let validity = decimal.validity()?;
64        let prim = match decimal.values_type() {
65            DecimalType::I8 => PrimitiveArray::new(decimal.buffer::<i8>(), validity),
66            DecimalType::I16 => PrimitiveArray::new(decimal.buffer::<i16>(), validity),
67            DecimalType::I32 => PrimitiveArray::new(decimal.buffer::<i32>(), validity),
68            DecimalType::I64 => PrimitiveArray::new(decimal.buffer::<i64>(), validity),
69            _ => return Ok(decimal.into_array()),
70        };
71
72        let compressed = compressor.compress_child(&prim.into_array(), &ctx, self.id(), 0)?;
73
74        DecimalByteParts::try_new(compressed, decimal.decimal_dtype()).map(|d| d.into_array())
75    }
76}