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