You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "ZhangHuiGui (via GitHub)" <gi...@apache.org> on 2024/04/24 08:51:32 UTC

[PR] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

ZhangHuiGui opened a new pull request, #41363:
URL: https://github.com/apache/arrow/pull/41363

   
   <!--
   Thanks for opening a pull request!
   If this is your first pull request you can find detailed information on how 
   to contribute here:
     * [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
     * [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
   
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
   
   Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename the pull request title in the following format?
   
       GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   In the case of PARQUET issues on JIRA the title also supports:
   
       PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   -->
   
   ### Rationale for this change
    Fix the bug of decimal types skipping cast in IfElse related expression function calls
   
   <!--
    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?
   1. Add the output resolver for the IfElse related kernel functions : `if_else`, `case_when`, `coalesce`, `choose`.
       Output resolver will make sure the input decimal types casted correctly by their `DispatchBest` in expression call.
   2. Add the tests include the combination of the decimal types and other numeric types to make sure they can be
       cast and execute correctly as `CallFunction`.
   
   <!--
   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?
   Yes
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   4. 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?
   No
   
   <!--
   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 uncomment the line below and explain which changes are breaking.
   -->
   <!-- **This PR includes breaking changes to public APIs.** -->
   
   <!--
   Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
   -->
   <!-- **This PR contains a "Critical Fix".** -->


-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "ZhangHuiGui (via GitHub)" <gi...@apache.org>.
ZhangHuiGui commented on code in PR #41363:
URL: https://github.com/apache/arrow/pull/41363#discussion_r1577578652


##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,
+                                          const std::vector<TypeHolder>& types,
+                                          ResolverForOtherTypes&& resolver) {
+  if (!HasDecimal(types)) {
+    return resolver(ctx, types);
+  }
+
+  int32_t max_precision = 0, max_scale = 0;
+  for (auto& type : types) {
+    if (is_floating(type.id()) || is_integer(type.id())) {
+      return Status::Invalid("Need to cast numeric types containing decimal types");
+    } else if (is_decimal(type.id())) {
+      const auto& decimal_type = checked_cast<const arrow::DecimalType&>(*type);
+      if (decimal_type.precision() < max_precision || decimal_type.scale() < max_scale) {

Review Comment:
   The returned invalid status used in the logic :
   https://github.com/bkietz/arrow/blob/c25866bc59e30e43e8dc3b05f3973d48074c3594/cpp/src/arrow/compute/expression.cc#L551-L552
   
   Let the expression call go into `DispatchBest` so they can be cast correctly.



-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "ZhangHuiGui (via GitHub)" <gi...@apache.org>.
ZhangHuiGui commented on code in PR #41363:
URL: https://github.com/apache/arrow/pull/41363#discussion_r1614383192


##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,
+                                          const std::vector<TypeHolder>& types,
+                                          ResolverForOtherTypes&& resolver) {
+  if (!HasDecimal(types)) {
+    return resolver(ctx, types);
+  }
+
+  int32_t max_precision = 0, max_scale = 0;
+  for (auto& type : types) {
+    if (is_floating(type.id()) || is_integer(type.id())) {
+      return Status::Invalid("Need to cast numeric types containing decimal types");

Review Comment:
   Yes, the approach with `TypeMatcher` is more suitable. If there is no problem with the PR https://github.com/apache/arrow/pull/41012  that contains a new matcher interface for combination-types, current PR will continue to be promoted.



-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 closed pull request #41363: GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls
URL: https://github.com/apache/arrow/pull/41363


-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "zanmato1984 (via GitHub)" <gi...@apache.org>.
zanmato1984 commented on PR #41363:
URL: https://github.com/apache/arrow/pull/41363#issuecomment-3248618452

   Issue fixed by #47479. Closing this one. Thanks you all the same @ZhangHuiGui !


-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #41363:
URL: https://github.com/apache/arrow/pull/41363#issuecomment-2074432280

   :warning: GitHub issue #41336 **has been automatically assigned in GitHub** to PR creator.


-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "ZhangHuiGui (via GitHub)" <gi...@apache.org>.
ZhangHuiGui commented on code in PR #41363:
URL: https://github.com/apache/arrow/pull/41363#discussion_r1577895599


##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,
+                                          const std::vector<TypeHolder>& types,
+                                          ResolverForOtherTypes&& resolver) {
+  if (!HasDecimal(types)) {
+    return resolver(ctx, types);
+  }
+
+  int32_t max_precision = 0, max_scale = 0;
+  for (auto& type : types) {
+    if (is_floating(type.id()) || is_integer(type.id())) {
+      return Status::Invalid("Need to cast numeric types containing decimal types");
+    } else if (is_decimal(type.id())) {
+      const auto& decimal_type = checked_cast<const arrow::DecimalType&>(*type);
+      if (decimal_type.precision() < max_precision || decimal_type.scale() < max_scale) {

Review Comment:
   In `BindNonRecursive`:
   `FinishBind` will do output type resolve. For the first time go into the resolver, our decimal related types haven't cast, we could check if the decimal types have different precision and scale. 
   
   And then return invalid to make the `BindNonRecursive` go into `IfElse`'s related function's `DispatchBest` and do cast.
   
   After that, when we go into type resolver in `BindNoRecursive` second time, we could find the decimal types already cast to same precision and scale, so we could find the correct result output type.



-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #41363:
URL: https://github.com/apache/arrow/pull/41363#discussion_r1577542485


##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,
+                                          const std::vector<TypeHolder>& types,
+                                          ResolverForOtherTypes&& resolver) {
+  if (!HasDecimal(types)) {
+    return resolver(ctx, types);
+  }
+
+  int32_t max_precision = 0, max_scale = 0;
+  for (auto& type : types) {
+    if (is_floating(type.id()) || is_integer(type.id())) {
+      return Status::Invalid("Need to cast numeric types containing decimal types");
+    } else if (is_decimal(type.id())) {
+      const auto& decimal_type = checked_cast<const arrow::DecimalType&>(*type);
+      if (decimal_type.precision() < max_precision || decimal_type.scale() < max_scale) {

Review Comment:
   < 0?



-- 
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] GH-41336: [C++][Compute] Fix the bug of decimal types skipping cast in IfElse related expression function calls [arrow]

Posted by "felipecrv (via GitHub)" <gi...@apache.org>.
felipecrv commented on code in PR #41363:
URL: https://github.com/apache/arrow/pull/41363#discussion_r1600293888


##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,
+                                          const std::vector<TypeHolder>& types,
+                                          ResolverForOtherTypes&& resolver) {
+  if (!HasDecimal(types)) {
+    return resolver(ctx, types);
+  }
+
+  int32_t max_precision = 0, max_scale = 0;
+  for (auto& type : types) {
+    if (is_floating(type.id()) || is_integer(type.id())) {
+      return Status::Invalid("Need to cast numeric types containing decimal types");

Review Comment:
   I think a better approach for these failures is to have kernels that fail. You write input matchers to cover all cases and dispatch to the fail kernels for types that can't be handled without casts.



##########
cpp/src/arrow/compute/kernels/scalar_if_else.cc:
##########
@@ -1195,6 +1195,43 @@ struct ResolveIfElseExec<NullType, AllocateMem> {
   }
 };
 
+template <typename ResolverForOtherTypes>
+Result<TypeHolder> ResolveDecimalCaseType(KernelContext* ctx,

Review Comment:
   Name suggestion: `ResolveDecimalCaseWhenOutputType`
   
   Because "DecimalCase" is ambiguous by "case" being a word that would fit here, but it actually refers to the kernel function called "CaseWhen".



-- 
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