Skip to content

Commit

Permalink
ARROW-8403: [C++] Add ToString() to ChunkedArray, Table and RecordBatch
Browse files Browse the repository at this point in the history
Closes #6900 from kou/cpp-to-string

Authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
kou committed Apr 13, 2020
1 parent c6f194a commit d0f56ed
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 37 deletions.
8 changes: 1 addition & 7 deletions c_glib/arrow-glib/basic-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,7 @@ gchar *
garrow_array_to_string(GArrowArray *array, GError **error)
{
const auto arrow_array = garrow_array_get_raw(array);
std::stringstream sink;
auto status = arrow::PrettyPrint(*arrow_array, 0, &sink);
if (garrow_error_check(error, status, "[array][to-string]")) {
return g_strdup(sink.str().c_str());
} else {
return NULL;
}
return g_strdup(arrow_array->ToString().c_str());
}

/**
Expand Down
8 changes: 1 addition & 7 deletions c_glib/arrow-glib/chunked-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,7 @@ gchar *
garrow_chunked_array_to_string(GArrowChunkedArray *chunked_array, GError **error)
{
const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
std::stringstream sink;
auto status = arrow::PrettyPrint(*arrow_chunked_array, 0, &sink);
if (garrow_error_check(error, status, "[chunked-array][to-string]")) {
return g_strdup(sink.str().c_str());
} else {
return NULL;
}
return g_strdup(arrow_chunked_array->ToString().c_str());
}

G_END_DECLS
Expand Down
10 changes: 4 additions & 6 deletions c_glib/arrow-glib/decimal128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,8 @@ garrow_decimal128_greater_than_or_equal(GArrowDecimal128 *decimal,
gchar *
garrow_decimal128_to_string_scale(GArrowDecimal128 *decimal, gint32 scale)
{
auto arrow_decimal = garrow_decimal128_get_raw(decimal);
auto string = arrow_decimal->ToString(scale);
return g_strndup(string.data(), string.size());
const auto arrow_decimal = garrow_decimal128_get_raw(decimal);
return g_strdup(arrow_decimal->ToString(scale).c_str());
}

/**
Expand All @@ -284,9 +283,8 @@ garrow_decimal128_to_string_scale(GArrowDecimal128 *decimal, gint32 scale)
gchar *
garrow_decimal128_to_string(GArrowDecimal128 *decimal)
{
auto arrow_decimal = garrow_decimal128_get_raw(decimal);
auto string = arrow_decimal->ToIntegerString();
return g_strndup(string.data(), string.size());
const auto arrow_decimal = garrow_decimal128_get_raw(decimal);
return g_strdup(arrow_decimal->ToIntegerString().c_str());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions c_glib/arrow-glib/file-system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ gchar *
garrow_file_info_to_string(GArrowFileInfo *file_info)
{
const auto arrow_file_info = garrow_file_info_get_raw(file_info);
auto string = arrow_file_info->ToString();
return g_strndup(string.data(), string.size());
return g_strdup(arrow_file_info->ToString().c_str());
}

/* arrow::fs::FileSelector */
Expand Down
8 changes: 1 addition & 7 deletions c_glib/arrow-glib/record-batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,7 @@ gchar *
garrow_record_batch_to_string(GArrowRecordBatch *record_batch, GError **error)
{
const auto arrow_record_batch = garrow_record_batch_get_raw(record_batch);
std::stringstream sink;
auto status = arrow::PrettyPrint(*arrow_record_batch, 0, &sink);
if (garrow_error_check(error, status, "[record-batch][to-string]")) {
return g_strdup(sink.str().c_str());
} else {
return NULL;
}
return g_strdup(arrow_record_batch->ToString().c_str());
}

