You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "eatthepear (via GitHub)" <gi...@apache.org> on 2024/11/25 19:22:29 UTC
[PR] [substrait] support-try-cast [datafusion]
eatthepear opened a new pull request, #13562:
URL: https://github.com/apache/datafusion/pull/13562
## 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 #13419 .
## 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.
-->
See issue.
## 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)?
-->
See added 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] feat: [substrait] support-try-cast [datafusion]
Posted by "eatthepear (via GitHub)" <gi...@apache.org>.
eatthepear commented on PR #13562:
URL: https://github.com/apache/datafusion/pull/13562#issuecomment-2501278485
Thanks for the review comments. Ready for another look!
--
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] feat: [substrait] support-try-cast [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13562:
URL: https://github.com/apache/datafusion/pull/13562#issuecomment-2499088430
Thank you @eatthepear 🙏
--
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] feat: [substrait] support-try-cast [datafusion]
Posted by "vbarua (via GitHub)" <gi...@apache.org>.
vbarua commented on code in PR #13562:
URL: https://github.com/apache/datafusion/pull/13562#discussion_r1858127024
##########
datafusion/substrait/src/logical_plan/consumer.rs:
##########
@@ -1682,18 +1683,35 @@ pub async fn from_substrait_rex(
Ok(Expr::Literal(scalar_value))
}
Some(RexType::Cast(cast)) => match cast.as_ref().r#type.as_ref() {
- Some(output_type) => Ok(Expr::Cast(Cast::new(
- Box::new(
- from_substrait_rex(
- state,
- cast.as_ref().input.as_ref().unwrap().as_ref(),
- input_schema,
- extensions,
- )
- .await?,
- ),
- from_substrait_type_without_names(output_type, extensions)?,
- ))),
+ Some(output_type) => {
+ if cast.failure_behavior() == ReturnNull {
+ Ok(Expr::TryCast(TryCast::new(
+ Box::new(
+ from_substrait_rex(
+ state,
+ cast.as_ref().input.as_ref().unwrap().as_ref(),
+ input_schema,
+ extensions,
+ )
+ .await?,
+ ),
+ from_substrait_type_without_names(output_type, extensions)?,
+ )))
+ } else {
+ Ok(Expr::Cast(Cast::new(
+ Box::new(
+ from_substrait_rex(
+ state,
+ cast.as_ref().input.as_ref().unwrap().as_ref(),
+ input_schema,
+ extensions,
+ )
+ .await?,
+ ),
+ from_substrait_type_without_names(output_type, extensions)?,
Review Comment:
minor: In both branches, the expression and data type we are creating and using is identical. It would be be good to pull their creating out of the if-else to avoid duplication.
##########
datafusion/substrait/src/logical_plan/producer.rs:
##########
@@ -1194,7 +1194,24 @@ pub fn to_substrait_rex(
col_ref_offset,
extensions,
)?)),
- failure_behavior: 0, // FAILURE_BEHAVIOR_UNSPECIFIED
+ failure_behavior: 2, // FAILURE_BEHAVIOR_THROW_EXCEPTION
+ },
+ ))),
+ })
+ }
+ Expr::TryCast(TryCast { expr, data_type }) => {
+ Ok(Expression {
+ rex_type: Some(RexType::Cast(Box::new(
+ substrait::proto::expression::Cast {
+ r#type: Some(to_substrait_type(data_type, true)?),
+ input: Some(Box::new(to_substrait_rex(
+ state,
+ expr,
+ schema,
+ col_ref_offset,
+ extensions,
+ )?)),
+ failure_behavior: 1, // FAILURE_BEHAVIOR_RETURN_NULL
Review Comment:
minor: This predates your PR, but it think it would be good to use something like
```rust
FailureBehavior::ReturnNull.into()
```
here, which allows us to set the value of `failure_behaviour` directly from the generated enum value.
##########
datafusion/substrait/tests/cases/roundtrip_logical_plan.rs:
##########
@@ -1464,3 +1464,13 @@ async fn create_all_type_context() -> Result<SessionContext> {
Ok(ctx)
}
+
+#[tokio::test]
+async fn try_cast_decimal_to_int() -> Result<()> {
+ roundtrip("SELECT * FROM data WHERE a = TRY_CAST(b AS int)").await
+}
+
+#[tokio::test]
+async fn try_cast_decimal_to_string() -> Result<()> {
+ roundtrip("SELECT * FROM data WHERE a = TRY_CAST(b AS string)").await
+}
Review Comment:
minor: There's some existing tests that verify other parts of casting behaviour (`cast_decimal_to_int()`, `implicit_cast()`. Could you move these closer to those for organizational purposes. The test themselves look great ✨
--
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] feat: [substrait] support-try-cast [datafusion]
Posted by "Blizzara (via GitHub)" <gi...@apache.org>.
Blizzara commented on PR #13562:
URL: https://github.com/apache/datafusion/pull/13562#issuecomment-2500166964
Changes + Victor's suggestions LGTM, thanks both!
--
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] feat: [substrait] support-try-cast [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13562:
URL: https://github.com/apache/datafusion/pull/13562#issuecomment-2499088149
FYI @vbarua @Blizzara
--
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] feat(substrait): support-try-cast [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #13562:
URL: https://github.com/apache/datafusion/pull/13562
--
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