minarrow 0.17.0

Apache Arrow-compatible, Rust-first columnar data library for high-performance computing, native streaming, and embedded workloads. Minimal dependencies, ultra-low-latency access, automatic 64-byte SIMD alignment, and fast compile times. Great for real-time analytics, HPC pipelines, and systems integration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2025 Peter Garfield Bower
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! # **Value Module** - *Single *Whole Type Universe* Value Container*
//!
//! Contains the `Value` enum, a unified container for any Minarrow-supported data structure.
//!
//! ## Description
//! -Encapsulates scalars, arrays, tables, views, chunked collections, bitmasks, fields,
//! matrices, cubes, nested values, and custom user-defined types.
//!
//! ## Purpose
//! Used to create a global type universe for function signatures and dispatch, enabling
//! constructs like `Result<Value, MinarrowError>` without restricting the contained type.
//!
//! ## Supports:
//! - recursive containers (boxed, arced, tuples, vectors)
//! - `From`/`TryFrom` conversions for safe extraction
//! - equality comparison across all variants, including custom values
//! - custom extension types if needed

mod conversions;

#[cfg(feature = "cube")]
use crate::Cube;
#[cfg(feature = "matrix")]
use crate::Matrix;
#[cfg(feature = "ndarray")]
use crate::NdArray;
#[cfg(all(feature = "ndarray", feature = "views"))]
use crate::NdArrayV;
#[cfg(feature = "scalar_type")]
use crate::Scalar;
#[cfg(all(feature = "ndarray", feature = "chunked"))]
use crate::SuperNdArray;
#[cfg(all(feature = "ndarray", feature = "chunked", feature = "views"))]
use crate::SuperNdArrayV;
#[cfg(feature = "xarray")]
use crate::XArray;
use crate::{Array, FieldArray, Table, traits::custom_value::CustomValue};
use std::sync::Arc;

#[cfg(feature = "chunked")]
use crate::{SuperArray, SuperTable};

#[cfg(feature = "views")]
use crate::{ArrayV, TableV};

#[cfg(all(feature = "chunked", feature = "views"))]
use crate::{SuperArrayV, SuperTableV};

mod impls;

/// # Value
///
/// Unified value enum representing any supported data structure.
///
/// ## Details
/// - Wraps scalar values, arrays, array windows, full tables, or table windows
/// under a single type for function signatures and downstream dispatch.
/// - This can be useful when you need a global *type universe*.
/// - It is not part of the `Arrow` specification, but is useful
/// because of the flexibility it adds unifying all types to a single one.
/// For example, to return `Result<Value, Error>`, particularly in engine contexts.
/// - It's enabled optionally via the `value_type` feature.
///
/// ## Usage
/// You can also use it to hold a custom type under the `Custom` entry.
/// As long as the object implements `Debug`, `Clone`, and `PartialEq`,
/// remains `Send + Sync`, and implements `Any` it can be stored in `Value::Custom`.
/// `Any` is implemented automatically for all Rust types with a `'static` lifetime.
#[derive(Debug, Clone)]
pub enum Value {
    #[cfg(feature = "scalar_type")]
    Scalar(Scalar),
    Array(Arc<Array>),
    #[cfg(feature = "views")]
    ArrayView(Arc<ArrayV>),
    FieldArray(Arc<FieldArray>),
    Table(Arc<Table>),
    #[cfg(feature = "views")]
    TableView(Arc<TableV>),
    #[cfg(feature = "chunked")]
    SuperArray(Arc<SuperArray>),
    #[cfg(all(feature = "chunked", feature = "views"))]
    SuperArrayView(Arc<SuperArrayV>),
    #[cfg(feature = "chunked")]
    SuperTable(Arc<SuperTable>),
    #[cfg(all(feature = "chunked", feature = "views"))]
    SuperTableView(Arc<SuperTableV>),
    #[cfg(feature = "matrix")]
    Matrix(Arc<Matrix>),
    #[cfg(feature = "ndarray")]
    NdArray(Arc<NdArray<f64>>),
    #[cfg(all(feature = "ndarray", feature = "views"))]
    NdArrayView(Arc<NdArrayV<f64>>),
    #[cfg(all(feature = "ndarray", feature = "chunked"))]
    SuperNdArray(Arc<SuperNdArray<f64>>),
    #[cfg(all(feature = "ndarray", feature = "chunked", feature = "views"))]
    SuperNdArrayView(Arc<SuperNdArrayV<f64>>),
    #[cfg(feature = "xarray")]
    XArray(Arc<XArray<f64>>),
    #[cfg(feature = "cube")]
    Cube(Arc<Cube>),
    VecValue(Arc<Vec<Value>>),
    // For recursive
    BoxValue(Box<Value>),
    ArcValue(Arc<Value>),
    Tuple2(Arc<(Value, Value)>),
    Tuple3(Arc<(Value, Value, Value)>),
    Tuple4(Arc<(Value, Value, Value, Value)>),
    Tuple5(Arc<(Value, Value, Value, Value, Value)>),
    Tuple6(Arc<(Value, Value, Value, Value, Value, Value)>),

