Skip to main content

arrow_data/transform/
list_view.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::ArrayData;
19use crate::transform::_MutableArrayData;
20use arrow_buffer::ArrowNativeType;
21use arrow_schema::ArrowError;
22use num_integer::Integer;
23use num_traits::CheckedAdd;
24
25pub(super) fn build_extend<T: ArrowNativeType + Integer + CheckedAdd>(
26    array: &ArrayData,
27) -> crate::transform::Extend<'_> {
28    let offsets = array.buffer::<T>(0);
29    let sizes = array.buffer::<T>(1);
30    Box::new(
31        move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
32            let mut new_offset = T::usize_as(mutable.child_data[0].len());
33
34            for i in start..start + len {
35                mutable.buffer1.push(new_offset);
36                mutable.buffer2.push(sizes[i]);
37                new_offset = new_offset.checked_add(&sizes[i]).ok_or_else(|| {
38                    ArrowError::InvalidArgumentError(
39                        "offset overflow: data exceeds the capacity of the offset type. \
40                         Try splitting into smaller batches or using a larger type \
41                         (e.g. LargeListView instead of ListView)"
42                            .to_string(),
43                    )
44                })?;
45
46                let size = sizes[i].as_usize();
47                if size > 0 {
48                    let child_start = offsets[i].as_usize();
49                    mutable.child_data[0].try_extend(index, child_start, child_start + size)?;
50                }
51            }
52            Ok(())
53        },
54    )
55}
56
57pub(super) fn extend_nulls<T: ArrowNativeType>(
58    mutable: &mut _MutableArrayData,
59    len: usize,
60) -> Result<(), ArrowError> {
61    let offset_buffer = &mut mutable.buffer1;
62    let sizes_buffer = &mut mutable.buffer2;
63
64    // We push 0 as a placeholder for NULL values in both the offsets and sizes
65    (0..len).for_each(|_| offset_buffer.push(T::default()));
66    (0..len).for_each(|_| sizes_buffer.push(T::default()));
67    Ok(())
68}