Skip to main content

vortex_compressor/builtins/constant/
string.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Constant encoding for string arrays.
5
6use vortex_array::ArrayRef;
7use vortex_array::Canonical;
8use vortex_array::aggregate_fn::fns::is_constant::is_constant;
9use vortex_error::VortexResult;
10
11use super::is_utf8_string;
12use crate::CascadingCompressor;
13use crate::builtins::StringConstantScheme;
14use crate::builtins::constant::compress_constant_array_with_validity;
15use crate::ctx::CompressorContext;
16use crate::estimate::CompressionEstimate;
17use crate::scheme::Scheme;
18use crate::stats::ArrayAndStats;
19
20impl Scheme for StringConstantScheme {
21    fn scheme_name(&self) -> &'static str {
22        "vortex.string.constant"
23    }
24
25    fn matches(&self, canonical: &Canonical) -> bool {
26        is_utf8_string(canonical)
27    }
28
29    fn expected_compression_ratio(
30        &self,
31        data: &mut ArrayAndStats,
32        ctx: CompressorContext,
33    ) -> CompressionEstimate {
34        // Constant detection on a sample is a false positive, since the sample being constant does
35        // not mean the full array is constant.
36        if ctx.is_sample() {
37            return CompressionEstimate::Skip;
38        }
39
40        let array_len = data.array().len();
41        let stats = data.string_stats();
42
43        // We want to use `Constant` if there are only nulls in the array.
44        if stats.value_count() == 0 {
45            debug_assert_eq!(stats.null_count() as usize, array_len);
46            return CompressionEstimate::AlwaysUse;
47        }
48
49        // Since the estimated distinct count is always going to be less than or equal to the actual
50        // distinct count, if this is not equal to 1 the actual is definitely not equal to 1.
51        if stats.estimated_distinct_count().is_some_and(|c| c > 1) {
52            return CompressionEstimate::Skip;
53        }
54
55        // Otherwise our best bet is to actually check if the array is constant.
56        // This is an expensive check, but the alternative of not compressing a constant array is
57        // far less preferable.
58        CompressionEstimate::Estimate(Box::new(|compressor, data, _ctx| {
59            if is_constant(data.array(), &mut compressor.execution_ctx())? {
60                Ok(CompressionEstimate::AlwaysUse)
61            } else {
62                Ok(CompressionEstimate::Skip)
63            }
64        }))
65    }
66
67    fn compress(
68        &self,
69        _compressor: &CascadingCompressor,
70        data: &mut ArrayAndStats,
71        _ctx: CompressorContext,
72    ) -> VortexResult<ArrayRef> {
73        compress_constant_array_with_validity(data.array())
74    }
75}