This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new acb62888c6 GH-49074: [Ruby] Add support for writing interval arrays
(#49075)
acb62888c6 is described below
commit acb62888c66b8b6b2265e7e8b883a92ab3720ca9
Author: Sutou Kouhei <[email protected]>
AuthorDate: Sat Jan 31 10:36:44 2026 +0900
GH-49074: [Ruby] Add support for writing interval arrays (#49075)
### Rationale for this change
There are year month/day time/month day nano variants.
### What changes are included in this PR?
* Add `ArrowFormat::IntervalType#to_flatbuffers`
### Are these changes tested?
Yes.
### Are there any user-facing changes?
Yes.
* GitHub Issue: #49074
Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
---
ruby/red-arrow-format/lib/arrow-format/readable.rb | 6 +-
ruby/red-arrow-format/lib/arrow-format/type.rb | 29 +++++++++
ruby/red-arrow-format/test/test-writer.rb | 71 ++++++++++++++++++++++
3 files changed, 103 insertions(+), 3 deletions(-)
diff --git a/ruby/red-arrow-format/lib/arrow-format/readable.rb
b/ruby/red-arrow-format/lib/arrow-format/readable.rb
index 9cf1beecbe..867a54c17b 100644
--- a/ruby/red-arrow-format/lib/arrow-format/readable.rb
+++ b/ruby/red-arrow-format/lib/arrow-format/readable.rb
@@ -78,11 +78,11 @@ module ArrowFormat
when FB::Interval
case fb_type.unit
when FB::IntervalUnit::YEAR_MONTH
- type = YearMonthIntervalType.new
+ type = YearMonthIntervalType.singleton
when FB::IntervalUnit::DAY_TIME
- type = DayTimeIntervalType.new
+ type = DayTimeIntervalType.singleton
when FB::IntervalUnit::MONTH_DAY_NANO
- type = MonthDayNanoIntervalType.new
+ type = MonthDayNanoIntervalType.singleton
end
when FB::Duration
unit = fb_type.unit.name.downcase.to_sym
diff --git a/ruby/red-arrow-format/lib/arrow-format/type.rb
b/ruby/red-arrow-format/lib/arrow-format/type.rb
index fd7582a776..9ba8cae710 100644
--- a/ruby/red-arrow-format/lib/arrow-format/type.rb
+++ b/ruby/red-arrow-format/lib/arrow-format/type.rb
@@ -458,9 +458,30 @@ module ArrowFormat
end
class IntervalType < TemporalType
+ class << self
+ def singleton
+ @singleton ||= new
+ end
+ end
+
+ attr_reader :unit
+ def initialize(unit)
+ super()
+ @unit = unit
+ end
+
+ def to_flatbuffers
+ fb_type = FB::Interval::Data.new
+ fb_type.unit = FB::IntervalUnit.try_convert(@unit.to_s.upcase)
+ fb_type
+ end
end
class YearMonthIntervalType < IntervalType
+ def initialize
+ super(:year_month)
+ end
+
def name
"YearMonthInterval"
end
@@ -471,6 +492,10 @@ module ArrowFormat
end
class DayTimeIntervalType < IntervalType
+ def initialize
+ super(:day_time)
+ end
+
def name
"DayTimeInterval"
end
@@ -481,6 +506,10 @@ module ArrowFormat
end
class MonthDayNanoIntervalType < IntervalType
+ def initialize
+ super(:month_day_nano)
+ end
+
def name
"MonthDayNanoInterval"
end
diff --git a/ruby/red-arrow-format/test/test-writer.rb
b/ruby/red-arrow-format/test/test-writer.rb
index c440bc4a59..841194ff51 100644
--- a/ruby/red-arrow-format/test/test-writer.rb
+++ b/ruby/red-arrow-format/test/test-writer.rb
@@ -61,6 +61,12 @@ module WriterTests
when Arrow::TimestampDataType
ArrowFormat::TimestampType.new(convert_time_unit(red_arrow_type.unit),
red_arrow_type.time_zone&.identifier)
+ when Arrow::MonthIntervalDataType
+ ArrowFormat::YearMonthIntervalType.singleton
+ when Arrow::DayTimeIntervalDataType
+ ArrowFormat::DayTimeIntervalType.singleton
+ when Arrow::MonthDayNanoIntervalDataType
+ ArrowFormat::MonthDayNanoIntervalType.singleton
when Arrow::BinaryDataType
ArrowFormat::BinaryType.singleton
when Arrow::LargeBinaryDataType
@@ -523,6 +529,71 @@ module WriterTests
end
end
+ sub_test_case("YearMonthInterval") do
+ def build_array
+ Arrow::MonthIntervalArray.new([0, nil, 100])
+ end
+
+ def test_write
+ assert_equal([0, nil, 100],
+ @values)
+ end
+ end
+
+ sub_test_case("DayTimeInterval") do
+ def build_array
+ Arrow::DayTimeIntervalArray.new([
+ {day: 1, millisecond: 100},
+ nil,
+ {day: 3, millisecond: 300},
+ ])
+ end
+
+ def test_write
+ assert_equal([
+ {day: 1, millisecond: 100},
+ nil,
+ {day: 3, millisecond: 300},
+ ],
+ @values)
+ end
+ end
+
+ sub_test_case("MonthDayNanoInterval") do
+ def build_array
+ Arrow::MonthDayNanoIntervalArray.new([
+ {
+ month: 1,
+ day: 1,
+ nanosecond: 100,
+ },
+ nil,
+ {
+ month: 3,
+ day: 3,
+ nanosecond: 300,
+ },
+ ])
+ end
+
+ def test_write
+ assert_equal([
+ {
+ month: 1,
+ day: 1,
+ nanosecond: 100,
+ },
+ nil,
+ {
+ month: 3,
+ day: 3,
+ nanosecond: 300,
+ },
+ ],
+ @values)
+ end
+ end
+
sub_test_case("Binary") do
def build_array
Arrow::BinaryArray.new(["Hello".b, nil, "World".b])