Skip to content

Commit

Permalink
use cancast
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Feb 21, 2024
1 parent 2f7ac44 commit ff91019
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions python/pyarrow/src/arrow/python/ipc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ Status CastingRecordBatchReader::Init(std::shared_ptr<RecordBatchReader> 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);
Expand Down
2 changes: 1 addition & 1 deletion python/pyarrow/tests/test_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff91019

Please sign in to comment.