forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-41803: [MATLAB] Add C Data Interface format import/export fu…
…nctionality for `arrow.tabular.RecordBatch` (apache#41817) ### Rationale for this change This pull requests adds two new APIs for importing and exporting `arrow.tabular.RecordBatch` instances using the C Data Interface format. **Example:** ```matlab >> T = table((1:3)', ["A"; "B"; "C"]); >> expected = arrow.recordBatch(T) expected = Arrow RecordBatch with 3 rows and 2 columns: Schema: Var1: Float64 | Var2: String First Row: 1 | "A" >> cArray = arrow.c.Array(); >> cSchema = arrow.c.Schema(); % Export the RecordBatch to C Data Interface Format >> expected.export(cArray.Address, cSchema.Address); % Import the RecordBatch from C Data Interface Format >> actual = arrow.tabular.RecordBatch.import(cArray, cSchema) actual = Arrow RecordBatch with 3 rows and 2 columns: Schema: Var1: Float64 | Var2: String First Row: 1 | "A" % The RecordBatch is the same after round-tripping to the C Data Interface format >> isequal(actual, expected) ans = logical 1 ``` ### What changes are included in this PR? 1. Added a new method `arrow.tabular.RecordBatch.export` for exporting `RecordBatch` objects to the C Data Interface format. 2. Added a new static method `arrow.tabular.RecordBatch.import` for importing `RecordBatch` objects from the C Data Interface format. 3. Added a new internal class `arrow.c.internal.RecordBatchImporter` for importing `RecordBatch` objects from the C Data Interface format. ### Are these changes tested? Yes. 1. Added a new test file `matlab/test/arrow/c/tRoundtripRecordBatch.m` which has basic round-trip tests for importing and exporting `RecordBatch` objects. ### Are there any user-facing changes? Yes. 1. Two new user-facing methods were added to `arrow.tabular.RecordBatch`. The first is `arrow.tabular.RecordBatch.export(cArrowArrayAddress, cArrowSchemaAddress)`. The second is `arrow.tabular.RecordBatch.import(cArray, cSchema)`. These APIs can be used to export/import `RecordBatch` objects using the C Data Interface format. ### Future Directions 1. Add integration tests for sharing data between MATLAB/mlarrow and Python/pyarrow running in the same process using the [MATLAB interface to Python](https://www.mathworks.com/help/matlab/call-python-libraries.html). 2. Add support for the Arrow [C stream interface format](https://arrow.apache.org/docs/format/CStreamInterface.html). ### Notes 1. Thanks to @ kevingurney for the help with this feature! * GitHub Issue: apache#41803 Authored-by: Sarah Gilmore <[email protected]> Signed-off-by: Sarah Gilmore <[email protected]>
- Loading branch information
1 parent
f904928
commit fe2d926
Showing
9 changed files
with
420 additions
and
54 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
matlab/src/cpp/arrow/matlab/c/proxy/record_batch_importer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "arrow/c/bridge.h" | ||
|
||
#include "arrow/matlab/c/proxy/record_batch_importer.h" | ||
#include "arrow/matlab/error/error.h" | ||
#include "arrow/matlab/tabular/proxy/record_batch.h" | ||
|
||
#include "libmexclass/proxy/ProxyManager.h" | ||
|
||
namespace arrow::matlab::c::proxy { | ||
|
||
RecordBatchImporter::RecordBatchImporter() { | ||
REGISTER_METHOD(RecordBatchImporter, import); | ||
} | ||
|
||
libmexclass::proxy::MakeResult RecordBatchImporter::make( | ||
const libmexclass::proxy::FunctionArguments& constructor_arguments) { | ||
return std::make_shared<RecordBatchImporter>(); | ||
} | ||
|
||
void RecordBatchImporter::import(libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
using namespace libmexclass::proxy; | ||
using RecordBatchProxy = arrow::matlab::tabular::proxy::RecordBatch; | ||
|
||
mda::StructArray args = context.inputs[0]; | ||
const mda::TypedArray<uint64_t> arrow_array_address_mda = args[0]["ArrowArrayAddress"]; | ||
const mda::TypedArray<uint64_t> arrow_schema_address_mda = | ||
args[0]["ArrowSchemaAddress"]; | ||
|
||
const auto arrow_array_address = uint64_t(arrow_array_address_mda[0]); | ||
const auto arrow_schema_address = uint64_t(arrow_schema_address_mda[0]); | ||
|
||
auto arrow_array = reinterpret_cast<struct ArrowArray*>(arrow_array_address); | ||
auto arrow_schema = reinterpret_cast<struct ArrowSchema*>(arrow_schema_address); | ||
|
||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto record_batch, | ||
arrow::ImportRecordBatch(arrow_array, arrow_schema), | ||
context, error::C_IMPORT_FAILED); | ||
|
||
auto record_batch_proxy = std::make_shared<RecordBatchProxy>(std::move(record_batch)); | ||
|
||
mda::ArrayFactory factory; | ||
const auto record_batch_proxy_id = ProxyManager::manageProxy(record_batch_proxy); | ||
const auto record_batch_proxy_id_mda = factory.createScalar(record_batch_proxy_id); | ||
|
||
context.outputs[0] = record_batch_proxy_id_mda; | ||
} | ||
|
||
} // namespace arrow::matlab::c::proxy |
37 changes: 37 additions & 0 deletions
37
matlab/src/cpp/arrow/matlab/c/proxy/record_batch_importer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include "libmexclass/proxy/Proxy.h" | ||
|
||
namespace arrow::matlab::c::proxy { | ||
|
||
class RecordBatchImporter : public libmexclass::proxy::Proxy { | ||
public: | ||
RecordBatchImporter(); | ||
|
||
~RecordBatchImporter() = default; | ||
|
||
static libmexclass::proxy::MakeResult make( | ||
const libmexclass::proxy::FunctionArguments& constructor_arguments); | ||
|
||
protected: | ||
void import(libmexclass::proxy::method::Context& context); | ||
}; | ||
|
||
} // namespace arrow::matlab::c::proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
matlab/src/matlab/+arrow/+c/+internal/RecordBatchImporter.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
%RECORDBATCHIMPORTER Imports Arrow RecordBatch using the C Data Interface | ||
% Format. | ||
|
||
% Licensed to the Apache Software Foundation (ASF) under one or more | ||
% contributor license agreements. See the NOTICE file distributed with | ||
% this work for additional information regarding copyright ownership. | ||
% The ASF licenses this file to you under the Apache License, Version | ||
% 2.0 (the "License"); you may not use this file except in compliance | ||
% with the License. You may obtain a copy of the License at | ||
% | ||
% http://www.apache.org/licenses/LICENSE-2.0 | ||
% | ||
% Unless required by applicable law or agreed to in writing, software | ||
% distributed under the License is distributed on an "AS IS" BASIS, | ||
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
% implied. See the License for the specific language governing | ||
% permissions and limitations under the License. | ||
|
||
classdef RecordBatchImporter | ||
|
||
properties (Hidden, SetAccess=private, GetAccess=public) | ||
Proxy | ||
end | ||
|
||
methods | ||
|
||
function obj = RecordBatchImporter() | ||
proxyName = "arrow.c.proxy.RecordBatchImporter"; | ||
proxy = arrow.internal.proxy.create(proxyName, struct()); | ||
obj.Proxy = proxy; | ||
end | ||
|
||
function recordBatch = import(obj, cArray, cSchema) | ||
arguments | ||
obj(1, 1) arrow.c.internal.RecordBatchImporter | ||
cArray(1, 1) arrow.c.Array | ||
cSchema(1, 1) arrow.c.Schema | ||
end | ||
args = struct(... | ||
ArrowArrayAddress=cArray.Address,... | ||
ArrowSchemaAddress=cSchema.Address... | ||
); | ||
proxyID = obj.Proxy.import(args); | ||
proxyName = "arrow.tabular.proxy.RecordBatch"; | ||
proxy = libmexclass.proxy.Proxy(Name=proxyName, ID=proxyID); | ||
recordBatch = arrow.tabular.RecordBatch(proxy); | ||
end | ||
|
||
end | ||
|
||
end | ||
|
Oops, something went wrong.