Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions ruby/red-arrow-format/lib/arrow-format/file-reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions ruby/red-arrow-format/test/test-file-reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down