Skip to main content

vortex_compressor/builtins/constant/
integer.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Constant encoding for integer arrays.
5
6use vortex_array::ArrayRef;
7use vortex_array::Canonical;
8use vortex_array::ExecutionCtx;
9use vortex_error::VortexResult;
10
11use crate::CascadingCompressor;
12use crate::builtins::constant::compress_constant_array_with_validity;
13use crate::ctx::CompressorContext;
14use crate::estimate::CompressionEstimate;
15use crate::estimate::EstimateVerdict;
16use crate::scheme::Scheme;
17use crate::stats::ArrayAndStats;
18
19/// Constant encoding for integer arrays with a single distinct value.
20#[derive(Debug, Copy, Clone, PartialEq, Eq)]
21pub struct IntConstantScheme;
22
23impl Scheme for IntConstantScheme {
24    fn scheme_name(&self) -> &'static str {
25        "vortex.int.constant"
26    }
27
28    fn matches(&self, canonical: &Canonical) -> bool {
29        canonical.dtype().is_int()
30    }
31
32    fn expected_compression_ratio(
33        &self,
34        data: &ArrayAndStats,
35        compress_ctx: CompressorContext,
36        exec_ctx: &mut ExecutionCtx,
37    ) -> CompressionEstimate {
38        // Constant detection on a sample is a false positive, since the sample being constant does
39        // not mean the full array is constant.
40        if compress_ctx.is_sample() {
41            return CompressionEstimate::Verdict(EstimateVerdict::Skip);
42        }
43
44        let array_len = data.array().len();
45        let stats = data.integer_stats(exec_ctx);
46
47        // Note that we only compute distinct counts if other schemes have requested it.
48        if let Some(distinct_count) = stats.distinct_count() {
49            if distinct_count > 1 {
50                return CompressionEstimate::Verdict(EstimateVerdict::Skip);
51            } else {
52                debug_assert_eq!(distinct_count, 1);
53                return CompressionEstimate::Verdict(EstimateVerdict::AlwaysUse);
54            }
55        }
56
57        // We want to use `Constant` if there are only nulls in the array.
58        if stats.value_count() == 0 {
59            debug_assert_eq!(stats.null_count() as usize, array_len);
60            return CompressionEstimate::Verdict(EstimateVerdict::AlwaysUse);
61        }
62
63        // Otherwise, use the max and min to determine if there is a single value.
64        match stats.erased().max_minus_min().checked_ilog2() {
65            Some(_) => CompressionEstimate::Verdict(EstimateVerdict::Skip),
66            // If max-min == 0, then we know that there is only 1 value.
67            None => CompressionEstimate::Verdict(EstimateVerdict::AlwaysUse),
68        }
69    }
70
71    fn compress(
72        &self,
73        _compressor: &CascadingCompressor,
74        data: &ArrayAndStats,
75        _compress_ctx: CompressorContext,
76        exec_ctx: &mut ExecutionCtx,
77    ) -> VortexResult<ArrayRef> {
78        compress_constant_array_with_validity(data.array(), exec_ctx)
79    }
80}