Skip to main content

parquet/arrow/record_reader/
buffer.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::arrow::buffer::bit_util::iter_set_bits_rev;
19
20/// A buffer that supports padding with nulls
21pub trait ValuesBuffer {
22    /// Create a new buffer with capacity for at least `capacity` elements
23    ///
24    /// This allows pre-allocating buffers to avoid reallocations during reading,
25    /// improving performance when the number of values is known in advance.
26    fn with_capacity(capacity: usize) -> Self;
27
28    /// If a column contains nulls, more level data may be read than value data, as null
29    /// values are not encoded. Therefore, first the levels data is read, the null count
30    /// determined, and then the corresponding number of values read to a [`ValuesBuffer`].
31    ///
32    /// It is then necessary to move this values data into positions that correspond to
33    /// the non-null level positions. This is what this method does.
34    ///
35    /// It is provided with:
36    ///
37    /// - `read_offset` - the offset in [`ValuesBuffer`] to start null padding from
38    /// - `values_read` - the number of values read
39    /// - `levels_read` - the number of levels read
40    /// - `valid_mask` - a packed mask of valid levels
41    ///
42    fn pad_nulls(
43        &mut self,
44        read_offset: usize,
45        values_read: usize,
46        levels_read: usize,
47        valid_mask: &[u8],
48    );
49}
50
51impl<T: Copy + Default> ValuesBuffer for Vec<T> {
52    fn with_capacity(capacity: usize) -> Self {
53        Vec::with_capacity(capacity)
54    }
55
56    fn pad_nulls(
57        &mut self,
58        read_offset: usize,
59        values_read: usize,
60        levels_read: usize,
61        valid_mask: &[u8],
62    ) {
63        self.resize(read_offset + levels_read, T::default());
64
65        let values_range = read_offset..read_offset + values_read;
66        for (value_pos, level_pos) in values_range.rev().zip(iter_set_bits_rev(valid_mask)) {
67            debug_assert!(level_pos >= value_pos);
68            if level_pos <= value_pos {
69                break;
70            }
71            self[level_pos] = self[value_pos];
72        }
73    }
74}