diff --git a/cpp/src/arrow/util/converter.h b/cpp/src/arrow/util/converter.h index d987bf3061fe..b40649fd691a 100644 --- a/cpp/src/arrow/util/converter.h +++ b/cpp/src/arrow/util/converter.h @@ -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()); @@ -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()); @@ -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; @@ -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; diff --git a/python/pyarrow/src/arrow/python/common.h b/python/pyarrow/src/arrow/python/common.h index a81782330b5e..dfd1a6d084c5 100644 --- a/python/pyarrow/src/arrow/python/common.h +++ b/python/pyarrow/src/arrow/python/common.h @@ -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(buffer->buf); size = buffer->len; is_utf8 = false; diff --git a/python/pyarrow/src/arrow/python/python_to_arrow.cc b/python/pyarrow/src/arrow/python/python_to_arrow.cc index 5b83875813a1..32491e53b62f 100644 --- a/python/pyarrow/src/arrow/python/python_to_arrow.cc +++ b/python/pyarrow/src/arrow/python/python_to_arrow.cc @@ -1007,6 +1007,11 @@ class PyListConverter : public ListConverter { class PyStructConverter : public StructConverter { 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)) { @@ -1026,17 +1031,16 @@ class PyStructConverter : public StructConverter 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::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(); @@ -1056,6 +1060,29 @@ class PyStructConverter : public StructConverter return Status::OK(); } + template + 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)) { diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index 3ac7be88eaa0..7cb5a34f9030 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -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 @@ -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) @@ -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'),