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 750670e8c7 GH-37155: [MATLAB] Use
`arrow.internal.validate.index.numeric()` in the `column()` method of
`arrow.tabular.RecordBatch` (#37156)
750670e8c7 is described below
commit 750670e8c7c8b7985323f7866e4aa09be4232169
Author: sgilmore10 <[email protected]>
AuthorDate: Mon Aug 14 15:07:06 2023 -0400
GH-37155: [MATLAB] Use `arrow.internal.validate.index.numeric()` in the
`column()` method of `arrow.tabular.RecordBatch` (#37156)
### Rationale for this change
Now that the PR introducing the `arrow.internal.validate.index.numeric()`
function has been merged (#37150), we should use this utility function within
the `column()` method of `arrow.tabular.RecordBatch`. Doing so will remove
duplicate code from the code base.
### What changes are included in this PR?
1. Updated the `column()` method of `arrow.tabular.RecordBatch` to use
`arrow.internal.validate.index.numeric()`.
### Are these changes tested?
Yes, these changes are covered by these two existing test classes:
1. `test/arrow/tabular/tRecordBatch.m`
2. `test/arrow/internal/validate/index/tNumeric.m`
In addition, I added a new test point to `tRecordBatch.m` called
`ErrorIfNonPositiveIndex` to verify `arrow.internal.validate.index.numeric` is
integrated correctly.
### Are there any user-facing changes?
No.
### Future Directions
1. In a future pull request, we will add support for indexing columns by
name. Currently, the `column()` method only supports numeric indexing.
* Closes: #37155
Authored-by: Sarah Gilmore <[email protected]>
Signed-off-by: Kevin Gurney <[email protected]>
---
matlab/src/matlab/+arrow/+tabular/RecordBatch.m | 24 +++++++++++++-----------
matlab/test/arrow/tabular/tRecordBatch.m | 16 +++++++++++++++-
2 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
b/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
index 0fd9fb81d4..fb978a09ec 100644
--- a/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
+++ b/matlab/src/matlab/+arrow/+tabular/RecordBatch.m
@@ -45,17 +45,19 @@ classdef RecordBatch < matlab.mixin.CustomDisplay & ...
end
function arrowArray = column(obj, idx)
- if ~isempty(idx) && isscalar(idx) && isnumeric(idx) && idx >= 1
- args = struct(Index=int32(idx));
- [proxyID, typeID] = obj.Proxy.getColumnByIndex(args);
- traits = arrow.type.traits.traits(arrow.type.ID(typeID));
- proxy =
libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName, ID=proxyID);
- arrowArray = traits.ArrayConstructor(proxy);
- else
- errid = "arrow:tabular:recordbatch:UnsupportedColumnIndexType";
- msg = "Index must be a positive scalar integer.";
- error(errid, msg);
- end
+ import arrow.internal.validate.*
+
+ idx = index.numeric(idx, "int32");
+ % TODO: Consider vectorizing column() in the future to support
+ % extracting multiple columns at once.
+ validateattributes(idx, "int32", "scalar");
+
+ args = struct(Index=idx);
+ [proxyID, typeID] = obj.Proxy.getColumnByIndex(args);
+
+ traits = arrow.type.traits.traits(arrow.type.ID(typeID));
+ proxy = libmexclass.proxy.Proxy(Name=traits.ArrayProxyClassName,
ID=proxyID);
+ arrowArray = traits.ArrayConstructor(proxy);
end
function T = table(obj)
diff --git a/matlab/test/arrow/tabular/tRecordBatch.m
b/matlab/test/arrow/tabular/tRecordBatch.m
index 025f15320a..e4ef649bd0 100644
--- a/matlab/test/arrow/tabular/tRecordBatch.m
+++ b/matlab/test/arrow/tabular/tRecordBatch.m
@@ -119,7 +119,21 @@ classdef tRecordBatch < matlab.unittest.TestCase
TOriginal = table(1, 2, 3);
arrowRecordBatch = arrow.recordbatch(TOriginal);
fcn = @() arrowRecordBatch.column(datetime(2022, 1, 3));
- tc.verifyError(fcn,
"arrow:tabular:recordbatch:UnsupportedColumnIndexType");
+ tc.verifyError(fcn, "arrow:badsubscript:NonNumeric");
+ end
+
+ function ErrorIfIndexIsNonScalar(tc)
+ TOriginal = table(1, 2, 3);
+ arrowRecordBatch = arrow.recordbatch(TOriginal);
+ fcn = @() arrowRecordBatch.column([1 2]);
+ tc.verifyError(fcn, "MATLAB:expectedScalar");
+ end
+
+ function ErrorIfIndexIsNonPositive(tc)
+ TOriginal = table(1, 2, 3);
+ arrowRecordBatch = arrow.recordbatch(TOriginal);
+ fcn = @() arrowRecordBatch.column(-1);
+ tc.verifyError(fcn, "arrow:badsubscript:NonPositive");
end
end
end