You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "guojidan (via GitHub)" <gi...@apache.org> on 2024/03/01 10:16:33 UTC
[PR] rewrite nvl function [arrow-datafusion]
guojidan opened a new pull request, #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413
## 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 #9365 .
## 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.
-->
now, `nvl2` function is supported, so we don't need keep nvl code, we can rewrite `nvl` to `nvl2`
## 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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [arrow-datafusion]
Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#discussion_r1509494269
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -311,4 +318,33 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
}
+
+ fn all_suger_function() -> &'static [&'static str] {
+ &["nvl", "ifnull"]
+ }
+
+ fn try_rewrite_suger_function(
+ &self,
+ name: &str,
+ args: &[FunctionArg],
+ schema: &DFSchema,
+ planner_context: &mut PlannerContext,
+ ) -> Result<Option<Expr>> {
+ match (name, args) {
+ // rewirte nvl function to nvl2 function
Review Comment:
```suggestion
// rewrite nvl function to nvl2 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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #9413:
URL: https://github.com/apache/datafusion/pull/9413#issuecomment-2103703270
Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.
--
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] rewrite nvl function [arrow-datafusion]
Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#discussion_r1509493692
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -311,4 +318,33 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
}
+
+ fn all_suger_function() -> &'static [&'static str] {
Review Comment:
This can be a `const`
`const NVL_ALIAS: &'static [&'static str] = ...`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [datafusion]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #9413: rewrite nvl function
URL: https://github.com/apache/datafusion/pull/9413
--
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] rewrite nvl function [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#issuecomment-1987182045
Marking this PR as draft as I don't think it is waiting for more review (and I am trying to get through the PR review backlog).
Do we have any alternate approaches? Or is the plan to leave both `nvl` and `nvl2`? Or something else?
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [arrow-datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#issuecomment-1973429711
I think that this manner of rewriting might have several downsides that need to be considered.
1) Because `NVL` no longer exists in the function list, some of its features will be missing. For example, the feature in issue #9392, and the hint after entering wrong parameters.
On this PR's branch:
```sh
DataFusion CLI v36.0.0
❯ select nvl(1);
Error during planning: Invalid function 'nvl'.
Did you mean 'nanvl'?
```
The hint on the main branch would be better.
```
❯ select nvl(1);
Error during planning: No function matches the given name and argument types 'nvl(Int64)'. You might need to add explicit type casts.
Candidate functions:
nvl(Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8, Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8)
```
2. There may be incorrect result when NVL's parameters contain a [volatile](https://github.com/apache/arrow-datafusion/blob/b2ff249bfb918ac6697dbc92b51262a7bdbb5971/datafusion/expr/src/expr.rs#L1903) expression.
For example, given an expression `expr` is `nullif(random()>0.5, true)`. The result of `select nvl(expr, false)`should always be false.
But if it is rewritten as `nvl(expr, expr, false)`, it could potentially yield a NULL value, when the random result of the first `expr` is less than 0.5, and the random result of the second `expr` is greater than 0.5.
I have performed such a test on this branch.
```
DataFusion CLI v36.0.0
❯ select nvl(nullif(random()>0.5, true), false) is null from (unnest(range(1,100)));
+--------------------------------------------------------------------------------------------------------------------------+
| nvl2(nullif(random() > Float64(0.5),Boolean(true)),nullif(random() > Float64(0.5),Boolean(true)),Boolean(false)) IS NULL |
+--------------------------------------------------------------------------------------------------------------------------+
| true |
| false |
| false |
| false |
| false |
```
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [arrow-datafusion]
Posted by "PsiACE (via GitHub)" <gi...@apache.org>.
PsiACE commented on PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#issuecomment-1983326472
It seems that `nvl` and `ifnull` can be considered to be rewritten as `coalesce` to ensure obtaining the correct result in this case.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] rewrite nvl function [arrow-datafusion]
Posted by "guojidan (via GitHub)" <gi...@apache.org>.
guojidan commented on PR #9413:
URL: https://github.com/apache/arrow-datafusion/pull/9413#issuecomment-1980333704
> I think that this manner of rewriting might have several downsides that need to be considered.
>
> 1. Because `NVL` no longer exists in the function list, some of its features will be missing. For example, the feature in issue [Add ScalarUDFs in missing function hints #9392](https://github.com/apache/arrow-datafusion/issues/9392), and the hint after entering wrong parameters.
>
> On this PR's branch:
>
> ```shell
> DataFusion CLI v36.0.0
> ❯ select nvl(1);
> Error during planning: Invalid function 'nvl'.
> Did you mean 'nanvl'?
> ```
>
> The hint on the main branch would be better.
>
> ```
> ❯ select nvl(1);
> Error during planning: No function matches the given name and argument types 'nvl(Int64)'. You might need to add explicit type casts.
> Candidate functions:
> nvl(Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8, Boolean/UInt8/UInt16/UInt32/UInt64/Int8/Int16/Int32/Int64/Float32/Float64/Utf8/LargeUtf8)
> ```
>
> 2. There may be incorrect result when NVL's parameters contain a [volatile](https://github.com/apache/arrow-datafusion/blob/b2ff249bfb918ac6697dbc92b51262a7bdbb5971/datafusion/expr/src/expr.rs#L1903) expression.
> For example, given an expression `expr` is `nullif(random()>0.5, true)`. The result of `select nvl(expr, false)`should always be false.
> But if it is rewritten as `nvl(expr, expr, false)`, it could potentially yield a NULL value, when the random result of the first `expr` is less than 0.5, and the random result of the second `expr` is greater than 0.5.
>
> I have performed such a test on this branch.
>
> ```
> DataFusion CLI v36.0.0
> ❯ select nvl(nullif(random()>0.5, true), false) is null from (unnest(range(1,100)));
> +--------------------------------------------------------------------------------------------------------------------------+
> | nvl2(nullif(random() > Float64(0.5),Boolean(true)),nullif(random() > Float64(0.5),Boolean(true)),Boolean(false)) IS NULL |
> +--------------------------------------------------------------------------------------------------------------------------+
> | true |
> | false |
> | false |
> | false |
> | false |
> ```
>
> There is approximately a 25% chance of an incorrect result.
>
> ```shell
> ❯ select count(*) from (
> select
> nvl(nullif(random()>0.5, true), false)
> from (unnest(range(1,10000)))
> ) as t(a)
> where a is null;
> +----------+
> | COUNT(*) |
> +----------+
> | 2517 |
> +----------+
> ```
yes, need to redesign rewrite manner
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org