Skip to content
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

Add DataFusionError::Collection to return multiple DataFusionErrors #14439

Merged
merged 22 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 108 additions & 3 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
eliaperantoni marked this conversation as resolved.
Show resolved Hide resolved
// 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()),
}
}
Expand Down Expand Up @@ -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(_) => "",
}
}
Expand Down Expand Up @@ -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()),
}
}
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@eliaperantoni eliaperantoni Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't handle recursive Collections but I think that is ok

But then in a query like:

SELECT
    bad,
    bad
UNION
SELECT
    bad

You would get one DataFusionError::Collection and one DataFusionError::Plan whereas you could've gotten three DataFusionError::Plan. I think that's worth having this iter that's slightly more complicated.

}

pub struct DataFusionErrorBuilder(Vec<DataFusionError>);
Copy link
Contributor

Choose a reason for hiding this comment

The 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`.
Expand Down Expand Up @@ -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![
Copy link
Contributor

Choose a reason for hiding this comment

The 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");
}
}
15 changes: 9 additions & 6 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ fn get_valid_types_with_scalar_udf(
TypeSignature::UserDefined => match func.coerce_types(current_types) {
Ok(coerced_types) => Ok(vec![coerced_types]),
Err(e) => exec_err!(
"Function '{}' user-defined coercion failed with {e:?}",
func.name()
"Function '{}' user-defined coercion failed with {:?}",
func.name(),
e.strip_backtrace()
),
},
TypeSignature::OneOf(signatures) => {
Expand Down Expand Up @@ -304,8 +305,9 @@ fn get_valid_types_with_aggregate_udf(
Ok(coerced_types) => vec![coerced_types],
Err(e) => {
return exec_err!(
"Function '{}' user-defined coercion failed with {e:?}",
func.name()
"Function '{}' user-defined coercion failed with {:?}",
func.name(),
e.strip_backtrace()
)
}
},
Expand All @@ -332,8 +334,9 @@ fn get_valid_types_with_window_udf(
Ok(coerced_types) => vec![coerced_types],
Err(e) => {
return exec_err!(
"Function '{}' user-defined coercion failed with {e:?}",
func.name()
"Function '{}' user-defined coercion failed with {:?}",
func.name(),
e.strip_backtrace()
)
}
},
Expand Down
14 changes: 10 additions & 4 deletions datafusion/sql/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::utils::{
CheckColumnsSatisfyExprsPurpose,
};

use datafusion_common::error::DataFusionErrorBuilder;
use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion};
use datafusion_common::{not_impl_err, plan_err, Result};
use datafusion_common::{RecursionUnnestOption, UnnestOptions};
Expand Down Expand Up @@ -574,10 +575,15 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
empty_from: bool,
planner_context: &mut PlannerContext,
) -> Result<Vec<Expr>> {
projection
.into_iter()
.map(|expr| self.sql_select_to_rex(expr, plan, empty_from, planner_context))
.collect::<Result<Vec<Expr>>>()
let mut prepared_select_exprs = vec![];
let mut error_builder = DataFusionErrorBuilder::new();
for expr in projection {
match self.sql_select_to_rex(expr, plan, empty_from, planner_context) {
Ok(expr) => prepared_select_exprs.push(expr),
Err(err) => error_builder.add_error(err),
}
}
error_builder.error_or(prepared_select_exprs)
}

/// Generate a relational expression from a select SQL expression
Expand Down
20 changes: 17 additions & 3 deletions datafusion/sql/src/set_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// under the License.

use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{not_impl_err, plan_err, Diagnostic, Result, Span};
use datafusion_common::{
not_impl_err, plan_err, DataFusionError, Diagnostic, Result, Span,
};
use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};
use sqlparser::ast::{SetExpr, SetOperator, SetQuantifier, Spanned};

Expand All @@ -39,8 +41,19 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
} => {
let left_span = Span::try_from_sqlparser_span(left.span());
let right_span = Span::try_from_sqlparser_span(right.span());
let left_plan = self.set_expr_to_plan(*left, planner_context)?;
let right_plan = self.set_expr_to_plan(*right, planner_context)?;
let left_plan = self.set_expr_to_plan(*left, planner_context);
let right_plan = self.set_expr_to_plan(*right, planner_context);
let (left_plan, right_plan) = match (left_plan, right_plan) {
(Ok(left_plan), Ok(right_plan)) => (left_plan, right_plan),
(Err(left_err), Err(right_err)) => {
return Err(DataFusionError::Collection(vec![
left_err, right_err,
]));
}
(Err(err), _) | (_, Err(err)) => {
return Err(err);
}
};
self.validate_set_expr_num_of_columns(
op,
left_span,
Expand All @@ -49,6 +62,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
&right_plan,
set_expr_span,
)?;

self.set_operation_to_plan(op, left_plan, right_plan, set_quantifier)
}
SetExpr::Query(q) => self.query_to_plan(*q, planner_context),
Expand Down
59 changes: 59 additions & 0 deletions datafusion/sql/tests/cases/collection.rs
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.");
}
1 change: 1 addition & 0 deletions datafusion/sql/tests/cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
// specific language governing permissions and limitations
// under the License.

mod collection;
mod diagnostic;
mod plan_to_sql;
15 changes: 11 additions & 4 deletions datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4422,10 +4422,17 @@ fn plan_create_index() {
}
}

fn assert_field_not_found(err: DataFusionError, name: &str) {
let err = match err {
DataFusionError::Diagnostic(_, err) => *err,
err => err,
fn assert_field_not_found(mut err: DataFusionError, name: &str) {
let err = loop {
match err {
DataFusionError::Diagnostic(_, wrapped_err) => {
err = *wrapped_err;
}
DataFusionError::Collection(errs) => {
err = errs.into_iter().next().unwrap();
}
err => break err,
}
};
match err {
DataFusionError::SchemaError { .. } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())]
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 backtrace feature.

I have no idea why running with --complete didn't write the backtrace to the .slt files, I find no call to strip_backtrace and I don't know what in my PR made this necessary. But it works 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

The 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 slt) as the backtraces would include line numbers / call stacks that would change over time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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}")]
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/errors.slt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ SELECT
query error DataFusion error: Arrow error: Cast error: Cannot cast string 'foo' to value of Int64 type
create table foo as values (1), ('foo');

query error No function matches
query error user-defined coercion failed
select 1 group by substr('');

# Error in filter should be reported
Expand Down
Loading