Skip to content

Commit aae5840

Browse files
authored
GH-49055: [Ruby] Add support for writing decimal128/256 arrays (#49056)
### Rationale for this change Decimal128/256 arrays are only supported. ### What changes are included in this PR? Add `ArrowFormat::DecimalType#to_flatbuffers`. ### Are these changes tested? Yes. ### Are there any user-facing changes? Yes. * GitHub Issue: #49055 Authored-by: Sutou Kouhei <kou@clear-code.com> Signed-off-by: Sutou Kouhei <kou@clear-code.com>
1 parent 3e6182a commit aae5840

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,14 @@ def initialize(byte_width, precision, scale)
627627
@precision = precision
628628
@scale = scale
629629
end
630+
631+
def to_flatbuffers
632+
fb_type = FB::Decimal::Data.new
633+
fb_type.bit_width = @byte_width * 8
634+
fb_type.precision = @precision
635+
fb_type.scale = @scale
636+
fb_type
637+
end
630638
end
631639

632640
class Decimal128Type < DecimalType

ruby/red-arrow-format/test/test-writer.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def convert_type(red_arrow_type)
6868
ArrowFormat::UTF8Type.singleton
6969
when Arrow::LargeStringDataType
7070
ArrowFormat::LargeUTF8Type.singleton
71+
when Arrow::Decimal128DataType
72+
ArrowFormat::Decimal128Type.new(red_arrow_type.precision,
73+
red_arrow_type.scale)
74+
when Arrow::Decimal256DataType
75+
ArrowFormat::Decimal256Type.new(red_arrow_type.precision,
76+
red_arrow_type.scale)
7177
when Arrow::FixedSizeBinaryDataType
7278
ArrowFormat::FixedSizeBinaryType.new(red_arrow_type.byte_width)
7379
else
@@ -444,6 +450,62 @@ def test_write
444450
@values)
445451
end
446452
end
453+
454+
sub_test_case("Decimal128") do
455+
def build_array
456+
@positive_small = "1.200"
457+
@positive_large = ("1234567890" * 3) + "12345.678"
458+
@negative_small = "-1.200"
459+
@negative_large = "-" + ("1234567890" * 3) + "12345.678"
460+
Arrow::Decimal128Array.new({precision: 38, scale: 3},
461+
[
462+
@positive_large,
463+
@positive_small,
464+
nil,
465+
@negative_small,
466+
@negative_large,
467+
])
468+
end
469+
470+
def test_write
471+
assert_equal([
472+
BigDecimal(@positive_large),
473+
BigDecimal(@positive_small),
474+
nil,
475+
BigDecimal(@negative_small),
476+
BigDecimal(@negative_large),
477+
],
478+
@values)
479+
end
480+
end
481+
482+
sub_test_case("Decimal256") do
483+
def build_array
484+
@positive_small = "1.200"
485+
@positive_large = ("1234567890" * 7) + "123.456"
486+
@negative_small = "-1.200"
487+
@negative_large = "-" + ("1234567890" * 7) + "123.456"
488+
Arrow::Decimal256Array.new({precision: 76, scale: 3},
489+
[
490+
@positive_large,
491+
@positive_small,
492+
nil,
493+
@negative_small,
494+
@negative_large,
495+
])
496+
end
497+
498+
def test_write
499+
assert_equal([
500+
BigDecimal(@positive_large),
501+
BigDecimal(@positive_small),
502+
nil,
503+
BigDecimal(@negative_small),
504+
BigDecimal(@negative_large),
505+
],
506+
@values)
507+
end
508+
end
447509
end
448510
end
449511
end

0 commit comments

Comments
 (0)