Skip to content

Commit

Permalink
prototype exporting to cdata interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sgilmore10 committed May 9, 2024
1 parent 3046501 commit 4fc6530
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions matlab/src/cpp/arrow/matlab/array/proxy/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include "arrow/util/utf8.h"
#include "arrow/c/bridge.h"

#include "arrow/matlab/array/proxy/array.h"
#include "arrow/matlab/array/proxy/wrap.h"
Expand All @@ -28,6 +29,7 @@

#include "libmexclass/proxy/ProxyManager.h"

#include <memory>
#include <sstream>

namespace arrow::matlab::array::proxy {
Expand All @@ -40,6 +42,7 @@ Array::Array(std::shared_ptr<arrow::Array> array) : array{std::move(array)} {
REGISTER_METHOD(Array, getType);
REGISTER_METHOD(Array, isEqual);
REGISTER_METHOD(Array, slice);
REGISTER_METHOD(Array, exportToC);
}

std::shared_ptr<arrow::Array> Array::unwrap() { return array; }
Expand Down Expand Up @@ -178,4 +181,20 @@ void Array::slice(libmexclass::proxy::method::Context& context) {
output[0]["TypeID"] = factory.createScalar(type_id);
context.outputs[0] = output;
}

void Array::exportToC(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;

struct ArrowArray* arrow_array = new ArrowArray();
struct ArrowSchema* arrow_schema = new ArrowSchema();

MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(
arrow::ExportArray(*array, arrow_array, arrow_schema), context, "arrow:cdata:error");

mda::ArrayFactory factory;
mda::StructArray output = factory.createStructArray({1, 1}, {"ArrowArrayAddress", "ArrowSchemaAddress"});
output[0]["ArrowArrayAddress"] = factory.createScalar(reinterpret_cast<uint64_t>(arrow_array));
output[0]["ArrowSchemaAddress"] = factory.createScalar(reinterpret_cast<uint64_t>(arrow_schema));
}

} // namespace arrow::matlab::array::proxy
2 changes: 2 additions & 0 deletions matlab/src/cpp/arrow/matlab/array/proxy/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Array : public libmexclass::proxy::Proxy {

void slice(libmexclass::proxy::method::Context& context);

void exportToC(libmexclass::proxy::method::Context& context);

std::shared_ptr<arrow::Array> array;
};

Expand Down

0 comments on commit 4fc6530

Please sign in to comment.