Skip to content

GH-51224: [C++] Keep the correct nulls when winsorizing a sliced array - #51251

Open
1fanwang wants to merge 6 commits into
apache:mainfrom
1fanwang:1fannnw/gh-51224-winsorize-slice-offset
Open

1fanwang wants to merge 6 commits into
apache:mainfrom
1fanwang:1fannnw/gh-51224-winsorize-slice-offset

Conversation

@1fanwang

@1fanwang 1fanwang commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Winsorizing a sliced array can put nulls in the wrong positions. Slices containing only nulls or NaNs can also return values from outside the slice.

Closes #51224.

What changes are included in this PR?

Use the bitmap helper to match validity bits to the output values. Preserve the input offset when returning the original buffers.

Are these changes tested?

I ran the native C++ tests and PyArrow on macOS arm64 with Apple clang 21. The new regression covers null-only, NaN-only and mixed null/NaN slices, with chunked inputs as controls.

With PyArrow loading the native library being tested, save this as repro-winsorize.py:

import pyarrow as pa
import pyarrow.compute as pc

parent = pa.array([1.0, 2.0, None, 4.0, None, 6.0, 7.0, 8.0])
result = pc.winsorize(parent.slice(2, 5), lower_limit=0.0, upper_limit=1.0)
print("mixed slice:", result.to_pylist(), flush=True)

parent = pa.array([1.0, 2.0, None, None, None, 3.0])
nulls = pc.winsorize(parent.slice(2, 3), lower_limit=0.0, upper_limit=1.0)
print("all-null slice:", nulls.to_pylist(), flush=True)
assert result.to_pylist() == [None, 4.0, None, 6.0, 7.0]
assert nulls.to_pylist() == [None, None, None]
Raw logs

Before:

$ python repro-winsorize.py
mixed slice: [0.0, 4.0, None, 6.0, None]
all-null slice: [1.0, 2.0, None]
AssertionError

After:

$ python repro-winsorize.py
mixed slice: [None, 4.0, None, 6.0, 7.0]
all-null slice: [None, None, None]
exit_code=0
$ build/release/arrow-compute-vector-test
[  PASSED  ] 1157 tests.
$ build/release/arrow-compute-aggregate-test --gtest_filter='*Quantile*:*TDigest*'
[  PASSED  ] 28 tests.

Are there any user-facing changes?

Sliced inputs retain the correct values and null positions.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Copilot AI lite review requested due to automatic review settings September 9, 2026 01:05
@1fanwang
1fanwang requested a review from pitrou as a code owner September 9, 2026 01:05
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has been automatically assigned in GitHub to PR creator.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has no components, please add labels for components.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change correctly addresses the sliced-offset bitmap bug and includes targeted regression tests, with only a minor optional performance optimization noted.

Pull request overview

Fixes winsorize producing an incorrect null pattern when the input is a sliced ArrayData with a non-zero offset, by ensuring the output’s zero-offset validity bitmap matches the slice rather than the parent.

Changes:

  • Copy the validity bitmap slice into the zero-offset output in Winsorize::ClipValues (instead of sharing the input bitmap buffer).
  • Add regression coverage for sliced floating-point and integer inputs (plus a null-free sliced input case).
File summaries
File Description
cpp/src/arrow/compute/kernels/vector_statistics.cc Ensures winsorize output validity bitmap matches sliced input offsets by copying the relevant bits.
cpp/src/arrow/compute/kernels/vector_statistics_test.cc Adds a regression test verifying correct null placement for sliced inputs across numeric types.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cpp/src/arrow/compute/kernels/vector_statistics.cc Outdated
Copilot AI review requested due to automatic review settings September 9, 2026 07:28
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Sep 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

ClipValues doesn’t explicitly force out->offset = 0, which can break correctness and potentially cause out-of-bounds writes when invoked with a non-zero output offset (e.g., sliced chunks in the chunked path).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread cpp/src/arrow/compute/kernels/vector_statistics.cc Outdated
Copilot AI review requested due to automatic review settings September 9, 2026 08:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The fix directly addresses the reported offset/bitmap bug with a targeted, safe change and includes a focused regression test that fails before and passes after the patch.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 9, 2026 08:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The fix is localized, addresses the described root cause (offset-aware validity), and is covered by targeted regression tests.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Comment thread cpp/src/arrow/compute/kernels/vector_statistics.cc Outdated
Comment thread cpp/src/arrow/compute/kernels/vector_statistics_test.cc Outdated
Comment thread cpp/src/arrow/compute/kernels/vector_statistics_test.cc Outdated
Comment thread cpp/src/arrow/compute/kernels/vector_statistics_test.cc Outdated
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has no components, please add labels for components.

singhpratech added a commit to singhpratech/ArrowMetal that referenced this pull request Sep 22, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has no components, please add labels for components.

Copilot AI review requested due to automatic review settings September 22, 2026 02:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The scalar early-return path can still mishandle validity offsets for sliced all-null/NaN inputs.

Review effort: Lite
Findings: None

Copilot AI review requested due to automatic review settings September 24, 2026 06:11
@1fanwang
1fanwang force-pushed the 1fannnw/gh-51224-winsorize-slice-offset branch from fdcef26 to 88f8f56 Compare September 24, 2026 06:11
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has no components, please add labels for components.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved review comments remain.

Review effort: Lite
Findings: None

@1fanwang
1fanwang force-pushed the 1fannnw/gh-51224-winsorize-slice-offset branch from 88f8f56 to 52b330f Compare September 24, 2026 07:09
singhpratech added a commit to singhpratech/ArrowMetal that referenced this pull request Sep 24, 2026
…d array

The output is zero-offset, so sharing a sliced input's validity buffer made
readers interpret it from bit 0 and move the nulls.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
… sliced

A zero-offset input is read from bit 0 in both the input and the output, so
copying it only adds an allocation.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
ExecChunked seeds the output from the input chunk, so a sliced chunk carried
its offset into buffers built for the slice alone.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang force-pushed the 1fannnw/gh-51224-winsorize-slice-offset branch from 52b330f to 97da9e3 Compare September 25, 2026 20:52
Copilot AI review requested due to automatic review settings September 25, 2026 20:52
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has been automatically assigned in GitHub to PR creator.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #51224 has no components, please add labels for components.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues were identified.

Review effort: Lite
Findings: None

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] winsorize on a sliced array ignores the offset: wrong rows come out null

3 participants