Skip to content

Commit 84a5701

Browse files
authored
GH-49364: [Ruby] Simplify reader tests (#49365)
### Rationale for this change The current reader tests use a sub test case per type but we can use a test per type like the current writer tests. ### What changes are included in this PR? * Create test data in a test not a setup to use a test no a sub test case per type * Add `ArrowFormat::Type#to_s` * Add `ArrowFormat::FileReader#schema` ### Are these changes tested? Yes. ### Are there any user-facing changes? No. * GitHub Issue: #49364 Authored-by: Sutou Kouhei <kou@clear-code.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 8c27898 commit 84a5701

3 files changed

Lines changed: 536 additions & 793 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FileReader
3434
FOOTER_SIZE_FORMAT = :s32
3535
FOOTER_SIZE_SIZE = IO::Buffer.size_of(FOOTER_SIZE_FORMAT)
3636

37+
attr_reader :schema
3738
def initialize(input)
3839
case input
3940
when IO

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
module ArrowFormat
1818
class Type
19+
def to_s
20+
name
21+
end
1922
end
2023

2124
class NullType < Type
@@ -412,6 +415,10 @@ def initialize(bit_width, unit)
412415
@unit = unit
413416
end
414417

418+
def to_s
419+
"#{super}(#{unit})"
420+
end
421+
415422
def to_flatbuffers
416423
fb_type = FB::Time::Data.new
417424
fb_type.bit_width = @bit_width
@@ -477,6 +484,12 @@ def build_array(size, validity_buffer, values_buffer)
477484
TimestampArray.new(self, size, validity_buffer, values_buffer)
478485
end
479486

487+
def to_s
488+
options = [@unit]
489+
options << @time_zone if @time_zone
490+
"#{super}(#{options.join(", ")})"
491+
end
492+
480493
def to_flatbuffers
481494
fb_type = FB::Timestamp::Data.new
482495
fb_type.unit = FB::TimeUnit.try_convert(@unit.to_s.upcase)
@@ -581,6 +594,10 @@ def build_array(size, validity_buffer, values_buffer)
581594
DurationArray.new(self, size, validity_buffer, values_buffer)
582595
end
583596

597+
def to_s
598+
"#{super}(#{@unit})"
599+
end
600+
584601
def to_flatbuffers
585602
fb_type = FB::Duration::Data.new
586603
fb_type.unit = FB::TimeUnit.try_convert(@unit.to_s.upcase)
@@ -730,6 +747,10 @@ def build_array(size, validity_buffer, values_buffer)
730747
FixedSizeBinaryArray.new(self, size, validity_buffer, values_buffer)
731748
end
732749

750+
def to_s
751+
"#{super}(#{@byte_width})"
752+
end
753+
733754
def to_flatbuffers
734755
fb_type = FB::FixedSizeBinary::Data.new
735756
fb_type.byte_width = @byte_width
@@ -746,6 +767,10 @@ def initialize(byte_width, precision, scale)
746767
@scale = scale
747768
end
748769

770+
def to_s
771+
"#{name}(#{@precision}, #{@scale})"
772+
end
773+
749774
def to_flatbuffers
750775
fb_type = FB::Decimal::Data.new
751776
fb_type.bit_width = @byte_width * 8
@@ -789,6 +814,10 @@ def initialize(child)
789814
super()
790815
@child = child
791816
end
817+
818+
def to_s
819+
"#{super}<#{child.name}: #{child.type}>"
820+
end
792821
end
793822

794823
class ListType < VariableSizeListType
@@ -842,6 +871,13 @@ def build_array(size, validity_buffer, children)
842871
StructArray.new(self, size, validity_buffer, children)
843872
end
844873

874+
def to_s
875+
fields = children.collect do |child|
876+
"#{child.name}: #{child.type}"
877+
end
878+
"#{super}<#{fields.join(", ")}>"
879+
end
880+
845881
def to_flatbuffers
846882
FB::Struct::Data.new
847883
end
@@ -880,6 +916,11 @@ def build_array(size, validity_buffer, offsets_buffer, child)
880916
MapArray.new(self, size, validity_buffer, offsets_buffer, child)
881917
end
882918

919+
def to_s
920+
key, value, = child.type.children
921+
"#{name}<#{key.type}, #{value.type}>"
922+
end
923+
883924
def to_flatbuffers
884925
FB::Map::Data.new
885926
end
@@ -900,6 +941,13 @@ def resolve_type_index(type)
900941
@type_indexes[type] ||= @type_ids.index(type)
901942
end
902943

944+
def to_s
945+
children = @children.collect.with_index do |child, i|
946+
"#{child.name}: #{child.type}=#{@type_ids[i]}"
947+
end
948+
"#{super}<#{children.join(", ")}>"
949+
end
950+
903951
def to_flatbuffers
904952
fb_type = FB::Union::Data.new
905953
fb_type.mode = FB::UnionMode.try_convert(@mode.to_s.capitalize)
@@ -975,5 +1023,10 @@ def build_fb_field(fb_field, field)
9751023
fb_field.type = @value_type.to_flatbuffers
9761024
fb_field.dictionary = fb_dictionary_encoding
9771025
end
1026+
1027+
def to_s
1028+
"#{super}<index=#{@index_type}, value=#{@value_type}, " +
1029+
"ordered=#{@ordered}>"
1030+
end
9781031
end
9791032
end

0 commit comments

Comments
 (0)