Skip to main content

iceberg/arrow/reader/
mod.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
18//! Parquet file data reader
19
20use crate::arrow::caching_delete_file_loader::CachingDeleteFileLoader;
21use crate::io::FileIO;
22use crate::runtime::Runtime;
23use crate::util::available_parallelism;
24
25/// Default gap between byte ranges below which they are coalesced into a
26/// single request. Matches object_store's `OBJECT_STORE_COALESCE_DEFAULT`.
27const DEFAULT_RANGE_COALESCE_BYTES: u64 = 1024 * 1024;
28
29/// Default maximum number of coalesced byte ranges fetched concurrently.
30/// Matches object_store's `OBJECT_STORE_COALESCE_PARALLEL`.
31const DEFAULT_RANGE_FETCH_CONCURRENCY: usize = 10;
32
33/// Default number of bytes to prefetch when parsing Parquet footer metadata.
34/// Matches DataFusion's default `ParquetOptions::metadata_size_hint`.
35const DEFAULT_METADATA_SIZE_HINT: usize = 512 * 1024;
36
37mod file_reader;
38mod options;
39mod pipeline;
40mod positional_deletes;
41mod predicate_visitor;
42mod projection;
43mod row_filter;
44mod row_lineage;
45pub use file_reader::ArrowFileReader;
46pub(crate) use options::ParquetReadOptions;
47use predicate_visitor::{CollectFieldIdVisitor, PredicateConverter};
48use projection::{
49    add_fallback_field_ids_to_arrow_schema, apply_name_mapping_to_arrow_schema,
50    find_leaf_by_field_id,
51};
52
53/// Builder to create ArrowReader
54pub struct ArrowReaderBuilder {
55    batch_size: Option<usize>,
56    file_io: FileIO,
57    concurrency_limit_data_files: usize,
58    row_group_filtering_enabled: bool,
59    row_selection_enabled: bool,
60    parquet_read_options: ParquetReadOptions,
61    runtime: Runtime,
62}
63
64impl ArrowReaderBuilder {
65    /// Create a new ArrowReaderBuilder
66    pub fn new(file_io: FileIO, runtime: Runtime) -> Self {
67        let num_cpus = available_parallelism().get();
68
69        ArrowReaderBuilder {
70            batch_size: None,
71            file_io,
72            concurrency_limit_data_files: num_cpus,
73            row_group_filtering_enabled: true,
74            row_selection_enabled: false,
75            parquet_read_options: ParquetReadOptions::builder().build(),
76            runtime,
77        }
78    }
79
80    /// Sets the max number of in flight data files that are being fetched
81    pub fn with_data_file_concurrency_limit(mut self, val: usize) -> Self {
82        self.concurrency_limit_data_files = val;
83        self
84    }
85
86    /// Sets the desired size of batches in the response
87    /// to something other than the default
88    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
89        self.batch_size = Some(batch_size);
90        self
91    }
92
93    /// Determines whether to enable row group filtering.
94    pub fn with_row_group_filtering_enabled(mut self, row_group_filtering_enabled: bool) -> Self {
95        self.row_group_filtering_enabled = row_group_filtering_enabled;
96        self
97    }
98
99    /// Determines whether to enable row selection.
100    pub fn with_row_selection_enabled(mut self, row_selection_enabled: bool) -> Self {
101        self.row_selection_enabled = row_selection_enabled;
102        self
103    }
104
105    /// Provide a hint as to the number of bytes to prefetch for parsing the Parquet metadata
106    ///
107    /// This hint can help reduce the number of fetch requests. For more details see the
108    /// [ParquetMetaDataReader documentation](https://docs.rs/parquet/latest/parquet/file/metadata/struct.ParquetMetaDataReader.html#method.with_prefetch_hint).
109    pub fn with_metadata_size_hint(mut self, metadata_size_hint: usize) -> Self {
110        self.parquet_read_options.metadata_size_hint = Some(metadata_size_hint);
111        self
112    }
113
114    /// Sets the gap threshold for merging nearby byte ranges into a single request.
115    /// Ranges with gaps smaller than this value will be coalesced.
116    ///
117    /// Defaults to 1 MiB, matching object_store's OBJECT_STORE_COALESCE_DEFAULT.
118    pub fn with_range_coalesce_bytes(mut self, range_coalesce_bytes: u64) -> Self {
119        self.parquet_read_options.range_coalesce_bytes = range_coalesce_bytes;
120        self
121    }
122
123    /// Sets the maximum number of merged byte ranges to fetch concurrently.
124    ///
125    /// Defaults to 10, matching object_store's OBJECT_STORE_COALESCE_PARALLEL.
126    pub fn with_range_fetch_concurrency(mut self, range_fetch_concurrency: usize) -> Self {
127        self.parquet_read_options.range_fetch_concurrency = range_fetch_concurrency;
128        self
129    }
130
131    /// Build the ArrowReader.
132    pub fn build(self) -> ArrowReader {
133        ArrowReader {
134            batch_size: self.batch_size,
135            file_io: self.file_io.clone(),
136            delete_file_loader: CachingDeleteFileLoader::new(
137                self.file_io.clone(),
138                self.concurrency_limit_data_files,
139                self.runtime.clone(),
140            ),
141            concurrency_limit_data_files: self.concurrency_limit_data_files,
142            row_group_filtering_enabled: self.row_group_filtering_enabled,
143            row_selection_enabled: self.row_selection_enabled,
144            parquet_read_options: self.parquet_read_options,
145        }
146    }
147}
148
149/// Reads data from Parquet files
150#[derive(Clone)]
151pub struct ArrowReader {
152    batch_size: Option<usize>,
153    file_io: FileIO,
154    delete_file_loader: CachingDeleteFileLoader,
155
156    /// the maximum number of data files that can be fetched at the same time
157    concurrency_limit_data_files: usize,
158
159    row_group_filtering_enabled: bool,
160    row_selection_enabled: bool,
161    parquet_read_options: ParquetReadOptions,
162}