vortex-array 0.59.4

Vortex in memory columnar data format
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use num_traits::AsPrimitive;
use vortex_buffer::Buffer;
use vortex_buffer::ByteBuffer;
use vortex_dtype::DType;
use vortex_dtype::IntegerPType;
use vortex_dtype::Nullability;
use vortex_dtype::match_each_integer_ptype;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;

use crate::Array;
use crate::ArrayRef;
use crate::IntoArray;
use crate::ToCanonical;
use crate::arrays::varbin::builder::VarBinBuilder;
use crate::buffer::BufferHandle;
use crate::stats::ArrayStats;
use crate::validity::Validity;

#[derive(Clone, Debug)]
pub struct VarBinArray {
    pub(super) dtype: DType,
    pub(super) bytes: BufferHandle,
    pub(super) offsets: ArrayRef,
    pub(super) validity: Validity,
    pub(super) stats_set: ArrayStats,
}

impl VarBinArray {
    /// Creates a new [`VarBinArray`].
    ///
    /// # Panics
    ///
    /// Panics if the provided components do not satisfy the invariants documented
    /// in [`VarBinArray::new_unchecked`].
    pub fn new(offsets: ArrayRef, bytes: ByteBuffer, dtype: DType, validity: Validity) -> Self {
        Self::try_new(offsets, bytes, dtype, validity).vortex_expect("VarBinArray new")
    }

    /// Creates a new [`VarBinArray`].
    ///
    /// # Panics
    ///
    /// Panics if the provided components do not satisfy the invariants documented
    /// in [`VarBinArray::new_unchecked`].
    pub fn new_from_handle(
        offset: ArrayRef,
        bytes: BufferHandle,
        dtype: DType,
        validity: Validity,
    ) -> Self {
        Self::try_new_from_handle(offset, bytes, dtype, validity).vortex_expect("VarBinArray new")
    }

    /// Constructs a new `VarBinArray`.
    ///
    /// See [`VarBinArray::new_unchecked`] for more information.
    ///
    /// # Errors
    ///
    /// Returns an error if the provided components do not satisfy the invariants documented in
    /// [`VarBinArray::new_unchecked`].
    pub fn try_new(
        offsets: ArrayRef,
        bytes: ByteBuffer,
        dtype: DType,
        validity: Validity,
    ) -> VortexResult<Self> {
        let bytes = BufferHandle::new_host(bytes);
        Self::validate(&offsets, &bytes, &dtype, &validity)?;

        // SAFETY: validate ensures all invariants are met.
        Ok(unsafe { Self::new_unchecked_from_handle(offsets, bytes, dtype, validity) })
    }

    /// Constructs a new `VarBinArray` from a `BufferHandle` of memory that may exist
    /// on the CPU or GPU.
    ///
    /// See [`VarBinArray::new_unchecked`] for more information.
    ///
    /// # Errors
    ///
    /// Returns an error if the provided components do not satisfy the invariants documented in
    /// [`VarBinArray::new_unchecked`].
    pub fn try_new_from_handle(
        offsets: ArrayRef,
        bytes: BufferHandle,
        dtype: DType,
        validity: Validity,
    ) -> VortexResult<Self> {
        Self::validate(&offsets, &bytes, &dtype, &validity)?;

        // SAFETY: validate ensures all invariants are met.
        Ok(unsafe { Self::new_unchecked_from_handle(offsets, bytes, dtype, validity) })
    }

