arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] pa.record_batch raises TypeError for tuple data

Open AlenkaF opened this issue 3 weeks ago • 8 comments

Describe the bug, including details regarding any error messages, version, and platform.

pa.record_batch() checks isinstance(data, (list, tuple)) before passing data to RecordBatch.from_arrays

https://github.com/apache/arrow/blob/802102addbbbc386ef24e4c46d90644efe46a17d/python/pyarrow/table.pxi#L6046-L6048

but a tuple of arrays raises a TypeError as arrays is typed as list in Cython:

https://github.com/apache/arrow/blob/802102addbbbc386ef24e4c46d90644efe46a17d/python/pyarrow/table.pxi#L3431

Example:

>>> import pyarrow as pa
>>> pa.record_batch((pa.array([1]),), names=["a"])
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    pa.record_batch((pa.array([1]),), names=["a"])
  File "pyarrow/table.pxi", line 6080, in pyarrow.lib.record_batch
    return RecordBatch.from_arrays(data, names=names, schema=schema,
TypeError: Argument 'arrays' has incorrect type (expected list, got tuple)

The same data passed as a list works. Note: pa.table() works correctly:

>>> pa.table(([1],), names=["a"])
pyarrow.Table
a: int64
----
a: [[1]]

I have found the issue when reviewing __lib/table.pyi in https://github.com/apache/arrow/pull/55264

Component(s)

Python

AlenkaF avatar Nov 05 '26 15:11 AlenkaF

Hello @AlenkaF I would like to work on this issue and submit a solution along with the necessary tests. Would it be possible for me to take responsibility for this issue ? Thank you

Moemenmohamed24 avatar Nov 05 '26 23:11 Moemenmohamed24

can I work on this now...??

kartik00052 avatar Nov 06 '26 13:11 kartik00052

This issue needs some discussion on the approach to take first, before any PR is made.

AlenkaF avatar Nov 06 '26 13:11 AlenkaF

@kartik00052 please stop indiscriminately posting on all newly created issues or we will have to ban you. There is a reason the previous PR was closed. Please investigate a little bit before indiscriminately pinging if you can work on an issue. Check whether there's someone else working on it (does it have PRs? is it assigned?) also check if there are related closed PRs what is the reason, if there is a conversation take that into account.

raulcd avatar Nov 06 '26 13:11 raulcd

There appear to be two ways to resolve this:

  1. Remove the Cython list type restriction on RecordBatch.from_arrays(arrays, ...) to accept any sequence, matching Table.from_arrays and the Sequence[...] typing stubs in #55264.
  2. Keep RecordBatch.from_arrays restricted to lists and convert tuples via list(data) inside pa.record_batch().

Option 1 provides consistency across both tabular constructors. Does removing the list restriction on RecordBatch.from_arrays match the intended design?

Happy to put together a PR with tests for (1) if that is the preferred direction.

Awesome06 avatar Nov 08 '26 08:11 Awesome06

My first question, which I haven't investigated yet, is: why is arrays parameter typed to list in Cython, is there a special reason behind that decision? If yes, we can not blindly remove the type restriction there.

No PRs yet, please.

AlenkaF avatar Nov 09 '26 05:11 AlenkaF

@AlenkaF I checked the history and implementation of RecordBatch.from_arrays to see why arrays was typed to list in Cython:

There does not appear to be an architectural or safety reason for it, looks to be a historical artifact from 2017.

A few details:

  1. Under the hood: RecordBatch.from_arrays only checks len(arrays) and arrays[0] before passing it to _sanitize_arrays(arrays, ...). _sanitize_arrays produces an internal Python list (converted_arrays), which is then used to populate the C++ vector[shared_ptr[CArray]]. The input sequence is never passed directly to C++.
  2. Parity with Table: Table.from_arrays has always kept arrays untyped and accepts tuples without issue.
  3. Original intent: When pa.record_batch() was added in ARROW-6263, it was explicitly written as if isinstance(data, (list, tuple)): return RecordBatch.from_arrays(data, ...). Tuples were clearly intended to be supported, but were blocked by the legacy Cython typing.

Given that, dropping the list restriction on RecordBatch.from_arrays (def from_arrays(arrays, ...)) should be completely safe and aligns it with Table.from_arrays and the typing stubs in #55264.

Commit history details
  • Oct 2016 (a9747ceac, ARROW-312): Both RecordBatch.from_arrays and Table.from_arrays were originally untyped (def from_arrays(names, arrays)).
  • Apr 2017 (7d433dc27, ARROW-483 / PR #588): RecordBatch.from_arrays had Cython types added (def from_arrays(list arrays, list names, dict metadata=None)), while Table.from_arrays kept arrays untyped.
  • Jul 2018 (ec9b72a23, ARROW-1715): list names was relaxed to names in RecordBatch.from_arrays to support schemas during unpickling, leaving list arrays.
  • Dec 2018 (9da458437, ARROW-2624): dict metadata was relaxed to metadata to allow arbitrary mappings, leaving list arrays.
  • Aug 2019 (05bc63c3b, ARROW-6263): pa.record_batch and pa.table factory functions were added with isinstance(data, (list, tuple)). pa.table worked because Table.from_arrays was untyped, while pa.record_batch failed on tuples due to the remaining Cython check.

Awesome06 avatar Nov 09 '26 05:11 Awesome06

Circling back to this to ask if the changes look good and if i should proceed with the implementation @AlenkaF

Awesome06 avatar Nov 18 '26 09:11 Awesome06