Skip to main content

vortex_array/arrays/varbin/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4mod array;
5pub use array::VarBinArrayExt;
6pub use array::VarBinArraySlotsExt;
7pub use array::VarBinData;
8pub use array::VarBinDataParts;
9pub use array::VarBinSlots;
10pub use array::VarBinSlotsView;
11pub(crate) use array::offsets_tile_utf8;
12pub use vtable::VarBinArray;
13
14pub(crate) mod compute;
15pub use compute::take_varbin;
16
17mod vtable;
18pub use vtable::VarBin;
19
20pub(crate) fn initialize(session: &vortex_session::VortexSession) {
21    vtable::initialize(session);
22}
23
24pub mod builder;
25
26use vortex_buffer::BufferString;
27use vortex_buffer::ByteBuffer;
28use vortex_error::VortexExpect;
29use vortex_error::vortex_err;
30
31use crate::dtype::DType;
32use crate::scalar::Scalar;
33use crate::scalar::ScalarValue;
34
35pub fn varbin_scalar(value: ByteBuffer, dtype: &DType) -> Scalar {
36    if matches!(dtype, DType::Utf8(_)) {
37        Scalar::try_utf8(value, dtype.nullability())
38            .map_err(|err| vortex_err!("Failed to create scalar from utf8 buffer: {}", err))
39            .vortex_expect("UTF-8 scalar creation should succeed")
40    } else {
41        Scalar::binary(value, dtype.nullability())
42    }
43}
44
45/// Creates a non-null [`Scalar`] from `value` without checking UTF-8.
46///
47/// # Safety
48///
49/// `dtype` must be [`DType::Utf8`] or [`DType::Binary`]. If it is [`DType::Utf8`], `value` must be
50/// valid UTF-8.
51pub(crate) unsafe fn varbin_scalar_unchecked(value: ByteBuffer, dtype: &DType) -> Scalar {
52    let value = match dtype {
53        // SAFETY: The caller guarantees that `value` is valid UTF-8.
54        DType::Utf8(_) => ScalarValue::Utf8(unsafe { BufferString::new_unchecked(value) }),
55        _ => ScalarValue::Binary(value),
56    };
57
58    // SAFETY: The value variant matches the dtype.
59    unsafe { Scalar::new_unchecked(dtype.clone(), Some(value)) }
60}
61
62#[cfg(test)]
63mod tests;