Skip to main content

arrow_data/transform/
primitive.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;
20use std::mem::size_of;
21use std::ops::Add;
22
23use super::{_MutableArrayData, Extend};
24
25pub(super) fn build_extend<T: ArrowNativeType>(array: &ArrayData) -> Extend<'_> {
26    let values = array.buffer::<T>(0);
27    Box::new(
28        move |mutable: &mut _MutableArrayData, _, start: usize, len: usize| {
29            mutable
30                .buffer1
31                .extend_from_slice(&values[start..start + len]);
32            Ok(())
33        },
34    )
35}
36
37pub(super) fn build_extend_with_offset<T>(array: &ArrayData, offset: T) -> Extend<'_>
38where
39    T: ArrowNativeType + Add<Output = T>,
40{
41    let values = array.buffer::<T>(0);
42    Box::new(
43        move |mutable: &mut _MutableArrayData, _, start: usize, len: usize| {
44            mutable
45                .buffer1
46                .extend(values[start..start + len].iter().map(|x| *x + offset));
47            Ok(())
48        },
49    )
50}
51
52pub(super) fn extend_nulls<T: ArrowNativeType>(
53    mutable: &mut _MutableArrayData,
54    len: usize,
55) -> Result<(), arrow_schema::ArrowError> {
56    mutable.buffer1.extend_zeros(len * size_of::<T>());
57    Ok(())
58}