Skip to main content

vortex_array/arrays/widen/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Stores integer and decimal values in a narrower child array.
5//!
6//! A [`WidenArray`] exposes the original dtype. Its child stores the same values at a strictly
7//! narrower canonical width and supplies their validity. The wrapper and child have the same
8//! nullability.
9//!
10//! An integer child has a narrower integer type of the same signedness. For example, an `i64` array
11//! whose values are all between 0 and 200 can store `i16` values, but not `u8` values.
12//! [`WidenArray::encode`] picks the narrowest such type for a [`PrimitiveArray`]. An encoding that
13//! fixes the dtype of an integer child and stores it narrower uses
14//! [`WidenArray::from_stored_integer`] on read and [`WidenArray::to_stored_integer`] on write.
15//!
16//! A decimal child has the logical scale and a lower precision, and **must** be valid for its dtype.
17//! Comparisons, bounds checks, and fills use the child dtype to bound its valid values.
18//! [`WidenArray::encode_decimal`] and [`WidenArray::from_stored_decimal`] choose child precision
19//! from valid values or supplied bounds, at the logical scale. A widening decimal cast keeps the
20//! source dtype on its child. Decimal execution copies child values directly into the logical
21//! canonical width, since yielding a cast would produce another `Widen` instead of canonical values.
22//!
23//! Narrowing is a physical choice, so it **must not** change a result. Execution casts the values
24//! to the logical dtype for integers, and an operation runs on the values only where it gives the
25//! canonical result. A `Widen` never wraps another `Widen`.
26//!
27//! Take executes its gather instead of leaving a lazy [`Dict`] under a `Widen`, because code such
28//! as the DuckDB exporter and the dict layout writer looks for a `Dict` at the root of an array.
29//!
30//! The default [`ArraySession`] registers [`WidenPlugin`]. For a [`Decimal`](crate::arrays::Decimal)
31//! child, the plugin writes a legacy `vortex.decimal` node so older readers can read it. The node
32//! stores the child's buffer width, and the enclosing format supplies the parent's logical dtype.
33//! [`WidenArray::from_stored_decimal`] handles these nodes in both old and newly written files.
34//! Integer children and decimals stored in another encoding use `vortex.widen`. When edition checks
35//! are enabled, the target edition must allow the chosen wire ID.
36//!
37//! [`ArraySession`]: crate::session::ArraySession
38//! [`Dict`]: crate::arrays::Dict
39//! [`PrimitiveArray`]: crate::arrays::PrimitiveArray
40
41use vortex_session::VortexSession;
42
43mod array;
44pub use array::WidenArraySlotsExt;
45pub use array::WidenSlots;
46pub use array::WidenSlotsView;
47
48pub mod compute;
49mod encode;
50mod stored;
51
52mod vtable;
53pub use vtable::Widen;
54pub use vtable::WidenArray;
55pub use vtable::WidenPlugin;
56
57use crate::array::VTable;
58use crate::arrays::Dict;
59use crate::arrays::dict::TakeExecuteAdaptor;
60use crate::arrays::widen::compute::numeric::WidenNumericKernel;
61use crate::optimizer::kernels::ArrayKernelsExt;
62use crate::scalar_fn::ScalarFnVTable;
63use crate::scalar_fn::builtin::compare::Compare;
64use crate::scalar_fn::builtin::compare::CompareExecuteAdaptor;
65use crate::scalar_fn::builtin::numeric::Numeric;
66
67pub(crate) fn initialize(session: &VortexSession) {
68    let kernels = session.kernels();
69    kernels.register_execute_parent_kernel(Compare.id(), Widen, CompareExecuteAdaptor(Widen));
70    kernels.register_execute_parent_kernel(Numeric.id(), Widen, WidenNumericKernel);
71    kernels.register_execute_parent_kernel(Dict.id(), Widen, TakeExecuteAdaptor(Widen));
72    compute::rules::register_parent_rules(session);
73}
74
75#[cfg(test)]
76mod tests;