From feb10f357d824a1bdadbed59b82403da13b42d28 Mon Sep 17 00:00:00 2001 From: Kevin Gurney Date: Mon, 7 Aug 2023 17:19:33 -0400 Subject: [PATCH] GH-37049: [MATLAB] Update feather `Reader` and `Writer` objects to work directly with `arrow.tabular.RecordBatch`s instead of MATLAB `table`s (#37052) ### Rationale for this change After thinking about how to re-implement `featherread` and `featherwrite`, we realized it would be better if the `Reader` and `Writer` classes worked directly with `arrow.tabular.RecordBatch`s instead of MATLAB `table`s. ### What changes are included in this PR? 1. Updated `read` method of `arrow.internal.io.feather.Reader` to return an `arrow.tabular.RecordBatch` rather than a MATLAB `table`. 2. Updated `write` method of `arrow.internal.io.feather.Writer` to accept an `arrow.tabular.RecordBatch` rather than a MATLAB `table`. ### Are these changes tested? Yes. 1. Updated `feather/tRoundTrip.m` to reflect the changes to the `Reader` and `Writer` classes. ### Are there any user-facing changes? 1. No These are internal APIs. * Closes: #37049 Authored-by: Kevin Gurney Signed-off-by: Kevin Gurney --- .../+arrow/+internal/+io/+feather/Reader.m | 3 +- .../+arrow/+internal/+io/+feather/Writer.m | 5 ++-- matlab/src/matlab/featherwrite.m | 3 +- matlab/test/arrow/io/feather/tRoundTrip.m | 30 +++++++++---------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/matlab/src/matlab/+arrow/+internal/+io/+feather/Reader.m b/matlab/src/matlab/+arrow/+internal/+io/+feather/Reader.m index 80da7294d2d8d..6cd78646767a7 100644 --- a/matlab/src/matlab/+arrow/+internal/+io/+feather/Reader.m +++ b/matlab/src/matlab/+arrow/+internal/+io/+feather/Reader.m @@ -36,11 +36,10 @@ obj.Proxy = arrow.internal.proxy.create("arrow.io.feather.proxy.Reader", args); end - function T = read(obj) + function recordBatch = 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) diff --git a/matlab/src/matlab/+arrow/+internal/+io/+feather/Writer.m b/matlab/src/matlab/+arrow/+internal/+io/+feather/Writer.m index 37c785f10a5e3..64872ba4a023c 100644 --- a/matlab/src/matlab/+arrow/+internal/+io/+feather/Writer.m +++ b/matlab/src/matlab/+arrow/+internal/+io/+feather/Writer.m @@ -35,9 +35,8 @@ obj.Proxy = arrow.internal.proxy.create(proxyName, args); end - function write(obj, T) - rb = arrow.recordbatch(T); - args = struct(RecordBatchProxyID=rb.Proxy.ID); + function write(obj, recordBatch) + args = struct(RecordBatchProxyID=recordBatch.Proxy.ID); obj.Proxy.write(args); end diff --git a/matlab/src/matlab/featherwrite.m b/matlab/src/matlab/featherwrite.m index cc3f45e954ad8..879edd8afc68e 100644 --- a/matlab/src/matlab/featherwrite.m +++ b/matlab/src/matlab/featherwrite.m @@ -28,6 +28,7 @@ function featherwrite(filename, t) t table end + recordBatch = arrow.recordbatch(t); writer = arrow.internal.io.feather.Writer(filename); - writer.write(t); + writer.write(recordBatch); end diff --git a/matlab/test/arrow/io/feather/tRoundTrip.m b/matlab/test/arrow/io/feather/tRoundTrip.m index e735d196c1875..f361a4543b8e4 100644 --- a/matlab/test/arrow/io/feather/tRoundTrip.m +++ b/matlab/test/arrow/io/feather/tRoundTrip.m @@ -31,27 +31,27 @@ function addFeatherFunctionsToMATLABPath(testCase) methods(Test) function Basic(testCase) import matlab.unittest.fixtures.TemporaryFolderFixture - + import arrow.internal.io.feather.* + fixture = testCase.applyFixture(TemporaryFolderFixture); filename = fullfile(fixture.Folder, "temp.feather"); DoubleVar = [10; 20; 30; 40]; SingleVar = single([10; 15; 20; 25]); - tWrite = table(DoubleVar, SingleVar); - - featherwrite(tWrite, filename); - tRead = featherread(filename); - testCase.verifyEqual(tWrite, tRead); + + tableWrite = table(DoubleVar, SingleVar); + recordBatchWrite = arrow.recordbatch(tableWrite); + + writer = Writer(filename); + writer.write(recordBatchWrite); + + reader = arrow.internal.io.feather.Reader(filename); + recordBatchRead = reader.read(); + + tableRead = table(recordBatchRead); + + testCase.verifyEqual(tableWrite, tableRead); end end -end - -function featherwrite(T, filename) - writer = arrow.internal.io.feather.Writer(filename); - writer.write(T); -end -function T = featherread(filename) - reader = arrow.internal.io.feather.Reader(filename); - T = reader.read(); end \ No newline at end of file