From 6fdd4041215f91f66f5868f521e80fe371de26aa Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Sun, 7 Dec 2025 14:44:15 +0900 Subject: [PATCH] GH-48380: [Ruby] Add support for reading float64 array --- .../lib/arrow-format/array.rb | 10 +++++-- .../lib/arrow-format/file-reader.rb | 5 ++-- .../red-arrow-format/lib/arrow-format/type.rb | 27 ++++++++++++++++--- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/ruby/red-arrow-format/lib/arrow-format/array.rb b/ruby/red-arrow-format/lib/arrow-format/array.rb index d4995cda3e68..41a553bcd9f5 100644 --- a/ruby/red-arrow-format/lib/arrow-format/array.rb +++ b/ruby/red-arrow-format/lib/arrow-format/array.rb @@ -91,19 +91,25 @@ def to_a end end - class FloatArray < Array + class FloatingPointArray < Array def initialize(type, size, validity_buffer, values_buffer) super(type, size, validity_buffer) @values_buffer = values_buffer end end - class Float32Array < FloatArray + class Float32Array < FloatingPointArray def to_a apply_validity(@values_buffer.values(:f32, 0, @size)) end end + class Float64Array < FloatingPointArray + def to_a + apply_validity(@values_buffer.values(:f64, 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 acd21b976481..72c5c9bca85b 100644 --- a/ruby/red-arrow-format/lib/arrow-format/file-reader.rb +++ b/ruby/red-arrow-format/lib/arrow-format/file-reader.rb @@ -154,6 +154,8 @@ def read_field(fb_field) case fb_type.precision when Org::Apache::Arrow::Flatbuf::Precision::SINGLE type = Float32Type.singleton + when Org::Apache::Arrow::Flatbuf::Precision::DOUBLE + type = Float64Type.singleton end when Org::Apache::Arrow::Flatbuf::List type = ListType.new(read_field(fb_field.children[0])) @@ -189,8 +191,7 @@ def read_column(field, nodes, buffers, body) case field.type when BooleanType, - IntType, - FloatType + NumberType 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 b6563956346b..c7cebcd745ea 100644 --- a/ruby/red-arrow-format/lib/arrow-format/type.rb +++ b/ruby/red-arrow-format/lib/arrow-format/type.rb @@ -54,7 +54,10 @@ def build_array(size, validity_buffer, values_buffer) end end - class IntType < Type + class NumberType < Type + end + + class IntType < NumberType attr_reader :bit_width attr_reader :signed def initialize(name, bit_width, signed) @@ -96,7 +99,7 @@ def build_array(size, validity_buffer, values_buffer) end end - class FloatType < Type + class FloatingPointType < NumberType attr_reader :precision def initialize(name, precision) super(name) @@ -104,7 +107,7 @@ def initialize(name, precision) end end - class Float32Type < FloatType + class Float32Type < FloatingPointType class << self def singleton @singleton ||= new @@ -112,7 +115,7 @@ def singleton end def initialize - super("Float32", 32) + super("Float32", :single) end def build_array(size, validity_buffer, values_buffer) @@ -120,6 +123,22 @@ def build_array(size, validity_buffer, values_buffer) end end + class Float64Type < FloatingPointType + class << self + def singleton + @singleton ||= new + end + end + + def initialize + super("Float64", :double) + end + + def build_array(size, validity_buffer, values_buffer) + Float64Array.new(self, size, validity_buffer, values_buffer) + end + end + class VariableSizeBinaryType < Type end