• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vortex-data / vortex / 16848689407

09 Aug 2025 11:04AM UTC coverage: 86.303% (+0.07%) from 86.234%
16848689407

Pull #4177

github

web-flow
Merge 4d406bf40 into de5f71d7b
Pull Request #4177: (WIP) feat: ArrayOperations infallible

619 of 689 new or added lines in 105 files covered. (89.84%)

13 existing lines in 8 files now uncovered.

53559 of 62059 relevant lines covered (86.3%)

543314.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.09
/vortex-array/src/array/mod.rs
1
//  SPDX-License-Identifier: Apache-2.0
2
//  SPDX-FileCopyrightText: Copyright the Vortex contributors
3

4
pub mod display;
5
mod visitor;
6

7
use std::any::Any;
8
use std::fmt::{Debug, Formatter};
9
use std::sync::Arc;
10

11
pub use visitor::*;
12
use vortex_buffer::ByteBuffer;
13
use vortex_dtype::{DType, Nullability};
14
use vortex_error::{VortexExpect, VortexResult, vortex_bail, vortex_err};
15
use vortex_mask::Mask;
16
use vortex_scalar::Scalar;
17

18
use crate::arrays::{
19
    BoolEncoding, ConstantVTable, DecimalEncoding, ExtensionEncoding, ListEncoding, NullEncoding,
20
    PrimitiveEncoding, StructEncoding, VarBinEncoding, VarBinViewEncoding,
21
};
22
use crate::builders::ArrayBuilder;
23
use crate::compute::{ComputeFn, Cost, InvocationArgs, IsConstantOpts, Output, is_constant_opts};
24
use crate::serde::ArrayChildren;
25
use crate::stats::{Precision, Stat, StatsSetRef};
26
use crate::vtable::{
27
    ArrayVTable, CanonicalVTable, ComputeVTable, OperationsVTable, SerdeVTable, VTable,
28
    ValidityVTable, VisitorVTable,
29
};
30
use crate::{Canonical, EncodingId, EncodingRef, SerializeMetadata};
31

32
/// The public API trait for all Vortex arrays.
33
pub trait Array: 'static + private::Sealed + Send + Sync + Debug + ArrayVisitor {
34
    /// Returns the array as a reference to a generic [`Any`] trait object.
35
    fn as_any(&self) -> &dyn Any;
36

37
    /// Returns the array as an [`ArrayRef`].
38
    fn to_array(&self) -> ArrayRef;
39

40
    /// Returns the length of the array.
41
    fn len(&self) -> usize;
42

43
    /// Returns whether the array is empty (has zero rows).
44
    fn is_empty(&self) -> bool {
497,624✔
45
        self.len() == 0
497,624✔
46
    }
497,624✔
47

48
    /// Returns the logical Vortex [`DType`] of the array.
49
    fn dtype(&self) -> &DType;
50

51
    /// Returns the encoding of the array.
52
    fn encoding(&self) -> EncodingRef;
53

54
    /// Returns the encoding ID of the array.
55
    fn encoding_id(&self) -> EncodingId;
56

57
    /// Performs a constant-time slice of the array.
58
    fn slice(&self, start: usize, end: usize) -> ArrayRef;
59

60
    /// Fetch the scalar at the given index.
61
    fn scalar_at(&self, index: usize) -> VortexResult<Scalar>;
62

63
    /// Returns whether the array is of the given encoding.
64
    fn is_encoding(&self, encoding: EncodingId) -> bool {
4,787,639✔
65
        self.encoding_id() == encoding
4,787,639✔
66
    }
4,787,639✔
67

68
    /// Returns whether this array is an arrow encoding.
69
    // TODO(ngates): this shouldn't live here.
70
    fn is_arrow(&self) -> bool {
44,399✔
71
        self.is_encoding(NullEncoding.id())
44,399✔
72
            || self.is_encoding(BoolEncoding.id())
44,399✔
73
            || self.is_encoding(PrimitiveEncoding.id())
35,678✔
74
            || self.is_encoding(VarBinEncoding.id())
23,877✔
75
            || self.is_encoding(VarBinViewEncoding.id())
23,877✔
76
    }
