arrow_array/array/byte_view_array.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::array::print_long_array;
19use crate::builder::{ArrayBuilder, GenericByteViewBuilder};
20use crate::iterator::ArrayIter;
21use crate::types::bytes::ByteArrayNativeType;
22use crate::types::{BinaryViewType, ByteViewType, StringViewType};
23use crate::{Array, ArrayAccessor, ArrayRef, GenericByteArray, OffsetSizeTrait, Scalar};
24use arrow_buffer::{ArrowNativeType, Buffer, NullBuffer, ScalarBuffer};
25use arrow_data::{ArrayData, ArrayDataBuilder, ByteView};
26use arrow_schema::{ArrowError, DataType};
27use core::str;
28use num::ToPrimitive;
29use std::any::Any;
30use std::fmt::Debug;
31use std::marker::PhantomData;
32use std::sync::Arc;
33
34use super::ByteArrayType;
35
36/// [Variable-size Binary View Layout]: An array of variable length bytes views.
37///
38/// This array type is used to store variable length byte data (e.g. Strings, Binary)
39/// and has efficient operations such as `take`, `filter`, and comparison.
40///
41/// [Variable-size Binary View Layout]: https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout
42///
43/// This is different from [`GenericByteArray`], which also stores variable
44/// length byte data, as it represents strings with an offset and length. `take`
45/// and `filter` like operations are implemented by manipulating the "views"
46/// (`u128`) without modifying the bytes. Each view also stores an inlined
47/// prefix which speed up comparisons.
48///
49/// # See Also
50///
51/// * [`StringViewArray`] for storing utf8 encoded string data
52/// * [`BinaryViewArray`] for storing bytes
53/// * [`ByteView`] to interpret `u128`s layout of the views.
54///
55/// [`ByteView`]: arrow_data::ByteView
56///
57/// # Layout: "views" and buffers
58///
59/// A `GenericByteViewArray` stores variable length byte strings. An array of
60/// `N` elements is stored as `N` fixed length "views" and a variable number
61/// of variable length "buffers".
62///
63/// Each view is a `u128` value whose layout is different depending on the
64/// length of the string stored at that location:
65///
66/// ```text
67/// ┌──────┬────────────────────────┐
68/// │length│ string value │
69/// Strings (len <= 12) │ │ (padded with 0) │
70/// └──────┴────────────────────────┘
71/// 0 31 127
72///
73/// ┌───────┬───────┬───────┬───────┐
74/// │length │prefix │ buf │offset │
75/// Strings (len > 12) │ │ │ index │ │
76/// └───────┴───────┴───────┴───────┘
77/// 0 31 63 95 127
78/// ```
79///
80/// * Strings with length <= 12 are stored directly in the view. See
81/// [`Self::inline_value`] to access the inlined prefix from a short view.
82///
83/// * Strings with length > 12: The first four bytes are stored inline in the
84/// view and the entire string is stored in one of the buffers. See [`ByteView`]
85/// to access the fields of the these views.
86///
87/// As with other arrays, the optimized kernels in [`arrow_compute`] are likely
88/// the easiest and fastest way to work with this data. However, it is possible
89/// to access the views and buffers directly for more control.
90///
91/// For example
92///
93/// ```rust
94/// # use arrow_array::StringViewArray;
95/// # use arrow_array::Array;
96/// use arrow_data::ByteView;
97/// let array = StringViewArray::from(vec![
98/// "hello",
99/// "this string is longer than 12 bytes",
100/// "this string is also longer than 12 bytes"
101/// ]);
102///
103/// // ** Examine the first view (short string) **
104/// assert!(array.is_valid(0)); // Check for nulls
105/// let short_view: u128 = array.views()[0]; // "hello"
106/// // get length of the string
107/// let len = short_view as u32;
108/// assert_eq!(len, 5); // strings less than 12 bytes are stored in the view
109/// // SAFETY: `view` is a valid view
110/// let value = unsafe {
111/// StringViewArray::inline_value(&short_view, len as usize)
112/// };
113/// assert_eq!(value, b"hello");
114///
115/// // ** Examine the third view (long string) **
116/// assert!(array.is_valid(12)); // Check for nulls
117/// let long_view: u128 = array.views()[2]; // "this string is also longer than 12 bytes"
118/// let len = long_view as u32;
119/// assert_eq!(len, 40); // strings longer than 12 bytes are stored in the buffer
120/// let view = ByteView::from(long_view); // use ByteView to access the fields
121/// assert_eq!(view.length, 40);
122/// assert_eq!(view.buffer_index, 0);
123/// assert_eq!(view.offset, 35); // data starts after the first long string
124/// // Views for long strings store a 4 byte prefix
125/// let prefix = view.prefix.to_le_bytes();
126/// assert_eq!(&prefix, b"this");
127/// let value = array.value(2); // get the string value (see `value` implementation for how to access the bytes directly)
128/// assert_eq!(value, "this string is also longer than 12 bytes");
129/// ```
130///
131/// [`arrow_compute`]: https://docs.rs/arrow/latest/arrow/compute/index.html
132///
133/// Unlike [`GenericByteArray`], there are no constraints on the offsets other
134/// than they must point into a valid buffer. However, they can be out of order,
135/// non continuous and overlapping.
136///
137/// For example, in the following diagram, the strings "FishWasInTownToday" and
138/// "CrumpleFacedFish" are both longer than 12 bytes and thus are stored in a
139/// separate buffer while the string "LavaMonster" is stored inlined in the
140/// view. In this case, the same bytes for "Fish" are used to store both strings.
141///
142/// [`ByteView`]: arrow_data::ByteView
143///
144/// ```text
145/// ┌───┐
146/// ┌──────┬──────┬──────┬──────┐ offset │...│
147/// "FishWasInTownTodayYay" │ 21 │ Fish │ 0 │ 115 │─ ─ 103 │Mr.│
148/// └──────┴──────┴──────┴──────┘ │ ┌ ─ ─ ─ ─ ▶ │Cru│
149/// ┌──────┬──────┬──────┬──────┐ │mpl│
150/// "CrumpleFacedFish" │ 16 │ Crum │ 0 │ 103 │─ ─│─ ─ ─ ┘ │eFa│
151/// └──────┴──────┴──────┴──────┘ │ced│
152/// ┌──────┬────────────────────┐ └ ─ ─ ─ ─ ─ ─ ─ ─ ▶│Fis│
153/// "LavaMonster" │ 11 │ LavaMonster\0 │ │hWa│
154/// └──────┴────────────────────┘ offset │sIn│
155/// 115 │Tow│
156/// │nTo│
157/// │day│
158/// u128 "views" │Yay│
159/// buffer 0 │...│
160/// └───┘
161/// ```
162pub struct GenericByteViewArray<T: ByteViewType + ?Sized> {
163 data_type: DataType,
164 views: ScalarBuffer<u128>,
165 buffers: Vec<Buffer>,
166 phantom: PhantomData<T>,
167 nulls: Option<NullBuffer>,
168}
169
170impl<T: ByteViewType + ?Sized> Clone for GenericByteViewArray<T> {
171 fn clone(&self) -> Self {
172 Self {
173 data_type: T::DATA_TYPE,
174 views: self.views.clone(),
175 buffers: self.buffers.clone(),
176 nulls: self.nulls.clone(),
177 phantom: Default::default(),
178 }
179 }
180}
181
182impl<T: ByteViewType + ?Sized> GenericByteViewArray<T> {
183 /// Create a new [`GenericByteViewArray`] from the provided parts, panicking on failure
184 ///
185 /// # Panics
186 ///
187 /// Panics if [`GenericByteViewArray::try_new`] returns an error
188 pub fn new(views: ScalarBuffer<u128>, buffers: Vec<Buffer>, nulls: Option<NullBuffer>) -> Self {
189 Self::try_new(views, buffers, nulls).unwrap()
190 }
191
192 /// Create a new [`GenericByteViewArray`] from the provided parts, returning an error on failure
193 ///
194 /// # Errors
195 ///
196 /// * `views.len() != nulls.len()`
197 /// * [ByteViewType::validate] fails
198 pub fn try_new(
199 views: ScalarBuffer<u128>,
200 buffers: Vec<Buffer>,
201 nulls: Option<NullBuffer>,
202 ) -> Result<Self, ArrowError> {
203 T::validate(&views, &buffers)?;
204
205 if let Some(n) = nulls.as_ref() {
206 if n.len() != views.len() {
207 return Err(ArrowError::InvalidArgumentError(format!(
208 "Incorrect length of null buffer for {}ViewArray, expected {} got {}",
209 T::PREFIX,
210 views.len(),
211 n.len(),
212 )));
213 }
214 }
215
216 Ok(Self {
217 data_type: T::DATA_TYPE,
218 views,
219 buffers,
220 nulls,
221 phantom: Default::default(),
222 })
223 }
224
225 /// Create a new [`GenericByteViewArray`] from the provided parts, without validation
226 ///
227 /// # Safety
228 ///
229 /// Safe if [`Self::try_new`] would not error
230 pub unsafe fn new_unchecked(
231 views: ScalarBuffer<u128>,
232 buffers: Vec<Buffer>,
233 nulls: Option<NullBuffer>,
234 ) -> Self {
235 Self {
236 data_type: T::DATA_TYPE,
237 phantom: Default::default(),
238 views,
239 buffers,
240 nulls,
241 }
242 }
243
244 /// Create a new [`GenericByteViewArray`] of length `len` where all values are null
245 pub fn new_null(len: usize) -> Self {
246 Self {
247 data_type: T::DATA_TYPE,
248 views: vec![0; len].into(),
249 buffers: vec![],
250 nulls: Some(NullBuffer::new_null(len)),
251 phantom: Default::default(),
252 }
253 }
254
255 /// Create a new [`Scalar`] from `value`
256 pub fn new_scalar(value: impl AsRef<T::Native>) -> Scalar<Self> {
257 Scalar::new(Self::from_iter_values(std::iter::once(value)))
258 }
259
260 /// Creates a [`GenericByteViewArray`] based on an iterator of values without nulls
261 pub fn from_iter_values<Ptr, I>(iter: I) -> Self
262 where
263 Ptr: AsRef<T::Native>,
264 I: IntoIterator<Item = Ptr>,
265 {
266 let iter = iter.into_iter();
267 let mut builder = GenericByteViewBuilder::<T>::with_capacity(iter.size_hint().0);
268 for v in iter {
269 builder.append_value(v);
270 }
271 builder.finish()
272 }
273
274 /// Deconstruct this array into its constituent parts
275 pub fn into_parts(self) -> (ScalarBuffer<u128>, Vec<Buffer>, Option<NullBuffer>) {
276 (self.views, self.buffers, self.nulls)
277 }
278
279 /// Returns the views buffer
280 #[inline]
281 pub fn views(&self) -> &ScalarBuffer<u128> {
282 &self.views
283 }
284
285 /// Returns the buffers storing string data
286 #[inline]
287 pub fn data_buffers(&self) -> &[Buffer] {
288 &self.buffers
289 }
290
291 /// Returns the element at index `i`
292 /// # Panics
293 /// Panics if index `i` is out of bounds.
294 pub fn value(&self, i: usize) -> &T::Native {
295 assert!(
296 i < self.len(),
297 "Trying to access an element at index {} from a {}ViewArray of length {}",
298 i,
299 T::PREFIX,
300 self.len()
301 );
302
303 unsafe { self.value_unchecked(i) }
304 }
305
306 /// Returns the element at index `i` without bounds checking
307 ///
308 /// # Safety
309 ///
310 /// Caller is responsible for ensuring that the index is within the bounds
311 /// of the array
312 pub unsafe fn value_unchecked(&self, idx: usize) -> &T::Native {
313 let v = self.views.get_unchecked(idx);
314 let len = *v as u32;
315 let b = if len <= 12 {
316 Self::inline_value(v, len as usize)
317 } else {
318 let view = ByteView::from(*v);
319 let data = self.buffers.get_unchecked(view.buffer_index as usize);
320 let offset = view.offset as usize;
321 data.get_unchecked(offset..offset + len as usize)
322 };
323 T::Native::from_bytes_unchecked(b)
324 }
325
326 /// Returns the first `len` bytes the inline value of the view.
327 ///
328 /// # Safety
329 /// - The `view` must be a valid element from `Self::views()` that adheres to the view layout.
330 /// - The `len` must be the length of the inlined value. It should never be larger than 12.
331 #[inline(always)]
332 pub unsafe fn inline_value(view: &u128, len: usize) -> &[u8] {
333 debug_assert!(len <= 12);
334 std::slice::from_raw_parts((view as *const u128 as *const u8).wrapping_add(4), len)
335 }
336
337 /// Constructs a new iterator for iterating over the values of this array
338 pub fn iter(&self) -> ArrayIter<&Self> {
339 ArrayIter::new(self)
340 }
341
342 /// Returns an iterator over the bytes of this array, including null values
343 pub fn bytes_iter(&self) -> impl Iterator<Item = &[u8]> {
344 self.views.iter().map(move |v| {
345 let len = *v as u32;
346 if len <= 12 {
347 unsafe { Self::inline_value(v, len as usize) }
348 } else {
349 let view = ByteView::from(*v);
350 let data = &self.buffers[view.buffer_index as usize];
351 let offset = view.offset as usize;
352 unsafe { data.get_unchecked(offset..offset + len as usize) }
353 }
354 })
355 }
356
357 /// Returns an iterator over the first `prefix_len` bytes of each array
358 /// element, including null values.
359 ///
360 /// If `prefix_len` is larger than the element's length, the iterator will
361 /// return an empty slice (`&[]`).
362 pub fn prefix_bytes_iter(&self, prefix_len: usize) -> impl Iterator<Item = &[u8]> {
363 self.views().into_iter().map(move |v| {
364 let len = (*v as u32) as usize;
365
366 if len < prefix_len {
367 return &[] as &[u8];
368 }
369
370 if prefix_len <= 4 || len <= 12 {
371 unsafe { StringViewArray::inline_value(v, prefix_len) }
372 } else {
373 let view = ByteView::from(*v);
374 let data = unsafe {
375 self.data_buffers()
376 .get_unchecked(view.buffer_index as usize)
377 };
378 let offset = view.offset as usize;
379 unsafe { data.get_unchecked(offset..offset + prefix_len) }
380 }
381 })
382 }
383
384 /// Returns an iterator over the last `suffix_len` bytes of each array
385 /// element, including null values.
386 ///
387 /// Note that for [`StringViewArray`] the last bytes may start in the middle
388 /// of a UTF-8 codepoint, and thus may not be a valid `&str`.
389 ///
390 /// If `suffix_len` is larger than the element's length, the iterator will
391 /// return an empty slice (`&[]`).
392 pub fn suffix_bytes_iter(&self, suffix_len: usize) -> impl Iterator<Item = &[u8]> {
393 self.views().into_iter().map(move |v| {
394 let len = (*v as u32) as usize;
395
396 if len < suffix_len {
397 return &[] as &[u8];
398 }
399
400 if len <= 12 {
401 unsafe { &StringViewArray::inline_value(v, len)[len - suffix_len..] }
402 } else {
403 let view = ByteView::from(*v);
404 let data = unsafe {
405 self.data_buffers()
406 .get_unchecked(view.buffer_index as usize)
407 };
408 let offset = view.offset as usize;
409 unsafe { data.get_unchecked(offset + len - suffix_len..offset + len) }
410 }
411 })
412 }
413
414 /// Returns a zero-copy slice of this array with the indicated offset and length.
415 pub fn slice(&self, offset: usize, length: usize) -> Self {
416 Self {
417 data_type: T::DATA_TYPE,
418 views: self.views.slice(offset, length),
419 buffers: self.buffers.clone(),
420 nulls: self.nulls.as_ref().map(|n| n.slice(offset, length)),
421 phantom: Default::default(),
422 }
423 }
424
425 /// Returns a "compacted" version of this array
426 ///
427 /// The original array will *not* be modified
428 ///
429 /// # Garbage Collection
430 ///
431 /// Before GC:
432 /// ```text
433 /// ┌──────┐
434 /// │......│
435 /// │......│
436 /// ┌────────────────────┐ ┌ ─ ─ ─ ▶ │Data1 │ Large buffer
437 /// │ View 1 │─ ─ ─ ─ │......│ with data that
438 /// ├────────────────────┤ │......│ is not referred
439 /// │ View 2 │─ ─ ─ ─ ─ ─ ─ ─▶ │Data2 │ to by View 1 or
440 /// └────────────────────┘ │......│ View 2
441 /// │......│
442 /// 2 views, refer to │......│
443 /// small portions of a └──────┘
444 /// large buffer
445 /// ```
446 ///
447 /// After GC:
448 ///
449 /// ```text
450 /// ┌────────────────────┐ ┌─────┐ After gc, only
451 /// │ View 1 │─ ─ ─ ─ ─ ─ ─ ─▶ │Data1│ data that is
452 /// ├────────────────────┤ ┌ ─ ─ ─ ▶ │Data2│ pointed to by
453 /// │ View 2 │─ ─ ─ ─ └─────┘ the views is
454 /// └────────────────────┘ left
455 ///
456 ///
457 /// 2 views
458 /// ```
459 /// This method will compact the data buffers by recreating the view array and only include the data
460 /// that is pointed to by the views.
461 ///
462 /// Note that it will copy the array regardless of whether the original array is compact.
463 /// Use with caution as this can be an expensive operation, only use it when you are sure that the view
464 /// array is significantly smaller than when it is originally created, e.g., after filtering or slicing.
465 ///
466 /// Note: this function does not attempt to canonicalize / deduplicate values. For this
467 /// feature see [`GenericByteViewBuilder::with_deduplicate_strings`].
468 pub fn gc(&self) -> Self {
469 let mut builder = GenericByteViewBuilder::<T>::with_capacity(self.len());
470
471 for v in self.iter() {
472 builder.append_option(v);
473 }
474
475 builder.finish()
476 }
477
478 /// Compare two [`GenericByteViewArray`] at index `left_idx` and `right_idx`
479 ///
480 /// Comparing two ByteView types are non-trivial.
481 /// It takes a bit of patience to understand why we don't just compare two &[u8] directly.
482 ///
483 /// ByteView types give us the following two advantages, and we need to be careful not to lose them:
484 /// (1) For string/byte smaller than 12 bytes, the entire data is inlined in the view.
485 /// Meaning that reading one array element requires only one memory access
486 /// (two memory access required for StringArray, one for offset buffer, the other for value buffer).
487 ///
488 /// (2) For string/byte larger than 12 bytes, we can still be faster than (for certain operations) StringArray/ByteArray,
489 /// thanks to the inlined 4 bytes.
490 /// Consider equality check:
491 /// If the first four bytes of the two strings are different, we can return false immediately (with just one memory access).
492 ///
493 /// If we directly compare two &[u8], we materialize the entire string (i.e., make multiple memory accesses), which might be unnecessary.
494 /// - Most of the time (eq, ord), we only need to look at the first 4 bytes to know the answer,
495 /// e.g., if the inlined 4 bytes are different, we can directly return unequal without looking at the full string.
496 ///
497 /// # Order check flow
498 /// (1) if both string are smaller than 12 bytes, we can directly compare the data inlined to the view.
499 /// (2) if any of the string is larger than 12 bytes, we need to compare the full string.
500 /// (2.1) if the inlined 4 bytes are different, we can return the result immediately.
501 /// (2.2) o.w., we need to compare the full string.
502 ///
503 /// # Safety
504 /// The left/right_idx must within range of each array
505 pub unsafe fn compare_unchecked(
506 left: &GenericByteViewArray<T>,
507 left_idx: usize,
508 right: &GenericByteViewArray<T>,
509 right_idx: usize,
510 ) -> std::cmp::Ordering {
511 let l_view = left.views().get_unchecked(left_idx);
512 let l_len = *l_view as u32;
513
514 let r_view = right.views().get_unchecked(right_idx);
515 let r_len = *r_view as u32;
516
517 if l_len <= 12 && r_len <= 12 {
518 let l_data = unsafe { GenericByteViewArray::<T>::inline_value(l_view, l_len as usize) };
519 let r_data = unsafe { GenericByteViewArray::<T>::inline_value(r_view, r_len as usize) };
520 return l_data.cmp(r_data);
521 }
522
523 // one of the string is larger than 12 bytes,
524 // we then try to compare the inlined data first
525 let l_inlined_data = unsafe { GenericByteViewArray::<T>::inline_value(l_view, 4) };
526 let r_inlined_data = unsafe { GenericByteViewArray::<T>::inline_value(r_view, 4) };
527 if r_inlined_data != l_inlined_data {
528 return l_inlined_data.cmp(r_inlined_data);
529 }
530
531 // unfortunately, we need to compare the full data
532 let l_full_data: &[u8] = unsafe { left.value_unchecked(left_idx).as_ref() };
533 let r_full_data: &[u8] = unsafe { right.value_unchecked(right_idx).as_ref() };
534
535 l_full_data.cmp(r_full_data)
536 }
537}
538
539impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T> {
540 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
541 write!(f, "{}ViewArray\n[\n", T::PREFIX)?;
542 print_long_array(self, f, |array, index, f| {
543 std::fmt::Debug::fmt(&array.value(index), f)
544 })?;
545 write!(f, "]")
546 }
547}
548
549impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
550 fn as_any(&self) -> &dyn Any {
551 self
552 }
553
554 fn to_data(&self) -> ArrayData {
555 self.clone().into()
556 }
557
558 fn into_data(self) -> ArrayData {
559 self.into()
560 }
561
562 fn data_type(&self) -> &DataType {
563 &self.data_type
564 }
565
566 fn slice(&self, offset: usize, length: usize) -> ArrayRef {
567 Arc::new(self.slice(offset, length))
568 }
569
570 fn len(&self) -> usize {
571 self.views.len()
572 }
573
574 fn is_empty(&self) -> bool {
575 self.views.is_empty()
576 }
577
578 fn shrink_to_fit(&mut self) {
579 self.views.shrink_to_fit();
580 self.buffers.iter_mut().for_each(|b| b.shrink_to_fit());
581 self.buffers.shrink_to_fit();
582 if let Some(nulls) = &mut self.nulls {
583 nulls.shrink_to_fit();
584 }
585 }
586
587 fn offset(&self) -> usize {
588 0
589 }
590
591 fn nulls(&self) -> Option<&NullBuffer> {
592 self.nulls.as_ref()
593 }
594
595 fn logical_null_count(&self) -> usize {
596 // More efficient that the default implementation
597 self.null_count()
598 }
599
600 fn get_buffer_memory_size(&self) -> usize {
601 let mut sum = self.buffers.iter().map(|b| b.capacity()).sum::<usize>();
602 sum += self.views.inner().capacity();
603 if let Some(x) = &self.nulls {
604 sum += x.buffer().capacity()
605 }
606 sum
607 }
608
609 fn get_array_memory_size(&self) -> usize {
610 std::mem::size_of::<Self>() + self.get_buffer_memory_size()
611 }
612}
613
614impl<'a, T: ByteViewType + ?Sized> ArrayAccessor for &'a GenericByteViewArray<T> {
615 type Item = &'a T::Native;
616
617 fn value(&self, index: usize) -> Self::Item {
618 GenericByteViewArray::value(self, index)
619 }
620
621 unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
622 GenericByteViewArray::value_unchecked(self, index)
623 }
624}
625
626impl<'a, T: ByteViewType + ?Sized> IntoIterator for &'a GenericByteViewArray<T> {
627 type Item = Option<&'a T::Native>;
628 type IntoIter = ArrayIter<Self>;
629
630 fn into_iter(self) -> Self::IntoIter {
631 ArrayIter::new(self)
632 }
633}
634
635impl<T: ByteViewType + ?Sized> From<ArrayData> for GenericByteViewArray<T> {
636 fn from(value: ArrayData) -> Self {
637 let views = value.buffers()[0].clone();
638 let views = ScalarBuffer::new(views, value.offset(), value.len());
639 let buffers = value.buffers()[1..].to_vec();
640 Self {
641 data_type: T::DATA_TYPE,
642 views,
643 buffers,
644 nulls: value.nulls().cloned(),
645 phantom: Default::default(),
646 }
647 }
648}
649
650/// Efficiently convert a [`GenericByteArray`] to a [`GenericByteViewArray`]
651///
652/// For example this method can convert a [`StringArray`] to a
653/// [`StringViewArray`].
654///
655/// If the offsets are all less than u32::MAX, the new [`GenericByteViewArray`]
656/// is built without copying the underlying string data (views are created
657/// directly into the existing buffer)
658///
659/// [`StringArray`]: crate::StringArray
660impl<FROM, V> From<&GenericByteArray<FROM>> for GenericByteViewArray<V>
661where
662 FROM: ByteArrayType,
663 FROM::Offset: OffsetSizeTrait + ToPrimitive,
664 V: ByteViewType<Native = FROM::Native>,
665{
666 fn from(byte_array: &GenericByteArray<FROM>) -> Self {
667 let offsets = byte_array.offsets();
668
669 let can_reuse_buffer = match offsets.last() {
670 Some(offset) => offset.as_usize() < u32::MAX as usize,
671 None => true,
672 };
673
674 if can_reuse_buffer {
675 // build views directly pointing to the existing buffer
676 let len = byte_array.len();
677 let mut views_builder = GenericByteViewBuilder::<V>::with_capacity(len);
678 let str_values_buf = byte_array.values().clone();
679 let block = views_builder.append_block(str_values_buf);
680 for (i, w) in offsets.windows(2).enumerate() {
681 let offset = w[0].as_usize();
682 let end = w[1].as_usize();
683 let length = end - offset;
684
685 if byte_array.is_null(i) {
686 views_builder.append_null();
687 } else {
688 // Safety: the input was a valid array so it valid UTF8 (if string). And
689 // all offsets were valid
690 unsafe {
691 views_builder.append_view_unchecked(block, offset as u32, length as u32)
692 }
693 }
694 }
695 assert_eq!(views_builder.len(), len);
696 views_builder.finish()
697 } else {
698 // Otherwise, create a new buffer for large strings
699 // TODO: the original buffer could still be used
700 // by making multiple slices of u32::MAX length
701 GenericByteViewArray::<V>::from_iter(byte_array.iter())
702 }
703 }
704}
705
706impl<T: ByteViewType + ?Sized> From<GenericByteViewArray<T>> for ArrayData {
707 fn from(mut array: GenericByteViewArray<T>) -> Self {
708 let len = array.len();
709 array.buffers.insert(0, array.views.into_inner());
710 let builder = ArrayDataBuilder::new(T::DATA_TYPE)
711 .len(len)
712 .buffers(array.buffers)
713 .nulls(array.nulls);
714
715 unsafe { builder.build_unchecked() }
716 }
717}
718
719impl<'a, Ptr, T> FromIterator<&'a Option<Ptr>> for GenericByteViewArray<T>
720where
721 Ptr: AsRef<T::Native> + 'a,
722 T: ByteViewType + ?Sized,
723{
724 fn from_iter<I: IntoIterator<Item = &'a Option<Ptr>>>(iter: I) -> Self {
725 iter.into_iter()
726 .map(|o| o.as_ref().map(|p| p.as_ref()))
727 .collect()
728 }
729}
730
731impl<Ptr, T: ByteViewType + ?Sized> FromIterator<Option<Ptr>> for GenericByteViewArray<T>
732where
733 Ptr: AsRef<T::Native>,
734{
735 fn from_iter<I: IntoIterator<Item = Option<Ptr>>>(iter: I) -> Self {
736 let iter = iter.into_iter();
737 let mut builder = GenericByteViewBuilder::<T>::with_capacity(iter.size_hint().0);
738 builder.extend(iter);
739 builder.finish()
740 }
741}
742
743/// A [`GenericByteViewArray`] of `[u8]`
744///
745/// See [`GenericByteViewArray`] for format and layout details.
746///
747/// # Example
748/// ```
749/// use arrow_array::BinaryViewArray;
750/// let array = BinaryViewArray::from_iter_values(vec![b"hello" as &[u8], b"world", b"lulu", b"large payload over 12 bytes"]);
751/// assert_eq!(array.value(0), b"hello");
752/// assert_eq!(array.value(3), b"large payload over 12 bytes");
753/// ```
754pub type BinaryViewArray = GenericByteViewArray<BinaryViewType>;
755
756impl BinaryViewArray {
757 /// Convert the [`BinaryViewArray`] to [`StringViewArray`]
758 /// If items not utf8 data, validate will fail and error returned.
759 pub fn to_string_view(self) -> Result<StringViewArray, ArrowError> {
760 StringViewType::validate(self.views(), self.data_buffers())?;
761 unsafe { Ok(self.to_string_view_unchecked()) }
762 }
763
764 /// Convert the [`BinaryViewArray`] to [`StringViewArray`]
765 /// # Safety
766 /// Caller is responsible for ensuring that items in array are utf8 data.
767 pub unsafe fn to_string_view_unchecked(self) -> StringViewArray {
768 StringViewArray::new_unchecked(self.views, self.buffers, self.nulls)
769 }
770}
771
772impl From<Vec<&[u8]>> for BinaryViewArray {
773 fn from(v: Vec<&[u8]>) -> Self {
774 Self::from_iter_values(v)
775 }
776}
777
778impl From<Vec<Option<&[u8]>>> for BinaryViewArray {
779 fn from(v: Vec<Option<&[u8]>>) -> Self {
780 v.into_iter().collect()
781 }
782}
783
784/// A [`GenericByteViewArray`] that stores utf8 data
785///
786/// See [`GenericByteViewArray`] for format and layout details.
787///
788/// # Example
789/// ```
790/// use arrow_array::StringViewArray;
791/// let array = StringViewArray::from_iter_values(vec!["hello", "world", "lulu", "large payload over 12 bytes"]);
792/// assert_eq!(array.value(0), "hello");
793/// assert_eq!(array.value(3), "large payload over 12 bytes");
794/// ```
795pub type StringViewArray = GenericByteViewArray<StringViewType>;
796
797impl StringViewArray {
798 /// Convert the [`StringViewArray`] to [`BinaryViewArray`]
799 pub fn to_binary_view(self) -> BinaryViewArray {
800 unsafe { BinaryViewArray::new_unchecked(self.views, self.buffers, self.nulls) }
801 }
802
803 /// Returns true if all data within this array is ASCII
804 pub fn is_ascii(&self) -> bool {
805 // Alternative (but incorrect): directly check the underlying buffers
806 // (1) Our string view might be sparse, i.e., a subset of the buffers,
807 // so even if the buffer is not ascii, we can still be ascii.
808 // (2) It is quite difficult to know the range of each buffer (unlike StringArray)
809 // This means that this operation is quite expensive, shall we cache the result?
810 // i.e. track `is_ascii` in the builder.
811 self.iter().all(|v| match v {
812 Some(v) => v.is_ascii(),
813 None => true,
814 })
815 }
816}
817
818impl From<Vec<&str>> for StringViewArray {
819 fn from(v: Vec<&str>) -> Self {
820 Self::from_iter_values(v)
821 }
822}
823
824impl From<Vec<Option<&str>>> for StringViewArray {
825 fn from(v: Vec<Option<&str>>) -> Self {
826 v.into_iter().collect()
827 }
828}
829
830impl From<Vec<String>> for StringViewArray {
831 fn from(v: Vec<String>) -> Self {
832 Self::from_iter_values(v)
833 }
834}
835
836impl From<Vec<Option<String>>> for StringViewArray {
837 fn from(v: Vec<Option<String>>) -> Self {
838 v.into_iter().collect()
839 }
840}
841
842#[cfg(test)]
843mod tests {
844 use crate::builder::{BinaryViewBuilder, StringViewBuilder};
845 use crate::{Array, BinaryViewArray, StringViewArray};
846 use arrow_buffer::{Buffer, ScalarBuffer};
847 use arrow_data::ByteView;
848
849 #[test]
850 fn try_new_string() {
851 let array = StringViewArray::from_iter_values(vec![
852 "hello",
853 "world",
854 "lulu",
855 "large payload over 12 bytes",
856 ]);
857 assert_eq!(array.value(0), "hello");
858 assert_eq!(array.value(3), "large payload over 12 bytes");
859 }
860
861 #[test]
862 fn try_new_binary() {
863 let array = BinaryViewArray::from_iter_values(vec![
864 b"hello".as_slice(),
865 b"world".as_slice(),
866 b"lulu".as_slice(),
867 b"large payload over 12 bytes".as_slice(),
868 ]);
869 assert_eq!(array.value(0), b"hello");
870 assert_eq!(array.value(3), b"large payload over 12 bytes");
871 }
872
873 #[test]
874 fn try_new_empty_string() {
875 // test empty array
876 let array = {
877 let mut builder = StringViewBuilder::new();
878 builder.finish()
879 };
880 assert!(array.is_empty());
881 }
882
883 #[test]
884 fn try_new_empty_binary() {
885 // test empty array
886 let array = {
887 let mut builder = BinaryViewBuilder::new();
888 builder.finish()
889 };
890 assert!(array.is_empty());
891 }
892
893 #[test]
894 fn test_append_string() {
895 // test builder append
896 let array = {
897 let mut builder = StringViewBuilder::new();
898 builder.append_value("hello");
899 builder.append_null();
900 builder.append_option(Some("large payload over 12 bytes"));
901 builder.finish()
902 };
903 assert_eq!(array.value(0), "hello");
904 assert!(array.is_null(1));
905 assert_eq!(array.value(2), "large payload over 12 bytes");
906 }
907
908 #[test]
909 fn test_append_binary() {
910 // test builder append
911 let array = {
912 let mut builder = BinaryViewBuilder::new();
913 builder.append_value(b"hello");
914 builder.append_null();
915 builder.append_option(Some(b"large payload over 12 bytes"));
916 builder.finish()
917 };
918 assert_eq!(array.value(0), b"hello");
919 assert!(array.is_null(1));
920 assert_eq!(array.value(2), b"large payload over 12 bytes");
921 }
922
923 #[test]
924 fn test_in_progress_recreation() {
925 let array = {
926 // make a builder with small block size.
927 let mut builder = StringViewBuilder::new().with_fixed_block_size(14);
928 builder.append_value("large payload over 12 bytes");
929 builder.append_option(Some("another large payload over 12 bytes that double than the first one, so that we can trigger the in_progress in builder re-created"));
930 builder.finish()
931 };
932 assert_eq!(array.value(0), "large payload over 12 bytes");
933 assert_eq!(array.value(1), "another large payload over 12 bytes that double than the first one, so that we can trigger the in_progress in builder re-created");
934 assert_eq!(2, array.buffers.len());
935 }
936
937 #[test]
938 #[should_panic(expected = "Invalid buffer index at 0: got index 3 but only has 1 buffers")]
939 fn new_with_invalid_view_data() {
940 let v = "large payload over 12 bytes";
941 let view = ByteView::new(13, &v.as_bytes()[0..4])
942 .with_buffer_index(3)
943 .with_offset(1);
944 let views = ScalarBuffer::from(vec![view.into()]);
945 let buffers = vec![Buffer::from_slice_ref(v)];
946 StringViewArray::new(views, buffers, None);
947 }
948
949 #[test]
950 #[should_panic(
951 expected = "Encountered non-UTF-8 data at index 0: invalid utf-8 sequence of 1 bytes from index 0"
952 )]
953 fn new_with_invalid_utf8_data() {
954 let v: Vec<u8> = vec![
955 // invalid UTF8
956 0xf0, 0x80, 0x80, 0x80, // more bytes to make it larger than 12
957 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
958 ];
959 let view = ByteView::new(v.len() as u32, &v[0..4]);
960 let views = ScalarBuffer::from(vec![view.into()]);
961 let buffers = vec![Buffer::from_slice_ref(v)];
962 StringViewArray::new(views, buffers, None);
963 }
964
965 #[test]
966 #[should_panic(expected = "View at index 0 contained non-zero padding for string of length 1")]
967 fn new_with_invalid_zero_padding() {
968 let mut data = [0; 12];
969 data[0] = b'H';
970 data[11] = 1; // no zero padding
971
972 let mut view_buffer = [0; 16];
973 view_buffer[0..4].copy_from_slice(&1u32.to_le_bytes());
974 view_buffer[4..].copy_from_slice(&data);
975
976 let view = ByteView::from(u128::from_le_bytes(view_buffer));
977 let views = ScalarBuffer::from(vec![view.into()]);
978 let buffers = vec![];
979 StringViewArray::new(views, buffers, None);
980 }
981
982 #[test]
983 #[should_panic(expected = "Mismatch between embedded prefix and data")]
984 fn test_mismatch_between_embedded_prefix_and_data() {
985 let input_str_1 = "Hello, Rustaceans!";
986 let input_str_2 = "Hallo, Rustaceans!";
987 let length = input_str_1.len() as u32;
988 assert!(input_str_1.len() > 12);
989
990 let mut view_buffer = [0; 16];
991 view_buffer[0..4].copy_from_slice(&length.to_le_bytes());
992 view_buffer[4..8].copy_from_slice(&input_str_1.as_bytes()[0..4]);
993 view_buffer[8..12].copy_from_slice(&0u32.to_le_bytes());
994 view_buffer[12..].copy_from_slice(&0u32.to_le_bytes());
995 let view = ByteView::from(u128::from_le_bytes(view_buffer));
996 let views = ScalarBuffer::from(vec![view.into()]);
997 let buffers = vec![Buffer::from_slice_ref(input_str_2.as_bytes())];
998
999 StringViewArray::new(views, buffers, None);
1000 }
1001
1002 #[test]
1003 fn test_gc() {
1004 let test_data = [
1005 Some("longer than 12 bytes"),
1006 Some("short"),
1007 Some("t"),
1008 Some("longer than 12 bytes"),
1009 None,
1010 Some("short"),
1011 ];
1012
1013 let array = {
1014 let mut builder = StringViewBuilder::new().with_fixed_block_size(8); // create multiple buffers
1015 test_data.into_iter().for_each(|v| builder.append_option(v));
1016 builder.finish()
1017 };
1018 assert!(array.buffers.len() > 1);
1019
1020 fn check_gc(to_test: &StringViewArray) {
1021 let gc = to_test.gc();
1022 assert_ne!(to_test.data_buffers().len(), gc.data_buffers().len());
1023
1024 to_test.iter().zip(gc.iter()).for_each(|(a, b)| {
1025 assert_eq!(a, b);
1026 });
1027 assert_eq!(to_test.len(), gc.len());
1028 }
1029
1030 check_gc(&array);
1031 check_gc(&array.slice(1, 3));
1032 check_gc(&array.slice(2, 1));
1033 check_gc(&array.slice(2, 2));
1034 check_gc(&array.slice(3, 1));
1035 }
1036
1037 #[test]
1038 fn test_eq() {
1039 let test_data = [
1040 Some("longer than 12 bytes"),
1041 None,
1042 Some("short"),
1043 Some("again, this is longer than 12 bytes"),
1044 ];
1045
1046 let array1 = {
1047 let mut builder = StringViewBuilder::new().with_fixed_block_size(8);
1048 test_data.into_iter().for_each(|v| builder.append_option(v));
1049 builder.finish()
1050 };
1051 let array2 = {
1052 // create a new array with the same data but different layout
1053 let mut builder = StringViewBuilder::new().with_fixed_block_size(100);
1054 test_data.into_iter().for_each(|v| builder.append_option(v));
1055 builder.finish()
1056 };
1057 assert_eq!(array1, array1.clone());
1058 assert_eq!(array2, array2.clone());
1059 assert_eq!(array1, array2);
1060 }
1061}