-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add DataFusionError::Collection
to return multiple DataFusionError
s
#14439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
184f7e6
ccf36af
1b287ed
ef71a85
8141f23
6ab8b36
4eae829
f642b82
9cd2e12
e2de823
fbe8b5d
5812715
eebf9e8
7b0e7f4
337ea50
28f351a
e3affd7
984d533
451bd71
d10c77b
415029f
b8cf6c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
use std::backtrace::{Backtrace, BacktraceStatus}; | ||
|
||
use std::borrow::Cow; | ||
use std::collections::VecDeque; | ||
use std::error::Error; | ||
use std::fmt::{Display, Formatter}; | ||
use std::io; | ||
|
@@ -136,6 +137,13 @@ pub enum DataFusionError { | |
/// human-readable messages, and locations in the source query that relate | ||
/// to the error in some way. | ||
Diagnostic(Box<Diagnostic>, Box<DataFusionError>), | ||
/// A collection of one or more [`DataFusionError`]. Useful in cases where | ||
/// DataFusion can recover from an erroneous state, and produce more errors | ||
/// before terminating. e.g. when planning a SELECT clause, DataFusion can | ||
/// synchronize to the next `SelectItem` if the previous one had errors. The | ||
/// end result is that the user can see errors about all `SelectItem`, | ||
/// instead of just the first one. | ||
Collection(Vec<DataFusionError>), | ||
/// A [`DataFusionError`] which shares an underlying [`DataFusionError`]. | ||
/// | ||
/// This is useful when the same underlying [`DataFusionError`] is passed | ||
|
@@ -360,6 +368,14 @@ impl Error for DataFusionError { | |
DataFusionError::Context(_, e) => Some(e.as_ref()), | ||
DataFusionError::Substrait(_) => None, | ||
DataFusionError::Diagnostic(_, e) => Some(e.as_ref()), | ||
// Can't really make a Collection fit into the mold of "an error has | ||
// at most one source", but returning the first one is probably good | ||
// idea. Especially since `DataFusionError::Collection` is mostly | ||
// meant for consumption by the end user, so shouldn't interfere | ||
// with programmatic usage too much. Plus, having 1 or 5 errors | ||
// doesn't really change the fact that the query is invalid and | ||
// can't be executed. | ||
DataFusionError::Collection(errs) => errs.first().map(|e| e as &dyn Error), | ||
DataFusionError::Shared(e) => Some(e.as_ref()), | ||
} | ||
} | ||
|
@@ -463,18 +479,27 @@ impl DataFusionError { | |
DataFusionError::ObjectStore(_) => "Object Store error: ", | ||
DataFusionError::IoError(_) => "IO error: ", | ||
DataFusionError::SQL(_, _) => "SQL error: ", | ||
DataFusionError::NotImplemented(_) => "This feature is not implemented: ", | ||
DataFusionError::NotImplemented(_) => { | ||
"This feature is not implemented: " | ||
} | ||
DataFusionError::Internal(_) => "Internal error: ", | ||
DataFusionError::Plan(_) => "Error during planning: ", | ||
DataFusionError::Configuration(_) => "Invalid or Unsupported Configuration: ", | ||
DataFusionError::Configuration(_) => { | ||
"Invalid or Unsupported Configuration: " | ||
} | ||
DataFusionError::SchemaError(_, _) => "Schema error: ", | ||
DataFusionError::Execution(_) => "Execution error: ", | ||
DataFusionError::ExecutionJoin(_) => "ExecutionJoin error: ", | ||
DataFusionError::ResourcesExhausted(_) => "Resources exhausted: ", | ||
DataFusionError::ResourcesExhausted(_) => { | ||
"Resources exhausted: " | ||
} | ||
DataFusionError::External(_) => "External error: ", | ||
DataFusionError::Context(_, _) => "", | ||
DataFusionError::Substrait(_) => "Substrait error: ", | ||
DataFusionError::Diagnostic(_, _) => "", | ||
DataFusionError::Collection(errs) => { | ||
errs.first().expect("cannot construct DataFusionError::Collection with 0 errors, but got one such case").error_prefix() | ||
} | ||
DataFusionError::Shared(_) => "", | ||
} | ||
} | ||
|
@@ -517,6 +542,13 @@ impl DataFusionError { | |
} | ||
DataFusionError::Substrait(ref desc) => Cow::Owned(desc.to_string()), | ||
DataFusionError::Diagnostic(_, ref err) => Cow::Owned(err.to_string()), | ||
// Returning the message of the first error is probably fine enough, | ||
// and makes `DataFusionError::Collection` a transparent wrapped, | ||
// unless the end user explicitly calls `DataFusionError::iter`. | ||
DataFusionError::Collection(ref errs) => errs | ||
.first() | ||
.expect("cannot construct DataFusionError::Collection with 0 errors") | ||
.message(), | ||
DataFusionError::Shared(ref desc) => Cow::Owned(desc.to_string()), | ||
} | ||
} | ||
|
@@ -569,6 +601,63 @@ impl DataFusionError { | |
|
||
DiagnosticsIterator { head: self }.next() | ||
} | ||
|
||
/// Sometimes DataFusion is able to collect multiple errors in a SQL query | ||
/// before terminating, e.g. across different expressions in a SELECT | ||
/// statements or different sides of a UNION. This method returns an | ||
/// iterator over all the errors in the collection. | ||
/// | ||
/// For this to work, the top-level error must be a | ||
/// `DataFusionError::Collection`, not something that contains it. | ||
pub fn iter(&self) -> impl Iterator<Item = &DataFusionError> { | ||
struct ErrorIterator<'a> { | ||
queue: VecDeque<&'a DataFusionError>, | ||
} | ||
|
||
impl<'a> Iterator for ErrorIterator<'a> { | ||
type Item = &'a DataFusionError; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
loop { | ||
let popped = self.queue.pop_front()?; | ||
match popped { | ||
DataFusionError::Collection(errs) => self.queue.extend(errs), | ||
_ => return Some(popped), | ||
} | ||
} | ||
} | ||
} | ||
|
||
let mut queue = VecDeque::new(); | ||
queue.push_back(self); | ||
ErrorIterator { queue } | ||
} | ||
Comment on lines
+612
to
+634
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can implement the same thing more simply via pub fn iter(&self) -> impl Iterator<Item = &DataFusionError> {
let mut current_err = self;
let errors: Vec<_> = match self {
DataFusionError::Collection(errs) => errs.iter().collect(),
_ => vec![self],
};
errors.into_iter()
} That doesn't handle recursive Collections but I think that is ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But then in a query like: SELECT
bad,
bad
UNION
SELECT
bad You would get one |
||
} | ||
|
||
pub struct DataFusionErrorBuilder(Vec<DataFusionError>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a follow on PR to add docs and examples: |
||
|
||
impl DataFusionErrorBuilder { | ||
pub fn new() -> Self { | ||
Self(Vec::new()) | ||
} | ||
|
||
pub fn add_error(&mut self, error: DataFusionError) { | ||
self.0.push(error); | ||
} | ||
|
||
pub fn error_or<T>(self, ok: T) -> Result<T, DataFusionError> { | ||
match self.0.len() { | ||
0 => Ok(ok), | ||
1 => Err(self.0.into_iter().next().expect("length matched 1")), | ||
_ => Err(DataFusionError::Collection(self.0)), | ||
} | ||
} | ||
} | ||
|
||
impl Default for DataFusionErrorBuilder { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
/// Unwrap an `Option` if possible. Otherwise return an `DataFusionError::Internal`. | ||
|
@@ -954,4 +1043,20 @@ mod test { | |
assert_eq!(e.strip_backtrace(), exp.strip_backtrace()); | ||
assert_eq!(std::mem::discriminant(e), std::mem::discriminant(&exp),) | ||
} | ||
|
||
#[test] | ||
fn test_iter() { | ||
let err = DataFusionError::Collection(vec![ | ||
DataFusionError::Plan("a".to_string()), | ||
DataFusionError::Collection(vec![ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 for the recursive check |
||
DataFusionError::Plan("b".to_string()), | ||
DataFusionError::Plan("c".to_string()), | ||
]), | ||
]); | ||
let errs = err.iter().collect::<Vec<_>>(); | ||
assert_eq!(errs.len(), 3); | ||
assert_eq!(errs[0].strip_backtrace(), "Error during planning: a"); | ||
assert_eq!(errs[1].strip_backtrace(), "Error during planning: b"); | ||
assert_eq!(errs[2].strip_backtrace(), "Error during planning: c"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use datafusion_common::{assert_contains, DataFusionError}; | ||
use datafusion_sql::planner::SqlToRel; | ||
use sqlparser::{dialect::GenericDialect, parser::Parser}; | ||
|
||
use crate::{MockContextProvider, MockSessionState}; | ||
|
||
fn do_query(sql: &'static str) -> DataFusionError { | ||
let dialect = GenericDialect {}; | ||
let statement = Parser::new(&dialect) | ||
.try_with_sql(sql) | ||
.expect("unable to create parser") | ||
.parse_statement() | ||
.expect("unable to parse query"); | ||
let state = MockSessionState::default(); | ||
let context = MockContextProvider { state }; | ||
let sql_to_rel = SqlToRel::new(&context); | ||
sql_to_rel | ||
.sql_statement_to_plan(statement) | ||
.expect_err("expected error") | ||
} | ||
|
||
#[test] | ||
fn test_collect_select_items() { | ||
let query = "SELECT first_namex, last_namex FROM person"; | ||
let error = do_query(query); | ||
let errors = error.iter().collect::<Vec<_>>(); | ||
assert_eq!(errors.len(), 2); | ||
assert!(errors[0] | ||
eliaperantoni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.to_string() | ||
.contains("No field named first_namex.")); | ||
assert_contains!(errors[1].to_string(), "No field named last_namex."); | ||
} | ||
|
||
#[test] | ||
fn test_collect_set_exprs() { | ||
let query = "SELECT first_namex FROM person UNION ALL SELECT last_namex FROM person"; | ||
let error = do_query(query); | ||
let errors = error.iter().collect::<Vec<_>>(); | ||
assert_eq!(errors.len(), 2); | ||
assert_contains!(errors[0].to_string(), "No field named first_namex."); | ||
assert_contains!(errors[1].to_string(), "No field named last_namex."); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ pub enum DFSqlLogicTestError { | |
#[error("SqlLogicTest error(from sqllogictest-rs crate): {0}")] | ||
SqlLogicTest(#[from] TestError), | ||
/// Error from datafusion | ||
#[error("DataFusion error: {0}")] | ||
#[error("DataFusion error: {}", .0.strip_backtrace())] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay so it turns out that the issue was that the expected error doesn't contain the backtrace but the actual one does. I couldn't reproduce locally at first because I wasn't using the I have no idea why running with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should add the backtrace to the errors in general (definitely not in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely. This PR doesn't change that, the backtraces are in the wrapped SchemaError and PlanError. I had to strip them away, don't know why that wasn't necessary before |
||
DataFusion(#[from] DataFusionError), | ||
/// Error returned when SQL is syntactically incorrect. | ||
#[error("SQL Parser error: {0}")] | ||
|
Uh oh!
There was an error while loading. Please reload this page.