Skip to main content

vortex_compressor/compressor/
cascade.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Core cascading compression flow.
5
6use vortex_array::ArrayRef;
7use vortex_array::Canonical;
8use vortex_array::CanonicalValidity;
9use vortex_array::ExecutionCtx;
10use vortex_array::IntoArray;
11use vortex_array::arrays::Constant;
12use vortex_array::arrays::ConstantArray;
13use vortex_array::arrays::ExtensionArray;
14use vortex_array::arrays::FixedSizeListArray;
15use vortex_array::arrays::Masked;
16use vortex_array::arrays::StructArray;
17use vortex_array::arrays::Variant;
18use vortex_array::arrays::VariantArray;
19use vortex_array::arrays::extension::ExtensionArrayExt;
20use vortex_array::arrays::fixed_size_list::FixedSizeListArrayExt;
21use vortex_array::arrays::listview::ListViewArrayExt;
22use vortex_array::arrays::listview::list_from_list_view;
23use vortex_array::arrays::masked::MaskedArraySlotsExt;
24use vortex_array::arrays::scalar_fn::AnyScalarFn;
25use vortex_array::arrays::struct_::StructArrayExt;
26use vortex_array::arrays::variant::VariantArrayExt;
27use vortex_array::scalar::Scalar;
28use vortex_error::VortexResult;
29
30use super::CascadingCompressor;
31use super::constant;
32use crate::scheme::CompressorContext;
33use crate::scheme::Scheme;
34use crate::scheme::SchemeExt;
35use crate::scheme::SchemeId;
36use crate::stats::ArrayAndStats;
37use crate::stats::GenerateStatsOptions;
38use crate::trace;
39
40impl CascadingCompressor {
41    /// Compresses an array using cascading adaptive compression.
42    ///
43    /// First canonicalizes and compacts the array, then applies optimal compression schemes.
44    ///
45    /// # Errors
46    ///
47    /// Returns an error if canonicalization or compression fails.
48    pub fn compress(
49        &self,
50        array: &ArrayRef,
51        exec_ctx: &mut ExecutionCtx,
52    ) -> VortexResult<ArrayRef> {
53        let before_nbytes = array.nbytes();
54        let span = trace::compress_span(array.len(), array.dtype(), before_nbytes);
55        let _enter = span.enter();
56
57        let canonical = array.clone().execute::<CanonicalValidity>(exec_ctx)?.0;
58        let compact = canonical.compact(exec_ctx)?;
59        let compressed = self.compress_canonical(compact, CompressorContext::new(), exec_ctx)?;
60
61        trace::record_compress_outcome(&span, before_nbytes, compressed.nbytes());
62
63        Ok(compressed)
64    }
65
66    /// Compresses a child array produced by a cascading scheme.
67    ///
68    /// If the cascade budget is exhausted, the canonical array is returned as-is. Otherwise, the
69    /// child context is created by descending and recording the parent scheme + child index, and
70    /// compression proceeds normally.
71    ///
72    /// # Errors
73    ///
74    /// Returns an error if compression fails.
75    pub fn compress_child(
76        &self,
77        child: &ArrayRef,
78        parent_ctx: &CompressorContext,
79        parent_id: SchemeId,
80        child_index: usize,
81        exec_ctx: &mut ExecutionCtx,
82    ) -> VortexResult<ArrayRef> {
83        if parent_ctx.finished_cascading() {
84            trace::cascade_exhausted(parent_id, child_index);
85            return Ok(child.clone());
86        }
87
88        let canonical = child.clone().execute::<CanonicalValidity>(exec_ctx)?.0;
89        let compact = canonical.compact(exec_ctx)?;
90
91        let child_ctx = parent_ctx
92            .clone()
93            .descend_with_scheme(parent_id, child_index);
94        self.compress_canonical(compact, child_ctx, exec_ctx)
95    }
96
97    /// Compresses a canonical array by dispatching to type-specific logic.
98    ///
99    /// # Errors
100    ///
101    /// Returns an error if compression of any sub-array fails.
102    pub(super) fn compress_canonical(
103        &self,
104        array: Canonical,
105        compress_ctx: CompressorContext,
106        exec_ctx: &mut ExecutionCtx,
107    ) -> VortexResult<ArrayRef> {
108        match array {
109            Canonical::Null(null_array) => Ok(null_array.into_array()),
110            Canonical::Bool(bool_array) => {
111                self.choose_and_compress(Canonical::Bool(bool_array), compress_ctx, exec_ctx)
112            }
113            Canonical::Primitive(primitive) => {
114                self.choose_and_compress(Canonical::Primitive(primitive), compress_ctx, exec_ctx)
115            }
116            Canonical::Decimal(decimal) => {
117                self.choose_and_compress(Canonical::Decimal(decimal), compress_ctx, exec_ctx)
118            }
119            Canonical::Struct(struct_array) => {
120                let fields = struct_array
121                    .iter_unmasked_fields()
122                    .map(|field| self.compress(field, exec_ctx))
123                    .collect::<Result<Vec<_>, _>>()?;
124
125                Ok(StructArray::try_new(
126                    struct_array.names().clone(),
127                    fields,
128                    struct_array.len(),
129                    struct_array.validity()?,
130                )?
131                .into_array())
132            }
133            Canonical::List(list_view_array) => {
134                if list_view_array.is_zero_copy_to_list() || list_view_array.elements().is_empty() {
135                    let list_array = list_from_list_view(list_view_array, exec_ctx)?;
136                    self.compress_list_array(list_array, compress_ctx, exec_ctx)
137                } else {
138                    self.compress_list_view_array(list_view_array, compress_ctx, exec_ctx)
139                }
140            }
141            Canonical::FixedSizeList(fsl_array) => {
142                let compressed_elems = self.compress(fsl_array.elements(), exec_ctx)?;
143
144                Ok(FixedSizeListArray::try_new(
145                    compressed_elems,
146                    fsl_array.list_size(),
147                    fsl_array.validity()?,
148                    fsl_array.len(),
149                )?
150                .into_array())
151            }
152            Canonical::VarBinView(varbinview) => {
153                self.choose_and_compress(Canonical::VarBinView(varbinview), compress_ctx, exec_ctx)
154            }
155            Canonical::Extension(ext_array) => {
156                // Try scheme-based compression first.
157                let scheme_compressed = self.choose_and_compress(
158                    Canonical::Extension(ext_array.clone()),
159                    compress_ctx,
160                    exec_ctx,
161                )?;
162                // TODO(connor): HACK TO SUPPORT L2 DENORMALIZATION!!!
163                if scheme_compressed.is::<AnyScalarFn>() {
164                    return Ok(scheme_compressed);
165                }
166
167                // A constant extension array (that might be masked) is already in its terminal
168                // representation, and compressing the storage separately cannot do better.
169                if scheme_compressed.is::<Constant>() {
170                    return Ok(scheme_compressed);
171                }
172                if let Some(masked) = scheme_compressed.as_opt::<Masked>()
173                    && masked.child().is::<Constant>()
174                {
175                    return Ok(scheme_compressed);
176                }
177
178                // Also compress the underlying storage array. Some extension schemes can beat the
179                // extension storage but still lose to ordinary storage compression.
180                let compressed_storage = self.compress(ext_array.storage_array(), exec_ctx)?;
181                let storage_compressed =
182                    ExtensionArray::new(ext_array.ext_dtype().clone(), compressed_storage)
183                        .into_array();
184
185                if scheme_compressed.nbytes() < storage_compressed.nbytes() {
186                    Ok(scheme_compressed)
187                } else {
188                    Ok(storage_compressed)
189                }
190            }
191            Canonical::Variant(variant_array) => {
192                let core_storage =
193                    self.compress_physical_slots(variant_array.core_storage(), exec_ctx)?;
194                let shredded = variant_array
195                    .shredded()
196                    .map(|arr| {
197                        // Avoid stack-overflow for variant shredded values
198                        if arr.is::<Variant>() {
199                            self.compress_physical_slots(arr, exec_ctx)
200                        } else {
201                            self.compress(arr, exec_ctx)
202                        }
203                    })
204                    .transpose()?;
205
206                Ok(VariantArray::try_new(core_storage, shredded)?.into_array())
207            }
208        }
209    }
210
211    /// The main scheme-selection entry point for a single leaf array.
212    ///
213    /// Filters allowed schemes by [`matches`] and exclusion rules, merges their [`stats_options`]
214    /// into a single [`GenerateStatsOptions`], and picks the winner by estimated compression
215    /// ratio.
216    ///
217    /// If a winner is found and its compressed output is actually smaller, that output is
218    /// returned. Otherwise, the original array is returned unchanged.
219    ///
220    /// Empty, all-null, and constant arrays are handled by the compressor itself before any
221    /// scheme evaluation (constant detection is skipped while compressing samples).
222    ///
223    /// [`matches`]: Scheme::matches
224    /// [`stats_options`]: Scheme::stats_options
225    fn choose_and_compress(
226        &self,
227        canonical: Canonical,
228        compress_ctx: CompressorContext,
229        exec_ctx: &mut ExecutionCtx,
230    ) -> VortexResult<ArrayRef> {
231        let eligible_schemes: Vec<&'static dyn Scheme> = self
232            .schemes
233            .iter()
234            .copied()
235            .filter(|s| s.matches(&canonical) && !self.is_excluded(*s, &compress_ctx))
236            .collect();
237
238        let array: ArrayRef = canonical.into();
239
240        if array.is_empty() {
241            return Ok(array);
242        }
243
244        if array.all_invalid(exec_ctx)? {
245            return Ok(
246                ConstantArray::new(Scalar::null(array.dtype().clone()), array.len()).into_array(),
247            );
248        }
249
250        let before_nbytes = array.nbytes();
251
252        let merged_opts = eligible_schemes
253            .iter()
254            .fold(GenerateStatsOptions::default(), |acc, s| {
255                acc.merge(s.stats_options())
256            });
257        let compress_ctx = compress_ctx.with_merged_stats_options(merged_opts);
258
259        let data = ArrayAndStats::new(array, merged_opts);
260
261        // Constant detection is built into the compressor: a constant leaf always short-circuits
262        // scheme selection. Samples are exempt because a constant sample does not imply that the
263        // full array is constant.
264        if !compress_ctx.is_sample() && constant::is_constant_for_compression(&data, exec_ctx)? {
265            let _winner_span =
266                trace::winner_compress_span(constant::CONSTANT_SCHEME_ID, before_nbytes).entered();
267            let compressed = constant::compress_constant(data.array(), exec_ctx)?;
268
269            let after_nbytes = compressed.nbytes();
270            let actual_ratio =
271                (after_nbytes != 0).then(|| before_nbytes as f64 / after_nbytes as f64);
272            let accepted = after_nbytes < before_nbytes;
273            trace::record_winner_compress_result(after_nbytes, None, actual_ratio, accepted);
274
275            return if accepted {
276                Ok(compressed)
277            } else {
278                Ok(data.into_array())
279            };
280        }
281
282        if eligible_schemes.is_empty() {
283            return Ok(data.into_array());
284        }
285
286        let Some((winner, winner_estimate)) =
287            self.choose_best_scheme(&eligible_schemes, &data, compress_ctx.clone(), exec_ctx)?
288        else {
289            return Ok(data.into_array());
290        };
291
292        // Run the winning scheme's `compress`. On failure, emit an ERROR event carrying the
293        // scheme name and cascade history before propagating.
294        let error_ctx = trace::enabled_error_context(&compress_ctx);
295        let _winner_span = trace::winner_compress_span(winner.id(), before_nbytes).entered();
296        let compressed = winner
297            .compress(self, &data, compress_ctx, exec_ctx)
298            .inspect_err(|err| {
299                // NB: this is the only way we can tell which scheme panicked / bailed on their
300                // data, especially for third-party schemes where the error site may not carry any
301                // compressor context.
302                trace::scheme_compress_failed(winner.id(), before_nbytes, error_ctx.as_ref(), err);
303            })?;
304
305        let after_nbytes = compressed.nbytes();
306        let actual_ratio = (after_nbytes != 0).then(|| before_nbytes as f64 / after_nbytes as f64);
307
308        // TODO(connor): HACK TO SUPPORT L2 DENORMALIZATION!!!
309        let accepted = after_nbytes < before_nbytes || compressed.is::<AnyScalarFn>();
310
311        trace::record_winner_compress_result(
312            after_nbytes,
313            winner_estimate.trace_ratio(),
314            actual_ratio,
315            accepted,
316        );
317
318        if accepted {
319            Ok(compressed)
320        } else {
321            Ok(data.into_array())
322        }
323    }
324}