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 offset_buffer = &mut mutable.buffer1;
32 let sizes_buffer = &mut mutable.buffer2;
33
34 for &offset in &offsets[start..start + len] {
35 offset_buffer.push(offset);
36 }
37
38 // sizes
39 for &size in &sizes[start..start + len] {
40 sizes_buffer.push(size);
41 }
42
43 // the beauty of views is that we don't need to copy child_data, we just splat
44 // the offsets and sizes.
45 },
46 )
47}
48
49pub(super) fn extend_nulls<T: ArrowNativeType>(mutable: &mut _MutableArrayData, len: usize) {
50 let offset_buffer = &mut mutable.buffer1;
51 let sizes_buffer = &mut mutable.buffer2;
52
53 // We push 0 as a placeholder for NULL values in both the offsets and sizes
54 (0..len).for_each(|_| offset_buffer.push(T::default()));
55 (0..len).for_each(|_| sizes_buffer.push(T::default()));
56}