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