diff --git a/python/pyarrow/src/arrow/python/ipc.cc b/python/pyarrow/src/arrow/python/ipc.cc index 547cc84739a05..9a48e1445be17 100644 --- a/python/pyarrow/src/arrow/python/ipc.cc +++ b/python/pyarrow/src/arrow/python/ipc.cc @@ -77,12 +77,14 @@ Status CastingRecordBatchReader::Init(std::shared_ptr parent, num_fields); } - // Try to cast an empty version of all the columns before succceeding + // Ensure all columns can be cast before succeeding compute::CastOptions options; for (int i = 0; i < num_fields; i++) { - ARROW_ASSIGN_OR_RAISE(auto empty_array, MakeEmptyArray(src->field(i)->type())); - options.to_type = schema->field(i)->type(); - ARROW_ASSIGN_OR_RAISE(auto emtpy_array_dst, compute::Cast(empty_array, options)); + if (!compute::CanCast(*src->field(i)->type(), *schema->field(i)->type())) { + return Status::NotImplemented("Field ", i, " cannot be cast from ", + src->field(i)->type()->ToString(), " to ", + schema->field(i)->type()->ToString()); + } } parent_ = std::move(parent); diff --git a/python/pyarrow/tests/test_cffi.py b/python/pyarrow/tests/test_cffi.py index 351617a73f670..f20414fc976d0 100644 --- a/python/pyarrow/tests/test_cffi.py +++ b/python/pyarrow/tests/test_cffi.py @@ -578,7 +578,7 @@ def test_roundtrip_reader_capsule(constructor): obj = constructor(schema, batches) bad_schema = pa.schema({'ints': pa.int32()}) - with pytest.raises(pa.lib.ArrowNotImplementedError, match="Unsupported cast"): + with pytest.raises(pa.lib.ArrowNotImplementedError, match="Field 0 cannot be cast"): obj.__arrow_c_stream__(bad_schema.__arrow_c_schema__()) # Can work with matching schema