From 5e62b6f17b9ae194839c4fc733cfec1fe4d85a3f Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Thu, 9 Jul 2026 16:05:36 +0900 Subject: [PATCH] GH-50434: [Ruby] Add `ArrowFormat::Date{32,64}.new(values)` --- .../red-arrow-format/lib/arrow-format/type.rb | 8 +++ .../test/test-date32-array.rb | 60 +++++++++++++++++++ .../test/test-date64-array.rb | 60 +++++++++++++++++++ .../test/test-float32-array.rb | 4 +- .../test/test-float64-array.rb | 4 +- .../red-arrow-format/test/test-int16-array.rb | 4 +- .../red-arrow-format/test/test-int32-array.rb | 4 +- .../red-arrow-format/test/test-int64-array.rb | 4 +- ruby/red-arrow-format/test/test-int8-array.rb | 4 +- .../test/test-uint16-array.rb | 4 +- .../test/test-uint32-array.rb | 4 +- .../test/test-uint64-array.rb | 4 +- .../red-arrow-format/test/test-uint8-array.rb | 4 +- 13 files changed, 148 insertions(+), 20 deletions(-) create mode 100644 ruby/red-arrow-format/test/test-date32-array.rb create mode 100644 ruby/red-arrow-format/test/test-date64-array.rb diff --git a/ruby/red-arrow-format/lib/arrow-format/type.rb b/ruby/red-arrow-format/lib/arrow-format/type.rb index affd9db0243..0e57e62c321 100644 --- a/ruby/red-arrow-format/lib/arrow-format/type.rb +++ b/ruby/red-arrow-format/lib/arrow-format/type.rb @@ -417,6 +417,10 @@ def buffer_type :s32 end + def pack_template + "l" + end + def build_array(...) Date32Array.new(...) end @@ -441,6 +445,10 @@ def buffer_type :s64 end + def pack_template + "q" + end + def build_array(...) Date64Array.new(...) end diff --git a/ruby/red-arrow-format/test/test-date32-array.rb b/ruby/red-arrow-format/test/test-date32-array.rb new file mode 100644 index 00000000000..ee9b95df3ca --- /dev/null +++ b/ruby/red-arrow-format/test/test-date32-array.rb @@ -0,0 +1,60 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class TestDate32Array < Test::Unit::TestCase + def setup + @date_2017_08_28 = 17406 + @date_2025_12_09 = 20431 + end + + sub_test_case("#initialize") do + def test_no_null + values = [@date_2017_08_28, @date_2025_12_09] + assert_equal(values, + ArrowFormat::Date32Array.new(values).to_a) + end + + def test_mixed + values = [@date_2017_08_28, nil, @date_2025_12_09] + assert_equal(values, + ArrowFormat::Date32Array.new(values).to_a) + end + end + + sub_test_case("#==") do + def test_no_slice + values = [@date_2017_08_28, nil, @date_2025_12_09] + array1 = ArrowFormat::Date32Array.new(values) + array2 = ArrowFormat::Date32Array.new(values) + assert_equal(array1, array2) + end + + def test_sliced + values = [@date_2017_08_28, nil, @date_2025_12_09] + array1 = ArrowFormat::Date32Array.new(values) + array2 = ArrowFormat::Date32Array.new([0, *values, 0]) + assert_equal(array1, array2.slice(1, 3)) + end + + def test_sliced_different_content + values = [@date_2017_08_28, nil, @date_2025_12_09] + array1 = ArrowFormat::Date32Array.new(values) + array2 = ArrowFormat::Date32Array.new([0, 0, *values, 0]) + assert_not_equal(array1, array2.slice(1, 3)) + end + end +end diff --git a/ruby/red-arrow-format/test/test-date64-array.rb b/ruby/red-arrow-format/test/test-date64-array.rb new file mode 100644 index 00000000000..5dcbdfc2cd6 --- /dev/null +++ b/ruby/red-arrow-format/test/test-date64-array.rb @@ -0,0 +1,60 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class TestDate64Array < Test::Unit::TestCase + def setup + @date_2017_08_28_00_00_00 = 1503878400000 + @date_2025_12_10_00_00_00 = 1765324800000 + end + + sub_test_case("#initialize") do + def test_no_null + values = [@date_2017_08_28_00_00_00, @date_2025_12_10_00_00_00] + assert_equal(values, + ArrowFormat::Date64Array.new(values).to_a) + end + + def test_mixed + values = [@date_2017_08_28_00_00_00, nil, @date_2025_12_10_00_00_00] + assert_equal(values, + ArrowFormat::Date64Array.new(values).to_a) + end + end + + sub_test_case("#==") do + def test_no_slice + values = [@date_2017_08_28_00_00_00, nil, @date_2025_12_10_00_00_00] + array1 = ArrowFormat::Date64Array.new(values) + array2 = ArrowFormat::Date64Array.new(values) + assert_equal(array1, array2) + end + + def test_sliced + values = [@date_2017_08_28_00_00_00, nil, @date_2025_12_10_00_00_00] + array1 = ArrowFormat::Date64Array.new(values) + array2 = ArrowFormat::Date64Array.new([0, *values, 0]) + assert_equal(array1, array2.slice(1, 3)) + end + + def test_sliced_different_content + values = [@date_2017_08_28_00_00_00, nil, @date_2025_12_10_00_00_00] + array1 = ArrowFormat::Date64Array.new(values) + array2 = ArrowFormat::Date64Array.new([0, 0, *values, 0]) + assert_not_equal(array1, array2.slice(1, 3)) + end + end +end diff --git a/ruby/red-arrow-format/test/test-float32-array.rb b/ruby/red-arrow-format/test/test-float32-array.rb index 76de94a650e..1769bb2d3fb 100644 --- a/ruby/red-arrow-format/test/test-float32-array.rb +++ b/ruby/red-arrow-format/test/test-float32-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] array1 = ArrowFormat::Float32Array.new(values) - array2 = ArrowFormat::Float32Array.new([0.0] + values + [0.0]) + array2 = ArrowFormat::Float32Array.new([0.0, *values, 0.0]) assert_equal(array1, array2.slice(1, 5)) end def test_sliced_different_content values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] array1 = ArrowFormat::Float32Array.new(values) - array2 = ArrowFormat::Float32Array.new([0.0, 0.0] + values + [0.0]) + array2 = ArrowFormat::Float32Array.new([0.0, 0.0, *values, 0.0]) assert_not_equal(array1, array2.slice(1, 5)) end end diff --git a/ruby/red-arrow-format/test/test-float64-array.rb b/ruby/red-arrow-format/test/test-float64-array.rb index c3feebdc93c..22393637cfa 100644 --- a/ruby/red-arrow-format/test/test-float64-array.rb +++ b/ruby/red-arrow-format/test/test-float64-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] array1 = ArrowFormat::Float64Array.new(values) - array2 = ArrowFormat::Float64Array.new([0.0] + values + [0.0]) + array2 = ArrowFormat::Float64Array.new([0.0, *values, 0.0]) assert_equal(array1, array2.slice(1, 5)) end def test_sliced_different_content values = [-Float::INFINITY, -0.0, nil, +0.0, +Float::INFINITY] array1 = ArrowFormat::Float64Array.new(values) - array2 = ArrowFormat::Float64Array.new([0.0, 0.0] + values + [0.0]) + array2 = ArrowFormat::Float64Array.new([0.0, 0.0, *values, 0.0]) assert_not_equal(array1, array2.slice(1, 5)) end end diff --git a/ruby/red-arrow-format/test/test-int16-array.rb b/ruby/red-arrow-format/test/test-int16-array.rb index b25198482b2..f98650895af 100644 --- a/ruby/red-arrow-format/test/test-int16-array.rb +++ b/ruby/red-arrow-format/test/test-int16-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-(2 ** 15), nil, (2 ** 15) - 1] array1 = ArrowFormat::Int16Array.new(values) - array2 = ArrowFormat::Int16Array.new([0] + values + [0]) + array2 = ArrowFormat::Int16Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [-(2 ** 15), nil, (2 ** 15) - 1] array1 = ArrowFormat::Int16Array.new(values) - array2 = ArrowFormat::Int16Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::Int16Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-int32-array.rb b/ruby/red-arrow-format/test/test-int32-array.rb index 34f68e12949..343c2cc6106 100644 --- a/ruby/red-arrow-format/test/test-int32-array.rb +++ b/ruby/red-arrow-format/test/test-int32-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-(2 ** 31), nil, (2 ** 31) - 1] array1 = ArrowFormat::Int32Array.new(values) - array2 = ArrowFormat::Int32Array.new([0] + values + [0]) + array2 = ArrowFormat::Int32Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [-(2 ** 31), nil, (2 ** 31) - 1] array1 = ArrowFormat::Int32Array.new(values) - array2 = ArrowFormat::Int32Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::Int32Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-int64-array.rb b/ruby/red-arrow-format/test/test-int64-array.rb index 2d92dd60143..d265b5da718 100644 --- a/ruby/red-arrow-format/test/test-int64-array.rb +++ b/ruby/red-arrow-format/test/test-int64-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-(2 ** 63), nil, (2 ** 63) - 1] array1 = ArrowFormat::Int64Array.new(values) - array2 = ArrowFormat::Int64Array.new([0] + values + [0]) + array2 = ArrowFormat::Int64Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [-(2 ** 63), nil, (2 ** 63) - 1] array1 = ArrowFormat::Int64Array.new(values) - array2 = ArrowFormat::Int64Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::Int64Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-int8-array.rb b/ruby/red-arrow-format/test/test-int8-array.rb index a882a4e23dd..8c802a9b7f3 100644 --- a/ruby/red-arrow-format/test/test-int8-array.rb +++ b/ruby/red-arrow-format/test/test-int8-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [-(2 ** 7), nil, (2 ** 7) - 1] array1 = ArrowFormat::Int8Array.new(values) - array2 = ArrowFormat::Int8Array.new([0] + values + [0]) + array2 = ArrowFormat::Int8Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [-(2 ** 7), nil, (2 ** 7) - 1] array1 = ArrowFormat::Int8Array.new(values) - array2 = ArrowFormat::Int8Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::Int8Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-uint16-array.rb b/ruby/red-arrow-format/test/test-uint16-array.rb index c406aa23cce..736cb7494a4 100644 --- a/ruby/red-arrow-format/test/test-uint16-array.rb +++ b/ruby/red-arrow-format/test/test-uint16-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [0, nil, (2 ** 16) - 1] array1 = ArrowFormat::UInt16Array.new(values) - array2 = ArrowFormat::UInt16Array.new([0] + values + [0]) + array2 = ArrowFormat::UInt16Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [0, nil, (2 ** 16) - 1] array1 = ArrowFormat::UInt16Array.new(values) - array2 = ArrowFormat::UInt16Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::UInt16Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-uint32-array.rb b/ruby/red-arrow-format/test/test-uint32-array.rb index 685f127ab8d..ab6188d7b6e 100644 --- a/ruby/red-arrow-format/test/test-uint32-array.rb +++ b/ruby/red-arrow-format/test/test-uint32-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [0, nil, (2 ** 32) - 1] array1 = ArrowFormat::UInt32Array.new(values) - array2 = ArrowFormat::UInt32Array.new([0] + values + [0]) + array2 = ArrowFormat::UInt32Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [0, nil, (2 ** 32) - 1] array1 = ArrowFormat::UInt32Array.new(values) - array2 = ArrowFormat::UInt32Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::UInt32Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-uint64-array.rb b/ruby/red-arrow-format/test/test-uint64-array.rb index ff8acf2f549..781bc6376d1 100644 --- a/ruby/red-arrow-format/test/test-uint64-array.rb +++ b/ruby/red-arrow-format/test/test-uint64-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [0, nil, (2 ** 64) - 1] array1 = ArrowFormat::UInt64Array.new(values) - array2 = ArrowFormat::UInt64Array.new([0] + values + [0]) + array2 = ArrowFormat::UInt64Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [0, nil, (2 ** 64) - 1] array1 = ArrowFormat::UInt64Array.new(values) - array2 = ArrowFormat::UInt64Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::UInt64Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end diff --git a/ruby/red-arrow-format/test/test-uint8-array.rb b/ruby/red-arrow-format/test/test-uint8-array.rb index 87068f48501..ae33f910ef2 100644 --- a/ruby/red-arrow-format/test/test-uint8-array.rb +++ b/ruby/red-arrow-format/test/test-uint8-array.rb @@ -41,14 +41,14 @@ def test_no_slice def test_sliced values = [0, nil, (2 ** 8) - 1] array1 = ArrowFormat::UInt8Array.new(values) - array2 = ArrowFormat::UInt8Array.new([0] + values + [0]) + array2 = ArrowFormat::UInt8Array.new([0, *values, 0]) assert_equal(array1, array2.slice(1, 3)) end def test_sliced_different_content values = [0, nil, (2 ** 8) - 1] array1 = ArrowFormat::UInt8Array.new(values) - array2 = ArrowFormat::UInt8Array.new([0, 0] + values + [0]) + array2 = ArrowFormat::UInt8Array.new([0, 0, *values, 0]) assert_not_equal(array1, array2.slice(1, 3)) end end