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
80extern crate self as vortex_array;
81
82pub use array::*;
83pub use canonical::*;
84pub use columnar::*;
85pub use executor::*;
86pub use hash::*;
87pub use mask_future::*;
88pub use metadata::*;
89pub use smallvec;
90pub use vortex_array_macros::array_slots;
91use vortex_session::SessionExt;
92use vortex_session::VortexSession;
93use vortex_session::registry::Interner;
94
95use crate::aggregate_fn::session::AggregateFnSession;
96use crate::dtype::session::DTypeSession;
97use crate::memory::MemorySession;
98use crate::optimizer::kernels::KernelSession;
99use crate::scalar_fn::session::ScalarFnSession;
100use crate::session::ArraySession;
101use crate::stats::session::StatsSession;
102
103pub mod aggregate_fn;
104#[doc(hidden)]
105pub mod aliases;
106mod trace_macros;
107pub(crate) use trace_macros::trace_op;
108mod array;
109pub mod arrays;
110pub mod buffer;
111pub mod builders;
112pub mod builtins;
113mod canonical;
114mod columnar;
115pub mod compute;
116pub mod display;
117pub mod dtype;
118mod executor;
119pub mod expr;
120mod expression;
121pub mod extension;
122pub mod flatbuffers;
123mod hash;
124pub mod iter;
125pub mod kernel;
126pub mod mask;
127mod mask_future;
128pub mod matcher;
129pub mod memory;
130mod metadata;
131pub mod normalize;
132pub mod optimizer;
133mod partial_ord;
134pub mod patches;
135pub mod proto;
136pub mod scalar;
137pub mod scalar_fn;
138pub mod search_sorted;
139pub mod serde;
140pub mod session;
141pub mod stats;
142pub mod stream;
143#[cfg(any(test, feature = "_test-harness"))]
144pub mod test_harness;
145pub mod validity;
146
147/// Register vortex-array's built-in session-scoped kernels into the active
148/// [`ArrayKernels`](optimizer::kernels::ArrayKernels) registry.
149///
150/// If the session contains a [`KernelSession`], this registers into its registry. Sessions that use
151/// [`KernelSession::default`] already receive these built-in kernels.
152pub fn initialize(session: &VortexSession) {
153 if session.get_opt::<KernelSession>().is_some() {
154 arrays::initialize(session);
155 }
156}
157
158/// Builds a fresh [`VortexSession`] registered with all of vortex-array's built-in session
159/// variables: arrays, dtypes, scalar functions, stats, optimizer kernels, aggregate functions,
160/// and memory.
161///
162/// Each call returns an independent session (with its own registries), so callers may register
163/// additional encodings or kernels into it without affecting any other session. This does not
164/// register file, layout, or runtime state — those live in higher-level crates. Arrow
165/// interoperability lives in the `vortex-arrow` crate, whose session variables are registered
166/// lazily on first use (or eagerly by consumers).
167pub fn array_session() -> VortexSession {
168 VortexSession::empty()
169 .with::<ArraySession>()
170 .with::<KernelSession>()
171 .with::<DTypeSession>()
172 .with::<ScalarFnSession>()
173 .with::<StatsSession>()
174 .with::<AggregateFnSession>()
175 .with::<MemorySession>()
176}
177
178pub type ArrayContext = Interner;