You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ko...@apache.org on 2018/11/08 06:03:09 UTC

[arrow] branch master updated: ARROW-3108: [C++] arrow::PrettyPrint for Table instances

This is an automated email from the ASF dual-hosted git repository.

kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new d194241  ARROW-3108: [C++] arrow::PrettyPrint for Table instances
d194241 is described below

commit d194241f0754692a95563b2d9cda7be9006c6e8c
Author: Korn, Uwe <Uw...@blue-yonder.com>
AuthorDate: Thu Nov 8 15:02:46 2018 +0900

    ARROW-3108: [C++] arrow::PrettyPrint for Table instances
    
    Author: Korn, Uwe <Uw...@blue-yonder.com>
    
    Closes #2857 from xhochy/ARROW-3108 and squashes the following commits:
    
    27d51f15 <Korn, Uwe> ARROW-3108:  arrow::PrettyPrint for Table instances
---
 cpp/src/arrow/pretty_print-test.cc | 70 ++++++++++++++++++++++++++++++++++++++
 cpp/src/arrow/pretty_print.cc      | 30 ++++++++++++++++
 cpp/src/arrow/pretty_print.h       | 14 +++++++-
 3 files changed, 113 insertions(+), 1 deletion(-)

diff --git a/cpp/src/arrow/pretty_print-test.cc b/cpp/src/arrow/pretty_print-test.cc
index eebb9a9..482bc43 100644
--- a/cpp/src/arrow/pretty_print-test.cc
+++ b/cpp/src/arrow/pretty_print-test.cc
@@ -431,6 +431,76 @@ TEST_F(TestPrettyPrint, ChunkedArrayPrimitiveType) {
   CheckStream(chunked_array_2, {0}, expected_2);
 }
 
+TEST_F(TestPrettyPrint, ColumnPrimitiveType) {
+  std::vector<bool> is_valid = {true, true, false, true, false};
+  std::vector<int32_t> values = {0, 1, 2, 3, 4};
+  std::shared_ptr<Array> array;
+  ArrayFromVector<Int32Type, int32_t>(is_valid, values, &array);
+  std::shared_ptr<Field> int_field = field("column", int32());
+  Column column(int_field, ArrayVector({array}));
+
+  static const char* expected = R"expected(column: int32
+[
+  [
+    0,
+    1,
+    null,
+    3,
+    null
+  ]
+])expected";
+  CheckStream(column, {0}, expected);
+
+  Column column_2(int_field, {array, array});
+
+  static const char* expected_2 = R"expected(column: int32
+[
+  [
+    0,
+    1,
+    null,
+    3,
+    null
+  ],
+  [
+    0,
+    1,
+    null,
+    3,
+    null
+  ]
+])expected";
+
+  CheckStream(column_2, {0}, expected_2);
+}
+
+TEST_F(TestPrettyPrint, TablePrimitive) {
+  std::vector<bool> is_valid = {true, true, false, true, false};
+  std::vector<int32_t> values = {0, 1, 2, 3, 4};
+  std::shared_ptr<Array> array;
+  ArrayFromVector<Int32Type, int32_t>(is_valid, values, &array);
+  std::shared_ptr<Field> int_field = field("column", int32());
+  std::shared_ptr<Column> column =
+      std::make_shared<Column>(int_field, ArrayVector({array}));
+  std::shared_ptr<Schema> table_schema = schema({int_field});
+  std::shared_ptr<Table> table = Table::Make(table_schema, {column});
+
+  static const char* expected = R"expected(column: int32
+----
+column:
+  [
+    [
+      0,
+      1,
+      null,
+      3,
+      null
+    ]
+  ]
+)expected";
+  CheckStream(*table, {0}, expected);
+}
+
 TEST_F(TestPrettyPrint, SchemaWithDictionary) {
   std::vector<bool> is_valid = {true, true, false, true, true, true};
 
diff --git a/cpp/src/arrow/pretty_print.cc b/cpp/src/arrow/pretty_print.cc
index 20a9d41..f4aeb7e 100644
--- a/cpp/src/arrow/pretty_print.cc
+++ b/cpp/src/arrow/pretty_print.cc
@@ -409,6 +409,16 @@ Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& op
   return Status::OK();
 }
 
+Status PrettyPrint(const Column& column, const PrettyPrintOptions& options,
+                   std::ostream* sink) {
+  for (int i = 0; i < options.indent; ++i) {
+    (*sink) << " ";
+  }
+  (*sink) << column.field()->ToString() << "\n";
+
+  return PrettyPrint(*column.data(), options, sink);
+}
+
 Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& options,
                    std::string* result) {
   std::ostringstream sink;
@@ -428,6 +438,26 @@ Status PrettyPrint(const RecordBatch& batch, int indent, std::ostream* sink) {
   return Status::OK();
 }
 
+Status PrettyPrint(const Table& table, const PrettyPrintOptions& options,
+                   std::ostream* sink) {
+  RETURN_NOT_OK(PrettyPrint(*table.schema(), options, sink));
+  (*sink) << "\n";
+  (*sink) << "----\n";
+
+  PrettyPrintOptions column_options = options;
+  column_options.indent += 2;
+  for (int i = 0; i < table.num_columns(); ++i) {
+    for (int j = 0; j < options.indent; ++j) {
+      (*sink) << " ";
+    }
+    (*sink) << table.schema()->field(i)->name() << ":\n";
+    RETURN_NOT_OK(PrettyPrint(*table.column(i)->data(), column_options, sink));
+    (*sink) << "\n";
+  }
+  (*sink) << std::flush;
+  return Status::OK();
+}
+
 Status DebugPrint(const Array& arr, int indent) {
   return PrettyPrint(arr, indent, &std::cout);
 }
diff --git a/cpp/src/arrow/pretty_print.h b/cpp/src/arrow/pretty_print.h
index 45c1431..fde6c29 100644
--- a/cpp/src/arrow/pretty_print.h
+++ b/cpp/src/arrow/pretty_print.h
@@ -59,14 +59,20 @@ struct PrettyPrintOptions {
 ARROW_EXPORT
 Status PrettyPrint(const RecordBatch& batch, int indent, std::ostream* sink);
 
+/// \brief Print human-readable representation of Table
+ARROW_EXPORT
+Status PrettyPrint(const Table& table, const PrettyPrintOptions& options,
+                   std::ostream* sink);
+
 /// \brief Print human-readable representation of Array
 ARROW_EXPORT
 Status PrettyPrint(const Array& arr, int indent, std::ostream* sink);
-/// \brief Print human-readable representation of Array
 
+/// \brief Print human-readable representation of Array
 ARROW_EXPORT
 Status PrettyPrint(const Array& arr, const PrettyPrintOptions& options,
                    std::ostream* sink);
+
 /// \brief Print human-readable representation of Array
 ARROW_EXPORT
 Status PrettyPrint(const Array& arr, const PrettyPrintOptions& options,
@@ -76,11 +82,17 @@ Status PrettyPrint(const Array& arr, const PrettyPrintOptions& options,
 ARROW_EXPORT
 Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& options,
                    std::ostream* sink);
+
 /// \brief Print human-readable representation of ChunkedArray
 ARROW_EXPORT
 Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& options,
                    std::string* result);
 
+/// \brief Print human-readable representation of Column
+ARROW_EXPORT
+Status PrettyPrint(const Column& column, const PrettyPrintOptions& options,
+                   std::ostream* sink);
+
 ARROW_EXPORT
 Status PrettyPrint(const Schema& schema, const PrettyPrintOptions& options,
                    std::ostream* sink);