use polars_error::PolarsResult;
use polars_parquet::write::{
BrotliLevel as BrotliLevelParquet, CompressionOptions, GzipLevel as GzipLevelParquet,
StatisticsOptions, ZstdLevel as ZstdLevelParquet,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ParquetWriteOptions {
pub compression: ParquetCompression,
pub statistics: StatisticsOptions,
pub row_group_size: Option<usize>,
pub data_pagesize_limit: Option<usize>,
pub maintain_order: bool,
}
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ParquetCompression {
Uncompressed,
Snappy,
Gzip(Option<GzipLevel>),
Lzo,
Brotli(Option<BrotliLevel>),
Zstd(Option<ZstdLevel>),
Lz4Raw,
}
impl Default for ParquetCompression {
fn default() -> Self {
Self::Zstd(None)
}
}
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GzipLevel(u8);
impl GzipLevel {
pub fn try_new(level: u8) -> PolarsResult<Self> {
GzipLevelParquet::try_new(level)?;
Ok(GzipLevel(level))
}
}
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BrotliLevel(u32);
impl BrotliLevel {
pub fn try_new(level: u32) -> PolarsResult<Self> {
BrotliLevelParquet::try_new(level)?;
Ok(BrotliLevel(level))
}
}
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ZstdLevel(i32);
impl ZstdLevel {
pub fn try_new(level: i32) -> PolarsResult<Self> {
ZstdLevelParquet::try_new(level)?;
Ok(ZstdLevel(level))
}
}
impl From<ParquetCompression> for CompressionOptions {
fn from(value: ParquetCompression) -> Self {
use ParquetCompression::*;
match value {
Uncompressed => CompressionOptions::Uncompressed,
Snappy => CompressionOptions::Snappy,
Gzip(level) => {
CompressionOptions::Gzip(level.map(|v| GzipLevelParquet::try_new(v.0).unwrap()))
},
Lzo => CompressionOptions::Lzo,
Brotli(level) => {
CompressionOptions::Brotli(level.map(|v| BrotliLevelParquet::try_new(v.0).unwrap()))
},
Lz4Raw => CompressionOptions::Lz4Raw,
Zstd(level) => {
CompressionOptions::Zstd(level.map(|v| ZstdLevelParquet::try_new(v.0).unwrap()))
},
}
}
}