forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-37041: [MATLAB] Implement Feather V1 Reader using new MATLAB…
… Interface APIs (apache#37044) ### Rationale for this change Now that we've have the basic building blocks for tabular IO in the MATLAB Interface (Array, Schema, RecordBatch), we can implement a Feather V1 reader in terms of the new APIs. This is a follow up to apache#37043, where a new Feather V1 internal `Writer` object was added. ### What changes are included in this PR? 1. Added a new class called arrow.internal.io.feather.Reader which can be used to read Feather V1 files. It has one public property named `Filename` and one public method named `read`. **Example Usage:** ```matlab >> T = array2table(rand(3)) T = 3x3 table Var1 Var2 Var3 _______ ________ _______ 0.79221 0.035712 0.67874 0.95949 0.84913 0.75774 0.65574 0.93399 0.74313 >> filename = "test.feather"; >> featherwrite(filename, T) >> reader = arrow.internal.io.feather.Reader(filename) reader = Reader with properties: Filename: "test.feather" >> T = reader.read() T = 3x3 table Var1 Var2 Var3 _______ ________ _______ 0.79221 0.035712 0.67874 0.95949 0.84913 0.75774 0.65574 0.93399 0.74313 ``` ### Are these changes tested? Yes. 1. Added `Reader` to `feather/tRoundTrip.m`. ### Are there any user-facing changes? No. These are only internal objects right now. ### Future Directions 1. Re-implement `featherread` in terms of the new `Reader` object. 2. Remove legacy feather code and infrastructure. ### Notes 1. For conciseness, I renamed the C++ Proxy class `FeatherWriter` to `Writer` since it is already inside of a `feather` namespace / "package". * Closes: apache#37041 Authored-by: Kevin Gurney <[email protected]> Signed-off-by: Kevin Gurney <[email protected]>
- Loading branch information
1 parent
71329ce
commit 152be67
Showing
10 changed files
with
219 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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 "libmexclass/proxy/ProxyManager.h" | ||
|
||
#include "arrow/matlab/error/error.h" | ||
#include "arrow/matlab/io/feather/proxy/reader.h" | ||
#include "arrow/matlab/tabular/proxy/record_batch.h" | ||
|
||
#include "arrow/util/utf8.h" | ||
|
||
#include "arrow/result.h" | ||
|
||
#include "arrow/io/file.h" | ||
#include "arrow/ipc/feather.h" | ||
#include "arrow/table.h" | ||
|
||
namespace arrow::matlab::io::feather::proxy { | ||
|
||
Reader::Reader(const std::string& filename) : filename{filename} { | ||
REGISTER_METHOD(Reader, read); | ||
REGISTER_METHOD(Reader, getFilename); | ||
} | ||
|
||
libmexclass::proxy::MakeResult Reader::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) { | ||
namespace mda = ::matlab::data; | ||
using ReaderProxy = arrow::matlab::io::feather::proxy::Reader; | ||
|
||
mda::StructArray args = constructor_arguments[0]; | ||
const mda::StringArray filename_utf16_mda = args[0]["Filename"]; | ||
const auto filename_utf16 = std::u16string(filename_utf16_mda[0]); | ||
MATLAB_ASSIGN_OR_ERROR(const auto filename, arrow::util::UTF16StringToUTF8(filename_utf16), error::UNICODE_CONVERSION_ERROR_ID); | ||
|
||
return std::make_shared<ReaderProxy>(filename); | ||
} | ||
|
||
void Reader::read(libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
using namespace libmexclass::proxy; | ||
using RecordBatchProxy = arrow::matlab::tabular::proxy::RecordBatch; | ||
|
||
mda::ArrayFactory factory; | ||
|
||
// Create a file input stream. | ||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto source, arrow::io::ReadableFile::Open(filename, arrow::default_memory_pool()), context, error::FAILED_TO_OPEN_FILE_FOR_READ); | ||
|
||
// Create a Reader from the file input stream. | ||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto reader, arrow::ipc::feather::Reader::Open(source), context, error::FEATHER_FAILED_TO_CREATE_READER); | ||
|
||
// Error if not Feather V1. | ||
const auto version = reader->version(); | ||
if (version == ipc::feather::kFeatherV2Version) { | ||
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(Status::NotImplemented("Support for Feather V2 has not been implemented."), context, error::FEATHER_VERSION_2); | ||
} else if (version != ipc::feather::kFeatherV1Version) { | ||
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(Status::Invalid("Unknown Feather format version."), context, error::FEATHER_VERSION_UNKNOWN); | ||
} | ||
|
||
// Read a Table from the file. | ||
std::shared_ptr<arrow::Table> table = nullptr; | ||
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(reader->Read(&table), context, error::FEATHER_FAILED_TO_READ_TABLE); | ||
|
||
// Get the first RecordBatch from the Table. | ||
arrow::TableBatchReader table_batch_reader{table}; | ||
std::shared_ptr<arrow::RecordBatch> record_batch = nullptr; | ||
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(table_batch_reader.ReadNext(&record_batch), context, error::FEATHER_FAILED_TO_READ_RECORD_BATCH); | ||
|
||
// Create a Proxy from the first RecordBatch. | ||
auto record_batch_proxy = std::make_shared<RecordBatchProxy>(record_batch); | ||
const auto record_batch_proxy_id = ProxyManager::manageProxy(record_batch_proxy); | ||
|
||
const auto record_batch_proxy_id_mda = factory.createScalar(record_batch_proxy_id); | ||
|
||
context.outputs[0] = record_batch_proxy_id_mda; | ||
} | ||
|
||
void Reader::getFilename(libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
mda::ArrayFactory factory; | ||
|
||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(const auto filename_utf16, arrow::util::UTF8StringToUTF16(filename), context, error::UNICODE_CONVERSION_ERROR_ID); | ||
auto filename_utf16_mda = factory.createScalar(filename_utf16); | ||
context.outputs[0] = filename_utf16_mda; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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 "libmexclass/proxy/Proxy.h" | ||
|
||
namespace arrow::matlab::io::feather::proxy { | ||
|
||
class Reader : public libmexclass::proxy::Proxy { | ||
public: | ||
Reader(const std::string& filename); | ||
|
||
virtual ~Reader() {} | ||
|
||
static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments); | ||
|
||
protected: | ||
void read(libmexclass::proxy::method::Context& context); | ||
void getFilename(libmexclass::proxy::method::Context& context); | ||
|
||
const std::string filename; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
% 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 Reader | ||
%READER An internal Reader object for reading Feather files. | ||
|
||
properties (GetAccess=public, SetAccess=private, Hidden) | ||
Proxy | ||
end | ||
|
||
properties (Dependent, SetAccess=private, GetAccess=public) | ||
% Name of the file to read. | ||
Filename | ||
end | ||
|
||
methods | ||
|
||
function obj = Reader(filename) | ||
arguments | ||
filename(1, 1) {mustBeNonmissing, mustBeNonzeroLengthText} | ||
end | ||
|
||
args = struct(Filename=filename); | ||
obj.Proxy = arrow.internal.proxy.create("arrow.io.feather.proxy.Reader", args); | ||
end | ||
|
||
function T = read(obj) | ||
recordBatchProxyID = obj.Proxy.read(); | ||
proxy = libmexclass.proxy.Proxy(Name="arrow.tabular.proxy.RecordBatch", ID=recordBatchProxyID); | ||
recordBatch = arrow.tabular.RecordBatch(proxy); | ||
T = recordBatch.toMATLAB(); | ||
end | ||
|
||
function filename = get.Filename(obj) | ||
filename = obj.Proxy.getFilename(); | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters