forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-37175: [MATLAB] Support creating
arrow.tabular.RecordBatch
…
… instances from a list of `arrow.array.Array` values (apache#37176) ### Rationale for this change Right now, the only way to construct an `arrow.tabular.RecordBatch` is from a MATLAB `table`: ```matlab >> t = table([1; 2; 3], ["A"; "B"; "C"], VariableNames=["Numbers", "Letters"]); t = 3×2 table Numbers Letters _______ _______ 1 "A" 2 "B" 3 "C" >> rb = arrow.recordbatch(t) rb = Numbers: [ 1, 2, 3 ] Letters: [ "A", "B", "C" ] ``` The interface should also support creating `arrow.tabular.RecordBatch` instances from lists of `arrow.array.Array` values. ### What changes are included in this PR? Added a new static method to `arrow.tabular.RecordBatch` called `fromArrays`. This method accepts a comma-separated list of `arrow.array.Array` values which it uses to construct an `arrow.tabular.RecordBatch`. It also accepts an optional name-value pair called `ColumnNames`, which can be used to specify the column names in the record batch. If this name-value pair is not supplied, the column names default to `"Column1"`, `"Column2"`, etc. **Example Usage:** ```matlab >> a1 = arrow.array([1, 2, 3]); >> a2 = arrow.array(["A", "B", "C"]); >> rb1 = arrow.tabular.RecordBatch.fromArrays(a1, a2) rb1 = Column1: [ 1, 2, 3 ] Column2: [ "A", "B", "C" ] >> rb2 = arrow.tabular.RecordBatch.fromArrays(a1, a2, ColumnNames=["Numbers", "Letters"]) rb2 = Numbers: [ 1, 2, 3 ] Letters: [ "A", "B", "C" ] ``` ### Are these changes tested? Yes. 1. Added new test class `arrow/test/tabular/tValidateArrayLengths.m` 2. Added new test class `arrow/test/tabular/tValidateColumnNames.m` 3. Added new test cases to `arrow/test/tabular/tRecordBatch.m` ### Are there any user-facing changes? Yes, users can now create `arrow.tabular.RecordBatch` instances using the static method `arrow.tabular.RecordBatch.fromArrays`. * Closes: apache#37175 Authored-by: Sarah Gilmore <sgilmore@mathworks.com> Signed-off-by: Kevin Gurney <kgurney@mathworks.com>
- Loading branch information
1 parent
2ffcfbe
commit 333f100
Showing
6 changed files
with
322 additions
and
21 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
matlab/src/matlab/+arrow/+tabular/+internal/validateArrayLengths.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
%VALIDATEARRAYLENGTHS Validates all arrays in the cell array arrowArrays | ||
%have the same length. | ||
|
||
% 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. | ||
|
||
function validateArrayLengths(arrowArrays) | ||
|
||
numArrays = numel(arrowArrays); | ||
|
||
if numArrays == 0 | ||
return; | ||
end | ||
|
||
expectedLength = arrowArrays{1}.Length; | ||
|
||
for ii = 2:numel(arrowArrays) | ||
if arrowArrays{ii}.Length ~= expectedLength | ||
errid = "arrow:tabular:UnequalArrayLengths"; | ||
msg = compose("Expected all arrays to have a length of %d," + ... | ||
" but the array at position %d has a length of %d.", ... | ||
expectedLength, ii, arrowArrays{ii}.Length); | ||
error(errid, msg); | ||
end | ||
end | ||
end | ||
|
25 changes: 25 additions & 0 deletions
25
matlab/src/matlab/+arrow/+tabular/+internal/validateColumnNames.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
%VAIDATECOLUMNNAMES Validates columnNames has the expected number of | ||
%elements. | ||
|
||
% 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. | ||
|
||
function validateColumnNames(columnNames, numColumns) | ||
if numel(columnNames) ~= numColumns | ||
errid = "arrow:tabular:WrongNumberColumnNames"; | ||
msg = compose("Expected ColumnNames to have %d values.", numColumns); | ||
error(errid, msg); | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
%TVALIDATEARRAYLENGTHS Unit tests for | ||
%arrow.tabular.internal.validateArrayLengths. | ||
|
||
% 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 tValidateArrayLengths < matlab.unittest.TestCase | ||
|
||
methods(Test) | ||
function ArraysWithEqualLength(testCase) | ||
% Verify validateArrayLengths() does not error if all the | ||
% arrays have the same length. | ||
|
||
import arrow.tabular.internal.validateArrayLengths | ||
|
||
a = arrow.array(["A", "B", "C"]); | ||
b = arrow.array([true, false, true]); | ||
c = arrow.array([1, 2, 3]); | ||
|
||
% cell array with one element | ||
fcn = @() validateArrayLengths({a}); | ||
testCase.verifyWarningFree(fcn); | ||
|
||
% cell array with two elements | ||
fcn = @() validateArrayLengths({a, b}); | ||
testCase.verifyWarningFree(fcn); | ||
|
||
% cell array with three elements | ||
fcn = @() validateArrayLengths({a, b, c}); | ||
testCase.verifyWarningFree(fcn); | ||
end | ||
|
||
function ArraysWithUnequalLengths(testCase) | ||
% Verify validateArrayLengths() throws an error whose | ||
% identifier is "arrow:tabular:UnequalArrayLengths" if | ||
% all the arrays do not have the same length. | ||
|
||
import arrow.tabular.internal.validateArrayLengths | ||
|
||
a = arrow.array(["A", "B", "C"]); | ||
b = arrow.array([true, false, true, true]); | ||
c = arrow.array([1, 2, 3]); | ||
|
||
fcn = @() validateArrayLengths({a, b}); | ||
testCase.verifyError(fcn, "arrow:tabular:UnequalArrayLengths"); | ||
|
||
fcn = @() validateArrayLengths({b, a}); | ||
testCase.verifyError(fcn, "arrow:tabular:UnequalArrayLengths"); | ||
|
||
fcn = @() validateArrayLengths({b, a, c}); | ||
testCase.verifyError(fcn, "arrow:tabular:UnequalArrayLengths"); | ||
|
||
fcn = @() validateArrayLengths({a, c, b}); | ||
testCase.verifyError(fcn, "arrow:tabular:UnequalArrayLengths"); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
%TVALIDATECOLUMNNAMES Unit tests for | ||
% arrow.tabular.internal.validateColumnNames. | ||
|
||
% 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 tValidateColumnNames < matlab.unittest.TestCase | ||
|
||
methods(Test) | ||
function ValidColumnNames(testCase) | ||
% Verify validateColumnNames() does not error if the | ||
% column names array has the expected number of elements. | ||
|
||
import arrow.tabular.internal.validateColumnNames | ||
|
||
columnNames = ["A", "B", "C"]; | ||
fcn = @() validateColumnNames(columnNames, 3); | ||
testCase.verifyWarningFree(fcn); | ||
|
||
columnNames = string.empty(1, 0); | ||
fcn = @() validateColumnNames(columnNames, 0); | ||
testCase.verifyWarningFree(fcn); | ||
end | ||
|
||
function WrongNumberColumnNames(testCase) | ||
% Verify validateColumnNames() errors if the column names | ||
% array provided does not have the correct number of elements. | ||
% The error thrown should have the identifier | ||
% "arrow:tabular:WrongNumberColumnNames"; | ||
|
||
import arrow.tabular.internal.validateColumnNames | ||
|
||
columnNames = ["A", "B", "C"]; | ||
fcn = @() validateColumnNames(columnNames, 2); | ||
testCase.verifyError(fcn, "arrow:tabular:WrongNumberColumnNames"); | ||
|
||
fcn = @() validateColumnNames(columnNames, 4); | ||
testCase.verifyError(fcn, "arrow:tabular:WrongNumberColumnNames"); | ||
|
||
fcn = @() validateColumnNames(columnNames, 0); | ||
testCase.verifyError(fcn, "arrow:tabular:WrongNumberColumnNames"); | ||
end | ||
end | ||
end |