diff --git a/c_glib/arrow-glib/array.cpp b/c_glib/arrow-glib/array.cpp index 8bc6ea95d6a..e9a6a494c0f 100644 --- a/c_glib/arrow-glib/array.cpp +++ b/c_glib/arrow-glib/array.cpp @@ -24,9 +24,11 @@ #include #include #include +#include #include #include +#include G_BEGIN_DECLS @@ -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, \ diff --git a/c_glib/arrow-glib/array.h b/c_glib/arrow-glib/array.h index c4efeafd640..d32157b74cb 100644 --- a/c_glib/arrow-glib/array.h +++ b/c_glib/arrow-glib/array.h @@ -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()) diff --git a/c_glib/arrow-glib/record-batch.cpp b/c_glib/arrow-glib/record-batch.cpp index 3eed1a097c9..cd030de526f 100644 --- a/c_glib/arrow-glib/record-batch.cpp +++ b/c_glib/arrow-glib/record-batch.cpp @@ -22,9 +22,12 @@ #endif #include +#include #include #include +#include + G_BEGIN_DECLS /** @@ -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 * diff --git a/c_glib/arrow-glib/record-batch.h b/c_glib/arrow-glib/record-batch.h index 61e8f3d42b1..021f894f3f5 100644 --- a/c_glib/arrow-glib/record-batch.h +++ b/c_glib/arrow-glib/record-batch.h @@ -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 diff --git a/c_glib/arrow-glib/table.h b/c_glib/arrow-glib/table.h index 9e21669cd11..7f83872163f 100644 --- a/c_glib/arrow-glib/table.h +++ b/c_glib/arrow-glib/table.h @@ -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 diff --git a/c_glib/test/test-array.rb b/c_glib/test/test-array.rb index ca02fa283b0..bd0c7d6ecda 100644 --- a/c_glib/test/test-array.rb +++ b/c_glib/test/test-array.rb @@ -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 diff --git a/c_glib/test/test-record-batch.rb b/c_glib/test/test-record-batch.rb index 048f6deb724..9fd34b7d45c 100644 --- a/c_glib/test/test-record-batch.rb +++ b/c_glib/test/test-record-batch.rb @@ -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