paleolimbot commented on code in PR #40070:
URL: https://github.com/apache/arrow/pull/40070#discussion_r1501013503


##########
python/pyarrow/src/arrow/python/ipc.cc:
##########
@@ -63,5 +64,59 @@ Result<std::shared_ptr<RecordBatchReader>> 
PyRecordBatchReader::Make(
   return reader;
 }
 
+CastingRecordBatchReader::CastingRecordBatchReader() {}
+
+Status CastingRecordBatchReader::Init(std::shared_ptr<RecordBatchReader> 
parent,
+                                      std::shared_ptr<Schema> schema) {
+  std::shared_ptr<Schema> src = parent->schema();
+
+  // Check for conformable number of columns
+  int num_fields = schema->num_fields();
+  if (src->num_fields() != num_fields) {
+    return Status::Invalid("Source has ", src->num_fields(), " but requested 
schema has ",
+                           num_fields);
+  }
+
+  // Try to cast an empty version of all the columns before succceeding
+  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));
+  }
+
+  parent_ = std::move(parent);
+  schema_ = std::move(schema);
+
+  return Status::OK();
+}
+
+std::shared_ptr<Schema> CastingRecordBatchReader::schema() const { return 
schema_; }
+
+Status CastingRecordBatchReader::ReadNext(std::shared_ptr<RecordBatch>* batch) 
{
+  std::shared_ptr<RecordBatch> out;
+  ARROW_RETURN_NOT_OK(parent_->ReadNext(&out));
+  if (!out) {
+    return Status::OK();
+  }
+
+  auto num_columns = out->num_columns();
+  ArrayVector columns(num_columns);
+  for (int i = 0; i < num_columns; i++) {
+    ARROW_ASSIGN_OR_RAISE(columns[i],
+                          compute::Cast(*out->column(i), 
schema_->field(i)->type()));

Review Comment:
   Done!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to