Skip to main content

pq_glue/
error.rs

1//! The errors [`concat`](crate::concat()) returns.
2
3use std::fmt;
4use std::io;
5use std::path::PathBuf;
6
7use parquet::errors::ParquetError;
8
9/// What [`concat`](crate::concat()) and the footer checks can fail with.
10#[derive(Debug)]
11pub enum Error {
12    /// No input files were given.
13    NoInputs,
14    /// The output path is one of the inputs.
15    OutputIsInput(PathBuf),
16    /// An input's Parquet schema, key-value metadata or column orders differ from the
17    /// first input's.
18    Incompatible {
19        /// The input that differs.
20        path: PathBuf,
21        /// The first input, which the others must match.
22        first: PathBuf,
23        /// What differs: "schema", "key-value metadata" or "column orders".
24        what: &'static str,
25    },
26    /// A file could not be opened, read or written.
27    Io {
28        /// The file.
29        path: PathBuf,
30        /// The underlying error.
31        source: io::Error,
32    },
33    /// A file's Parquet metadata could not be read, or the output could not be written.
34    Parquet {
35        /// The file.
36        path: PathBuf,
37        /// The underlying error.
38        source: ParquetError,
39    },
40    /// A row group asked for that the file does not have.
41    RowGroup {
42        /// The file.
43        path: PathBuf,
44        /// The row group asked for.
45        row_group: usize,
46        /// The file's row groups.
47        row_groups: usize,
48    },
49    /// The thread pool for a write could not be made.
50    Pool(String),
51    /// A write's options do not fit its data: a column to encode that the schema does
52    /// not have, or an encoding its type does not take.
53    Option(String),
54    /// The footer parquet-rs wrote was not laid out as expected, so its column orders
55    /// were not rewritten (see [`footer::restore_column_orders`](crate::footer::restore_column_orders)).
56    Footer {
57        /// The output file.
58        path: PathBuf,
59        /// What was unexpected.
60        reason: String,
61    },
62}
63
64/// A `Result` with this crate's [`Error`].
65pub type Result<T> = std::result::Result<T, Error>;
66
67impl Error {
68    pub(crate) fn io(path: impl Into<PathBuf>) -> impl FnOnce(io::Error) -> Self {
69        let path = path.into();
70        move |source| Error::Io { path, source }
71    }
72
73    pub(crate) fn parquet(path: impl Into<PathBuf>) -> impl FnOnce(ParquetError) -> Self {
74        let path = path.into();
75        move |source| Error::Parquet { path, source }
76    }
77
78    pub(crate) fn footer(path: impl Into<PathBuf>, reason: impl Into<String>) -> Self {
79        Error::Footer {
80            path: path.into(),
81            reason: reason.into(),
82        }
83    }
84}
85
86impl fmt::Display for Error {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self {
89            Error::NoInputs => write!(f, "no input files"),
90            Error::OutputIsInput(p) => write!(f, "the output {} is one of the inputs", p.display()),
91            Error::Incompatible { path, first, what } => {
92                write!(
93                    f,
94                    "{} has other {what} than {}",
95                    path.display(),
96                    first.display()
97                )
98            }
99            Error::Io { path, source } => write!(f, "{}: {source}", path.display()),
100            Error::Parquet { path, source } => write!(f, "{}: {source}", path.display()),
101            Error::RowGroup {
102                path,
103                row_group,
104                row_groups,
105            } => write!(
106                f,
107                "{}: no row group {row_group} (the file has {row_groups})",
108                path.display()
109            ),
110            Error::Pool(reason) => write!(f, "no thread pool for the write: {reason}"),
111            Error::Option(reason) => write!(f, "{reason}"),
112            Error::Footer { path, reason } => {
113                write!(
114                    f,
115                    "{}: column orders not restored: {reason}",
116                    path.display()
117                )
118            }
119        }
120    }
121}
122
123impl std::error::Error for Error {
124    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
125        match self {
126            Error::Io { source, .. } => Some(source),
127            Error::Parquet { source, .. } => Some(source),
128            _ => None,
129        }
130    }
131}