@@ -82,7 +82,9 @@ pub enum DataFusionError {
82
82
Configuration ( String ) ,
83
83
/// This error happens with schema-related errors, such as schema inference not possible
84
84
/// and non-unique column names.
85
- SchemaError ( SchemaError ) ,
85
+ /// 2nd argument is for optional backtrace
86
+ /// Boxing the optional backtrace to prevent <https://rust-lang.github.io/rust-clippy/master/index.html#/result_large_err>
87
+ SchemaError ( SchemaError , Box < Option < String > > ) ,
86
88
/// Error returned during execution of the query.
87
89
/// Examples include files not found, errors in parsing certain types.
88
90
Execution ( String ) ,
@@ -125,34 +127,6 @@ pub enum SchemaError {
125
127
} ,
126
128
}
127
129
128
- /// Create a "field not found" DataFusion::SchemaError
129
- pub fn field_not_found < R : Into < OwnedTableReference > > (
130
- qualifier : Option < R > ,
131
- name : & str ,
132
- schema : & DFSchema ,
133
- ) -> DataFusionError {
134
- DataFusionError :: SchemaError ( SchemaError :: FieldNotFound {
135
- field : Box :: new ( Column :: new ( qualifier, name) ) ,
136
- valid_fields : schema
137
- . fields ( )
138
- . iter ( )
139
- . map ( |f| f. qualified_column ( ) )
140
- . collect ( ) ,
141
- } )
142
- }
143
-
144
- /// Convenience wrapper over [`field_not_found`] for when there is no qualifier
145
- pub fn unqualified_field_not_found ( name : & str , schema : & DFSchema ) -> DataFusionError {
146
- DataFusionError :: SchemaError ( SchemaError :: FieldNotFound {
147
- field : Box :: new ( Column :: new_unqualified ( name) ) ,
148
- valid_fields : schema
149
- . fields ( )
150
- . iter ( )
151
- . map ( |f| f. qualified_column ( ) )
152
- . collect ( ) ,
153
- } )
154
- }
155
-
156
130
impl Display for SchemaError {
157
131
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
158
132
match self {
@@ -298,7 +272,7 @@ impl Display for DataFusionError {
298
272
write ! ( f, "IO error: {desc}" )
299
273
}
300
274
DataFusionError :: SQL ( ref desc, ref backtrace) => {
301
- let backtrace = backtrace. clone ( ) . unwrap_or ( "" . to_owned ( ) ) ;
275
+ let backtrace: String = backtrace. clone ( ) . unwrap_or ( "" . to_owned ( ) ) ;
302
276
write ! ( f, "SQL error: {desc:?}{backtrace}" )
303
277
}
304
278
DataFusionError :: Configuration ( ref desc) => {
@@ -314,8 +288,10 @@ impl Display for DataFusionError {
314
288
DataFusionError :: Plan ( ref desc) => {
315
289
write ! ( f, "Error during planning: {desc}" )
316
290
}
317
- DataFusionError :: SchemaError ( ref desc) => {
318
- write ! ( f, "Schema error: {desc}" )
291
+ DataFusionError :: SchemaError ( ref desc, ref backtrace) => {
292
+ let backtrace: & str =
293
+ & backtrace. as_ref ( ) . clone ( ) . unwrap_or ( "" . to_owned ( ) ) ;
294
+ write ! ( f, "Schema error: {desc}{backtrace}" )
319
295
}
320
296
DataFusionError :: Execution ( ref desc) => {
321
297
write ! ( f, "Execution error: {desc}" )
@@ -356,7 +332,7 @@ impl Error for DataFusionError {
356
332
DataFusionError :: Internal ( _) => None ,
357
333
DataFusionError :: Configuration ( _) => None ,
358
334
DataFusionError :: Plan ( _) => None ,
359
- DataFusionError :: SchemaError ( e) => Some ( e) ,
335
+ DataFusionError :: SchemaError ( e, _ ) => Some ( e) ,
360
336
DataFusionError :: Execution ( _) => None ,
361
337
DataFusionError :: ResourcesExhausted ( _) => None ,
362
338
DataFusionError :: External ( e) => Some ( e. as_ref ( ) ) ,
@@ -556,12 +532,63 @@ macro_rules! arrow_err {
556
532
} ;
557
533
}
558
534
535
+ // Exposes a macro to create `DataFusionError::SchemaError` with optional backtrace
536
+ #[ macro_export]
537
+ macro_rules! schema_datafusion_err {
538
+ ( $ERR: expr) => {
539
+ DataFusionError :: SchemaError (
540
+ $ERR,
541
+ Box :: new( Some ( DataFusionError :: get_back_trace( ) ) ) ,
542
+ )
543
+ } ;
544
+ }
545
+
546
+ // Exposes a macro to create `Err(DataFusionError::SchemaError)` with optional backtrace
547
+ #[ macro_export]
548
+ macro_rules! schema_err {
549
+ ( $ERR: expr) => {
550
+ Err ( DataFusionError :: SchemaError (
551
+ $ERR,
552
+ Box :: new( Some ( DataFusionError :: get_back_trace( ) ) ) ,
553
+ ) )
554
+ } ;
555
+ }
556
+
559
557
// To avoid compiler error when using macro in the same crate:
560
558
// macros from the current crate cannot be referred to by absolute paths
561
559
pub use internal_datafusion_err as _internal_datafusion_err;
562
560
pub use internal_err as _internal_err;
563
561
pub use not_impl_err as _not_impl_err;
564
562
pub use plan_err as _plan_err;
563
+ pub use schema_err as _schema_err;
564
+
565
+ /// Create a "field not found" DataFusion::SchemaError
566
+ pub fn field_not_found < R : Into < OwnedTableReference > > (
567
+ qualifier : Option < R > ,
568
+ name : & str ,
569
+ schema : & DFSchema ,
570
+ ) -> DataFusionError {
571
+ schema_datafusion_err ! ( SchemaError :: FieldNotFound {
572
+ field: Box :: new( Column :: new( qualifier, name) ) ,
573
+ valid_fields: schema
574
+ . fields( )
575
+ . iter( )
576
+ . map( |f| f. qualified_column( ) )
577
+ . collect( ) ,
578
+ } )
579
+ }
580
+
581
+ /// Convenience wrapper over [`field_not_found`] for when there is no qualifier
582
+ pub fn unqualified_field_not_found ( name : & str , schema : & DFSchema ) -> DataFusionError {
583
+ schema_datafusion_err ! ( SchemaError :: FieldNotFound {
584
+ field: Box :: new( Column :: new_unqualified( name) ) ,
585
+ valid_fields: schema
586
+ . fields( )
587
+ . iter( )
588
+ . map( |f| f. qualified_column( ) )
589
+ . collect( ) ,
590
+ } )
591
+ }
565
592
566
593
#[ cfg( test) ]
567
594
mod test {
0 commit comments