arrow_data/transform/list.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 super::{
19 _MutableArrayData, Extend,
20 utils::{get_last_offset, try_extend_offsets},
21};
22use crate::ArrayData;
23use arrow_buffer::ArrowNativeType;
24use arrow_schema::ArrowError;
25use num_integer::Integer;
26use num_traits::CheckedAdd;
27
28pub(super) fn build_extend<T: ArrowNativeType + Integer + CheckedAdd>(
29 array: &ArrayData,
30) -> Extend<'_> {
31 let offsets = array.buffer::<T>(0);
32 Box::new(
33 move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
34 let offset_buffer = &mut mutable.buffer1;
35
36 // this is safe due to how offset is built. See details on `get_last_offset`
37 let last_offset: T = unsafe { get_last_offset(offset_buffer) };
38
39 // offsets
40 try_extend_offsets::<T>(offset_buffer, last_offset, &offsets[start..start + len + 1])?;
41
42 mutable.child_data[0].try_extend(
43 index,
44 offsets[start].as_usize(),
45 offsets[start + len].as_usize(),
46 )
47 },
48 )
49}
50
51pub(super) fn extend_nulls<T: ArrowNativeType>(
52 mutable: &mut _MutableArrayData,
53 len: usize,
54) -> Result<(), ArrowError> {
55 let offset_buffer = &mut mutable.buffer1;
56
57 // this is safe due to how offset is built. See details on `get_last_offset`
58 let last_offset: T = unsafe { get_last_offset(offset_buffer) };
59
60 (0..len).for_each(|_| offset_buffer.push(last_offset));
61 Ok(())
62}