Skip to content

[C++][Compute] partition_nth_indices segfaults or returns wrong result with ChunkedArray input #55775

Backlog

Description

@AlenkaF

Describe the bug, including details regarding any error messages, version, and platform.

In [4]: pc.partition_nth_indices(pa.chunked_array([[5, 4, 3], [2, 1, 0]]), pivot=1)
Out[4]: 
<pyarrow.lib.ChunkedArray object at 0x108120700>
[
  [
    0,
    1,
    2,
    3,
    4,
    5
  ]
]

vs

In [5]: pc.partition_nth_indices(pa.array([5, 4, 3, 2, 1, 0]), pivot=1)
Out[5]: 
<pyarrow.lib.UInt64Array object at 0x107fc1540>
[
  5,
  4,
  3,
  2,
  1,
  0
]

In a debug build the chunked array case segfaults, see https://github.com/apache/arrow/actions/runs/45373797129/job/138374519791?pr=49201.

Claude states: In vector_array_sort.cc, partition_nth_indices reuses the base kernel from array_sort_indices without resetting exec_chunked, so chunked input runs ArraySortIndicesChunked with PartitionNthOptions. Chunked input should either be supported properly or raise an error.

Found while working on the compute stubs (#49201).

Component(s)

C++

Activity

  1. rvcreationworld commented on Nov 25, 2026

    @rvcreationworld

    Hi @kou @pitrou,

    I looked into the failure in pc.partition_nth_indices when evaluated on a ChunkedArray.

    Findings

    When executing over an arrow::Array, PartitionNthIndicesKernel accesses a single contiguous data buffer. However, when called on a ChunkedArray, the kernel fails to resolve global logical indices across chunk boundaries, resulting in either per-chunk reset indices or out-of-bounds pointer dereferences (SIGSEGV).

    Proposed Solution

    1. Avoid materializing a full contiguous copy of the ChunkedArray in memory to prevent memory doubling on large datasets.
    2. Adapt PartitionNthIndicesKernel to follow the pattern used in SortIndices: create an indirect index array $[0, N-1]$ and supply a custom comparator that translates logical index $i$ to (chunk_idx, offset_in_chunk) via binary search or chunk offset lookup tables.
    3. Add GoogleTest coverage in vector_sort_test.cc with multi-chunk arrays of varying/empty chunk sizes, including null placement checks (NullPlacement::AtStart / AtEnd).

    Would you prefer the chunk offset lookup approach, or is there an ongoing effort to generalize PartitionNth under the broader Acero chunked compute interface?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions