Chetansahney opened a new pull request, #55781:
URL: https://github.com/apache/arrow/pull/55781

   ### Rationale for this change
   
   `filter`, `array_filter` and `array_take` crash the process when scalar and 
array arguments are mixed:
   
   ```python
   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.
   
   ### 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](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
 please disclose below whether and how AI was used in this PR.
   
   **PR code and description written by:**
   
   - [ ] Human
   - [x] AI
   
   **Reviewed before submission by:**
   
   - [ ] Human
   - [ ] AI
   - [ ] Not reviewed
   
   * GitHub Issue: #55780
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to