parquet/
errors.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//! Common Parquet errors and macros.
19
20use core::num::TryFromIntError;
21use std::error::Error;
22use std::{cell, io, result, str};
23
24#[cfg(feature = "arrow")]
25use arrow_schema::ArrowError;
26
27/// Parquet error enumeration
28// Note: we don't implement PartialEq as the semantics for the
29// external variant are not well defined (#4469)
30#[derive(Debug)]
31#[non_exhaustive]
32pub enum ParquetError {
33    /// General Parquet error.
34    /// Returned when code violates normal workflow of working with Parquet files.
35    General(String),
36    /// "Not yet implemented" Parquet error.
37    /// Returned when functionality is not yet available.
38    NYI(String),
39    /// "End of file" Parquet error.
40    /// Returned when IO related failures occur, e.g. when there are not enough bytes to
41    /// decode.
42    EOF(String),
43    #[cfg(feature = "arrow")]
44    /// Arrow error.
45    /// Returned when reading into arrow or writing from arrow.
46    ArrowError(String),
47    /// Error when the requested column index is more than the
48    /// number of columns in the row group
49    IndexOutOfBound(usize, usize),
50    /// An external error variant
51    External(Box<dyn Error + Send + Sync>),
52    /// Returned when a function needs more data to complete properly. The `usize` field indicates
53    /// the total number of bytes required, not the number of additional bytes.
54    NeedMoreData(usize),
55}
56
57impl std::fmt::Display for ParquetError {
58    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
59        match &self {
60            ParquetError::General(message) => {
61                write!(fmt, "Parquet error: {message}")
62            }
63            ParquetError::NYI(message) => write!(fmt, "NYI: {message}"),
64            ParquetError::EOF(message) => write!(fmt, "EOF: {message}"),
65            #[cfg(feature = "arrow")]
66            ParquetError::ArrowError(message) => write!(fmt, "Arrow: {message}"),
67            ParquetError::IndexOutOfBound(index, ref bound) => {
68                write!(fmt, "Index {index} out of bound: {bound}")
69            }
70            ParquetError::External(e) => write!(fmt, "External: {e}"),
71            ParquetError::NeedMoreData(needed) => write!(fmt, "NeedMoreData: {needed}"),
72        }
73    }
74}
75
76impl Error for ParquetError {
77    fn source(&self) -> Option<&(dyn Error + 'static)> {
78        match self {
79            ParquetError::External(e) => Some(e.as_ref()),
80            _ => None,
81        }
82    }
83}
84
85impl From<TryFromIntError> for ParquetError {
86    fn from(e: TryFromIntError) -> ParquetError {
87        ParquetError::General(format!("Integer overflow: {e}"))
88    }
89}
90
91impl From<io::Error> for ParquetError {
92    fn from(e: io::Error) -> ParquetError {
93        ParquetError::External(Box::new(e))
94    }
95}
96
97#[cfg(any(feature = "snap", test))]
98impl From<snap::Error> for ParquetError {
99    fn from(e: snap::Error) -> ParquetError {
100        ParquetError::External(Box::new(e))
101    }
102}
103
104impl From<thrift::Error> for ParquetError {
105    fn from(e: thrift::Error) -> ParquetError {
106        ParquetError::External(Box::new(e))
107    }
108}
109
110impl From<cell::BorrowMutError> for ParquetError {
111    fn from(e: cell::BorrowMutError) -> ParquetError {
112        ParquetError::External(Box::new(e))
113    }
114}
115
116impl From<str::Utf8Error> for ParquetError {
117    fn from(e: str::Utf8Error) -> ParquetError {
118        ParquetError::External(Box::new(e))
119    }
120}
121#[cfg(feature = "arrow")]
122impl From<ArrowError> for ParquetError {
123    fn from(e: ArrowError) -> ParquetError {
124        ParquetError::External(Box::new(e))
125    }
126}
127
128#[cfg(feature = "object_store")]
129impl From<object_store::Error> for ParquetError {
130    fn from(e: object_store::Error) -> ParquetError {
131        ParquetError::External(Box::new(e))
132    }
133}
134
135/// A specialized `Result` for Parquet errors.
136pub type Result<T, E = ParquetError> = result::Result<T, E>;
137
138// ----------------------------------------------------------------------
139// Conversion from `ParquetError` to other types of `Error`s
140
141impl From<ParquetError> for io::Error {
142    fn from(e: ParquetError) -> Self {
143        io::Error::new(io::ErrorKind::Other, e)
144    }
145}
146
147// ----------------------------------------------------------------------
148// Convenient macros for different errors
149
150macro_rules! general_err {
151    ($fmt:expr) => (ParquetError::General($fmt.to_owned()));
152    ($fmt:expr, $($args:expr),*) => (ParquetError::General(format!($fmt, $($args),*)));
153    ($e:expr, $fmt:expr) => (ParquetError::General($fmt.to_owned(), $e));
154    ($e:ident, $fmt:expr, $($args:tt),*) => (
155        ParquetError::General(&format!($fmt, $($args),*), $e));
156}
157
158macro_rules! nyi_err {
159    ($fmt:expr) => (ParquetError::NYI($fmt.to_owned()));
160    ($fmt:expr, $($args:expr),*) => (ParquetError::NYI(format!($fmt, $($args),*)));
161}
162
163macro_rules! eof_err {
164    ($fmt:expr) => (ParquetError::EOF($fmt.to_owned()));
165    ($fmt:expr, $($args:expr),*) => (ParquetError::EOF(format!($fmt, $($args),*)));
166}
167
168#[cfg(feature = "arrow")]
169macro_rules! arrow_err {
170    ($fmt:expr) => (ParquetError::ArrowError($fmt.to_owned()));
171    ($fmt:expr, $($args:expr),*) => (ParquetError::ArrowError(format!($fmt, $($args),*)));
172    ($e:expr, $fmt:expr) => (ParquetError::ArrowError($fmt.to_owned(), $e));
173    ($e:ident, $fmt:expr, $($args:tt),*) => (
174        ParquetError::ArrowError(&format!($fmt, $($args),*), $e));
175}
176
177// ----------------------------------------------------------------------
178// Convert parquet error into other errors
179
180#[cfg(feature = "arrow")]
181impl From<ParquetError> for ArrowError {
182    fn from(p: ParquetError) -> Self {
183        Self::ParquetError(format!("{p}"))
184    }
185}