Skip to main content

vortex_array/arrays/widen/
stored.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Conversion between an integer child at a fixed dtype and the narrower array that is stored.
5//!
6//! Most encodings accept an integer child of any width, and they need nothing from this module.
7//! Some encodings fix the dtype of an integer child. For example, the most significant part of a
8//! `DecimalByteParts` array has a dtype that follows from the decimal precision. Such an encoding
9//! can still store the child at a narrower width, and hold it in memory as a [`WidenArray`] at the
10//! fixed dtype.
11//!
12//! The reader and the writer of such an encoding need the same two steps:
13//! [`WidenArray::from_stored_integer`] on read and [`WidenArray::to_stored_integer`] on write.
14//! This module owns both steps, so that an encoding does not repeat the requirements of a `Widen`.
15//! Neither step reads a value. The encoding still owns how it records the stored dtype, and how it
16//! reads a child that is stored wider than the fixed dtype.
17
18use vortex_error::VortexResult;
19use vortex_error::vortex_ensure;
20
21use crate::ArrayRef;
22use crate::arrays::Widen;
23use crate::arrays::WidenArray;
24use crate::arrays::widen::WidenArraySlotsExt;
25use crate::arrays::widen::compute::wrap_values;
26use crate::dtype::DType;
27
28impl WidenArray {
29    /// Restores the fixed dtype of a stored integer child, without reading its values.
30    ///
31    /// Returns `stored` unchanged when it already has `dtype`, and a [`WidenArray`] of `dtype` over
32    /// `stored` when it is narrower. A reader calls this function on the child that it decoded at
33    /// the stored dtype.
34    ///
35    /// No scan is needed, because every value of a narrower integer type with the same signedness
36    /// is also a value of `dtype`. A decimal precision does not cover every value of its storage
37    /// width, so stored decimals go through [`Self::from_stored_decimal`] instead.
38    ///
39    /// # Errors
40    ///
41    /// Returns an error if `dtype` is not an integer dtype. Also returns an error if `stored` does
42    /// not have `dtype` and breaks a requirement of [`Self::try_new`], which includes a `stored`
43    /// that is wider than `dtype`.
44    pub fn from_stored_integer(stored: ArrayRef, dtype: DType) -> VortexResult<ArrayRef> {
45        vortex_ensure!(
46            dtype.is_int(),
47            "a stored integer requires an integer dtype, got {dtype}"
48        );
49
50        wrap_values(stored, dtype)
51    }
52
53    /// Returns the narrower child of an integer [`WidenArray`] at the root of `array`.
54    ///
55    /// The writer records the child's dtype. On read, [`Self::from_stored_integer`] restores the
56    /// dtype the caller's encoding expects.
57    ///
58    /// All other inputs are returned as-is. If an integer `Widen` sits below another encoding,
59    /// it stays in the result and the writer needs permission to emit `vortex.widen` when edition
60    /// checks are enabled. To keep `Widen` at the root during a take, take rows from its child and
61    /// wrap the result in `Widen`.
62    pub fn to_stored_integer(array: &ArrayRef) -> ArrayRef {
63        // TODO(connor)[Widen]: Extract the narrower values through encodings such as `Dict`.
64        // When the optimizer moves `Widen` below a `Dict`, the writer needs to keep the codes
65        // and store the narrower dictionary values (#10748).
66        match array.as_opt::<Widen>() {
67            Some(widen) if widen.dtype().is_int() => widen.values().clone(),
68            _ => array.clone(),
69        }
70    }
71}