Skip to content

Commit ac06277

Browse files
committed
GH-50438: Add ArrowFormat::DurationArray.new(unit, values)
Signed-off-by: Aaditya Srinivasan <aadityasri03@gmail.com>
1 parent 98a92c2 commit ac06277

3 files changed

Lines changed: 78 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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
sub_test_case("#initialize") do
20+
def test_no_null
21+
values = [-(2 ** 63), (2 ** 63) - 1]
22+
assert_equal(values,
23+
ArrowFormat::DurationArray.new(:second, values).to_a)
24+
end
25+
26+
def test_mixed
27+
values = [-(2 ** 63), nil, (2 ** 63) - 1]
28+
assert_equal(values,
29+
ArrowFormat::DurationArray.new(:second, values).to_a)
30+
end
31+
end
32+
33+
sub_test_case("#==") do
34+
def test_no_slice
35+
values = [-(2 ** 63), nil, (2 ** 63) - 1]
36+
array1 = ArrowFormat::DurationArray.new(:second, values)
37+
array2 = ArrowFormat::DurationArray.new(:second, values)
38+
assert_equal(array1, array2)
39+
end
40+
41+
def test_sliced
42+
values = [-(2 ** 63), nil, (2 ** 63) - 1]
43+
array1 = ArrowFormat::DurationArray.new(:second, values)
44+
array2 = ArrowFormat::DurationArray.new(:second, [0, *values, 0])
45+
assert_equal(array1, array2.slice(1, 3))
46+
end
47+
48+
def test_sliced_different_content
49+
values = [-(2 ** 63), nil, (2 ** 63) - 1]
50+
array1 = ArrowFormat::DurationArray.new(:second, values)
51+
array2 = ArrowFormat::DurationArray.new(:second, [0, 0, *values, 0])
52+
assert_not_equal(array1, array2.slice(1, 3))
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)