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