-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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(4981): incorrect error wrapping in OnceFut
#4983
Changes from 1 commit
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 |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
|
||
//! Join related functionality used both on logical and physical plans | ||
|
||
use crate::error::{DataFusionError, Result}; | ||
use crate::error::{DataFusionError, Result, SharedResult}; | ||
use crate::logical_expr::JoinType; | ||
use crate::physical_plan::expressions::Column; | ||
use crate::physical_plan::SchemaRef; | ||
|
@@ -438,7 +438,7 @@ impl<T: 'static> OnceAsync<T> { | |
} | ||
|
||
/// The shared future type used internally within [`OnceAsync`] | ||
type OnceFutPending<T> = Shared<BoxFuture<'static, Arc<Result<T>>>>; | ||
type OnceFutPending<T> = Shared<BoxFuture<'static, Arc<SharedResult<T>>>>; | ||
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. Now that 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 decided to declare it as |
||
|
||
/// A [`OnceFut`] represents a shared asynchronous computation, that will be evaluated | ||
/// once for all [`Clone`]'s, with [`OnceFut::get`] providing a non-consuming interface | ||
|
@@ -653,7 +653,7 @@ fn get_int_range(min: ScalarValue, max: ScalarValue) -> Option<usize> { | |
|
||
enum OnceFutState<T> { | ||
Pending(OnceFutPending<T>), | ||
Ready(Arc<Result<T>>), | ||
Ready(Arc<SharedResult<T>>), | ||
DDtKey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl<T> Clone for OnceFutState<T> { | ||
|
@@ -672,7 +672,11 @@ impl<T: 'static> OnceFut<T> { | |
Fut: Future<Output = Result<T>> + Send + 'static, | ||
{ | ||
Self { | ||
state: OnceFutState::Pending(fut.map(Arc::new).boxed().shared()), | ||
state: OnceFutState::Pending( | ||
fut.map(|res| Arc::new(res.map_err(Arc::new))) | ||
.boxed() | ||
.shared(), | ||
), | ||
} | ||
} | ||
|
||
|
@@ -692,7 +696,7 @@ impl<T: 'static> OnceFut<T> { | |
OnceFutState::Ready(r) => Poll::Ready( | ||
r.as_ref() | ||
.as_ref() | ||
.map_err(|e| ArrowError::ExternalError(e.to_string().into())), | ||
.map_err(|e| ArrowError::ExternalError(Box::new(e.clone()))), | ||
), | ||
} | ||
} | ||
|
@@ -939,6 +943,7 @@ mod tests { | |
use super::*; | ||
use arrow::datatypes::DataType; | ||
use datafusion_common::ScalarValue; | ||
use std::pin::Pin; | ||
|
||
fn check(left: &[Column], right: &[Column], on: &[(Column, Column)]) -> Result<()> { | ||
let left = left | ||
|
@@ -971,6 +976,41 @@ mod tests { | |
assert!(check(&left, &right, on).is_err()); | ||
} | ||
|
||
#[tokio::test] | ||
async fn check_error_nesting() { | ||
let once_fut = OnceFut::<()>::new(async { | ||
Err(DataFusionError::ArrowError(ArrowError::CsvError( | ||
"some error".to_string(), | ||
))) | ||
}); | ||
|
||
struct TestFut(OnceFut<()>); | ||
impl Future for TestFut { | ||
type Output = ArrowResult<()>; | ||
|
||
fn poll( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
) -> Poll<Self::Output> { | ||
match ready!(self.0.get(cx)) { | ||
Ok(()) => Poll::Ready(Ok(())), | ||
Err(e) => Poll::Ready(Err(e)), | ||
} | ||
} | ||
} | ||
|
||
let res = TestFut(once_fut).await; | ||
let arrow_err_from_fut = res.expect_err("once_fut always return error"); | ||
|
||
let wrapped_err = DataFusionError::from(arrow_err_from_fut); | ||
let root_err = wrapped_err.find_root(); | ||
|
||
assert!(matches!( | ||
root_err, | ||
DataFusionError::ArrowError(ArrowError::CsvError(_)) | ||
)) | ||
} | ||
|
||
#[test] | ||
fn check_not_in_left() { | ||
let left = vec![Column::new("b", 0)]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this PR never actually returns this type, do we need to declare it here? Or could we just move it into utils?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I observed that
Result<T, Arc<Error>
is quite common approach to allow clonning errors in datafustion & arrow.And not to miss such kind of errors, I think it would be nice to have it as general type.
Take a look at #4992 and even current
find_root
impl, it's easy to forget that error could be wrapped intoArc
actually (IMO)I believe this type should be used when it's needed to explicitly declare that error is wrapped into
Arc
. And could be used in other places later.And this PR doesn't return this type, however it will be inside
ArrowError::ExternalError(..)
, so actually it's way of explicit usage to be able to check all usages of this patternThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any other examples of this pattern?
I'd hope that any codepath that cares about these variants would walk the
source
tree and effectively blindly skip over the Arc...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #4992 for the code (that does indeed walk over Arcs). It was non ideal however. Figuring out some way to avoid Arc'ing errors would be better in my opinion (not for this PR)