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

Fix: Avoid recursive external error wrapping #14371

Merged
merged 16 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
57 changes: 55 additions & 2 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ pub enum DataFusionError {
/// Errors from either mapping LogicalPlans to/from Substrait plans
/// or serializing/deserializing protobytes to Substrait plans
Substrait(String),

/// Errors for wrapping other errors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed (compared to Context for example?) I don't understand why we need to keep the data wrappeed (why don't we just pass the underlying error back?)

Copy link
Contributor Author

@getChan getChan Feb 1, 2025

Choose a reason for hiding this comment

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

Because if we only pass the underlying error, we can't share the error (due to ownership constraints). For example, RepartitionExec needs to send errors to all output partitions. You can see this in the source code.

Ok(Err(e)) => {
let e = Arc::new(e);
for (_, tx) in txs {
// wrap it because need to send error to all output partitions
let err = Err(DataFusionError::External(Box::new(Arc::clone(&e))));
tx.send(Some(err)).await.ok();
}

To share the error, I considered the following.

  1. Implement Clone for DataFusionError and use err.clone(): This is not possible because nested Error types do not implement theClone trait.
  2. Pass Arc<DatafusionError> : I implemented it this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If using Context, the underlying error is only passed as a single Result due to ownership constraints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I renamed it to SharedError

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you @getChan -- As DataFusionError is used all over the place and is a very visible part of the API I was thinking it would be best if we can avoid adding too many variants. However, after playing with some other options (like deep cloning the error) I agree that this solution is the best.

to be consistent with the other variants which don't have a Error in their name, I renamed this to DataFusionError::Shared

/// This is useful when we need to share DatafusionError.
WrappedError(Arc<DataFusionError>),
}

#[macro_export]
Expand Down Expand Up @@ -293,7 +297,16 @@ impl From<ParserError> for DataFusionError {

impl From<GenericError> for DataFusionError {
fn from(err: GenericError) -> Self {
DataFusionError::External(err)
// If the error is already a DataFusionError, not wrapping it.
if err.is::<DataFusionError>() {
if let Ok(e) = err.downcast::<DataFusionError>() {
*e
} else {
unreachable!()
}
} else {
DataFusionError::External(err)
}
}
}

Expand Down Expand Up @@ -328,6 +341,7 @@ impl Error for DataFusionError {
DataFusionError::External(e) => Some(e.as_ref()),
DataFusionError::Context(_, e) => Some(e.as_ref()),
DataFusionError::Substrait(_) => None,
DataFusionError::WrappedError(e) => Some(e.as_ref()),
}
}
}
Expand Down Expand Up @@ -441,6 +455,7 @@ impl DataFusionError {
DataFusionError::External(_) => "External error: ",
DataFusionError::Context(_, _) => "",
DataFusionError::Substrait(_) => "Substrait error: ",
DataFusionError::WrappedError(_) => "",
}
}

Expand Down Expand Up @@ -481,6 +496,7 @@ impl DataFusionError {
Cow::Owned(format!("{desc}\ncaused by\n{}", *err))
}
DataFusionError::Substrait(ref desc) => Cow::Owned(desc.to_string()),
DataFusionError::WrappedError(ref desc) => Cow::Owned(desc.to_string()),
}
}
}
Expand Down Expand Up @@ -656,7 +672,7 @@ pub fn unqualified_field_not_found(name: &str, schema: &DFSchema) -> DataFusionE
mod test {
use std::sync::Arc;

use crate::error::DataFusionError;
use crate::error::{DataFusionError, GenericError};
use arrow::error::ArrowError;

#[test]
Expand Down Expand Up @@ -810,6 +826,43 @@ mod test {
);
}

#[test]
fn external_error() {
// assert not wrapping DataFusionError
let generic_error: GenericError =
Box::new(DataFusionError::Plan("test".to_string()));
let datafusion_error: DataFusionError = generic_error.into();
println!("{}", datafusion_error.strip_backtrace());
assert_eq!(
datafusion_error.strip_backtrace(),
"Error during planning: test"
);

// assert wrapping other Error
let generic_error: GenericError =
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "io error"));
let datafusion_error: DataFusionError = generic_error.into();
println!("{}", datafusion_error.strip_backtrace());
assert_eq!(
datafusion_error.strip_backtrace(),
"External error: io error"
);
}

#[test]
fn external_error_no_recursive() {
let generic_error_1: GenericError =
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "io error"));
let external_error_1: DataFusionError = generic_error_1.into();
let generic_error_2: GenericError = Box::new(external_error_1);
let external_error_2: DataFusionError = generic_error_2.into();

