Skip to content

Commit

Permalink
GH-35480: [MATLAB] Add abstract MATLAB base class called `arrow.array…
Browse files Browse the repository at this point in the history
….Array` (#35491)

### Rationale for this change

It will be helpful for sharing the implementation of common functionality if there is an abstract base class from which the MATLAB `arrow.array.[Type]Array` classes can inherit. 

### What changes are included in this PR?

1. Added abstract base class `arrow.array.Array`
2. Changed `arrow.array.Float64Array` to inherit from `arrow.array.Array`
3. Added `Length` property on `arrow.array.Array`

### Are these changes tested?

1. Added a test point for the `Length` property in `tFloat64Array.m`.
2. Qualified locally on macOS and linux.

### Are there any user-facing changes?

Yes, there is now a `Length` property on `arrow.array.Float64Array`

### Future Directions

We will continue to build out the concrete Array subclasses. 
* Closes: #35480

Lead-authored-by: Sarah Gilmore <sgilmore@mathworks.com>
Co-authored-by: Kevin Gurney <kgurney@mathworks.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
sgilmore10 and kevingurney committed May 11, 2023
1 parent 14f9bf9 commit cdefbb8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
8 changes: 7 additions & 1 deletion matlab/src/cpp/arrow/matlab/array/proxy/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace arrow::matlab::array::proxy {
// Register Proxy methods.
REGISTER_METHOD(Array, ToString);
REGISTER_METHOD(Array, ToMatlab);

REGISTER_METHOD(Array, Length);
}

void Array::ToString(libmexclass::proxy::method::Context& context) {
Expand All @@ -34,4 +34,10 @@ namespace arrow::matlab::array::proxy {
auto str_mda = factory.createScalar(array->ToString());
context.outputs[0] = str_mda;
}

void Array::Length(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;
auto length_mda = factory.createScalar(array->length());
context.outputs[0] = length_mda;
}
}
2 changes: 2 additions & 0 deletions matlab/src/cpp/arrow/matlab/array/proxy/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Array : public libmexclass::proxy::Proxy {

void ToString(libmexclass::proxy::method::Context& context);

void Length(libmexclass::proxy::method::Context& context);

virtual void ToMatlab(libmexclass::proxy::method::Context& context) = 0;

std::shared_ptr<arrow::Array> array;
Expand Down
51 changes: 51 additions & 0 deletions matlab/src/matlab/+arrow/+array/Array.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
classdef (Abstract) Array < matlab.mixin.CustomDisplay & ...
matlab.mixin.Scalar
% arrow.array.Array

% 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.


properties (Access=protected)
Proxy
end

properties (Dependent)
Length
end

methods
function obj = Array(varargin)
obj.Proxy = libmexclass.proxy.Proxy(varargin{:});
end

function numElements = get.Length(obj)
numElements = obj.Proxy.Length();
end
end

methods (Access = private)
function str = ToString(obj)
str = obj.Proxy.ToString();
end
end

methods (Access=protected)
function displayScalarObject(obj)
disp(obj.ToString());
end
end
end

20 changes: 2 additions & 18 deletions matlab/src/matlab/+arrow/+array/Float64Array.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
classdef Float64Array < matlab.mixin.CustomDisplay
classdef Float64Array < arrow.array.Array
% arrow.array.Float64Array

% Licensed to the Apache Software Foundation (ASF) under one or more
Expand All @@ -16,10 +16,6 @@
% implied. See the License for the specific language governing
% permissions and limitations under the License.

properties (Access=private)
Proxy
end

properties (Hidden, SetAccess=private)
MatlabArray
end
Expand All @@ -33,25 +29,13 @@

validateattributes(data, "double", ["2d", "nonsparse", "real"]);
if ~isempty(data), validateattributes(data, "double", "vector"); end
obj@arrow.array.Array("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {data, opts.DeepCopy});
% Store a reference to the array if not doing a deep copy
if (~opts.DeepCopy), obj.MatlabArray = data; end
obj.Proxy = libmexclass.proxy.Proxy("Name", "arrow.array.proxy.Float64Array", "ConstructorArguments", {data, opts.DeepCopy});
end

function data = double(obj)
data = obj.Proxy.ToMatlab();
end
end

methods (Access=protected)
function displayScalarObject(obj)
disp(obj.ToString());
end
end

methods (Access=private)
function str = ToString(obj)
str = obj.Proxy.ToString();
end
end
end
17 changes: 17 additions & 0 deletions matlab/test/arrow/array/tFloat64Array.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,22 @@ function ErrorIfSparse(testCase, MakeDeepCopy)
fcn = @() arrow.array.Float64Array(sparse(ones([10 1])), DeepCopy=MakeDeepCopy);
testCase.verifyError(fcn, "MATLAB:expectedNonsparse");
end

function Length(testCase, MakeDeepCopy)
% Zero length array
A = arrow.array.Float64Array([], DeepCopy=MakeDeepCopy);
expectedLength = int64(0);
testCase.verifyEqual(A.Length, expectedLength);

% Scalar
A = arrow.array.Float64Array(1, DeepCopy=MakeDeepCopy);
expectedLength = int64(1);
testCase.verifyEqual(A.Length, expectedLength);

% Vector
A = arrow.array.Float64Array(1:100, DeepCopy=MakeDeepCopy);
expectedLength = int64(100);
testCase.verifyEqual(A.Length, expectedLength);
end
end
end

0 comments on commit cdefbb8

Please sign in to comment.