Skip to content

Commit

Permalink
ARROW-1031: [GLib] Support pretty print
Browse files Browse the repository at this point in the history
Author: Kouhei Sutou <kou@clear-code.com>

Closes apache#688 from kou/glib-support-pretty-print and squashes the following commits:

815f87f [Kouhei Sutou] [GLib] Support pretty print
  • Loading branch information
kou authored and jeffknupp committed Jun 3, 2017
1 parent 7a7e85d commit f9fe2fd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
27 changes: 27 additions & 0 deletions c_glib/arrow-glib/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
#include <arrow-glib/array.hpp>
#include <arrow-glib/buffer.hpp>
#include <arrow-glib/data-type.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/type.hpp>

#include <iostream>
#include <sstream>

G_BEGIN_DECLS

Expand Down Expand Up @@ -395,6 +397,31 @@ garrow_array_slice(GArrowArray *array,
return garrow_array_new_raw(&arrow_sub_array);
}

/**
* garrow_array_to_string:
* @array: A #GArrowArray.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): The formatted array content or %NULL on error.
*
* The returned string should be freed when with g_free() when no
* longer needed.
*
* Since: 0.4.0
*/
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;
}
}


G_DEFINE_TYPE(GArrowNullArray, \
garrow_null_array, \
Expand Down
2 changes: 2 additions & 0 deletions c_glib/arrow-glib/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ GArrowType garrow_array_get_value_type(GArrowArray *array);
GArrowArray *garrow_array_slice (GArrowArray *array,
gint64 offset,
gint64 length);
gchar *garrow_array_to_string (GArrowArray *array,
GError **error);

#define GARROW_TYPE_NULL_ARRAY \
(garrow_null_array_get_type())
Expand Down
29 changes: 29 additions & 0 deletions c_glib/arrow-glib/record-batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#endif

#include <arrow-glib/array.hpp>
#include <arrow-glib/error.hpp>
#include <arrow-glib/record-batch.hpp>
#include <arrow-glib/schema.hpp>

#include <sstream>

G_BEGIN_DECLS

/**
Expand Down Expand Up @@ -286,6 +289,32 @@ garrow_record_batch_slice(GArrowRecordBatch *record_batch,
return garrow_record_batch_new_raw(&arrow_sub_record_batch);
}

/**
* garrow_record_batch_to_string:
* @record_batch: A #GArrowRecordBatch.
* @error: (nullable): Return location for a #GError or %NULL.
*
* Returns: (nullable): The formatted record batch content or %NULL on error.
*
* The returned string should be freed when with g_free() when no
* longer needed.
*
* Since: 0.4.0
*/
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;
}
}


G_END_DECLS

GArrowRecordBatch *
Expand Down
3 changes: 3 additions & 0 deletions c_glib/arrow-glib/record-batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ GArrowRecordBatch *garrow_record_batch_slice (GArrowRecordBatch *record_batc
gint64 offset,
gint64 length);

gchar *garrow_record_batch_to_string (GArrowRecordBatch *record_batch,
GError **error);

G_END_DECLS
2 changes: 2 additions & 0 deletions c_glib/arrow-glib/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,6 @@ GArrowTable *garrow_table_remove_column (GArrowTable *table,
guint i,
GError **error);

gchar *garrow_table_to_string (GArrowTable *table);

G_END_DECLS
5 changes: 5 additions & 0 deletions c_glib/test/test-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,9 @@ def test_slice
assert_equal([false, true],
sub_array.length.times.collect {|i| sub_array.get_value(i)})
end

def test_to_s
assert_equal("[true, false, true]",
build_boolean_array([true, false, true]).to_s)
end
end
7 changes: 7 additions & 0 deletions c_glib/test/test-record-batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,12 @@ def test_slice
assert_equal([false, true],
sub_visible_values)
end

def test_to_s
assert_equal(<<-PRETTY_PRINT, @record_batch.to_s)
visible: [true, false, true, false, true, false]
valid: [false, true, false, true, false]
PRETTY_PRINT
end
end
end

0 comments on commit f9fe2fd

Please sign in to comment.