44,399✔
77

78
    /// Whether the array is of a canonical encoding.
79
    // TODO(ngates): this shouldn't live here.
80
    fn is_canonical(&self) -> bool {
756,386✔
81
        self.is_encoding(NullEncoding.id())
756,386✔
82
            || self.is_encoding(BoolEncoding.id())
756,386✔
83
            || self.is_encoding(PrimitiveEncoding.id())
741,828✔
84
            || self.is_encoding(DecimalEncoding.id())
474,500✔
85
            || self.is_encoding(StructEncoding.id())
472,450✔
86
            || self.is_encoding(ListEncoding.id())
470,697✔
87
            || self.is_encoding(VarBinViewEncoding.id())
470,375✔
88
            || self.is_encoding(ExtensionEncoding.id())
463,053✔
89
    }
756,386✔
90

91
    /// Returns whether the item at `index` is valid.
92
    fn is_valid(&self, index: usize) -> VortexResult<bool>;
93

94
    /// Returns whether the item at `index` is invalid.
95
    fn is_invalid(&self, index: usize) -> VortexResult<bool>;
96

97
    /// Returns whether all items in the array are valid.
98
    ///
99
    /// This is usually cheaper than computing a precise `valid_count`.
100
    fn all_valid(&self) -> VortexResult<bool>;
101

102
    /// Returns whether the array is all invalid.
103
    ///
104
    /// This is usually cheaper than computing a precise `invalid_count`.
105
    fn all_invalid(&self) -> VortexResult<bool>;
106

107
    /// Returns the number of valid elements in the array.
108
    fn valid_count(&self) -> VortexResult<usize>;
109

110
    /// Returns the number of invalid elements in the array.
111
    fn invalid_count(&self) -> VortexResult<usize>;
112

113
    /// Returns the canonical validity mask for the array.
114
    fn validity_mask(&self) -> VortexResult<Mask>;
115

116
    /// Returns the canonical representation of the array.
117
    fn to_canonical(&self) -> VortexResult<Canonical>;
118

119
    /// Writes the array into the canonical builder.
120
    ///
121
    /// The [`DType`] of the builder must match that of the array.
122
    fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()>;
123

124
    /// Returns the statistics of the array.
125
    // TODO(ngates): change how this works. It's weird.
126
    fn statistics(&self) -> StatsSetRef<'_>;
127

128
    /// Replaces the children of the array with the given array references.
129
    fn with_children(&self, children: &[ArrayRef]) -> VortexResult<ArrayRef>;
130

131
    /// Optionally invoke a kernel for the given compute function.
132
    ///
133
    /// These encoding-specific kernels are independent of kernels registered directly with
134
    /// compute functions using [`ComputeFn::register_kernel`], and are attempted only if none of
135
    /// the function-specific kernels returns a result.
136
    ///
137
    /// This allows encodings the opportunity to generically implement many compute functions
138
    /// that share some property, for example [`ComputeFn::is_elementwise`], without prior
139
    /// knowledge of the function itself, while still allowing users to override the implementation
140
    /// of compute functions for built-in encodings. For an example, see the implementation for
141
    /// chunked arrays.
142
    ///
143
    /// The first input in the [`InvocationArgs`] is always the array itself.
144
    ///
145
    /// Warning: do not call `compute_fn.invoke(args)` directly, as this will result in a recursive
146
    /// call.
147
    fn invoke(&self, compute_fn: &ComputeFn, args: &InvocationArgs)
148
    -> VortexResult<Option<Output>>;
149
}
150

