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

Simplify error handling in case.rs (#13990) #14033

Merged

Conversation

cj-zhukov
Copy link
Contributor

Which issue does this PR close?

Closes #13990.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the physical-expr Physical Expressions label Jan 7, 2025
@alamb
Copy link
Contributor

alamb commented Jan 7, 2025

Thank you @cj-zhukov !

It seems as if there is a compile error in this PR: https://github.com/apache/datafusion/actions/runs/12646530220/job/35237388507?pr=14033

error[E0433]: failed to resolve: use of undeclared type `DataFusionError`
   --> datafusion/physical-expr/src/expressions/case.rs:407:13
    |
407 |             DataFusionError::Context(
    |             ^^^^^^^^^^^^^^^ use of undeclared type `DataFusionError`
    |
help: consider importing this enum
    |
18  + use datafusion_common::DataFusionError;
    |

    Checking datafusion-sql v44.0.0 (/__w/datafusion/datafusion/datafusion/sql)
    Checking datafusion-functions v44.0.0 (/__w/datafusion/datafusion/datafusion/functions)
For more information about this error, try `rustc --explain E0433`.
error: could not compile `datafusion-physical-expr` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
Error: Process completed with exit code 101.

@cj-zhukov
Copy link
Contributor Author

@alamb Andrew, I noticed the build fails unless I import datafusion_common::DataFusionError, which is not used in my PR changes but appears necessary for compatibility with the main branch. Please let me know if there's a better way to handle this.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @cj-zhukov -- this looks quite nice to me ❤️

@alamb Andrew, I noticed the build fails unless I import datafusion_common::DataFusionError, which is not used in my PR changes but appears necessary for compatibility with the main branch. Please let me know if there's a better way to handle this.

I am not sure why this was needed -- DataFusionError is used indirectly by the macros you use (e.g. internal_datafusion_err uses DataFusionError internally. Maybe it has something to do with scoping rules 🤔 I am not sure

// keep `else_expr`'s data type and return type consistent
let expr = try_cast(Arc::clone(e), &batch.schema(), return_type.clone())
.unwrap_or_else(|_| Arc::clone(e));
let expr = try_cast(Arc::clone(e), &batch.schema(), return_type.clone())?;
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 this change is an improvement -- previously the error was ignored. In this new formulation it is returned.

// keep `else_expr`'s data type and return type consistent
let expr = try_cast(Arc::clone(e), &batch.schema(), return_type.clone())
.unwrap_or_else(|_| Arc::clone(e));
let expr = try_cast(Arc::clone(e), &batch.schema(), return_type.clone())?;
Copy link
Contributor

Choose a reason for hiding this comment

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

likewise here it is a good change not to silently ignore the error -- that would likely just make any future error more confusing

@@ -369,11 +366,8 @@ impl CaseExpr {
// evaluate when expression
let when_value = self.when_then_expr[0].0.evaluate(batch)?;
let when_value = when_value.into_array(batch.num_rows())?;
let when_value = as_boolean_array(&when_value).map_err(|e| {
DataFusionError::Context(
Copy link
Contributor

Choose a reason for hiding this comment

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

Another potential way to to keep the context would be something like

.map_err(|e| e.context("WHEN expression did not return a BooleanArray"))

I think what you have here is good though as it makes it clear this is an internal / non expected error

datafusion/physical-expr/src/expressions/case.rs Outdated Show resolved Hide resolved
@cj-zhukov
Copy link
Contributor Author

@alamb Andrew, thank you for feedback and useful advices. Really appreciate your work that helps me improve my Rust code.

@alamb
Copy link
Contributor

alamb commented Jan 9, 2025

Thanks again @cj-zhukov

@alamb alamb merged commit ac9584e into apache:main Jan 9, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
physical-expr Physical Expressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify error handling in case.rs
2 participants