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
17 changes: 17 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,21 @@ def to_a
apply_validity(values)
end
end

class StructArray < Array
def initialize(type, size, validity_buffer, children)
super(type, size, validity_buffer)
@children = children
end

def to_a
if @children.empty?
values = [[]] * @size
else
children_values = @children.collect(&:to_a)
values = children_values[0].zip(*children_values[1..-1])
end
apply_validity(values)
end
end
end
9 changes: 9 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/file-reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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/struct_"
require_relative "org/apache/arrow/flatbuf/utf8"

module ArrowFormat
Expand Down Expand Up @@ -157,6 +158,9 @@ def read_field(fb_field)
end
when Org::Apache::Arrow::Flatbuf::List
type = ListType.new(read_field(fb_field.children[0]))
when Org::Apache::Arrow::Flatbuf::Struct
children = fb_field.children.collect {|child| read_field(child)}
type = StructType.new(children)
when Org::Apache::Arrow::Flatbuf::Binary
type = BinaryType.singleton
when Org::Apache::Arrow::Flatbuf::LargeBinary
Expand Down Expand Up @@ -199,6 +203,11 @@ def read_column(field, nodes, buffers, body)
offsets = body.slice(offsets_buffer.offset, offsets_buffer.length)
child = read_column(field.type.child, nodes, buffers, body)
field.type.build_array(length, validity, offsets, child)
when StructType
children = field.type.children.collect do |child|
read_column(child, nodes, buffers, body)
end
field.type.build_array(length, validity, children)
when VariableSizeBinaryType
offsets_buffer = buffers.shift
values_buffer = buffers.shift
Expand Down
12 changes: 12 additions & 0 deletions ruby/red-arrow-format/lib/arrow-format/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,16 @@ def build_array(size, validity_buffer, offsets_buffer, child)
ListArray.new(self, size, validity_buffer, offsets_buffer, child)
end
end

class StructType < Type
attr_reader :children
def initialize(children)
super("Struct")
@children = children
end

def build_array(size, validity_buffer, children)
StructArray.new(self, size, validity_buffer, children)
end
end
end
21 changes: 21 additions & 0 deletions ruby/red-arrow-format/test/test-file-reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,25 @@ def test_read
read)
end
end

sub_test_case("Struct") do
def build_array
data_type = Arrow::StructDataType.new(count: :int8,
visible: :boolean)
Arrow::StructArray.new(data_type, [[-128, nil], nil, [nil, true]])
end

def test_read
assert_equal([
{
"value" => [
[-128, nil],
nil,
[nil, true],
],
},
],
read)
end
end
end
Loading