From 6d1208fea64cbfb48e43e50fbc68dc2bc3e5dd91 Mon Sep 17 00:00:00 2001 From: samuelpwlim Date: Sun, 8 Dec 2024 15:48:24 -0500 Subject: [PATCH] Added ReadTable method to read an entire file as table and respective test --- matlab/src/cpp/arrow/matlab/error/error.h | 1 + .../io/ipc/proxy/record_batch_file_reader.cc | 18 ++++++++++++++++++ .../io/ipc/proxy/record_batch_file_reader.h | 2 ++ .../test/arrow/io/ipc/tRecordBatchFileReader.m | 15 +++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/matlab/src/cpp/arrow/matlab/error/error.h b/matlab/src/cpp/arrow/matlab/error/error.h index e5a5df6f4bc..35df0b0af9f 100644 --- a/matlab/src/cpp/arrow/matlab/error/error.h +++ b/matlab/src/cpp/arrow/matlab/error/error.h @@ -249,5 +249,6 @@ static const char* IPC_RECORD_BATCH_READER_OPEN_FAILED = "arrow:io:ipc:FailedToOpenRecordBatchReader"; static const char* IPC_RECORD_BATCH_READ_INVALID_INDEX = "arrow:io:ipc:InvalidIndex"; static const char* IPC_RECORD_BATCH_READ_FAILED = "arrow:io:ipc:ReadFailed"; +static const char* IPC_TABLE_READ_FAILED = "arrow:io:ipc:TableReadFailed"; } // namespace arrow::matlab::error diff --git a/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.cc b/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.cc index 9db5d6a9baf..ae8ef8639c5 100644 --- a/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.cc +++ b/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.cc @@ -47,6 +47,7 @@ RecordBatchFileReader::RecordBatchFileReader( REGISTER_METHOD(RecordBatchFileReader, getNumRecordBatches); REGISTER_METHOD(RecordBatchFileReader, getSchema); REGISTER_METHOD(RecordBatchFileReader, readRecordBatchAtIndex); + REGISTER_METHOD(RecordBatchFileReader, readTable); } libmexclass::proxy::MakeResult RecordBatchFileReader::make( @@ -125,4 +126,21 @@ void RecordBatchFileReader::readRecordBatchAtIndex( context.outputs[0] = record_batch_proxyy_id_mda; } +void RecordBatchFileReader::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(std::move(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; +} + } // namespace arrow::matlab::io::ipc::proxy \ No newline at end of file diff --git a/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.h b/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.h index 41d5c9a1588..f3c217bd7ba 100644 --- a/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.h +++ b/matlab/src/cpp/arrow/matlab/io/ipc/proxy/record_batch_file_reader.h @@ -39,6 +39,8 @@ class RecordBatchFileReader : public libmexclass::proxy::Proxy { void getSchema(libmexclass::proxy::method::Context& context); void readRecordBatchAtIndex(libmexclass::proxy::method::Context& context); + + void readTable(libmexclass::proxy::method::Context& context); }; } // namespace arrow::matlab::io::ipc::proxy \ No newline at end of file diff --git a/matlab/test/arrow/io/ipc/tRecordBatchFileReader.m b/matlab/test/arrow/io/ipc/tRecordBatchFileReader.m index eb0069d3077..61994bf4ea9 100644 --- a/matlab/test/arrow/io/ipc/tRecordBatchFileReader.m +++ b/matlab/test/arrow/io/ipc/tRecordBatchFileReader.m @@ -180,5 +180,20 @@ function readAtIndex(testCase) expected2 = arrow.recordBatch(t2); testCase.verifyEqual(actual2, expected2); end + + function readTable(testCase) + % Verify readTable returns the expected table for the given + % file + // t1 = table(["Row1"; "Row2"], single([1; 2]), VariableNames=["A", "B"]); + // t2 = table(["Row3"; "Row4"], single([3; 4]), VariableNames=["A", "B"]); + expectedTable = [t1]; + + reader = arrow.io.ipc.RecordBatchFileReader(testCase.MultipleBatchFile); + actualTable = reader.readTable(); + + testCase.verifyEqual(actualTable, expectedTable); + testCase.verifyClass(actualTable, "table"); + testCase.verifySize(actualTable, [4, 2]); + end end end \ No newline at end of file