Skip to content

Commit 2ca9a2b

Browse files
committed
GH-48579: [Ruby] Add support for reading duration array
1 parent b3e2e08 commit 2ca9a2b

File tree

10 files changed

+255
-0
lines changed

10 files changed

+255
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ def to_a
189189
end
190190
end
191191

192+
class DurationArray < TemporalArray
193+
def to_a
194+
apply_validity(@values_buffer.values(:s64, 0, @size))
195+
end
196+
end
197+
192198
class VariableSizeBinaryLayoutArray < Array
193199
def initialize(type, size, validity_buffer, offsets_buffer, values_buffer)
194200
super(type, size, validity_buffer)

ruby/red-arrow-format/lib/arrow-format/file-reader.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require_relative "org/apache/arrow/flatbuf/bool"
2727
require_relative "org/apache/arrow/flatbuf/date"
2828
require_relative "org/apache/arrow/flatbuf/date_unit"
29+
require_relative "org/apache/arrow/flatbuf/duration"
2930
require_relative "org/apache/arrow/flatbuf/fixed_size_binary"
3031
require_relative "org/apache/arrow/flatbuf/floating_point"
3132
require_relative "org/apache/arrow/flatbuf/footer"
@@ -214,6 +215,9 @@ def read_field(fb_field)
214215
when Org::Apache::Arrow::Flatbuf::Timestamp
215216
unit = fb_type.unit.name.downcase.to_sym
216217
type = TimestampType.new(unit, fb_type.timezone)
218+
when Org::Apache::Arrow::Flatbuf::Duration
219+
unit = fb_type.unit.name.downcase.to_sym
220+
type = DurationType.new(unit)
217221
when Org::Apache::Arrow::Flatbuf::List
218222
type = ListType.new(read_field(fb_field.children[0]))
219223
when Org::Apache::Arrow::Flatbuf::LargeList

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,18 @@ def build_array(size, validity_buffer, values_buffer)
315315
end
316316
end
317317

318+
class DurationType < TemporalType
319+
attr_reader :unit
320+
def initialize(unit)
321+
super("Duration")
322+
@unit = unit
323+
end
324+
325+
def build_array(size, validity_buffer, values_buffer)
326+
DurationArray.new(self, size, validity_buffer, values_buffer)
327+
end
328+
end
329+
318330
class VariableSizeBinaryType < Type
319331
end
320332

ruby/red-arrow-format/test/test-file-reader.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,88 @@ def test_type
489489
end
490490
end
491491

492+
sub_test_case("Duration(:second)") do
493+
def build_array
494+
Arrow::DurationArray.new(:second, [0, nil, 100])
495+
end
496+
497+
def test_read
498+
assert_equal([{"value" => [0, nil, 100]}],
499+
read)
500+
end
501+
end
502+
503+
sub_test_case("Duration(:second)") do
504+
def build_array
505+
Arrow::DurationArray.new(:second, [0, nil, 100])
506+
end
507+
508+
def test_read
509+
assert_equal([{"value" => [0, nil, 100]}],
510+
read)
511+
end
512+
end
513+
514+
sub_test_case("Duration(:second)") do
515+
def build_array
516+
Arrow::DurationArray.new(:second, [0, nil, 100])
517+
end
518+
519+
def test_read
520+
assert_equal([{"value" => [0, nil, 100]}],
521+
read)
522+
end
523+
524+
def test_type
525+
assert_equal(:second, type.unit)
526+
end
527+
end
528+
529+
sub_test_case("Duration(:millisecond)") do
530+
def build_array
531+
Arrow::DurationArray.new(:milli, [0, nil, 100_000])
532+
end
533+
534+
def test_read
535+
assert_equal([{"value" => [0, nil, 100_000]}],
536+
read)
537+
end
538+
539+
def test_type
540+
assert_equal(:millisecond, type.unit)
541+
end
542+
end
543+
544+
sub_test_case("Duration(:microsecond)") do
545+
def build_array
546+
Arrow::DurationArray.new(:micro, [0, nil, 100_000_000])
547+
end
548+
549+
def test_read
550+
assert_equal([{"value" => [0, nil, 100_000_000]}],
551+
read)
552+
end
553+
554+
def test_type
555+
assert_equal(:microsecond, type.unit)
556+
end
557+
end
558+
559+
sub_test_case("Duration(:nano)") do
560+
def build_array
561+
Arrow::DurationArray.new(:nano, [0, nil, 100_000_000_000])
562+
end
563+
564+
def test_read
565+
assert_equal([{"value" => [0, nil, 100_000_000_000]}],
566+
read)
567+
end
568+
569+
def test_type
570+
assert_equal(:nanosecond, type.unit)
571+
end
572+
end
573+
492574
sub_test_case("Binary") do
493575
def build_array
494576
Arrow::BinaryArray.new(["Hello".b, nil, "World".b])
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
module Arrow
19+
class DurationArrayBuilder
20+
class << self
21+
def build(data_type, values)
22+
builder = new(data_type)
23+
builder.build(values)
24+
end
25+
end
26+
end
27+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
module Arrow
19+
class DurationArray
20+
def unit
21+
@unit ||= value_data_type.unit
22+
end
23+
end
24+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
module Arrow
19+
class DurationDataType
20+
class << self
21+
# @api private
22+
def try_convert(value)
23+
case value
24+
when Symbol, Arrow::TimeUnit
25+
new(value)
26+
else
27+
super
28+
end
29+
end
30+
end
31+
end
32+
end

ruby/red-arrow/lib/arrow/libraries.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
require_relative "dense-union-data-type"
6060
require_relative "dictionary-array"
6161
require_relative "dictionary-data-type"
62+
require_relative "duration-array"
63+
require_relative "duration-array-builder"
64+
require_relative "duration-data-type"
6265
require_relative "equal-options"
6366
require_relative "expression"
6467
require_relative "field"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 DurationArrayTest < Test::Unit::TestCase
19+
test("#[]") do
20+
array = Arrow::DurationArray.new(:micro, [29])
21+
assert_equal(29, array[0])
22+
end
23+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 DurationDataTypeTest < Test::Unit::TestCase
19+
sub_test_case(".new") do
20+
test("Arrow::TimeUnit") do
21+
assert_equal("duration[ms]",
22+
Arrow::DurationDataType.new(Arrow::TimeUnit::MILLI).to_s)
23+
end
24+
25+
test("Symbol") do
26+
assert_equal("duration[ms]",
27+
Arrow::DurationDataType.new(:milli).to_s)
28+
end
29+
30+
test("unit: Arrow::TimeUnit") do
31+
data_type = Arrow::DurationDataType.new(unit: Arrow::TimeUnit::MILLI)
32+
assert_equal("duration[ms]",
33+
data_type.to_s)
34+
end
35+
36+
test("unit: Symbol") do
37+
data_type = Arrow::DurationDataType.new(unit: :milli)
38+
assert_equal("duration[ms]",
39+
data_type.to_s)
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)