Skip to content

Commit

Permalink
apacheGH-39488: [Ruby] Add support for ChunkedArray in Ractor (apache…
Browse files Browse the repository at this point in the history
…#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: apache#39488

Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
kou authored Jan 7, 2024
1 parent 37a8bf0 commit de3130e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ruby/red-arrow/lib/arrow/chunked-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions ruby/red-arrow/test/helper/omittable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
34 changes: 34 additions & 0 deletions ruby/red-arrow/test/test-ractor.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de3130e

Please sign in to comment.