    /// Arbitrary user or library-defined payload.
    ///
    /// As long as the object implements `Debug`, `Clone`, and `PartialEq`,
    /// remains `Send + Sync`, and implements `Any` it can be stored in `Value::Custom`.
    /// `Any` is implemented automatically for all Rust types with a `'static` lifetime.
    ///
    /// Borrowed values **cannot** be used directly.
    /// These must be wrapped in `Arc` or otherwise promoted to `'static` to
    /// store inside `Value`.
    ///
    /// It's recommended that creators also implement `From` and `TryFrom`.
    Custom(Arc<dyn CustomValue>),
}

impl Value {
    // Length and Shape

    /// Computes the logical row/element count for the batch's input `Value`.
    ///
    /// This normalises the various `Value` representations so callers can consistently pass a
    /// `[start, len)` range to `execute_fn`. For the n-dimensional types
    /// this is the leading-axis observation count, matching the units
    /// `slice` windows over.
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            #[cfg(feature = "scalar_type")]
            Value::Scalar(_) => 1,

            Value::Table(t) => t.n_rows,

            #[cfg(feature = "views")]
            Value::TableView(tv) => tv.len,

            Value::Array(a) => a.len(),

            #[cfg(feature = "views")]
            Value::ArrayView(av) => av.len(),

            Value::FieldArray(fa) => fa.array.len(),

            #[cfg(feature = "chunked")]
            Value::SuperArray(sa) => sa.len(),

            #[cfg(all(feature = "chunked", feature = "views"))]
            Value::SuperArrayView(sav) => sav.len(),

            #[cfg(feature = "chunked")]
            Value::SuperTable(st) => st.len(),

            #[cfg(all(feature = "chunked", feature = "views"))]
            Value::SuperTableView(stv) => stv.len,

            #[cfg(feature = "matrix")]
            Value::Matrix(m) => m.len(),

            #[cfg(feature = "ndarray")]
            Value::NdArray(nd) => nd.shape()[0],

            #[cfg(all(feature = "ndarray", feature = "views"))]
            Value::NdArrayView(v) => v.shape()[0],

            #[cfg(all(feature = "ndarray", feature = "chunked"))]
            Value::SuperNdArray(snd) => snd.n_obs(),

            #[cfg(all(feature = "ndarray", feature = "chunked", feature = "views"))]
            Value::SuperNdArrayView(v) => v.n_obs(),

            #[cfg(feature = "xarray")]
            Value::XArray(xa) => xa.shape()[0],

            #[cfg(feature = "cube")]
            Value::Cube(c) => c.len(),

            // A vector of `Value`s is treated as a logical concatenation.
            Value::VecValue(vv) => vv.iter().map(|x| x.len()).sum(),

            // Recursive wrappers: delegate to the inner `Value`.
            Value::BoxValue(bv) => bv.len(),
            Value::ArcValue(av) => av.len(),

            // Tuples are treated as a logical concatenation of their elements.
            Value::Tuple2(t2) => t2.0.len() + t2.1.len(),
            Value::Tuple3(t3) => t3.0.len() + t3.1.len() + t3.2.len(),
            Value::Tuple4(t4) => t4.0.len() + t4.1.len() + t4.2.len() + t4.3.len(),
            Value::Tuple5(t5) => t5.0.len() + t5.1.len() + t5.2.len() + t5.3.len() + t5.4.len(),
            Value::Tuple6(t6) => {
                t6.0.len() + t6.1.len() + t6.2.len() + t6.3.len() + t6.4.len() + t6.5.len()
            }

            // Defer to the custom payload's notion of length (per `CustomValue` contract).
            Value::Custom(_cv) => panic!("Length is not implemented for custom value type."),
        }
    }

    /// Returns true if the value is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns a zero-copy view over `[offset .. offset + length)` rows of this Value.
    ///
    /// For table and array types this returns the corresponding view variant.
    /// For scalars, returns a clone since they are dimensionless.
    /// For recursive wrappers, delegates to the inner value.
    /// For tuples, slices each element independently.
    #[cfg(feature = "views")]
    pub fn slice(&self, offset: usize, length: usize) -> Value {
        match self {
            #[cfg(feature = "scalar_type")]
            Value::Scalar(s) => Value::Scalar(s.clone()),

            Value::Table(t) => Value::TableView(Arc::new(t.slice(offset, length))),
            Value::TableView(tv) => Value::TableView(Arc::new(tv.from_self(offset, length))),

            Value::Array(a) => {
                Value::ArrayView(Arc::new(ArrayV::new((**a).clone(), offset, length)))
            }
            Value::ArrayView(av) => Value::ArrayView(Arc::new(av.slice(offset, length))),

            Value::FieldArray(fa) => {
                Value::ArrayView(Arc::new(ArrayV::new(fa.array.clone(), offset, length)))
            }

            #[cfg(feature = "chunked")]
            Value::SuperArray(sa) => Value::SuperArrayView(Arc::new(sa.slice(offset, length))),
            #[cfg(all(feature = "chunked", feature = "views"))]
            Value::SuperArrayView(sav) => {
                Value::SuperArrayView(Arc::new(sav.slice(offset, length)))
            }
            #[cfg(feature = "chunked")]
            Value::SuperTable(st) => Value::SuperTableView(Arc::new(st.view(offset, length))),
            #[cfg(all(feature = "chunked", feature = "views"))]
            Value::SuperTableView(stv) => {
                Value::SuperTableView(Arc::new(stv.slice(offset, length)))
            }

            #[cfg(feature = "matrix")]
            Value::Matrix(_) => unimplemented!("Matrix slicing"),
            #[cfg(feature = "ndarray")]
            Value::NdArray(nd) => {
                assert!(
                    offset + length <= nd.shape()[0],
                    "Value::slice: window {}..{} out of bounds for axis 0 (size {})",
                    offset, offset + length, nd.shape()[0]
                );
                let mut window_shape = vec![length];
                window_shape.extend_from_slice(&nd.shape()[1..]);
                Value::NdArrayView(Arc::new(NdArrayV::new(
                    nd.as_ref().clone(),
                    offset * nd.strides()[0],
                    &window_shape,
                    nd.strides(),
                )))
            }
            #[cfg(all(feature = "ndarray", feature = "views"))]
            Value::NdArrayView(v) => {
                assert!(
                    offset + length <= v.shape()[0],
                    "Value::slice: window {}..{} out of bounds for axis 0 (size {})",
                    offset, offset + length, v.shape()[0]
                );
                let mut window_shape = vec![length];
                window_shape.extend_from_slice(&v.shape()[1..]);
                Value::NdArrayView(Arc::new(NdArrayV::new(
                    v.source.clone(),
                    v.offset + offset * v.strides()[0],
                    &window_shape,
                    v.strides(),
                )))
            }
            #[cfg(all(feature = "ndarray", feature = "chunked"))]
            Value::SuperNdArray(snd) => {
                Value::SuperNdArrayView(Arc::new(snd.slice(offset, length)))
            }
            #[cfg(all(feature = "ndarray", feature = "chunked", feature = "views"))]
            Value::SuperNdArrayView(v) => {
                Value::SuperNdArrayView(Arc::new(v.slice(offset, length)))
            }
            #[cfg(all(feature = "xarray", feature = "select"))]
            Value::XArray(xa) => {
                // An axis-0 window through select, which narrows the
                // leading axis coords alongside the data.
                let range = offset..offset + length;
                let dim0 = xa.dim_names()[0].to_string();
                Value::XArray(Arc::new(xa.select(&[(dim0.as_str(), &range)])))
            }
            #[cfg(all(feature = "xarray", not(feature = "select")))]
            Value::XArray(_) => unimplemented!("XArray slicing requires the select feature"),
            #[cfg(feature = "cube")]
            Value::Cube(_) => unimplemented!("Cube slicing"),

            Value::VecValue(v) => {
                let end = (offset + length).min(v.len());
                Value::VecValue(Arc::new(v[offset..end].to_vec()))
            }

            Value::BoxValue(bv) => bv.slice(offset, length),
            Value::ArcValue(av) => av.slice(offset, length),

            Value::Tuple2(t) => Value::Tuple2(Arc::new((
                t.0.slice(offset, length),
                t.1.slice(offset, length),
            ))),
            Value::Tuple3(t) => Value::Tuple3(Arc::new((
                t.0.slice(offset, length),
                t.1.slice(offset, length),
                t.2.slice(offset, length),
            ))),
            Value::Tuple4(t) => Value::Tuple4(Arc::new((
                t.0.slice(offset, length),
                t.1.slice(offset, length),
                t.2.slice(offset, length),
                t.3.slice(offset, length),
            ))),
            Value::Tuple5(t) => Value::Tuple5(Arc::new((
                t.0.slice(offset, length),
                t.1.slice(offset, length),
                t.2.slice(offset, length),
                t.3.slice(offset, length),
                t.4.slice(offset, length),
            ))),
            Value::Tuple6(t) => Value::Tuple6(Arc::new((
                t.0.slice(offset, length),
                t.1.slice(offset, length),
                t.2.slice(offset, length),
                t.3.slice(offset, length),
                t.4.slice(offset, length),
                t.5.slice(offset, length),
            ))),

            Value::Custom(_) => panic!("Slicing is not implemented for custom value types."),
        }
    }

    /// Returns how many piped data arguments a Value carries.
    ///
    /// Non-tuple Values represent a single argument and return 1.
    /// Tuple variants carry multiple arguments and return their width (2-6).
    pub fn arity(&self) -> usize {
        match self {
            Value::Tuple2(_) => 2,
            Value::Tuple3(_) => 3,
            Value::Tuple4(_) => 4,
            Value::Tuple5(_) => 5,
            Value::Tuple6(_) => 6,
            _ => 1,
        }
    }
}

// Also see typed accessors in ./conversions.rs and trait impls in ./impls.rs

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::{
        Array, ArrayV, ArrowType, Field, FieldArray, IntegerArray, MaskedArray, NumericArray, Table,
    };

    fn seq_array(n: usize) -> Array {
        let mut arr = IntegerArray::<i64>::default();
        for i in 0..n {
            arr.push(i as i64);
        }
        Array::NumericArray(NumericArray::Int64(Arc::new(arr)))
    }

    /// `len` counts the rows a view covers, so callers can pass the count
    /// straight to `slice`.
    #[cfg(feature = "views")]
    #[test]
    fn len_of_an_array_view_counts_the_window() {
        let view = ArrayV::new(seq_array(100), 30, 10);
        let value = Value::ArrayView(Arc::new(view));

        assert_eq!(value.len(), 10, "the window, not the backing array");
    }

    /// The two view variants agree, so an operator sizing its work from
    /// `len` behaves the same whichever shape it was handed.
    #[cfg(feature = "views")]
    #[test]
    fn len_agrees_across_the_view_variants() {
        let array = Value::ArrayView(Arc::new(ArrayV::new(seq_array(100), 30, 10)));

        let mut table = Table::new("t".to_string(), None);
        table.add_col(FieldArray::new(
            Field::new("v", ArrowType::Int64, false, None),
            seq_array(100),
        ));
        let table = Value::Table(Arc::new(table)).slice(30, 10);

        assert_eq!(array.len(), table.len());
    }

    /// `len` bounds a `slice` on the same value. The two are read together
    /// wherever work is split into row ranges, so a `len` drawn from the
    /// backing array would run a window past its own end.
    #[cfg(feature = "views")]
    #[test]
    fn len_bounds_a_slice_of_the_same_value() {
        let value = Value::ArrayView(Arc::new(ArrayV::new(seq_array(100), 30, 10)));

        let whole = value.slice(0, value.len());
        assert_eq!(whole.len(), 10);
    }
}