vortex_array/lib.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3//! Vortex crate containing core logic for encoding and memory representation of [arrays](ArrayRef).
4//!
5//! At the heart of Vortex are [arrays](ArrayRef).
6//!
7//! Arrays are typed views of memory buffers that hold [scalars](scalar::Scalar). These
8//! buffers can be held in a number of physical encodings to perform lightweight compression that
9//! exploits the particular data distribution of the array's values.
10//!
11//! Every data type recognized by Vortex also has a canonical physical encoding format, which
12//! arrays can be [canonicalized](Canonical) into for ease of access in compute functions.
13//!
14//! # Core Handles
15//!
16//! [`ArrayRef`] is the erased, shared handle used by most public APIs. It carries the logical
17//! [`DType`], row count, encoding id, children, buffers, and statistics for an
18//! array tree. Use it when an API should accept any encoding.
19//!
20//! [`Array<V>`] is the typed owned handle for a known encoding `V: VTable`. It wraps an
21//! [`ArrayRef`] and dereferences to the encoding-specific `V::TypedArrayData`.
22//!
23//! [`ArrayView<V>`] is the lightweight typed borrow handed to vtable methods. It exposes both the
24//! shared [`ArrayRef`] metadata and the encoding-specific data without cloning the handle.
25//!
26//! [`ArrayParts<V>`] is the construction boundary for typed arrays. It groups externally supplied
27//! logical metadata and encoding data, then [`Array::try_from_parts`] validates that they agree.
28//!
29//! # Logical Types and Physical Encodings
30//!
31//! A [`DType`] describes the logical values an array may hold. It does not
32//! describe the memory layout. For example, a `DType::Primitive(I32, Nullable)` can be stored as a
33//! canonical [`PrimitiveArray`], a dictionary, a slice, or a
34//! compressed external encoding.
35//!
36//! The [`Canonical`] enum names the default uncompressed encoding for each logical family. Execution
37//! normally moves an array tree toward canonical form, but canonicalization is shallow: children of
38//! canonical struct/list arrays may still be encoded.
39//!
40//! # Built-in, Lazy, and Experimental Arrays
41//!
42//! Built-in arrays live in [`arrays`]. Some are canonical (`PrimitiveArray`, `StructArray`,
43//! `UnionArray`, `VarBinViewArray`); others are utility or lazy arrays such as [`ChunkedArray`],
44//! [`ConstantArray`], [`FilterArray`], [`SliceArray`], and [`ScalarFnArray`].
45//! Lazy arrays defer work so compute kernels can operate on encoded data or prune children
46//! before materialization.
47//!
48//! Experimental arrays are public because they are used inside Vortex, but their storage contracts
49//! may still move. Prefer the higher-level constructors and accessors documented on each array
50//! module rather than relying on child slot order.
51//!
52//! # Nulls and Scalars
53//!
54//! [`Validity`](validity::Validity) separates nullness from values. It can be a cheap
55//! constant state (`NonNullable`, `AllValid`, `AllInvalid`) or a boolean array that may itself be
56//! encoded. [`Scalar`](scalar::Scalar) is the single-value counterpart: it pairs a
57//! [`DType`] with an optional [`ScalarValue`](scalar::ScalarValue).
58//!
59//! # Extending Vortex
60//!
61//! New array encodings implement [`VTable`], usually through the local `array_slots!` and
62//! `vtable!` patterns used by built-ins. The important extension contracts are:
63//!
64//! - [`VTable::validate`] checks that externally supplied dtype, length, slots, and data agree.
65//! - [`VTable::execute`] returns an [`ExecutionResult`] that makes progress toward canonical form.
66//! - [`OperationsVTable`] provides scalar access.
67//! - [`ValidityVTable`] exposes validity only for nullable arrays.
68//!
69//! New logical extension dtypes implement [`ExtVTable`](dtype::extension::ExtVTable) and
70//! store values in an ordinary Vortex storage dtype.
71//!
72//! [`PrimitiveArray`]: arrays::PrimitiveArray
73//! [`DType`]: dtype::DType
74//! [`ChunkedArray`]: arrays::ChunkedArray
75//! [`ConstantArray`]: arrays::ConstantArray
76//! [`FilterArray`]: arrays::FilterArray
77//! [`SliceArray`]: arrays::SliceArray
78//! [`ScalarFnArray`]: arrays::ScalarFnArray
79
80#[cfg(not(target_endian = "little"))]
81compile_error!("vortex-array requires a little-endian target");
82
83extern crate self as vortex_array;
84
85pub use array::*;
86pub use canonical::*;
87pub use columnar::*;
88pub use executor::*;
89pub use hash::*;
90pub use mask_future::*;
91pub use metadata::*;
92pub use smallvec;
93pub use vortex_array_macros::array_slots;
94pub use vortex_buffer::match_each_power_of_two_width;
95use vortex_session::SessionExt;
96use vortex_session::VortexSession;
97use vortex_session::registry::Interner;
98
99use crate::optimizer::kernels::KernelSession;
100
101pub mod aggregate_fn;
102#[doc(hidden)]
103pub mod aliases;
104mod trace_macros;
105pub(crate) use trace_macros::trace_op;
106mod array;
107pub mod arrays;
108pub mod buffer;
109pub mod builders;
110pub mod builtins;
111mod canonical;
112mod columnar;
113pub mod compute;
114pub mod display;
115pub mod dtype;
116mod executor;
117pub mod expr;
118pub mod extension;
119pub mod flatbuffers;
120mod hash;
121pub mod iter;
122pub mod kernel;
123pub mod mask;
124mod mask_future;
125pub mod matcher;
126pub mod memory;
127mod metadata;
128pub mod normalize;
129pub mod optimizer;
130mod partial_ord;
131pub mod patches;
132pub mod proto;
133pub mod scalar;
134pub mod scalar_fn;
135pub mod search_sorted;
136pub mod serde;
137pub mod session;
138pub mod stats;
139pub mod stream;
140#[cfg(any(test, feature = "_test-harness"))]
141pub mod test_harness;
142pub mod validity;
143
144/// Register vortex-array's built-in session-scoped kernels into the active
145/// [`ArrayKernels`](optimizer::kernels::ArrayKernels) registry.
146///
147/// If the session contains a [`KernelSession`], this registers into its registry. Sessions that use
148/// [`KernelSession::default`] already receive these built-in kernels.
149pub fn initialize(session: &VortexSession) {
150 if session.get_opt::<KernelSession>().is_some() {
151 arrays::initialize(session);
152 }
153}
154
155pub type ArrayContext = Interner;