[Python] pa.record_batch raises TypeError for tuple data
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
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
can I work on this now...??
This issue needs some discussion on the approach to take first, before any PR is made.
@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.
There appear to be two ways to resolve this:
- Remove the Cython
listtype restriction onRecordBatch.from_arrays(arrays, ...)to accept any sequence, matchingTable.from_arraysand theSequence[...]typing stubs in #55264. - Keep
RecordBatch.from_arraysrestricted to lists and convert tuples vialist(data)insidepa.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.
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 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:
- Under the hood:
RecordBatch.from_arraysonly checkslen(arrays)andarrays[0]before passing it to_sanitize_arrays(arrays, ...)._sanitize_arraysproduces 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++. - Parity with
Table:Table.from_arrayshas always keptarraysuntyped and accepts tuples without issue. - Original intent: When
pa.record_batch()was added in ARROW-6263, it was explicitly written asif 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): BothRecordBatch.from_arraysandTable.from_arrayswere originally untyped (def from_arrays(names, arrays)). - Apr 2017 (
7d433dc27, ARROW-483 / PR #588):RecordBatch.from_arrayshad Cython types added (def from_arrays(list arrays, list names, dict metadata=None)), whileTable.from_arrayskeptarraysuntyped. - Jul 2018 (
ec9b72a23, ARROW-1715):list nameswas relaxed tonamesinRecordBatch.from_arraysto support schemas during unpickling, leavinglist arrays. - Dec 2018 (
9da458437, ARROW-2624):dict metadatawas relaxed tometadatato allow arbitrary mappings, leavinglist arrays. - Aug 2019 (
05bc63c3b, ARROW-6263):pa.record_batchandpa.tablefactory functions were added withisinstance(data, (list, tuple)).pa.tableworked becauseTable.from_arrayswas untyped, whilepa.record_batchfailed on tuples due to the remaining Cython check.
Circling back to this to ask if the changes look good and if i should proceed with the implementation @AlenkaF