You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2024/04/14 07:35:57 UTC
[PR] Coerce Dictionary types for scalar functions [arrow-datafusion]
viirya opened a new pull request, #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077
## 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 #10076.
## 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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565012112
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
> "check inner data type with coerced_from"
Similar to the current implementation of Dict
https://github.com/apache/arrow-datafusion/blob/671cef85c550969ab2c86d644968a048cb181c0c/datafusion/expr/src/type_coercion/functions.rs#L316-L321
The above once checks if the inner type in Dict is coercible by the `coerced_from` function.
But `dictionary_coercion` checks the inner type of Dict with `comparison_coercion`.
The `coerced_from` and `comparison_coercion` are slightly different.
`comparison_coercion` cares about the scenario in comparison, so **loss is allowed**. For example, i64 and u64, we return i64, while we get None in `coerced_from` for casting u64 to i64.
I tried to have one coercion for all but ended up not finding a way to cover all the cases in https://github.com/apache/arrow-datafusion/issues/8302.
I suggest we don't mix the logic for `coerce_from` and `comparison_coercion`. It would be nice to avoid using `comparison_binary_numeric_coercion ` in `coerced_from ` too.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565115708
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
Ah, I don't notice `coerced_from` is updated to coerce dictionary type. I was working on a branch without update yet. The current update with dictionary coercion looks good now to fix the issue I encountered.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565012112
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
> "check inner data type with coerced_from"
Similar to the current implementation of Dict
https://github.com/apache/arrow-datafusion/blob/671cef85c550969ab2c86d644968a048cb181c0c/datafusion/expr/src/type_coercion/functions.rs#L316-L321
The above once checks if the inner type in Dict is coercible by the `coerced_from` function.
But `dictionary_coercion` checks the inner type of Dict with `comparison_coercion`.
The `coerced_from` and `comparison_coercion` are slightly different.
`comparison_coercion` cares about the scenario in comparison, so **loss is allowed**. For example, i64 and u64, we return i64, while we get None in `coerced_from` for casting u64 to i64.
I tried to have one coercion for all but ended up not finding a way to cover all the cases in https://github.com/apache/arrow-datafusion/issues/8302.
I suggest we don't mix the logic for `coerce_from` and `comparison_coercion`. It would be nice to avoid `comparison_binary_numeric_coercion ` too.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#issuecomment-2057397095
Thank you @andygrove
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1564772922
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
I couldn't quite tell the difference. What would be the benefit?
I always get a little confused with the type coercion logic -- that there are different rules for certain operations I think.
##########
datafusion/expr/src/built_in_function.rs:
##########
@@ -635,4 +635,19 @@ mod tests {
.unwrap();
assert_eq!(return_type, DataType::Date32);
}
+
+ #[test]
+ fn test_coalesce_return_types_dictionary() {
Review Comment:
👍 we saw something similar in https://github.com/apache/arrow-datafusion/issues/9925
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565553785
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -314,8 +314,13 @@ fn coerced_from<'a>(
// match Dictionary first
match (type_into, type_from) {
// coerced dictionary first
- (cur_type, Dictionary(_, value_type)) | (Dictionary(_, value_type), cur_type)
- if coerced_from(cur_type, value_type).is_some() =>
Review Comment:
That is an excellent find. Thanks @jayzhan211 for pointing that out
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#issuecomment-2057396299
Thank you @alamb @jayzhan211
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565119139
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
But I notice that the current implementation has a small issue.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565012112
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
> "check inner data type with coerced_from"
Similar to the current implementation of Dict
https://github.com/apache/arrow-datafusion/blob/671cef85c550969ab2c86d644968a048cb181c0c/datafusion/expr/src/type_coercion/functions.rs#L316-L321
The above once checks if the inner type in Dict is coercible by the `coerced_from` function.
But `dictionary_coercion` checks the inner type of Dict with `comparison_coercion`.
The `coerced_from` and `comparison_coercion` are slightly different.
`comparison_coercion` cares about the scenario in comparison, so **loss is allowed**. For example, i64 and u64, we return i64, while we get None in `coerced_from` for casting u64 to i64.
I had tried to find one coercion for all but ended up with the conclusion that we keep these two coercion functions.
https://github.com/apache/arrow-datafusion/issues/8302.
I suggest we don't mix the logic for `coerce_from` and `comparison_coercion`. It would be nice to avoid using `comparison_binary_numeric_coercion ` in `coerced_from ` too.
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
> "check inner data type with coerced_from"
Similar to the current implementation of Dict
https://github.com/apache/arrow-datafusion/blob/671cef85c550969ab2c86d644968a048cb181c0c/datafusion/expr/src/type_coercion/functions.rs#L316-L321
The above once checks if the inner type in Dict is coercible by the `coerced_from` function.
But `dictionary_coercion` checks the inner type of Dict with `comparison_coercion`.
The `coerced_from` and `comparison_coercion` are slightly different.
`comparison_coercion` cares about the scenario in comparison, so **loss is allowed**. For example, i64 and u64, we return i64, while we get None in `coerced_from` for casting u64 to i64.
I had tried to find one coercion for all but ended up with the conclusion that we keep these two coercion functions. https://github.com/apache/arrow-datafusion/issues/8302.
I suggest we don't mix the logic for `coerce_from` and `comparison_coercion`. It would be nice to avoid using `comparison_binary_numeric_coercion ` in `coerced_from ` too.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565012112
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
> "check inner data type with coerced_from"
Similar to the current implementation of Dict
```rust
// coerced dictionary first
(cur_type, Dictionary(_, value_type)) | (Dictionary(_, value_type), cur_type)
if coerced_from(cur_type, value_type).is_some() =>
{
Some(type_into.clone())
}
```
The above once checks if the inner type in Dict is coercible by the `coerced_from` function.
But `dictionary_coercion` checks the inner type of Dict with `comparison_coercion`.
The `coerced_from` and `comparison_coercion` are slightly different.
`comparison_coercion` cares about the scenario in comparison, so **loss is allowed**. For example, i64 and u64, we return i64, while we get None in `coerced_from` for casting u64 to i64.
I tried to have one coercion for all but ended up not finding a way to cover all the cases in https://github.com/apache/arrow-datafusion/issues/8302.
I suggest we don't mix the logic for `coerce_from` and `comparison_coercion`. It would be nice to avoid `comparison_binary_numeric_coercion ` too.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1564854439
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
Hmm, not sure I understand the comment. What you mean "check inner data type with `coerced_from`"?
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
Hmm, not sure I understand the comment too. What you mean "check inner data type with `coerced_from`"?
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1565122093
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -314,8 +314,13 @@ fn coerced_from<'a>(
// match Dictionary first
match (type_into, type_from) {
// coerced dictionary first
- (cur_type, Dictionary(_, value_type)) | (Dictionary(_, value_type), cur_type)
- if coerced_from(cur_type, value_type).is_some() =>
Review Comment:
When coercing into dictionary type, the `type_into` and `type_from` parameters are in incorrect order.
--
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] Coerce Dictionary types for scalar functions [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #10077:
URL: https://github.com/apache/arrow-datafusion/pull/10077#discussion_r1564548305
##########
datafusion/expr/src/type_coercion/functions.rs:
##########
@@ -435,15 +437,15 @@ fn coerced_from<'a>(
// Note that not all rules in `comparison_coercion` can be reused here.
// For example, all numeric types can be coerced into Utf8 for comparison,
// but not for function arguments.
- _ => comparison_binary_numeric_coercion(type_into, type_from).and_then(
- |coerced_type| {
+ _ => comparison_binary_numeric_coercion(type_into, type_from)
+ .or_else(|| dictionary_coercion(type_into, type_from, true))
Review Comment:
Should we check the inner data type with `coerced_from` instead of `comparison_coercion` 🤔 ?
--
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