arrow_select/coalesce/generic.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::InProgressArray;
19use crate::concat::concat;
20use arrow_array::ArrayRef;
21use arrow_schema::ArrowError;
22
23/// Generic implementation for [`InProgressArray`] that works with any type of
24/// array.
25///
26/// Internally, this buffers arrays and then calls other kernels such as
27/// [`concat`] to produce the final array.
28///
29/// [`concat`]: crate::concat::concat
30#[derive(Debug)]
31pub(crate) struct GenericInProgressArray {
32 /// The current source
33 source: Option<ArrayRef>,
34 /// The buffered array slices
35 buffered_arrays: Vec<ArrayRef>,
36}
37
38impl GenericInProgressArray {
39 /// Create a new `GenericInProgressArray`
40 pub(crate) fn new() -> Self {
41 Self {
42 source: None,
43 buffered_arrays: vec![],
44 }
45 }
46}
47impl InProgressArray for GenericInProgressArray {
48 fn set_source(&mut self, source: Option<ArrayRef>) {
49 self.source = source
50 }
51
52 fn copy_rows(&mut self, offset: usize, len: usize) -> Result<(), ArrowError> {
53 let source = self.source.as_ref().ok_or_else(|| {
54 ArrowError::InvalidArgumentError(
55 "Internal Error: GenericInProgressArray: source not set".to_string(),
56 )
57 })?;
58 let array = source.slice(offset, len);
59 self.buffered_arrays.push(array);
60 Ok(())
61 }
62
63 fn finish(&mut self) -> Result<ArrayRef, ArrowError> {
64 // Concatenate all buffered arrays into a single array, which uses 2x
65 // peak memory
66 let array = concat(
67 &self
68 .buffered_arrays
69 .iter()
70 .map(|array| array.as_ref())
71 .collect::<Vec<_>>(),
72 )?;
73 self.buffered_arrays.clear();
74 Ok(array)
75 }
76}