Skip to content
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-44922: [MATLAB] Add IPC RecordBatchStreamFileWriter MATLAB class #44925

Merged
merged 20 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4986f1d
Add RecordBatchWriter proxy class
sgilmore10 Dec 3, 2024
2e7c2f2
Add record_batch_writer.cc to CMake sources
sgilmore10 Dec 3, 2024
b4eba33
Re-implement proxy::RecordBatchFileWriter to inherit from proxy::Reco…
sgilmore10 Dec 3, 2024
a72d5ad
Add RecordBatchWriter MATLAB class
sgilmore10 Dec 3, 2024
1160fa7
Update RecordBatchFileWriter to inherit from RecordBatchWriter
sgilmore10 Dec 3, 2024
81f64f7
Add RecordBatchStreamWriter Proxy class
sgilmore10 Dec 3, 2024
b2402fe
Add record_batch_stream_writer.cc to CMake sources
sgilmore10 Dec 3, 2024
db64729
Fix RecordBatchStreamWriter compiler errors
sgilmore10 Dec 3, 2024
2caefcd
Add RecordBatchStreamWriter MATLAB class
sgilmore10 Dec 3, 2024
8ba7709
Add #pragma once
sgilmore10 Dec 3, 2024
5f888fd
Register arrow.io.ipc.proxy.RecordBatchStreamReader class
sgilmore10 Dec 3, 2024
ce2b6a3
Parameterize tRecordBatchFileWriter to test both RecordBatchFileWrite…
sgilmore10 Dec 3, 2024
b8dc29b
Rename tRecordBatchFileWriter.m to tRecordBatchWriter.m
sgilmore10 Dec 3, 2024
87a7b0d
Ran clang-format
sgilmore10 Dec 3, 2024
e0417e1
Update matlab/src/matlab/+arrow/+io/+ipc/RecordBatchFileWriter.m
sgilmore10 Dec 4, 2024
bfed788
Deleted declaration of un-implemented static method RecordBatchWriter…
sgilmore10 Dec 4, 2024
86092fc
Update matlab/src/matlab/+arrow/+io/+ipc/RecordBatchWriter.m
sgilmore10 Dec 4, 2024
6dbb881
Update matlab/src/matlab/+arrow/+io/+ipc/RecordBatchWriter.m
sgilmore10 Dec 4, 2024
ac2c18a
Update matlab/src/matlab/+arrow/+io/+ipc/RecordBatchWriter.m
sgilmore10 Dec 4, 2024
cd08378
Update error message ID
sgilmore10 Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
#include "arrow/matlab/io/ipc/proxy/record_batch_file_writer.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"
Expand All @@ -29,11 +27,7 @@ namespace arrow::matlab::io::ipc::proxy {

RecordBatchFileWriter::RecordBatchFileWriter(
const std::shared_ptr<arrow::ipc::RecordBatchWriter> writer)
: writer{std::move(writer)} {
REGISTER_METHOD(RecordBatchFileWriter, close);
REGISTER_METHOD(RecordBatchFileWriter, writeRecordBatch);
REGISTER_METHOD(RecordBatchFileWriter, writeTable);
}
: RecordBatchWriter(std::move(writer)) {}

libmexclass::proxy::MakeResult RecordBatchFileWriter::make(
const libmexclass::proxy::FunctionArguments& constructor_arguments) {
Expand Down Expand Up @@ -65,43 +59,4 @@ libmexclass::proxy::MakeResult RecordBatchFileWriter::make(
return std::make_shared<RecordBatchFileWriterProxy>(std::move(writer));
}

void RecordBatchFileWriter::writeRecordBatch(
libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
using RecordBatchProxy = ::arrow::matlab::tabular::proxy::RecordBatch;

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<uint64_t> record_batch_proxy_id_mda =
opts[0]["RecordBatchProxyID"];
const uint64_t record_batch_proxy_id = record_batch_proxy_id_mda[0];

auto proxy = libmexclass::proxy::ProxyManager::getProxy(record_batch_proxy_id);
auto record_batch_proxy = std::static_pointer_cast<RecordBatchProxy>(proxy);
auto record_batch = record_batch_proxy->unwrap();

MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->WriteRecordBatch(*record_batch), context,
error::IPC_RECORD_BATCH_WRITE_FAILED);
}

void RecordBatchFileWriter::writeTable(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
using TableProxy = ::arrow::matlab::tabular::proxy::Table;

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<uint64_t> table_proxy_id_mda = opts[0]["TableProxyID"];
const uint64_t table_proxy_id = table_proxy_id_mda[0];

auto proxy = libmexclass::proxy::ProxyManager::getProxy(table_proxy_id);
auto table_proxy = std::static_pointer_cast<TableProxy>(proxy);
auto table = table_proxy->unwrap();

MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->WriteTable(*table), context,
error::IPC_RECORD_BATCH_WRITE_FAILED);
}