151
impl Array for Arc<dyn Array> {
152
    fn as_any(&self) -> &dyn Any {
4,299,549✔
153
        self.as_ref().as_any()
4,299,549✔
154
    }
4,299,549✔
155

156
    fn to_array(&self) -> ArrayRef {
287,174✔
157
        self.clone()
287,174✔
158
    }
287,174✔
159

160
    fn len(&self) -> usize {
32,210,132✔
161
        self.as_ref().len()
32,210,132✔
162
    }
32,210,132✔
163

164
    fn dtype(&self) -> &DType {
10,429,882✔
165
        self.as_ref().dtype()
10,429,882✔
166
    }
10,429,882✔
167

168
    fn encoding(&self) -> EncodingRef {
23,355✔
169
        self.as_ref().encoding()
23,355✔
170
    }
23,355✔
171

172
    fn encoding_id(&self) -> EncodingId {
4,061,999✔
173
        self.as_ref().encoding_id()
4,061,999✔
174
    }
4,061,999✔
175

176
    fn slice(&self, start: usize, end: usize) -> ArrayRef {
1,885,831✔
177
        self.as_ref().slice(start, end)
1,885,831✔
178
    }
1,885,831✔
179

180
    fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
50,301,359✔
181
        self.as_ref().scalar_at(index)
50,301,359✔
182
    }
50,301,359✔
183

184
    fn is_valid(&self, index: usize) -> VortexResult<bool> {
4,940,500✔
185
        self.as_ref().is_valid(index)
4,940,500✔
186
    }
4,940,500✔
187

188
    fn is_invalid(&self, index: usize) -> VortexResult<bool> {
2,862✔
189
        self.as_ref().is_invalid(index)
2,862✔
190
    }
2,862✔
191

192
    fn all_valid(&self) -> VortexResult<bool> {
208,987✔
193
        self.as_ref().all_valid()
208,987✔
194
    }
208,987✔
195

196
    fn all_invalid(&self) -> VortexResult<bool> {
282,923✔
197
        self.as_ref().all_invalid()
282,923✔
198
    }
282,923✔
199

200
    fn valid_count(&self) -> VortexResult<usize> {
11,895✔
201
        self.as_ref().valid_count()
11,895✔
202
    }
11,895✔
203

204
    fn invalid_count(&self) -> VortexResult<usize> {
8,458✔
205
        self.as_ref().invalid_count()
8,458✔
206
    }
8,458✔
207

208
    fn validity_mask(&self) -> VortexResult<Mask> {
36,877✔
209
        self.as_ref().validity_mask()
36,877✔
210
    }
36,877✔
211

212
    fn to_canonical(&self) -> VortexResult<Canonical> {
4,346,107✔
213
        self.as_ref().to_canonical()
4,346,107✔
214
    }
4,346,107✔
215

216
    fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()> {
51,734✔
217
        self.as_ref().append_to_builder(builder)
51,734✔
218
    }
51,734✔
219

220
    fn statistics(&self) -> StatsSetRef<'_> {
4,745,572✔
221
        self.as_ref().statistics()
4,745,572✔
222
    }
4,745,572✔
223

224
    fn with_children(&self, children: &[ArrayRef]) -> VortexResult<ArrayRef> {
×
225
        self.as_ref().with_children(children)
×
226
    }
×
227

228
    fn invoke(
572,503✔
229
        &self,
572,503✔
230
        compute_fn: &ComputeFn,
572,503✔
231
        args: &InvocationArgs,
572,503✔
232
    ) -> VortexResult<Option<Output>> {
572,503✔
233
        self.as_ref().invoke(compute_fn, args)
572,503✔
234
    }
572,503✔
235
}
236

237
/// A reference counted pointer to a dynamic [`Array`] trait object.
238
pub type ArrayRef = Arc<dyn Array>;
239

240
impl ToOwned for dyn Array {
241
    type Owned = ArrayRef;
242

243
    fn to_owned(&self) -> Self::Owned {
×
244
        self.to_array()
×
245
    }
×
246
}
247

248
impl dyn Array + '_ {
249
    /// Returns the array downcast to the given `A`.
250
    pub fn as_<V: VTable>(&self) -> &V::Array {
425✔
251
        self.as_opt::<V>().vortex_expect("Failed to downcast")
425✔
252
    }
425✔
253

254
    /// Returns the array downcast to the given `A`.
255
    pub fn as_opt<V: VTable>(&self) -> Option<&V::Array> {
13,470,445✔
256
        self.as_any()
13,470,445✔
257
            .downcast_ref::<ArrayAdapter<V>>()
13,470,445✔
258
            .map(|array_adapter| &array_adapter.0)
13,470,445✔
259
    }
13,470,445✔
260

261
    /// Is self an array with encoding from vtable `V`.
262
    pub fn is<V: VTable>(&self) -> bool {
4,354,446✔
263
        self.as_opt::<V>().is_some()
4,354,446✔
264
    }
4,354,446✔
265

266
    pub fn is_constant(&self) -> bool {
1,130,605✔
267
        let opts = IsConstantOpts {
1,130,605✔
268
            cost: Cost::Specialized,
1,130,605✔
269
        };
1,130,605✔
270
        is_constant_opts(self, &opts)
1,130,605✔
271
            .inspect_err(|e| log::warn!("Failed to compute IsConstant: {e}"))
1,130,605✔
272
            .ok()
1,130,605✔
273
            .flatten()
1,130,605✔
274
            .unwrap_or_default()
1,130,605✔
275
    }
1,130,605✔
276

277
    pub fn is_constant_opts(&self, cost: Cost) -> bool {
×
278
        let opts = IsConstantOpts { cost };
×
279
        is_constant_opts(self, &opts)
×
280
            .inspect_err(|e| log::warn!("Failed to compute IsConstant: {e}"))
×
281
            .ok()
×
282
            .flatten()
×
283
            .unwrap_or_default()
×
284
    }
×
285

286
    pub fn as_constant(&self) -> Option<Scalar> {
541,474✔
287
        self.is_constant().then(|| self.scalar_at(0).ok()).flatten()
541,474✔
288
    }
541,474✔
289

290
    /// Total size of the array in bytes, including all children and buffers.
291
    pub fn nbytes(&self) -> u64 {
185,141✔
292
        let mut nbytes = 0;
185,141✔
293
        for array in self.depth_first_traversal() {
259,669✔
294
            for buffer in array.buffers() {
308,656✔
295
                nbytes += buffer.len() as u64;
308,656✔
296
            }
308,656✔
297
        }
298
        nbytes
185,141✔
299
    }
185,141✔
300
}
301

302
/// Trait for converting a type into a Vortex [`ArrayRef`].
303
pub trait IntoArray {
304
    fn into_array(self) -> ArrayRef;
305
}
306

307
impl IntoArray for ArrayRef {
308
    fn into_array(self) -> ArrayRef {
5,059✔
309
        self
5,059✔
310
    }
5,059✔
311
}
312

313
mod private {
314
    use super::*;
315

316
    pub trait Sealed {}
317

318
    impl<V: VTable> Sealed for ArrayAdapter<V> {}
319
    impl Sealed for Arc<dyn Array> {}
320
}
321

322
/// Adapter struct used to lift the [`VTable`] trait into an object-safe [`Array`]
323
/// implementation.
324
///
325
/// Since this is a unit struct with `repr(transparent)`, we are able to turn un-adapted array
326
/// structs into [`dyn Array`] using some cheeky casting inside [`std::ops::Deref`] and
327
/// [`AsRef`]. See the `vtable!` macro for more details.
328
#[repr(transparent)]
329
pub struct ArrayAdapter<V: VTable>(V::Array);
330

331
impl<V: VTable> ArrayAdapter<V> {
332
    /// Provide a reference to the underlying array held within the adapter.
333
    pub fn as_inner(&self) -> &V::Array {
×
334
        &self.0
×
335
    }
×
336

337
    /// Unwrap into the inner array type, consuming the adapter.
338
    pub fn into_inner(self) -> V::Array {
×
339
        self.0
×
340
    }
×
341
}
342

343
impl<V: VTable> Debug for ArrayAdapter<V> {
344
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
×
345
        self.0.fmt(f)
×
346
    }
×
347
}
348

