From de3130ede1a2db7bbe9e129f21856ee80de0f8cb Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Mon, 8 Jan 2024 07:21:00 +0900 Subject: [PATCH] GH-39488: [Ruby] Add support for ChunkedArray in Ractor (#39490) ### Rationale for this change We can't use `@ cache ||= build_cache` idiom in Ractor because Ractor requires that shared objects are immutable. ### What changes are included in this PR? Compute caches before making ChunkedArray immutable. ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * Closes: #39488 Authored-by: Sutou Kouhei Signed-off-by: Sutou Kouhei --- ruby/red-arrow/lib/arrow/chunked-array.rb | 8 ++++++ ruby/red-arrow/test/helper/omittable.rb | 5 ++++ ruby/red-arrow/test/test-ractor.rb | 34 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 ruby/red-arrow/test/test-ractor.rb diff --git a/ruby/red-arrow/lib/arrow/chunked-array.rb b/ruby/red-arrow/lib/arrow/chunked-array.rb index 7d83becc6deb3..0cdd9b6a6d2f0 100644 --- a/ruby/red-arrow/lib/arrow/chunked-array.rb +++ b/ruby/red-arrow/lib/arrow/chunked-array.rb @@ -24,6 +24,14 @@ class ChunkedArray include GenericTakeable include InputReferable + def freeze + unless frozen? + # Ensure caching + chunks + end + super + end + def to_arrow self end diff --git a/ruby/red-arrow/test/helper/omittable.rb b/ruby/red-arrow/test/helper/omittable.rb index a1c0334b63a2b..8e1564be47879 100644 --- a/ruby/red-arrow/test/helper/omittable.rb +++ b/ruby/red-arrow/test/helper/omittable.rb @@ -17,6 +17,11 @@ module Helper module Omittable + def require_ruby(major, minor, micro=0) + return if (RUBY_VERSION <=> "#{major}.#{minor}.#{micro}") >= 0 + omit("Require Ruby #{major}.#{minor}.#{micro} or later: #{RUBY_VERSION}") + end + def require_gi_bindings(major, minor, micro) return if GLib.check_binding_version?(major, minor, micro) message = diff --git a/ruby/red-arrow/test/test-ractor.rb b/ruby/red-arrow/test/test-ractor.rb new file mode 100644 index 0000000000000..daf674d135257 --- /dev/null +++ b/ruby/red-arrow/test/test-ractor.rb @@ -0,0 +1,34 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class RactorTest < Test::Unit::TestCase + include Helper::Omittable + + ractor + test("ChunkedArray") do + require_ruby(3, 1, 0) + array = Arrow::Array.new([1, 2, 3]) + chunked_array = Arrow::ChunkedArray.new([array]) + Ractor.make_shareable(chunked_array) + ractor = Ractor.new do + recived_chunked_array = Ractor.receive + recived_chunked_array.chunks + end + ractor.send(chunked_array) + assert_equal([array], ractor.take) + end +end