Skip to content

Commit 8f90970

Browse files
GH-48471: [Ruby] Add support for reading Int16 and UInt16 arrays (#48472)
### Rationale for this change They are int16 and uint16 variants of an int array. ### What changes are included in this PR? * Add `ArrowFromat::Int16Type` * Add `ArrowFromat::UInt16Type` * Add `ArrowFromat::Int16Array` * Add `ArrowFromat::UInt16Array` ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #48471 Authored-by: Hiroyuki Sato <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent f8e40da commit 8f90970

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
@@ -91,6 +91,18 @@ def to_a
9191
end
9292
end
9393

94+
class Int16Array < IntArray
95+
def to_a
96+
apply_validity(@values_buffer.values(:s16, 0, @size))
97+
end
98+
end
99+
100+
class UInt16Array < IntArray
101+
def to_a
102+
apply_validity(@values_buffer.values(:u16, 0, @size))
103+
end
104+
end
105+
94106
class FloatingPointArray < Array
95107
def initialize(type, size, validity_buffer, values_buffer)
96108
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
@@ -156,6 +156,12 @@ def read_field(fb_field)
156156
else
157157
type = UInt8Type.singleton
158158
end
159+
when 16
160+
if fb_type.signed?
161+
type = Int16Type.singleton
162+
else
163+
type = UInt16Type.singleton
164+
end
159165
end
160166
when Org::Apache::Arrow::Flatbuf::FloatingPoint
161167
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
@@ -99,6 +99,38 @@ def build_array(size, validity_buffer, values_buffer)
9999
end
100100
end
101101

102+
class Int16Type < IntType
103+
class << self
104+
def singleton
105+
@singleton ||= new
106+
end
107+
end
108+
109+
def initialize
110+
super("Int16", 16, true)
111+
end
112+
113+
def build_array(size, validity_buffer, values_buffer)
114+
Int16Array.new(self, size, validity_buffer, values_buffer)
115+
end
116+
end
117+
118+
class UInt16Type < IntType
119+
class << self
120+
def singleton
121+
@singleton ||= new
122+
end
123+
end
124+
125+
def initialize
126+
super("UInt16", 16, false)
127+
end
128+
129+
def build_array(size, validity_buffer, values_buffer)
130+
UInt16Array.new(self, size, validity_buffer, values_buffer)
131+
end
132+
end
133+
102134
class FloatingPointType < NumberType
103135
attr_reader :precision
104136
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
@@ -84,6 +84,28 @@ def test_read
8484
end
8585
end
8686

87+
sub_test_case("Int16") do
88+
def build_array
89+
Arrow::Int16Array.new([-32768, nil, 32767])
90+
end
91+
92+
def test_read
93+
assert_equal([{"value" => [-32768, nil, 32767]}],
94+
read)
95+
end
96+
end
97+
98+
sub_test_case("UInt16") do
99+
def build_array
100+
Arrow::UInt16Array.new([0, nil, 65535])
101+
end
102+
103+
def test_read
104+
assert_equal([{"value" => [0, nil, 65535]}],
105+
read)
106+
end
107+
end
108+
87109
sub_test_case("Float32") do
88110
def build_array
89111
Arrow::FloatArray.new([-0.5, nil, 0.5])

0 commit comments

Comments
 (0)