Skip to content

GH-55769: [C++] Use NaN as the floating-point identity for cumulative_max/min - #55832

Open
pierceluu10 wants to merge 1 commit into
apache:mainfrom
pierceluu10:fix/cumulative-leading-nan
Open

pierceluu10 wants to merge 1 commit into
apache:mainfrom
pierceluu10:fix/cumulative-leading-nan

Conversation

@pierceluu10

@pierceluu10 pierceluu10 commented Nov 28, 2026 •

Copy link
Copy Markdown

Rationale for this change

With the default start, cumulative_max and cumulative_min on floating-point input emit -inf / inf for every NaN before the first non-NaN value, so cumulative_max([NaN]) returns [-inf] while max([NaN]) returns NaN.

The default start comes from Identity<Max> / Identity<Min>, which since GH-51194 return -inf / +inf for floating-point types. Max and Min return the other operand when one side is NaN, so Max(-inf, NaN) is -inf and the identity property Op(identity, x) == x does not hold for NaN. NaN does satisfy it for every input, including NaN and the infinities.

What changes are included in this PR?

Identity<Max> and Identity<Min> return quiet NaN for floating-point types. Integer types are unchanged. Identity is only used for the default start in vector_cumulative_ops.cc, so no other kernel is affected.

An explicit start is unchanged: it is a real value and still applies from the first element, as suggested in the issue.

Are these changes tested?

Yes. TestCumulative.NaN now checks leading NaNs for float32 and float64 with the default start, and that an explicit start still applies to them. test_cumulative_max_min_leading_nan covers the same case on a chunked array whose first chunk is all NaN.

Both new tests fail on main (C++ got -inf / inf where NaN was expected; the Python test fails on pyarrow 26.0.1) and pass with this change. arrow-compute-vector-test passes (1157 tests), and the cumulative tests in test_compute.py pass. I also checked empty input, all-NaN input, nulls with and without skip_nulls, sliced arrays, chunked arrays with empty chunks, integer input and explicit starts of 0, NaN and -inf by hand. Tested on macOS arm64.

Are there any user-facing changes?

Yes. With the default start, leading NaNs in floating-point input now produce NaN, e.g. cumulative_max([NaN, 1, NaN, 2]) returns [NaN, 1, 1, 2] instead of [-inf, 1, 1, 2]. Results after the first non-NaN value are unchanged.

Was AI used for this PR?

Yes. I used Claude Code to reproduce the issue and draft the change, the tests and this description. I went through the diff and checked the identity argument myself (Max/Min pick the non-NaN operand, so NaN is the only value with Op(identity, x) == x for every float x), and reran the C++ and Python tests locally.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant