From ea4629cded68384981845c77f38564a9a6ac3adc Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 24 May 2021 14:04:52 -0400 Subject: [PATCH 01/14] ARROW-12751: [C++] Implement minimum/maximum kernels --- cpp/src/arrow/compute/api_scalar.cc | 10 + cpp/src/arrow/compute/api_scalar.h | 23 ++ .../compute/kernels/scalar_arithmetic.cc | 303 ++++++++++++++++++ .../compute/kernels/scalar_arithmetic_test.cc | 299 +++++++++++++++++ docs/source/cpp/compute.rst | 14 + docs/source/python/api/compute.rst | 8 + 6 files changed, 657 insertions(+) diff --git a/cpp/src/arrow/compute/api_scalar.cc b/cpp/src/arrow/compute/api_scalar.cc index 105ba7a0589..f2fdeb5eead 100644 --- a/cpp/src/arrow/compute/api_scalar.cc +++ b/cpp/src/arrow/compute/api_scalar.cc @@ -63,6 +63,16 @@ SCALAR_ARITHMETIC_BINARY(Multiply, "multiply", "multiply_checked") SCALAR_ARITHMETIC_BINARY(Divide, "divide", "divide_checked") SCALAR_ARITHMETIC_BINARY(Power, "power", "power_checked") +Result Maximum(const std::vector& args, MinMaxOptions options, + ExecContext* ctx) { + return CallFunction("maximum", args, &options, ctx); +} + +Result Minimum(const std::vector& args, MinMaxOptions options, + ExecContext* ctx) { + return CallFunction("minimum", args, &options, ctx); +} + // ---------------------------------------------------------------------- // Set-related operations diff --git a/cpp/src/arrow/compute/api_scalar.h b/cpp/src/arrow/compute/api_scalar.h index 0a05b123a44..e389e6d344e 100644 --- a/cpp/src/arrow/compute/api_scalar.h +++ b/cpp/src/arrow/compute/api_scalar.h @@ -42,6 +42,11 @@ struct ArithmeticOptions : public FunctionOptions { bool check_overflow; }; +struct ARROW_EXPORT MinMaxOptions : public FunctionOptions { + MinMaxOptions() : skip_nulls(true) {} + bool skip_nulls; +}; + struct ARROW_EXPORT MatchSubstringOptions : public FunctionOptions { explicit MatchSubstringOptions(std::string pattern, bool ignore_case = false) : pattern(std::move(pattern)), ignore_case(ignore_case) {} @@ -253,6 +258,24 @@ Result Power(const Datum& left, const Datum& right, ArithmeticOptions options = ArithmeticOptions(), ExecContext* ctx = NULLPTR); +/// \brief +/// +/// \param[in] args +/// \param[in] ctx the function execution context, optional +/// \return +ARROW_EXPORT +Result Maximum(const std::vector& args, MinMaxOptions options = {}, + ExecContext* ctx = NULLPTR); + +/// \brief +/// +/// \param[in] args +/// \param[in] ctx the function execution context, optional +/// \return +ARROW_EXPORT +Result Minimum(const std::vector& args, MinMaxOptions options = {}, + ExecContext* ctx = NULLPTR); + /// \brief Compare a numeric array with a scalar. /// /// \param[in] left datum to compare, must be an Array diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index 743d2e3fc0e..2501517fb80 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -18,8 +18,10 @@ #include #include +#include "arrow/compute/api_scalar.h" #include "arrow/compute/kernels/common.h" #include "arrow/type_traits.h" +#include "arrow/util/bitmap_ops.h" #include "arrow/util/int_util_internal.h" #include "arrow/util/macros.h" @@ -397,6 +399,30 @@ struct PowerChecked { } }; +struct Minimum { + template + static enable_if_floating_point Call(T left, T right) { + return std::fmin(left, right); + } + + template + static enable_if_integer Call(T left, T right) { + return std::min(left, right); + } +}; + +struct Maximum { + template + static enable_if_floating_point Call(T left, T right) { + return std::fmax(left, right); + } + + template + static enable_if_integer Call(T left, T right) { + return std::max(left, right); + } +}; + // Generate a kernel given an arithmetic functor template