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 num_integer::Integer;
22use num_traits::CheckedAdd;
23
24pub(super) fn build_extend<T: ArrowNativeType + Integer + CheckedAdd>(
25    array: &ArrayData,
26) -> crate::transform::Extend<'_> {
27    let offsets = array.buffer::<T>(0);
28    let sizes = array.buffer::<T>(1);
29    Box::new(
30        move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
31            let mut new_offset = T::usize_as(mutable.child_data[0].len());
32
33            for i in start..start + len {
34                mutable.buffer1.push(new_offset);
35                mutable.buffer2.push(sizes[i]);
36                new_offset = new_offset.checked_add(&sizes[i]).expect("offset overflow");
37
38                let size = sizes[i].as_usize();
39                if size > 0 {
40                    let child_start = offsets[i].as_usize();
41                    mutable.child_data[0].extend(index, child_start, child_start + size);
42                }
43            }
44        },
45    )
46}
47
48pub(super) fn extend_nulls<T: ArrowNativeType>(mutable: &mut _MutableArrayData, len: usize) {
49    let offset_buffer = &mut mutable.buffer1;
50    let sizes_buffer = &mut mutable.buffer2;
51
52    // We push 0 as a placeholder for NULL values in both the offsets and sizes
53    (0..len).for_each(|_| offset_buffer.push(T::default()));
54    (0..len).for_each(|_| sizes_buffer.push(T::default()));
55}