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 super::is_float_primitive;
13use crate::CascadingCompressor;
14use crate::builtins::FloatConstantScheme;
15use crate::builtins::constant::compress_constant_array_with_validity;
16use crate::ctx::CompressorContext;
17use crate::estimate::CompressionEstimate;
18use crate::estimate::DeferredEstimate;
19use crate::estimate::EstimateVerdict;
20use crate::scheme::Scheme;
21use crate::stats::ArrayAndStats;
22
23impl Scheme for FloatConstantScheme {
24    fn scheme_name(&self) -> &'static str {
25        "vortex.float.constant"
26    }
27
28    fn matches(&self, canonical: &Canonical) -> bool {
29        is_float_primitive(canonical)
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.float_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        // TODO(connor): Can we be smart here with the max and min like with integers?
64
65        // Otherwise our best bet is to actually check if the array is constant.
66        // This is an expensive check, but in practice the distinct count is known because we often
67        // include dictionary encoding in our set of schemes, so we rarely call this.
68        CompressionEstimate::Deferred(DeferredEstimate::Callback(Box::new(
69            |_compressor, data, _best_so_far, _ctx, exec_ctx| {
70                if is_constant(data.array(), exec_ctx)? {
71                    Ok(EstimateVerdict::AlwaysUse)
72                } else {
73                    Ok(EstimateVerdict::Skip)
74                }
75            },
76        )))
77    }
78
79    fn compress(
80        &self,
81        _compressor: &CascadingCompressor,
82        data: &ArrayAndStats,
83        _compress_ctx: CompressorContext,
84        exec_ctx: &mut ExecutionCtx,
85    ) -> VortexResult<ArrayRef> {
86        compress_constant_array_with_validity(data.array(), exec_ctx)
87    }
88}