kou commented on code in PR #35479: URL: https://github.com/apache/arrow/pull/35479#discussion_r1187981731
########## matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h: ########## @@ -0,0 +1,93 @@ +// 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/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 bool make_deep_copy = make_copy[0]; Review Comment: ```suggestion const auto make_deep_copy = make_copy[0]; ``` ########## matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h: ########## @@ -0,0 +1,93 @@ +// 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/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 bool 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 { + // Pass pointer to Arrow array constructor that takes a buffer + // Do not make a copy when creating arrow::Buffer + std::shared_ptr<arrow::Buffer> buffer( + new arrow::Buffer(reinterpret_cast<const uint8_t*>(dt), + sizeof(CType) * numeric_mda.getNumberOfElements())); Review Comment: ```suggestion auto buffer = std::make_shared<arrow::Buffer>(reinterpret_cast<const uint8_t*>(dt), sizeof(CType) * numeric_mda.getNumberOfElements()); ``` ########## matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h: ########## @@ -0,0 +1,93 @@ +// 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/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 bool 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 { + // Pass pointer to Arrow array constructor that takes a buffer + // Do not make a copy when creating arrow::Buffer + std::shared_ptr<arrow::Buffer> buffer( + new arrow::Buffer(reinterpret_cast<const uint8_t*>(dt), + sizeof(CType) * numeric_mda.getNumberOfElements())); + + // Construct arrow::NumericArray specialization using arrow::Buffer. + // pass in nulls information...we could compute and provide the number of nulls here too + std::shared_ptr<arrow::Array> array_wrapper( + new arrow::NumericArray<ArrowType>(numeric_mda.getNumberOfElements(), buffer, + nullptr, // TODO: fill validity bitmap with data + -1)); Review Comment: How about creating an `ArrayData` https://github.com/apache/arrow/blob/main/cpp/src/arrow/array/data.h and then creating an `Array` by `MakeArray()` https://github.com/apache/arrow/blob/main/cpp/src/arrow/array/util.h#L34-L38 ? We may be able to share the same implementation with other array types with this approach. ########## matlab/test/arrow/array/tFloat64Array.m: ########## @@ -24,18 +28,77 @@ function verifyOnMatlabPath(testCase) end end - methods(TestMethodSetup) - function setupTempWorkingDirectory(testCase) - import matlab.unittest.fixtures.WorkingFolderFixture; - testCase.applyFixture(WorkingFolderFixture); - end - end - methods(Test) - function Basic(testCase) - A = arrow.array.Float64Array([1, 2, 3]); + function Basic(testCase, MakeDeepCopy) + A = arrow.array.Float64Array([1, 2, 3], DeepCopy=MakeDeepCopy); className = string(class(A)); testCase.verifyEqual(className, "arrow.array.Float64Array"); end + + function ShallowCopy(testCase) + % By default, Float64Array does not create a deep copy on + % construction when constructed from a MATLAB array. Instead, + % it stores a shallow copy of the array keep the memory alive. Review Comment: ```suggestion % By default, Float64Array does not create a deep copy on % construction when constructed from a MATLAB array. Instead, % it stores a shallow copy of the array keep the memory alive. ``` ########## matlab/src/matlab/+arrow/+array/Float64Array.m: ########## @@ -20,25 +20,38 @@ Proxy end - properties (Access=private) + properties (Hidden, SetAccess=private) MatlabArray end methods - function obj = Float64Array(matlabArray) - obj.MatlabArray = matlabArray; - obj.Proxy = libmexclass.proxy.Proxy("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {obj.MatlabArray}); + function obj = Float64Array(data, opts) + arguments + data + opts.DeepCopy = false + end + + validateattributes(data, "double", ["2d", "nonsparse", "real"]); + if ~isempty(data), validateattributes(data, "double", "vector"); end + % Store a reference to the array if not doing a deep copy + if (~opts.DeepCopy), obj.MatlabArray = data; end + obj.Proxy = libmexclass.proxy.Proxy("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {data, opts.DeepCopy}); end - function Print(obj) - obj.Proxy.Print(); + function data = double(obj) + data = obj.Proxy.ToMatlab(); end end methods (Access=protected) function displayScalarObject(obj) - obj.Print(); + disp(obj.ToString()); end end + methods (Access = private) Review Comment: ```suggestion methods (Access=private) ``` ########## matlab/test/arrow/array/tFloat64Array.m: ########## @@ -24,18 +28,77 @@ function verifyOnMatlabPath(testCase) end end - methods(TestMethodSetup) - function setupTempWorkingDirectory(testCase) - import matlab.unittest.fixtures.WorkingFolderFixture; - testCase.applyFixture(WorkingFolderFixture); - end - end - methods(Test) - function Basic(testCase) - A = arrow.array.Float64Array([1, 2, 3]); + function Basic(testCase, MakeDeepCopy) + A = arrow.array.Float64Array([1, 2, 3], DeepCopy=MakeDeepCopy); className = string(class(A)); testCase.verifyEqual(className, "arrow.array.Float64Array"); end + + function ShallowCopy(testCase) + % By default, Float64Array does not create a deep copy on + % construction when constructed from a MATLAB array. Instead, + % it stores a shallow copy of the array keep the memory alive. + A = arrow.array.Float64Array([1, 2, 3]); + testCase.verifyEqual(A.MatlabArray, [1 2 3]); + testCase.verifyEqual(double(A), [1 2 3]'); + + A = arrow.array.Float64Array([1, 2, 3], DeepCopy=false); + testCase.verifyEqual(A.MatlabArray, [1 2 3]); + testCase.verifyEqual(double(A), [1 2 3]'); + end + + function DeepCopy(testCase) + % Verify Float64Array does not store shallow copy of the MATLAB + % array if DeepCopy=true was supplied. Review Comment: ```suggestion % Verify Float64Array does not store shallow copy of the MATLAB % array if DeepCopy=true was supplied. ``` ########## matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h: ########## @@ -0,0 +1,93 @@ +// 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/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 bool 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 { + // Pass pointer to Arrow array constructor that takes a buffer + // Do not make a copy when creating arrow::Buffer + std::shared_ptr<arrow::Buffer> buffer( + new arrow::Buffer(reinterpret_cast<const uint8_t*>(dt), + sizeof(CType) * numeric_mda.getNumberOfElements())); + + // Construct arrow::NumericArray specialization using arrow::Buffer. + // pass in nulls information...we could compute and provide the number of nulls here too + std::shared_ptr<arrow::Array> array_wrapper( + new arrow::NumericArray<ArrowType>(numeric_mda.getNumberOfElements(), buffer, + nullptr, // TODO: fill validity bitmap with data + -1)); + array = array_wrapper; + } + } + + protected: + void ToMatlab(libmexclass::proxy::method::Context& context) override { + using ArrowArrayType = typename arrow::CTypeTraits<CType>::ArrayType; + + const size_t num_elements = static_cast<size_t>(array->length()); Review Comment: ```suggestion const auto num_elements = static_cast<size_t>(array->length()); ``` -- 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]
