Skip to content

Fix: Avoid recursive external error wrapping #14371

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

Merged
merged 16 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
70 changes: 68 additions & 2 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ 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 [`DataFusionError`] which shares an underlying [`DataFusionError`].
///
/// This is useful when the same underlying [`DataFusionError`] is passed
/// to multiple receivers. For example, when the source of a repartition
/// errors and the error is propagated to multiple consumers.
Shared(Arc<DataFusionError>),
}

#[macro_export]
Expand Down Expand Up @@ -262,6 +268,17 @@ impl From<DataFusionError> for ArrowError {
}
}

impl From<&Arc<DataFusionError>> for DataFusionError {
fn from(e: &Arc<DataFusionError>) -> Self {
if let DataFusionError::Shared(e_inner) = e.as_ref() {
// don't re-wrap
DataFusionError::Shared(Arc::clone(e_inner))
} else {
DataFusionError::Shared(Arc::clone(e))
}
}
}

#[cfg(feature = "parquet")]
impl From<ParquetError> for DataFusionError {
fn from(e: ParquetError) -> Self {
Expand Down Expand Up @@ -298,7 +315,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 @@ -334,6 +360,7 @@ impl Error for DataFusionError {
DataFusionError::Context(_, e) => Some(e.as_ref()),
DataFusionError::Substrait(_) => None,
DataFusionError::Diagnostic(_, e) => Some(e.as_ref()),
DataFusionError::Shared(e) => Some(e.as_ref()),
}
}
}
Expand Down Expand Up @@ -448,6 +475,7 @@ impl DataFusionError {
DataFusionError::Context(_, _) => "",
DataFusionError::Substrait(_) => "Substrait error: ",
DataFusionError::Diagnostic(_, _) => "",
DataFusionError::Shared(_) => "",
}
}

Expand Down Expand Up @@ -489,6 +517,7 @@ impl DataFusionError {
}
DataFusionError::Substrait(ref desc) => Cow::Owned(desc.to_string()),
DataFusionError::Diagnostic(_, ref err) => Cow::Owned(err.to_string()),
DataFusionError::Shared(ref desc) => Cow::Owned(desc.to_string()),
}
}

Expand Down Expand Up @@ -713,7 +742,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 @@ -867,6 +896,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 @@ -4095,7 +4095,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 @@ -1514,7 +1514,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 @@ -1077,7 +1077,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(DataFusionError::from),
),
}
}
Expand All @@ -1091,10 +1091,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::Shared))
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion datafusion/physical-plan/src/repartition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,12 @@ impl RepartitionExec {
}
// Error from running input task
Ok(Err(e)) => {
// send the same Arc'd error to all output partitions
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))));
let err = Err(DataFusionError::from(&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 @@ -142,7 +142,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