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

   ### Rationale for this change
   Fixes #55295. Currently, `Dataset.head`, `Scanner.head`, and `Fragment.head` 
accept negative `num_rows` (e.g. `head(-1)`). `AsyncScanner::Head` passes the 
negative count directly into `RecordBatch::Slice(0, num_rows)`, producing 
invalid record batches with negative lengths that cause `Table.validate()` to 
fail with `Array length is negative` and `len()` in Python to fail with 
`SystemError`.
   
   ### What changes are included in this PR?
   - In `AsyncScanner::Head`, check `if (num_rows < 0)` and return 
`Status::Invalid("num_rows must be non-negative")`.
   - In C++ `cpp/src/arrow/dataset/scanner_test.cc`, add 
`ASSERT_RAISES(Invalid, scanner->Head(-1))` to `TestScanner.Head`.
   - In Python `python/pyarrow/tests/test_dataset.py`, test that negative rows 
raise `ValueError` (as `ArrowInvalid` subclasses `ValueError`) for both 
`dataset_reader.head(dataset, -1)` and `fragment.head(-1)`.
   
   ### Are these changes tested?
   Yes, added C++ unit test in `scanner_test.cc` and Python tests in 
`test_dataset.py`.
   
   ### Are there any user-facing changes?
   Yes, calling `head()` with a negative `num_rows` now returns an 
`ArrowInvalid` error instead of returning a malformed Table with negative row 
count.
   
   ### 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:**
   
   - [x] Human
   - [x] AI
   
   **Reviewed before submission by:**
   
   - [x] Human
   - [ ] AI
   - [ ] Not reviewed
   
   * GitHub Issue: #55295
   


-- 
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