349
impl<V: VTable> Array for ArrayAdapter<V> {
350
    fn as_any(&self) -> &dyn Any {
8,636,463✔
351
        self
8,636,463✔
352
    }
8,636,463✔
353

354
    fn to_array(&self) -> ArrayRef {
738,245✔
355
        Arc::new(ArrayAdapter::<V>(self.0.clone()))
738,245✔
356
    }
738,245✔
357

358
    fn len(&self) -> usize {
195,498,623✔
359
        <V::ArrayVTable as ArrayVTable<V>>::len(&self.0)
195,498,623✔
360
    }
195,498,623✔
361

362
    fn dtype(&self) -> &DType {
368,396,200✔
363
        <V::ArrayVTable as ArrayVTable<V>>::dtype(&self.0)
368,396,200✔
364
    }
368,396,200✔
365

366
    fn encoding(&self) -> EncodingRef {
28,754✔
367
        V::encoding(&self.0)
28,754✔
368
    }
28,754✔
369

370
    fn encoding_id(&self) -> EncodingId {
3,440,751✔
371
        V::encoding(&self.0).id()
3,440,751✔
372
    }
3,440,751✔
373

374
    fn slice(&self, start: usize, stop: usize) -> ArrayRef {
2,854,223✔
375
        if start == 0 && stop == self.len() {
2,854,223✔
376
            return self.to_array();
135,382✔
377
        }
2,718,841✔
378

379
        assert!(
2,718,841✔
380
            start <= self.len(),
2,718,841✔
NEW
381
            "OutOfBounds: start {start} > length {}",
×
NEW
382
            self.len()
×
383
        );
384
        assert!(
2,718,841✔
385
            stop <= self.len(),
2,718,841✔
NEW
386
            "OutOfBounds: stop {stop} > length {}",
×
NEW
387
            self.len()
×
388
        );
389

390
        assert!(start <= stop, "start ({start}) must be <= stop ({stop})");
2,718,841✔
391

392
        if start == stop {
2,718,841✔
393
            return Canonical::empty(self.dtype()).into_array();
454✔
394
        }
2,718,387✔
395

396
        let sliced = <V::OperationsVTable as OperationsVTable<V>>::slice(&self.0, start, stop);
2,718,387✔
397

398
        assert_eq!(
2,718,387✔
399
            sliced.len(),
2,718,387✔
400
            stop - start,
2,718,387✔
401
            "Slice length mismatch {}",
×
402
            self.encoding_id()
×
403
        );
404

405
        // Slightly more expensive, so only do this in debug builds.
406
        debug_assert_eq!(
2,718,387✔
407
            sliced.dtype(),
2,718,387✔
408
            self.dtype(),
2,718,387✔
409
            "Slice dtype mismatch {}",
×
410
            self.encoding_id()
×
411
        );
412

413
        // Propagate some stats from the original array to the sliced array.
414
        if !sliced.is::<ConstantVTable>() {
2,718,387✔
415
            self.statistics().with_iter(|iter| {
2,586,131✔
416
                sliced.statistics().inherit(iter.filter(|(stat, value)| {
2,641,610✔
417
                    matches!(
142,872✔
418
                        stat,
188,623✔
419
                        Stat::IsConstant | Stat::IsSorted | Stat::IsStrictSorted
420
                    ) && value.as_ref().as_exact().is_some_and(|v| {
45,751✔
421
                        Scalar::new(DType::Bool(Nullability::NonNullable), v.clone())
45,751✔
422
                            .as_bool()
45,751✔
423
                            .value()
45,751✔
424
                            .unwrap_or_default()
45,751✔
425
                    })
45,751✔
426
                }));
188,623✔
427
            });
2,586,131✔
428
        }
132,256✔
429

430
        sliced
2,718,387✔
431
    }
2,854,223✔
432

433
    fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
63,563,935✔
434
        if index >= self.len() {
63,563,935✔
435
            vortex_bail!(OutOfBounds: index, 0, self.len());
8✔
436
        }
63,563,927✔
437
        if self.is_invalid(index)? {
63,563,927✔
438
            return Ok(Scalar::null(self.dtype().clone()));
3,899,133✔
439
        }
59,664,794✔
440
        let scalar = <V::OperationsVTable as OperationsVTable<V>>::scalar_at(&self.0, index)?;
59,664,794✔
441
        assert_eq!(self.dtype(), scalar.dtype(), "Scalar dtype mismatch");
59,664,794✔
442
        Ok(scalar)
59,664,794✔
443
    }
