vortex_compute/arrow/
binaryview.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use arrow_array::Array;
7use arrow_array::GenericByteViewArray;
8use vortex_buffer::Buffer;
9use vortex_buffer::ByteBuffer;
10use vortex_error::VortexResult;
11use vortex_vector::binaryview::BinaryType;
12use vortex_vector::binaryview::BinaryView;
13use vortex_vector::binaryview::BinaryViewVector;
14use vortex_vector::binaryview::StringType;
15
16use crate::arrow::IntoArrow;
17use crate::arrow::IntoVector;
18use crate::arrow::nulls_to_mask;
19
20macro_rules! impl_binaryview_to_arrow {
21    ($T:ty, $A:ty) => {
22        impl IntoArrow for BinaryViewVector<$T> {
23            type Output = GenericByteViewArray<$A>;
24
25            fn into_arrow(self) -> VortexResult<Self::Output> {
26                let (views, buffers, validity) = self.into_parts();
27
28                let views = Buffer::<u128>::from_byte_buffer(views.into_byte_buffer())
29                    .into_arrow_scalar_buffer();
30                let buffers: Vec<_> = buffers
31                    .iter()
32                    .cloned()
33                    .map(|b| b.into_arrow_buffer())
34                    .collect();
35
36                // SAFETY: our own guarantees are the same as Arrow's guarantees for BinaryViewArray
37                Ok(unsafe {
38                    GenericByteViewArray::<$A>::new_unchecked(views, buffers, validity.into())
39                })
40            }
41        }
42    };
43}
44
45impl_binaryview_to_arrow!(BinaryType, arrow_array::types::BinaryViewType);
46impl_binaryview_to_arrow!(StringType, arrow_array::types::StringViewType);
47
48macro_rules! impl_binaryview_from_arrow {
49    ($T:ty, $A:ty) => {
50        impl IntoVector for &GenericByteViewArray<$A> {
51            type Output = BinaryViewVector<$T>;
52
53            fn into_vector(self) -> VortexResult<Self::Output> {
54                // Convert views from Arrow's u128 representation to BinaryView
55                let arrow_views = self.views();
56                let views = Buffer::<BinaryView>::from_byte_buffer(
57                    Buffer::<u128>::from_arrow_scalar_buffer(arrow_views.clone())
58                        .into_byte_buffer(),
59                );
60
61                // Convert buffers
62                let buffers: Box<[ByteBuffer]> = self
63                    .data_buffers()
64                    .iter()
65                    .map(|b| {
66                        ByteBuffer::from_arrow_buffer(
67                            b.clone(),
68                            vortex_buffer::Alignment::of::<u8>(),
69                        )
70                    })
71                    .collect();
72
73                let validity = nulls_to_mask(self.nulls(), self.len());
74
75                // SAFETY: Arrow's GenericByteViewArray maintains the same invariants as our BinaryViewVector
76                Ok(unsafe { BinaryViewVector::new_unchecked(views, Arc::new(buffers), validity) })
77            }
78        }
79    };
80}
81
82impl_binaryview_from_arrow!(BinaryType, arrow_array::types::BinaryViewType);
83impl_binaryview_from_arrow!(StringType, arrow_array::types::StringViewType);