    /// Creates a new [`VarBinArray`] without validation from these components:
    ///
    /// * `offsets` is an array of byte offsets into the `bytes` buffer.
    /// * `bytes` is a buffer containing all the variable-length data concatenated.
    /// * `dtype` specifies whether this contains UTF-8 strings or binary data.
    /// * `validity` holds the null values.
    ///
    /// # Safety
    ///
    /// The caller must ensure all of the following invariants are satisfied:
    ///
    /// ## Offsets Requirements
    ///
    /// - `offsets` must be a non-nullable integer array.
    /// - `offsets` must contain at least 1 element (for empty array, it contains \[0\]).
    /// - All values in `offsets` must be monotonically non-decreasing.
    /// - The first value in `offsets` must be 0.
    /// - No offset value may exceed `bytes.len()`.
    ///
    /// ## Type Requirements
    ///
    /// - `dtype` must be exactly [`DType::Binary`] or [`DType::Utf8`].
    /// - If `dtype` is [`DType::Utf8`], every byte slice `bytes[offsets[i]..offsets[i+1]]` must be valid UTF-8.
    /// - `dtype.is_nullable()` must match the nullability of `validity`.
    ///
    /// ## Validity Requirements
    ///
    /// - If `validity` is [`Validity::Array`], its length must exactly equal `offsets.len() - 1`.
    pub unsafe fn new_unchecked(
        offsets: ArrayRef,
        bytes: ByteBuffer,
        dtype: DType,
        validity: Validity,
    ) -> Self {
        // SAFETY: `new_unchecked_from_handle` has same invariants which should be checked
        //  by caller.
        unsafe {
            Self::new_unchecked_from_handle(offsets, BufferHandle::new_host(bytes), dtype, validity)
        }
    }

    /// Creates a new [`VarBinArray`] without validation from its components, with string data
    /// stored in a `BufferHandle` (CPU or GPU).
    ///
    /// # Safety
    ///
    /// The caller must ensure all the invariants documented in `new_unchecked` are satisfied.
    pub unsafe fn new_unchecked_from_handle(
        offsets: ArrayRef,
        bytes: BufferHandle,
        dtype: DType,
        validity: Validity,
    ) -> Self {
        #[cfg(debug_assertions)]
        Self::validate(&offsets, &bytes, &dtype, &validity)
            .vortex_expect("[Debug Assertion]: Invalid `VarBinArray` parameters");

        Self {
            dtype,
            bytes,
            offsets,
            validity,
            stats_set: Default::default(),
        }
    }

    /// Validates the components that would be used to create a [`VarBinArray`].
    ///
    /// This function checks all the invariants required by [`VarBinArray::new_unchecked`].
    pub fn validate(
        offsets: &dyn Array,
        bytes: &BufferHandle,
        dtype: &DType,
        validity: &Validity,
    ) -> VortexResult<()> {
        // Check offsets are non-nullable integer
        vortex_ensure!(
            offsets.dtype().is_int() && !offsets.dtype().is_nullable(),
            MismatchedTypes: "non nullable int", offsets.dtype()
        );

        // Check dtype is Binary or Utf8
        vortex_ensure!(
            matches!(dtype, DType::Binary(_) | DType::Utf8(_)),
            MismatchedTypes: "utf8 or binary", dtype
        );

        // Check nullability matches
        vortex_ensure!(
            dtype.is_nullable() != (validity == &Validity::NonNullable),
            InvalidArgument: "incorrect validity {:?} for dtype {}",
            validity,
            dtype
        );

        // Check offsets has at least one element
        vortex_ensure!(
            !offsets.is_empty(),
            InvalidArgument: "Offsets must have at least one element"
        );

        // Skip host-only validation when offsets/bytes are not host-resident.
        if offsets.is_host() && bytes.is_on_host() {
            let last_offset = offsets
                .scalar_at(offsets.len() - 1)?
                .as_primitive()
                .as_::<usize>()
                .ok_or_else(
                    || vortex_err!(InvalidArgument: "Last offset must be convertible to usize"),
                )?;
            vortex_ensure!(
                last_offset <= bytes.len(),
                InvalidArgument: "Last offset {} exceeds bytes length {}",
                last_offset,
                bytes.len()
            );
        }

        // Check validity length
        if let Some(validity_len) = validity.maybe_len() {
            vortex_ensure!(
                validity_len == offsets.len() - 1,
                "Validity length {} doesn't match array length {}",
                validity_len,
                offsets.len() - 1
            );
        }

        // Validate UTF-8 for Utf8 dtype. Skip when offsets/bytes are not host-resident.
        if offsets.is_host()
            && bytes.is_on_host()
            && matches!(dtype, DType::Utf8(_))
            && let Some(bytes) = bytes.as_host_opt()
        {
            let primitive_offsets = offsets.to_primitive();
            match_each_integer_ptype!(primitive_offsets.dtype().as_ptype(), |O| {
                let offsets_slice = primitive_offsets.as_slice::<O>();
                for (i, (start, end)) in offsets_slice
                    .windows(2)
                    .map(|o| (o[0].as_(), o[1].as_()))
                    .enumerate()
                {
                    if validity.is_null(i)? {
                        continue;
                    }

                    let string_bytes = &bytes.as_ref()[start..end];
                    simdutf8::basic::from_utf8(string_bytes).map_err(|_| {
                        #[allow(clippy::unwrap_used)]
                        // run validation using `compat` package to get more detailed error message
                        let err = simdutf8::compat::from_utf8(string_bytes).unwrap_err();
                        vortex_err!("invalid utf-8: {err} at index {i}")
                    })?;
                }
            });
        }

        Ok(())
    }

