Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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