forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-35411: [MATLAB] Create a templated C++ Proxy Class for Numer…
…ic Arrays (apache#35479) ### Rationale for this change This pull request is a followup to apache#34563. To facilitate the implementation of future array types, we would like to first create a C++ template class for numeric arrays. We also want to start adding basic tests for the array functionality in the MATLAB interface. ### What changes are included in this PR? 1. Added a C++ template Class called `NumericArray` templated on `CType`. 2. Re-implemented the `Float64Array` C++ proxy class in terms of the new template class, i.e. `NumericArray<double>`. 3. Added a method called `double()` on the MATLAB Float64Array class to convert the arrow.[Type]Array to a MATLAB `double` array. 4. Added basic tests for round-tripping float64 arrays. 5. Created a base C++ proxy `Array` class that all proxy array classes will inherit from. 6. Renamed `Print()` to `ToString()` and made it return a string instead of printing to the screen. ### Are these changes tested? Yes, we added automated test cases to the test class `tFloat64Array.m`. In addition, we manually qualified these changes on macOS. ### Are there any user-facing changes? Yes, the `Print()` method is no longer public and there is now a method called `double()` on `arrow.array.Float64Array`. Included below is a simple example of using the `double()` method: ```matlab >> arrowArray = arrow.array.Float64Array([1, 2, 3]) arrowArray = [ 1, 2, 3 ] >> matlabArray = double(arrowArray) matlabArray = 1 2 3 >> class(arrowArray) ans = 'arrow.array.Float64Array' >> class(matlabArray) ans = 'double' ``` ### Future Directions 1. Support the rest of the numeric types. 2. Add an abstract MATLAB base class called `arrow.array.Array`. 3. Continue building out the methods (e.g. `length()`) 4. Support `null` values (validity bitmap). 5. Handle converting non-ascii characters from `UTF-8` to `UTF-16`. 6. Handle errors in the C++ layer. * Closes: apache#35411 Lead-authored-by: Sarah Gilmore <sgilmore@mathworks.com> Co-authored-by: sgilmore10 <74676073+sgilmore10@users.noreply.github.com> Co-authored-by: Kevin Gurney <kgurney@mathworks.com> Co-authored-by: Sutou Kouhei <kou@cozmixng.org> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
- Loading branch information
Showing
8 changed files
with
251 additions
and
84 deletions.
There are no files selected for viewing
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. | ||
|
||
#include "arrow/matlab/array/proxy/array.h" | ||
|
||
namespace arrow::matlab::array::proxy { | ||
|
||
Array::Array(const libmexclass::proxy::FunctionArguments& constructor_arguments) { | ||
|
||
// Register Proxy methods. | ||
REGISTER_METHOD(Array, ToString); | ||
REGISTER_METHOD(Array, ToMatlab); | ||
|
||
} | ||
|
||
void Array::ToString(libmexclass::proxy::method::Context& context) { | ||
::matlab::data::ArrayFactory factory; | ||
|
||
// TODO: handle non-ascii characters | ||
auto str_mda = factory.createScalar(array->ToString()); | ||
context.outputs[0] = str_mda; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,97 @@ | ||
// 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 "arrow/array.h" | ||
#include "arrow/array/data.h" | ||
#include "arrow/array/util.h" | ||
|
||
#include "arrow/builder.h" | ||
#include "arrow/type_traits.h" | ||
|
||
#include "arrow/matlab/array/proxy/array.h" | ||
|
||
#include "libmexclass/proxy/Proxy.h" | ||
|
||
namespace arrow::matlab::array::proxy { | ||
|
||
template<typename CType> | ||
class NumericArray : public arrow::matlab::array::proxy::Array { | ||
public: | ||
NumericArray(const libmexclass::proxy::FunctionArguments& constructor_arguments) | ||
: arrow::matlab::array::proxy::Array(constructor_arguments) { | ||
using ArrowType = typename arrow::CTypeTraits<CType>::ArrowType; | ||
using BuilderType = typename arrow::CTypeTraits<CType>::BuilderType; | ||
|
||
// Get the mxArray from constructor arguments | ||
const ::matlab::data::TypedArray<CType> numeric_mda = constructor_arguments[0]; | ||
const ::matlab::data::TypedArray<bool> make_copy = constructor_arguments[1]; | ||
|
||
// Get raw pointer of mxArray | ||
auto it(numeric_mda.cbegin()); | ||
auto dt = it.operator->(); | ||
|
||
const auto make_deep_copy = make_copy[0]; | ||
|
||
if (make_deep_copy) { | ||
BuilderType builder; | ||
auto st = builder.AppendValues(dt, numeric_mda.getNumberOfElements()); | ||
|
||
// TODO: handle error case | ||
if (st.ok()) { | ||
auto maybe_array = builder.Finish(); | ||
if (maybe_array.ok()) { | ||
array = *maybe_array; | ||
} | ||
} | ||
} else { | ||
const auto data_type = arrow::CTypeTraits<CType>::type_singleton(); | ||
const auto length = static_cast<int64_t>(numeric_mda.getNumberOfElements()); // cast size_t to int64_t | ||
|
||
// Do not make a copy when creating arrow::Buffer | ||
auto data_buffer = std::make_shared<arrow::Buffer>(reinterpret_cast<const uint8_t*>(dt), | ||
sizeof(CType) * numeric_mda.getNumberOfElements()); | ||
|
||
// TODO: Implement null support | ||
std::shared_ptr<arrow::Buffer> null_buffer = nullptr; | ||
|
||
auto array_data = arrow::ArrayData::Make(data_type, length, {null_buffer, data_buffer}); | ||
array = arrow::MakeArray(array_data); | ||
|
||
} | ||
} | ||
|
||
protected: | ||
void ToMatlab(libmexclass::proxy::method::Context& context) override { | ||
using ArrowArrayType = typename arrow::CTypeTraits<CType>::ArrayType; | ||
|
||
const auto num_elements = static_cast<size_t>(array->length()); | ||
const auto numeric_array = std::static_pointer_cast<ArrowArrayType>(array); | ||
const CType* const data_begin = numeric_array->raw_values(); | ||
const CType* const data_end = data_begin + num_elements; | ||
|
||
::matlab::data::ArrayFactory factory; | ||
|
||
// Constructs a TypedArray from the raw values. Makes a copy. | ||
::matlab::data::TypedArray<CType> result = factory.createArray({num_elements, 1}, data_begin, data_end); | ||
context.outputs[0] = result; | ||
} | ||
}; | ||
|
||
} |
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
Oops, something went wrong.