sgilmore10 commented on code in PR #46885:
URL: https://github.com/apache/arrow/pull/46885#discussion_r2162312088


##########
matlab/src/cpp/arrow/matlab/tabular/proxy/table.cc:
##########
@@ -114,9 +116,63 @@ libmexclass::proxy::MakeResult Table::make(
                          error::SCHEMA_BUILDER_FINISH_ERROR_ID);
   const auto num_rows = arrow_arrays.size() == 0 ? 0 : 
arrow_arrays[0]->length();
   const auto table = arrow::Table::Make(schema, arrow_arrays, num_rows);
-  auto table_proxy = std::make_shared<TableProxy>(table);
+  return std::make_shared<TableProxy>(table);
+}
 
-  return table_proxy;
+libmexclass::proxy::MakeResult from_record_batches(const mda::StructArray& 
opts) {
+  using RecordBatchProxy = arrow::matlab::tabular::proxy::RecordBatch;
+  using TableProxy = arrow::matlab::tabular::proxy::Table;
+
+  size_t num_rows = 0;
+  const mda::TypedArray<uint64_t> record_batch_proxy_ids = 
opts[0]["RecordBatchProxyIDs"];
+
+  std::vector<std::shared_ptr<arrow::RecordBatch>> record_batches;
+  // Retrieve all of the Arrow RecordBatch Proxy instances from the libmexclass
+  // ProxyManager.
+  for (const auto& proxy_id : record_batch_proxy_ids) {
+    auto proxy = libmexclass::proxy::ProxyManager::getProxy(proxy_id);
+    auto record_batch_proxy = std::static_pointer_cast<RecordBatch>(proxy);
+    auto record_batch = record_batch_proxy->unwrap();
+    record_batches.push_back(record_batch);
+    num_rows += record_batches.back()->num_rows();
+  }
+
+  // This function can only be invoked if there's at least 1 RecordBatch,
+  // so this should be safe.
+  auto schema = record_batches[0]->schema();
+  // We've already validated the schemas are consistent.
+  // Just create a vector of columns.
+  size_t num_columns = schema->num_fields();
+  std::vector<std::shared_ptr<ChunkedArray>> columns(num_columns);
+
+  size_t num_batches = record_batches.size();
+
+  for (size_t i = 0; i < num_columns; ++i) {
+    std::vector<std::shared_ptr<Array>> column_arrays(num_batches);
+    for (size_t j = 0; j < num_batches; ++j) {
+      column_arrays[j] = record_batches[j]->column(i);
+    }
+    columns[i] = std::make_shared<ChunkedArray>(column_arrays, 
schema->field(i)->type());
+  }
+  const auto table = arrow::Table::Make(std::move(schema), std::move(columns), 
num_rows);
+  return std::make_shared<TableProxy>(table);
+}
+}  // anonymous namespace
+
+libmexclass::proxy::MakeResult Table::make(
+    const libmexclass::proxy::FunctionArguments& constructor_arguments) {
+  mda::StructArray opts = constructor_arguments[0];
+  const mda::StringArray method = opts[0]["Method"];
+
+  if (method[0] == u"FromArrays") {
+    return from_arrays(opts);
+  } else if (method[0] == u"FromRecordBatches") {
+    return from_record_batches(opts);
+  } else {
+    const std::string error_msg = "Unknown method";
+    return 
libmexclass::error::Error{error::TABLE_NUMERIC_INDEX_WITH_EMPTY_TABLE,

Review Comment:
   Good catch! I forgot to update this!



-- 
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