kevingurney commented on code in PR #37725:
URL: https://github.com/apache/arrow/pull/37725#discussion_r1327604159
##########
matlab/src/cpp/arrow/matlab/type/proxy/type.cc:
##########
@@ -47,6 +52,36 @@ namespace arrow::matlab::type::proxy {
context.outputs[0] = num_fields_mda;
}
+ void Type::getFieldByIndex(libmexclass::proxy::method::Context& context) {
+ namespace mda = ::matlab::data;
+ mda::ArrayFactory factory;
+
+ mda::StructArray args = context.inputs[0];
+ const mda::TypedArray<int32_t> index_mda = args[0]["Index"];
+ const auto matlab_index = int32_t(index_mda[0]);
+
+ // Validate there is at least 1 field
+ MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(
+ index::validateNonEmptyFields(data_type->num_fields()),
+ context,
+ error::INDEX_EMPTY_CONTAINER);
+
+ // Validate the matlab index provided is within the range [1,
num_fields]
+ MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(
+ index::validateNumericFieldIndexInRange(matlab_index,
data_type->num_fields()),
+ context,
+ error::INDEX_OUT_OF_RANGE);
+
+ // Note: MATLAB uses 1-based indexing, so subtract 1.
+ // arrow::Schema::field does not do any bounds checking.
Review Comment:
arrow::Schema::field -> arrow::DataType::field
##########
matlab/src/cpp/arrow/matlab/index/validate.cc:
##########
@@ -0,0 +1,56 @@
+// 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/index/validate.h"
+
+#include <sstream>
+
+namespace arrow::matlab::index {
+
+ namespace {
+ std::string makeZeroFieldsErrorMessage() {
+ return "Numeric indexing using the field method is not supported
for objects with zero fields.";
+ }
+
+ std::string makeInvalidNumericFieldIndexErrorMessage(const int32_t
matlab_index, const int32_t num_fields) {
+ std::stringstream error_message_stream;
+ error_message_stream << "Invalid field index: ";
+ // matlab uses 1-based indexing
+ error_message_stream << matlab_index;
+ error_message_stream << ". Field index must be between 1 and the
number of fields (";
+ error_message_stream << num_fields;
+ error_message_stream << ").";
+ return error_message_stream.str();
+ }
+ } // anonymous namespace
+
+ arrow::Status validateNonEmptyFields(const int32_t num_fields) {
+ if (num_fields == 0) {
+ const auto msg = makeZeroFieldsErrorMessage();
+ return arrow::Status::Invalid(std::move(msg));
+ }
+ return arrow::Status::OK();
+ }
+
+ arrow::Status validateNumericFieldIndexInRange(const int32_t matlab_index,
const int32_t num_fields) {
+ if (matlab_index < 1 || matlab_index > num_fields) {
+ const auto msg =
makeInvalidNumericFieldIndexErrorMessage(matlab_index, num_fields);
Review Comment:
Now that you've updated the error message IDs, it may make sense to update
the function names too:
`makeInvalidNumericFieldIndexErrorMessage` ->
`makeIndexOutOfRangeErrorMessage`
--
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]