arrow_data/transform/
union.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::{Extend, _MutableArrayData};
19use crate::ArrayData;
20
21pub(super) fn build_extend_sparse(array: &ArrayData) -> Extend {
22    let type_ids = array.buffer::<i8>(0);
23
24    Box::new(
25        move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
26            // extends type_ids
27            mutable
28                .buffer1
29                .extend_from_slice(&type_ids[start..start + len]);
30
31            mutable
32                .child_data
33                .iter_mut()
34                .for_each(|child| child.extend(index, start, start + len))
35        },
36    )
37}
38
39pub(super) fn build_extend_dense(array: &ArrayData) -> Extend {
40    let type_ids = array.buffer::<i8>(0);
41    let offsets = array.buffer::<i32>(1);
42    let arrow_schema::DataType::Union(src_fields, _) = array.data_type() else {
43        unreachable!();
44    };
45
46    Box::new(
47        move |mutable: &mut _MutableArrayData, index: usize, start: usize, len: usize| {
48            // extends type_ids
49            mutable
50                .buffer1
51                .extend_from_slice(&type_ids[start..start + len]);
52
53            (start..start + len).for_each(|i| {
54                let type_id = type_ids[i];
55                let child_index = src_fields
56                    .iter()
57                    .position(|(r, _)| r == type_id)
58                    .expect("invalid union type ID");
59                let src_offset = offsets[i] as usize;
60                let child_data = &mut mutable.child_data[child_index];
61                let dst_offset = child_data.len();
62
63                // Extend offsets
64                mutable.buffer2.push(dst_offset as i32);
65                mutable.child_data[child_index].extend(index, src_offset, src_offset + 1)
66            })
67        },
68    )
69}
70
71pub(super) fn extend_nulls_dense(_mutable: &mut _MutableArrayData, _len: usize) {
72    panic!("cannot call extend_nulls on UnionArray as cannot infer type");
73}
74
75pub(super) fn extend_nulls_sparse(_mutable: &mut _MutableArrayData, _len: usize) {
76    panic!("cannot call extend_nulls on UnionArray as cannot infer type");
77}