Skip to main content

parquet/arrow/
in_memory_row_group.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::ProjectionMask;
19use crate::arrow::array_reader::RowGroups;
20use crate::arrow::arrow_reader::RowSelection;
21use crate::column::page::{PageIterator, PageReader};
22use crate::errors::ParquetError;
23use crate::file::metadata::page_index::RowGroupPageIndex;
24use crate::file::metadata::{ParquetMetaData, RowGroupMetaData};
25use crate::file::reader::{ChunkReader, Length, SerializedPageReader};
26use bytes::{Buf, Bytes};
27use std::ops::Range;
28use std::sync::Arc;
29
30/// An in-memory collection of column chunks
31#[derive(Debug)]
32pub(crate) struct InMemoryRowGroup<'a> {
33    pub(crate) page_index: Option<RowGroupPageIndex>,
34    /// Column chunks for this row group
35    pub(crate) column_chunks: Vec<Option<Arc<ColumnChunkData>>>,
36    pub(crate) row_count: usize,
37    pub(crate) row_group_idx: usize,
38    pub(crate) metadata: &'a ParquetMetaData,
39}
40
41/// What ranges to fetch for the columns in this row group
42#[derive(Debug)]
43pub(crate) struct FetchRanges {
44    /// The byte ranges to fetch
45    pub(crate) ranges: Vec<Range<u64>>,
46    /// If `Some`, the start offsets of each page for each column chunk
47    pub(crate) page_start_offsets: Option<Vec<Vec<u64>>>,
48}
49
50impl InMemoryRowGroup<'_> {
51    /// Returns the byte ranges to fetch for the columns specified in
52    /// `projection` and `selection`.
53    ///
54    /// `cache_mask` indicates which columns, if any, are being cached by
55    /// [`RowGroupCache`](crate::arrow::array_reader::RowGroupCache).
56    /// The `selection` for Cached columns is expanded to batch boundaries to simplify
57    /// accounting for what data is cached.
58    pub(crate) fn fetch_ranges(
59        &self,
60        projection: &ProjectionMask,
61        selection: Option<&RowSelection>,
62        batch_size: usize,
63        cache_mask: Option<&ProjectionMask>,
64    ) -> FetchRanges {
65        let metadata = self.metadata.row_group(self.row_group_idx);
66        if let Some((selection, page_index)) = selection.zip(self.page_index.as_ref()) {
67            let expanded_selection =
68                selection.expand_to_batch_boundaries(batch_size, self.row_count);
69
70            // If we have a `RowSelection` and an `OffsetIndex` then only fetch
71            // pages required for the `RowSelection`
72            // Consider preallocating outer vec: https://github.com/apache/arrow-rs/issues/8667
73            let mut page_start_offsets: Vec<Vec<u64>> = vec![];
74
75            let ranges = self
76                .column_chunks
77                .iter()
78                .zip(metadata.columns())
79                .enumerate()
80                .filter(|&(idx, (chunk, _chunk_meta))| {
81                    chunk.is_none() && projection.leaf_included(idx)
82                })
83                .flat_map(|(idx, (_chunk, chunk_meta))| {
84                    // If the first page does not start at the beginning of the column,
85                    // then we need to also fetch a dictionary page.
86                    let mut ranges: Vec<Range<u64>> = vec![];
87                    let (start, len) = chunk_meta.byte_range();
88                    let Some(offset_idx) = page_index.offset_index(idx) else {
89                        // No offset index for this column, fetch the entire column
90                        ranges.push(start..start + len);
91                        return ranges;
92                    };
93
94                    match offset_idx.page_locations().first() {
95                        Some(first) if first.offset as u64 != start => {
96                            ranges.push(start..first.offset as u64);
97                        }
98                        _ => (),
99                    }
100
101                    // Expand selection to batch boundaries if needed for caching
102                    // (see doc comment for this function for details on `cache_mask`)
103                    let use_expanded = cache_mask.map(|m| m.leaf_included(idx)).unwrap_or(false);
104                    if use_expanded {
105                        ranges.extend(expanded_selection.scan_ranges(offset_idx.page_locations()));
106                    } else {
107                        ranges.extend(selection.scan_ranges(offset_idx.page_locations()));
108                    }
109                    page_start_offsets.push(ranges.iter().map(|range| range.start).collect());
110
111                    ranges
112                })
113                .collect();
114            FetchRanges {
115                ranges,
116                page_start_offsets: Some(page_start_offsets),
117            }
118        } else {
119            let ranges = self
120                .column_chunks
121                .iter()
122                .enumerate()
123                .filter(|&(idx, chunk)| chunk.is_none() && projection.leaf_included(idx))
124                .map(|(idx, _chunk)| {
125                    let column = metadata.column(idx);
126                    let (start, length) = column.byte_range();
127                    start..(start + length)
128                })
129                .collect();
130            FetchRanges {
131                ranges,
132                page_start_offsets: None,
133            }
134        }
135    }
136
137    /// Fills in `self.column_chunks` with the data fetched from `chunk_data`.
138    ///
139    /// This function **must** be called with the data from the ranges returned by
140    /// `fetch_ranges` and the corresponding page_start_offsets, with the exact same and `selection`.
141    pub(crate) fn fill_column_chunks<I>(
142        &mut self,
143        projection: &ProjectionMask,
144        page_start_offsets: Option<Vec<Vec<u64>>>,
145        chunk_data: I,
146    ) where
147        I: IntoIterator<Item = Bytes>,
148    {
149        let mut chunk_data = chunk_data.into_iter();
150        let metadata = self.metadata.row_group(self.row_group_idx);
151        if let Some(page_start_offsets) = page_start_offsets {
152            // If we have a `RowSelection` and an `OffsetIndex` then only fetch pages required for the
153            // `RowSelection`
154            let mut page_start_offsets = page_start_offsets.into_iter();
155
156            for (idx, chunk) in self.column_chunks.iter_mut().enumerate() {
157                if chunk.is_some() || !projection.leaf_included(idx) {
158                    continue;
159                }
160
161                if let Some(offsets) = page_start_offsets.next() {
162                    let mut chunks = Vec::with_capacity(offsets.len());
163                    for _ in 0..offsets.len() {
164                        chunks.push(chunk_data.next().unwrap());
165                    }
166
167                    *chunk = Some(Arc::new(ColumnChunkData::Sparse {
168                        length: metadata.column(idx).byte_range().1 as usize,
169                        data: offsets
170                            .into_iter()
171                            .map(|x| x as usize)
172                            .zip(chunks)
173                            .collect(),
174                    }))
175                }
176            }
177        } else {
178            for (idx, chunk) in self.column_chunks.iter_mut().enumerate() {
179                if chunk.is_some() || !projection.leaf_included(idx) {
180                    continue;
181                }
182
183                if let Some(data) = chunk_data.next() {
184                    *chunk = Some(Arc::new(ColumnChunkData::Dense {
185                        offset: metadata.column(idx).byte_range().0 as usize,
186                        data,
187                    }));
188                }
189            }
190        }
191    }
192}
193
194impl RowGroups for InMemoryRowGroup<'_> {
195    fn num_rows(&self) -> usize {
196        self.row_count
197    }
198
199    /// Return chunks for column i
200    fn column_chunks(&self, i: usize) -> crate::errors::Result<Box<dyn PageIterator>> {
201        match &self.column_chunks[i] {
202            None => Err(ParquetError::General(format!(
203                "Invalid column index {i}, column was not fetched"
204            ))),
205            Some(data) => {
206                let page_locations = self
207                    .page_index
208                    .as_ref()
209                    .and_then(|pi| pi.page_locations(i).cloned());
210                let column_chunk_metadata = self.metadata.row_group(self.row_group_idx).column(i);
211                let page_reader = SerializedPageReader::new(
212                    data.clone(),
213                    column_chunk_metadata,
214                    self.row_count,
215                    page_locations,
216                )?;
217                let page_reader = page_reader.add_crypto_context(
218                    self.row_group_idx,
219                    i,
220                    self.metadata,
221                    column_chunk_metadata,
222                )?;
223
224                let page_reader: Box<dyn PageReader> = Box::new(page_reader);
225
226                Ok(Box::new(ColumnChunkIterator {
227                    reader: Some(Ok(page_reader)),
228                }))
229            }
230        }
231    }
232
233    fn row_groups(&self) -> Box<dyn Iterator<Item = &RowGroupMetaData> + '_> {
234        Box::new(std::iter::once(self.metadata.row_group(self.row_group_idx)))
235    }
236
237    fn metadata(&self) -> &ParquetMetaData {
238        self.metadata
239    }
240}
241
242/// An in-memory column chunk.
243/// This allows us to hold either dense column chunks or sparse column chunks and easily
244/// access them by offset.
245#[derive(Clone, Debug)]
246pub(crate) enum ColumnChunkData {
247    /// Column chunk data representing only a subset of data pages.
248    /// For example if a row selection (possibly caused by a filter in a query) causes us to read only
249    /// a subset of the rows in the column.
250    Sparse {
251        /// Length of the full column chunk
252        length: usize,
253        /// Subset of data pages included in this sparse chunk.
254        ///
255        /// Each element is a tuple of (page offset within file, page data).
256        /// Each entry is a complete page and the list is ordered by offset.
257        data: Vec<(usize, Bytes)>,
258    },
259    /// Full column chunk and the offset within the original file
260    Dense { offset: usize, data: Bytes },
261}
262
263impl ColumnChunkData {
264    /// Return the data for this column chunk at the given offset
265    fn get(&self, start: u64) -> crate::errors::Result<Bytes> {
266        match &self {
267            ColumnChunkData::Sparse { data, .. } => data
268                .binary_search_by_key(&start, |(offset, _)| *offset as u64)
269                .map(|idx| data[idx].1.clone())
270                .map_err(|_| {
271                    ParquetError::General(format!(
272                        "Invalid offset in sparse column chunk data: {start}, no matching page found.\
273                         If you are using a `SelectionStrategyPolicy::Mask`, ensure that the OffsetIndex is provided when \
274                         creating the InMemoryRowGroup."
275                    ))
276                }),
277            ColumnChunkData::Dense { offset, data } => {
278                let start = start as usize - *offset;
279                Ok(data.slice(start..))
280            }
281        }
282    }
283}
284
285impl Length for ColumnChunkData {
286    /// Return the total length of the full column chunk
287    fn len(&self) -> u64 {
288        match &self {
289            ColumnChunkData::Sparse { length, .. } => *length as u64,
290            ColumnChunkData::Dense { data, .. } => data.len() as u64,
291        }
292    }
293}
294
295impl ChunkReader for ColumnChunkData {
296    type T = bytes::buf::Reader<Bytes>;
297
298    fn get_read(&self, start: u64) -> crate::errors::Result<Self::T> {
299        Ok(self.get(start)?.reader())
300    }
301
302    fn get_bytes(&self, start: u64, length: usize) -> crate::errors::Result<Bytes> {
303        Ok(self.get(start)?.slice(..length))
304    }
305}
306
307/// Implements [`PageIterator`] for a single column chunk, yielding a single [`PageReader`]
308struct ColumnChunkIterator {
309    reader: Option<crate::errors::Result<Box<dyn PageReader>>>,
310}
311
312impl Iterator for ColumnChunkIterator {
313    type Item = crate::errors::Result<Box<dyn PageReader>>;
314
315    fn next(&mut self) -> Option<Self::Item> {
316        self.reader.take()
317    }
318}
319
320impl PageIterator for ColumnChunkIterator {}