From 2fc10901b8c989462f67a017049f92b5227244f7 Mon Sep 17 00:00:00 2001 From: sgilmore10 <74676073+sgilmore10@users.noreply.github.com> Date: Thu, 7 Sep 2023 17:06:55 -0400 Subject: [PATCH] GH-37568: [MATLAB] Implement `isequal` for the `arrow.tabular.Schema` MATLAB class (#37619) ### Rationale for this change Following on to https://github.com/apache/arrow/pull/37474, https://github.com/apache/arrow/pull/37446, and https://github.com/apache/arrow/pull/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 Signed-off-by: Kevin Gurney --- matlab/src/matlab/+arrow/+tabular/Schema.m | 31 +++++++++-- matlab/test/arrow/tabular/tSchema.m | 61 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/matlab/src/matlab/+arrow/+tabular/Schema.m b/matlab/src/matlab/+arrow/+tabular/Schema.m index 4b4a3d57821b1..f679b1e0bc22c 100644 --- a/matlab/src/matlab/+arrow/+tabular/Schema.m +++ b/matlab/src/matlab/+arrow/+tabular/Schema.m @@ -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. @@ -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 @@ -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) diff --git a/matlab/test/arrow/tabular/tSchema.m b/matlab/test/arrow/tabular/tSchema.m index b57ebffbc577e..3220236d4aabe 100644 --- a/matlab/test/arrow/tabular/tSchema.m +++ b/matlab/test/arrow/tabular/tSchema.m @@ -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