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 4dd742c83d GH-37157: [MATLAB] Use 
`arrow.internal.validate.index.numericOrString()` in the `field()` method of 
`arrow.tabular.Schema` (#37162)
4dd742c83d is described below

commit 4dd742c83d76e696f0ae8c2dd2f667d9ab2bf555
Author: sgilmore10 <[email protected]>
AuthorDate: Mon Aug 14 16:52:17 2023 -0400

    GH-37157: [MATLAB] Use `arrow.internal.validate.index.numericOrString()` in 
the `field()` method of `arrow.tabular.Schema` (#37162)
    
    
    
    ### Rationale for this change
    
    Now that the PR introducing the 
`arrow.internal.validate.inex.numericOrString()` function has been merged 
(#37150), we should use this utility function within the `field()` method of 
`arrow.tabular.Schema`. Doing so will remove duplicate code from the code base.
    
    ### What changes are included in this PR?
    
    1. Updated the `field()` method of `arrow.tabular.Schema` to use 
`arrow.internal.validate.index.numericOrString()`.
    
    ### Are these changes tested?
    
    Yes. Updated the test points in `test/arrow/tabular/tSchema.m` to account 
for the new error message IDs.
    
    ### Are there any user-facing changes?
    
    No.
    
    * Closes: #37157
    
    Authored-by: Sarah Gilmore <[email protected]>
    Signed-off-by: Kevin Gurney <[email protected]>
---
 matlab/src/matlab/+arrow/+tabular/Schema.m | 23 ++++++++++++++---------
 matlab/test/arrow/tabular/tSchema.m        | 30 +++++++++++++++---------------
 2 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/matlab/src/matlab/+arrow/+tabular/Schema.m 
b/matlab/src/matlab/+arrow/+tabular/Schema.m
index 3d75e6c44a..b613bd650d 100644
--- a/matlab/src/matlab/+arrow/+tabular/Schema.m
+++ b/matlab/src/matlab/+arrow/+tabular/Schema.m
@@ -43,17 +43,22 @@ classdef Schema < matlab.mixin.CustomDisplay
         end
         
         function F = field(obj, idx)
-            idx = convertCharsToStrings(idx);
-            if ~isempty(idx) && isscalar(idx) && isnumeric(idx) && idx >= 1
-                args = struct(Index=int32(idx));
+            import arrow.internal.validate.*
+            
+            idx = index.numericOrString(idx, "int32");
+
+            if isnumeric(idx)
+                % TODO: Consider vectorizing field() to support extracting
+                % multiple fields at once.
+                validateattributes(idx, "int32", "scalar");
+                args = struct(Index=idx);
                 proxyID = obj.Proxy.getFieldByIndex(args);
-            elseif isscalar(idx) && isstring(idx)
-                name = idx;
-                args = struct(Name=name);
-                proxyID = obj.Proxy.getFieldByName(args);
             else
-                error("arrow:tabular:schema:UnsupportedFieldIndexType", ...
-                      "Index must be a positive scalar integer or a valid 
field name.");
+                % TODO: Consider vectorizing field() to support extracting
+                % multiple fields at once.
+                validateattributes(idx, "string", "scalar");
+                args = struct(Name=idx);
+                proxyID = obj.Proxy.getFieldByName(args);
             end
 
             proxy = libmexclass.proxy.Proxy(Name="arrow.type.proxy.Field", 
ID=proxyID);
diff --git a/matlab/test/arrow/tabular/tSchema.m 
b/matlab/test/arrow/tabular/tSchema.m
index d550c488fd..dbeb2db1fd 100644
--- a/matlab/test/arrow/tabular/tSchema.m
+++ b/matlab/test/arrow/tabular/tSchema.m
@@ -139,25 +139,25 @@ classdef tSchema < matlab.unittest.TestCase
             ]);
 
             index = [];
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"MATLAB:expectedScalar");
 
             index = 0;
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"arrow:badsubscript:NonPositive");
 
             index = -1;
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"arrow:badsubscript:NonPositive");
 
-            index = -1.23;
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            index = 1.23;
+            testCase.verifyError(@() schema.field(index), 
"arrow:badsubscript:NonInteger");
 
             index = NaN;
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"arrow:badsubscript:NonInteger");
 
             index = {1};
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"arrow:badsubscript:UnsupportedIndexType");
 
             index = [1; 1];
-            testCase.verifyError(@() schema.field(index), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(index), 
"MATLAB:expectedScalar");
         end
 
         function GetFieldByIndex(testCase)
@@ -375,7 +375,7 @@ classdef tSchema < matlab.unittest.TestCase
             testCase.verifyEqual(schema.NumFields, int32(0));
             testCase.verifyEqual(schema.FieldNames, string.empty(1, 0));
             testCase.verifyEqual(schema.Fields, arrow.type.Field.empty(0, 0));
-            testCase.verifyError(@() schema.field(0), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(0), 
"arrow:badsubscript:NonPositive");
             testCase.verifyError(@() schema.field(1), 
"arrow:tabular:schema:NumericFieldIndexWithEmptySchema");
 
             % 0x1 empty Field array.
@@ -384,7 +384,7 @@ classdef tSchema < matlab.unittest.TestCase
             testCase.verifyEqual(schema.NumFields, int32(0));
             testCase.verifyEqual(schema.FieldNames, string.empty(1, 0));
             testCase.verifyEqual(schema.Fields, arrow.type.Field.empty(0, 0));
-            testCase.verifyError(@() schema.field(0), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(0), 
"arrow:badsubscript:NonPositive");
             testCase.verifyError(@() schema.field(1), 
"arrow:tabular:schema:NumericFieldIndexWithEmptySchema");
 
             % 1x0 empty Field array.
@@ -393,7 +393,7 @@ classdef tSchema < matlab.unittest.TestCase
             testCase.verifyEqual(schema.NumFields, int32(0));
             testCase.verifyEqual(schema.FieldNames, string.empty(1, 0));
             testCase.verifyEqual(schema.Fields, arrow.type.Field.empty(0, 0));
-            testCase.verifyError(@() schema.field(0), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(0), 
"arrow:badsubscript:NonPositive");
             testCase.verifyError(@() schema.field(1), 
"arrow:tabular:schema:NumericFieldIndexWithEmptySchema");
         end
 
@@ -446,10 +446,10 @@ classdef tSchema < matlab.unittest.TestCase
             ]);
 
             fieldName = [1, 2, 3];
-            testCase.verifyError(@() schema.field(fieldName), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(fieldName), 
"MATLAB:expectedScalar");
 
             fieldName = [1; 2; 3];
-            testCase.verifyError(@() schema.field(fieldName), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(fieldName), 
"MATLAB:expectedScalar");
         end
 
         function ErrorIfFieldNameIsNonScalar(testCase)
@@ -462,10 +462,10 @@ classdef tSchema < matlab.unittest.TestCase
             ]);
 
             fieldName = ["A", "B", "C"];
-            testCase.verifyError(@() schema.field(fieldName), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(fieldName), 
"MATLAB:expectedScalar");
 
             fieldName = ["A";  "B"; "C"];
-            testCase.verifyError(@() schema.field(fieldName), 
"arrow:tabular:schema:UnsupportedFieldIndexType");
+            testCase.verifyError(@() schema.field(fieldName), 
"MATLAB:expectedScalar");
         end
 
     end

Reply via email to