Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-parquet/src/parquet/error.rs
6940 views
1
//! Contains [`Error`]
2
3
/// List of features whose non-activation may cause a runtime error.
4
/// Used to indicate which lack of feature caused [`Error::FeatureNotActive`].
5
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6
#[non_exhaustive]
7
pub enum Feature {
8
/// Snappy compression and decompression
9
Snappy,
10
/// Brotli compression and decompression
11
Brotli,
12
/// Gzip compression and decompression
13
Gzip,
14
/// Lz4 raw compression and decompression
15
Lz4,
16
/// Zstd compression and decompression
17
Zstd,
18
}
19
20
/// Errors generated by this crate
21
#[derive(Debug, Clone)]
22
#[non_exhaustive]
23
pub enum ParquetError {
24
/// When the parquet file is known to be out of spec.
25
OutOfSpec(String),
26
/// Error presented when trying to use a code branch that requires activating a feature.
27
FeatureNotActive(Feature, String),
28
/// Error presented when trying to use a feature from parquet that is not yet supported
29
FeatureNotSupported(String),
30
/// When encoding, the user passed an invalid parameter
31
InvalidParameter(String),
32
/// When decoding or decompressing, the page would allocate more memory than allowed
33
WouldOverAllocate,
34
}
35
36
impl ParquetError {
37
/// Create an OutOfSpec error from any Into<String>
38
pub(crate) fn oos<I: Into<String>>(message: I) -> Self {
39
Self::OutOfSpec(message.into())
40
}
41
42
/// Create an FeatureNotSupported error from any Into<String>
43
pub(crate) fn not_supported<I: Into<String>>(message: I) -> Self {
44
Self::FeatureNotSupported(message.into())
45
}
46
}
47
48
impl std::error::Error for ParquetError {}
49
50
impl std::fmt::Display for ParquetError {
51
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
52
match self {
53
ParquetError::OutOfSpec(message) => {
54
write!(fmt, "File out of specification: {message}")
55
},
56
ParquetError::FeatureNotActive(feature, reason) => {
57
write!(
58
fmt,
59
"The feature \"{feature:?}\" needs to be active to {reason}"
60
)
61
},
62
ParquetError::FeatureNotSupported(reason) => {
63
write!(fmt, "Not yet supported: {reason}")
64
},
65
ParquetError::InvalidParameter(message) => {
66
write!(fmt, "Invalid parameter: {message}")
67
},
68
ParquetError::WouldOverAllocate => {
69
write!(fmt, "Operation would exceed memory use threshold")
70
},
71
}
72
}
73
}
74
75
#[cfg(feature = "snappy")]
76
impl From<snap::Error> for ParquetError {
77
fn from(e: snap::Error) -> ParquetError {
78
ParquetError::OutOfSpec(format!("underlying snap error: {e}"))
79
}
80
}
81
82
#[cfg(feature = "lz4_flex")]
83
impl From<lz4_flex::block::DecompressError> for ParquetError {
84
fn from(e: lz4_flex::block::DecompressError) -> ParquetError {
85
ParquetError::OutOfSpec(format!("underlying lz4_flex error: {e}"))
86
}
87
}
88
89
#[cfg(feature = "lz4_flex")]
90
impl From<lz4_flex::block::CompressError> for ParquetError {
91
fn from(e: lz4_flex::block::CompressError) -> ParquetError {
92
ParquetError::OutOfSpec(format!("underlying lz4_flex error: {e}"))
93
}
94
}
95
96
impl From<polars_parquet_format::thrift::Error> for ParquetError {
97
fn from(e: polars_parquet_format::thrift::Error) -> ParquetError {
98
ParquetError::OutOfSpec(format!("Invalid thrift: {e}"))
99
}
100
}
101
102
impl From<std::io::Error> for ParquetError {
103
fn from(e: std::io::Error) -> ParquetError {
104
ParquetError::OutOfSpec(format!("underlying IO error: {e}"))
105
}
106
}
107
108
impl From<std::collections::TryReserveError> for ParquetError {
109
fn from(e: std::collections::TryReserveError) -> ParquetError {
110
ParquetError::OutOfSpec(format!("OOM: {e}"))
111
}
112
}
113
114
impl From<std::num::TryFromIntError> for ParquetError {
115
fn from(e: std::num::TryFromIntError) -> ParquetError {
116
ParquetError::OutOfSpec(format!("Number must be zero or positive: {e}"))
117
}
118
}
119
120
impl From<std::array::TryFromSliceError> for ParquetError {
121
fn from(e: std::array::TryFromSliceError) -> ParquetError {
122
ParquetError::OutOfSpec(format!("Can't deserialize to parquet native type: {e}"))
123
}
124
}
125
126
/// A specialized `Result` for Parquet errors.
127
pub type ParquetResult<T> = std::result::Result<T, ParquetError>;
128
129
impl From<ParquetError> for polars_error::PolarsError {
130
fn from(e: ParquetError) -> polars_error::PolarsError {
131
polars_error::PolarsError::ComputeError(format!("parquet: {e}").into())
132
}
133
}
134
135
impl From<polars_error::PolarsError> for ParquetError {
136
fn from(e: polars_error::PolarsError) -> ParquetError {
137
ParquetError::OutOfSpec(format!("OOM: {e}"))
138
}
139
}
140
141