Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions cpp/src/arrow/util/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ class Chunker {
Status AppendNull() {
auto status = converter_->AppendNull();
if (ARROW_PREDICT_FALSE(status.IsCapacityError())) {
if (converter_->builder()->length() == 0) {
// Builder length == 0 means the individual element is too large to append.
// In this case, no need to try again.
if (converter_->builder()->length() == 0 || length_ == 0) {
// Builder length == 0 means the individual element is too large to
// append. Length == 0 means the current chunk has no rows to finish.
// In either case, no need to try again.
return status;
}
ARROW_RETURN_NOT_OK(FinishChunk());
Expand All @@ -300,7 +301,10 @@ class Chunker {
Status Append(InputType value) {
auto status = converter_->Append(value);
if (ARROW_PREDICT_FALSE(status.IsCapacityError())) {
if (converter_->builder()->length() == 0) {
if (converter_->builder()->length() == 0 || length_ == 0) {
// Builder length == 0 means the individual element is too large to
// append. Length == 0 means the current chunk has no rows to finish.
// In either case, no need to try again.
return status;
}
ARROW_RETURN_NOT_OK(FinishChunk());
Expand Down Expand Up @@ -334,6 +338,11 @@ class Chunker {
length_ -= 1;
offset -= 1;
}
if (length_ == 0) {
// Nothing was committed to the current chunk, so retrying the same
// element cannot make progress.
return status;
}
ARROW_RETURN_NOT_OK(FinishChunk());
} else if (!status.ok()) {
return status;
Expand Down Expand Up @@ -367,6 +376,11 @@ class Chunker {
length_ -= 1;
offset -= 1;
}
if (length_ == 0) {
// Nothing was committed to the current chunk, so retrying the same
// element cannot make progress.
return status;
}
ARROW_RETURN_NOT_OK(FinishChunk());
} else if (!status.ok()) {
return status;
Expand Down
4 changes: 2 additions & 2 deletions python/pyarrow/src/arrow/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ struct PyBytesView {
size = PyByteArray_GET_SIZE(obj);
is_utf8 = false;
} else if (PyMemoryView_Check(obj)) {
PyObject* ref = PyMemoryView_GetContiguous(obj, PyBUF_READ, 'C');
ref.reset(PyMemoryView_GetContiguous(obj, PyBUF_READ, 'C'));
RETURN_IF_PYERROR();
Py_buffer* buffer = PyMemoryView_GET_BUFFER(ref);
Py_buffer* buffer = PyMemoryView_GET_BUFFER(ref.obj());
bytes = reinterpret_cast<const char*>(buffer->buf);
size = buffer->len;
is_utf8 = false;
Expand Down
37 changes: 32 additions & 5 deletions python/pyarrow/src/arrow/python/python_to_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,11 @@ class PyListConverter : public ListConverter<T, PyConverter, PyConverterTrait> {
class PyStructConverter : public StructConverter<PyConverter, PyConverterTrait> {
public:
Status Append(PyObject* value) override {
return AppendWithCapacityRecovery([this, value]() { return AppendValue(value); });
}

protected:
Status AppendValue(PyObject* value) {
if (PyValue::IsNull(this->options_, value)) {
return this->struct_builder_->AppendNull();
} else if (arrow::py::is_scalar(value)) {
Expand All @@ -1026,17 +1031,16 @@ class PyStructConverter : public StructConverter<PyConverter, PyConverterTrait>
return this->struct_builder_->Append();
default:
RETURN_NOT_OK(InferInputKind(value));
return Append(value);
return AppendValue(value);
}
}

protected:
Status Init(MemoryPool* pool) override {
RETURN_NOT_OK((StructConverter<PyConverter, PyConverterTrait>::Init(pool)));

// This implementation will check the child values before appending itself,
// so no rewind is necessary
this->rewind_on_overflow_ = false;
// Child builders are appended one at a time. If a later child overflows,
// the chunker must discard the partially appended row before retrying.
this->rewind_on_overflow_ = true;

// Store the field names as a PyObjects for dict matching
num_fields_ = this->struct_type_->num_fields();
Expand All @@ -1056,6 +1060,29 @@ class PyStructConverter : public StructConverter<PyConverter, PyConverterTrait>
return Status::OK();
}

template <typename AppendFunc>
Status AppendWithCapacityRecovery(AppendFunc&& append) {
const int64_t length_before = this->struct_builder_->length();
const Status status = append();
if (!status.IsCapacityError()) {
return status;
}

// If one child raises CapacityError, other children may already contain a
// partially appended row. Pad the missing children so the struct remains
// internally consistent; the chunker drops this placeholder row.
for (const auto& child : this->children_) {
const auto& builder = child->builder();
if (builder->length() == length_before) {
RETURN_NOT_OK(builder->AppendEmptyValue());
}
}
if (this->struct_builder_->length() == length_before) {
RETURN_NOT_OK(this->struct_builder_->Append());
}
return status;
}

Status InferInputKind(PyObject* value) {
// Infer input object's type, note that heterogeneous sequences are not allowed
if (PyDict_Check(value)) {
Expand Down
45 changes: 45 additions & 0 deletions python/pyarrow/tests/test_convert_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,18 @@ def test_sequence_bytes():
assert arr.to_pylist() == [b'foo', b'dada', b'data', u1, b'bar', None]


def test_sequence_memoryview_releases_buffer():
import mmap

value = mmap.mmap(-1, 1)
try:
with memoryview(value) as view:
arr = pa.array([view], type=pa.binary())
assert arr.to_pylist() == [b"\x00"]
finally:
value.close()


@pytest.mark.parametrize("ty", [pa.string(), pa.large_string(), pa.string_view()])
def test_sequence_utf8_to_unicode(ty):
# ARROW-1225
Expand Down Expand Up @@ -2167,6 +2179,24 @@ def test_map_from_tuples():
pa.array([entry], type=pa.map_('i4', 'i4'))


@pytest.mark.large_memory
def test_map_child_capacity_error_does_not_abort():
# GH-44643: the key is appended before the item raises CapacityError.
# The partially appended map row must not make MapBuilder abort on Finish.
import mmap

value = mmap.mmap(-1, 2**31)
try:
with memoryview(value) as view:
with pytest.raises(pa.ArrowCapacityError):
pa.array(
[[(b"key", view)]],
type=pa.map_(pa.string(), pa.string()),
)
finally:
value.close()


def test_dictionary_from_boolean():
typ = pa.dictionary(pa.int8(), value_type=pa.bool_())
a = pa.array([False, False, True, False, True], type=typ)
Expand Down Expand Up @@ -2419,6 +2449,21 @@ def test_auto_chunking_map_type():
assert len(arr.chunk(1)) == 1


@pytest.mark.large_memory
def test_auto_chunking_map_of_strings():
# GH-44643: overflow in the item builder must chunk the whole map row.
value = b"x" * (50 * 1024 * 1024)
data = [[(b"key", value)]] * 41
arr = pa.array(data, type=pa.map_(pa.string(), pa.string()))

assert isinstance(arr, pa.ChunkedArray)
assert arr.num_chunks == 2
arr.validate(full=True)
assert len(arr.chunk(0)) == 40
assert len(arr.chunk(1)) == 1
assert arr.chunk(1)[0].as_py() == [("key", value.decode("ascii"))]


@pytest.mark.large_memory
@pytest.mark.parametrize(('ty', 'char'), [
(pa.string(), 'x'),
Expand Down