sgilmore10 commented on code in PR #38357: URL: https://github.com/apache/arrow/pull/38357#discussion_r1365908049
########## matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc: ########## @@ -0,0 +1,103 @@ +// 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/list_array.h" +#include "arrow/matlab/array/proxy/numeric_array.h" +#include "arrow/matlab/array/proxy/wrap.h" +#include "arrow/matlab/error/error.h" +#include "libmexclass/proxy/ProxyManager.h" + +namespace arrow::matlab::array::proxy { + + ListArray::ListArray(std::shared_ptr<arrow::ListArray> list_array) : proxy::Array{std::move(list_array)} { + REGISTER_METHOD(ListArray, getValues); + REGISTER_METHOD(ListArray, getOffsets); + } + + libmexclass::proxy::MakeResult ListArray::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) { + namespace mda = ::matlab::data; + using libmexclass::proxy::ProxyManager; + using Int32ArrayProxy = ::arrow::matlab::array::proxy::NumericArray<arrow::Int32Type>; + using ListArrayProxy = ::arrow::matlab::array::proxy::ListArray; + using ArrayProxy = ::arrow::matlab::array::proxy::Array; + + mda::StructArray opts = constructor_arguments[0]; + const mda::TypedArray<uint64_t> offsets_proxy_id_mda = opts[0]["OffsetsProxyID"]; + const mda::TypedArray<uint64_t> values_proxy_id_mda = opts[0]["ValuesProxyID"]; + const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"]; + + const auto offsets_proxy_id = offsets_proxy_id_mda[0]; + const auto values_proxy_id = values_proxy_id_mda[0]; + + const auto offsets_proxy = std::static_pointer_cast<Int32ArrayProxy>(ProxyManager::getProxy(offsets_proxy_id)); + const auto values_proxy = std::static_pointer_cast<ArrayProxy>(ProxyManager::getProxy(values_proxy_id)); + + const auto offsets = offsets_proxy->unwrap(); + const auto values = values_proxy->unwrap(); + + // Pack the validity bitmap values. + MATLAB_ASSIGN_OR_ERROR(auto validity_bitmap_buffer, + bit::packValid(validity_bitmap_mda), + error::BITPACK_VALIDITY_BITMAP_ERROR_ID); + + // Create a ListArray from values and offsets. + MATLAB_ASSIGN_OR_ERROR(auto array, + arrow::ListArray::FromArrays(*offsets, *values, arrow::default_memory_pool(), validity_bitmap_buffer), + error::LIST_ARRAY_FROM_ARRAYS_FAILED); + + // Return a ListArray Proxy. + auto list_array = std::static_pointer_cast<arrow::ListArray>(array); + return std::make_shared<ListArrayProxy>(std::move(list_array)); + } + + void ListArray::getValues(libmexclass::proxy::method::Context& context) { + namespace mda = ::matlab::data; + using libmexclass::proxy::ProxyManager; + + auto list_array = std::static_pointer_cast<arrow::ListArray>(array); + auto value_array = list_array->values(); + + // Wrap the array within a proxy object if possible. + MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto value_array_proxy, Review Comment: Just a general observation. We should add a utility function that manages the proxy and creates the `struct` returned. Would reduce code duplication since we do this in many places. ########## matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc: ########## @@ -0,0 +1,103 @@ +// 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/list_array.h" +#include "arrow/matlab/array/proxy/numeric_array.h" +#include "arrow/matlab/array/proxy/wrap.h" +#include "arrow/matlab/error/error.h" +#include "libmexclass/proxy/ProxyManager.h" + +namespace arrow::matlab::array::proxy { + + ListArray::ListArray(std::shared_ptr<arrow::ListArray> list_array) : proxy::Array{std::move(list_array)} { + REGISTER_METHOD(ListArray, getValues); + REGISTER_METHOD(ListArray, getOffsets); + } + + libmexclass::proxy::MakeResult ListArray::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) { + namespace mda = ::matlab::data; + using libmexclass::proxy::ProxyManager; + using Int32ArrayProxy = ::arrow::matlab::array::proxy::NumericArray<arrow::Int32Type>; Review Comment: I don't think you need to specify `::` (the global namespace) before `arrow` here. We need it above because `matlab` without the `::` prefix resolves to `arrow::matlab`. ########## matlab/test/arrow/array/tListArray.m: ########## @@ -0,0 +1,165 @@ +%TLISTARRAY Tests for arrow.array.ListArray + +% 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 tListArray < matlab.unittest.TestCase + + properties (Constant) + Traits = arrow.type.traits.traits(arrow.type.ID.List) + end + + properties (TestParameter) + TestArrowArray + end + + methods (TestParameterDefinition, Static) + + function TestArrowArray = initializeTestArrowArray() + %% Empty (zero-element) list Review Comment: Could you include the list type, i.e. List<Float64> ########## matlab/test/arrow/array/tListArray.m: ########## @@ -0,0 +1,165 @@ +%TLISTARRAY Tests for arrow.array.ListArray + +% 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 tListArray < matlab.unittest.TestCase + + properties (Constant) + Traits = arrow.type.traits.traits(arrow.type.ID.List) + end + + properties (TestParameter) + TestArrowArray + end + + methods (TestParameterDefinition, Static) + + function TestArrowArray = initializeTestArrowArray() + %% Empty (zero-element) list + Type = arrow.list(arrow.float64()); + NumElements = int64(0); + Valid = logical.empty(0, 1); + Offsets = arrow.array(int32(0)); + Values = arrow.array([]); + ArrowArray = arrow.array.ListArray.fromArrays(Offsets, Values, Valid=Valid); + MatlabArray = {cell.empty(0, 1)}; + + TestArrowArray.EmptyList = struct( ... + ArrowArray=ArrowArray, ... + MatlabArray=MatlabArray, ... + Properties=struct(... + Type=Type, ... + NumElements=NumElements, ... + Valid=Valid, ... + Offsets=Offsets, ... + Values=Values ... + ) ... + ); + + %% List with NULLs Review Comment: Could you mention the list type here too? ########## matlab/src/cpp/arrow/matlab/array/proxy/list_array.cc: ########## @@ -0,0 +1,103 @@ +// 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/list_array.h" +#include "arrow/matlab/array/proxy/numeric_array.h" +#include "arrow/matlab/array/proxy/wrap.h" +#include "arrow/matlab/error/error.h" +#include "libmexclass/proxy/ProxyManager.h" + +namespace arrow::matlab::array::proxy { + + ListArray::ListArray(std::shared_ptr<arrow::ListArray> list_array) : proxy::Array{std::move(list_array)} { + REGISTER_METHOD(ListArray, getValues); + REGISTER_METHOD(ListArray, getOffsets); + } + + libmexclass::proxy::MakeResult ListArray::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) { + namespace mda = ::matlab::data; + using libmexclass::proxy::ProxyManager; + using Int32ArrayProxy = ::arrow::matlab::array::proxy::NumericArray<arrow::Int32Type>; + using ListArrayProxy = ::arrow::matlab::array::proxy::ListArray; + using ArrayProxy = ::arrow::matlab::array::proxy::Array; + + mda::StructArray opts = constructor_arguments[0]; + const mda::TypedArray<uint64_t> offsets_proxy_id_mda = opts[0]["OffsetsProxyID"]; + const mda::TypedArray<uint64_t> values_proxy_id_mda = opts[0]["ValuesProxyID"]; + const mda::TypedArray<bool> validity_bitmap_mda = opts[0]["Valid"]; + + const auto offsets_proxy_id = offsets_proxy_id_mda[0]; + const auto values_proxy_id = values_proxy_id_mda[0]; + + const auto offsets_proxy = std::static_pointer_cast<Int32ArrayProxy>(ProxyManager::getProxy(offsets_proxy_id)); + const auto values_proxy = std::static_pointer_cast<ArrayProxy>(ProxyManager::getProxy(values_proxy_id)); + + const auto offsets = offsets_proxy->unwrap(); + const auto values = values_proxy->unwrap(); + + // Pack the validity bitmap values. + MATLAB_ASSIGN_OR_ERROR(auto validity_bitmap_buffer, + bit::packValid(validity_bitmap_mda), + error::BITPACK_VALIDITY_BITMAP_ERROR_ID); + + // Create a ListArray from values and offsets. + MATLAB_ASSIGN_OR_ERROR(auto array, Review Comment: Maybe not in this PR, but should we add an option to validate the `Offsets` and `Values` array are correct? -- 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]
