Skip to content

Commit

Permalink
GH-37568: [MATLAB] Implement isequal for the arrow.tabular.Schema
Browse files Browse the repository at this point in the history
… MATLAB class (#37619)

### Rationale for this change

Following on to #37474, #37446, and #37525, we should implement `isequal` for the `arrow.tabular.Schema` MATLAB class.

### What changes are included in this PR?

1. Updated `arrow.tabular.Schema` class to inherit from `matlab.mixin.Scalar`.
2. Added `isequal` method to `arrow.tabular.Schema`.

### Are these changes tested?

Yes. Added `isequal` unit tests to `tSchema.m`

### Are there any user-facing changes?

Yes. Users can now compare two `arrow.tabular.Schema` objects via `isequal`.

**Example**
```matlab
>> schema1 = arrow.schema([arrow.field("A", arrow.uint8), arrow.field("B", arrow.uint16)]);
>> schema2 = arrow.schema([arrow.field("A", arrow.uint8), arrow.field("B", arrow.uint16)]);
>> schema3 = arrow.schema([arrow.field("A", arrow.uint8)]);

>> isequal(schema1, schema2)

ans =

  logical

   1

>> isequal(schema1, schema3)

ans =

  logical

   0
```

### Future Directions
1. #37570 

* Closes: #37568

Authored-by: Sarah Gilmore <sgilmore@mathworks.com>
Signed-off-by: Kevin Gurney <kgurney@mathworks.com>
  • Loading branch information
sgilmore10 committed Sep 7, 2023
1 parent 3aa7e24 commit cdc95c2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
31 changes: 26 additions & 5 deletions matlab/src/matlab/+arrow/+tabular/Schema.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
%SCHEMA A tabular schema which semantically describes
% the names and types of the columns of an associated tabular
% Arrow data type.

% 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.
Expand All @@ -13,10 +17,8 @@
% implied. See the License for the specific language governing
% permissions and limitations under the License.

classdef Schema < matlab.mixin.CustomDisplay
%SCHEMA A tabular schema which semantically describes
% the names and types of the columns of an associated tabular
% Arrow data type.
classdef Schema < matlab.mixin.CustomDisplay & ...
matlab.mixin.Scalar

properties (GetAccess=public, SetAccess=private, Hidden)
Proxy
Expand Down Expand Up @@ -73,7 +75,26 @@
function numFields = get.NumFields(obj)
numFields = obj.Proxy.getNumFields();
end


function tf = isequal(obj, varargin)
narginchk(2, inf);
tf = false;

fieldsToCompare = cell([1 numel(varargin)]);
for ii = 1:numel(varargin)
schema = varargin{ii};
if ~isa(schema, "arrow.tabular.Schema")
% Return false early if schema is not actually an
% arrow.tabular.Schema instance.
return;
end

fieldsToCompare{ii} = schema.Fields;
end

% Return if the Schema Fields properties are equal
tf = isequal(obj.Fields, fieldsToCompare{:});
end
end

methods (Access = private)
Expand Down
61 changes: 61 additions & 0 deletions matlab/test/arrow/tabular/tSchema.m
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,67 @@ function ErrorIfFieldNameIsNonScalar(testCase)
testCase.verifyError(@() schema.field(fieldName), "arrow:badsubscript:NonScalar");
end

function TestIsEqualTrue(testCase)
% Schema objects are considered equal if:
% 1. They have the same number of fields
% 2. Their corresponding Fields properties are equal

schema1 = arrow.schema([...
arrow.field("A", arrow.uint8), ...
arrow.field("B", arrow.uint16), ...
arrow.field("123", arrow.uint32)
]);
schema2 = arrow.schema([...
arrow.field("A", arrow.uint8), ...
arrow.field("B", arrow.uint16), ...
arrow.field("123", arrow.uint32)
]);

% Create a Schema with zero fields
schema3 = arrow.recordBatch(table).Schema;
schema4 = arrow.recordBatch(table).Schema;

testCase.verifyTrue(isequal(schema1, schema2));
testCase.verifyTrue(isequal(schema3, schema4));
end

function TestIsEqualFalse(testCase)
% Verify isequal returns false when expected.

schema1 = arrow.schema([...
arrow.field("A", arrow.uint8), ...
arrow.field("B", arrow.uint16), ...
arrow.field("123", arrow.uint32)
]);
schema2 = arrow.schema([...
arrow.field("A", arrow.uint8), ...
arrow.field("B", arrow.uint16), ...
]);
schema3 = arrow.schema([...
arrow.field("A", arrow.float32), ...
arrow.field("B", arrow.uint16), ...
]);
schema4 = arrow.schema([...
arrow.field("C", arrow.uint8), ...
arrow.field("B", arrow.uint16), ...
]);

% Create a Schema with zero fields
schema5 = arrow.recordBatch(table).Schema;

% Have different number of fields
testCase.verifyFalse(isequal(schema1, schema2));

% Fields properties are not equal
testCase.verifyFalse(isequal(schema2, schema3));
testCase.verifyFalse(isequal(schema2, schema4));
testCase.verifyFalse(isequal(schema4, schema5));

% Compare schema to double
testCase.verifyFalse(isequal(schema4, 5));

end

end

end

0 comments on commit cdc95c2

Please sign in to comment.