Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-37568: [MATLAB] Implement isequal for the arrow.tabular.Schema MATLAB class #37619

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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