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 97b867856b GH-50438: Add ArrowFormat::DurationArray.new(unit, values) 
(#50447)
97b867856b is described below

commit 97b867856baef4f9d6778eadbe660a48a1adff8c
Author: Aaditya Srinivasan <[email protected]>
AuthorDate: Fri Jul 10 10:07:44 2026 +0530

    GH-50438: Add ArrowFormat::DurationArray.new(unit, values) (#50447)
    
    ### Rationale for this change
    
    Building a duration Arrow array from Ruby objects is convenient.
    
    ### What changes are included in this PR?
    
    Accept `ArrowFormat::DurationArray.new(unit, values)`.
    
    ### Are these changes tested?
    
    Yes.
    
    Added tests for:
    
    * initialization
    * mixed values with `nil`
    * equality
    * sliced arrays
    
    ### Are there any user-facing changes?
    
    Yes.
    
    * GitHub Issue: #50438
    
    Authored-by: Aaditya Srinivasan <[email protected]>
    Signed-off-by: Sutou Kouhei <[email protected]>
---
 ruby/red-arrow-format/lib/arrow-format/array.rb   |  5 +++
 ruby/red-arrow-format/lib/arrow-format/type.rb    | 18 ++++++++
 ruby/red-arrow-format/test/test-duration-array.rb | 55 +++++++++++++++++++++++
 3 files changed, 78 insertions(+)

diff --git a/ruby/red-arrow-format/lib/arrow-format/array.rb 
b/ruby/red-arrow-format/lib/arrow-format/array.rb
index 0ff88d23ae..0b42a692ec 100644
--- a/ruby/red-arrow-format/lib/arrow-format/array.rb
+++ b/ruby/red-arrow-format/lib/arrow-format/array.rb
@@ -625,6 +625,11 @@ module ArrowFormat
   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 b17cdb8e8e..d9e427a0d5 100644
--- a/ruby/red-arrow-format/lib/arrow-format/type.rb
+++ b/ruby/red-arrow-format/lib/arrow-format/type.rb
@@ -696,6 +696,20 @@ module ArrowFormat
   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()
@@ -710,6 +724,10 @@ module ArrowFormat
       :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 0000000000..92362c2e4d
--- /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

Reply via email to