You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "cj-zhukov (via GitHub)" <gi...@apache.org> on 2025/01/07 06:40:42 UTC
[PR] Simplify error handling in case.rs (#13990) [datafusion]
cj-zhukov opened a new pull request, #14033:
URL: https://github.com/apache/datafusion/pull/14033
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #13990.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14033:
URL: https://github.com/apache/datafusion/pull/14033#issuecomment-2575793893
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.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #14033:
URL: https://github.com/apache/datafusion/pull/14033#discussion_r1907660263
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -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(
Review Comment:
Another potential way to to keep the context would be something like
```rust
.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:
##########
@@ -386,13 +380,14 @@ impl CaseExpr {
let then_value = self.when_then_expr[0].1.evaluate(batch)?;
let then_value = Scalar::new(then_value.into_array(1)?);
- // keep `else_expr`'s data type and return type consistent
- let e = self.else_expr.as_ref().unwrap();
- let expr = try_cast(Arc::clone(e), &batch.schema(), return_type)
- .unwrap_or_else(|_| Arc::clone(e));
- let else_ = Scalar::new(expr.evaluate(batch)?.into_array(1)?);
-
- Ok(ColumnarValue::Array(zip(&when_value, &then_value, &else_)?))
+ if let Some(e) = self.else_expr() {
+ // keep `else_expr`'s data type and return type consistent
+ let expr = try_cast(Arc::clone(e), &batch.schema(), return_type)?;
+ let else_ = Scalar::new(expr.evaluate(batch)?.into_array(1)?);
+ Ok(ColumnarValue::Array(zip(&when_value, &then_value, &else_)?))
+ } else {
+ internal_err!("expression did not evaluate to an array")
+ }
Review Comment:
This is totally fine . Just FYI can write this with less indenting (and returning the error first) with slightly different syntax
```suggestion
let Some(e) = self.else_expr() else {
return internal_err!("expression did not evaluate to an array");
};
// keep `else_expr`'s data type and return type consistent
let expr = try_cast(Arc::clone(e), &batch.schema(), return_type)?;
let else_ = Scalar::new(expr.evaluate(batch)?.into_array(1)?);
Ok(ColumnarValue::Array(zip(&when_value, &then_value, &else_)?))
```
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -315,10 +313,9 @@ impl CaseExpr {
remainder = and_not(&remainder, &when_value)?;
}
- if let Some(e) = &self.else_expr {
+ if let Some(e) = self.else_expr() {
// 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())?;
Review Comment:
likewise here it is a good change not to silently ignore the error -- that would likely just make any future error more confusing
##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -242,10 +244,9 @@ impl CaseExpr {
remainder = and_not(&remainder, &when_match)?;
}
- if let Some(e) = &self.else_expr {
+ if let Some(e) = self.else_expr() {
// 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())?;
Review Comment:
I think this change is an improvement -- previously the error was ignored. In this new formulation it is returned.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "cj-zhukov (via GitHub)" <gi...@apache.org>.
cj-zhukov commented on PR #14033:
URL: https://github.com/apache/datafusion/pull/14033#issuecomment-2580372236
@alamb Andrew, thank you for feedback and useful advices. Really appreciate your work that helps me improve my Rust code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #14033:
URL: https://github.com/apache/datafusion/pull/14033#issuecomment-2581281922
Thanks again @cj-zhukov
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #14033:
URL: https://github.com/apache/datafusion/pull/14033
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] Simplify error handling in case.rs (#13990) [datafusion]
Posted by "cj-zhukov (via GitHub)" <gi...@apache.org>.
cj-zhukov commented on PR #14033:
URL: https://github.com/apache/datafusion/pull/14033#issuecomment-2576860655
@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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org