kevingurney commented on code in PR #38463:
URL: https://github.com/apache/arrow/pull/38463#discussion_r1372244335


##########
matlab/src/cpp/arrow/matlab/tabular/print_row.h:
##########
@@ -0,0 +1,73 @@
+// 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/pretty_print.h"
+
+#include <sstream>
+
+namespace arrow::matlab::tabular {
+
+    namespace {
+        arrow::PrettyPrintOptions make_pretty_print_options() {
+            auto opts = arrow::PrettyPrintOptions::Defaults();
+            opts.skip_new_lines = true;
+            opts.array_delimiters.open = "";
+            opts.array_delimiters.close = "";
+            opts.chunked_array_delimiters.open = "";
+            opts.chunked_array_delimiters.close = "";
+            return opts;
+        }
+    }
+
+    template <typename TabularType>
+    arrow::Result<std::string> print_row(const std::shared_ptr<TabularType>& 
tabular_object, const int64_t matlab_row_index) {
+        std::stringstream ss;
+        const int64_t row_index = matlab_row_index - 1;
+        if (row_index >= tabular_object->num_rows() || row_index < 0) {
+            ss << "Invalid Row Index: " << matlab_row_index;
+            return arrow::Status::Invalid(ss.str());
+        }
+
+        const auto opts = make_pretty_print_options();
+        const auto num_columns = tabular_object->num_columns();
+        const auto& columns = tabular_object->columns();
+
+        for (int32_t i = 0; i < num_columns; ++i) {
+            const auto& column = columns[i];
+            const auto type_id = column->type()->id();
+            if (arrow::is_primitive(type_id) || arrow::is_string(type_id)) {
+                auto slice = column->Slice(row_index, 1);
+                ARROW_RETURN_NOT_OK(arrow::PrettyPrint(*slice, opts, &ss));
+            } else if (type_id == arrow::Type::type::STRUCT) {
+                ss << "<Struct>";

Review Comment:
   Could you add a comment here exlpaining that these are "placeholders", since 
we don't have a straightforward of representing List/Struct for a horizontal 
display?



##########
matlab/src/cpp/arrow/matlab/tabular/print_row.h:
##########
@@ -0,0 +1,73 @@
+// 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/pretty_print.h"
+
+#include <sstream>
+
+namespace arrow::matlab::tabular {
+
+    namespace {
+        arrow::PrettyPrintOptions make_pretty_print_options() {
+            auto opts = arrow::PrettyPrintOptions::Defaults();
+            opts.skip_new_lines = true;
+            opts.array_delimiters.open = "";
+            opts.array_delimiters.close = "";
+            opts.chunked_array_delimiters.open = "";
+            opts.chunked_array_delimiters.close = "";
+            return opts;
+        }
+    }
+
+    template <typename TabularType>
+    arrow::Result<std::string> print_row(const std::shared_ptr<TabularType>& 
tabular_object, const int64_t matlab_row_index) {

Review Comment:
   Just my personal opinion. When I think of "Printing", I usually think of 
printing to screen. Maybe we could call this `get_row_as_string` or 
`row_as_string` instead? I don't think it's too important which name we go 
with, but just wanted to suggest some alternate names.



##########
matlab/test/arrow/tabular/tTabularInternal.m:
##########
@@ -0,0 +1,110 @@
+%TTABULARINTERNAL Unit tests for internal functionality of tabular types.
+
+% 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 tTabularInternal < matlab.unittest.TestCase
+
+    properties(TestParameter)
+        TabularObjectWithAllTypes
+
+        TabularObjectWithOneColumn
+
+        TabularObjectWithThreeRows
+    end
+
+    methods (TestParameterDefinition, Static)
+        function TabularObjectWithAllTypes = 
initializeTabularObjectWithAllTypes()
+            arrays = 
arrow.internal.test.tabular.createAllSupportedArrayTypes(NumRows=1);
+            arrowTable = arrow.tabular.Table.fromArrays(arrays{:});
+            arrowRecordBatch = arrow.tabular.Table.fromArrays(arrays{:});
+            TabularObjectWithAllTypes = struct(Table=arrowTable, ...
+                RecordBatch=arrowRecordBatch);
+        end
+
+        function TabularObjectWithOneColumn = 
initializeTabularObjectWithOneColumn()
+            t = table((1:3)');
+            arrowTable = arrow.table(t);
+            arrowRecordBatch = arrow.recordBatch(t);
+            TabularObjectWithOneColumn = struct(Table=arrowTable, ...
+                RecordBatch=arrowRecordBatch);
+        end
+
+        function TabularObjectWithThreeRows = 
initializeTabularObjectWithThreeRows()
+            t = table((1:3)', ["A"; "B"; "C"]);
+            arrowTable = arrow.table(t);
+            arrowRecordBatch = arrow.recordBatch(t);
+            TabularObjectWithThreeRows = struct(Table=arrowTable, ...
+                RecordBatch=arrowRecordBatch);
+        end
+    end
+
+    methods (Test)
+        function RowWithAllTypes(testCase, TabularObjectWithAllTypes)
+            % Verify getRowString successfully returns the expected string
+            % when called on a Table/RecordBatch that contains all
+            % supported array types.
+            proxy = TabularObjectWithAllTypes.Proxy;
+            columnStrs = ["false", "2024-02-23", "2023-08-24", "78", "38", ...
+                          "24", "48", "89", "102", "<List>", """107""", 
"<Struct>", ...
+                          "00:03:44", "00:00:07.000000", "2024-02-10 
00:00:00.000000", ...
+                          "107", "143", "36", "51"];
+            expectedString = strjoin(columnStrs, " | ");
+            actualString = proxy.getRowString(struct(Index=int64(1)));
+            testCase.verifyEqual(actualString, expectedString);
+        end
+
+        function RowWithOneColumn(testCase, TabularObjectWithOneColumn)
+            % Verify getRowString successfully returns the expected string
+            % when called on a Table/RecordBatch with one column.
+            proxy = TabularObjectWithOneColumn.Proxy;
+            expectedString = "1";
+            actualString = proxy.getRowString(struct(Index=int64(1)));
+            testCase.verifyEqual(actualString, expectedString);
+        end
+
+        function RowIndex(testCase, TabularObjectWithThreeRows)
+            % Verify getRowString returns the expected string when provided

Review Comment:
   I think there is a typo in this comment.



##########
matlab/src/cpp/arrow/matlab/tabular/print_row.h:
##########
@@ -0,0 +1,73 @@
+// 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/pretty_print.h"
+
+#include <sstream>
+
+namespace arrow::matlab::tabular {
+
+    namespace {
+        arrow::PrettyPrintOptions make_pretty_print_options() {
+            auto opts = arrow::PrettyPrintOptions::Defaults();
+            opts.skip_new_lines = true;
+            opts.array_delimiters.open = "";
+            opts.array_delimiters.close = "";
+            opts.chunked_array_delimiters.open = "";
+            opts.chunked_array_delimiters.close = "";
+            return opts;
+        }
+    }
+
+    template <typename TabularType>
+    arrow::Result<std::string> print_row(const std::shared_ptr<TabularType>& 
tabular_object, const int64_t matlab_row_index) {
+        std::stringstream ss;
+        const int64_t row_index = matlab_row_index - 1;
+        if (row_index >= tabular_object->num_rows() || row_index < 0) {
+            ss << "Invalid Row Index: " << matlab_row_index;
+            return arrow::Status::Invalid(ss.str());
+        }
+
+        const auto opts = make_pretty_print_options();
+        const auto num_columns = tabular_object->num_columns();
+        const auto& columns = tabular_object->columns();
+
+        for (int32_t i = 0; i < num_columns; ++i) {
+            const auto& column = columns[i];
+            const auto type_id = column->type()->id();
+            if (arrow::is_primitive(type_id) || arrow::is_string(type_id)) {
+                auto slice = column->Slice(row_index, 1);
+                ARROW_RETURN_NOT_OK(arrow::PrettyPrint(*slice, opts, &ss));
+            } else if (type_id == arrow::Type::type::STRUCT) {
+                ss << "<Struct>";
+            } else if (type_id == arrow::Type::type::LIST) {
+                ss << "<List>";
+            } else {
+                return arrow::Status::NotImplemented("Invalid Datatype: " + 
column->type()->ToString());

Review Comment:
   ```suggestion
                   return arrow::Status::NotImplemented("Datatype " + 
column->type()->ToString() + "is not currently supported for display.");
   ```



##########
matlab/src/cpp/arrow/matlab/tabular/proxy/record_batch.cc:
##########
@@ -58,6 +59,7 @@ namespace arrow::matlab::tabular::proxy {
         REGISTER_METHOD(RecordBatch, getColumnByIndex);
         REGISTER_METHOD(RecordBatch, getColumnByName);
         REGISTER_METHOD(RecordBatch, getSchema);
+        REGISTER_METHOD(RecordBatch, getRowString);

Review Comment:
   ```suggestion
           REGISTER_METHOD(RecordBatch, getRowAsString);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to