You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "joroKr21 (via GitHub)" <gi...@apache.org> on 2024/10/16 09:40:41 UTC
[PR] Handle one-element array return value in ScalarFunctionExpr [datafusion]
joroKr21 opened a new pull request, #12965:
URL: https://github.com/apache/datafusion/pull/12965
This was done in #12922 only for math functions.
We now generalize this fallback to all scalar UDFs.
## 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 #12959
## 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 #12922
## 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.
-->
* We move the fallback for one-element array return value from the math function macros to `ScalarFunctionExpr`
* We remove the helper method added in #12959
## 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)?
-->
Relying on existing tests.
## Are there any user-facing changes?
`ColumnarValue::from_args_and_result` is removed again, it's not released yet.
<!--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#issuecomment-2420099089
Thanks @joroKr21 @eejbyfeldt and @jonahgao
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "eejbyfeldt (via GitHub)" <gi...@apache.org>.
eejbyfeldt commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1803576103
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
Review Comment:
I read the discussion and I see where you coming from. I guess a bit confusing since that function is currently not implementable like you said in that thread.
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1803999817
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
Review Comment:
> I guess a bit confusing since that function is currently not implementable like you said in that thread.
I think so. It's currently not implementable.
I think under the current UDF invoke model, we should preserve scalar even if it is volatile. For example, we have a UDF `fn random_string(len: int) -> String`, which returns a random string of length `len`. This function is `volatile`, but it should return a scalar if the len parameter is a scalar.
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "eejbyfeldt (via GitHub)" <gi...@apache.org>.
eejbyfeldt commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1802941364
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
Review Comment:
Why can we not do this for functions that are `Volatile`?
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1804008922
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
+ && inputs
+ .iter()
+ .all(|arg| matches!(arg, ColumnarValue::Scalar(_)));
Review Comment:
I think we should consider the case where the inputs are empty. For UDFs without args, either return a scalar directly or return an array with `num_rows`. Trying to convert the output array back to scalar for them seems unnecessary.
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "joroKr21 (via GitHub)" <gi...@apache.org>.
joroKr21 commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1802959651
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
Review Comment:
A volatile function is supposed to return a different result on every invocation so I don't think it's safe to do that. The example we were discussing in the earlier PR is a hypothetical `rand(upper_bound: Int64) -> Int64` function.
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #12965:
URL: https://github.com/apache/datafusion/pull/12965
--
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] Handle one-element array return value in ScalarFunctionExpr [datafusion]
Posted by "joroKr21 (via GitHub)" <gi...@apache.org>.
joroKr21 commented on code in PR #12965:
URL: https://github.com/apache/datafusion/pull/12965#discussion_r1804230294
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
Review Comment:
Sure, I can make that change - was just leaning more on the safe side.
##########
datafusion/physical-expr/src/scalar_function.rs:
##########
@@ -147,8 +149,19 @@ impl PhysicalExpr for ScalarFunctionExpr {
if let ColumnarValue::Array(array) = &output {
if array.len() != batch.num_rows() {
- return internal_err!("UDF returned a different number of rows than expected. Expected: {}, Got: {}",
- batch.num_rows(), array.len());
+ // If the function is not volatile and all arguments are scalars,
+ // we can assume that returning a one-element array is equivalent to returning a scalar.
+ let preserve_scalar = array.len() == 1
+ && self.fun.signature().volatility != Volatility::Volatile
+ && inputs
+ .iter()
+ .all(|arg| matches!(arg, ColumnarValue::Scalar(_)));
Review Comment:
Nice catch 👍
--
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