/**
Expand Down
8 changes: 1 addition & 7 deletions c_glib/arrow-glib/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,7 @@ gchar *
garrow_table_to_string(GArrowTable *table, GError **error)
{
const auto arrow_table = garrow_table_get_raw(table);
std::stringstream sink;
auto status = arrow::PrettyPrint(*arrow_table, 0, &sink);
if (garrow_error_check(error, status, "[table][to-string]")) {
return g_strdup(sink.str().c_str());
} else {
return NULL;
}
return g_strdup(arrow_table->ToString().c_str());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion c_glib/test/test-record-batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def setup
Arrow::Field.new("visible", Arrow::BooleanDataType.new),
Arrow::Field.new("valid", Arrow::BooleanDataType.new),
]
@schema = Arrow::Schema.new(fields)
@schema = Arrow::Schema.new(@fields)
@columns = [
build_boolean_array([true, false, true, false, true]),
build_boolean_array([false, true, false, true, false]),
Expand Down
8 changes: 8 additions & 0 deletions cpp/src/arrow/record_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
#include <atomic>
#include <cstdlib>
#include <memory>
#include <sstream>
#include <string>
#include <utility>

#include "arrow/array.h"
#include "arrow/array/validate.h"
#include "arrow/pretty_print.h"
#include "arrow/status.h"
#include "arrow/table.h"
#include "arrow/type.h"
Expand Down Expand Up @@ -246,6 +248,12 @@ std::shared_ptr<RecordBatch> RecordBatch::Slice(int64_t offset) const {
return Slice(offset, this->num_rows() - offset);
}

std::string RecordBatch::ToString() const {
std::stringstream ss;
ARROW_CHECK_OK(PrettyPrint(*this, 0, &ss));
return ss.str();
}

Status RecordBatch::Validate() const {
for (int i = 0; i < num_columns(); ++i) {
const auto& array = *this->column(i);
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/arrow/record_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class ARROW_EXPORT RecordBatch {
/// \return new record batch
virtual std::shared_ptr<RecordBatch> Slice(int64_t offset, int64_t length) const = 0;

/// \return PrettyPrint representation suitable for debugging
std::string ToString() const;

/// \brief Perform cheap validation checks to determine obvious inconsistencies
/// within the record batch's schema and internal data.
///
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "arrow/array.h"
#include "arrow/array/concatenate.h"
#include "arrow/array/validate.h"
#include "arrow/pretty_print.h"
#include "arrow/record_batch.h"
#include "arrow/status.h"
#include "arrow/type.h"
Expand Down Expand Up @@ -176,6 +177,12 @@ Status ChunkedArray::View(const std::shared_ptr<DataType>& type,
return View(type).Value(out);
}

std::string ChunkedArray::ToString() const {
std::stringstream ss;
ARROW_CHECK_OK(PrettyPrint(*this, 0, &ss));
return ss.str();
}

Status ChunkedArray::Validate() const {
if (chunks_.size() == 0) {
return Status::OK();
Expand Down Expand Up @@ -599,6 +606,12 @@ Status Table::Flatten(MemoryPool* pool, std::shared_ptr<Table>* out) const {
return Flatten(pool).Value(out);
}

std::string Table::ToString() const {
std::stringstream ss;
ARROW_CHECK_OK(PrettyPrint(*this, 0, &ss));
return ss.str();
}

Result<std::shared_ptr<Table>> ConcatenateTables(
const std::vector<std::shared_ptr<Table>>& tables,
const ConcatenateTablesOptions options, MemoryPool* memory_pool) {
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class ARROW_EXPORT ChunkedArray {
/// \brief Determine if two chunked arrays are equal.
bool Equals(const std::shared_ptr<ChunkedArray>& other) const;

/// \return PrettyPrint representation suitable for debugging
std::string ToString() const;

/// \brief Perform cheap validation checks to determine obvious inconsistencies
/// within the chunk array's internal data.
///
Expand Down Expand Up @@ -366,6 +369,9 @@ class ARROW_EXPORT Table {
ARROW_DEPRECATED("Use Result-returning version")
Status Flatten(MemoryPool* pool, std::shared_ptr<Table>* out) const;

/// \return PrettyPrint representation suitable for debugging
std::string ToString() const;

/// \brief Perform cheap validation checks to determine obvious inconsistencies
/// within the table's schema and internal data.
///
Expand Down

0 comments on commit d0f56ed

Please sign in to comment.