This is an automated email from the ASF dual-hosted git repository.
kevingurney pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new e821473445 GH-36734: [MATLAB] template
arrow::matlab::proxy::NumericArray on ArrowType instead of CType (#36738)
e821473445 is described below
commit e8214734459eff5cfc9e67e8b1fdef46f6d8c2ea
Author: sgilmore10 <[email protected]>
AuthorDate: Tue Jul 18 13:07:26 2023 -0400
GH-36734: [MATLAB] template arrow::matlab::proxy::NumericArray on ArrowType
instead of CType (#36738)
### Rationale for this change
We decided to change the template parameter on
`arrow::matlab::proxy::NumericArray` to `ArrowType` from `CType` to avoid
writing duplicate code. If `proxy::NumericArray` is templated on `ArrowType`,
we can use it to implement the proxies for `Date64Array`, `Date32Array`,
`Time32Array`, `Time64Array`, and `TimestampArray`. This will help us avoid
duplicating code.
### What changes are included in this PR?
1. Changed the template on `proxy::NumericArray` from `CType` to `ArrowType`
2. Re-implemented the C++ proxy object used for `TimestampArray` in terms
of `proxy::NumericArray<arrow::TimestampType>`
3. Defined a template specialization for `NumericArray::make` when the
template parameter is `arrow::TimestampType`
4. Defined a `proxy::Traits` `struct` that is templated on `ArrowType`.
Specializations of `Traits` define a`TypeProxy` typedef that can be used at
compile-time to get the proxy class that is used to wrap an `ArrowType`.
### Are these changes tested?
Existing tests used.
### Are there any user-facing changes?
No.
* Closes: #36734
Authored-by: Sarah Gilmore <[email protected]>
Signed-off-by: Kevin Gurney <[email protected]>
---
.../cpp/arrow/matlab/array/proxy/numeric_array.h | 66 +++++++++++++--
.../arrow/matlab/array/proxy/timestamp_array.cc | 99 ----------------------
.../cpp/arrow/matlab/array/proxy/timestamp_array.h | 43 ----------
matlab/src/cpp/arrow/matlab/proxy/factory.cc | 23 +++--
matlab/src/cpp/arrow/matlab/type/proxy/traits.h | 90 ++++++++++++++++++++
matlab/tools/cmake/BuildMatlabArrowInterface.cmake | 1 -
6 files changed, 158 insertions(+), 164 deletions(-)
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h
b/matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h
index c66c1d044f..f358e05db6 100644
--- a/matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h
+++ b/matlab/src/cpp/arrow/matlab/array/proxy/numeric_array.h
@@ -24,7 +24,7 @@
#include "arrow/type_traits.h"
#include "arrow/matlab/array/proxy/array.h"
-#include "arrow/matlab/type/proxy/primitive_ctype.h"
+#include "arrow/matlab/type/proxy/traits.h"
#include "arrow/matlab/error/error.h"
#include "arrow/matlab/bit/pack.h"
@@ -33,20 +33,23 @@
#include "libmexclass/proxy/Proxy.h"
+#include "arrow/matlab/type/time_unit.h"
+#include "arrow/util/utf8.h"
+
namespace arrow::matlab::array::proxy {
-template<typename CType>
+template<typename ArrowType>
class NumericArray : public arrow::matlab::array::proxy::Array {
public:
- using ArrowType = typename arrow::CTypeTraits<CType>::ArrowType;
NumericArray(const std::shared_ptr<arrow::NumericArray<ArrowType>>
numeric_array)
: arrow::matlab::array::proxy::Array{std::move(numeric_array)} {}
static libmexclass::proxy::MakeResult make(const
libmexclass::proxy::FunctionArguments& constructor_arguments) {
using MatlabBuffer = arrow::matlab::buffer::MatlabBuffer;
+ using CType = typename arrow::TypeTraits<ArrowType>::CType;
using NumericArray = arrow::NumericArray<ArrowType>;
- using NumericArrayProxy = typename
arrow::matlab::array::proxy::NumericArray<CType>;
+ using NumericArrayProxy = typename proxy::NumericArray<ArrowType>;
::matlab::data::StructArray opts = constructor_arguments[0];
@@ -68,10 +71,11 @@ class NumericArray : public
arrow::matlab::array::proxy::Array {
protected:
void toMATLAB(libmexclass::proxy::method::Context& context) override {
- using ArrowArrayType = typename
arrow::CTypeTraits<CType>::ArrayType;
+ using CType = typename arrow::TypeTraits<ArrowType>::CType;
+ using NumericArray = arrow::NumericArray<ArrowType>;
const auto num_elements = static_cast<size_t>(array->length());
- const auto numeric_array =
std::static_pointer_cast<ArrowArrayType>(array);
+ const auto numeric_array =
std::static_pointer_cast<NumericArray>(array);
const CType* const data_begin = numeric_array->raw_values();
const CType* const data_end = data_begin + num_elements;
@@ -83,11 +87,55 @@ class NumericArray : public
arrow::matlab::array::proxy::Array {
}
std::shared_ptr<type::proxy::Type> typeProxy() override {
- using ArrowTypeProxy = type::proxy::PrimitiveCType<CType>;
+ using TypeProxy = typename type::proxy::Traits<ArrowType>::TypeProxy;
auto type = std::static_pointer_cast<ArrowType>(array->type());
- return std::make_shared<ArrowTypeProxy>(std::move(type));
+ return std::make_shared<TypeProxy>(std::move(type));
}
-
};
+ // Specialization of NumericArray::Make for arrow::TimestampType.
+ template <>
+ libmexclass::proxy::MakeResult
NumericArray<arrow::TimestampType>::make(const
libmexclass::proxy::FunctionArguments& constructor_arguments) {
+ namespace mda = ::matlab::data;
+ using MatlabBuffer = arrow::matlab::buffer::MatlabBuffer;
+ using TimestampArray = arrow::TimestampArray;
+ using TimestampArrayProxy =
arrow::matlab::array::proxy::NumericArray<arrow::TimestampType>;
+
+ mda::StructArray opts = constructor_arguments[0];
+
+ // Get the mxArray from constructor arguments
+ const mda::TypedArray<int64_t> timestamp_mda = opts[0]["MatlabArray"];
+ const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"];
+
+ const mda::TypedArray<mda::MATLABString> timezone_mda =
opts[0]["TimeZone"];
+ const mda::TypedArray<mda::MATLABString> units_mda =
opts[0]["TimeUnit"];
+
+ // extract the time zone string
+ const std::u16string& u16_timezone = timezone_mda[0];
+ MATLAB_ASSIGN_OR_ERROR(const auto timezone,
+ arrow::util::UTF16StringToUTF8(u16_timezone),
+ error::UNICODE_CONVERSION_ERROR_ID);
+
+ // extract the time unit
+ const std::u16string& u16_timeunit = units_mda[0];
+ MATLAB_ASSIGN_OR_ERROR(const auto time_unit,
+
arrow::matlab::type::timeUnitFromString(u16_timeunit),
+ error::UKNOWN_TIME_UNIT_ERROR_ID)
+
+ // create the timestamp_type
+ auto data_type = arrow::timestamp(time_unit, timezone);
+ auto array_length =
static_cast<int64_t>(timestamp_mda.getNumberOfElements()); // cast size_t to
int64_t
+
+ auto data_buffer = std::make_shared<MatlabBuffer>(timestamp_mda);
+
+ // Pack the validity bitmap values.
+ MATLAB_ASSIGN_OR_ERROR(auto packed_validity_bitmap,
+ bit::packValid(validity_bitmap_mda),
+ error::BITPACK_VALIDITY_BITMAP_ERROR_ID);
+
+ auto array_data = arrow::ArrayData::Make(data_type, array_length,
{packed_validity_bitmap, data_buffer});
+ auto timestamp_array =
std::static_pointer_cast<TimestampArray>(arrow::MakeArray(array_data));
+ return
std::make_shared<TimestampArrayProxy>(std::move(timestamp_array));
+ }
+
}
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.cc
b/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.cc
deleted file mode 100644
index b9bbf3d7e7..0000000000
--- a/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.cc
+++ /dev/null
@@ -1,99 +0,0 @@
-// 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/timestamp_array.h"
-#include "arrow/matlab/type/proxy/timestamp_type.h"
-
-#include "arrow/matlab/error/error.h"
-#include "arrow/matlab/bit/pack.h"
-#include "arrow/matlab/bit/unpack.h"
-#include "arrow/matlab/buffer/matlab_buffer.h"
-
-#include "arrow/matlab/type/time_unit.h"
-#include "arrow/util/utf8.h"
-#include "arrow/type.h"
-
-namespace arrow::matlab::array::proxy {
-
- TimestampArray::TimestampArray(std::shared_ptr<arrow::TimestampArray>
array)
- : arrow::matlab::array::proxy::Array{std::move(array)} {}
-
- libmexclass::proxy::MakeResult TimestampArray::make(const
libmexclass::proxy::FunctionArguments& constructor_arguments) {
- namespace mda = ::matlab::data;
- using MatlabBuffer = arrow::matlab::buffer::MatlabBuffer;
- using TimestampArray = arrow::TimestampArray;
- using TimestampArrayProxy =
arrow::matlab::array::proxy::TimestampArray;
-
- mda::StructArray opts = constructor_arguments[0];
-
- // Get the mxArray from constructor arguments
- const mda::TypedArray<int64_t> timestamp_mda = opts[0]["MatlabArray"];
- const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"];
-
- const mda::TypedArray<mda::MATLABString> timezone_mda =
opts[0]["TimeZone"];
- const mda::TypedArray<mda::MATLABString> units_mda =
opts[0]["TimeUnit"];
-
- // extract the time zone string
- const std::u16string& u16_timezone = timezone_mda[0];
- MATLAB_ASSIGN_OR_ERROR(const auto timezone,
- arrow::util::UTF16StringToUTF8(u16_timezone),
- error::UNICODE_CONVERSION_ERROR_ID);
-
- // extract the time unit
- const std::u16string& u16_timeunit = units_mda[0];
- MATLAB_ASSIGN_OR_ERROR(const auto time_unit,
-
arrow::matlab::type::timeUnitFromString(u16_timeunit),
- error::UKNOWN_TIME_UNIT_ERROR_ID)
-
- // create the timestamp_type
- auto data_type = arrow::timestamp(time_unit, timezone);
- auto array_length =
static_cast<int64_t>(timestamp_mda.getNumberOfElements()); // cast size_t to
int64_t
-
- auto data_buffer = std::make_shared<MatlabBuffer>(timestamp_mda);
-
- // Pack the validity bitmap values.
- MATLAB_ASSIGN_OR_ERROR(auto packed_validity_bitmap,
- bit::packValid(validity_bitmap_mda),
- error::BITPACK_VALIDITY_BITMAP_ERROR_ID);
-
- auto array_data = arrow::ArrayData::Make(data_type, array_length,
{packed_validity_bitmap, data_buffer});
- auto timestamp_array =
std::static_pointer_cast<TimestampArray>(arrow::MakeArray(array_data));
- return
std::make_shared<TimestampArrayProxy>(std::move(timestamp_array));
- }
-
- void TimestampArray::toMATLAB(libmexclass::proxy::method::Context&
context) {
- namespace mda = ::matlab::data;
-
- const auto num_elements = static_cast<size_t>(array->length());
- const auto timestamp_array =
std::static_pointer_cast<arrow::TimestampArray>(array);
- const int64_t* const data_begin = timestamp_array->raw_values();
- const int64_t* const data_end = data_begin + num_elements;
-
- mda::ArrayFactory factory;
-
- // Constructs a TypedArray from the raw values. Makes a copy.
- mda::TypedArray<int64_t> result = factory.createArray({num_elements,
1}, data_begin, data_end);
- context.outputs[0] = result;
- }
-
- std::shared_ptr<type::proxy::Type> TimestampArray::typeProxy() {
- using TimestampProxyType = type::proxy::TimestampType;
- auto type =
std::static_pointer_cast<arrow::TimestampType>(array->type());
- return std::make_shared<TimestampProxyType>(std::move(type));
-
- }
-}
diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.h
b/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.h
deleted file mode 100644
index a312a129a2..0000000000
--- a/matlab/src/cpp/arrow/matlab/array/proxy/timestamp_array.h
+++ /dev/null
@@ -1,43 +0,0 @@
-// 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/matlab/array/proxy/array.h"
-
-#include "libmexclass/proxy/Proxy.h"
-
-#include "arrow/type_fwd.h"
-
-namespace arrow::matlab::array::proxy {
-
-class TimestampArray : public arrow::matlab::array::proxy::Array {
- public:
- TimestampArray(std::shared_ptr<arrow::TimestampArray> array);
-
- static libmexclass::proxy::MakeResult make(const
libmexclass::proxy::FunctionArguments& constructor_arguments);
-
- protected:
- void toMATLAB(libmexclass::proxy::method::Context& context) override;
-
- std::shared_ptr<type::proxy::Type> typeProxy() override;
-
-};
-
-}
diff --git a/matlab/src/cpp/arrow/matlab/proxy/factory.cc
b/matlab/src/cpp/arrow/matlab/proxy/factory.cc
index 0f7751035a..2fb3207e59 100644
--- a/matlab/src/cpp/arrow/matlab/proxy/factory.cc
+++ b/matlab/src/cpp/arrow/matlab/proxy/factory.cc
@@ -18,7 +18,6 @@
#include "arrow/matlab/array/proxy/boolean_array.h"
#include "arrow/matlab/array/proxy/numeric_array.h"
#include "arrow/matlab/array/proxy/string_array.h"
-#include "arrow/matlab/array/proxy/timestamp_array.h"
#include "arrow/matlab/tabular/proxy/record_batch.h"
#include "arrow/matlab/error/error.h"
#include "arrow/matlab/type/proxy/primitive_ctype.h"
@@ -30,19 +29,19 @@
namespace arrow::matlab::proxy {
libmexclass::proxy::MakeResult Factory::make_proxy(const ClassName&
class_name, const FunctionArguments& constructor_arguments) {
- REGISTER_PROXY(arrow.array.proxy.Float32Array ,
arrow::matlab::array::proxy::NumericArray<float>);
- REGISTER_PROXY(arrow.array.proxy.Float64Array ,
arrow::matlab::array::proxy::NumericArray<double>);
- REGISTER_PROXY(arrow.array.proxy.UInt8Array ,
arrow::matlab::array::proxy::NumericArray<uint8_t>);
- REGISTER_PROXY(arrow.array.proxy.UInt16Array ,
arrow::matlab::array::proxy::NumericArray<uint16_t>);
- REGISTER_PROXY(arrow.array.proxy.UInt32Array ,
arrow::matlab::array::proxy::NumericArray<uint32_t>);
- REGISTER_PROXY(arrow.array.proxy.UInt64Array ,
arrow::matlab::array::proxy::NumericArray<uint64_t>);
- REGISTER_PROXY(arrow.array.proxy.Int8Array ,
arrow::matlab::array::proxy::NumericArray<int8_t>);
- REGISTER_PROXY(arrow.array.proxy.Int16Array ,
arrow::matlab::array::proxy::NumericArray<int16_t>);
- REGISTER_PROXY(arrow.array.proxy.Int32Array ,
arrow::matlab::array::proxy::NumericArray<int32_t>);
- REGISTER_PROXY(arrow.array.proxy.Int64Array ,
arrow::matlab::array::proxy::NumericArray<int64_t>);
+ REGISTER_PROXY(arrow.array.proxy.Float32Array ,
arrow::matlab::array::proxy::NumericArray<arrow::FloatType>);
+ REGISTER_PROXY(arrow.array.proxy.Float64Array ,
arrow::matlab::array::proxy::NumericArray<arrow::DoubleType>);
+ REGISTER_PROXY(arrow.array.proxy.UInt8Array ,
arrow::matlab::array::proxy::NumericArray<arrow::UInt8Type>);
+ REGISTER_PROXY(arrow.array.proxy.UInt16Array ,
arrow::matlab::array::proxy::NumericArray<arrow::UInt16Type>);
+ REGISTER_PROXY(arrow.array.proxy.UInt32Array ,
arrow::matlab::array::proxy::NumericArray<arrow::UInt32Type>);
+ REGISTER_PROXY(arrow.array.proxy.UInt64Array ,
arrow::matlab::array::proxy::NumericArray<arrow::UInt64Type>);
+ REGISTER_PROXY(arrow.array.proxy.Int8Array ,
arrow::matlab::array::proxy::NumericArray<arrow::Int8Type>);
+ REGISTER_PROXY(arrow.array.proxy.Int16Array ,
arrow::matlab::array::proxy::NumericArray<arrow::Int16Type>);
+ REGISTER_PROXY(arrow.array.proxy.Int32Array ,
arrow::matlab::array::proxy::NumericArray<arrow::Int32Type>);
+ REGISTER_PROXY(arrow.array.proxy.Int64Array ,
arrow::matlab::array::proxy::NumericArray<arrow::Int64Type>);
REGISTER_PROXY(arrow.array.proxy.BooleanArray ,
arrow::matlab::array::proxy::BooleanArray);
REGISTER_PROXY(arrow.array.proxy.StringArray ,
arrow::matlab::array::proxy::StringArray);
- REGISTER_PROXY(arrow.array.proxy.TimestampArray,
arrow::matlab::array::proxy::TimestampArray);
+ REGISTER_PROXY(arrow.array.proxy.TimestampArray,
arrow::matlab::array::proxy::NumericArray<arrow::TimestampType>);
REGISTER_PROXY(arrow.tabular.proxy.RecordBatch ,
arrow::matlab::tabular::proxy::RecordBatch);
REGISTER_PROXY(arrow.type.proxy.Float32Type ,
arrow::matlab::type::proxy::PrimitiveCType<float>);
REGISTER_PROXY(arrow.type.proxy.Float64Type ,
arrow::matlab::type::proxy::PrimitiveCType<double>);
diff --git a/matlab/src/cpp/arrow/matlab/type/proxy/traits.h
b/matlab/src/cpp/arrow/matlab/type/proxy/traits.h
new file mode 100644
index 0000000000..3d9a957a5e
--- /dev/null
+++ b/matlab/src/cpp/arrow/matlab/type/proxy/traits.h
@@ -0,0 +1,90 @@
+// 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/type_fwd.h"
+
+#include "arrow/matlab/type/proxy/primitive_ctype.h"
+#include "arrow/matlab/type/proxy/timestamp_type.h"
+#include "arrow/matlab/type/proxy/string_type.h"
+
+namespace arrow::matlab::type::proxy {
+
+ template <typename ArrowType>
+ struct Traits;
+
+ template <>
+ struct Traits<arrow::FloatType> {
+ using TypeProxy = PrimitiveCType<float>;
+ };
+
+ template <>
+ struct Traits<arrow::DoubleType> {
+ using TypeProxy = PrimitiveCType<double>;
+ };
+
+ template <>
+ struct Traits<arrow::Int8Type> {
+ using TypeProxy = PrimitiveCType<int8_t>;
+ };
+
+ template <>
+ struct Traits<arrow::Int16Type> {
+ using TypeProxy = PrimitiveCType<int16_t>;
+ };
+
+ template <>
+ struct Traits<arrow::Int32Type> {
+ using TypeProxy = PrimitiveCType<int32_t>;
+ };
+
+ template <>
+ struct Traits<arrow::Int64Type> {
+ using TypeProxy = PrimitiveCType<int64_t>;
+ };
+
+ template <>
+ struct Traits<arrow::UInt8Type> {
+ using TypeProxy = PrimitiveCType<uint8_t>;
+ };
+
+ template <>
+ struct Traits<arrow::UInt16Type> {
+ using TypeProxy = PrimitiveCType<uint16_t>;
+ };
+
+ template <>
+ struct Traits<arrow::UInt32Type> {
+ using TypeProxy = PrimitiveCType<uint32_t>;
+ };
+
+ template <>
+ struct Traits<arrow::UInt64Type> {
+ using TypeProxy = PrimitiveCType<uint64_t>;
+ };
+
+ template <>
+ struct Traits<arrow::StringType> {
+ using TypeProxy = StringType;
+ };
+
+ template <>
+ struct Traits<arrow::TimestampType> {
+ using TypeProxy = TimestampType;
+ };
+}
diff --git a/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
b/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
index 253632d221..c10ce07280 100644
--- a/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
+++ b/matlab/tools/cmake/BuildMatlabArrowInterface.cmake
@@ -44,7 +44,6 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_INCLUDE_DIR
"${CMAKE_SOURCE_DIR}/src/c
set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/array.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/boolean_array.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/string_array.cc"
-
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/array/proxy/timestamp_array.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/bit/pack.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/bit/unpack.cc"