    #[inline]
    pub fn offsets(&self) -> &ArrayRef {
        &self.offsets
    }

    /// Access the value bytes child buffer
    ///
    /// # Note
    ///
    /// Bytes child buffer is never sliced when the array is sliced so this can include values
    /// that are not logically present in the array. Users should prefer [sliced_bytes][Self::sliced_bytes]
    /// unless they're resolving values via the offset child array.
    #[inline]
    pub fn bytes(&self) -> &ByteBuffer {
        self.bytes.as_host()
    }

    /// Access the value bytes buffer handle.
    #[inline]
    pub fn bytes_handle(&self) -> &BufferHandle {
        &self.bytes
    }

    /// Access value bytes child array limited to values that are logically present in
    /// the array unlike [bytes][Self::bytes].
    pub fn sliced_bytes(&self) -> ByteBuffer {
        let first_offset: usize = self.offset_at(0);
        let last_offset = self.offset_at(self.len());

        self.bytes().slice(first_offset..last_offset)
    }

    pub fn from_vec<T: AsRef<[u8]>>(vec: Vec<T>, dtype: DType) -> Self {
        let size: usize = vec.iter().map(|v| v.as_ref().len()).sum();
        if size < u32::MAX as usize {
            Self::from_vec_sized::<u32, T>(vec, dtype)
        } else {
            Self::from_vec_sized::<u64, T>(vec, dtype)
        }
    }

    fn from_vec_sized<O, T>(vec: Vec<T>, dtype: DType) -> Self
    where
        O: IntegerPType,
        T: AsRef<[u8]>,
    {
        let mut builder = VarBinBuilder::<O>::with_capacity(vec.len());
        for v in vec {
            builder.append_value(v.as_ref());
        }
        builder.finish(dtype)
    }

    #[expect(
        clippy::same_name_method,
        reason = "intentionally named from_iter like Iterator::from_iter"
    )]
    pub fn from_iter<T: AsRef<[u8]>, I: IntoIterator<Item = Option<T>>>(
        iter: I,
        dtype: DType,
    ) -> Self {
        let iter = iter.into_iter();
        let mut builder = VarBinBuilder::<u32>::with_capacity(iter.size_hint().0);
        for v in iter {
            builder.append(v.as_ref().map(|o| o.as_ref()));
        }
        builder.finish(dtype)
    }

    pub fn from_iter_nonnull<T: AsRef<[u8]>, I: IntoIterator<Item = T>>(
        iter: I,
        dtype: DType,
    ) -> Self {
        let iter = iter.into_iter();
        let mut builder = VarBinBuilder::<u32>::with_capacity(iter.size_hint().0);
        for v in iter {
            builder.append_value(v);
        }
        builder.finish(dtype)
    }

    /// Get value offset at a given index
    ///
    /// Note: There's 1 more offsets than the elements in the array, thus last offset is at array length index
    ///
    /// Panics if index is out of bounds
    pub fn offset_at(&self, index: usize) -> usize {
        assert!(
            index <= self.len(),
            "Index {index} out of bounds 0..={}",
            self.len()
        );

        (&self
            .offsets()
            .scalar_at(index)
            .vortex_expect("offsets must support scalar_at"))
            .try_into()
            .vortex_expect("Failed to convert offset to usize")
    }

    /// Access value bytes at a given index
    ///
    /// Will return buffer referencing underlying data without performing a copy
    pub fn bytes_at(&self, index: usize) -> ByteBuffer {
        let start = self.offset_at(index);
        let end = self.offset_at(index + 1);

        self.bytes().slice(start..end)
    }

    /// Consumes self, returning a tuple containing the `DType`, the `bytes` array,
    /// the `offsets` array, and the `validity`.
    pub fn into_parts(self) -> (DType, BufferHandle, ArrayRef, Validity) {
        (self.dtype, self.bytes, self.offsets, self.validity)
    }
}

