Skip to content

Commit 4ebb2bf

Browse files
committed
GH-50436: [Ruby] Add ArrowFormat::Timestamp.new(unit, values)
1 parent 6166ef5 commit 4ebb2bf

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,23 @@ def build_array(...)
548548
end
549549

550550
class TimestampType < TemporalType
551+
class << self
552+
def try_convert(value)
553+
case value
554+
when Symbol
555+
unit = value
556+
new(unit, nil)
557+
when Array
558+
unit, time_zone = value
559+
new(unit, time_zone)
560+
when self
561+
value
562+
else
563+
nil
564+
end
565+
end
566+
end
567+
551568
attr_reader :unit
552569
attr_reader :time_zone
553570
def initialize(unit, time_zone)
@@ -564,6 +581,10 @@ def buffer_type
564581
:s64
565582
end
566583

584+
def pack_template
585+
"q"
586+
end
587+
567588
def build_array(...)
568589
TimestampArray.new(self, ...)
569590
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 TestTimestampArray < Test::Unit::TestCase
19+
def setup
20+
@timestamp_2019_11_17_15_09_11 = 1574003351
21+
@timestamp_2025_12_16_05_33_58 = 1765863238
22+
end
23+
24+
sub_test_case("#initialize") do
25+
def test_no_null
26+
values = [
27+
@timestamp_2019_11_17_15_09_11,
28+
@timestamp_2025_12_16_05_33_58,
29+
]
30+
assert_equal(values,
31+
ArrowFormat::TimestampArray.new(:second, values).to_a)
32+
end
33+
34+
def test_mixed
35+
values = [
36+
@timestamp_2019_11_17_15_09_11,
37+
nil,
38+
@timestamp_2025_12_16_05_33_58,
39+
]
40+
assert_equal(values,
41+
ArrowFormat::TimestampArray.new(:second, values).to_a)
42+
end
43+
end
44+
45+
sub_test_case("#==") do
46+
def test_no_slice
47+
values = [
48+
@timestamp_2019_11_17_15_09_11,
49+
nil,
50+
@timestamp_2025_12_16_05_33_58,
51+
]
52+
array1 = ArrowFormat::TimestampArray.new(:second, values)
53+
array2 = ArrowFormat::TimestampArray.new(:second, values)
54+
assert_equal(array1, array2)
55+
end
56+
57+
def test_sliced
58+
values = [
59+
@timestamp_2019_11_17_15_09_11,
60+
nil,
61+
@timestamp_2025_12_16_05_33_58,
62+
]
63+
array1 = ArrowFormat::TimestampArray.new(:second, values)
64+
array2 = ArrowFormat::TimestampArray.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+
@timestamp_2019_11_17_15_09_11,
71+
nil,
72+
@timestamp_2025_12_16_05_33_58,
73+
]
74+
array1 = ArrowFormat::TimestampArray.new(:second, values)
75+
array2 = ArrowFormat::TimestampArray.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)