polars_lazy/scan/
parquet.rs1use polars_buffer::Buffer;
2use polars_core::prelude::*;
3use polars_io::cloud::CloudOptions;
4use polars_io::parquet::read::ParallelStrategy;
5use polars_io::prelude::ParquetOptions;
6use polars_io::{HiveOptions, RowIndex};
7use polars_utils::pl_path::PlRefPath;
8use polars_utils::slice_enum::Slice;
9
10use crate::prelude::*;
11
12#[derive(Clone)]
13pub struct ScanArgsParquet {
14 pub n_rows: Option<usize>,
15 pub parallel: ParallelStrategy,
16 pub row_index: Option<RowIndex>,
17 pub cloud_options: Option<CloudOptions>,
18 pub hive_options: HiveOptions,
19 pub use_statistics: bool,
20 pub schema: Option<SchemaRef>,
21 pub low_memory: bool,
22 pub rechunk: bool,
23 pub cache: bool,
24 pub glob: bool,
26 pub include_file_paths: Option<PlSmallStr>,
27 pub allow_missing_columns: bool,
28}
29
30impl Default for ScanArgsParquet {
31 fn default() -> Self {
32 Self {
33 n_rows: None,
34 parallel: Default::default(),
35 row_index: None,
36 cloud_options: None,
37 hive_options: Default::default(),
38 use_statistics: true,
39 schema: None,
40 rechunk: false,
41 low_memory: false,
42 cache: true,
43 glob: true,
44 include_file_paths: None,
45 allow_missing_columns: false,
46 }
47 }
48}
49
50#[derive(Clone)]
51struct LazyParquetReader {
52 args: ScanArgsParquet,
53 sources: ScanSources,
54}
55
56impl LazyParquetReader {
57 fn new(args: ScanArgsParquet) -> Self {
58 Self {
59 args,
60 sources: ScanSources::default(),
61 }
62 }
63}
64
65impl LazyFileListReader for LazyParquetReader {
66 fn finish(self) -> PolarsResult<LazyFrame> {
68 let parquet_options = ParquetOptions {
69 schema: self.args.schema,
70 parallel: self.args.parallel,
71 low_memory: self.args.low_memory,
72 use_statistics: self.args.use_statistics,
73 decryption: None,
74 };
75
76 let unified_scan_args = UnifiedScanArgs {
77 schema: None,
78 cloud_options: self.args.cloud_options,
79 hive_options: self.args.hive_options,
80 rechunk: self.args.rechunk,
81 cache: self.args.cache,
82 glob: self.args.glob,
83 expand_paths: true,
84 hidden_file_prefix: None,
85 projection: None,
86 field_selection: None,
87 column_mapping: None,
88 default_values: None,
89 row_index: None,
91 pre_slice: self
92 .args
93 .n_rows
94 .map(|len| Slice::Positive { offset: 0, len }),
95 cast_columns_policy: CastColumnsPolicy::ERROR_ON_MISMATCH,
96 missing_columns_policy: if self.args.allow_missing_columns {
97 MissingColumnsPolicy::Insert
98 } else {
99 MissingColumnsPolicy::Raise
100 },
101 extra_columns_policy: ExtraColumnsPolicy::Raise,
102 include_file_paths: self.args.include_file_paths,
103 deletion_files: None,
104 table_statistics: None,
105 hive_partitions: None,
106 row_count: None,
107 source_sizes: None,
108 resolve_heavy_sources: None,
109 };
110
111 let mut lf: LazyFrame =
112 DslBuilder::scan_parquet(self.sources, parquet_options, unified_scan_args)?
113 .build()
114 .into();
115
116 if let Some(row_index) = self.args.row_index {
118 lf = lf.with_row_index(row_index.name, Some(row_index.offset))
119 }
120
121 Ok(lf)
122 }
123
124 fn glob(&self) -> bool {
125 self.args.glob
126 }
127
128 fn finish_no_glob(self) -> PolarsResult<LazyFrame> {
129 unreachable!();
130 }
131
132 fn sources(&self) -> &ScanSources {
133 &self.sources
134 }
135
136 fn with_sources(mut self, sources: ScanSources) -> Self {
137 self.sources = sources;
138 self
139 }
140
141 fn with_n_rows(mut self, n_rows: impl Into<Option<usize>>) -> Self {
142 self.args.n_rows = n_rows.into();
143 self
144 }
145
146 fn with_row_index(mut self, row_index: impl Into<Option<RowIndex>>) -> Self {
147 self.args.row_index = row_index.into();
148 self
149 }
150
151 fn rechunk(&self) -> bool {
152 self.args.rechunk
153 }
154
155 fn with_rechunk(mut self, toggle: bool) -> Self {
156 self.args.rechunk = toggle;
157 self
158 }
159
160 fn cloud_options(&self) -> Option<&CloudOptions> {
161 self.args.cloud_options.as_ref()
162 }
163
164 fn n_rows(&self) -> Option<usize> {
165 self.args.n_rows
166 }
167
168 fn row_index(&self) -> Option<&RowIndex> {
169 self.args.row_index.as_ref()
170 }
171}
172
173impl LazyFrame {
174 pub fn scan_parquet(path: PlRefPath, args: ScanArgsParquet) -> PolarsResult<Self> {
176 Self::scan_parquet_sources(ScanSources::Paths(Buffer::from_iter([path])), args)
177 }
178
179 pub fn scan_parquet_sources(sources: ScanSources, args: ScanArgsParquet) -> PolarsResult<Self> {
181 LazyParquetReader::new(args).with_sources(sources).finish()
182 }
183
184 pub fn scan_parquet_files(
186 paths: Buffer<PlRefPath>,
187 args: ScanArgsParquet,
188 ) -> PolarsResult<Self> {
189 Self::scan_parquet_sources(ScanSources::Paths(paths), args)
190 }
191}