Skip to content

Commit a9200a4

Browse files
committed
GH-48475: [Ruby] Add support for reading Int32 and UInt32 arrays
1 parent 2a4ed78 commit a9200a4

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def to_a
103103
end
104104
end
105105

106+
class Int32Array < IntArray
107+
def to_a
108+
apply_validity(@values_buffer.values(:s32, 0, @size))
109+
end
110+
end
111+
112+
class UInt32Array < IntArray
113+
def to_a
114+
apply_validity(@values_buffer.values(:u32, 0, @size))
115+
end
116+
end
117+
106118
class FloatingPointArray < Array
107119
def initialize(type, size, validity_buffer, values_buffer)
108120
super(type, size, validity_buffer)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def read_field(fb_field)
164164
else
165165
type = UInt16Type.singleton
166166
end
167+
when 32
168+
if fb_type.signed?
169+
type = Int32Type.singleton
170+
else
171+
type = UInt32Type.singleton
172+
end
167173
end
168174
when Org::Apache::Arrow::Flatbuf::FloatingPoint
169175
case fb_type.precision

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,38 @@ def build_array(size, validity_buffer, values_buffer)
131131
end
132132
end
133133

134+
class Int32Type < IntType
135+
class << self
136+
def singleton
137+
@singleton ||= new
138+
end
139+
end
140+
141+
def initialize
142+
super("Int32", 32, true)
143+
end
144+
145+
def build_array(size, validity_buffer, values_buffer)
146+
Int32Array.new(self, size, validity_buffer, values_buffer)
147+
end
148+
end
149+
150+
class UInt32Type < IntType
151+
class << self
152+
def singleton
153+
@singleton ||= new
154+
end
155+
end
156+
157+
def initialize
158+
super("UInt32", 32, false)
159+
end
160+
161+
def build_array(size, validity_buffer, values_buffer)
162+
UInt32Array.new(self, size, validity_buffer, values_buffer)
163+
end
164+
end
165+
134166
class FloatingPointType < NumberType
135167
attr_reader :precision
136168
def initialize(name, precision)

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ def test_read
106106
end
107107
end
108108

109+
sub_test_case("Int32") do
110+
def build_array
111+
Arrow::Int32Array.new([-2147483648, nil, 2147483647])
112+
end
113+
114+
def test_read
115+
assert_equal([{"value" => [-2147483648, nil, 2147483647]}],
116+
read)
117+
end
118+
end
119+
120+
sub_test_case("UInt32") do
121+
def build_array
122+
Arrow::UInt32Array.new([0, nil, 4294967295])
123+
end
124+
125+
def test_read
126+
assert_equal([{"value" => [0, nil, 4294967295]}],
127+
read)
128+
end
129+
end
130+
109131
sub_test_case("Float32") do
110132
def build_array
111133
Arrow::FloatArray.new([-0.5, nil, 0.5])

0 commit comments

Comments
 (0)