parquet/file/properties.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//! Configuration via [`WriterProperties`] and [`ReaderProperties`]
19use crate::basic::{Compression, Encoding};
20use crate::compression::{CodecOptions, CodecOptionsBuilder};
21#[cfg(feature = "encryption")]
22use crate::encryption::encrypt::FileEncryptionProperties;
23use crate::file::metadata::{KeyValue, SortingColumn};
24use crate::schema::types::ColumnPath;
25use std::str::FromStr;
26use std::{collections::HashMap, sync::Arc};
27
28/// Default value for [`WriterProperties::data_page_size_limit`]
29pub const DEFAULT_PAGE_SIZE: usize = 1024 * 1024;
30/// Default value for [`WriterProperties::write_batch_size`]
31pub const DEFAULT_WRITE_BATCH_SIZE: usize = 1024;
32/// Default value for [`WriterProperties::writer_version`]
33pub const DEFAULT_WRITER_VERSION: WriterVersion = WriterVersion::PARQUET_1_0;
34/// Default value for [`WriterProperties::compression`]
35pub const DEFAULT_COMPRESSION: Compression = Compression::UNCOMPRESSED;
36/// Default value for [`WriterProperties::dictionary_enabled`]
37pub const DEFAULT_DICTIONARY_ENABLED: bool = true;
38/// Default value for [`WriterProperties::dictionary_page_size_limit`]
39pub const DEFAULT_DICTIONARY_PAGE_SIZE_LIMIT: usize = DEFAULT_PAGE_SIZE;
40/// Default value for [`WriterProperties::data_page_row_count_limit`]
41pub const DEFAULT_DATA_PAGE_ROW_COUNT_LIMIT: usize = 20_000;
42/// Default value for [`WriterProperties::statistics_enabled`]
43pub const DEFAULT_STATISTICS_ENABLED: EnabledStatistics = EnabledStatistics::Page;
44/// Default value for [`WriterProperties::write_page_header_statistics`]
45pub const DEFAULT_WRITE_PAGE_HEADER_STATISTICS: bool = false;
46/// Default value for [`WriterProperties::max_row_group_row_count`]
47pub const DEFAULT_MAX_ROW_GROUP_ROW_COUNT: usize = 1024 * 1024;
48/// Default value for [`WriterProperties::bloom_filter_position`]
49pub const DEFAULT_BLOOM_FILTER_POSITION: BloomFilterPosition = BloomFilterPosition::AfterRowGroup;
50/// Default value for [`WriterProperties::created_by`]
51pub const DEFAULT_CREATED_BY: &str = concat!("parquet-rs version ", env!("CARGO_PKG_VERSION"));
52/// Default value for [`WriterProperties::column_index_truncate_length`]
53pub const DEFAULT_COLUMN_INDEX_TRUNCATE_LENGTH: Option<usize> = Some(64);
54/// Default value for [`BloomFilterProperties::fpp`]
55pub const DEFAULT_BLOOM_FILTER_FPP: f64 = 0.05;
56/// Default value for [`BloomFilterProperties::ndv`].
57///
58/// Note: this is only the fallback default used when constructing [`BloomFilterProperties`]
59/// directly. When using [`WriterPropertiesBuilder`], columns with bloom filters enabled
60/// but without an explicit NDV will have their NDV resolved at build time to
61/// [`WriterProperties::max_row_group_row_count`], which may differ from this constant
62/// if the user configured a custom row group size.
63pub const DEFAULT_BLOOM_FILTER_NDV: u64 = DEFAULT_MAX_ROW_GROUP_ROW_COUNT as u64;
64/// Default values for [`WriterProperties::statistics_truncate_length`]
65pub const DEFAULT_STATISTICS_TRUNCATE_LENGTH: Option<usize> = Some(64);
66/// Default value for [`WriterProperties::offset_index_disabled`]
67pub const DEFAULT_OFFSET_INDEX_DISABLED: bool = false;
68/// Default values for [`WriterProperties::coerce_types`]
69pub const DEFAULT_COERCE_TYPES: bool = false;
70/// Default value for [`WriterProperties::data_page_v2_compression_ratio_threshold`]
71pub const DEFAULT_DATA_PAGE_V2_COMPRESSION_RATIO_THRESHOLD: f64 = 1.0;
72/// Default minimum chunk size for content-defined chunking: 256 KiB.
73pub const DEFAULT_CDC_MIN_CHUNK_SIZE: usize = 256 * 1024;
74/// Default maximum chunk size for content-defined chunking: 1024 KiB.
75pub const DEFAULT_CDC_MAX_CHUNK_SIZE: usize = 1024 * 1024;
76/// Default normalization level for content-defined chunking.
77pub const DEFAULT_CDC_NORM_LEVEL: i32 = 0;
78
79/// EXPERIMENTAL: Options for content-defined chunking (CDC).
80///
81/// Content-defined chunking is an experimental feature that optimizes parquet
82/// files for content addressable storage (CAS) systems by writing data pages
83/// according to content-defined chunk boundaries. This allows for more
84/// efficient deduplication of data across files, hence more efficient network
85/// transfers and storage.
86///
87/// Each content-defined chunk is written as a separate parquet data page. The
88/// following options control the chunks' size and the chunking process. Note
89/// that the chunk size is calculated based on the logical value of the data,
90/// before any encoding or compression is applied.
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub struct CdcOptions {
93 /// Minimum chunk size in bytes, default is 256 KiB.
94 /// The rolling hash will not be updated until this size is reached for each chunk.
95 /// Note that all data sent through the hash function is counted towards the chunk
96 /// size, including definition and repetition levels if present.
97 pub min_chunk_size: usize,
98 /// Maximum chunk size in bytes, default is 1024 KiB.
99 /// The chunker will create a new chunk whenever the chunk size exceeds this value.
100 /// Note that the parquet writer has a related [`data_page_size_limit`] property that
101 /// controls the maximum size of a parquet data page after encoding. While setting
102 /// `data_page_size_limit` to a smaller value than `max_chunk_size` doesn't affect
103 /// the chunking effectiveness, it results in more small parquet data pages.
104 ///
105 /// [`data_page_size_limit`]: WriterPropertiesBuilder::set_data_page_size_limit
106 pub max_chunk_size: usize,
107 /// Number of bit adjustment to the gearhash mask in order to center the chunk size
108 /// around the average size more aggressively, default is 0.
109 /// Increasing the normalization level increases the probability of finding a chunk,
110 /// improving the deduplication ratio, but also increasing the number of small chunks
111 /// resulting in many small parquet data pages. The default value provides a good
112 /// balance between deduplication ratio and fragmentation.
113 /// Use norm_level=1 or norm_level=2 to reach a higher deduplication ratio at the
114 /// expense of fragmentation. Negative values can also be used to reduce the
115 /// probability of finding a chunk, resulting in larger chunks and fewer data pages.
116 /// Note that values outside [-3, 3] are not recommended, prefer using the default
117 /// value of 0 for most use cases.
118 pub norm_level: i32,
119}
120
121impl Default for CdcOptions {
122 fn default() -> Self {
123 Self {
124 min_chunk_size: DEFAULT_CDC_MIN_CHUNK_SIZE,
125 max_chunk_size: DEFAULT_CDC_MAX_CHUNK_SIZE,
126 norm_level: DEFAULT_CDC_NORM_LEVEL,
127 }
128 }
129}
130
131/// Parquet writer version.
132///
133/// Basic constant, which is not part of the Thrift definition.
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135#[allow(non_camel_case_types)]
136pub enum WriterVersion {
137 /// Parquet format version 1.0
138 PARQUET_1_0,
139 /// Parquet format version 2.0
140 PARQUET_2_0,
141}
142
143impl WriterVersion {
144 /// Returns writer version as `i32`.
145 pub fn as_num(&self) -> i32 {
146 match self {
147 WriterVersion::PARQUET_1_0 => 1,
148 WriterVersion::PARQUET_2_0 => 2,
149 }
150 }
151}
152
153impl FromStr for WriterVersion {
154 type Err = String;
155
156 fn from_str(s: &str) -> Result<Self, Self::Err> {
157 match s {
158 "PARQUET_1_0" | "parquet_1_0" => Ok(WriterVersion::PARQUET_1_0),
159 "PARQUET_2_0" | "parquet_2_0" => Ok(WriterVersion::PARQUET_2_0),
160 _ => Err(format!("Invalid writer version: {s}")),
161 }
162 }
163}
164
165/// Where in the file [`ArrowWriter`](crate::arrow::arrow_writer::ArrowWriter) should
166/// write Bloom filters
167///
168/// Basic constant, which is not part of the Thrift definition.
169#[derive(Debug, Clone, Copy, PartialEq, Eq)]
170pub enum BloomFilterPosition {
171 /// Write Bloom Filters of each row group right after the row group
172 ///
173 /// This saves memory by writing it as soon as it is computed, at the cost
174 /// of data locality for readers
175 AfterRowGroup,
176 /// Write Bloom Filters at the end of the file
177 ///
178 /// This allows better data locality for readers, at the cost of memory usage
179 /// for writers.
180 End,
181}
182
183/// Reference counted writer properties.
184pub type WriterPropertiesPtr = Arc<WriterProperties>;
185
186/// Resolved state of [`WriterPropertiesBuilder::set_offset_index_disabled`].
187///
188/// When a user disables offset indexes but page-level statistics are enabled,
189/// the setting is overridden (offset indexes remain enabled). This enum
190/// preserves the user's original intent so that a round-trip through
191/// `WriterPropertiesBuilder` does not lose it.
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193enum OffsetIndexSetting {
194 /// Offset indexes are enabled (the default).
195 Enabled,
196 /// User disabled offset indexes and no page-level statistics override it.
197 Disabled,
198 /// User disabled offset indexes, but page-level statistics require them,
199 /// so they remain enabled.
200 DisabledOverridden,
201}
202
203/// Configuration settings for writing parquet files.
204///
205/// Use [`Self::builder`] to create a [`WriterPropertiesBuilder`] to change settings.
206///
207/// # Example
208///
209/// ```rust
210/// # use parquet::{
211/// # basic::{Compression, Encoding},
212/// # file::properties::*,
213/// # schema::types::ColumnPath,
214/// # };
215/// #
216/// // Create properties with default configuration.
217/// let props = WriterProperties::default();
218///
219/// // Use properties builder to set certain options and assemble the configuration.
220/// let props = WriterProperties::builder()
221/// .set_writer_version(WriterVersion::PARQUET_1_0)
222/// .set_encoding(Encoding::PLAIN)
223/// .set_column_encoding(ColumnPath::from("col1"), Encoding::DELTA_BINARY_PACKED)
224/// .set_compression(Compression::SNAPPY)
225/// .build();
226///
227/// assert_eq!(props.writer_version(), WriterVersion::PARQUET_1_0);
228/// assert_eq!(
229/// props.encoding(&ColumnPath::from("col1")),
230/// Some(Encoding::DELTA_BINARY_PACKED)
231/// );
232/// assert_eq!(
233/// props.encoding(&ColumnPath::from("col2")),
234/// Some(Encoding::PLAIN)
235/// );
236/// ```
237#[derive(Debug, Clone)]
238pub struct WriterProperties {
239 data_page_row_count_limit: usize,
240 write_batch_size: usize,
241 max_row_group_row_count: Option<usize>,
242 max_row_group_bytes: Option<usize>,
243 bloom_filter_position: BloomFilterPosition,
244 writer_version: WriterVersion,
245 created_by: String,
246 offset_index_setting: OffsetIndexSetting,
247 pub(crate) key_value_metadata: Option<Vec<KeyValue>>,
248 default_column_properties: ColumnProperties,
249 column_properties: HashMap<ColumnPath, ColumnProperties>,
250 sorting_columns: Option<Vec<SortingColumn>>,
251 column_index_truncate_length: Option<usize>,
252 statistics_truncate_length: Option<usize>,
253 coerce_types: bool,
254 content_defined_chunking: Option<CdcOptions>,
255 #[cfg(feature = "encryption")]
256 pub(crate) file_encryption_properties: Option<Arc<FileEncryptionProperties>>,
257}
258
259impl Default for WriterProperties {
260 fn default() -> Self {
261 Self::builder().build()
262 }
263}
264
265impl WriterProperties {
266 /// Create a new [`WriterProperties`] with the default settings
267 ///
268 /// See [`WriterProperties::builder`] for customising settings
269 pub fn new() -> Self {
270 Self::default()
271 }
272
273 /// Returns a new default [`WriterPropertiesBuilder`] for creating writer
274 /// properties.
275 pub fn builder() -> WriterPropertiesBuilder {
276 WriterPropertiesBuilder::default()
277 }
278
279 /// Converts this [`WriterProperties`] into a [`WriterPropertiesBuilder`]
280 /// Used for mutating existing property settings
281 pub fn into_builder(self) -> WriterPropertiesBuilder {
282 self.into()
283 }
284
285 /// Returns data page size limit.
286 ///
287 /// Note: this is a best effort limit based on the write batch size
288 ///
289 /// For more details see [`WriterPropertiesBuilder::set_data_page_size_limit`]
290 pub fn data_page_size_limit(&self) -> usize {
291 self.default_column_properties
292 .data_page_size_limit()
293 .unwrap_or(DEFAULT_PAGE_SIZE)
294 }
295
296 /// Returns data page size limit for a specific column.
297 ///
298 /// Takes precedence over [`Self::data_page_size_limit`].
299 ///
300 /// Note: this is a best effort limit based on the write batch size.
301 pub fn column_data_page_size_limit(&self, col: &ColumnPath) -> usize {
302 self.column_properties
303 .get(col)
304 .and_then(|c| c.data_page_size_limit())
305 .or_else(|| self.default_column_properties.data_page_size_limit())
306 .unwrap_or(DEFAULT_PAGE_SIZE)
307 }
308
309 /// Returns dictionary page size limit.
310 ///
311 /// Note: this is a best effort limit based on the write batch size
312 ///
313 /// For more details see [`WriterPropertiesBuilder::set_dictionary_page_size_limit`]
314 pub fn dictionary_page_size_limit(&self) -> usize {
315 self.default_column_properties
316 .dictionary_page_size_limit()
317 .unwrap_or(DEFAULT_DICTIONARY_PAGE_SIZE_LIMIT)
318 }
319
320 /// Returns dictionary page size limit for a specific column.
321 pub fn column_dictionary_page_size_limit(&self, col: &ColumnPath) -> usize {
322 self.column_properties
323 .get(col)
324 .and_then(|c| c.dictionary_page_size_limit())
325 .or_else(|| self.default_column_properties.dictionary_page_size_limit())
326 .unwrap_or(DEFAULT_DICTIONARY_PAGE_SIZE_LIMIT)
327 }
328
329 /// Returns the maximum page row count
330 ///
331 /// Note: this is a best effort limit based on the write batch size
332 ///
333 /// For more details see [`WriterPropertiesBuilder::set_data_page_row_count_limit`]
334 pub fn data_page_row_count_limit(&self) -> usize {
335 self.data_page_row_count_limit
336 }
337
338 /// Returns configured batch size for writes.
339 ///
340 /// When writing a batch of data, this setting allows to split it internally into
341 /// smaller batches so we can better estimate the size of a page currently being
342 /// written.
343 ///
344 /// For more details see [`WriterPropertiesBuilder::set_write_batch_size`]
345 pub fn write_batch_size(&self) -> usize {
346 self.write_batch_size
347 }
348
349 /// Returns maximum number of rows in a row group, or `usize::MAX` if unlimited.
350 ///
351 /// For more details see [`WriterPropertiesBuilder::set_max_row_group_size`]
352 #[deprecated(since = "58.0.0", note = "Use `max_row_group_row_count` instead")]
353 pub fn max_row_group_size(&self) -> usize {
354 self.max_row_group_row_count.unwrap_or(usize::MAX)
355 }
356
357 /// Returns maximum number of rows in a row group, or `None` if unlimited.
358 ///
359 /// For more details see [`WriterPropertiesBuilder::set_max_row_group_row_count`]
360 pub fn max_row_group_row_count(&self) -> Option<usize> {
361 self.max_row_group_row_count
362 }
363
364 /// Returns maximum size of a row group in bytes, or `None` if unlimited.
365 ///
366 /// For more details see [`WriterPropertiesBuilder::set_max_row_group_bytes`]
367 pub fn max_row_group_bytes(&self) -> Option<usize> {
368 self.max_row_group_bytes
369 }
370
371 /// Returns bloom filter position.
372 ///
373 /// For more details see [`WriterPropertiesBuilder::set_bloom_filter_position`]
374 pub fn bloom_filter_position(&self) -> BloomFilterPosition {
375 self.bloom_filter_position
376 }
377
378 /// Returns configured writer version.
379 ///
380 /// For more details see [`WriterPropertiesBuilder::set_writer_version`]
381 pub fn writer_version(&self) -> WriterVersion {
382 self.writer_version
383 }
384
385 /// Returns `created_by` string.
386 ///
387 /// For more details see [`WriterPropertiesBuilder::set_created_by`]
388 pub fn created_by(&self) -> &str {
389 &self.created_by
390 }
391
392 /// Returns `true` if offset index writing is disabled.
393 ///
394 /// For more details see [`WriterPropertiesBuilder::set_offset_index_disabled`]
395 pub fn offset_index_disabled(&self) -> bool {
396 matches!(self.offset_index_setting, OffsetIndexSetting::Disabled)
397 }
398
399 /// Returns `key_value_metadata` KeyValue pairs.
400 ///
401 /// For more details see [`WriterPropertiesBuilder::set_key_value_metadata`]
402 pub fn key_value_metadata(&self) -> Option<&Vec<KeyValue>> {
403 self.key_value_metadata.as_ref()
404 }
405
406 /// Returns sorting columns.
407 ///
408 /// For more details see [`WriterPropertiesBuilder::set_sorting_columns`]
409 pub fn sorting_columns(&self) -> Option<&Vec<SortingColumn>> {
410 self.sorting_columns.as_ref()
411 }
412
413 /// Returns the maximum length of truncated min/max values in the column index.
414 ///
415 /// `None` if truncation is disabled, must be greater than 0 otherwise.
416 ///
417 /// For more details see [`WriterPropertiesBuilder::set_column_index_truncate_length`]
418 pub fn column_index_truncate_length(&self) -> Option<usize> {
419 self.column_index_truncate_length
420 }
421
422 /// Returns the maximum length of truncated min/max values in [`Statistics`].
423 ///
424 /// `None` if truncation is disabled, must be greater than 0 otherwise.
425 ///
426 /// For more details see [`WriterPropertiesBuilder::set_statistics_truncate_length`]
427 ///
428 /// [`Statistics`]: crate::file::statistics::Statistics
429 pub fn statistics_truncate_length(&self) -> Option<usize> {
430 self.statistics_truncate_length
431 }
432
433 /// Returns `true` if type coercion is enabled.
434 ///
435 /// For more details see [`WriterPropertiesBuilder::set_coerce_types`]
436 pub fn coerce_types(&self) -> bool {
437 self.coerce_types
438 }
439
440 /// EXPERIMENTAL: Returns content-defined chunking options, or `None` if CDC is disabled.
441 ///
442 /// For more details see [`WriterPropertiesBuilder::set_content_defined_chunking`]
443 pub fn content_defined_chunking(&self) -> Option<&CdcOptions> {
444 self.content_defined_chunking.as_ref()
445 }
446
447 /// Returns the compression ratio threshold at or above which a Data Page v2's
448 /// compressed values are discarded in favor of writing the values uncompressed.
449 ///
450 /// For more details see [`WriterPropertiesBuilder::set_data_page_v2_compression_ratio_threshold`]
451 pub fn data_page_v2_compression_ratio_threshold(&self) -> f64 {
452 self.default_column_properties
453 .data_page_v2_compression_ratio_threshold()
454 .unwrap_or(DEFAULT_DATA_PAGE_V2_COMPRESSION_RATIO_THRESHOLD)
455 }
456
457 /// Returns the Data Page v2 compression ratio threshold for a specific column.
458 ///
459 /// Takes precedence over [`Self::data_page_v2_compression_ratio_threshold`].
460 pub fn column_data_page_v2_compression_ratio_threshold(&self, col: &ColumnPath) -> f64 {
461 self.column_properties
462 .get(col)
463 .and_then(|c| c.data_page_v2_compression_ratio_threshold())
464 .or_else(|| {
465 self.default_column_properties
466 .data_page_v2_compression_ratio_threshold()
467 })
468 .unwrap_or(DEFAULT_DATA_PAGE_V2_COMPRESSION_RATIO_THRESHOLD)
469 }
470
471 /// Returns encoding for a data page, when dictionary encoding is enabled.
472 ///
473 /// This is not configurable.
474 #[inline]
475 pub fn dictionary_data_page_encoding(&self) -> Encoding {
476 // PLAIN_DICTIONARY encoding is deprecated in writer version 1.
477 // Dictionary values are encoded using RLE_DICTIONARY encoding.
478 Encoding::RLE_DICTIONARY
479 }
480
481 /// Returns encoding for dictionary page, when dictionary encoding is enabled.
482 ///
483 /// This is not configurable.
484 #[inline]
485 pub fn dictionary_page_encoding(&self) -> Encoding {
486 // PLAIN_DICTIONARY is deprecated in writer version 1.
487 // Dictionary is encoded using plain encoding.
488 Encoding::PLAIN
489 }
490
491 /// Returns encoding for a column, if set.
492 ///
493 /// In case when dictionary is enabled, returns fallback encoding.
494 ///
495 /// If encoding is not set, then column writer will choose the best encoding
496 /// based on the column type.
497 pub fn encoding(&self, col: &ColumnPath) -> Option<Encoding> {
498 self.column_properties
499 .get(col)
500 .and_then(|c| c.encoding())
501 .or_else(|| self.default_column_properties.encoding())
502 }
503
504 /// Returns compression codec for a column.
505 ///
506 /// For more details see [`WriterPropertiesBuilder::set_column_compression`]
507 pub fn compression(&self, col: &ColumnPath) -> Compression {
508 self.column_properties
509 .get(col)
510 .and_then(|c| c.compression())
511 .or_else(|| self.default_column_properties.compression())
512 .unwrap_or(DEFAULT_COMPRESSION)
513 }
514
515 /// Returns `true` if dictionary encoding is enabled for a column.
516 ///
517 /// For more details see [`WriterPropertiesBuilder::set_dictionary_enabled`]
518 pub fn dictionary_enabled(&self, col: &ColumnPath) -> bool {
519 self.column_properties
520 .get(col)
521 .and_then(|c| c.dictionary_enabled())
522 .or_else(|| self.default_column_properties.dictionary_enabled())
523 .unwrap_or(DEFAULT_DICTIONARY_ENABLED)
524 }
525
526 /// Returns which statistics are written for a column.
527 ///
528 /// For more details see [`WriterPropertiesBuilder::set_statistics_enabled`]
529 pub fn statistics_enabled(&self, col: &ColumnPath) -> EnabledStatistics {
530 self.column_properties
531 .get(col)
532 .and_then(|c| c.statistics_enabled())
533 .or_else(|| self.default_column_properties.statistics_enabled())
534 .unwrap_or(DEFAULT_STATISTICS_ENABLED)
535 }
536
537 /// Returns `true` if [`Statistics`] are to be written to the page header for a column.
538 ///
539 /// For more details see [`WriterPropertiesBuilder::set_write_page_header_statistics`]
540 ///
541 /// [`Statistics`]: crate::file::statistics::Statistics
542 pub fn write_page_header_statistics(&self, col: &ColumnPath) -> bool {
543 self.column_properties
544 .get(col)
545 .and_then(|c| c.write_page_header_statistics())
546 .or_else(|| {
547 self.default_column_properties
548 .write_page_header_statistics()
549 })
550 .unwrap_or(DEFAULT_WRITE_PAGE_HEADER_STATISTICS)
551 }
552
553 /// Returns the [`BloomFilterProperties`] for the given column
554 ///
555 /// Returns `None` if bloom filter is disabled
556 ///
557 /// For more details see [`WriterPropertiesBuilder::set_column_bloom_filter_enabled`]
558 pub fn bloom_filter_properties(&self, col: &ColumnPath) -> Option<&BloomFilterProperties> {
559 self.column_properties
560 .get(col)
561 .and_then(|c| c.bloom_filter_properties())
562 .or_else(|| self.default_column_properties.bloom_filter_properties())
563 }
564
565 /// Return file encryption properties
566 ///
567 /// For more details see [`WriterPropertiesBuilder::with_file_encryption_properties`]
568 #[cfg(feature = "encryption")]
569 pub fn file_encryption_properties(&self) -> Option<&Arc<FileEncryptionProperties>> {
570 self.file_encryption_properties.as_ref()
571 }
572}
573
574/// Builder for [`WriterProperties`] Parquet writer configuration.
575///
576/// See example on [`WriterProperties`]
577#[derive(Debug, Clone)]
578pub struct WriterPropertiesBuilder {
579 data_page_row_count_limit: usize,
580 write_batch_size: usize,
581 max_row_group_row_count: Option<usize>,
582 max_row_group_bytes: Option<usize>,
583 bloom_filter_position: BloomFilterPosition,
584 writer_version: WriterVersion,
585 created_by: String,
586 offset_index_disabled: bool,
587 key_value_metadata: Option<Vec<KeyValue>>,
588 default_column_properties: ColumnProperties,
589 column_properties: HashMap<ColumnPath, ColumnProperties>,
590 sorting_columns: Option<Vec<SortingColumn>>,
591 column_index_truncate_length: Option<usize>,
592 statistics_truncate_length: Option<usize>,
593 coerce_types: bool,
594 content_defined_chunking: Option<CdcOptions>,
595 #[cfg(feature = "encryption")]
596 file_encryption_properties: Option<Arc<FileEncryptionProperties>>,
597}
598
599impl Default for WriterPropertiesBuilder {
600 /// Returns default state of the builder.
601 fn default() -> Self {
602 Self {
603 data_page_row_count_limit: DEFAULT_DATA_PAGE_ROW_COUNT_LIMIT,
604 write_batch_size: DEFAULT_WRITE_BATCH_SIZE,
605 max_row_group_row_count: Some(DEFAULT_MAX_ROW_GROUP_ROW_COUNT),
606 max_row_group_bytes: None,
607 bloom_filter_position: DEFAULT_BLOOM_FILTER_POSITION,
608 writer_version: DEFAULT_WRITER_VERSION,
609 created_by: DEFAULT_CREATED_BY.to_string(),
610 offset_index_disabled: DEFAULT_OFFSET_INDEX_DISABLED,
611 key_value_metadata: None,
612 default_column_properties: Default::default(),
613 column_properties: HashMap::new(),
614 sorting_columns: None,
615 column_index_truncate_length: DEFAULT_COLUMN_INDEX_TRUNCATE_LENGTH,
616 statistics_truncate_length: DEFAULT_STATISTICS_TRUNCATE_LENGTH,
617 coerce_types: DEFAULT_COERCE_TYPES,
618 content_defined_chunking: None,
619 #[cfg(feature = "encryption")]
620 file_encryption_properties: None,
621 }
622 }
623}
624
625impl WriterPropertiesBuilder {
626 /// Finalizes the configuration and returns immutable writer properties struct.
627 pub fn build(self) -> WriterProperties {
628 // Pre-compute offset_index_setting
629 let offset_index_setting = if self.offset_index_disabled {
630 let default_page_stats_enabled = self.default_column_properties.statistics_enabled()
631 == Some(EnabledStatistics::Page);
632 let column_page_stats_enabled = self.column_properties.iter().any(|path_props| {
633 path_props.1.statistics_enabled() == Some(EnabledStatistics::Page)
634 });
635 if default_page_stats_enabled || column_page_stats_enabled {
636 OffsetIndexSetting::DisabledOverridden
637 } else {
638 OffsetIndexSetting::Disabled
639 }
640 } else {
641 OffsetIndexSetting::Enabled
642 };
643
644 // Resolve bloom filter NDV for columns where it wasn't explicitly set:
645 // default to max_row_group_row_count so the filter is never undersized.
646 let default_ndv = self
647 .max_row_group_row_count
648 .unwrap_or(DEFAULT_MAX_ROW_GROUP_ROW_COUNT) as u64;
649 let mut default_column_properties = self.default_column_properties;
650 default_column_properties.resolve_bloom_filter_ndv(default_ndv);
651 let mut column_properties = self.column_properties;
652 for props in column_properties.values_mut() {
653 props.resolve_bloom_filter_ndv(default_ndv);
654 }
655
656 WriterProperties {
657 data_page_row_count_limit: self.data_page_row_count_limit,
658 write_batch_size: self.write_batch_size,
659 max_row_group_row_count: self.max_row_group_row_count,
660 max_row_group_bytes: self.max_row_group_bytes,
661 bloom_filter_position: self.bloom_filter_position,
662 writer_version: self.writer_version,
663 created_by: self.created_by,
664 offset_index_setting,
665 key_value_metadata: self.key_value_metadata,
666 default_column_properties,
667 column_properties,
668 sorting_columns: self.sorting_columns,
669 column_index_truncate_length: self.column_index_truncate_length,
670 statistics_truncate_length: self.statistics_truncate_length,
671 coerce_types: self.coerce_types,
672 content_defined_chunking: self.content_defined_chunking,
673 #[cfg(feature = "encryption")]
674 file_encryption_properties: self.file_encryption_properties,
675 }
676 }
677
678 // ----------------------------------------------------------------------
679 // Writer properties related to a file
680
681 /// Sets the `WriterVersion` written into the parquet metadata (defaults to [`PARQUET_1_0`]
682 /// via [`DEFAULT_WRITER_VERSION`])
683 ///
684 /// This value can determine what features some readers will support.
685 ///
686 /// [`PARQUET_1_0`]: [WriterVersion::PARQUET_1_0]
687 pub fn set_writer_version(mut self, value: WriterVersion) -> Self {
688 self.writer_version = value;
689 self
690 }
691
692 /// Sets best effort maximum number of rows in a data page (defaults to `20_000`
693 /// via [`DEFAULT_DATA_PAGE_ROW_COUNT_LIMIT`]).
694 ///
695 /// The parquet writer will attempt to limit the number of rows in
696 /// each `DataPage` to this value. Reducing this value will result
697 /// in larger parquet files, but may improve the effectiveness of
698 /// page index based predicate pushdown during reading.
699 ///
700 /// Note: this is a best effort limit based on value of
701 /// [`set_write_batch_size`](Self::set_write_batch_size).
702 pub fn set_data_page_row_count_limit(mut self, value: usize) -> Self {
703 self.data_page_row_count_limit = value;
704 self
705 }
706
707 /// Sets write batch size (defaults to 1024 via [`DEFAULT_WRITE_BATCH_SIZE`]).
708 ///
709 /// For performance reasons, data for each column is written in
710 /// batches of this size.
711 ///
712 /// Additional limits such as such as
713 /// [`set_data_page_row_count_limit`](Self::set_data_page_row_count_limit)
714 /// are checked between batches, and thus the write batch size value acts as an
715 /// upper-bound on the enforcement granularity of other limits.
716 pub fn set_write_batch_size(mut self, value: usize) -> Self {
717 self.write_batch_size = value;
718 self
719 }
720
721 /// Sets maximum number of rows in a row group (defaults to `1024 * 1024`
722 /// via [`DEFAULT_MAX_ROW_GROUP_ROW_COUNT`]).
723 ///
724 /// # Panics
725 /// If the value is set to 0.
726 #[deprecated(since = "58.0.0", note = "Use `set_max_row_group_row_count` instead")]
727 pub fn set_max_row_group_size(mut self, value: usize) -> Self {
728 assert!(value > 0, "Cannot have a 0 max row group size");
729 self.max_row_group_row_count = Some(value);
730 self
731 }
732
733 /// Sets maximum number of rows in a row group, or `None` for unlimited.
734 ///
735 /// If both `max_row_group_row_count` and `max_row_group_bytes` are set,
736 /// the row group with the smaller limit will be produced.
737 ///
738 /// # Panics
739 /// If the value is `Some(0)`.
740 pub fn set_max_row_group_row_count(mut self, value: Option<usize>) -> Self {
741 assert_ne!(value, Some(0), "Cannot have a 0 max row group row count");
742 self.max_row_group_row_count = value;
743 self
744 }
745
746 /// Sets maximum size of a row group in bytes, or `None` for unlimited.
747 ///
748 /// Row groups are flushed when their estimated encoded size exceeds this threshold.
749 /// This is similar to the official Java implementation for `parquet.block.size`'s behavior.
750 ///
751 /// If both `max_row_group_row_count` and `max_row_group_bytes` are set,
752 /// the row group with the smaller limit will be produced.
753 ///
754 /// # Panics
755 /// If the value is `Some(0)`.
756 pub fn set_max_row_group_bytes(mut self, value: Option<usize>) -> Self {
757 assert_ne!(value, Some(0), "Cannot have a 0 max row group bytes");
758 self.max_row_group_bytes = value;
759 self
760 }
761
762 /// Sets where in the final file Bloom Filters are written (defaults to [`AfterRowGroup`]
763 /// via [`DEFAULT_BLOOM_FILTER_POSITION`])
764 ///
765 /// [`AfterRowGroup`]: BloomFilterPosition::AfterRowGroup
766 pub fn set_bloom_filter_position(mut self, value: BloomFilterPosition) -> Self {
767 self.bloom_filter_position = value;
768 self
769 }
770
771 /// Sets "created by" property (defaults to `parquet-rs version <VERSION>` via
772 /// [`DEFAULT_CREATED_BY`]).
773 ///
774 /// This is a string that will be written into the file metadata
775 pub fn set_created_by(mut self, value: String) -> Self {
776 self.created_by = value;
777 self
778 }
779
780 /// Sets whether the writing of offset indexes is disabled (defaults to `false` via
781 /// [`DEFAULT_OFFSET_INDEX_DISABLED`]).
782 ///
783 /// If statistics level is set to [`Page`] this setting will be overridden with `false`.
784 ///
785 /// Note: As the offset indexes are useful for accessing data by row number,
786 /// they are always written by default, regardless of whether other statistics
787 /// are enabled. Disabling this metadata may result in a degradation in read
788 /// performance, so use this option with care.
789 ///
790 /// [`Page`]: EnabledStatistics::Page
791 pub fn set_offset_index_disabled(mut self, value: bool) -> Self {
792 self.offset_index_disabled = value;
793 self
794 }
795
796 /// Sets "key_value_metadata" property (defaults to `None`).
797 pub fn set_key_value_metadata(mut self, value: Option<Vec<KeyValue>>) -> Self {
798 self.key_value_metadata = value;
799 self
800 }
801
802 /// Sets sorting order of rows in the row group if any (defaults to `None`).
803 pub fn set_sorting_columns(mut self, value: Option<Vec<SortingColumn>>) -> Self {
804 self.sorting_columns = value;
805 self
806 }
807
808 /// Sets the max length of min/max value fields when writing the column
809 /// [`Index`] (defaults to `Some(64)` via [`DEFAULT_COLUMN_INDEX_TRUNCATE_LENGTH`]).
810 ///
811 /// This can be used to prevent columns with very long values (hundreds of
812 /// bytes long) from causing the parquet metadata to become huge.
813 ///
814 /// # Notes
815 ///
816 /// The column [`Index`] is written when [`Self::set_statistics_enabled`] is
817 /// set to [`EnabledStatistics::Page`].
818 ///
819 /// * If `Some`, must be greater than 0, otherwise will panic
820 /// * If `None`, there's no effective limit.
821 ///
822 /// [`Index`]: crate::file::page_index::column_index::ColumnIndexMetaData
823 pub fn set_column_index_truncate_length(mut self, max_length: Option<usize>) -> Self {
824 if let Some(value) = max_length {
825 assert!(
826 value > 0,
827 "Cannot have a 0 column index truncate length. If you wish to disable min/max value truncation, set it to `None`."
828 );
829 }
830
831 self.column_index_truncate_length = max_length;
832 self
833 }
834
835 /// Sets the max length of min/max value fields in row group and data page header
836 /// [`Statistics`] (defaults to `Some(64)` via [`DEFAULT_STATISTICS_TRUNCATE_LENGTH`]).
837 ///
838 /// # Notes
839 /// Row group [`Statistics`] are written when [`Self::set_statistics_enabled`] is
840 /// set to [`EnabledStatistics::Chunk`] or [`EnabledStatistics::Page`]. Data page header
841 /// [`Statistics`] are written when [`Self::set_statistics_enabled`] is set to
842 /// [`EnabledStatistics::Page`].
843 ///
844 /// * If `Some`, must be greater than 0, otherwise will panic
845 /// * If `None`, there's no effective limit.
846 ///
847 /// # See also
848 /// Truncation of Page Index statistics is controlled separately via
849 /// [`WriterPropertiesBuilder::set_column_index_truncate_length`]
850 ///
851 /// [`Statistics`]: crate::file::statistics::Statistics
852 pub fn set_statistics_truncate_length(mut self, max_length: Option<usize>) -> Self {
853 if let Some(value) = max_length {
854 assert!(
855 value > 0,
856 "Cannot have a 0 statistics truncate length. If you wish to disable min/max value truncation, set it to `None`."
857 );
858 }
859
860 self.statistics_truncate_length = max_length;
861 self
862 }
863
864 /// Should the writer coerce types to parquet native types (defaults to `false` via
865 /// [`DEFAULT_COERCE_TYPES`]).
866 ///
867 /// Leaving this option the default `false` will ensure the exact same data
868 /// written to parquet using this library will be read.
869 ///
870 /// Setting this option to `true` will result in parquet files that can be
871 /// read by more readers, but potentially lose information in the process.
872 ///
873 /// * Types such as [`DataType::Date64`], which have no direct corresponding
874 /// Parquet type, may be stored with lower precision.
875 ///
876 /// * The internal field names of `List` and `Map` types will be renamed if
877 /// necessary to match what is required by the newest Parquet specification.
878 ///
879 /// See [`ArrowToParquetSchemaConverter::with_coerce_types`] for more details
880 ///
881 /// [`DataType::Date64`]: arrow_schema::DataType::Date64
882 /// [`ArrowToParquetSchemaConverter::with_coerce_types`]: crate::arrow::ArrowSchemaConverter::with_coerce_types
883 pub fn set_coerce_types(mut self, coerce_types: bool) -> Self {
884 self.coerce_types = coerce_types;
885 self
886 }
887
888 /// EXPERIMENTAL: Sets content-defined chunking options, or disables CDC with `None`.
889 ///
890 /// When enabled, data page boundaries are determined by a rolling hash of the
891 /// column values, so unchanged data produces identical byte sequences across
892 /// file versions. This enables efficient deduplication on content-addressable
893 /// storage systems.
894 ///
895 /// Only supported through the Arrow writer interface ([`ArrowWriter`]).
896 ///
897 /// # Panics
898 ///
899 /// Panics if `min_chunk_size == 0` or `max_chunk_size <= min_chunk_size`.
900 ///
901 /// [`ArrowWriter`]: crate::arrow::arrow_writer::ArrowWriter
902 pub fn set_content_defined_chunking(mut self, options: Option<CdcOptions>) -> Self {
903 if let Some(ref options) = options {
904 assert!(
905 options.min_chunk_size > 0,
906 "min_chunk_size must be positive"
907 );
908 assert!(
909 options.max_chunk_size > options.min_chunk_size,
910 "max_chunk_size ({}) must be greater than min_chunk_size ({})",
911 options.max_chunk_size,
912 options.min_chunk_size
913 );
914 }
915 self.content_defined_chunking = options;
916 self
917 }
918
919 /// Sets the default compression ratio threshold at or above which a Data Page
920 /// v2's compressed values are discarded in favor of writing the values
921 /// uncompressed, for all columns (defaults to `1.0` via
922 /// [`DEFAULT_DATA_PAGE_V2_COMPRESSION_RATIO_THRESHOLD`]).
923 ///
924 /// When writing a Data Page v2 with a configured compression codec, the writer
925 /// first compresses the values and then compares the compressed size to the
926 /// uncompressed size. If `compressed_size >= uncompressed_size * threshold`, the
927 /// compressed buffer is discarded and the values are written uncompressed for
928 /// that page (the page's `is_compressed` flag is set to `false`).
929 ///
930 /// The default of `1.0` preserves the historical behavior of only keeping
931 /// compression when it strictly reduces the size. Setting a value below `1.0`
932 /// requires a minimum amount of size reduction to keep the compressed page —
933 /// for example `0.9` requires at least a 10% reduction. Setting a value above
934 /// `1.0` keeps the compressed buffer even if it's somewhat larger than the
935 /// uncompressed values.
936 ///
937 /// This setting only affects Data Page v2; Data Page v1 always stores the
938 /// compressor's output regardless of the resulting size.
939 ///
940 /// # Panics
941 /// If `value` is not finite or is not strictly positive.
942 pub fn set_data_page_v2_compression_ratio_threshold(mut self, value: f64) -> Self {
943 self.default_column_properties
944 .set_data_page_v2_compression_ratio_threshold(value);
945 self
946 }
947
948 /// Sets FileEncryptionProperties (defaults to `None`)
949 ///
950 /// # Notes
951 ///
952 /// Writing encrypted bloom filters is not yet supported. See
953 /// <https://github.com/apache/arrow-rs/issues/12166>
954 #[cfg(feature = "encryption")]
955 pub fn with_file_encryption_properties(
956 mut self,
957 file_encryption_properties: Arc<FileEncryptionProperties>,
958 ) -> Self {
959 self.file_encryption_properties = Some(file_encryption_properties);
960 self
961 }
962
963 // ----------------------------------------------------------------------
964 // Setters for any column (global)
965
966 /// Sets default encoding for all columns.
967 ///
968 /// If dictionary is not enabled, this is treated as a primary encoding for all
969 /// columns. In case when dictionary is enabled for any column, this value is
970 /// considered to be a fallback encoding for that column.
971 ///
972 /// # Panics
973 ///
974 /// if dictionary encoding is specified, regardless of dictionary
975 /// encoding flag being set.
976 pub fn set_encoding(mut self, value: Encoding) -> Self {
977 self.default_column_properties.set_encoding(value);
978 self
979 }
980
981 /// Sets default compression codec for all columns (default to [`UNCOMPRESSED`] via
982 /// [`DEFAULT_COMPRESSION`]).
983 ///
984 /// [`UNCOMPRESSED`]: Compression::UNCOMPRESSED
985 pub fn set_compression(mut self, value: Compression) -> Self {
986 self.default_column_properties.set_compression(value);
987 self
988 }
989
990 /// Sets default flag to enable/disable dictionary encoding for all columns (defaults to `true`
991 /// via [`DEFAULT_DICTIONARY_ENABLED`]).
992 ///
993 /// Use this method to set dictionary encoding, instead of explicitly specifying
994 /// encoding in `set_encoding` method.
995 pub fn set_dictionary_enabled(mut self, value: bool) -> Self {
996 self.default_column_properties.set_dictionary_enabled(value);
997 self
998 }
999
1000 /// Sets best effort maximum dictionary page size, in bytes (defaults to `1024 * 1024`
1001 /// via [`DEFAULT_DICTIONARY_PAGE_SIZE_LIMIT`]).
1002 ///
1003 /// The parquet writer will attempt to limit the size of each
1004 /// `DataPage` used to store dictionaries to this many
1005 /// bytes. Reducing this value will result in larger parquet
1006 /// files, but may improve the effectiveness of page index based
1007 /// predicate pushdown during reading.
1008 ///
1009 /// Note: this is a best effort limit based on value of
1010 /// [`set_write_batch_size`](Self::set_write_batch_size).
1011 pub fn set_dictionary_page_size_limit(mut self, value: usize) -> Self {
1012 self.default_column_properties
1013 .set_dictionary_page_size_limit(value);
1014 self
1015 }
1016
1017 /// Sets best effort maximum size of a data page in bytes (defaults to `1024 * 1024`
1018 /// via [`DEFAULT_PAGE_SIZE`]).
1019 ///
1020 /// The parquet writer will attempt to limit the sizes of each
1021 /// `DataPage` to this many bytes. Reducing this value will result
1022 /// in larger parquet files, but may improve the effectiveness of
1023 /// page index based predicate pushdown during reading.
1024 ///
1025 /// Note: this is a best effort limit based on value of
1026 /// [`set_write_batch_size`](Self::set_write_batch_size).
1027 pub fn set_data_page_size_limit(mut self, value: usize) -> Self {
1028 self.default_column_properties
1029 .set_data_page_size_limit(value);
1030 self
1031 }
1032
1033 /// Sets default [`EnabledStatistics`] level for all columns (defaults to [`Page`] via
1034 /// [`DEFAULT_STATISTICS_ENABLED`]).
1035 ///
1036 /// [`Page`]: EnabledStatistics::Page
1037 pub fn set_statistics_enabled(mut self, value: EnabledStatistics) -> Self {
1038 self.default_column_properties.set_statistics_enabled(value);
1039 self
1040 }
1041
1042 /// enable/disable writing [`Statistics`] in the page header
1043 /// (defaults to `false` via [`DEFAULT_WRITE_PAGE_HEADER_STATISTICS`]).
1044 ///
1045 /// Only applicable if [`Page`] level statistics are gathered.
1046 ///
1047 /// Setting this value to `true` can greatly increase the size of the resulting Parquet
1048 /// file while yielding very little added benefit. Most modern Parquet implementations
1049 /// will use the min/max values stored in the [`ParquetColumnIndex`] rather than
1050 /// those in the page header.
1051 ///
1052 /// # Note
1053 ///
1054 /// Prior to version 56.0.0, the `parquet` crate always wrote these
1055 /// statistics (the equivalent of setting this option to `true`). This was
1056 /// changed in 56.0.0 to follow the recommendation in the Parquet
1057 /// specification. See [issue #7580] for more details.
1058 ///
1059 /// [`Statistics`]: crate::file::statistics::Statistics
1060 /// [`ParquetColumnIndex`]: crate::file::metadata::ParquetColumnIndex
1061 /// [`Page`]: EnabledStatistics::Page
1062 /// [issue #7580]: https://github.com/apache/arrow-rs/issues/7580
1063 pub fn set_write_page_header_statistics(mut self, value: bool) -> Self {
1064 self.default_column_properties
1065 .set_write_page_header_statistics(value);
1066 self
1067 }
1068
1069 /// Sets if bloom filter should be written for all columns (defaults to `false`).
1070 ///
1071 /// # Notes
1072 ///
1073 /// * If the bloom filter is enabled previously then it is a no-op.
1074 ///
1075 /// * If the bloom filter is not enabled, default values for ndv and fpp
1076 /// value are used used. See [`set_bloom_filter_ndv`] and
1077 /// [`set_bloom_filter_fpp`] to further adjust the ndv and fpp.
1078 ///
1079 /// [`set_bloom_filter_ndv`]: Self::set_bloom_filter_ndv
1080 /// [`set_bloom_filter_fpp`]: Self::set_bloom_filter_fpp
1081 pub fn set_bloom_filter_enabled(mut self, value: bool) -> Self {
1082 self.default_column_properties
1083 .set_bloom_filter_enabled(value);
1084 self
1085 }
1086
1087 /// Sets the default target bloom filter false positive probability (fpp)
1088 /// for all columns (defaults to `0.05` via [`DEFAULT_BLOOM_FILTER_FPP`]).
1089 ///
1090 /// Implicitly enables bloom writing, as if [`set_bloom_filter_enabled`] had
1091 /// been called.
1092 ///
1093 /// [`set_bloom_filter_enabled`]: Self::set_bloom_filter_enabled
1094 pub fn set_bloom_filter_fpp(mut self, value: f64) -> Self {
1095 self.default_column_properties.set_bloom_filter_fpp(value);
1096 self
1097 }
1098
1099 /// Sets default maximum expected number of distinct values (ndv) for bloom filter
1100 /// for all columns (defaults to [`DEFAULT_BLOOM_FILTER_NDV`]).
1101 ///
1102 /// The bloom filter is initially sized for this many distinct values at the
1103 /// configured FPP, then folded down after all values are inserted to achieve
1104 /// optimal size. A good heuristic is to set this to the expected number of rows
1105 /// in the row group.
1106 ///
1107 /// Implicitly enables bloom writing, as if [`set_bloom_filter_enabled`] had
1108 /// been called.
1109 ///
1110 /// [`set_bloom_filter_enabled`]: Self::set_bloom_filter_enabled
1111 pub fn set_bloom_filter_ndv(mut self, value: u64) -> Self {
1112 self.default_column_properties.set_bloom_filter_ndv(value);
1113 self
1114 }
1115
1116 // ----------------------------------------------------------------------
1117 // Setters for a specific column
1118
1119 /// Helper method to get existing or new mutable reference of column properties.
1120 #[inline]
1121 fn get_mut_props(&mut self, col: ColumnPath) -> &mut ColumnProperties {
1122 self.column_properties.entry(col).or_default()
1123 }
1124
1125 /// Sets encoding for a specific column.
1126 ///
1127 /// Takes precedence over [`Self::set_encoding`].
1128 ///
1129 /// If dictionary is not enabled, this is treated as a primary encoding for this
1130 /// column. In case when dictionary is enabled for this column, either through
1131 /// global defaults or explicitly, this value is considered to be a fallback
1132 /// encoding for this column.
1133 ///
1134 /// # Panics
1135 /// If user tries to set dictionary encoding here, regardless of dictionary
1136 /// encoding flag being set.
1137 pub fn set_column_encoding(mut self, col: ColumnPath, value: Encoding) -> Self {
1138 self.get_mut_props(col).set_encoding(value);
1139 self
1140 }
1141
1142 /// Sets compression codec for a specific column.
1143 ///
1144 /// Takes precedence over [`Self::set_compression`].
1145 pub fn set_column_compression(mut self, col: ColumnPath, value: Compression) -> Self {
1146 self.get_mut_props(col).set_compression(value);
1147 self
1148 }
1149
1150 /// Sets flag to enable/disable dictionary encoding for a specific column.
1151 ///
1152 /// Takes precedence over [`Self::set_dictionary_enabled`].
1153 pub fn set_column_dictionary_enabled(mut self, col: ColumnPath, value: bool) -> Self {
1154 self.get_mut_props(col).set_dictionary_enabled(value);
1155 self
1156 }
1157
1158 /// Sets dictionary page size limit for a specific column.
1159 ///
1160 /// Takes precedence over [`Self::set_dictionary_page_size_limit`].
1161 pub fn set_column_dictionary_page_size_limit(mut self, col: ColumnPath, value: usize) -> Self {
1162 self.get_mut_props(col)
1163 .set_dictionary_page_size_limit(value);
1164 self
1165 }
1166
1167 /// Sets data page size limit for a specific column.
1168 ///
1169 /// Takes precedence over [`Self::set_data_page_size_limit`].
1170 pub fn set_column_data_page_size_limit(mut self, col: ColumnPath, value: usize) -> Self {
1171 self.get_mut_props(col).set_data_page_size_limit(value);
1172 self
1173 }
1174
1175 /// Sets [`EnabledStatistics`] level for a specific column.
1176 ///
1177 /// Takes precedence over [`Self::set_statistics_enabled`].
1178 pub fn set_column_statistics_enabled(
1179 mut self,
1180 col: ColumnPath,
1181 value: EnabledStatistics,
1182 ) -> Self {
1183 self.get_mut_props(col).set_statistics_enabled(value);
1184 self
1185 }
1186
1187 /// Sets whether to write [`Statistics`] in the page header for a specific column.
1188 ///
1189 /// Takes precedence over [`Self::set_write_page_header_statistics`].
1190 ///
1191 /// [`Statistics`]: crate::file::statistics::Statistics
1192 pub fn set_column_write_page_header_statistics(mut self, col: ColumnPath, value: bool) -> Self {
1193 self.get_mut_props(col)
1194 .set_write_page_header_statistics(value);
1195 self
1196 }
1197
1198 /// Sets whether a bloom filter should be written for a specific column.
1199 ///
1200 /// Takes precedence over [`Self::set_bloom_filter_enabled`].
1201 pub fn set_column_bloom_filter_enabled(mut self, col: ColumnPath, value: bool) -> Self {
1202 self.get_mut_props(col).set_bloom_filter_enabled(value);
1203 self
1204 }
1205
1206 /// Sets the false positive probability for bloom filter for a specific column.
1207 ///
1208 /// Takes precedence over [`Self::set_bloom_filter_fpp`].
1209 pub fn set_column_bloom_filter_fpp(mut self, col: ColumnPath, value: f64) -> Self {
1210 self.get_mut_props(col).set_bloom_filter_fpp(value);
1211 self
1212 }
1213
1214 /// Sets the number of distinct values for bloom filter for a specific column.
1215 ///
1216 /// Takes precedence over [`Self::set_bloom_filter_ndv`].
1217 pub fn set_column_bloom_filter_ndv(mut self, col: ColumnPath, value: u64) -> Self {
1218 self.get_mut_props(col).set_bloom_filter_ndv(value);
1219 self
1220 }
1221
1222 /// Sets the Data Page v2 compression ratio threshold for a specific column.
1223 ///
1224 /// Takes precedence over [`Self::set_data_page_v2_compression_ratio_threshold`].
1225 ///
1226 /// # Panics
1227 /// If `value` is not finite or is not strictly positive.
1228 pub fn set_column_data_page_v2_compression_ratio_threshold(
1229 mut self,
1230 col: ColumnPath,
1231 value: f64,
1232 ) -> Self {
1233 self.get_mut_props(col)
1234 .set_data_page_v2_compression_ratio_threshold(value);
1235 self
1236 }
1237}
1238
1239impl From<WriterProperties> for WriterPropertiesBuilder {
1240 fn from(props: WriterProperties) -> Self {
1241 WriterPropertiesBuilder {
1242 data_page_row_count_limit: props.data_page_row_count_limit,
1243 write_batch_size: props.write_batch_size,
1244 max_row_group_row_count: props.max_row_group_row_count,
1245 max_row_group_bytes: props.max_row_group_bytes,
1246 bloom_filter_position: props.bloom_filter_position,
1247 writer_version: props.writer_version,
1248 created_by: props.created_by,
1249 offset_index_disabled: !matches!(
1250 props.offset_index_setting,
1251 OffsetIndexSetting::Enabled
1252 ),
1253 key_value_metadata: props.key_value_metadata,
1254 default_column_properties: props.default_column_properties,
1255 column_properties: props.column_properties,
1256 sorting_columns: props.sorting_columns,
1257 column_index_truncate_length: props.column_index_truncate_length,
1258 statistics_truncate_length: props.statistics_truncate_length,
1259 coerce_types: props.coerce_types,
1260 content_defined_chunking: props.content_defined_chunking,
1261 #[cfg(feature = "encryption")]
1262 file_encryption_properties: props.file_encryption_properties,
1263 }
1264 }
1265}
1266
1267/// Controls the level of statistics to be computed by the writer and stored in
1268/// the parquet file.
1269///
1270/// Enabling statistics makes the resulting Parquet file larger and requires
1271/// more time to read the parquet footer.
1272///
1273/// Statistics can be used to improve query performance by pruning row groups
1274/// and pages during query execution if the query engine supports evaluating the
1275/// predicate using the statistics.
1276#[derive(Debug, Clone, Copy, Eq, PartialEq)]
1277pub enum EnabledStatistics {
1278 /// Compute no statistics.
1279 None,
1280 /// Compute column chunk-level statistics but not page-level.
1281 ///
1282 /// Setting this option will store one set of statistics for each relevant
1283 /// column for each row group. The more row groups written, the more
1284 /// statistics will be stored.
1285 Chunk,
1286 /// Compute page-level and column chunk-level statistics.
1287 ///
1288 /// Setting this option will store one set of statistics for each relevant
1289 /// column for each row group. In addition, this will enable the writing
1290 /// of the column index (the offset index is always written regardless of
1291 /// this setting). See [`ParquetColumnIndex`] for
1292 /// more information.
1293 ///
1294 /// [`ParquetColumnIndex`]: crate::file::metadata::ParquetColumnIndex
1295 Page,
1296}
1297
1298impl FromStr for EnabledStatistics {
1299 type Err = String;
1300
1301 fn from_str(s: &str) -> Result<Self, Self::Err> {
1302 match s {
1303 "NONE" | "none" => Ok(EnabledStatistics::None),
1304 "CHUNK" | "chunk" => Ok(EnabledStatistics::Chunk),
1305 "PAGE" | "page" => Ok(EnabledStatistics::Page),
1306 _ => Err(format!("Invalid statistics arg: {s}")),
1307 }
1308 }
1309}
1310
1311impl Default for EnabledStatistics {
1312 fn default() -> Self {
1313 DEFAULT_STATISTICS_ENABLED
1314 }
1315}
1316
1317/// Controls the bloom filter to be computed by the writer.
1318///
1319/// The bloom filter is initially sized for `ndv` distinct values at the given `fpp`, then
1320/// automatically folded down after all values are inserted to achieve optimal size while
1321/// maintaining the target `fpp`. See [`Sbbf::fold_to_target_fpp`] for details on the
1322/// folding algorithm.
1323///
1324/// [`Sbbf::fold_to_target_fpp`]: crate::bloom_filter::Sbbf::fold_to_target_fpp
1325#[derive(Debug, Clone, PartialEq)]
1326pub struct BloomFilterProperties {
1327 /// False positive probability. This should be always between 0 and 1 exclusive. Defaults to [`DEFAULT_BLOOM_FILTER_FPP`].
1328 ///
1329 /// You should set this value by calling [`WriterPropertiesBuilder::set_bloom_filter_fpp`].
1330 ///
1331 /// The bloom filter data structure is a trade of between disk and memory space versus fpp, the
1332 /// smaller the fpp, the more memory and disk space is required, thus setting it to a reasonable value
1333 /// e.g. 0.1, 0.05, or 0.001 is recommended.
1334 ///
1335 /// This value also serves as the target FPP for bloom filter folding: after all values
1336 /// are inserted, the filter is folded down to the smallest size that still meets this FPP.
1337 pub fpp: f64,
1338 /// Maximum expected number of distinct values. Defaults to [`DEFAULT_BLOOM_FILTER_NDV`].
1339 ///
1340 /// You should set this value by calling [`WriterPropertiesBuilder::set_bloom_filter_ndv`].
1341 ///
1342 /// When not explicitly set via the builder, this defaults to
1343 /// [`max_row_group_row_count`](WriterProperties::max_row_group_row_count) (resolved at
1344 /// build time). The bloom filter is initially sized for this many distinct values at the
1345 /// given `fpp`, then folded down after insertion to achieve optimal size. A good heuristic
1346 /// is to set this to the expected number of rows in the row group. If fewer distinct values
1347 /// are actually written, the filter will be automatically compacted via folding.
1348 ///
1349 /// Thus the only negative side of overestimating this value is that the bloom filter
1350 /// will use more memory during writing than necessary, but it will not affect the final
1351 /// bloom filter size on disk.
1352 ///
1353 /// If you wish to reduce memory usage during writing and are able to make a reasonable estimate
1354 /// of the number of distinct values in a row group, it is recommended to set this value explicitly
1355 /// rather than relying on the default dynamic sizing based on `max_row_group_row_count`.
1356 /// If you do set this value explicitly it is probably best to set it for each column
1357 /// individually via [`WriterPropertiesBuilder::set_column_bloom_filter_ndv`] rather than globally,
1358 /// since different columns may have different numbers of distinct values.
1359 pub ndv: u64,
1360}
1361
1362impl Default for BloomFilterProperties {
1363 fn default() -> Self {
1364 BloomFilterProperties {
1365 fpp: DEFAULT_BLOOM_FILTER_FPP,
1366 ndv: DEFAULT_BLOOM_FILTER_NDV,
1367 }
1368 }
1369}
1370
1371/// Container for column properties that can be changed as part of writer.
1372///
1373/// If a field is `None`, it means that no specific value has been set for this column,
1374/// so some subsequent or default value must be used.
1375#[derive(Debug, Clone, Default, PartialEq)]
1376struct ColumnProperties {
1377 encoding: Option<Encoding>,
1378 codec: Option<Compression>,
1379 data_page_size_limit: Option<usize>,
1380 dictionary_page_size_limit: Option<usize>,
1381 dictionary_enabled: Option<bool>,
1382 statistics_enabled: Option<EnabledStatistics>,
1383 write_page_header_statistics: Option<bool>,
1384 /// bloom filter related properties
1385 bloom_filter_properties: Option<BloomFilterProperties>,
1386 /// Whether the bloom filter NDV was explicitly set by the user
1387 bloom_filter_ndv_is_set: bool,
1388 data_page_v2_compression_ratio_threshold: Option<f64>,
1389}
1390
1391impl ColumnProperties {
1392 /// Sets encoding for this column.
1393 ///
1394 /// If dictionary is not enabled, this is treated as a primary encoding for a column.
1395 /// In case when dictionary is enabled for a column, this value is considered to
1396 /// be a fallback encoding.
1397 ///
1398 /// Panics if user tries to set dictionary encoding here, regardless of dictionary
1399 /// encoding flag being set. Use `set_dictionary_enabled` method to enable dictionary
1400 /// for a column.
1401 fn set_encoding(&mut self, value: Encoding) {
1402 if value == Encoding::PLAIN_DICTIONARY || value == Encoding::RLE_DICTIONARY {
1403 panic!("Dictionary encoding can not be used as fallback encoding");
1404 }
1405 self.encoding = Some(value);
1406 }
1407
1408 /// Sets compression codec for this column.
1409 fn set_compression(&mut self, value: Compression) {
1410 self.codec = Some(value);
1411 }
1412
1413 /// Sets data page size limit for this column.
1414 fn set_data_page_size_limit(&mut self, value: usize) {
1415 self.data_page_size_limit = Some(value);
1416 }
1417
1418 /// Sets whether dictionary encoding is enabled for this column.
1419 fn set_dictionary_enabled(&mut self, enabled: bool) {
1420 self.dictionary_enabled = Some(enabled);
1421 }
1422
1423 /// Sets dictionary page size limit for this column.
1424 fn set_dictionary_page_size_limit(&mut self, value: usize) {
1425 self.dictionary_page_size_limit = Some(value);
1426 }
1427
1428 /// Sets the statistics level for this column.
1429 fn set_statistics_enabled(&mut self, enabled: EnabledStatistics) {
1430 self.statistics_enabled = Some(enabled);
1431 }
1432
1433 /// Sets whether to write statistics in the page header for this column.
1434 fn set_write_page_header_statistics(&mut self, enabled: bool) {
1435 self.write_page_header_statistics = Some(enabled);
1436 }
1437
1438 /// If `value` is `true`, sets bloom filter properties to default values if not previously set,
1439 /// otherwise it is a no-op.
1440 /// If `value` is `false`, resets bloom filter properties to `None`.
1441 fn set_bloom_filter_enabled(&mut self, value: bool) {
1442 if value && self.bloom_filter_properties.is_none() {
1443 self.bloom_filter_properties = Some(Default::default())
1444 } else if !value {
1445 self.bloom_filter_properties = None
1446 }
1447 }
1448
1449 /// Sets the false positive probability for bloom filter for this column, and implicitly enables
1450 /// bloom filter if not previously enabled.
1451 ///
1452 /// # Panics
1453 ///
1454 /// Panics if the `value` is not between 0 and 1 exclusive
1455 fn set_bloom_filter_fpp(&mut self, value: f64) {
1456 assert!(
1457 value > 0. && value < 1.0,
1458 "fpp must be between 0 and 1 exclusive, got {value}"
1459 );
1460
1461 self.bloom_filter_properties
1462 .get_or_insert_with(Default::default)
1463 .fpp = value;
1464 }
1465
1466 /// Sets the maximum expected number of distinct (unique) values for bloom filter for this
1467 /// column, and implicitly enables bloom filter if not previously enabled.
1468 fn set_bloom_filter_ndv(&mut self, value: u64) {
1469 self.bloom_filter_properties
1470 .get_or_insert_with(Default::default)
1471 .ndv = value;
1472 self.bloom_filter_ndv_is_set = true;
1473 }
1474
1475 /// Sets the Data Page v2 compression ratio threshold for this column.
1476 ///
1477 /// # Panics
1478 /// If `value` is not finite or is not strictly positive.
1479 fn set_data_page_v2_compression_ratio_threshold(&mut self, value: f64) {
1480 assert!(
1481 value.is_finite() && value > 0.0,
1482 "data_page_v2_compression_ratio_threshold must be a positive finite number, got {value}"
1483 );
1484 self.data_page_v2_compression_ratio_threshold = Some(value);
1485 }
1486
1487 /// Returns optional encoding for this column.
1488 fn encoding(&self) -> Option<Encoding> {
1489 self.encoding
1490 }
1491
1492 /// Returns optional compression codec for this column.
1493 fn compression(&self) -> Option<Compression> {
1494 self.codec
1495 }
1496
1497 /// Returns `Some(true)` if dictionary encoding is enabled for this column, if
1498 /// disabled then returns `Some(false)`. If result is `None`, then no setting has
1499 /// been provided.
1500 fn dictionary_enabled(&self) -> Option<bool> {
1501 self.dictionary_enabled
1502 }
1503
1504 /// Returns optional dictionary page size limit for this column.
1505 fn dictionary_page_size_limit(&self) -> Option<usize> {
1506 self.dictionary_page_size_limit
1507 }
1508
1509 /// Returns optional data page size limit for this column.
1510 fn data_page_size_limit(&self) -> Option<usize> {
1511 self.data_page_size_limit
1512 }
1513
1514 /// Returns optional statistics level requested for this column. If result is `None`,
1515 /// then no setting has been provided.
1516 fn statistics_enabled(&self) -> Option<EnabledStatistics> {
1517 self.statistics_enabled
1518 }
1519
1520 /// Returns `Some(true)` if [`Statistics`] are to be written to the page header for this
1521 /// column.
1522 ///
1523 /// [`Statistics`]: crate::file::statistics::Statistics
1524 fn write_page_header_statistics(&self) -> Option<bool> {
1525 self.write_page_header_statistics
1526 }
1527
1528 /// Returns the bloom filter properties, or `None` if not enabled
1529 fn bloom_filter_properties(&self) -> Option<&BloomFilterProperties> {
1530 self.bloom_filter_properties.as_ref()
1531 }
1532
1533 /// Returns optional Data Page v2 compression ratio threshold for this column.
1534 fn data_page_v2_compression_ratio_threshold(&self) -> Option<f64> {
1535 self.data_page_v2_compression_ratio_threshold
1536 }
1537
1538 /// If bloom filter is enabled and NDV was not explicitly set, resolve it to the
1539 /// given `default_ndv` (typically derived from `max_row_group_row_count`).
1540 fn resolve_bloom_filter_ndv(&mut self, default_ndv: u64) {
1541 if !self.bloom_filter_ndv_is_set {
1542 if let Some(ref mut bf) = self.bloom_filter_properties {
1543 bf.ndv = default_ndv;
1544 }
1545 }
1546 }
1547}
1548
1549/// Reference counted reader properties.
1550pub type ReaderPropertiesPtr = Arc<ReaderProperties>;
1551
1552const DEFAULT_READ_BLOOM_FILTER: bool = false;
1553const DEFAULT_READ_PAGE_STATS: bool = false;
1554
1555/// Configuration settings for reading parquet files.
1556///
1557/// All properties are immutable and `Send` + `Sync`.
1558/// Use [`ReaderPropertiesBuilder`] to assemble these properties.
1559///
1560/// # Example
1561///
1562/// ```rust
1563/// use parquet::file::properties::ReaderProperties;
1564///
1565/// // Create properties with default configuration.
1566/// let props = ReaderProperties::builder().build();
1567///
1568/// // Use properties builder to set certain options and assemble the configuration.
1569/// let props = ReaderProperties::builder()
1570/// .set_backward_compatible_lz4(false)
1571/// .build();
1572/// ```
1573pub struct ReaderProperties {
1574 codec_options: CodecOptions,
1575 read_bloom_filter: bool,
1576 read_page_stats: bool,
1577}
1578
1579impl ReaderProperties {
1580 /// Returns builder for reader properties with default values.
1581 pub fn builder() -> ReaderPropertiesBuilder {
1582 ReaderPropertiesBuilder::with_defaults()
1583 }
1584
1585 /// Returns codec options.
1586 pub(crate) fn codec_options(&self) -> &CodecOptions {
1587 &self.codec_options
1588 }
1589
1590 /// Returns whether to read bloom filter
1591 pub(crate) fn read_bloom_filter(&self) -> bool {
1592 self.read_bloom_filter
1593 }
1594
1595 /// Returns whether to read page level statistics
1596 pub(crate) fn read_page_stats(&self) -> bool {
1597 self.read_page_stats
1598 }
1599}
1600
1601/// Builder for parquet file reader configuration. See example on
1602/// [`ReaderProperties`]
1603pub struct ReaderPropertiesBuilder {
1604 codec_options_builder: CodecOptionsBuilder,
1605 read_bloom_filter: Option<bool>,
1606 read_page_stats: Option<bool>,
1607}
1608
1609/// Reader properties builder.
1610impl ReaderPropertiesBuilder {
1611 /// Returns default state of the builder.
1612 fn with_defaults() -> Self {
1613 Self {
1614 codec_options_builder: CodecOptionsBuilder::default(),
1615 read_bloom_filter: None,
1616 read_page_stats: None,
1617 }
1618 }
1619
1620 /// Finalizes the configuration and returns immutable reader properties struct.
1621 pub fn build(self) -> ReaderProperties {
1622 ReaderProperties {
1623 codec_options: self.codec_options_builder.build(),
1624 read_bloom_filter: self.read_bloom_filter.unwrap_or(DEFAULT_READ_BLOOM_FILTER),
1625 read_page_stats: self.read_page_stats.unwrap_or(DEFAULT_READ_PAGE_STATS),
1626 }
1627 }
1628
1629 /// Enable/disable backward compatible LZ4.
1630 ///
1631 /// If backward compatible LZ4 is enable, on LZ4_HADOOP error it will fallback
1632 /// to the older versions LZ4 algorithms. That is LZ4_FRAME, for backward compatibility
1633 /// with files generated by older versions of this library, and LZ4_RAW, for backward
1634 /// compatibility with files generated by older versions of parquet-cpp.
1635 ///
1636 /// If backward compatible LZ4 is disabled, on LZ4_HADOOP error it will return the error.
1637 pub fn set_backward_compatible_lz4(mut self, value: bool) -> Self {
1638 self.codec_options_builder = self
1639 .codec_options_builder
1640 .set_backward_compatible_lz4(value);
1641 self
1642 }
1643
1644 /// Enable/disable reading bloom filter
1645 ///
1646 /// If reading bloom filter is enabled, bloom filter will be read from the file.
1647 /// If reading bloom filter is disabled, bloom filter will not be read from the file.
1648 ///
1649 /// By default bloom filter is set to be read.
1650 pub fn set_read_bloom_filter(mut self, value: bool) -> Self {
1651 self.read_bloom_filter = Some(value);
1652 self
1653 }
1654
1655 /// Enable/disable reading page-level statistics
1656 ///
1657 /// If set to `true`, then the reader will decode and populate the [`Statistics`] for
1658 /// each page, if present.
1659 /// If set to `false`, then the reader will skip decoding the statistics.
1660 ///
1661 /// By default statistics will not be decoded.
1662 ///
1663 /// [`Statistics`]: crate::file::statistics::Statistics
1664 pub fn set_read_page_statistics(mut self, value: bool) -> Self {
1665 self.read_page_stats = Some(value);
1666 self
1667 }
1668}
1669
1670#[cfg(test)]
1671mod tests {
1672 use super::*;
1673
1674 #[test]
1675 fn test_writer_version() {
1676 assert_eq!(WriterVersion::PARQUET_1_0.as_num(), 1);
1677 assert_eq!(WriterVersion::PARQUET_2_0.as_num(), 2);
1678 }
1679
1680 #[test]
1681 fn test_writer_properties_default_settings() {
1682 let props = WriterProperties::default();
1683 assert_eq!(props.data_page_size_limit(), DEFAULT_PAGE_SIZE);
1684 assert_eq!(
1685 props.dictionary_page_size_limit(),
1686 DEFAULT_DICTIONARY_PAGE_SIZE_LIMIT
1687 );
1688 assert_eq!(props.write_batch_size(), DEFAULT_WRITE_BATCH_SIZE);
1689 assert_eq!(
1690 props.max_row_group_row_count(),
1691 Some(DEFAULT_MAX_ROW_GROUP_ROW_COUNT)
1692 );
1693 assert_eq!(props.max_row_group_bytes(), None);
1694 assert_eq!(props.bloom_filter_position(), DEFAULT_BLOOM_FILTER_POSITION);
1695 assert_eq!(props.writer_version(), DEFAULT_WRITER_VERSION);
1696 assert_eq!(props.created_by(), DEFAULT_CREATED_BY);
1697 assert_eq!(props.key_value_metadata(), None);
1698 assert_eq!(props.encoding(&ColumnPath::from("col")), None);
1699 assert_eq!(
1700 props.compression(&ColumnPath::from("col")),
1701 DEFAULT_COMPRESSION
1702 );
1703 assert_eq!(
1704 props.dictionary_enabled(&ColumnPath::from("col")),
1705 DEFAULT_DICTIONARY_ENABLED
1706 );
1707 assert_eq!(
1708 props.statistics_enabled(&ColumnPath::from("col")),
1709 DEFAULT_STATISTICS_ENABLED
1710 );
1711 assert!(
1712 props
1713 .bloom_filter_properties(&ColumnPath::from("col"))
1714 .is_none()
1715 );
1716 }
1717
1718 #[test]
1719 fn test_writer_properties_dictionary_encoding() {
1720 // dictionary encoding is not configurable, and it should be the same for both
1721 // writer version 1 and 2.
1722 for version in &[WriterVersion::PARQUET_1_0, WriterVersion::PARQUET_2_0] {
1723 let props = WriterProperties::builder()
1724 .set_writer_version(*version)
1725 .build();
1726 assert_eq!(props.dictionary_page_encoding(), Encoding::PLAIN);
1727 assert_eq!(
1728 props.dictionary_data_page_encoding(),
1729 Encoding::RLE_DICTIONARY
1730 );
1731 }
1732 }
1733
1734 #[test]
1735 #[should_panic(expected = "Dictionary encoding can not be used as fallback encoding")]
1736 fn test_writer_properties_panic_when_plain_dictionary_is_fallback() {
1737 // Should panic when user specifies dictionary encoding as fallback encoding.
1738 WriterProperties::builder()
1739 .set_encoding(Encoding::PLAIN_DICTIONARY)
1740 .build();
1741 }
1742
1743 #[test]
1744 #[should_panic(expected = "Dictionary encoding can not be used as fallback encoding")]
1745 fn test_writer_properties_panic_when_rle_dictionary_is_fallback() {
1746 // Should panic when user specifies dictionary encoding as fallback encoding.
1747 WriterProperties::builder()
1748 .set_encoding(Encoding::RLE_DICTIONARY)
1749 .build();
1750 }
1751
1752 #[test]
1753 #[should_panic(expected = "Dictionary encoding can not be used as fallback encoding")]
1754 fn test_writer_properties_panic_when_dictionary_is_enabled() {
1755 WriterProperties::builder()
1756 .set_dictionary_enabled(true)
1757 .set_column_encoding(ColumnPath::from("col"), Encoding::RLE_DICTIONARY)
1758 .build();
1759 }
1760
1761 #[test]
1762 #[should_panic(expected = "Dictionary encoding can not be used as fallback encoding")]
1763 fn test_writer_properties_panic_when_dictionary_is_disabled() {
1764 WriterProperties::builder()
1765 .set_dictionary_enabled(false)
1766 .set_column_encoding(ColumnPath::from("col"), Encoding::RLE_DICTIONARY)
1767 .build();
1768 }
1769
1770 #[test]
1771 fn test_writer_properties_builder() {
1772 let props = WriterProperties::builder()
1773 // file settings
1774 .set_writer_version(WriterVersion::PARQUET_2_0)
1775 .set_data_page_size_limit(10)
1776 .set_dictionary_page_size_limit(20)
1777 .set_write_batch_size(30)
1778 .set_max_row_group_row_count(Some(40))
1779 .set_created_by("default".to_owned())
1780 .set_key_value_metadata(Some(vec![KeyValue::new(
1781 "key".to_string(),
1782 "value".to_string(),
1783 )]))
1784 // global column settings
1785 .set_encoding(Encoding::DELTA_BINARY_PACKED)
1786 .set_compression(Compression::GZIP(Default::default()))
1787 .set_dictionary_enabled(false)
1788 .set_statistics_enabled(EnabledStatistics::None)
1789 // specific column settings
1790 .set_column_encoding(ColumnPath::from("col"), Encoding::RLE)
1791 .set_column_compression(ColumnPath::from("col"), Compression::SNAPPY)
1792 .set_column_dictionary_enabled(ColumnPath::from("col"), true)
1793 .set_column_statistics_enabled(ColumnPath::from("col"), EnabledStatistics::Chunk)
1794 .set_column_bloom_filter_enabled(ColumnPath::from("col"), true)
1795 .set_column_bloom_filter_ndv(ColumnPath::from("col"), 100_u64)
1796 .set_column_bloom_filter_fpp(ColumnPath::from("col"), 0.1)
1797 .build();
1798
1799 fn test_props(props: &WriterProperties) {
1800 assert_eq!(props.writer_version(), WriterVersion::PARQUET_2_0);
1801 assert_eq!(props.data_page_size_limit(), 10);
1802 assert_eq!(props.dictionary_page_size_limit(), 20);
1803 assert_eq!(props.write_batch_size(), 30);
1804 assert_eq!(props.max_row_group_row_count(), Some(40));
1805 assert_eq!(props.created_by(), "default");
1806 assert_eq!(
1807 props.key_value_metadata(),
1808 Some(&vec![
1809 KeyValue::new("key".to_string(), "value".to_string(),)
1810 ])
1811 );
1812
1813 assert_eq!(
1814 props.encoding(&ColumnPath::from("a")),
1815 Some(Encoding::DELTA_BINARY_PACKED)
1816 );
1817 assert_eq!(
1818 props.compression(&ColumnPath::from("a")),
1819 Compression::GZIP(Default::default())
1820 );
1821 assert!(!props.dictionary_enabled(&ColumnPath::from("a")));
1822 assert_eq!(
1823 props.statistics_enabled(&ColumnPath::from("a")),
1824 EnabledStatistics::None
1825 );
1826
1827 assert_eq!(
1828 props.encoding(&ColumnPath::from("col")),
1829 Some(Encoding::RLE)
1830 );
1831 assert_eq!(
1832 props.compression(&ColumnPath::from("col")),
1833 Compression::SNAPPY
1834 );
1835 assert!(props.dictionary_enabled(&ColumnPath::from("col")));
1836 assert_eq!(
1837 props.statistics_enabled(&ColumnPath::from("col")),
1838 EnabledStatistics::Chunk
1839 );
1840 assert_eq!(
1841 props.bloom_filter_properties(&ColumnPath::from("col")),
1842 Some(&BloomFilterProperties { fpp: 0.1, ndv: 100 })
1843 );
1844 }
1845
1846 // Test direct build of properties
1847 test_props(&props);
1848
1849 // Test that into_builder() gives the same result
1850 let props_into_builder_and_back = props.into_builder().build();
1851 test_props(&props_into_builder_and_back);
1852 }
1853
1854 #[test]
1855 fn test_writer_properties_builder_partial_defaults() {
1856 let props = WriterProperties::builder()
1857 .set_encoding(Encoding::DELTA_BINARY_PACKED)
1858 .set_compression(Compression::GZIP(Default::default()))
1859 .set_bloom_filter_enabled(true)
1860 .set_column_encoding(ColumnPath::from("col"), Encoding::RLE)
1861 .build();
1862
1863 assert_eq!(
1864 props.encoding(&ColumnPath::from("col")),
1865 Some(Encoding::RLE)
1866 );
1867 assert_eq!(
1868 props.compression(&ColumnPath::from("col")),
1869 Compression::GZIP(Default::default())
1870 );
1871 assert_eq!(
1872 props.dictionary_enabled(&ColumnPath::from("col")),
1873 DEFAULT_DICTIONARY_ENABLED
1874 );
1875 assert_eq!(
1876 props.bloom_filter_properties(&ColumnPath::from("col")),
1877 Some(&BloomFilterProperties {
1878 fpp: DEFAULT_BLOOM_FILTER_FPP,
1879 ndv: DEFAULT_BLOOM_FILTER_NDV,
1880 })
1881 );
1882 }
1883
1884 #[test]
1885 #[allow(deprecated)]
1886 fn test_writer_properties_deprecated_max_row_group_size_still_works() {
1887 let props = WriterProperties::builder()
1888 .set_max_row_group_size(42)
1889 .build();
1890
1891 assert_eq!(props.max_row_group_row_count(), Some(42));
1892 assert_eq!(props.max_row_group_size(), 42);
1893 }
1894
1895 #[test]
1896 #[should_panic(expected = "Cannot have a 0 max row group row count")]
1897 fn test_writer_properties_panic_on_zero_row_group_row_count() {
1898 let _ = WriterProperties::builder().set_max_row_group_row_count(Some(0));
1899 }
1900
1901 #[test]
1902 #[should_panic(expected = "Cannot have a 0 max row group bytes")]
1903 fn test_writer_properties_panic_on_zero_row_group_bytes() {
1904 let _ = WriterProperties::builder().set_max_row_group_bytes(Some(0));
1905 }
1906
1907 #[test]
1908 fn test_writer_properties_bloom_filter_ndv_fpp_set() {
1909 assert_eq!(
1910 WriterProperties::builder()
1911 .build()
1912 .bloom_filter_properties(&ColumnPath::from("col")),
1913 None
1914 );
1915 assert_eq!(
1916 WriterProperties::builder()
1917 .set_bloom_filter_ndv(100)
1918 .build()
1919 .bloom_filter_properties(&ColumnPath::from("col")),
1920 Some(&BloomFilterProperties {
1921 fpp: DEFAULT_BLOOM_FILTER_FPP,
1922 ndv: 100,
1923 })
1924 );
1925 assert_eq!(
1926 WriterProperties::builder()
1927 .set_bloom_filter_fpp(0.1)
1928 .build()
1929 .bloom_filter_properties(&ColumnPath::from("col")),
1930 Some(&BloomFilterProperties {
1931 fpp: 0.1,
1932 ndv: DEFAULT_BLOOM_FILTER_NDV,
1933 })
1934 );
1935 }
1936
1937 #[test]
1938 fn test_writer_properties_column_data_page_v2_compression_ratio_threshold() {
1939 let props = WriterProperties::builder()
1940 .set_data_page_v2_compression_ratio_threshold(0.5)
1941 .set_column_data_page_v2_compression_ratio_threshold(ColumnPath::from("col"), 0.1)
1942 .build();
1943
1944 assert_eq!(props.data_page_v2_compression_ratio_threshold(), 0.5);
1945 assert_eq!(
1946 props.column_data_page_v2_compression_ratio_threshold(&ColumnPath::from("col")),
1947 0.1
1948 );
1949 assert_eq!(
1950 props.column_data_page_v2_compression_ratio_threshold(&ColumnPath::from("other")),
1951 0.5
1952 );
1953 }
1954
1955 #[test]
1956 #[should_panic(
1957 expected = "data_page_v2_compression_ratio_threshold must be a positive finite number"
1958 )]
1959 fn test_writer_properties_panic_on_invalid_data_page_v2_compression_ratio_threshold() {
1960 WriterProperties::builder()
1961 .set_data_page_v2_compression_ratio_threshold(0.0)
1962 .build();
1963 }
1964
1965 #[test]
1966 fn test_writer_properties_column_dictionary_page_size_limit() {
1967 let props = WriterProperties::builder()
1968 .set_dictionary_page_size_limit(100)
1969 .set_column_dictionary_page_size_limit(ColumnPath::from("col"), 10)
1970 .build();
1971
1972 assert_eq!(props.dictionary_page_size_limit(), 100);
1973 assert_eq!(
1974 props.column_dictionary_page_size_limit(&ColumnPath::from("col")),
1975 10
1976 );
1977 assert_eq!(
1978 props.column_dictionary_page_size_limit(&ColumnPath::from("other")),
1979 100
1980 );
1981 }
1982
1983 #[test]
1984 fn test_writer_properties_column_data_page_size_limit() {
1985 let props = WriterProperties::builder()
1986 .set_data_page_size_limit(100)
1987 .set_column_data_page_size_limit(ColumnPath::from("col"), 10)
1988 .build();
1989
1990 assert_eq!(props.data_page_size_limit(), 100);
1991 assert_eq!(
1992 props.column_data_page_size_limit(&ColumnPath::from("col")),
1993 10
1994 );
1995 assert_eq!(
1996 props.column_data_page_size_limit(&ColumnPath::from("other")),
1997 100
1998 );
1999 }
2000
2001 #[test]
2002 fn test_reader_properties_default_settings() {
2003 let props = ReaderProperties::builder().build();
2004
2005 let codec_options = CodecOptionsBuilder::default()
2006 .set_backward_compatible_lz4(true)
2007 .build();
2008
2009 assert_eq!(props.codec_options(), &codec_options);
2010 assert!(!props.read_bloom_filter());
2011 }
2012
2013 #[test]
2014 fn test_reader_properties_builder() {
2015 let props = ReaderProperties::builder()
2016 .set_backward_compatible_lz4(false)
2017 .build();
2018
2019 let codec_options = CodecOptionsBuilder::default()
2020 .set_backward_compatible_lz4(false)
2021 .build();
2022
2023 assert_eq!(props.codec_options(), &codec_options);
2024 }
2025
2026 #[test]
2027 fn test_parse_writerversion() {
2028 let mut writer_version = "PARQUET_1_0".parse::<WriterVersion>().unwrap();
2029 assert_eq!(writer_version, WriterVersion::PARQUET_1_0);
2030 writer_version = "PARQUET_2_0".parse::<WriterVersion>().unwrap();
2031 assert_eq!(writer_version, WriterVersion::PARQUET_2_0);
2032
2033 // test lowercase
2034 writer_version = "parquet_1_0".parse::<WriterVersion>().unwrap();
2035 assert_eq!(writer_version, WriterVersion::PARQUET_1_0);
2036
2037 // test invalid version
2038 match "PARQUET_-1_0".parse::<WriterVersion>() {
2039 Ok(_) => panic!("Should not be able to parse PARQUET_-1_0"),
2040 Err(e) => {
2041 assert_eq!(e, "Invalid writer version: PARQUET_-1_0");
2042 }
2043 }
2044 }
2045
2046 #[test]
2047 fn test_parse_enabledstatistics() {
2048 let mut enabled_statistics = "NONE".parse::<EnabledStatistics>().unwrap();
2049 assert_eq!(enabled_statistics, EnabledStatistics::None);
2050 enabled_statistics = "CHUNK".parse::<EnabledStatistics>().unwrap();
2051 assert_eq!(enabled_statistics, EnabledStatistics::Chunk);
2052 enabled_statistics = "PAGE".parse::<EnabledStatistics>().unwrap();
2053 assert_eq!(enabled_statistics, EnabledStatistics::Page);
2054
2055 // test lowercase
2056 enabled_statistics = "none".parse::<EnabledStatistics>().unwrap();
2057 assert_eq!(enabled_statistics, EnabledStatistics::None);
2058
2059 //test invalid statistics
2060 match "ChunkAndPage".parse::<EnabledStatistics>() {
2061 Ok(_) => panic!("Should not be able to parse ChunkAndPage"),
2062 Err(e) => {
2063 assert_eq!(e, "Invalid statistics arg: ChunkAndPage");
2064 }
2065 }
2066 }
2067
2068 #[test]
2069 fn test_cdc_options_equality() {
2070 let opts = CdcOptions::default();
2071 assert_eq!(opts, CdcOptions::default());
2072
2073 let custom = CdcOptions {
2074 min_chunk_size: 1024,
2075 max_chunk_size: 8192,
2076 norm_level: 1,
2077 };
2078 assert_eq!(custom, custom);
2079 assert_ne!(opts, custom);
2080 }
2081}