arrow_data/transform/
variable_size.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 arrow_buffer::{ArrowNativeType, MutableBuffer};
20use num::traits::AsPrimitive;
21use num::{CheckedAdd, Integer};
22
23use super::{
24    Extend, _MutableArrayData,
25    utils::{extend_offsets, get_last_offset},
26};
27
28#[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) {
36    let start_values = offsets[start].as_();
37    let end_values: usize = offsets[start + len].as_();
38    let new_values = &values[start_values..end_values];
39    buffer.extend_from_slice(new_values);
40}
41
42pub(super) fn build_extend<T: ArrowNativeType + Integer + CheckedAdd + AsPrimitive<usize>>(
43    array: &ArrayData,
44) -> Extend {
45    let offsets = array.buffer::<T>(0);
46    let values = array.buffers()[1].as_slice();
47    Box::new(
48        move |mutable: &mut _MutableArrayData, _, start: usize, len: usize| {
49            let offset_buffer = &mut mutable.buffer1;
50            let values_buffer = &mut mutable.buffer2;
51
52            // this is safe due to how offset is built. See details on `get_last_offset`
53            let last_offset = unsafe { get_last_offset(offset_buffer) };
54
55            extend_offsets::<T>(offset_buffer, last_offset, &offsets[start..start + len + 1]);
56            // values
57            extend_offset_values::<T>(values_buffer, offsets, values, start, len);
58        },
59    )
60}
61
62pub(super) fn extend_nulls<T: ArrowNativeType>(mutable: &mut _MutableArrayData, len: usize) {
63    let offset_buffer = &mut mutable.buffer1;
64
65    // this is safe due to how offset is built. See details on `get_last_offset`
66    let last_offset: T = unsafe { get_last_offset(offset_buffer) };
67
68    (0..len).for_each(|_| offset_buffer.push(last_offset))
69}