63,563,935✔
444

445
    fn is_valid(&self, index: usize) -> VortexResult<bool> {
76,164,241✔
446
        if index >= self.len() {
76,164,241✔
447
            vortex_bail!(OutOfBounds: index, 0, self.len());
×
448
        }
76,164,241✔
449
        <V::ValidityVTable as ValidityVTable<V>>::is_valid(&self.0, index)
76,164,241✔
450
    }
76,164,241✔
451

452
    fn is_invalid(&self, index: usize) -> VortexResult<bool> {
63,567,201✔
453
        self.is_valid(index).map(|valid| !valid)
63,567,201✔
454
    }
63,567,201✔
455

456
    fn all_valid(&self) -> VortexResult<bool> {
19,387,556✔
457
        <V::ValidityVTable as ValidityVTable<V>>::all_valid(&self.0)
19,387,556✔
458
    }
19,387,556✔
459

460
    fn all_invalid(&self) -> VortexResult<bool> {
446,271✔
461
        <V::ValidityVTable as ValidityVTable<V>>::all_invalid(&self.0)
446,271✔
462
    }
446,271✔
463

464
    fn valid_count(&self) -> VortexResult<usize> {
84,004✔
465
        if let Some(Precision::Exact(invalid_count)) =
12,008✔
466
            self.statistics().get_as::<usize>(Stat::NullCount)
84,004✔
467
        {
468
            return Ok(self.len() - invalid_count);
12,008✔
469
        }
71,996✔
470

471
        let count = <V::ValidityVTable as ValidityVTable<V>>::valid_count(&self.0)?;
71,996✔
472
        assert!(count <= self.len(), "Valid count exceeds array length");
71,996✔
473

474
        self.statistics()
71,996✔
475
            .set(Stat::NullCount, Precision::exact(self.len() - count));
71,996✔
476

477
        Ok(count)
71,996✔
478
    }
84,004✔
479

480
    fn invalid_count(&self) -> VortexResult<usize> {
13,617✔
481
        if let Some(Precision::Exact(invalid_count)) =
306✔
482
            self.statistics().get_as::<usize>(Stat::NullCount)
13,617✔
483
        {
484
            return Ok(invalid_count);
306✔
485
        }
13,311✔
486

487
        let count = <V::ValidityVTable as ValidityVTable<V>>::invalid_count(&self.0)?;
13,311✔
488
        assert!(count <= self.len(), "Invalid count exceeds array length");
13,311✔
489

490
        self.statistics()
13,311✔
491
            .set(Stat::NullCount, Precision::exact(count));
13,311✔
492

493
        Ok(count)
13,311✔
494
    }
13,617✔
495

496
    fn validity_mask(&self) -> VortexResult<Mask> {
835,823✔
497
        let mask = <V::ValidityVTable as ValidityVTable<V>>::validity_mask(&self.0)?;
835,823✔
498
        assert_eq!(mask.len(), self.len(), "Validity mask length mismatch");
835,823✔
499
        Ok(mask)
835,823✔
500
    }
835,823✔
501

