-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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-44923: [MATLAB] Add IPC RecordBatchStreamReader
MATLAB class
#45068
Open
kevingurney
wants to merge
20
commits into
apache:main
Choose a base branch
from
mathworks:GH-44923
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+622
−0
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6db726a
Register RecordBatchStreamReader proxy with proxy factory.
kevingurney 9396088
Partial implementation for C++ RecordBatchStreamReader class.
kevingurney 75902fe
Fix compilation errors.
kevingurney 55dee07
Basic MATLAB implementation of RecordBatchStreamReader class.
kevingurney a2d5673
Check for NULL record batch in hasNextRecordBatch.
kevingurney 8b5574f
Handle end of stream case when calling readRecordBatch.
kevingurney fab1063
Implement readTable, hasnext, and done methods.
kevingurney 432eae8
Add basic MATLAB tests for RecordBatchStreamReader class.
kevingurney c2a9d2e
Add test which verifies that an error is thrown if provided with a fi…
kevingurney 63f6314
Fix formatting in factory.cc based on CI failures.
kevingurney 481a095
Fix formatting in record_batch_stream_reader.cc based on CI failures.
kevingurney 9531e70
Add error ID constant for arrow:io:ipc:EndOfStream.
kevingurney 2c49160
Update test case name and comment for testing for arrow:io:ipc:EndOfS…
kevingurney 2868d0a
Fix incorrect comment for multiple batch read test case.
kevingurney 4c6af41
Fix incorrect comment for hasnext method test case.
kevingurney 380708e
Fix incorrect comments for done method test case.
kevingurney 2fccd18
Fix incorrect error message ID being used when unable to open file fo…
kevingurney 8f23362
Alphabetize headers.
kevingurney 1915da4
Fix linting errors.
kevingurney d3d5255
Fix typo.
kevingurney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
154 changes: 154 additions & 0 deletions
154
matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_stream_reader.cc
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,154 @@ | ||
// 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/io/ipc/proxy/record_batch_stream_reader.h" | ||
#include "arrow/io/file.h" | ||
#include "arrow/matlab/error/error.h" | ||
#include "arrow/matlab/tabular/proxy/record_batch.h" | ||
#include "arrow/matlab/tabular/proxy/schema.h" | ||
#include "arrow/matlab/tabular/proxy/table.h" | ||
#include "arrow/util/utf8.h" | ||
|
||
#include "libmexclass/proxy/ProxyManager.h" | ||
|
||
namespace arrow::matlab::io::ipc::proxy { | ||
|
||
RecordBatchStreamReader::RecordBatchStreamReader( | ||
const std::shared_ptr<arrow::ipc::RecordBatchStreamReader> reader) | ||
: reader{std::move(reader)} { | ||
REGISTER_METHOD(RecordBatchStreamReader, getSchema); | ||
REGISTER_METHOD(RecordBatchStreamReader, readRecordBatch); | ||
REGISTER_METHOD(RecordBatchStreamReader, hasNextRecordBatch); | ||
REGISTER_METHOD(RecordBatchStreamReader, readTable); | ||
} | ||
|
||
libmexclass::proxy::MakeResult RecordBatchStreamReader::make( | ||
const libmexclass::proxy::FunctionArguments& constructor_arguments) { | ||
namespace mda = ::matlab::data; | ||
using RecordBatchStreamReaderProxy = | ||
arrow::matlab::io::ipc::proxy::RecordBatchStreamReader; | ||
|
||
const mda::StructArray opts = constructor_arguments[0]; | ||
|
||
const mda::StringArray filename_mda = opts[0]["Filename"]; | ||
const auto filename_utf16 = std::u16string(filename_mda[0]); | ||
MATLAB_ASSIGN_OR_ERROR(const auto filename_utf8, | ||
arrow::util::UTF16StringToUTF8(filename_utf16), | ||
error::UNICODE_CONVERSION_ERROR_ID); | ||
|
||
MATLAB_ASSIGN_OR_ERROR(auto input_stream, arrow::io::ReadableFile::Open(filename_utf8), | ||
error::FAILED_TO_OPEN_FILE_FOR_READ); | ||
|
||
MATLAB_ASSIGN_OR_ERROR(auto reader, | ||
arrow::ipc::RecordBatchStreamReader::Open(input_stream), | ||
error::IPC_RECORD_BATCH_READER_OPEN_FAILED); | ||
|
||
return std::make_shared<RecordBatchStreamReaderProxy>(std::move(reader)); | ||
} | ||
|
||
void RecordBatchStreamReader::getSchema(libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
using SchemaProxy = arrow::matlab::tabular::proxy::Schema; | ||
|
||
auto schema = reader->schema(); | ||
|
||
auto schema_proxy = std::make_shared<SchemaProxy>(std::move(schema)); | ||
const auto schema_proxy_id = | ||
libmexclass::proxy::ProxyManager::manageProxy(schema_proxy); | ||
|
||
mda::ArrayFactory factory; | ||
const auto schema_proxy_id_mda = factory.createScalar(schema_proxy_id); | ||
context.outputs[0] = schema_proxy_id_mda; | ||
} | ||
|
||
void RecordBatchStreamReader::readTable(libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
using TableProxy = arrow::matlab::tabular::proxy::Table; | ||
|
||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(auto table, reader->ToTable(), context, | ||
error::IPC_TABLE_READ_FAILED); | ||
auto table_proxy = std::make_shared<TableProxy>(table); | ||
const auto table_proxy_id = libmexclass::proxy::ProxyManager::manageProxy(table_proxy); | ||
|
||
mda::ArrayFactory factory; | ||
const auto table_proxy_id_mda = factory.createScalar(table_proxy_id); | ||
context.outputs[0] = table_proxy_id_mda; | ||
} | ||
|
||
void RecordBatchStreamReader::readRecordBatch( | ||
libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
using RecordBatchProxy = arrow::matlab::tabular::proxy::RecordBatch; | ||
using namespace libmexclass::error; | ||
// If we don't have a "pre-cached" record batch to return, then try reading another | ||
// record batch from the IPC Stream. If there are no more record batches in the stream, | ||
// then error. | ||
if (!nextRecordBatch) { | ||
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(nextRecordBatch, reader->Next(), context, | ||
error::IPC_RECORD_BATCH_READ_FAILED); | ||
} | ||
// Even if the read was "successful", the resulting record batch may be empty, | ||
// signaling the end of the stream. | ||
if (!nextRecordBatch) { | ||
context.error = | ||
Error{error::IPC_END_OF_STREAM, | ||
"Reached end of Arrow IPC Stream. No more record batches to read."}; | ||
return; | ||
} | ||
auto record_batch_proxy = std::make_shared<RecordBatchProxy>(nextRecordBatch); | ||
const auto record_batch_proxy_id = | ||
libmexclass::proxy::ProxyManager::manageProxy(record_batch_proxy); | ||
// Once we have "consumed" the next RecordBatch, set nextRecordBatch to nullptr | ||
// so that the next call to hasNextRecordBatch correctly checks whether there are more | ||
// record batches remaining in the IPC Stream. | ||
nextRecordBatch = nullptr; | ||
mda::ArrayFactory factory; | ||
const auto record_batch_proxy_id_mda = factory.createScalar(record_batch_proxy_id); | ||
context.outputs[0] = record_batch_proxy_id_mda; | ||
} | ||
|
||
void RecordBatchStreamReader::hasNextRecordBatch( | ||
libmexclass::proxy::method::Context& context) { | ||
namespace mda = ::matlab::data; | ||
bool has_next_record_batch = true; | ||
if (!nextRecordBatch) { | ||
// Try to read another RecordBatch from the | ||
// IPC Stream. | ||
auto maybe_record_batch = reader->Next(); | ||
if (!maybe_record_batch.ok()) { | ||
has_next_record_batch = false; | ||
} else { | ||
// If we read a RecordBatch successfully, | ||
// then "cache" the RecordBatch | ||
// so that we can return it on the next | ||
// call to readRecordBatch. | ||
nextRecordBatch = *maybe_record_batch; | ||
|
||
// Even if the read was "successful", the resulting | ||
// record batch may be empty, signaling that | ||
// the end of the IPC stream has been reached. | ||
if (!nextRecordBatch) { | ||
has_next_record_batch = false; | ||
} | ||
} | ||
} | ||
|
||
mda::ArrayFactory factory; | ||
context.outputs[0] = factory.createScalar(has_next_record_batch); | ||
} | ||
|
||
} // namespace arrow::matlab::io::ipc::proxy |
44 changes: 44 additions & 0 deletions
44
matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_stream_reader.h
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,44 @@ | ||
// 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/ipc/reader.h" | ||
#include "libmexclass/proxy/Proxy.h" | ||
|
||
namespace arrow::matlab::io::ipc::proxy { | ||
|
||
class RecordBatchStreamReader : public libmexclass::proxy::Proxy { | ||
public: | ||
RecordBatchStreamReader(std::shared_ptr<arrow::ipc::RecordBatchStreamReader> reader); | ||
|
||
~RecordBatchStreamReader() = default; | ||
|
||
static libmexclass::proxy::MakeResult make( | ||
const libmexclass::proxy::FunctionArguments& constructor_arguments); | ||
|
||
protected: | ||
std::shared_ptr<arrow::ipc::RecordBatchStreamReader> reader; | ||
std::shared_ptr<arrow::RecordBatch> nextRecordBatch; | ||
|
||
void getSchema(libmexclass::proxy::method::Context& context); | ||
void readRecordBatch(libmexclass::proxy::method::Context& context); | ||
void hasNextRecordBatch(libmexclass::proxy::method::Context& context); | ||
void readTable(libmexclass::proxy::method::Context& context); | ||
}; | ||
|
||
} // namespace arrow::matlab::io::ipc::proxy |
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
83 changes: 83 additions & 0 deletions
83
matlab/src/matlab/+arrow/+io/+ipc/RecordBatchStreamReader.m
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,83 @@ | ||
%RECORDBATCHSTREAMREADER Class for reading Arrow record batches from the | ||
% Arrow IPC Stream format. | ||
|
||
% 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 RecordBatchStreamReader < matlab.mixin.Scalar | ||
|
||
properties(SetAccess=private, GetAccess=public, Hidden) | ||
Proxy | ||
end | ||
|
||
properties (Dependent, SetAccess=private, GetAccess=public) | ||
Schema | ||
end | ||
|
||
methods | ||
function obj = RecordBatchStreamReader(filename) | ||
arguments | ||
filename(1, 1) string {mustBeNonzeroLengthText} | ||
end | ||
args = struct(Filename=filename); | ||
proxyName = "arrow.io.ipc.proxy.RecordBatchStreamReader"; | ||
obj.Proxy = arrow.internal.proxy.create(proxyName, args); | ||
end | ||
|
||
function schema = get.Schema(obj) | ||
proxyID = obj.Proxy.getSchema(); | ||
proxyName = "arrow.tabular.proxy.Schema"; | ||
proxy = libmexclass.proxy.Proxy(ID=proxyID, Name=proxyName); | ||
schema = arrow.tabular.Schema(proxy); | ||
end | ||
|
||
function tf = hasnext(obj) | ||
tf = obj.Proxy.hasNextRecordBatch(); | ||
end | ||
|
||
function tf = done(obj) | ||
tf = ~obj.Proxy.hasNextRecordBatch(); | ||
end | ||
|
||
function arrowRecordBatch = read(obj) | ||
% NOTE: This function is a "convenience alias" for the readRecordBatch | ||
% method, which has a longer name. This is the exact same implementation | ||
% as readRecordBatch. Since this method might be called in a tight loop, | ||
% it should be slightly more efficient to call the C++ code directly, | ||
% rather than invoking obj.readRecordBatch indirectly. We are intentionally | ||
% trading off code duplication for performance here. | ||
proxyID = obj.Proxy.readRecordBatch(); | ||
proxyName = "arrow.tabular.proxy.RecordBatch"; | ||
proxy = libmexclass.proxy.Proxy(ID=proxyID, Name=proxyName); | ||
arrowRecordBatch = arrow.tabular.RecordBatch(proxy); | ||
end | ||
|
||
function arrowRecordBatch = readRecordBatch(obj) | ||
proxyID = obj.Proxy.readRecordBatch(); | ||
proxyName = "arrow.tabular.proxy.RecordBatch"; | ||
proxy = libmexclass.proxy.Proxy(ID=proxyID, Name=proxyName); | ||
arrowRecordBatch = arrow.tabular.RecordBatch(proxy); | ||
end | ||
|
||
function arrowTable = readTable(obj) | ||
proxyID = obj.Proxy.readTable(); | ||
proxyName = "arrow.tabular.proxy.Table"; | ||
proxy = libmexclass.proxy.Proxy(ID=proxyID, Name=proxyName); | ||
arrowTable = arrow.tabular.Table(proxy); | ||
end | ||
|
||
end | ||
|
||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with MATLAB but does MATLAB require those APIs for iterator (or something)?
If we don't require this, it may be better that we don't provide them... If we don't have them, we can simplify our implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question.
There is no specific API for iteration in MATLAB (i.e. there is no
for each
style iteration). So, we were trying to emulate this behavior using a "while has more data"-style loop. There is precedent for this pattern of iteration being used elsewhere in MATLAB.Does that answer your question?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Yes.
One more question. Is
while record_batch = reader.read()
-style loop used in MATLAB too? If the style is also used in MATLAB, we may want to recommend the style for the MATLAB bindings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, no - MATLAB does not support variable assignment like
while record_batch = reader.read()
inside of awhile
loop expression, either. If that syntax was supported, I agree it would be preferable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see!