Skip to main content

vortex_compressor/builtins/constant/
float.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Constant encoding for float 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_float_primitive;
12use crate::CascadingCompressor;
13use crate::builtins::FloatConstantScheme;
14use crate::builtins::constant::compress_constant_array_with_validity;
15use crate::ctx::CompressorContext;
16use crate::estimate::CompressionEstimate;
17use crate::estimate::DeferredEstimate;
18use crate::estimate::EstimateVerdict;
19use crate::scheme::Scheme;
20use crate::stats::ArrayAndStats;
21
22impl Scheme for FloatConstantScheme {
23    fn scheme_name(&self) -> &'static str {
24        "vortex.float.constant"
25    }
26
27    fn matches(&self, canonical: &Canonical) -> bool {
28        is_float_primitive(canonical)
29    }
30
31    fn expected_compression_ratio(
32        &self,
33        data: &mut ArrayAndStats,
34        ctx: CompressorContext,
35    ) -> CompressionEstimate {
36        // Constant detection on a sample is a false positive, since the sample being constant does
37        // not mean the full array is constant.
38        if ctx.is_sample() {
39            return CompressionEstimate::Verdict(EstimateVerdict::Skip);
40        }
41
42        let array_len = data.array().len();
43        let stats = data.float_stats();
44
45        // Note that we only compute distinct counts if other schemes have requested it.
46        if let Some(distinct_count) = stats.distinct_count() {
47            if distinct_count > 1 {
48                return CompressionEstimate::Verdict(EstimateVerdict::Skip);
49            } else {
50                debug_assert_eq!(distinct_count, 1);
51                return CompressionEstimate::Verdict(EstimateVerdict::AlwaysUse);
52            }
53        }
54
55        // We want to use `Constant` if there are only nulls in the array.
56        if stats.value_count() == 0 {
57            debug_assert_eq!(stats.null_count() as usize, array_len);
58            return CompressionEstimate::Verdict(EstimateVerdict::AlwaysUse);
59        }
60
61        // TODO(connor): Can we be smart here with the max and min like with integers?
62
63        // Otherwise our best bet is to actually check if the array is constant.
64        // This is an expensive check, but in practice the distinct count is known because we often
65        // include dictionary encoding in our set of schemes, so we rarely call this.
66        CompressionEstimate::Deferred(DeferredEstimate::Callback(Box::new(
67            |compressor, data, _ctx| {
68                if is_constant(data.array(), &mut compressor.execution_ctx())? {
69                    Ok(EstimateVerdict::AlwaysUse)
70                } else {
71                    Ok(EstimateVerdict::Skip)
72                }
73            },
74        )))
75    }
76
77    fn compress(
78        &self,
79        _compressor: &CascadingCompressor,
80        data: &mut ArrayAndStats,
81        _ctx: CompressorContext,
82    ) -> VortexResult<ArrayRef> {
83        compress_constant_array_with_validity(data.array())
84    }
85}