From 502c910dac3aac51af0a499e88f034de26968d8d Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Fri, 5 Dec 2025 20:52:29 +0900 Subject: [PATCH] GH-48358: [Ruby] Add support for reading float32 array --- .../lib/arrow-format/array.rb | 13 ++++++++++ .../lib/arrow-format/file-reader.rb | 11 +++++++-- .../red-arrow-format/lib/arrow-format/type.rb | 24 +++++++++++++++++++ .../red-arrow-format/test/test-file-reader.rb | 11 +++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/ruby/red-arrow-format/lib/arrow-format/array.rb b/ruby/red-arrow-format/lib/arrow-format/array.rb index 2a304f54163a..f3c3c49233d1 100644 --- a/ruby/red-arrow-format/lib/arrow-format/array.rb +++ b/ruby/red-arrow-format/lib/arrow-format/array.rb @@ -91,6 +91,19 @@ def to_a end end + class FloatArray < Array + def initialize(type, size, validity_buffer, values_buffer) + super(type, size, validity_buffer) + @values_buffer = values_buffer + end + end + + class Float32Array < FloatArray + def to_a + apply_validity(@values_buffer.values(:f32, 0, @size)) + end + end + class VariableSizeBinaryLayoutArray < Array def initialize(type, size, validity_buffer, offsets_buffer, values_buffer) super(type, size, validity_buffer) 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 3db6bad77add..edc866b3f071 100644 --- a/ruby/red-arrow-format/lib/arrow-format/file-reader.rb +++ b/ruby/red-arrow-format/lib/arrow-format/file-reader.rb @@ -24,11 +24,13 @@ require_relative "org/apache/arrow/flatbuf/binary" require_relative "org/apache/arrow/flatbuf/bool" +require_relative "org/apache/arrow/flatbuf/floating_point" require_relative "org/apache/arrow/flatbuf/footer" require_relative "org/apache/arrow/flatbuf/int" require_relative "org/apache/arrow/flatbuf/list" require_relative "org/apache/arrow/flatbuf/message" require_relative "org/apache/arrow/flatbuf/null" +require_relative "org/apache/arrow/flatbuf/precision" require_relative "org/apache/arrow/flatbuf/schema" require_relative "org/apache/arrow/flatbuf/utf8" @@ -147,6 +149,11 @@ def read_field(fb_field) type = UInt8Type.singleton end end + when Org::Apache::Arrow::Flatbuf::FloatingPoint + case fb_type.precision + when Org::Apache::Arrow::Flatbuf::Precision::SINGLE + type = Float32Type.singleton + end when Org::Apache::Arrow::Flatbuf::List type = ListType.new(read_field(fb_field.children[0])) when Org::Apache::Arrow::Flatbuf::Binary @@ -179,8 +186,8 @@ def read_column(field, nodes, buffers, body) case field.type when BooleanType, - Int8Type, - UInt8Type + IntType, + FloatType values_buffer = buffers.shift values = body.slice(values_buffer.offset, values_buffer.length) field.type.build_array(length, validity, values) diff --git a/ruby/red-arrow-format/lib/arrow-format/type.rb b/ruby/red-arrow-format/lib/arrow-format/type.rb index c792eac17555..75586c2f3523 100644 --- a/ruby/red-arrow-format/lib/arrow-format/type.rb +++ b/ruby/red-arrow-format/lib/arrow-format/type.rb @@ -96,6 +96,30 @@ def build_array(size, validity_buffer, values_buffer) end end + class FloatType < Type + attr_reader :precision + def initialize(name, precision) + super(name) + @precision = precision + end + end + + class Float32Type < FloatType + class << self + def singleton + @singleton ||= new + end + end + + def initialize + super("Float32", 32) + end + + def build_array(size, validity_buffer, values_buffer) + Float32Array.new(self, size, validity_buffer, values_buffer) + end + end + class BinaryType < Type class << self def singleton diff --git a/ruby/red-arrow-format/test/test-file-reader.rb b/ruby/red-arrow-format/test/test-file-reader.rb index 95cb6f3b1a84..02685b1987c6 100644 --- a/ruby/red-arrow-format/test/test-file-reader.rb +++ b/ruby/red-arrow-format/test/test-file-reader.rb @@ -84,6 +84,17 @@ def test_read end end + sub_test_case("Float32") do + def build_array + Arrow::FloatArray.new([-0.5, nil, 0.5]) + end + + def test_read + assert_equal([{"value" => [-0.5, nil, 0.5]}], + read) + end + end + sub_test_case("Binary") do def build_array Arrow::BinaryArray.new(["Hello".b, nil, "World".b])