diff --git a/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h b/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h index 5b22fde6025..3d247214468 100644 --- a/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h +++ b/cpp/src/arrow/compute/kernels/base_arithmetic_internal.h @@ -834,11 +834,10 @@ 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. + // Max returns the other operand when one is NaN, so NaN is the identity + // for floating-point types. -infinity is not: Max(-inf, NaN) is -inf. if constexpr (std::is_floating_point_v || std::is_same_v) { - return -std::numeric_limits::infinity(); + return std::numeric_limits::quiet_NaN(); } else { return std::numeric_limits::lowest(); } @@ -849,9 +848,9 @@ template <> struct Identity { template static constexpr Value value() { - // Mirror of Identity: use +infinity for floating-point types. + // Mirror of Identity: use NaN for floating-point types. if constexpr (std::is_floating_point_v || std::is_same_v) { - return std::numeric_limits::infinity(); + return std::numeric_limits::quiet_NaN(); } else { return std::numeric_limits::max(); } 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..cdadb4867d2 100644 --- a/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc +++ b/cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc @@ -945,6 +945,21 @@ TEST(TestCumulative, NaN) { CheckVectorUnary("cumulative_min", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"), ArrayFromJSON(float64(), "[5, 4, 4, 2, 1]")); + // GH-55769: leading NaNs must not be replaced by the default start + for (const auto& ty : FloatingPointTypes()) { + CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[NaN, NaN, 1, NaN, 2]"), + ArrayFromJSON(ty, "[NaN, NaN, 1, 1, 2]")); + CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[NaN, NaN, 2, NaN, 1]"), + ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 1]")); + + // An explicit start is a real value and still applies from the first element + CumulativeOptions options(0.0); + CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[NaN, -1, 1]"), + ArrayFromJSON(ty, "[0, 0, 1]"), &options); + CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[NaN, 1, -1]"), + ArrayFromJSON(ty, "[0, 0, -1]"), &options); + } + // mean with NaN is always Nan CheckVectorUnary("cumulative_mean", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"), ArrayFromJSON(float64(), "[5, 4.5, NaN, NaN, NaN]")); diff --git a/python/pyarrow/tests/test_compute.py b/python/pyarrow/tests/test_compute.py index 0e0260808d6..7916c580ea1 100644 --- a/python/pyarrow/tests/test_compute.py +++ b/python/pyarrow/tests/test_compute.py @@ -3746,6 +3746,17 @@ def test_cumulative_max_min_negative_default_start(): assert pc.cumulative_min(arr).to_pylist() == [np.inf, 2.5] +@pytest.mark.numpy +@pytest.mark.parametrize('ty', [pa.float32(), pa.float64()]) +def test_cumulative_max_min_leading_nan(ty): + # GH-55769: leading NaNs were replaced by the default start (-inf / inf) + arr = pa.chunked_array([[np.nan], [np.nan, 1.0, np.nan, 2.0]], type=ty) + np.testing.assert_array_equal(pc.cumulative_max(arr), + [np.nan, np.nan, 1.0, 1.0, 2.0]) + np.testing.assert_array_equal(pc.cumulative_min(arr), + [np.nan, np.nan, 1.0, 1.0, 1.0]) + + @pytest.mark.numpy @pytest.mark.parametrize('start', (0.5, 3.5, 6.5)) @pytest.mark.parametrize('skip_nulls', (True, False))