cyb70289 commented on a change in pull request #9841: URL: https://github.com/apache/arrow/pull/9841#discussion_r608292204
########## File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc ########## @@ -233,6 +236,104 @@ struct DivideChecked { } }; +template <typename T> +inline T integer_power(KernelContext* ctx, T left, T right) { + if (right < 0) { + ctx->SetStatus( + Status::Invalid("integers to negative integer powers are not allowed")); + } + T result = 1; + if (left == 0 && right != 0) { + return 0; + } + while (true) { + if (right % 2) { + result *= left; + } + right /= 2; + if (!right) { + break; + } + left *= left; + } + return result; +} + +template <typename T, typename Arg0, typename Arg1> +inline T integer_power_checked(KernelContext* ctx, Arg0 left, Arg1 right) { + if (right < 0) { + ctx->SetStatus( + Status::Invalid("integers to negative integer powers are not allowed")); + } + T result = 1; + if (left == 0 && right != 0) { + return 0; + } + while (true) { + if (right % 2) { + if (ARROW_PREDICT_FALSE(MultiplyWithOverflow(result, left, &result))) { + ctx->SetStatus(Status::Invalid("overflow")); + } + } + right /= 2; + if (!right) { + break; + } + if (ARROW_PREDICT_FALSE(MultiplyWithOverflow(left, left, &left))) { + ctx->SetStatus(Status::Invalid("overflow")); + } + } + return result; +} + +template <typename T, typename Arg0, typename Arg1> +inline T power(KernelContext* ctx, Arg0 left, Arg1 right) { + if (left == 0 && right < 0) { + ctx->SetStatus(Status::Invalid("divide by zero")); + } + if (left == 0 && right != 0) { + return 0; + } + return pow(left, right); Review comment: Just call `std::pow(left, right)`, leave the decision to the standard library. It says `0^-1 = inf`, `0^0 = 1`. It's fine. Our invention is no better than that. ########## File path: cpp/src/arrow/compute/kernels/scalar_arithmetic.cc ########## @@ -233,6 +236,104 @@ struct DivideChecked { } }; +template <typename T> +inline T integer_power(KernelContext* ctx, T left, T right) { + if (right < 0) { + ctx->SetStatus( + Status::Invalid("integers to negative integer powers are not allowed")); + } + T result = 1; + if (left == 0 && right != 0) { + return 0; + } + while (true) { + if (right % 2) { + result *= left; Review comment: Please do check the UBSAN CI failure. I think it's caused by signed integer overflow in this function. You can enable UBSAN check on your local host with `cmake -DARROW_USE_UBSAN=ON ....`. ########## File path: cpp/src/arrow/compute/api_scalar.cc ########## @@ -44,14 +44,16 @@ namespace compute { #define SCALAR_ARITHMETIC_BINARY(NAME, REGISTRY_NAME, REGISTRY_CHECKED_NAME) \ Result<Datum> NAME(const Datum& left, const Datum& right, ArithmeticOptions options, \ ExecContext* ctx) { \ - auto func_name = (options.check_overflow) ? REGISTRY_CHECKED_NAME : REGISTRY_NAME; \ + std::string func_name = \ + (options.check_overflow) ? REGISTRY_CHECKED_NAME : REGISTRY_NAME; \ Review comment: Please restore this line -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org