From 0263be56f6dbe09900c677a222923a1ac1ab08fd Mon Sep 17 00:00:00 2001 From: AlenkaF Date: Thu, 22 Jun 2023 10:31:35 +0200 Subject: [PATCH] Correct Append step for null scalar --- python/pyarrow/src/arrow/python/python_to_arrow.cc | 9 ++++++++- python/pyarrow/tests/test_convert_builtin.py | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/python/pyarrow/src/arrow/python/python_to_arrow.cc b/python/pyarrow/src/arrow/python/python_to_arrow.cc index 8041a0cdd1852..486bd840775b6 100644 --- a/python/pyarrow/src/arrow/python/python_to_arrow.cc +++ b/python/pyarrow/src/arrow/python/python_to_arrow.cc @@ -602,7 +602,14 @@ class PyPrimitiveConverter> if (PyValue::IsNull(this->options_, value)) { return this->primitive_builder_->AppendNull(); } else if (arrow::py::is_scalar(value)) { - return this->primitive_builder_->AppendNull(); + ARROW_ASSIGN_OR_RAISE(std::shared_ptr scalar, + arrow::py::unwrap_scalar(value)); + if (scalar->is_valid) { + return Status::Invalid("Cannot append scalar of type ", scalar->type->ToString(), + " to builder for type null"); + } else { + return this->primitive_builder_->AppendNull(); + } } else { ARROW_ASSIGN_OR_RAISE( auto converted, PyValue::Convert(this->primitive_type_, this->options_, value)); diff --git a/python/pyarrow/tests/test_convert_builtin.py b/python/pyarrow/tests/test_convert_builtin.py index 18a58ae988976..e2670aaed6e58 100644 --- a/python/pyarrow/tests/test_convert_builtin.py +++ b/python/pyarrow/tests/test_convert_builtin.py @@ -2437,7 +2437,7 @@ def test_array_accepts_pyarrow_scalar_with_type(seq, data, scalar_data, value_ty assert expect.equals(result) -def test_array_accepts_pyarrow_scalar_something(): +def test_array_accepts_pyarrow_scalar_from_compute(): arr = pa.array([1, 2, 3]) result = pa.array([arr.sum()]) expect = pa.array([6]) @@ -2473,3 +2473,8 @@ def test_array_accepts_pyarrow_scalar_errors(seq): match="Cannot append scalar of type string " "to builder for type int32"): pa.array([pa.scalar("a")], type=pa.int32()) + + with pytest.raises(pa.ArrowInvalid, + match="Cannot append scalar of type int64 " + "to builder for type null"): + pa.array([pa.scalar(1)], type=pa.null())