Skip to content

Commit 5fbd2dc

Browse files
authored
GH-48358: [Ruby] Add support for reading float32 array (#48359)
### Rationale for this change It's the first float array. ### What changes are included in this PR? * Add `ArrowFormat::Float32Type` * Add `ArrowFormat::Float32Array` ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #48358 Authored-by: Sutou Kouhei <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 62d3db6 commit 5fbd2dc

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

ruby/red-arrow-format/lib/arrow-format/array.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ def to_a
9191
end
9292
end
9393

94+
class FloatArray < Array
95+
def initialize(type, size, validity_buffer, values_buffer)
96+
super(type, size, validity_buffer)
97+
@values_buffer = values_buffer
98+
end
99+
end
100+
101+
class Float32Array < FloatArray
102+
def to_a
103+
apply_validity(@values_buffer.values(:f32, 0, @size))
104+
end
105+
end
106+
94107
class VariableSizeBinaryLayoutArray < Array
95108
def initialize(type, size, validity_buffer, offsets_buffer, values_buffer)
96109
super(type, size, validity_buffer)

ruby/red-arrow-format/lib/arrow-format/file-reader.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424

2525
require_relative "org/apache/arrow/flatbuf/binary"
2626
require_relative "org/apache/arrow/flatbuf/bool"
27+
require_relative "org/apache/arrow/flatbuf/floating_point"
2728
require_relative "org/apache/arrow/flatbuf/footer"
2829
require_relative "org/apache/arrow/flatbuf/int"
2930
require_relative "org/apache/arrow/flatbuf/list"
3031
require_relative "org/apache/arrow/flatbuf/message"
3132
require_relative "org/apache/arrow/flatbuf/null"
33+
require_relative "org/apache/arrow/flatbuf/precision"
3234
require_relative "org/apache/arrow/flatbuf/schema"
3335
require_relative "org/apache/arrow/flatbuf/utf8"
3436

@@ -147,6 +149,11 @@ def read_field(fb_field)
147149
type = UInt8Type.singleton
148150
end
149151
end
152+
when Org::Apache::Arrow::Flatbuf::FloatingPoint
153+
case fb_type.precision
154+
when Org::Apache::Arrow::Flatbuf::Precision::SINGLE
155+
type = Float32Type.singleton
156+
end
150157
when Org::Apache::Arrow::Flatbuf::List
151158
type = ListType.new(read_field(fb_field.children[0]))
152159
when Org::Apache::Arrow::Flatbuf::Binary
@@ -179,8 +186,8 @@ def read_column(field, nodes, buffers, body)
179186

180187
case field.type
181188
when BooleanType,
182-
Int8Type,
183-
UInt8Type
189+
IntType,
190+
FloatType
184191
values_buffer = buffers.shift
185192
values = body.slice(values_buffer.offset, values_buffer.length)
186193
field.type.build_array(length, validity, values)

ruby/red-arrow-format/lib/arrow-format/type.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ def build_array(size, validity_buffer, values_buffer)
9696
end
9797
end
9898

99+
class FloatType < Type
100+
attr_reader :precision
101+
def initialize(name, precision)
102+
super(name)
103+
@precision = precision
104+
end
105+
end
106+
107+
class Float32Type < FloatType
108+
class << self
109+
def singleton
110+
@singleton ||= new
111+
end
112+
end
113+
114+
def initialize
115+
super("Float32", 32)
116+
end
117+
118+
def build_array(size, validity_buffer, values_buffer)
119+
Float32Array.new(self, size, validity_buffer, values_buffer)
120+
end
121+
end
122+
99123
class BinaryType < Type
100124
class << self
101125
def singleton

ruby/red-arrow-format/test/test-file-reader.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ def test_read
8484
end
8585
end
8686

87+
sub_test_case("Float32") do
88+
def build_array
89+
Arrow::FloatArray.new([-0.5, nil, 0.5])
90+
end
91+
92+
def test_read
93+
assert_equal([{"value" => [-0.5, nil, 0.5]}],
94+
read)
95+
end
96+
end
97+
8798
sub_test_case("Binary") do
8899
def build_array
89100
Arrow::BinaryArray.new(["Hello".b, nil, "World".b])

0 commit comments

Comments
 (0)