Skip to content

Commit 4ff852b

Browse files
committed
GH-50438: Add ArrowFormat::DurationArray.new(unit, values)
1 parent 98a92c2 commit 4ff852b

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ def element_size
534534
end
535535

536536
class DurationArray < TemporalArray
537+
class << self
538+
def type_class
539+
DurationType
540+
end
541+
end
537542
end
538543

539544
class VariableSizeBinaryArray < Array

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,20 @@ def build_array(...)
657657
end
658658

659659
class DurationType < TemporalType
660+
class << self
661+
def try_convert(value)
662+
case value
663+
when Symbol
664+
unit = value
665+
new(unit)
666+
when self
667+
value
668+
else
669+
nil
670+
end
671+
end
672+
end
673+
660674
attr_reader :unit
661675
def initialize(unit)
662676
super()
@@ -671,6 +685,10 @@ def buffer_type
671685
:s64
672686
end
673687

688+
def pack_template
689+
"q"
690+
end
691+
674692
def build_array(...)
675693
DurationArray.new(self, ...)
676694
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestDurationArray < Test::Unit::TestCase
19+
def setup
20+
@duration1 = 100
21+
@duration2 = 200
22+
end
23+
24+
sub_test_case("#initialize") do
25+
def test_no_null
26+
values = [
27+
@duration1,
28+
@duration2,
29+
]
30+
assert_equal(values,
31+
ArrowFormat::DurationArray.new(:second, values).to_a)
32+
end
33+
34+
def test_mixed
35+
values = [
36+
@duration1,
37+
nil,
38+
@duration2,
39+
]
40+
assert_equal(values,
41+
ArrowFormat::DurationArray.new(:second, values).to_a)
42+
end
43+
end
44+
45+
sub_test_case("#==") do
46+
def test_no_slice
47+
values = [
48+
@duration1,
49+
nil,
50+
@duration2,
51+
]
52+
array1 = ArrowFormat::DurationArray.new(:second, values)
53+
array2 = ArrowFormat::DurationArray.new(:second, values)
54+
assert_equal(array1, array2)
55+
end
56+
57+
def test_sliced
58+
values = [
59+
@duration1,
60+
nil,
61+
@duration2,
62+
]
63+
array1 = ArrowFormat::DurationArray.new(:second, values)
64+
array2 = ArrowFormat::DurationArray.new(:second, [0, *values, 0])
65+
assert_equal(array1, array2.slice(1, 3))
66+
end
67+
68+
def test_sliced_different_content
69+
values = [
70+
@duration1,
71+
nil,
72+
@duration2,
73+
]
74+
array1 = ArrowFormat::DurationArray.new(:second, values)
75+
array2 = ArrowFormat::DurationArray.new(:second, [0, 0, *values, 0])
76+
assert_not_equal(array1, array2.slice(1, 3))
77+
end
78+
end
79+
end

0 commit comments

Comments
 (0)