This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new abe77b7acb GH-54688: [GLib][Ruby] Keep chunks of ChunkedArray alive
(#55376)
abe77b7acb is described below
commit abe77b7acb248ccb49e05919d596c3f49cae9d29
Author: Sutou Kouhei <[email protected]>
AuthorDate: Fri Nov 6 10:13:42 2026 +0900
GH-54688: [GLib][Ruby] Keep chunks of ChunkedArray alive (#55376)
### Rationale for this change
Arrays referred by `Arrow::ChunkedArray` aren't referred. So These arrays
may be GC-ed.
### What changes are included in this PR?
* `GArrowChunkedArray`: Refer passed chunks.
* `garrow_chunked_array_peek_chunk()`: New function that returns a chunk if
it exists.
* `Arrow::ChunkedArray`: Mark kept chunks.
### Are these changes tested?
Yes.
### Are there any user-facing changes?
Yes.
### Was AI used for this PR?
In accordance to the [AI generation
guidelines](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
please disclose below whether and how AI was used in this PR.
**PR code and description written by:**
- [x] Human
- [x] AI
**Reviewed before submission by:**
- [x] Human
- [x] AI
- [ ] Not reviewed
* GitHub Issue: #54688
Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
---
c_glib/arrow-glib/chunked-array.cpp | 108 +++++++++++++++++++++++++++---
c_glib/arrow-glib/chunked-array.h | 4 ++
c_glib/arrow-glib/chunked-array.hpp | 6 ++
c_glib/test/test-chunked-array.rb | 57 ++++++++++++++++
ruby/red-arrow/ext/arrow/arrow.cpp | 15 +++++
ruby/red-arrow/test/test-chunked-array.rb | 13 ++++
6 files changed, 194 insertions(+), 9 deletions(-)
diff --git a/c_glib/arrow-glib/chunked-array.cpp
b/c_glib/arrow-glib/chunked-array.cpp
index 8870e71894..a96dbb7f24 100644
--- a/c_glib/arrow-glib/chunked-array.cpp
+++ b/c_glib/arrow-glib/chunked-array.cpp
@@ -41,11 +41,13 @@ struct GArrowChunkedArrayPrivate
{
std::shared_ptr<arrow::ChunkedArray> chunked_array;
GArrowDataType *data_type;
+ GPtrArray *chunks;
};
enum {
PROP_CHUNKED_ARRAY = 1,
PROP_DATA_TYPE,
+ PROP_CHUNKS,
};
G_DEFINE_TYPE_WITH_PRIVATE(GArrowChunkedArray, garrow_chunked_array,
G_TYPE_OBJECT)
@@ -54,6 +56,14 @@ G_DEFINE_TYPE_WITH_PRIVATE(GArrowChunkedArray,
garrow_chunked_array, G_TYPE_OBJE
static_cast<GArrowChunkedArrayPrivate *>(
\
garrow_chunked_array_get_instance_private(GARROW_CHUNKED_ARRAY(obj)))
+static void
+garrow_chunked_array_chunk_unref(gpointer chunk)
+{
+ if (chunk) {
+ g_object_unref(chunk);
+ }
+}
+
static void
garrow_chunked_array_dispose(GObject *object)
{
@@ -64,6 +74,11 @@ garrow_chunked_array_dispose(GObject *object)
priv->data_type = nullptr;
}
+ if (priv->chunks) {
+ g_ptr_array_unref(priv->chunks);
+ priv->chunks = nullptr;
+ }
+
G_OBJECT_CLASS(garrow_chunked_array_parent_class)->dispose(object);
}
@@ -93,6 +108,18 @@ garrow_chunked_array_set_property(GObject *object,
case PROP_DATA_TYPE:
priv->data_type = GARROW_DATA_TYPE(g_value_dup_object(value));
break;
+ case PROP_CHUNKS:
+ {
+ auto chunks = static_cast<GList *>(g_value_get_pointer(value));
+ if (chunks) {
+ priv->chunks =
+ g_ptr_array_new_full(g_list_length(chunks),
garrow_chunked_array_chunk_unref);
+ for (auto node = chunks; node; node = g_list_next(node)) {
+ g_ptr_array_add(priv->chunks, g_object_ref(node->data));
+ }
+ }
+ }
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
@@ -146,6 +173,13 @@ garrow_chunked_array_class_init(GArrowChunkedArrayClass
*klass)
GARROW_TYPE_DATA_TYPE,
static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property(gobject_class, PROP_DATA_TYPE, spec);
+
+ spec = g_param_spec_pointer(
+ "chunks",
+ "Chunks",
+ "The chunks of this chunked array",
+ static_cast<GParamFlags>(G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property(gobject_class, PROP_CHUNKS, spec);
}
/**
@@ -168,7 +202,7 @@ garrow_chunked_array_new(GList *chunks, GError **error)
auto arrow_chunked_array_result = arrow::ChunkedArray::Make(arrow_chunks);
if (garrow::check(error, arrow_chunked_array_result,
"[chunked-array][new]")) {
auto arrow_chunked_array = *arrow_chunked_array_result;
- return garrow_chunked_array_new_raw(&arrow_chunked_array);
+ return garrow_chunked_array_new_raw(&arrow_chunked_array, nullptr, chunks);
} else {
return nullptr;
}
@@ -315,32 +349,78 @@ garrow_chunked_array_get_n_chunks(GArrowChunkedArray
*chunked_array)
* @chunked_array: A #GArrowChunkedArray.
* @i: The index of the target chunk.
*
- * Returns: (transfer full): The i-th chunk of the chunked array.
+ * The returned chunk is cached. The same object is returned for the
+ * same @i.
+ *
+ * Returns: (transfer full) (nullable): The i-th chunk of the chunked
+ * array on success, %NULL on out of index.
*/
GArrowArray *
garrow_chunked_array_get_chunk(GArrowChunkedArray *chunked_array, guint i)
{
- const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
- auto arrow_chunk = arrow_chunked_array->chunk(i);
- return garrow_array_new_raw(&arrow_chunk);
+ auto priv = GARROW_CHUNKED_ARRAY_GET_PRIVATE(chunked_array);
+ const auto n_chunks = garrow_chunked_array_get_n_chunks(chunked_array);
+ if (i >= n_chunks) {
+ return nullptr;
+ }
+ if (!priv->chunks) {
+ priv->chunks = g_ptr_array_new_full(n_chunks,
garrow_chunked_array_chunk_unref);
+ g_ptr_array_set_size(priv->chunks, n_chunks);
+ }
+ auto chunk = static_cast<GArrowArray *>(g_ptr_array_index(priv->chunks, i));
+ if (!chunk) {
+ const auto arrow_chunked_array =
garrow_chunked_array_get_raw(chunked_array);
+ auto arrow_chunk = arrow_chunked_array->chunk(i);
+ chunk = garrow_array_new_raw(&arrow_chunk);
+ g_ptr_array_index(priv->chunks, i) = chunk;
+ }
+ return GARROW_ARRAY(g_object_ref(chunk));
+}
+
+/**
+ * garrow_chunked_array_peek_chunk:
+ * @chunked_array: A #GArrowChunkedArray.
+ * @i: The index of the target chunk.
+ *
+ * This returns the cached @i-th chunk. This never creates a new
+ * chunk object.
+ *
+ * Returns: (transfer none) (nullable): The @i-th chunk in the chunked
+ * array if it's already created, %NULL otherwise.
+ *
+ * Since: 27.0.0
+ */
+GArrowArray *
+garrow_chunked_array_peek_chunk(GArrowChunkedArray *chunked_array, guint i)
+{
+ auto priv = GARROW_CHUNKED_ARRAY_GET_PRIVATE(chunked_array);
+ if (!priv->chunks) {
+ return nullptr;
+ }
+ if (i >= priv->chunks->len) {
+ return nullptr;
+ }
+ return static_cast<GArrowArray *>(g_ptr_array_index(priv->chunks, i));
}
/**
* garrow_chunked_array_get_chunks:
* @chunked_array: A #GArrowChunkedArray.
*
+ * The returned chunks are cached. The same objects are returned for
+ * each call.
+ *
* Returns: (element-type GArrowArray) (transfer full):
* The chunks in the chunked array.
*/
GList *
garrow_chunked_array_get_chunks(GArrowChunkedArray *chunked_array)
{
- const auto arrow_chunked_array = garrow_chunked_array_get_raw(chunked_array);
+ const auto n_chunks = garrow_chunked_array_get_n_chunks(chunked_array);
GList *chunks = NULL;
- for (auto arrow_chunk : arrow_chunked_array->chunks()) {
- GArrowArray *chunk = garrow_array_new_raw(&arrow_chunk);
- chunks = g_list_prepend(chunks, chunk);
+ for (guint i = 0; i < n_chunks; ++i) {
+ chunks = g_list_prepend(chunks,
garrow_chunked_array_get_chunk(chunked_array, i));
}
return g_list_reverse(chunks);
@@ -469,12 +549,22 @@
garrow_chunked_array_new_raw(std::shared_ptr<arrow::ChunkedArray> *arrow_chunked
GArrowChunkedArray *
garrow_chunked_array_new_raw(std::shared_ptr<arrow::ChunkedArray>
*arrow_chunked_array,
GArrowDataType *data_type)
+{
+ return garrow_chunked_array_new_raw(arrow_chunked_array, data_type, nullptr);
+}
+
+GArrowChunkedArray *
+garrow_chunked_array_new_raw(std::shared_ptr<arrow::ChunkedArray>
*arrow_chunked_array,
+ GArrowDataType *data_type,
+ GList *chunks)
{
auto chunked_array =
GARROW_CHUNKED_ARRAY(g_object_new(GARROW_TYPE_CHUNKED_ARRAY,
"chunked-array",
arrow_chunked_array,
"data-type",
data_type,
+ "chunks",
+ chunks,
NULL));
return chunked_array;
}
diff --git a/c_glib/arrow-glib/chunked-array.h
b/c_glib/arrow-glib/chunked-array.h
index 27fedb6a4f..bb04d74131 100644
--- a/c_glib/arrow-glib/chunked-array.h
+++ b/c_glib/arrow-glib/chunked-array.h
@@ -65,6 +65,10 @@ GARROW_AVAILABLE_IN_ALL
GArrowArray *
garrow_chunked_array_get_chunk(GArrowChunkedArray *chunked_array, guint i);
+GARROW_AVAILABLE_IN_27_0
+GArrowArray *
+garrow_chunked_array_peek_chunk(GArrowChunkedArray *chunked_array, guint i);
+
GARROW_AVAILABLE_IN_ALL
GList *
garrow_chunked_array_get_chunks(GArrowChunkedArray *chunked_array);
diff --git a/c_glib/arrow-glib/chunked-array.hpp
b/c_glib/arrow-glib/chunked-array.hpp
index 674ef9606b..6c69fc0620 100644
--- a/c_glib/arrow-glib/chunked-array.hpp
+++ b/c_glib/arrow-glib/chunked-array.hpp
@@ -32,6 +32,12 @@ GArrowChunkedArray *
garrow_chunked_array_new_raw(std::shared_ptr<arrow::ChunkedArray>
*arrow_chunked_array,
GArrowDataType *data_type);
+GARROW_EXTERN
+GArrowChunkedArray *
+garrow_chunked_array_new_raw(std::shared_ptr<arrow::ChunkedArray>
*arrow_chunked_array,
+ GArrowDataType *data_type,
+ GList *chunks);
+
GARROW_EXTERN
std::shared_ptr<arrow::ChunkedArray>
garrow_chunked_array_get_raw(GArrowChunkedArray *chunked_array);
diff --git a/c_glib/test/test-chunked-array.rb
b/c_glib/test/test-chunked-array.rb
index 2d5dd5cd64..f6b30d59e4 100644
--- a/c_glib/test/test-chunked-array.rb
+++ b/c_glib/test/test-chunked-array.rb
@@ -92,6 +92,35 @@ class TestChunkedArray < Test::Unit::TestCase
assert_equal(2, chunked_array.get_chunk(0).length)
end
+ def test_chunk_out_of_index
+ chunks = [
+ build_boolean_array([true, false]),
+ build_boolean_array([false]),
+ ]
+ chunked_array = Arrow::ChunkedArray.new(chunks)
+ assert_nil(chunked_array.get_chunk(2))
+ end
+
+ def test_peek_chunk
+ chunks = [
+ build_boolean_array([true, false]),
+ build_boolean_array([false]),
+ ]
+ chunked_array = build_uncached_chunked_array(chunks)
+ assert_nil(chunked_array.peek_chunk(0))
+ chunk = chunked_array.get_chunk(0)
+ assert_equal([
+ chunk.object_id,
+ nil,
+ nil,
+ ],
+ [
+ chunked_array.peek_chunk(0).object_id,
+ chunked_array.peek_chunk(1),
+ chunked_array.peek_chunk(2),
+ ])
+ end
+
def test_chunks
chunks = [
build_boolean_array([true, false]),
@@ -102,6 +131,26 @@ class TestChunkedArray < Test::Unit::TestCase
chunked_array.chunks.collect(&:length))
end
+ def test_chunks_new
+ chunks = [
+ build_boolean_array([true, false]),
+ build_boolean_array([false]),
+ ]
+ chunked_array = Arrow::ChunkedArray.new(chunks)
+ assert_equal(chunks.collect(&:object_id),
+ chunked_array.chunks.collect(&:object_id))
+ end
+
+ def test_chunks_cache
+ chunks = [
+ build_boolean_array([true, false]),
+ build_boolean_array([false]),
+ ]
+ chunked_array = build_uncached_chunked_array(chunks)
+ assert_equal(chunked_array.chunks.collect(&:object_id),
+ chunked_array.chunks.collect(&:object_id))
+ end
+
def test_slice
chunks1 = [
build_boolean_array([true, false, true]),
@@ -155,4 +204,12 @@ class TestChunkedArray < Test::Unit::TestCase
assert_equal(original_chunked_array,
Arrow::ChunkedArray.import(c_abi_array_stream))
end
+
+ private
+ def build_uncached_chunked_array(chunks)
+ field = Arrow::Field.new("value", chunks[0].value_data_type)
+ schema = Arrow::Schema.new([field])
+ table = Arrow::Table.new(schema, [Arrow::ChunkedArray.new(chunks)])
+ table.get_column_data(0)
+ end
end
diff --git a/ruby/red-arrow/ext/arrow/arrow.cpp
b/ruby/red-arrow/ext/arrow/arrow.cpp
index b87b2774b3..7727d4d976 100644
--- a/ruby/red-arrow/ext/arrow/arrow.cpp
+++ b/ruby/red-arrow/ext/arrow/arrow.cpp
@@ -44,6 +44,19 @@ namespace red_arrow {
VALUE nanosecond;
}
+ void
+ chunked_array_mark(gpointer object)
+ {
+ auto chunked_array = GARROW_CHUNKED_ARRAY(object);
+ auto n_chunks = garrow_chunked_array_get_n_chunks(chunked_array);
+ for (guint i = 0; i < n_chunks; ++i) {
+ auto chunk = garrow_chunked_array_peek_chunk(chunked_array, i);
+ if (chunk) {
+ rbgobj_gc_mark_instance(chunk);
+ }
+ }
+ }
+
void
record_batch_reader_mark(gpointer object)
{
@@ -150,6 +163,8 @@ extern "C" void Init_arrow() {
red_arrow::symbols::month = ID2SYM(rb_intern("month"));
red_arrow::symbols::nanosecond = ID2SYM(rb_intern("nanosecond"));
+ rbgobj_register_mark_func(GARROW_TYPE_CHUNKED_ARRAY,
+ red_arrow::chunked_array_mark);
rbgobj_register_mark_func(GARROW_TYPE_RECORD_BATCH_READER,
red_arrow::record_batch_reader_mark);
rbgobj_register_mark_func(GARROW_TYPE_EXECUTE_PLAN,
diff --git a/ruby/red-arrow/test/test-chunked-array.rb
b/ruby/red-arrow/test/test-chunked-array.rb
index 7c23cee7f7..56cdae88ab 100644
--- a/ruby/red-arrow/test/test-chunked-array.rb
+++ b/ruby/red-arrow/test/test-chunked-array.rb
@@ -195,4 +195,17 @@ class ChunkedArrayTest < Test::Unit::TestCase
chunked_array = Arrow::ChunkedArray.new(arrays)
assert_equal(2, chunked_array.index(3))
end
+
+ test("GC") do
+ arrays = [
+ Arrow::Int32Array.new([1, 2]),
+ Arrow::Int32Array.new([3]),
+ ]
+ chunked_array = Arrow::ChunkedArray.new(arrays)
+ array_ids = arrays.collect(&:object_id)
+ arrays = nil
+ GC.start
+ assert_equal(array_ids,
+ chunked_array.chunks.collect(&:object_id))
+ end
end