diff --git a/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h b/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h index 5b22fde6025..a73298eb276 100644 --- a/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h +++ b/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h @@ -834,11 +834,9 @@ template <> struct Identity { template static constexpr Value value() { - // Note that `min()` returns the smallest positive value for - // floating-point types, and `lowest()` doesn't satisfy the identity - // property for -inf inputs, so use -infinity for those types. - if constexpr (std::is_floating_point_v || std::is_same_v) { - return -std::numeric_limits::infinity(); + // Max ignores NaN when the other operand is a number. + if constexpr (std::numeric_limits::has_quiet_NaN) { + return std::numeric_limits::quiet_NaN(); } else { return std::numeric_limits::lowest(); } @@ -849,9 +847,9 @@ template <> struct Identity { template static constexpr Value value() { - // Mirror of Identity: use +infinity for floating-point types. - if constexpr (std::is_floating_point_v || std::is_same_v) { - return std::numeric_limits::infinity(); + // Min ignores NaN when the other operand is a number. + if constexpr (std::numeric_limits::has_quiet_NaN) { + return std::numeric_limits::quiet_NaN(); } else { return std::numeric_limits::max(); } diff --git a/cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc b/cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc index 3c4f833b752..1363367ca60 100644 --- a/cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc +++ b/cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc @@ -241,26 +241,23 @@ const FunctionDoc cumulative_prod_checked_doc{ const FunctionDoc cumulative_max_doc{ "Compute the cumulative max over a numeric input", ("`values` must be numeric. Return an array/chunked array which is the\n" - "cumulative max computed over `values`. The default start is the minimum\n" - "value of input type (so that any other value will replace the\n" - "start as the new maximum)."), + "cumulative max computed over `values`. NaNs are ignored once a non-NaN\n" + "value is available. Without a start value, leading NaNs produce NaN."), {"values"}, "CumulativeOptions"}; const FunctionDoc cumulative_min_doc{ "Compute the cumulative min over a numeric input", ("`values` must be numeric. Return an array/chunked array which is the\n" - "cumulative min computed over `values`. The default start is the maximum\n" - "value of input type (so that any other value will replace the\n" - "start as the new minimum)."), + "cumulative min computed over `values`. NaNs are ignored once a non-NaN\n" + "value is available. Without a start value, leading NaNs produce NaN."), {"values"}, "CumulativeOptions"}; const FunctionDoc cumulative_mean_doc{ "Compute the cumulative mean over a numeric input", ("`values` must be numeric. Return an array/chunked array which is the\n" - "cumulative mean computed over `values`. CumulativeOptions::start_value is \n" - "ignored."), + "cumulative mean computed over `values`. The start option is ignored."), {"values"}, "CumulativeOptions"}; @@ -355,7 +352,7 @@ void RegisterVectorCumulativeSum(FunctionRegistry* registry) { registry, "cumulative_max", cumulative_max_doc); MakeVectorCumulativeStatefulFunction( - registry, "cumulative_mean", cumulative_max_doc); + registry, "cumulative_mean", cumulative_mean_doc); } } // namespace arrow::compute::internal diff --git a/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc b/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc index 6880b7c2720..e13fef63e5a 100644 --- a/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc @@ -937,13 +937,19 @@ TEST(TestCumulative, NaN) { CheckVectorUnary("cumulative_prod", ArrayFromJSON(float64(), "[1, 2, NaN, 4, 5]"), ArrayFromJSON(float64(), "[1, 2, NaN, NaN, NaN]")); - // max with NaN is always ignored because Nan > a always returns false - CheckVectorUnary("cumulative_max", ArrayFromJSON(float64(), "[1, 2, NaN, 4, 5]"), - ArrayFromJSON(float64(), "[1, 2, 2, 4, 5]")); - - // min with NaN is always ignored because Nan < a always returns false - CheckVectorUnary("cumulative_min", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"), - ArrayFromJSON(float64(), "[5, 4, 4, 2, 1]")); + for (const auto& ty : FloatingPointTypes()) { + auto values = ArrayFromJSON(ty, "[NaN, NaN, 2, NaN, 1, 3]"); + CheckVectorUnary("cumulative_max", values, + ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 2, 3]")); + CheckVectorUnary("cumulative_min", values, + ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 1, 1]")); + + CumulativeOptions start(4.0); + CheckVectorUnary("cumulative_max", values, ArrayFromJSON(ty, "[4, 4, 4, 4, 4, 4]"), + &start); + CheckVectorUnary("cumulative_min", values, ArrayFromJSON(ty, "[4, 4, 2, 2, 1, 1]"), + &start); + } // mean with NaN is always Nan CheckVectorUnary("cumulative_mean", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"), @@ -970,8 +976,7 @@ TEST(TestCumulative, NegativeValues) { CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[-2.5, -1.5, -3.5, -0.5]"), ArrayFromJSON(ty, "[-2.5, -1.5, -1.5, -0.5]"), &options); - // The default start must compare lower (higher for min) than every value - // of the type, including infinities + // Infinities are valid extrema. CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[-Inf, -2.5]"), ArrayFromJSON(ty, "[-Inf, -2.5]"), &options); CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[Inf, 2.5]"),