impl VarBinArray {
    /// Return an array containing the same data, but where the internal `offsets` start at zero
    /// and all wasted space in the bytes child has been clipped.
    #[doc(hidden)]
    pub fn zero_offsets(self) -> Self {
        if self.is_empty() {
            return self;
        }

        let first = self.offset_at(0);

        let bytes = self.sliced_bytes();
        let dtype = self.dtype;
        let validity = self.validity;
        let offsets = self.offsets;

        let offsets = if first == 0 {
            offsets
        } else {
            let offsets = offsets.to_primitive();
            match_each_integer_ptype!(offsets.ptype(), |P| {
                let offsets = offsets.as_slice::<P>();
                let buffer: Buffer<P> = offsets.iter().map(|index| index - offsets[0]).collect();
                buffer.into_array()
            })
        };

        // SAFETY: we make the first offset start at zero, and slice the bytes accordingly,
        //  so all offsets stay valid.
        unsafe { Self::new_unchecked(offsets, bytes, dtype, validity) }
    }
}

impl From<Vec<&[u8]>> for VarBinArray {
    fn from(value: Vec<&[u8]>) -> Self {
        Self::from_vec(value, DType::Binary(Nullability::NonNullable))
    }
}

impl From<Vec<Vec<u8>>> for VarBinArray {
    fn from(value: Vec<Vec<u8>>) -> Self {
        Self::from_vec(value, DType::Binary(Nullability::NonNullable))
    }
}

impl From<Vec<String>> for VarBinArray {
    fn from(value: Vec<String>) -> Self {
        Self::from_vec(value, DType::Utf8(Nullability::NonNullable))
    }
}

impl From<Vec<&str>> for VarBinArray {
    fn from(value: Vec<&str>) -> Self {
        Self::from_vec(value, DType::Utf8(Nullability::NonNullable))
    }
}

impl From<Vec<Option<&[u8]>>> for VarBinArray {
    fn from(value: Vec<Option<&[u8]>>) -> Self {
        Self::from_iter(value, DType::Binary(Nullability::Nullable))
    }
}

impl From<Vec<Option<Vec<u8>>>> for VarBinArray {
    fn from(value: Vec<Option<Vec<u8>>>) -> Self {
        Self::from_iter(value, DType::Binary(Nullability::Nullable))
    }
}

impl From<Vec<Option<String>>> for VarBinArray {
    fn from(value: Vec<Option<String>>) -> Self {
        Self::from_iter(value, DType::Utf8(Nullability::Nullable))
    }
}

impl From<Vec<Option<&str>>> for VarBinArray {
    fn from(value: Vec<Option<&str>>) -> Self {
        Self::from_iter(value, DType::Utf8(Nullability::Nullable))
    }
}

impl<'a> FromIterator<Option<&'a [u8]>> for VarBinArray {
    fn from_iter<T: IntoIterator<Item = Option<&'a [u8]>>>(iter: T) -> Self {
        Self::from_iter(iter, DType::Binary(Nullability::Nullable))
    }
}

impl FromIterator<Option<Vec<u8>>> for VarBinArray {
    fn from_iter<T: IntoIterator<Item = Option<Vec<u8>>>>(iter: T) -> Self {
        Self::from_iter(iter, DType::Binary(Nullability::Nullable))
    }
}

impl FromIterator<Option<String>> for VarBinArray {
    fn from_iter<T: IntoIterator<Item = Option<String>>>(iter: T) -> Self {
        Self::from_iter(iter, DType::Utf8(Nullability::Nullable))
    }
}

impl<'a> FromIterator<Option<&'a str>> for VarBinArray {
    fn from_iter<T: IntoIterator<Item = Option<&'a str>>>(iter: T) -> Self {
        Self::from_iter(iter, DType::Utf8(Nullability::Nullable))
    }
}