From 96bf0deb9bfcd7b8736f4a2a57970bb308b702a0 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Thu, 4 Dec 2025 17:46:37 +0900 Subject: [PATCH] GH-48328: [Ruby] Add support for reading UTF-8 array --- .../red-arrow-format/lib/arrow-format/array.rb | 18 ++++++++++++++++-- .../lib/arrow-format/file-reader.rb | 5 ++++- ruby/red-arrow-format/lib/arrow-format/type.rb | 17 +++++++++++++++++ ruby/red-arrow-format/test/test-file-reader.rb | 11 +++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/ruby/red-arrow-format/lib/arrow-format/array.rb b/ruby/red-arrow-format/lib/arrow-format/array.rb index 8315868892c3..85daaf7d283d 100644 --- a/ruby/red-arrow-format/lib/arrow-format/array.rb +++ b/ruby/red-arrow-format/lib/arrow-format/array.rb @@ -73,7 +73,7 @@ def to_a end end - class BinaryArray < Array + class VariableSizeBinaryLayoutArray < Array def initialize(type, size, validity_buffer, offsets_buffer, values_buffer) super(type, size, validity_buffer) @offsets_buffer = offsets_buffer @@ -86,9 +86,23 @@ def to_a each_cons(2). collect do |(_, offset), (_, next_offset)| length = next_offset - offset - @values_buffer.get_string(offset, length) + @values_buffer.get_string(offset, length, encoding) end apply_validity(values) end end + + class BinaryArray < VariableSizeBinaryLayoutArray + private + def encoding + Encoding::ASCII_8BIT + end + end + + class UTF8Array < VariableSizeBinaryLayoutArray + private + def encoding + Encoding::UTF_8 + end + end end diff --git a/ruby/red-arrow-format/lib/arrow-format/file-reader.rb b/ruby/red-arrow-format/lib/arrow-format/file-reader.rb index 6d638b5461fb..7802acd9e4d4 100644 --- a/ruby/red-arrow-format/lib/arrow-format/file-reader.rb +++ b/ruby/red-arrow-format/lib/arrow-format/file-reader.rb @@ -139,6 +139,8 @@ def read_schema(fb_schema) end when Org::Apache::Arrow::Flatbuf::Binary type = BinaryType.singleton + when Org::Apache::Arrow::Flatbuf::Utf8 + type = UTF8Type.singleton end Field.new(fb_field.name, type) end @@ -159,7 +161,8 @@ def read_column(field, n_rows, buffers, body) values_buffer = buffers.shift values = body.slice(values_buffer.offset, values_buffer.length) field.type.build_array(n_rows, validity, values) - when BinaryType + when BinaryType, + UTF8Type offsets_buffer = buffers.shift values_buffer = buffers.shift offsets = body.slice(offsets_buffer.offset, offsets_buffer.length) diff --git a/ruby/red-arrow-format/lib/arrow-format/type.rb b/ruby/red-arrow-format/lib/arrow-format/type.rb index 657ee3ea7575..308299f5644d 100644 --- a/ruby/red-arrow-format/lib/arrow-format/type.rb +++ b/ruby/red-arrow-format/lib/arrow-format/type.rb @@ -80,4 +80,21 @@ def build_array(size, validity_buffer, offsets_buffer, values_buffer) BinaryArray.new(self, size, validity_buffer, offsets_buffer, values_buffer) end end + + class UTF8Type < Type + class << self + def singleton + @singleton ||= new + end + end + + attr_reader :name + def initialize + super("UTF8") + end + + def build_array(size, validity_buffer, offsets_buffer, values_buffer) + UTF8Array.new(self, size, validity_buffer, offsets_buffer, values_buffer) + end + end end diff --git a/ruby/red-arrow-format/test/test-file-reader.rb b/ruby/red-arrow-format/test/test-file-reader.rb index 153a0b93f7d6..c3d75a30440b 100644 --- a/ruby/red-arrow-format/test/test-file-reader.rb +++ b/ruby/red-arrow-format/test/test-file-reader.rb @@ -72,4 +72,15 @@ def test_read read) end end + + sub_test_case("UTF8") do + def build_array + Arrow::StringArray.new(["Hello", nil, "World"]) + end + + def test_read + assert_equal([{"value" => ["Hello", nil, "World"]}], + read) + end + end end