Skip to content

Commit 118ca35

Browse files
authored
GH-45713: [GLib] Add garrow_chunked_array_(import|export)() (#46876)
### Rationale for this change The GLib bindings were missing C data interface functions for ChunkedArray import|export operations. ### What changes are included in this PR? This PR added the follwoings. - Add garrow_chunked_array_import() - Add garrow_chunked_array_export() - Add round-trip test with boolean chunked array data ### Are these changes tested? Yes. ### Are there any user-facing changes? No. * GitHub Issue: #45713 Authored-by: otegami <a.s.takuya1026@gmail.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 3dd8b66 commit 118ca35

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

c_glib/arrow-glib/chunked-array.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <arrow-glib/type.hpp>
2424
#include <arrow-glib/error.hpp>
2525

26+
#include <arrow/c/bridge.h>
27+
2628
#include <sstream>
2729

2830
G_BEGIN_DECLS
@@ -406,6 +408,56 @@ garrow_chunked_array_combine(GArrowChunkedArray *chunked_array, GError **error)
406408
}
407409
}
408410

411+
/**
412+
* garrow_chunked_array_import:
413+
* @c_abi_array_stream: (not nullable): A `struct ArrowArrayStream *`.
414+
* @error: (nullable): Return location for a #GError or %NULL.
415+
*
416+
* Returns: (transfer full) (nullable): An imported chunked array on success,
417+
* %NULL on error.
418+
*
419+
* Since: 21.0.0
420+
*/
421+
GArrowChunkedArray *
422+
garrow_chunked_array_import(gpointer c_abi_array_stream, GError **error)
423+
{
424+
auto arrow_chunked_array_result =
425+
arrow::ImportChunkedArray(static_cast<struct ArrowArrayStream *>(c_abi_array_stream));
426+
if (garrow::check(error, arrow_chunked_array_result, "[chunked-array][import]")) {
427+
return garrow_chunked_array_new_raw(&(*arrow_chunked_array_result));
428+
} else {
429+
return NULL;
430+
}
431+
}
432+
433+
/**
434+
* garrow_chunked_array_export:
435+
* @chunked_array: A #GArrowChunkedArray to be exported.
436+
* @error: (nullable): Return location for a #GError or %NULL.
437+
*
438+
* Returns: (transfer full) (nullable): An exported chunked array as
439+
* `struct ArrowArrayStream *` on success, %NULL on error.
440+
* It should be freed with the `ArrowArrayStream::release` callback then
441+
* g_free() when no longer needed.
442+
*
443+
* Since: 21.0.0
444+
*/
445+
gpointer
446+
garrow_chunked_array_export(GArrowChunkedArray *chunked_array, GError **error)
447+
{
448+
const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
449+
auto c_abi_array_stream = g_new(struct ArrowArrayStream, 1);
450+
auto status =
451+
arrow::ExportChunkedArray(arrow_chunked_array,
452+
static_cast<struct ArrowArrayStream *>(c_abi_array_stream));
453+
if (garrow::check(error, status, "[chunked-array][export]")) {
454+
return c_abi_array_stream;
455+
} else {
456+
g_free(c_abi_array_stream);
457+
return NULL;
458+
}
459+
}
460+
409461
G_END_DECLS
410462

411463
GArrowChunkedArray *

c_glib/arrow-glib/chunked-array.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,12 @@ GARROW_AVAILABLE_IN_4_0
8383
GArrowArray *
8484
garrow_chunked_array_combine(GArrowChunkedArray *chunked_array, GError **error);
8585

86+
GARROW_AVAILABLE_IN_21_0
87+
GArrowChunkedArray *
88+
garrow_chunked_array_import(gpointer c_abi_array_stream, GError **error);
89+
90+
GARROW_AVAILABLE_IN_21_0
91+
gpointer
92+
garrow_chunked_array_export(GArrowChunkedArray *chunked_array, GError **error);
93+
8694
G_END_DECLS

c_glib/test/test-chunked-array.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,15 @@ def test_combine
144144
assert_equal(build_boolean_array([true, false, nil]),
145145
chunked_array.combine)
146146
end
147+
148+
def test_export_import
149+
chunks = [
150+
build_boolean_array([true, false, true]),
151+
build_boolean_array([false, nil]),
152+
]
153+
original_chunked_array = Arrow::ChunkedArray.new(chunks)
154+
c_abi_array_stream = original_chunked_array.export
155+
assert_equal(original_chunked_array,
156+
Arrow::ChunkedArray.import(c_abi_array_stream))
157+
end
147158
end

0 commit comments

Comments
 (0)