println!("{}", external_error_2);
assert!(external_error_2
.to_string()
.starts_with("External error: io error"));
}

/// Model what happens when implementing SendableRecordBatchStream:
/// DataFusion code needs to return an ArrowError
fn return_arrow_error() -> arrow::error::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/joins/cross_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ mod tests {

assert_contains!(
err.to_string(),
"External error: Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: CrossJoinExec"
"Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: CrossJoinExec"
);

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions datafusion/physical-plan/src/joins/hash_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4014,7 +4014,7 @@ mod tests {
// Asserting that operator-level reservation attempting to overallocate
assert_contains!(
err.to_string(),
"External error: Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: HashJoinInput"
"Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: HashJoinInput"
);

assert_contains!(
Expand Down Expand Up @@ -4099,7 +4099,7 @@ mod tests {
// Asserting that stream-level reservation attempting to overallocate
assert_contains!(
err.to_string(),
"External error: Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: HashJoinInput[1]"
"Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: HashJoinInput[1]"

);

Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/joins/nested_loop_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ pub(crate) mod tests {

assert_contains!(
err.to_string(),
"External error: Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: NestedLoopJoinLoad[0]"
"Resources exhausted: Additional allocation failed with top memory consumers (across reservations) as: NestedLoopJoinLoad[0]"
);
}

Expand Down
9 changes: 4 additions & 5 deletions datafusion/physical-plan/src/joins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ impl<T: 'static> OnceFut<T> {
OnceFutState::Ready(r) => Poll::Ready(
r.as_ref()
.map(|r| r.as_ref())
.map_err(|e| DataFusionError::External(Box::new(Arc::clone(e)))),
.map_err(|e| DataFusionError::WrappedError(Arc::clone(e))),
),
}
}
Expand All @@ -1090,10 +1090,9 @@ impl<T: 'static> OnceFut<T> {

match &self.state {
OnceFutState::Pending(_) => unreachable!(),
OnceFutState::Ready(r) => Poll::Ready(
r.clone()
.map_err(|e| DataFusionError::External(Box::new(e))),
),
OnceFutState::Ready(r) => {
Poll::Ready(r.clone().map_err(DataFusionError::WrappedError))
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/physical-plan/src/repartition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ impl RepartitionExec {

for (_, tx) in txs {
// wrap it because need to send error to all output partitions
let err = Err(DataFusionError::External(Box::new(Arc::clone(&e))));
let err = Err(DataFusionError::WrappedError(Arc::clone(&e)));
tx.send(Some(err)).await.ok();
}
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/sqllogictest/test_files/aggregate.slt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ statement error DataFusion error: Error during planning: Failed to coerce argume
SELECT approx_percentile_cont_with_weight(c3, c2, c1) FROM aggregate_test_100

# csv_query_approx_percentile_cont_with_histogram_bins
statement error DataFusion error: External error: This feature is not implemented: Tdigest max_size value for 'APPROX_PERCENTILE_CONT' must be UInt > 0 literal \(got data type Int64\)\.
statement error DataFusion error: This feature is not implemented: Tdigest max_size value for 'APPROX_PERCENTILE_CONT' must be UInt > 0 literal \(got data type Int64\)\.
SELECT c1, approx_percentile_cont(c3, 0.95, -1000) AS c3_p95 FROM aggregate_test_100 GROUP BY 1 ORDER BY 1

statement error DataFusion error: Error during planning: Failed to coerce arguments to satisfy a call to approx_percentile_cont function: coercion from \[Int16, Float64, Utf8\] to the signature OneOf(.*) failed(.|\n)*
Expand Down
9 changes: 9 additions & 0 deletions datafusion/sqllogictest/test_files/errors.slt
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,12 @@ create table records (timestamp timestamp, value float) as values (
'2021-01-01 00:00:00', 1.0,
'2021-01-01 00:00:00', 2.0
);

statement ok
CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER);

statement ok
INSERT INTO tab0 VALUES(83,0,38);

query error DataFusion error: Arrow error: Divide by zero error
SELECT DISTINCT - 84 FROM tab0 AS cor0 WHERE NOT + 96 / + col1 <= NULL GROUP BY col1, col0;
Copy link
Contributor Author

@getChan getChan Feb 1, 2025

Choose a reason for hiding this comment

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

AS-IS

  • query error DataFusion error: External error: External error: External error: Arrow error: Divide by zero error
  • An ExternalError is added to each RepartitionExec.
스크린샷 2025-02-02 오전 12 05 06