AdvancedUno opened a new pull request, #50369:
URL: https://github.com/apache/arrow/pull/50369
### Rationale for this change
`Table::Slice` on a table with **no columns** does not adjust the row count.
`SimpleTable::Slice` computes `num_rows` inside the per-column loop, so with
zero columns the loop never runs and `num_rows` keeps the raw `length` argument
instead of being clamped to the rows actually available.
```python
import pyarrow as pa
table = pa.table({'col': range(3)})
table.slice(1).num_rows # 2 (correct)
table.select([])[1:].num_rows # 2 (correct - __getitem__ normalizes)
table.select([]).slice(1).num_rows # 3 (WRONG, should be 2)
table.select([]).slice(1, 4).num_rows # 4 (WRONG, should be 2)
This violates the contract documented in table.h: "If there are not enough
rows in the table, the length will be adjusted accordingly."
### What changes are included in this PR?
- cpp/src/arrow/table.cc : in SimpleTable::Slice, seed num_rows with a
clamped value, std::max<int64_t>(0, std::min(length, num_rows() - offset)), so
it is correct even when the column vector is empty. When columns exist the loop
still overwrites num_rows with column->length(), so behavior for tables with
columns is unchanged. The single-arg Table::Slice(offset) routes through this
method, so it is fixed as well.
- cpp/src/arrow/table_test.cc: new TEST_F(TestTable, SliceZeroColumns).
- python/pyarrow/tests/test_table.py: new test_table_slice_no_columns
### Are these changes tested?
Yes. The new C++ and Python regression tests cover slicing a zero-column
table with offset only, offset + length, an offset past the end , and
consistency with a table whose columns were all removed using SelectColumns({}).
### Are there any user-facing changes?
Yes. Table.slice() on a table with no columns now returns the correct
num_rows instead of echoing back the raw length argument. This is a bug fix,
there are no API changes.
Closes #30894
--
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]