Skip to content

Commit

Permalink
apacheGH-37262: [MATLAB] Add an abstract class called `arrow.type.Tim…
Browse files Browse the repository at this point in the history
…eType` (apache#37279)

### Rationale for this change

To reduce code duplication within `Time32Type` and `Time64Type`, we should add an abstract class called `arrow.type.TimeType` that implements  the getter method for the `TimeUnit` property. This class hierarchy will mirror the class hierarchy in the C++ arrow implementation.

### What changes are included in this PR?

1. Defined a new C++ proxy class for called `arrow::matlab::type::proxy::TimeType`. This class defines a `getTimeUnit` method.
2. Modified the C++ proxy class `Time32Type` to inherit from `arrow::matlab::type::proxy::TimeType`. Removed the `getTimeUnit` method from this class because it's now defined on `TimeType`.
3. Added a new MATLAB class called `arrow.type.TimeType`. It has one method: `get.TimeUnit`.
4. Modified `arrow.type.Time32Type` to inherit from `arrow.type.TimeType` and removed its `get.TimeUnit` method.

### Are these changes tested?

Yes, the existing tests cover these changes.

### Are there any user-facing changes?

No.

### Future Directions

1. apache#37232
2. apache#37229
3. apache#37230   
* Closes: apache#37262

Authored-by: Sarah Gilmore <sgilmore@mathworks.com>
Signed-off-by: Kevin Gurney <kgurney@mathworks.com>
  • Loading branch information
sgilmore10 authored and loicalleyne committed Nov 13, 2023
1 parent bb9e097 commit 45e8448
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 38 deletions.
16 changes: 1 addition & 15 deletions matlab/src/cpp/arrow/matlab/type/proxy/time32_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

namespace arrow::matlab::type::proxy {

Time32Type::Time32Type(std::shared_ptr<arrow::Time32Type> time32_type) : FixedWidthType(std::move(time32_type)) {
REGISTER_METHOD(Time32Type, getTimeUnit);
}
Time32Type::Time32Type(std::shared_ptr<arrow::Time32Type> time32_type) : TimeType(std::move(time32_type)) {}

libmexclass::proxy::MakeResult Time32Type::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) {
namespace mda = ::matlab::data;
Expand All @@ -45,16 +43,4 @@ namespace arrow::matlab::type::proxy {
auto time_type = std::static_pointer_cast<arrow::Time32Type>(type);
return std::make_shared<Time32TypeProxy>(std::move(time_type));
}

void Time32Type::getTimeUnit(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
mda::ArrayFactory factory;

auto time32_type = std::static_pointer_cast<arrow::Time32Type>(data_type);
const auto timeunit = time32_type->unit();
// Cast to uint8_t since there are only four supported TimeUnit enumeration values:
// Nanosecond, Microsecond, Millisecond, Second
auto timeunit_mda = factory.createScalar(static_cast<uint8_t>(timeunit));
context.outputs[0] = timeunit_mda;
}
}
7 changes: 2 additions & 5 deletions matlab/src/cpp/arrow/matlab/type/proxy/time32_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

#pragma once

#include "arrow/matlab/type/proxy/fixed_width_type.h"
#include "arrow/type_traits.h"
#include "arrow/matlab/type/proxy/time_type.h"

namespace arrow::matlab::type::proxy {

class Time32Type : public arrow::matlab::type::proxy::FixedWidthType {
class Time32Type : public arrow::matlab::type::proxy::TimeType {

public:
Time32Type(std::shared_ptr<arrow::Time32Type> time32_type);
Expand All @@ -31,8 +30,6 @@ class Time32Type : public arrow::matlab::type::proxy::FixedWidthType {

static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments);

protected:
void getTimeUnit(libmexclass::proxy::method::Context& context);
};

}
Expand Down
37 changes: 37 additions & 0 deletions matlab/src/cpp/arrow/matlab/type/proxy/time_type.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.

#include "arrow/matlab/type/proxy/time_type.h"

namespace arrow::matlab::type::proxy {

TimeType::TimeType(std::shared_ptr<arrow::TimeType> time_type) : FixedWidthType(std::move(time_type)) {
REGISTER_METHOD(TimeType, getTimeUnit);
}

void TimeType::getTimeUnit(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
mda::ArrayFactory factory;

auto time_type = std::static_pointer_cast<arrow::TimeType>(data_type);
const auto timeunit = time_type->unit();
// Cast to uint8_t since there are only four supported TimeUnit enumeration values:
// Nanosecond, Microsecond, Millisecond, Second
auto timeunit_mda = factory.createScalar(static_cast<uint8_t>(timeunit));
context.outputs[0] = timeunit_mda;
}
}
35 changes: 35 additions & 0 deletions matlab/src/cpp/arrow/matlab/type/proxy/time_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#pragma once

#include "arrow/matlab/type/proxy/fixed_width_type.h"

namespace arrow::matlab::type::proxy {

class TimeType : public arrow::matlab::type::proxy::FixedWidthType {

public:
TimeType(std::shared_ptr<arrow::TimeType> time_type);

~TimeType() {}

protected:
void getTimeUnit(libmexclass::proxy::method::Context& context);
};

}
20 changes: 2 additions & 18 deletions matlab/src/matlab/+arrow/+type/Time32Type.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
% implied. See the License for the specific language governing
% permissions and limitations under the License.

classdef Time32Type < arrow.type.TemporalType

properties(Dependent, GetAccess=public, SetAccess=private)
TimeUnit
end
classdef Time32Type < arrow.type.TimeType

methods
function obj = Time32Type(proxy)
Expand All @@ -28,19 +24,7 @@
end
import arrow.internal.proxy.validate

obj@arrow.type.TemporalType(proxy);
end

function timeUnit = get.TimeUnit(obj)
timeUnitValue = obj.Proxy.getTimeUnit();
timeUnit = arrow.type.TimeUnit(timeUnitValue);
end
end

methods (Access=protected)
function group = getPropertyGroups(~)
targets = ["ID" "TimeUnit"];
group = matlab.mixin.util.PropertyGroup(targets);
obj@arrow.type.TimeType(proxy);
end
end

Expand Down
45 changes: 45 additions & 0 deletions matlab/src/matlab/+arrow/+type/TimeType.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%TIMETYPE Type class for time data.

% 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 TimeType < arrow.type.TemporalType

properties(Dependent, SetAccess=private, GetAccess=public)
TimeUnit
end

methods
function obj = TimeType(proxy)
arguments
proxy(1, 1) libmexclass.proxy.Proxy
end
obj@arrow.type.TemporalType(proxy);
end

function timeUnit = get.TimeUnit(obj)
timeUnitValue = obj.Proxy.getTimeUnit();
timeUnit = arrow.type.TimeUnit(timeUnitValue);
end
end

methods (Access=protected)
function group = getPropertyGroups(~)
targets = ["ID" "TimeUnit"];
group = matlab.mixin.util.PropertyGroup(targets);
end
end

end
1 change: 1 addition & 0 deletions matlab/tools/cmake/BuildMatlabArrowInterface.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(MATLAB_ARROW_LIBMEXCLASS_CLIENT_PROXY_SOURCES "${CMAKE_SOURCE_DIR}/src/cpp/a
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/fixed_width_type.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/string_type.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/timestamp_type.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/time_type.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/time32_type.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/field.cc"
"${CMAKE_SOURCE_DIR}/src/cpp/arrow/matlab/type/proxy/wrap.cc"
Expand Down

0 comments on commit 45e8448

Please sign in to comment.