Path: blob/main/crates/polars-parquet/src/parquet/error.rs
6940 views
//! Contains [`Error`]12/// List of features whose non-activation may cause a runtime error.3/// Used to indicate which lack of feature caused [`Error::FeatureNotActive`].4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]5#[non_exhaustive]6pub enum Feature {7/// Snappy compression and decompression8Snappy,9/// Brotli compression and decompression10Brotli,11/// Gzip compression and decompression12Gzip,13/// Lz4 raw compression and decompression14Lz4,15/// Zstd compression and decompression16Zstd,17}1819/// Errors generated by this crate20#[derive(Debug, Clone)]21#[non_exhaustive]22pub enum ParquetError {23/// When the parquet file is known to be out of spec.24OutOfSpec(String),25/// Error presented when trying to use a code branch that requires activating a feature.26FeatureNotActive(Feature, String),27/// Error presented when trying to use a feature from parquet that is not yet supported28FeatureNotSupported(String),29/// When encoding, the user passed an invalid parameter30InvalidParameter(String),31/// When decoding or decompressing, the page would allocate more memory than allowed32WouldOverAllocate,33}3435impl ParquetError {36/// Create an OutOfSpec error from any Into<String>37pub(crate) fn oos<I: Into<String>>(message: I) -> Self {38Self::OutOfSpec(message.into())39}4041/// Create an FeatureNotSupported error from any Into<String>42pub(crate) fn not_supported<I: Into<String>>(message: I) -> Self {43Self::FeatureNotSupported(message.into())44}45}4647impl std::error::Error for ParquetError {}4849impl std::fmt::Display for ParquetError {50fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {51match self {52ParquetError::OutOfSpec(message) => {53write!(fmt, "File out of specification: {message}")54},55ParquetError::FeatureNotActive(feature, reason) => {56write!(57fmt,58"The feature \"{feature:?}\" needs to be active to {reason}"59)60},61ParquetError::FeatureNotSupported(reason) => {62write!(fmt, "Not yet supported: {reason}")63},64ParquetError::InvalidParameter(message) => {65write!(fmt, "Invalid parameter: {message}")66},67ParquetError::WouldOverAllocate => {68write!(fmt, "Operation would exceed memory use threshold")69},70}71}72}7374#[cfg(feature = "snappy")]75impl From<snap::Error> for ParquetError {76fn from(e: snap::Error) -> ParquetError {77ParquetError::OutOfSpec(format!("underlying snap error: {e}"))78}79}8081#[cfg(feature = "lz4_flex")]82impl From<lz4_flex::block::DecompressError> for ParquetError {83fn from(e: lz4_flex::block::DecompressError) -> ParquetError {84ParquetError::OutOfSpec(format!("underlying lz4_flex error: {e}"))85}86}8788#[cfg(feature = "lz4_flex")]89impl From<lz4_flex::block::CompressError> for ParquetError {90fn from(e: lz4_flex::block::CompressError) -> ParquetError {91ParquetError::OutOfSpec(format!("underlying lz4_flex error: {e}"))92}93}9495impl From<polars_parquet_format::thrift::Error> for ParquetError {96fn from(e: polars_parquet_format::thrift::Error) -> ParquetError {97ParquetError::OutOfSpec(format!("Invalid thrift: {e}"))98}99}100101impl From<std::io::Error> for ParquetError {102fn from(e: std::io::Error) -> ParquetError {103ParquetError::OutOfSpec(format!("underlying IO error: {e}"))104}105}106107impl From<std::collections::TryReserveError> for ParquetError {108fn from(e: std::collections::TryReserveError) -> ParquetError {109ParquetError::OutOfSpec(format!("OOM: {e}"))110}111}112113impl From<std::num::TryFromIntError> for ParquetError {114fn from(e: std::num::TryFromIntError) -> ParquetError {115ParquetError::OutOfSpec(format!("Number must be zero or positive: {e}"))116}117}118119impl From<std::array::TryFromSliceError> for ParquetError {120fn from(e: std::array::TryFromSliceError) -> ParquetError {121ParquetError::OutOfSpec(format!("Can't deserialize to parquet native type: {e}"))122}123}124125/// A specialized `Result` for Parquet errors.126pub type ParquetResult<T> = std::result::Result<T, ParquetError>;127128impl From<ParquetError> for polars_error::PolarsError {129fn from(e: ParquetError) -> polars_error::PolarsError {130polars_error::PolarsError::ComputeError(format!("parquet: {e}").into())131}132}133134impl From<polars_error::PolarsError> for ParquetError {135fn from(e: polars_error::PolarsError) -> ParquetError {136ParquetError::OutOfSpec(format!("OOM: {e}"))137}138}139140141