Skip to content

GH-55780: [C++] Raise instead of crashing in array_filter and array_take with mixed scalar and array arguments - #55781

Open
Chetansahney wants to merge 2 commits into
apache:mainfrom
Chetansahney:GH-55780-selection-scalar-args
Open

Chetansahney wants to merge 2 commits into
apache:mainfrom
Chetansahney:GH-55780-selection-scalar-args

Conversation

@Chetansahney

@Chetansahney Chetansahney commented Nov 25, 2026 •

Copy link
Copy Markdown

Rationale for this change

filter, array_filter and array_take crash the process when scalar and array arguments are mixed:

pc.filter(pa.scalar(1), pa.array([True]))
pc.array_filter(pa.scalar(1), pa.array([True]))
pc.array_take(pa.array([1]), pa.scalar(0))

The vector executor only promotes scalar arguments to arrays when all arguments are scalars. In the mixed case the scalar reaches the selection kernels, which read both arguments as arrays. filter crashes because it passes scalar values on to array_filter.

What changes are included in this PR?

array_filter and array_take are now registered as a small VectorFunction subclass that checks the argument kinds in Execute and returns TypeError when scalars and arrays are mixed. Putting the check there covers every selection kernel at once, instead of adding it to each of the type-specific exec functions. When all arguments are scalars they are still promoted to length-1 arrays, so that case is unchanged. take already rejected scalar indices itself, so its behavior is unchanged.

Are these changes tested?

Yes. The new TestFilterKernel.MixedScalarAndArrayArguments and TestTakeKernel.MixedScalarAndArrayArguments cover scalar values with array selection and array values with scalar selection for filter, array_filter and array_take, and check that all-scalar input still returns a length-1 array.

Run locally on Windows (MSVC, Release). Without the change, both new tests fail with an access violation (SEH exception with code 0xc0000005). With it, they pass, and arrow-compute-vector-selection-test (170 tests) and arrow-compute-vector-test pass in full.

Python: test_array_take_mixed_scalar_and_array_arguments and test_array_filter_mixed_scalar_and_array_arguments in test_compute.py check the TypeError for mixed arguments to array_take, array_filter and filter, and the all-scalar case. With pyarrow 26.0.1 both crash the interpreter (access violation); with pyarrow built from this branch they pass.

Are there any user-facing changes?

The calls above now raise ArrowTypeError instead of crashing.

This PR contains a "Critical Fix". It fixes a crash (segmentation fault) on user input.

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

…rray_take with mixed scalar and array arguments
Copilot AI balanced review requested due to automatic review settings November 25, 2026 09:51

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:51

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 was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Chetansahney

Copy link
Copy Markdown
Author

I've added Python tests in bd9a129: test_array_take_mixed_scalar_and_array_arguments and test_array_filter_mixed_scalar_and_array_arguments in test_compute.py. With pyarrow 26.0.1 they crash the interpreter, and with pyarrow built from this branch they pass. I've also reviewed this PR myself and ticked "Reviewed before submission by: Human" (see #55779 (comment)).

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.

2 participants