Skip to content

Commit 05f67ed

Browse files
committed
GH-50435: [Ruby] Add ArrowFormat::Time{32,64}.new(values)
1 parent 32513fe commit 05f67ed

4 files changed

Lines changed: 153 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def initialize(*args)
191191
type = self.class.type
192192
expected_n_args = "1 or 3"
193193
else
194-
type = args.shift
194+
type = ensure_type(args.shift)
195195
expected_n_args = "2 or 4"
196196
end
197197
args = build_data(args[0], type) if args.size == 1
@@ -259,6 +259,10 @@ def element_size
259259
IO::Buffer.size_of(@type.buffer_type)
260260
end
261261

262+
def ensure_type(type)
263+
type
264+
end
265+
262266
def build_data(data, type)
263267
n = 0
264268
validity_buffer_builder = nil
@@ -461,9 +465,29 @@ class TimeArray < TemporalArray
461465
end
462466

463467
class Time32Array < TimeArray
468+
private
469+
def ensure_type(type)
470+
case type
471+
when Symbol
472+
unit = type
473+
Time32Type.new(unit)
474+
else
475+
type
476+
end
477+
end
464478
end
465479

466480
class Time64Array < TimeArray
481+
private
482+
def ensure_type(type)
483+
case type
484+
when Symbol
485+
unit = type
486+
Time64Type.new(unit)
487+
else
488+
type
489+
end
490+
end
467491
end
468492

469493
class TimestampArray < TemporalArray

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ def buffer_type
488488
:s32
489489
end
490490

491+
def pack_template
492+
"l"
493+
end
494+
491495
def build_array(...)
492496
Time32Array.new(self, ...)
493497
end
@@ -506,6 +510,10 @@ def buffer_type
506510
:s64
507511
end
508512

513+
def pack_template
514+
"q"
515+
end
516+
509517
def build_array(...)
510518
Time64Array.new(self, ...)
511519
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 TestTime32Array < Test::Unit::TestCase
19+
def setup
20+
@time_00_00_10 = 10
21+
@time_00_01_10 = 60 + 10
22+
end
23+
24+
sub_test_case("#initialize") do
25+
def test_no_null
26+
values = [@time_00_00_10, @time_00_01_10]
27+
assert_equal(values,
28+
ArrowFormat::Time32Array.new(:second, values).to_a)
29+
end
30+
31+
def test_mixed
32+
values = [@time_00_00_10, nil, @time_00_01_10]
33+
assert_equal(values,
34+
ArrowFormat::Time32Array.new(:second, values).to_a)
35+
end
36+
end
37+
38+
sub_test_case("#==") do
39+
def test_no_slice
40+
values = [@time_00_00_10, nil, @time_00_01_10]
41+
array1 = ArrowFormat::Time32Array.new(:second, values)
42+
array2 = ArrowFormat::Time32Array.new(:second, values)
43+
assert_equal(array1, array2)
44+
end
45+
46+
def test_sliced
47+
values = [@time_00_00_10, nil, @time_00_01_10]
48+
array1 = ArrowFormat::Time32Array.new(:second, values)
49+
array2 = ArrowFormat::Time32Array.new(:second, [0, *values, 0])
50+
assert_equal(array1, array2.slice(1, 3))
51+
end
52+
53+
def test_sliced_different_content
54+
values = [@time_00_00_10, nil, @time_00_01_10]
55+
array1 = ArrowFormat::Time32Array.new(:second, values)
56+
array2 = ArrowFormat::Time32Array.new(:second, [0, 0, *values, 0])
57+
assert_not_equal(array1, array2.slice(1, 3))
58+
end
59+
end
60+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 TestTime64Array < Test::Unit::TestCase
19+
def setup
20+
@time_00_00_10_000_000 = 10 * 1_000_000
21+
@time_00_01_10_000_000 = (60 + 10) * 1_000_000
22+
end
23+
24+
sub_test_case("#initialize") do
25+
def test_no_null
26+
values = [@time_00_00_10_000_000, @time_00_01_10_000_000]
27+
assert_equal(values,
28+
ArrowFormat::Time64Array.new(:microsecond, values).to_a)
29+
end
30+
31+
def test_mixed
32+
values = [@time_00_00_10_000_000, nil, @time_00_01_10_000_000]
33+
assert_equal(values,
34+
ArrowFormat::Time64Array.new(:microsecond, values).to_a)
35+
end
36+
end
37+
38+
sub_test_case("#==") do
39+
def test_no_slice
40+
values = [@time_00_00_10_000_000, nil, @time_00_01_10_000_000]
41+
array1 = ArrowFormat::Time64Array.new(:microsecond, values)
42+
array2 = ArrowFormat::Time64Array.new(:microsecond, values)
43+
assert_equal(array1, array2)
44+
end
45+
46+
def test_sliced
47+
values = [@time_00_00_10_000_000, nil, @time_00_01_10_000_000]
48+
array1 = ArrowFormat::Time64Array.new(:microsecond, values)
49+
array2 = ArrowFormat::Time64Array.new(:microsecond, [0, *values, 0])
50+
assert_equal(array1, array2.slice(1, 3))
51+
end
52+
53+
def test_sliced_different_content
54+
values = [@time_00_00_10_000_000, nil, @time_00_01_10_000_000]
55+
array1 = ArrowFormat::Time64Array.new(:microsecond, values)
56+
array2 = ArrowFormat::Time64Array.new(:microsecond, [0, 0, *values, 0])
57+
assert_not_equal(array1, array2.slice(1, 3))
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)