502
    fn to_canonical(&self) -> VortexResult<Canonical> {
3,756,018✔
503
        let canonical = <V::CanonicalVTable as CanonicalVTable<V>>::canonicalize(&self.0)?;
3,756,018✔
504
        assert_eq!(
3,756,018✔
505
            self.len(),
3,756,018✔
506
            canonical.as_ref().len(),
3,756,018✔
507
            "Canonical length mismatch {}. Expected {} but encoded into {}.",
×
508
            self.encoding_id(),
×
509
            self.len(),
×
510
            canonical.as_ref().len()
×
511
        );
512
        assert_eq!(
3,756,018✔
513
            self.dtype(),
3,756,018✔
514
            canonical.as_ref().dtype(),
3,756,018✔
515
            "Canonical dtype mismatch {}. Expected {} but encoded into {}.",
×
516
            self.encoding_id(),
×
517
            self.dtype(),
×
518
            canonical.as_ref().dtype()
×
519
        );
520
        canonical
3,756,018✔
521
            .as_ref()
3,756,018✔
522
            .statistics()
3,756,018✔
523
            .replace(self.statistics().to_owned());
3,756,018✔
524
        Ok(canonical)
3,756,018✔
525
    }
3,756,018✔
526

527
    fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()> {
54,426✔
528
        if builder.dtype() != self.dtype() {
54,426✔
529
            vortex_bail!(
×
530
                "Builder dtype mismatch: expected {}, got {}",
×
531
                self.dtype(),
×
532
                builder.dtype(),
×
533
            );
534
        }
54,426✔
535
        let len = builder.len();
54,426✔
536

537
        <V::CanonicalVTable as CanonicalVTable<V>>::append_to_builder(&self.0, builder)?;
54,426✔
538
        assert_eq!(
54,426✔
539
            len + self.len(),
54,426✔
540
            builder.len(),
54,426✔
541
            "Builder length mismatch after writing array for encoding {}",
×
542
            self.encoding_id(),
×
543
        );
544
        Ok(())
54,426✔
545
    }
54,426✔
546

547
    fn statistics(&self) -> StatsSetRef<'_> {
16,132,998✔
548
        <V::ArrayVTable as ArrayVTable<V>>::stats(&self.0)
16,132,998✔
549
    }
16,132,998✔
550

551
    fn with_children(&self, children: &[ArrayRef]) -> VortexResult<ArrayRef> {
×
552
        struct ReplacementChildren<'a> {
553
            children: &'a [ArrayRef],
554
        }
555

556
        impl ArrayChildren for ReplacementChildren<'_> {
557
            fn get(&self, index: usize, dtype: &DType, len: usize) -> VortexResult<ArrayRef> {
×
558
                if index >= self.children.len() {
×
559
                    vortex_bail!(OutOfBounds: index, 0, self.children.len());
×
560
                }
×
561
                let child = &self.children[index];
×
562
                if child.len() != len {
×
563
                    vortex_bail!(
×
564
                        "Child length mismatch: expected {}, got {}",
×
565
                        len,
566
                        child.len()
×
567
                    );
568
                }
×
569
                if child.dtype() != dtype {
×
570
                    vortex_bail!(
×
571
                        "Child dtype mismatch: expected {}, got {}",
×
572
                        dtype,
573
                        child.dtype()
×
574
                    );
575
                }
×
576
                Ok(child.clone())
×
577
            }
×
578

579
            fn len(&self) -> usize {
×
580
                self.children.len()
×
581
            }
×
582
        }
583

584
        let metadata = self.metadata()?.ok_or_else(|| {
×
585
            vortex_err!("Cannot replace children for arrays that do not support serialization")
×
586
        })?;
×
587

588
        // Replace the children of the array by re-building the array from parts.
589
        self.encoding().build(
×
590
            self.dtype(),
×
591
            self.len(),
×
592
            &metadata,
×
593
            &self.buffers(),
×
594
            &ReplacementChildren { children },
×
595
        )
×
596
    }
×
597

598
    fn invoke(
480,770✔
599
        &self,
480,770✔
600
        compute_fn: &ComputeFn,
480,770✔
601
        args: &InvocationArgs,
480,770✔
602
    ) -> VortexResult<Option<Output>> {
480,770✔
603
        <V::ComputeVTable as ComputeVTable<V>>::invoke(&self.0, compute_fn, args)
480,770✔
604
    }
480,770✔
605
}
606

