Repository navigation
Description
Describe the bug, including details regarding any error messages, version, and platform.
When a pyarrow.Table contains very large rows, whose size is very close to 2**31 - 1, segfault or allocator exceptions can be raised when performing a group_by on very big large_utf8 columns:
import pyarrow as pa
# MAX_SIZE is the largest value that can fit in a 32-bit signed integer.
MAX_SIZE = int(2**31) - 1
# Create a string whose length is very close to MAX_SIZE:
BIG_STR_LEN = MAX_SIZE - 1
print(f"{BIG_STR_LEN=} = 2**31 - {2**31 - BIG_STR_LEN}")
BIG_STR = "A" * BIG_STR_LEN
# Create a record batch with two rows, both containing the BIG_STR in each of their columns:
record_batch = pa.RecordBatch.from_pydict(
mapping={
"id": [BIG_STR, BIG_STR],
"other": [BIG_STR, BIG_STR],
},
schema=pa.schema(
{
"id": pa.large_utf8(),
"other": pa.large_utf8(),
}
),
)
# Create a table containing just the one RecordBatch:
table = pa.Table.from_batches([record_batch])
# Attempt to group by `id`:
ans = table.group_by(["id"]).aggregate([("other", "max")])
print(ans)On my M1 mac, the output from running this program looks like:
Pyarrow version: 14.0.1
BIG_STR_LEN=2147483646 = 2**31 - 2
libc++abi: terminating due to uncaught exception of type std::bad_alloc: std::bad_alloc
zsh: abort python main.py=
(in the previous version Pyarrow==10.0.1, this was a segfault instead of just a bad_alloc exception):
BIG_STR_LEN=2147483642 = 2**31 - 2
zsh: segmentation fault python main.py
I need to emphasize that there is more than enough memory to satisfy this operation. The problem is actually caused by integer overflow; I believe in one/both of the following places:
- In
VarLengthKeyEncoder::AddLengththere is no check that the size of the offset does not cause the length of the buffer to overflow anint32_t - In
GrouperImpl::Consumethere's no check that sums of theoffsets_batchdo not overflow anint32_t
Overflow in signed integer arithmetic is undefined behavior in C++, but typically results in "wrap-around". The result is that we're getting a negative int32_t value.
Then, when we construct
std::vector<uint8_t> key_bytes_batch(total_length);the total_length is converted from int32_t to uint64_t (since std::vector's length constructor accepts a size_t, which is uint64_t on most modern computers). The conversion goes like this:
int32_t(-1) ==> int64_t(-1) ==> uint64_t(2**64 - 1)
But 2**64 - 1 bytes is obviously more memory than is available on my computer. The overflow needs to be detected sooner to prevent this excessively-large number from being used as an impossible allocation request.
Component(s)
Python
Activity
thisisnic commentedon Nov 26, 2026
Thanks for reporting this, and for the analysis! I'm going through older bug issues to see if we can close them out. I used an AI agent to run a reduced version of this, and I've reviewed the results.
🤖 :
The diagnosis above is right: the summed key lengths in
GrouperImpl::Consumeoverflowedint32_t. That was fixed by #55170 (for #54885), which makes those offsets and the total length 64-bit. It is onmainand will be in 27.0.0.A lower-memory variant of the script above (2200
large_utf8keys of 1 MiB each, 2.15 GiB of key bytes in one batch,group_by(["id"]).aggregate([("other", "max")])):std::length_error,cannot create std::vector larger than max_size()The original script (two keys of 2**31 - 2 bytes, with a
large_utf8value column) was not run, as it needs about 10 GB.Closing as fixed. If you still see this on 27.0.0 or later, please open a new issue.