From 49838858eab984fc4a7e8595d7dd1883b00350ea Mon Sep 17 00:00:00 2001 From: sgilmore10 <74676073+sgilmore10@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:14:31 -0500 Subject: [PATCH] GH-38630: [MATLAB] `arrow.array.BooleanArray`'s `toMATLAB` method does not take slice offsets into account (#38636) ### Rationale for this change While working on #38415, I noticed that the `toMATLAB` method of `arrow.array.BooleanArray` does not take the slice offset into account. This will cause the `toMATLAB` method to return the wrong value. ### What changes are included in this PR? 1. Updated `arrow::matlab::bit::unpack` function to accept a `start_offset` as input. 2. Updated clients (`BooleanArray::toMATLAB` and `Array::getValid`) to supply the array `offset` as the `start_offset`. ### Are these changes tested? The existing tests cover these changes. Additionally, the changes for #38415 will include tests that verify the `toMATLAB` method returns the correct MATLAB array when the underlying Arrow array has been sliced. ### Are there any user-facing changes? No. * Closes: #38630 Authored-by: Sarah Gilmore Signed-off-by: Kevin Gurney --- matlab/src/cpp/arrow/matlab/array/proxy/array.cc | 2 +- matlab/src/cpp/arrow/matlab/array/proxy/boolean_array.cc | 2 +- matlab/src/cpp/arrow/matlab/bit/unpack.cc | 3 +-- matlab/src/cpp/arrow/matlab/bit/unpack.h | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc index 4e52c990d3eae..09d9473df4795 100644 --- a/matlab/src/cpp/arrow/matlab/array/proxy/array.cc +++ b/matlab/src/cpp/arrow/matlab/array/proxy/array.cc @@ -100,7 +100,7 @@ namespace arrow::matlab::array::proxy { } auto validity_bitmap = array->null_bitmap(); - auto valid_elements_mda = bit::unpack(validity_bitmap, array_length); + auto valid_elements_mda = bit::unpack(validity_bitmap, array_length, array->offset()); context.outputs[0] = valid_elements_mda; } diff --git a/matlab/src/cpp/arrow/matlab/array/proxy/boolean_array.cc b/matlab/src/cpp/arrow/matlab/array/proxy/boolean_array.cc index 6a6e478274823..da3560ce522f3 100644 --- a/matlab/src/cpp/arrow/matlab/array/proxy/boolean_array.cc +++ b/matlab/src/cpp/arrow/matlab/array/proxy/boolean_array.cc @@ -53,7 +53,7 @@ namespace arrow::matlab::array::proxy { void BooleanArray::toMATLAB(libmexclass::proxy::method::Context& context) { auto array_length = array->length(); auto packed_logical_data_buffer = std::static_pointer_cast(array)->values(); - auto logical_array_mda = bit::unpack(packed_logical_data_buffer, array_length); + auto logical_array_mda = bit::unpack(packed_logical_data_buffer, array_length, array->offset()); context.outputs[0] = logical_array_mda; } } diff --git a/matlab/src/cpp/arrow/matlab/bit/unpack.cc b/matlab/src/cpp/arrow/matlab/bit/unpack.cc index 7135d593cf752..6cc88d48ede43 100644 --- a/matlab/src/cpp/arrow/matlab/bit/unpack.cc +++ b/matlab/src/cpp/arrow/matlab/bit/unpack.cc @@ -20,7 +20,7 @@ #include "arrow/util/bitmap_visit.h" namespace arrow::matlab::bit { - ::matlab::data::TypedArray unpack(const std::shared_ptr& packed_buffer, int64_t length) { + ::matlab::data::TypedArray unpack(const std::shared_ptr& packed_buffer, int64_t length, int64_t start_offset) { const auto packed_buffer_ptr = packed_buffer->data(); ::matlab::data::ArrayFactory factory; @@ -31,7 +31,6 @@ namespace arrow::matlab::bit { auto unpacked_buffer_ptr = unpacked_buffer.get(); auto visitFcn = [&](const bool is_valid) { *unpacked_buffer_ptr++ = is_valid; }; - const int64_t start_offset = 0; arrow::internal::VisitBitsUnrolled(packed_buffer_ptr, start_offset, length, visitFcn); ::matlab::data::TypedArray unpacked_matlab_logical_Array = factory.createArrayFromBuffer({array_length, 1}, std::move(unpacked_buffer)); diff --git a/matlab/src/cpp/arrow/matlab/bit/unpack.h b/matlab/src/cpp/arrow/matlab/bit/unpack.h index b6debb85f837b..6cd633e76fa56 100644 --- a/matlab/src/cpp/arrow/matlab/bit/unpack.h +++ b/matlab/src/cpp/arrow/matlab/bit/unpack.h @@ -22,6 +22,6 @@ #include "MatlabDataArray.hpp" namespace arrow::matlab::bit { - ::matlab::data::TypedArray unpack(const std::shared_ptr& packed_buffer, int64_t length); + ::matlab::data::TypedArray unpack(const std::shared_ptr& packed_buffer, int64_t length, int64_t start_offset); const uint8_t* extract_ptr(const ::matlab::data::TypedArray& unpacked_validity_bitmap); }