607
impl<V: VTable> ArrayVisitor for ArrayAdapter<V> {
608
    fn children(&self) -> Vec<ArrayRef> {
352,574✔
609
        struct ChildrenCollector {
610
            children: Vec<ArrayRef>,
611
        }
612

613
        impl ArrayChildVisitor for ChildrenCollector {
614
            fn visit_child(&mut self, _name: &str, array: &dyn Array) {
142,435✔
615
                self.children.push(array.to_array());
142,435✔
616
            }
142,435✔
617
        }
618

619
        let mut collector = ChildrenCollector {
352,574✔
620
            children: Vec::new(),
352,574✔
621
        };
352,574✔
622
        <V::VisitorVTable as VisitorVTable<V>>::visit_children(&self.0, &mut collector);
352,574✔
623
        collector.children
352,574✔
624
    }
352,574✔
625

626
    fn nchildren(&self) -> usize {
×
627
        <V::VisitorVTable as VisitorVTable<V>>::nchildren(&self.0)
×
628
    }
×
629

630
    fn children_names(&self) -> Vec<String> {
741✔
631
        struct ChildNameCollector {
632
            names: Vec<String>,
633
        }
634

635
        impl ArrayChildVisitor for ChildNameCollector {
636
            fn visit_child(&mut self, name: &str, _array: &dyn Array) {
508✔
637
                self.names.push(name.to_string());
508✔
638
            }
508✔
639
        }
640

641
        let mut collector = ChildNameCollector { names: Vec::new() };
741✔
642
        <V::VisitorVTable as VisitorVTable<V>>::visit_children(&self.0, &mut collector);
741✔
643
        collector.names
741✔
644
    }
741✔
645

646
    fn named_children(&self) -> Vec<(String, ArrayRef)> {
×
647
        struct NamedChildrenCollector {
648
            children: Vec<(String, ArrayRef)>,
649
        }
650

651
        impl ArrayChildVisitor for NamedChildrenCollector {
652
            fn visit_child(&mut self, name: &str, array: &dyn Array) {
×
653
                self.children.push((name.to_string(), array.to_array()));
×
654
            }
×
655
        }
656

657
        let mut collector = NamedChildrenCollector {
×
658
            children: Vec::new(),
×
659
        };
×
660

661
        <V::VisitorVTable as VisitorVTable<V>>::visit_children(&self.0, &mut collector);
×
662
        collector.children
×
663
    }
×
664

665
    fn buffers(&self) -> Vec<ByteBuffer> {
271,317✔
666
        struct BufferCollector {
667
            buffers: Vec<ByteBuffer>,
668
        }
669

670
        impl ArrayBufferVisitor for BufferCollector {
671
            fn visit_buffer(&mut self, buffer: &ByteBuffer) {
335,422✔
672
                self.buffers.push(buffer.clone());
335,422✔
673
            }
335,422✔
674
        }
675

676
        let mut collector = BufferCollector {
271,317✔
677
            buffers: Vec::new(),
271,317✔
678
        };
271,317✔
679
        <V::VisitorVTable as VisitorVTable<V>>::visit_buffers(&self.0, &mut collector);
271,317✔
680
        collector.buffers
271,317✔
681
    }
271,317✔
682

683
    fn nbuffers(&self) -> usize {
52,542✔
684
        <V::VisitorVTable as VisitorVTable<V>>::nbuffers(&self.0)
52,542✔
685
    }
52,542✔
686

687
    fn metadata(&self) -> VortexResult<Option<Vec<u8>>> {
57,118✔
688
        Ok(<V::SerdeVTable as SerdeVTable<V>>::metadata(&self.0)?.map(|m| m.serialize()))
57,118✔
689
    }
57,118✔
690

691
    fn metadata_fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
741✔
692
        match <V::SerdeVTable as SerdeVTable<V>>::metadata(&self.0) {
741✔
693
            Err(e) => write!(f, "<serde error: {e}>"),
×
694
            Ok(None) => write!(f, "<serde not supported>"),
×
695
            Ok(Some(metadata)) => Debug::fmt(&metadata, f),
741✔
696
        }
697
    }
741✔
698
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc