1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use std::error;
use std::fmt::{Display, Formatter};
use std::io;
use std::result;
use crate::DFSchema;
use arrow::error::ArrowError;
#[cfg(feature = "avro")]
use avro_rs::Error as AvroError;
#[cfg(feature = "jit")]
use cranelift_module::ModuleError;
#[cfg(feature = "parquet")]
use parquet::errors::ParquetError;
use sqlparser::parser::ParserError;
pub type Result<T> = result::Result<T, DataFusionError>;
pub type GenericError = Box<dyn error::Error + Send + Sync>;
#[derive(Debug)]
pub enum DataFusionError {
ArrowError(ArrowError),
#[cfg(feature = "parquet")]
ParquetError(ParquetError),
#[cfg(feature = "avro")]
AvroError(AvroError),
IoError(io::Error),
SQL(ParserError),
NotImplemented(String),
Internal(String),
Plan(String),
SchemaError(SchemaError),
Execution(String),
ResourcesExhausted(String),
External(GenericError),
#[cfg(feature = "jit")]
JITError(ModuleError),
}
#[derive(Debug)]
pub enum SchemaError {
AmbiguousReference {
qualifier: Option<String>,
name: String,
},
DuplicateQualifiedField { qualifier: String, name: String },
DuplicateUnqualifiedField { name: String },
FieldNotFound {
qualifier: Option<String>,
name: String,
valid_fields: Option<Vec<String>>,
},
}
pub fn field_not_found(
qualifier: Option<String>,
name: &str,
schema: &DFSchema,
) -> DataFusionError {
DataFusionError::SchemaError(SchemaError::FieldNotFound {
qualifier,
name: name.to_string(),
valid_fields: Some(schema.field_names()),
})
}
impl Display for SchemaError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::FieldNotFound {
qualifier,
name,
valid_fields,
} => {
write!(f, "No field named ")?;
if let Some(q) = qualifier {
write!(f, "'{}.{}'", q, name)?;
} else {
write!(f, "'{}'", name)?;
}
if let Some(field_names) = valid_fields {
write!(
f,
". Valid fields are {}",
field_names
.iter()
.map(|name| format!("'{}'", name))
.collect::<Vec<String>>()
.join(", ")
)?;
}
write!(f, ".")
}
Self::DuplicateQualifiedField { qualifier, name } => {
write!(
f,
"Schema contains duplicate qualified field name '{}.{}'",
qualifier, name
)
}
Self::DuplicateUnqualifiedField { name } => {
write!(
f,
"Schema contains duplicate unqualified field name '{}'",
name
)
}
Self::AmbiguousReference { qualifier, name } => {
if let Some(q) = qualifier {
write!(f, "Schema contains qualified field name '{}.{}' and unqualified field name '{}' which would be ambiguous", q, name, name)
} else {
write!(f, "Ambiguous reference to unqualified field '{}'", name)
}
}
}
}
}
impl From<io::Error> for DataFusionError {
fn from(e: io::Error) -> Self {
DataFusionError::IoError(e)
}
}
impl From<ArrowError> for DataFusionError {
fn from(e: ArrowError) -> Self {
DataFusionError::ArrowError(e)
}
}
impl From<DataFusionError> for ArrowError {
fn from(e: DataFusionError) -> Self {
match e {
DataFusionError::ArrowError(e) => e,
DataFusionError::External(e) => ArrowError::ExternalError(e),
other => ArrowError::ExternalError(Box::new(other)),
}
}
}
#[cfg(feature = "parquet")]
impl From<ParquetError> for DataFusionError {
fn from(e: ParquetError) -> Self {
DataFusionError::ParquetError(e)
}
}
#[cfg(feature = "avro")]
impl From<AvroError> for DataFusionError {
fn from(e: AvroError) -> Self {
DataFusionError::AvroError(e)
}
}
impl From<ParserError> for DataFusionError {
fn from(e: ParserError) -> Self {
DataFusionError::SQL(e)
}
}
#[cfg(feature = "jit")]
impl From<ModuleError> for DataFusionError {
fn from(e: ModuleError) -> Self {
DataFusionError::JITError(e)
}
}
impl From<GenericError> for DataFusionError {
fn from(err: GenericError) -> Self {
DataFusionError::External(err)
}
}
impl Display for DataFusionError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match *self {
DataFusionError::ArrowError(ref desc) => write!(f, "Arrow error: {}", desc),
#[cfg(feature = "parquet")]
DataFusionError::ParquetError(ref desc) => {
write!(f, "Parquet error: {}", desc)
}
#[cfg(feature = "avro")]
DataFusionError::AvroError(ref desc) => {
write!(f, "Avro error: {}", desc)
}
DataFusionError::IoError(ref desc) => write!(f, "IO error: {}", desc),
DataFusionError::SQL(ref desc) => {
write!(f, "SQL error: {:?}", desc)
}
DataFusionError::NotImplemented(ref desc) => {
write!(f, "This feature is not implemented: {}", desc)
}
DataFusionError::Internal(ref desc) => {
write!(f, "Internal error: {}. This was likely caused by a bug in DataFusion's \
code and we would welcome that you file an bug report in our issue tracker", desc)
}
DataFusionError::Plan(ref desc) => {
write!(f, "Error during planning: {}", desc)
}
DataFusionError::SchemaError(ref desc) => {
write!(f, "Schema error: {}", desc)
}
DataFusionError::Execution(ref desc) => {
write!(f, "Execution error: {}", desc)
}
DataFusionError::ResourcesExhausted(ref desc) => {
write!(f, "Resources exhausted: {}", desc)
}
DataFusionError::External(ref desc) => {
write!(f, "External error: {}", desc)
}
#[cfg(feature = "jit")]
DataFusionError::JITError(ref desc) => {
write!(f, "JIT error: {}", desc)
}
}
}
}
impl error::Error for DataFusionError {}
#[cfg(test)]
mod test {
use crate::error::DataFusionError;
use arrow::error::ArrowError;
#[test]
fn arrow_error_to_datafusion() {
let res = return_arrow_error().unwrap_err();
assert_eq!(
res.to_string(),
"External error: Error during planning: foo"
);
}
#[test]
fn datafusion_error_to_arrow() {
let res = return_datafusion_error().unwrap_err();
assert_eq!(res.to_string(), "Arrow error: Schema error: bar");
}
#[allow(clippy::try_err)]
fn return_arrow_error() -> arrow::error::Result<()> {
let _foo = Err(DataFusionError::Plan("foo".to_string()))?;
Ok(())
}
#[allow(clippy::try_err)]
fn return_datafusion_error() -> crate::error::Result<()> {
let _bar = Err(ArrowError::SchemaError("bar".to_string()))?;
Ok(())
}
}
#[macro_export]
macro_rules! internal_err {
($($arg:tt)*) => {
Err(DataFusionError::Internal(format!($($arg)*)))
};
}