diff --git a/cpp/src/arrow/compute/api_scalar.cc b/cpp/src/arrow/compute/api_scalar.cc index 07e56d5f3d1..9357fb5f557 100644 --- a/cpp/src/arrow/compute/api_scalar.cc +++ b/cpp/src/arrow/compute/api_scalar.cc @@ -358,6 +358,9 @@ SCALAR_ARITHMETIC_BINARY(Power, "power", "power_checked") SCALAR_ARITHMETIC_BINARY(ShiftLeft, "shift_left", "shift_left_checked") SCALAR_ARITHMETIC_BINARY(ShiftRight, "shift_right", "shift_right_checked") SCALAR_EAGER_BINARY(Atan2, "atan2") +SCALAR_EAGER_UNARY(Floor, "floor") +SCALAR_EAGER_UNARY(Ceil, "ceil") +SCALAR_EAGER_UNARY(Trunc, "trunc") Result MaxElementWise(const std::vector& args, ElementWiseAggregateOptions options, ExecContext* ctx) { diff --git a/cpp/src/arrow/compute/api_scalar.h b/cpp/src/arrow/compute/api_scalar.h index 285e1eb4f51..c77994d89bb 100644 --- a/cpp/src/arrow/compute/api_scalar.h +++ b/cpp/src/arrow/compute/api_scalar.h @@ -487,6 +487,35 @@ ARROW_EXPORT Result Log1p(const Datum& arg, ArithmeticOptions options = ArithmeticOptions(), ExecContext* ctx = NULLPTR); +/// \brief Round to the nearest integer less than or equal in magnitude to the +/// argument. Array values can be of arbitrary length. If argument is null the +/// result will be null. +/// +/// \param[in] arg the value to round +/// \param[in] ctx the function execution context, optional +/// \return the rounded value +ARROW_EXPORT +Result Floor(const Datum& arg, ExecContext* ctx = NULLPTR); + +/// \brief Round to the nearest integer greater than or equal in magnitude to the +/// argument. Array values can be of arbitrary length. If argument is null the +/// result will be null. +/// +/// \param[in] arg the value to round +/// \param[in] ctx the function execution context, optional +/// \return the rounded value +ARROW_EXPORT +Result Ceil(const Datum& arg, ExecContext* ctx = NULLPTR); + +/// \brief Get the integral part without fractional digits. Array values can be +/// of arbitrary length. If argument is null the result will be null. +/// +/// \param[in] arg the value to truncate +/// \param[in] ctx the function execution context, optional +/// \return the truncated value +ARROW_EXPORT +Result Trunc(const Datum& arg, ExecContext* ctx = NULLPTR); + /// \brief Find the element-wise maximum of any number of arrays or scalars. /// Array values must be the same length. /// diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index 28904bdbfa0..a5d4a557740 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -817,6 +817,27 @@ struct Log1pChecked { } }; +struct Floor { + template + static constexpr enable_if_floating_point Call(KernelContext*, Arg arg, Status*) { + return std::floor(arg); + } +}; + +struct Ceil { + template + static constexpr enable_if_floating_point Call(KernelContext*, Arg arg, Status*) { + return std::ceil(arg); + } +}; + +struct Trunc { + template + static constexpr enable_if_floating_point Call(KernelContext*, Arg arg, Status*) { + return std::trunc(arg); + } +}; + // Generate a kernel given an arithmetic functor template