From ac06277268dded00ecb50966f8da75361077151e Mon Sep 17 00:00:00 2001 From: Aaditya Srinivasan Date: Thu, 9 Jul 2026 19:17:33 +0530 Subject: [PATCH] GH-50438: Add ArrowFormat::DurationArray.new(unit, values) Signed-off-by: Aaditya Srinivasan --- .../lib/arrow-format/array.rb | 5 ++ .../red-arrow-format/lib/arrow-format/type.rb | 18 ++++++ .../test/test-duration-array.rb | 55 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 ruby/red-arrow-format/test/test-duration-array.rb diff --git a/ruby/red-arrow-format/lib/arrow-format/array.rb b/ruby/red-arrow-format/lib/arrow-format/array.rb index bc79edf9eb0..1b74887c350 100644 --- a/ruby/red-arrow-format/lib/arrow-format/array.rb +++ b/ruby/red-arrow-format/lib/arrow-format/array.rb @@ -534,6 +534,11 @@ def element_size end class DurationArray < TemporalArray + class << self + def type_class + DurationType + end + end end class VariableSizeBinaryArray < Array diff --git a/ruby/red-arrow-format/lib/arrow-format/type.rb b/ruby/red-arrow-format/lib/arrow-format/type.rb index 6711f995fdb..37567a06fb6 100644 --- a/ruby/red-arrow-format/lib/arrow-format/type.rb +++ b/ruby/red-arrow-format/lib/arrow-format/type.rb @@ -657,6 +657,20 @@ def build_array(...) end class DurationType < TemporalType + class << self + def try_convert(value) + case value + when Symbol + unit = value + new(unit) + when self + value + else + nil + end + end + end + attr_reader :unit def initialize(unit) super() @@ -671,6 +685,10 @@ def buffer_type :s64 end + def pack_template + "q" + end + def build_array(...) DurationArray.new(self, ...) end diff --git a/ruby/red-arrow-format/test/test-duration-array.rb b/ruby/red-arrow-format/test/test-duration-array.rb new file mode 100644 index 00000000000..92362c2e4d8 --- /dev/null +++ b/ruby/red-arrow-format/test/test-duration-array.rb @@ -0,0 +1,55 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class TestDurationArray < Test::Unit::TestCase + sub_test_case("#initialize") do + def test_no_null + values = [-(2 ** 63), (2 ** 63) - 1] + assert_equal(values, + ArrowFormat::DurationArray.new(:second, values).to_a) + end + + def test_mixed + values = [-(2 ** 63), nil, (2 ** 63) - 1] + assert_equal(values, + ArrowFormat::DurationArray.new(:second, values).to_a) + end + end + + sub_test_case("#==") do + def test_no_slice + values = [-(2 ** 63), nil, (2 ** 63) - 1] + array1 = ArrowFormat::DurationArray.new(:second, values) + array2 = ArrowFormat::DurationArray.new(:second, values) + assert_equal(array1, array2) + end + + def test_sliced + values = [-(2 ** 63), nil, (2 ** 63) - 1] + array1 = ArrowFormat::DurationArray.new(:second, values) + array2 = ArrowFormat::DurationArray.new(:second, [0, *values, 0]) + assert_equal(array1, array2.slice(1, 3)) + end + + def test_sliced_different_content + values = [-(2 ** 63), nil, (2 ** 63) - 1] + array1 = ArrowFormat::DurationArray.new(:second, values) + array2 = ArrowFormat::DurationArray.new(:second, [0, 0, *values, 0]) + assert_not_equal(array1, array2.slice(1, 3)) + end + end +end