You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C++][Python] Casting extension types with list-based storage segfaults #37004
Describe the bug, including details regarding any error messages, version, and platform.
I'm taking a table and casting it to its own schema. This should always work, but it errors in a certain case.
(this is deep in some library code - please forgive how strange this scenario is; just trust me that it really does happen!)
# An extension type which wraps list-of-int32:classMyExtensionType(pa.PyExtensionType):
def__init__(self):
pa.PyExtensionType.__init__(self, pa.list_(pa.int32()))
def__reduce__(self):
returnMyExtensionType, (),
# an array of this type:ext_array=pa.array([[1, 2, 3]], MyExtensionType())
# Wrap that in a struct:struct_array=pa.StructArray.from_arrays([ext_array], "x")
# Wrap the struct in a table:table=pa.Table.from_arrays([struct_array], ["field1"])
All of the above is fine, and then:
table.cast(table.schema)
Blows up:
File ~/.pyenv/versions/3.10.10/envs/general-3.10/lib/python3.10/site-packages/pyarrow/table.pxi:3616, in pyarrow.lib.Table.cast()
File ~/.pyenv/versions/3.10.10/envs/general-3.10/lib/python3.10/site-packages/pyarrow/table.pxi:3798, in pyarrow.lib.Table.from_arrays()
File ~/.pyenv/versions/3.10.10/envs/general-3.10/lib/python3.10/site-packages/pyarrow/table.pxi:2962, in pyarrow.lib.Table.validate()
File ~/.pyenv/versions/3.10.10/envs/general-3.10/lib/python3.10/site-packages/pyarrow/error.pxi:100, in pyarrow.lib.check_status()
ArrowInvalid: Column 0: In chunk 0: Invalid: Struct child array #0 invalid: Invalid: Expected 1 child arrays in array of type list<item: int32>, got 0
I am not sure if the issue is in the struct or the underlying list type. But it seems to me that the casting of the column in the table.cast is behaving erroneous
Thanks for taking a look @AlenkaF. Here is a reproducer for FixedShapeTensor - it only happens if you go through the struct. The issue is mis-titled. It's actually a bug with Tables which contain a Struct field which have an ExtensionType which has a ListTyped-storage (whew, that's a lot!):
In [38]: importpyarrowaspaIn [39]: tensor_type=pa.fixed_shape_tensor(pa.int32(), (2, 2))
In [40]: arr= [[1, 2, 3, 4], [10, 20, 30, 40], [100, 200, 300, 400]]
In [41]: storage=pa.array(arr, pa.list_(pa.int32(), 4))
In [42]: tensor_array=pa.ExtensionArray.from_storage(tensor_type, storage)
...:
In [43]: pa.StructArray.from_arrays([tensor_array], "x")
Out[43]:
<pyarrow.lib.StructArrayobjectat0x154e5a680>--is_valid: allnotnull--child0 type: extension<arrow.fixed_shape_tensor>
[
[
1,
2,
3,
4
],
[
10,
20,
30,
40
],
[
100,
200,
300,
400
]
]
In [44]: struct_array=pa.StructArray.from_arrays([tensor_array], "x")
In [45]: table=pa.Table.from_arrays([struct_array], ["field1"])
In [46]: table.cast(table.schema)
---------------------------------------------------------------------------ArrowInvalidTraceback (mostrecentcalllast)
CellIn[46], line1---->1table.cast(table.schema)
File~/.pyenv/versions/3.10.12/envs/adam-core/lib/python3.10/site-packages/pyarrow/table.pxi:3616, inpyarrow.lib.Table.cast()
File~/.pyenv/versions/3.10.12/envs/adam-core/lib/python3.10/site-packages/pyarrow/table.pxi:3798, inpyarrow.lib.Table.from_arrays()
File~/.pyenv/versions/3.10.12/envs/adam-core/lib/python3.10/site-packages/pyarrow/table.pxi:2962, inpyarrow.lib.Table.validate()
File~/.pyenv/versions/3.10.12/envs/adam-core/lib/python3.10/site-packages/pyarrow/error.pxi:100, inpyarrow.lib.check_status()
ArrowInvalid: Column0: Inchunk0: Invalid: Structchildarray#0 invalid: Invalid: Expected 1 child arrays in array of type fixed_size_list<item: int32>[4], got 0
I asked Copilot to go through the backtrace and I checked the result. It does seem that, in both cases, when casting an extension type with list-based storage, the cast kernel produces ArrayData with the correct type but drops the child data. When the extension type then tries to wrap this into an ExtensionArray and calls MakeArray, it tries to construct a list array from that incomplete ArrayData and crashes.
In case that helps anybody starting to look for a fix. Will also update the title to reflect the underlying bug.
changed the title [-][Python] pyarrow.Table.cast does not handle extension types which wrap list types[/-][+][C++][Python] Casting extension types with list-based storage segfaults[/+]on May 8, 2026
Hello! I've looked into this issue and I've diagnosed the cause in array/data.cc. I've got a fix with C++/Python tests, and i've also checked it against the Python snippets in this thread. If no one is working on this, I'd be more than happy to open a PR :).
Activity
spenczar commentedon Aug 4, 2023
This seems to happen as well for FixedShapeTensor tensor types, which indicates it happens for fixed-size list types.
AlenkaF commentedon Aug 17, 2023
I can reproduce the first issue but cannot reproduce it for
FixedShapeTensorarray:I am not sure if the issue is in the struct or the underlying list type. But it seems to me that the casting of the column in the
table.castis behaving erroneousarrow/python/pyarrow/table.pxi
Line 3694 in 4fc2731
and I think this should work but it segfaults:
spenczar commentedon Aug 17, 2023
Thanks for taking a look @AlenkaF. Here is a reproducer for FixedShapeTensor - it only happens if you go through the struct. The issue is mis-titled. It's actually a bug with Tables which contain a Struct field which have an ExtensionType which has a ListTyped-storage (whew, that's a lot!):
NellyWhads commentedon Nov 21, 2024
Is this related to #34985?
AlenkaF commentedon May 8, 2026
I asked Copilot to go through the backtrace and I checked the result. It does seem that, in both cases, when casting an extension type with list-based storage, the cast kernel produces
ArrayDatawith the correct type but drops the child data. When the extension type then tries to wrap this into anExtensionArrayand callsMakeArray, it tries to construct a list array from that incompleteArrayDataand crashes.In case that helps anybody starting to look for a fix. Will also update the title to reflect the underlying bug.
[-][Python] pyarrow.Table.cast does not handle extension types which wrap list types[/-][+][C++][Python] Casting extension types with list-based storage segfaults[/+]connorsimms commentedon Jul 13, 2026
Hello! I've looked into this issue and I've diagnosed the cause in
array/data.cc. I've got a fix with C++/Python tests, and i've also checked it against the Python snippets in this thread. If no one is working on this, I'd be more than happy to open a PR :).connorsimms commentedon Jul 13, 2026
take
apacheGH-37004: [C++][Python] Fix dropped child data when viewing/cas…
GH-37004: [C++][Python] Fix dropped child data when viewing/casting e… (
pitrou commentedon Jul 22, 2026
Issue resolved by pull request 50502
#50502