Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/src/arrow/dataset/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ Result<std::shared_ptr<Table>> AsyncScanner::TakeRows(const Array& indices) {
}

Result<std::shared_ptr<Table>> AsyncScanner::Head(int64_t num_rows) {
if (num_rows < 0) {
return Status::Invalid("num_rows must be non-negative");
}

if (num_rows == 0) {
return Table::FromRecordBatches(options()->projected_schema, {});
}
Expand Down
13 changes: 13 additions & 0 deletions python/pyarrow/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,19 @@ def test_head(dataset, dataset_reader):
result = fragment.head(1024, columns=['i64']).to_pydict()
assert result == {'i64': list(range(5))}

# ARROW-#55295: Check for negative num_rows
@pytest.mark.parquet
def test_head_negative_num_rows(dataset):
with pytest.raises(pa.ArrowInvalid, match="num_rows must be non-negative"):
dataset.head(-1)

with pytest.raises(pa.ArrowInvalid, match="num_rows must be non-negative"):
dataset.scanner().head(-1)

fragment = next(dataset.get_fragments())
with pytest.raises(pa.ArrowInvalid, match="num_rows must be non-negative"):
fragment.head(-1)


@pytest.mark.parquet
def test_take(dataset, dataset_reader):
Expand Down
Loading