void RecordBatchFileWriter::close(libmexclass::proxy::method::Context& context) {
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->Close(), context,
error::IPC_RECORD_BATCH_WRITE_CLOSE_FAILED);
}

} // namespace arrow::matlab::io::ipc::proxy
} // namespace arrow::matlab::io::ipc::proxy
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@
// under the License.

#include "arrow/ipc/writer.h"
#include "arrow/matlab/io/ipc/proxy/record_batch_writer.h"

#include "libmexclass/proxy/Proxy.h"

namespace arrow::matlab::io::ipc::proxy {

class RecordBatchFileWriter : public libmexclass::proxy::Proxy {
class RecordBatchFileWriter : public RecordBatchWriter {
public:
RecordBatchFileWriter(std::shared_ptr<arrow::ipc::RecordBatchWriter> writer);

~RecordBatchFileWriter() = default;
virtual ~RecordBatchFileWriter() = default;

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

protected:
std::shared_ptr<arrow::ipc::RecordBatchWriter> writer;

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

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

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

} // namespace arrow::matlab::io::ipc::proxy
} // namespace arrow::matlab::io::ipc::proxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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_writer.h"
#include "arrow/io/file.h"
#include "arrow/ipc/writer.h"
#include "arrow/matlab/error/error.h"
#include "arrow/matlab/tabular/proxy/schema.h"
#include "arrow/util/utf8.h"

#include "libmexclass/proxy/ProxyManager.h"

namespace arrow::matlab::io::ipc::proxy {

RecordBatchStreamWriter::RecordBatchStreamWriter(
const std::shared_ptr<arrow::ipc::RecordBatchWriter> writer)
: RecordBatchWriter(std::move(writer)) {}

libmexclass::proxy::MakeResult RecordBatchStreamWriter::make(
const libmexclass::proxy::FunctionArguments& constructor_arguments) {
namespace mda = ::matlab::data;
using RecordBatchStreamWriterProxy =
arrow::matlab::io::ipc::proxy::RecordBatchStreamWriter;
using SchemaProxy = arrow::matlab::tabular::proxy::Schema;

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);

const mda::TypedArray<uint64_t> arrow_schema_proxy_id_mda = opts[0]["SchemaProxyID"];
auto proxy = libmexclass::proxy::ProxyManager::getProxy(arrow_schema_proxy_id_mda[0]);
auto arrow_schema_proxy = std::static_pointer_cast<SchemaProxy>(proxy);
auto arrow_schema = arrow_schema_proxy->unwrap();

MATLAB_ASSIGN_OR_ERROR(auto output_stream,
arrow::io::FileOutputStream::Open(filename_utf8),
error::FAILED_TO_OPEN_FILE_FOR_WRITE);

MATLAB_ASSIGN_OR_ERROR(auto writer,
arrow::ipc::MakeStreamWriter(output_stream, arrow_schema),
"arrow:matlab:MakeFailed");

return std::make_shared<RecordBatchStreamWriterProxy>(std::move(writer));
}

} // namespace arrow::matlab::io::ipc::proxy
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.

#include "arrow/ipc/writer.h"
#include "arrow/matlab/io/ipc/proxy/record_batch_writer.h"

#include "libmexclass/proxy/Proxy.h"

