Skip to content

Commit

Permalink
ARROW-6350: [Ruby] Remove Arrow::Struct and use Hash instead
Browse files Browse the repository at this point in the history
This is backward incompatible change. But this simple API will be
useful for users.

Closes apache#5190 from kou/ruby-remove-struct and squashes the following commits:

e565c3b <Sutou Kouhei>  Remove Arrow::Struct and use Hash instead

Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Yosuke Shiro <[email protected]>
  • Loading branch information
kou authored and shiro615 committed Aug 25, 2019
1 parent e0fa3d1 commit 0f9c4a9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 186 deletions.
8 changes: 1 addition & 7 deletions ruby/red-arrow/lib/arrow/struct-array-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,14 @@ def append_value(*args)
value.each_with_index do |sub_value, i|
self[i].append(sub_value)
end
when Arrow::Struct
append_value_raw
value.values.each_with_index do |sub_value, i|
self[i].append(sub_value)
end
when Hash
append_value_raw
value.each do |name, sub_value|
self[name].append(sub_value)
end
else
message =
"struct value must be nil, Array, " +
"Arrow::Struct or Hash: #{value.inspect}"
"struct value must be nil, Array or Hash: #{value.inspect}"
raise ArgumentError, message
end
else
Expand Down
24 changes: 13 additions & 11 deletions ruby/red-arrow/lib/arrow/struct-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
# specific language governing permissions and limitations
# under the License.

require "arrow/struct"

module Arrow
class StructArray
# @param i [Integer]
# The index of the value to be gotten. You must specify the value index.
#
# You can use {Arrow::Array#[]} for convenient value access.
#
# @return [Arrow::Struct] The `i`-th value.
# @return [Hash] The `i`-th struct.
def get_value(i)
Struct.new(self, i)
value = {}
value_data_type.fields.zip(fields) do |field, field_array|
value[field.name] = field_array[i]
end
value
end

# @overload find_field(index)
Expand All @@ -45,20 +47,20 @@ def find_field(index_or_name)
(@name_to_field ||= build_name_to_field)[name.to_s]
else
index = index_or_name
cached_fields[index]
fields[index]
end
end

private
def cached_fields
@fields ||= fields
alias_method :fields_raw, :fields
def fields
@fields ||= fields_raw
end

private
def build_name_to_field
name_to_field = {}
field_arrays = cached_fields
value_data_type.fields.each_with_index do |field, i|
name_to_field[field.name] = field_arrays[i]
value_data_type.fields.zip(fields) do |field, field_array|
name_to_field[field.name] = field_array
end
name_to_field
end
Expand Down
79 changes: 0 additions & 79 deletions ruby/red-arrow/lib/arrow/struct.rb

This file was deleted.

8 changes: 4 additions & 4 deletions ruby/red-arrow/test/test-struct-array-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ def setup
@builder.get_field_builder(1).append(2)
array = @builder.finish
assert_equal([
[true, 1],
[false, 2],
{"visible" => true, "count" => 1},
{"visible" => false, "count" => 2},
],
[
array.get_value(0).values,
array.get_value(1).values,
array.get_value(0),
array.get_value(1),
])
end
end
Expand Down
8 changes: 4 additions & 4 deletions ruby/red-arrow/test/test-struct-array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def setup

test("#[]") do
assert_equal([
Arrow::Struct.new(@array, 0),
Arrow::Struct.new(@array, 1),
{"visible" => true, "count" => 1},
{"visible" => false, "count" => 2},
],
@array.to_a)
end

test("#get_value") do
assert_equal([
Arrow::Struct.new(@array, 0),
Arrow::Struct.new(@array, 1),
{"visible" => true, "count" => 1},
{"visible" => false, "count" => 2},
],
[
@array.get_value(0),
Expand Down
81 changes: 0 additions & 81 deletions ruby/red-arrow/test/test-struct.rb

This file was deleted.

0 comments on commit 0f9c4a9

Please sign in to comment.