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.
1718use crate::ArrayData;
19use arrow_buffer::{ArrowNativeType, MutableBuffer};
20use num::traits::AsPrimitive;
21use num::{CheckedAdd, Integer};
2223use super::{
24 Extend, _MutableArrayData,
25 utils::{extend_offsets, get_last_offset},
26};
2728#[inline]
29fn extend_offset_values<T: ArrowNativeType + AsPrimitive<usize>>(
30 buffer: &mut MutableBuffer,
31 offsets: &[T],
32 values: &[u8],
33 start: usize,
34 len: usize,
35) {
36let start_values = offsets[start].as_();
37let end_values: usize = offsets[start + len].as_();
38let new_values = &values[start_values..end_values];
39 buffer.extend_from_slice(new_values);
40}
4142pub(super) fn build_extend<T: ArrowNativeType + Integer + CheckedAdd + AsPrimitive<usize>>(
43 array: &ArrayData,
44) -> Extend {
45let offsets = array.buffer::<T>(0);
46let values = array.buffers()[1].as_slice();
47 Box::new(
48move |mutable: &mut _MutableArrayData, _, start: usize, len: usize| {
49let offset_buffer = &mut mutable.buffer1;
50let values_buffer = &mut mutable.buffer2;
5152// this is safe due to how offset is built. See details on `get_last_offset`
53let last_offset = unsafe { get_last_offset(offset_buffer) };
5455 extend_offsets::<T>(offset_buffer, last_offset, &offsets[start..start + len + 1]);
56// values
57extend_offset_values::<T>(values_buffer, offsets, values, start, len);
58 },
59 )
60}
6162pub(super) fn extend_nulls<T: ArrowNativeType>(mutable: &mut _MutableArrayData, len: usize) {
63let offset_buffer = &mut mutable.buffer1;
6465// this is safe due to how offset is built. See details on `get_last_offset`
66let last_offset: T = unsafe { get_last_offset(offset_buffer) };
6768 (0..len).for_each(|_| offset_buffer.push(last_offset))
69}