namespace arrow::matlab::io::ipc::proxy {

class RecordBatchStreamWriter : public RecordBatchWriter {
public:
RecordBatchStreamWriter(std::shared_ptr<arrow::ipc::RecordBatchWriter> writer);

virtual ~RecordBatchStreamWriter() = default;

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

} // namespace arrow::matlab::io::ipc::proxy
75 changes: 75 additions & 0 deletions matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_writer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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_writer.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 "libmexclass/proxy/ProxyManager.h"

namespace arrow::matlab::io::ipc::proxy {

RecordBatchWriter::RecordBatchWriter(
const std::shared_ptr<arrow::ipc::RecordBatchWriter> writer)
: writer{std::move(writer)} {
REGISTER_METHOD(RecordBatchWriter, close);
REGISTER_METHOD(RecordBatchWriter, writeRecordBatch);
REGISTER_METHOD(RecordBatchWriter, writeTable);
}

void RecordBatchWriter::writeRecordBatch(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
using RecordBatchProxy = ::arrow::matlab::tabular::proxy::RecordBatch;

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<uint64_t> record_batch_proxy_id_mda =
opts[0]["RecordBatchProxyID"];
const uint64_t record_batch_proxy_id = record_batch_proxy_id_mda[0];

auto proxy = libmexclass::proxy::ProxyManager::getProxy(record_batch_proxy_id);
auto record_batch_proxy = std::static_pointer_cast<RecordBatchProxy>(proxy);
auto record_batch = record_batch_proxy->unwrap();

MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->WriteRecordBatch(*record_batch), context,
error::IPC_RECORD_BATCH_WRITE_FAILED);
}

void RecordBatchWriter::writeTable(libmexclass::proxy::method::Context& context) {
namespace mda = ::matlab::data;
using TableProxy = ::arrow::matlab::tabular::proxy::Table;

mda::StructArray opts = context.inputs[0];
const mda::TypedArray<uint64_t> table_proxy_id_mda = opts[0]["TableProxyID"];
const uint64_t table_proxy_id = table_proxy_id_mda[0];

auto proxy = libmexclass::proxy::ProxyManager::getProxy(table_proxy_id);
auto table_proxy = std::static_pointer_cast<TableProxy>(proxy);
auto table = table_proxy->unwrap();

MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->WriteTable(*table), context,
error::IPC_RECORD_BATCH_WRITE_FAILED);
}

void RecordBatchWriter::close(libmexclass::proxy::method::Context& context) {
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(writer->Close(), context,
error::IPC_RECORD_BATCH_WRITE_CLOSE_FAILED);
}

} // namespace arrow::matlab::io::ipc::proxy
41 changes: 41 additions & 0 deletions matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_writer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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/writer.h"
#include "libmexclass/proxy/Proxy.h"

namespace arrow::matlab::io::ipc::proxy {

class RecordBatchWriter : public libmexclass::proxy::Proxy {
public:
RecordBatchWriter(std::shared_ptr<arrow::ipc::RecordBatchWriter> writer);

virtual ~RecordBatchWriter() = default;

protected:
std::shared_ptr<arrow::ipc::RecordBatchWriter> writer;

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

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

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

} // namespace arrow::matlab::io::ipc::proxy
3 changes: 3 additions & 0 deletions matlab/src/cpp/arrow/matlab/proxy/factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "arrow/matlab/io/feather/proxy/writer.h"
#include "arrow/matlab/io/ipc/proxy/record_batch_file_reader.h"
#include "arrow/matlab/io/ipc/proxy/record_batch_file_writer.h"
#include "arrow/matlab/io/ipc/proxy/record_batch_stream_writer.h"
#include "arrow/matlab/tabular/proxy/record_batch.h"
#include "arrow/matlab/tabular/proxy/schema.h"
#include "arrow/matlab/tabular/proxy/table.h"
Expand Down Expand Up @@ -111,6 +112,8 @@ libmexclass::proxy::MakeResult Factory::make_proxy(
REGISTER_PROXY(arrow.c.proxy.RecordBatchImporter , arrow::matlab::c::proxy::RecordBatchImporter);
REGISTER_PROXY(arrow.io.ipc.proxy.RecordBatchFileReader , arrow::matlab::io::ipc::proxy::RecordBatchFileReader);
REGISTER_PROXY(arrow.io.ipc.proxy.RecordBatchFileWriter , arrow::matlab::io::ipc::proxy::RecordBatchFileWriter);
REGISTER_PROXY(arrow.io.ipc.proxy.RecordBatchStreamWriter , arrow::matlab::io::ipc::proxy::RecordBatchStreamWriter);

// clang-format on

return libmexclass::error::Error{error::UNKNOWN_PROXY_ERROR_ID,
Expand Down
Loading
Loading