From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- .../red-arrow/test/values/test-basic-arrays.rb | 295 ++++++++++++ .../test/values/test-dense-union-array.rb | 482 +++++++++++++++++++ .../ruby/red-arrow/test/values/test-list-array.rb | 532 +++++++++++++++++++++ .../ruby/red-arrow/test/values/test-map-array.rb | 433 +++++++++++++++++ .../test/values/test-sparse-union-array.rb | 473 ++++++++++++++++++ .../red-arrow/test/values/test-struct-array.rb | 482 +++++++++++++++++++ 6 files changed, 2697 insertions(+) create mode 100644 src/arrow/ruby/red-arrow/test/values/test-basic-arrays.rb create mode 100644 src/arrow/ruby/red-arrow/test/values/test-dense-union-array.rb create mode 100644 src/arrow/ruby/red-arrow/test/values/test-list-array.rb create mode 100644 src/arrow/ruby/red-arrow/test/values/test-map-array.rb create mode 100644 src/arrow/ruby/red-arrow/test/values/test-sparse-union-array.rb create mode 100644 src/arrow/ruby/red-arrow/test/values/test-struct-array.rb (limited to 'src/arrow/ruby/red-arrow/test/values') diff --git a/src/arrow/ruby/red-arrow/test/values/test-basic-arrays.rb b/src/arrow/ruby/red-arrow/test/values/test-basic-arrays.rb new file mode 100644 index 000000000..c54c7f62d --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-basic-arrays.rb @@ -0,0 +1,295 @@ +# 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. + +module ValuesBasicArraysTests + def test_null + target = build(Arrow::NullArray.new(4)) + assert_equal([nil] * 4, target.values) + end + + def test_boolean + values = [true, nil, false] + target = build(Arrow::BooleanArray.new(values)) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + -(2 ** 7), + nil, + (2 ** 7) - 1, + ] + target = build(Arrow::Int8Array.new(values)) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + 0, + nil, + (2 ** 8) - 1, + ] + target = build(Arrow::UInt8Array.new(values)) + assert_equal(values, target.values) + end + + def test_int16 + values = [ + -(2 ** 15), + nil, + (2 ** 15) - 1, + ] + target = build(Arrow::Int16Array.new(values)) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + 0, + nil, + (2 ** 16) - 1, + ] + target = build(Arrow::UInt16Array.new(values)) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + -(2 ** 31), + nil, + (2 ** 31) - 1, + ] + target = build(Arrow::Int32Array.new(values)) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + 0, + nil, + (2 ** 32) - 1, + ] + target = build(Arrow::UInt32Array.new(values)) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + -(2 ** 63), + nil, + (2 ** 63) - 1, + ] + target = build(Arrow::Int64Array.new(values)) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + 0, + nil, + (2 ** 64) - 1, + ] + target = build(Arrow::UInt64Array.new(values)) + assert_equal(values, target.values) + end + + def test_float + values = [ + -1.0, + nil, + 1.0, + ] + target = build(Arrow::FloatArray.new(values)) + assert_equal(values, target.values) + end + + def test_double + values = [ + -1.0, + nil, + 1.0, + ] + target = build(Arrow::DoubleArray.new(values)) + assert_equal(values, target.values) + end + + def test_binary + values = [ + "\x00".b, + nil, + "\xff".b, + ] + target = build(Arrow::BinaryArray.new(values)) + assert_equal(values, target.values) + end + + def test_tring + values = [ + "Ruby", + nil, + "\u3042", # U+3042 HIRAGANA LETTER A + ] + target = build(Arrow::StringArray.new(values)) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + Date.new(1960, 1, 1), + nil, + Date.new(2017, 8, 23), + ] + target = build(Arrow::Date32Array.new(values)) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + DateTime.new(1960, 1, 1, 2, 9, 30), + nil, + DateTime.new(2017, 8, 23, 14, 57, 2), + ] + target = build(Arrow::Date64Array.new(values)) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + Time.parse("1960-01-01T02:09:30Z"), + nil, + Time.parse("2017-08-23T14:57:02Z"), + ] + target = build(Arrow::TimestampArray.new(:second, values)) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + Time.parse("1960-01-01T02:09:30.123Z"), + nil, + Time.parse("2017-08-23T14:57:02.987Z"), + ] + target = build(Arrow::TimestampArray.new(:milli, values)) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + Time.parse("1960-01-01T02:09:30.123456Z"), + nil, + Time.parse("2017-08-23T14:57:02.987654Z"), + ] + target = build(Arrow::TimestampArray.new(:micro, values)) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + Time.parse("1960-01-01T02:09:30.123456789Z"), + nil, + Time.parse("2017-08-23T14:57:02.987654321Z"), + ] + target = build(Arrow::TimestampArray.new(:nano, values)) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + Arrow::Time.new(unit, 60 * 10), # 00:10:00 + nil, + Arrow::Time.new(unit, 60 * 60 * 2 + 9), # 02:00:09 + ] + target = build(Arrow::Time32Array.new(:second, values)) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + Arrow::Time.new(unit, (60 * 10) * 1000 + 123), # 00:10:00.123 + nil, + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1000 + 987), # 02:00:09.987 + ] + target = build(Arrow::Time32Array.new(:milli, values)) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + # 00:10:00.123456 + Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456), + nil, + # 02:00:09.987654 + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000 + 987_654), + ] + target = build(Arrow::Time64Array.new(:micro, values)) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + # 00:10:00.123456789 + Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789), + nil, + # 02:00:09.987654321 + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000_000 + 987_654_321), + ] + target = build(Arrow::Time64Array.new(:nano, values)) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + BigDecimal("92.92"), + nil, + BigDecimal("29.29"), + ] + data_type = Arrow::Decimal128DataType.new(8, 2) + target = build(Arrow::Decimal128Array.new(data_type, values)) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + BigDecimal("92.92"), + nil, + BigDecimal("29.29"), + ] + data_type = Arrow::Decimal256DataType.new(38, 2) + target = build(Arrow::Decimal256Array.new(data_type, values)) + assert_equal(values, target.values) + end +end + +class ValuesArrayBasicArraysTest < Test::Unit::TestCase + include ValuesBasicArraysTests + + def build(array) + array + end +end + +class ValuesChunkedArrayBasicArraysTest < Test::Unit::TestCase + include ValuesBasicArraysTests + + def build(array) + Arrow::ChunkedArray.new([array]) + end +end diff --git a/src/arrow/ruby/red-arrow/test/values/test-dense-union-array.rb b/src/arrow/ruby/red-arrow/test/values/test-dense-union-array.rb new file mode 100644 index 000000000..465ffb9e6 --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-dense-union-array.rb @@ -0,0 +1,482 @@ +# 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. + +module ValuesDenseUnionArrayTests + def build_data_type(type, type_codes) + field_description = {} + if type.is_a?(Hash) + field_description = field_description.merge(type) + else + field_description[:type] = type + end + Arrow::DenseUnionDataType.new(fields: [ + field_description.merge(name: "0"), + field_description.merge(name: "1"), + ], + type_codes: type_codes) + end + + def build_array(type, values) + type_codes = [0, 1] + data_type = build_data_type(type, type_codes) + type_ids = [] + offsets = [] + arrays = data_type.fields.collect do |field| + sub_schema = Arrow::Schema.new([field]) + sub_records = [] + values.each do |value| + next if value.nil? + next unless value.key?(field.name) + sub_records << [value[field.name]] + end + sub_record_batch = Arrow::RecordBatch.new(sub_schema, + sub_records) + sub_record_batch.columns[0].data + end + values.each do |value| + if value.key?("0") + type_id = type_codes[0] + type_ids << type_id + offsets << (type_ids.count(type_id) - 1) + elsif value.key?("1") + type_id = type_codes[1] + type_ids << type_id + offsets << (type_ids.count(type_id) - 1) + end + end + Arrow::DenseUnionArray.new(data_type, + Arrow::Int8Array.new(type_ids), + Arrow::Int32Array.new(offsets), + arrays) + end + + def test_null + values = [ + {"0" => nil}, + ] + target = build(:null, values) + assert_equal(values, target.values) + end + + def test_boolean + values = [ + {"0" => true}, + {"1" => nil}, + ] + target = build(:boolean, values) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + {"0" => -(2 ** 7)}, + {"1" => nil}, + ] + target = build(:int8, values) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + {"0" => (2 ** 8) - 1}, + {"1" => nil}, + ] + target = build(:uint8, values) + assert_equal(values, target.values) + end + + def test_int16 + values = [ + {"0" => -(2 ** 15)}, + {"1" => nil}, + ] + target = build(:int16, values) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + {"0" => (2 ** 16) - 1}, + {"1" => nil}, + ] + target = build(:uint16, values) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + {"0" => -(2 ** 31)}, + {"1" => nil}, + ] + target = build(:int32, values) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + {"0" => (2 ** 32) - 1}, + {"1" => nil}, + ] + target = build(:uint32, values) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + {"0" => -(2 ** 63)}, + {"1" => nil}, + ] + target = build(:int64, values) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + {"0" => (2 ** 64) - 1}, + {"1" => nil}, + ] + target = build(:uint64, values) + assert_equal(values, target.values) + end + + def test_float + values = [ + {"0" => -1.0}, + {"1" => nil}, + ] + target = build(:float, values) + assert_equal(values, target.values) + end + + def test_double + values = [ + {"0" => -1.0}, + {"1" => nil}, + ] + target = build(:double, values) + assert_equal(values, target.values) + end + + def test_binary + values = [ + {"0" => "\xff".b}, + {"1" => nil}, + ] + target = build(:binary, values) + assert_equal(values, target.values) + end + + def test_string + values = [ + {"0" => "Ruby"}, + {"1" => nil}, + ] + target = build(:string, values) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + {"0" => Date.new(1960, 1, 1)}, + {"1" => nil}, + ] + target = build(:date32, values) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + {"0" => DateTime.new(1960, 1, 1, 2, 9, 30)}, + {"1" => nil}, + ] + target = build(:date64, values) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + {"0" => Time.parse("1960-01-01T02:09:30Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123456Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123456789Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + # 00:10:00 + {"0" => Arrow::Time.new(unit, 60 * 10)}, + {"1" => nil}, + ] + target = build({ + type: :time32, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + # 00:10:00.123 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1000 + 123)}, + {"1" => nil}, + ] + target = build({ + type: :time32, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + # 00:10:00.123456 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456)}, + {"1" => nil}, + ] + target = build({ + type: :time64, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + # 00:10:00.123456789 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789)}, + {"1" => nil}, + ] + target = build({ + type: :time64, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + {"0" => BigDecimal("92.92")}, + {"1" => nil}, + ] + target = build({ + type: :decimal128, + precision: 8, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + {"0" => BigDecimal("92.92")}, + {"1" => nil}, + ] + target = build({ + type: :decimal256, + precision: 38, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_list + values = [ + {"0" => [true, nil, false]}, + {"1" => nil}, + ] + target = build({ + type: :list, + field: { + name: :sub_element, + type: :boolean, + }, + }, + values) + assert_equal(values, target.values) + end + + def test_struct + values = [ + {"0" => {"sub_field" => true}}, + {"1" => nil}, + {"0" => {"sub_field" => nil}}, + ] + target = build({ + type: :struct, + fields: [ + { + name: :sub_field, + type: :boolean, + }, + ], + }, + values) + assert_equal(values, target.values) + end + + def test_map + values = [ + {"0" => {"key1" => true, "key2" => nil}}, + {"1" => nil}, + ] + target = build({ + type: :map, + key: :string, + item: :boolean, + }, + values) + assert_equal(values, target.values) + end + + def test_sparse_union + omit("Need to add support for SparseUnionArrayBuilder") + values = [ + {"0" => {"field1" => true}}, + {"1" => nil}, + {"0" => {"field2" => nil}}, + ] + target = build({ + type: :sparse_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dense_union + omit("Need to add support for DenseUnionArrayBuilder") + values = [ + {"0" => {"field1" => true}}, + {"1" => nil}, + {"0" => {"field2" => nil}}, + ] + target = build({ + type: :dense_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dictionary + omit("Need to add support for DictionaryArrayBuilder") + values = [ + {"0" => "Ruby"}, + {"1" => nil}, + {"0" => "GLib"}, + ] + dictionary = Arrow::StringArray.new(["GLib", "Ruby"]) + target = build({ + type: :dictionary, + index_data_type: :int8, + dictionary: dictionary, + ordered: true, + }, + values) + assert_equal(values, target.values) + end +end + +class ValuesArrayDenseUnionArrayTest < Test::Unit::TestCase + include ValuesDenseUnionArrayTests + + def build(type, values) + build_array(type, values) + end +end + +class ValuesChunkedArrayDenseUnionArrayTest < Test::Unit::TestCase + include ValuesDenseUnionArrayTests + + def build(type, values) + Arrow::ChunkedArray.new([build_array(type, values)]) + end +end diff --git a/src/arrow/ruby/red-arrow/test/values/test-list-array.rb b/src/arrow/ruby/red-arrow/test/values/test-list-array.rb new file mode 100644 index 000000000..d2905b36b --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-list-array.rb @@ -0,0 +1,532 @@ +# 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. + +module ValuesListArrayTests + def build_data_type(type) + field_description = { + name: :element, + } + if type.is_a?(Hash) + field_description = field_description.merge(type) + else + field_description[:type] = type + end + Arrow::ListDataType.new(field: field_description) + end + + def build_array(type, values) + Arrow::ListArray.new(build_data_type(type), values) + end + + def test_null + values = [ + [nil, nil, nil], + nil, + ] + target = build(:null, values) + assert_equal(values, target.values) + end + + def test_boolean + values = [ + [true, nil, false], + nil, + ] + target = build(:boolean, values) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + [-(2 ** 7), nil, (2 ** 7) - 1], + nil, + ] + target = build(:int8, values) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + [0, nil, (2 ** 8) - 1], + nil, + ] + target = build(:uint8, values) + assert_equal(values, target.values) + end + + def test_int16 + values = [ + [-(2 ** 15), nil, (2 ** 15) - 1], + nil, + ] + target = build(:int16, values) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + [0, nil, (2 ** 16) - 1], + nil, + ] + target = build(:uint16, values) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + [-(2 ** 31), nil, (2 ** 31) - 1], + nil, + ] + target = build(:int32, values) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + [0, nil, (2 ** 32) - 1], + nil, + ] + target = build(:uint32, values) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + [-(2 ** 63), nil, (2 ** 63) - 1], + nil, + ] + target = build(:int64, values) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + [0, nil, (2 ** 64) - 1], + nil, + ] + target = build(:uint64, values) + assert_equal(values, target.values) + end + + def test_float + values = [ + [-1.0, nil, 1.0], + nil, + ] + target = build(:float, values) + assert_equal(values, target.values) + end + + def test_double + values = [ + [-1.0, nil, 1.0], + nil, + ] + target = build(:double, values) + assert_equal(values, target.values) + end + + def test_binary + values = [ + ["\x00".b, nil, "\xff".b], + nil, + ] + target = build(:binary, values) + assert_equal(values, target.values) + end + + def test_string + values = [ + [ + "Ruby", + nil, + "\u3042", # U+3042 HIRAGANA LETTER A + ], + nil, + ] + target = build(:string, values) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + [ + Date.new(1960, 1, 1), + nil, + Date.new(2017, 8, 23), + ], + nil, + ] + target = build(:date32, values) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + [ + DateTime.new(1960, 1, 1, 2, 9, 30), + nil, + DateTime.new(2017, 8, 23, 14, 57, 2), + ], + nil, + ] + target = build(:date64, values) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + [ + Time.parse("1960-01-01T02:09:30Z"), + nil, + Time.parse("2017-08-23T14:57:02Z"), + ], + nil, + ] + target = build({ + type: :timestamp, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + [ + Time.parse("1960-01-01T02:09:30.123Z"), + nil, + Time.parse("2017-08-23T14:57:02.987Z"), + ], + nil, + ] + target = build({ + type: :timestamp, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + [ + Time.parse("1960-01-01T02:09:30.123456Z"), + nil, + Time.parse("2017-08-23T14:57:02.987654Z"), + ], + nil, + ] + target = build({ + type: :timestamp, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + [ + Time.parse("1960-01-01T02:09:30.123456789Z"), + nil, + Time.parse("2017-08-23T14:57:02.987654321Z"), + ], + nil, + ] + target = build({ + type: :timestamp, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + [ + # 00:10:00 + Arrow::Time.new(unit, 60 * 10), + nil, + # 02:00:09 + Arrow::Time.new(unit, 60 * 60 * 2 + 9), + ], + nil, + ] + target = build({ + type: :time32, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + [ + # 00:10:00.123 + Arrow::Time.new(unit, (60 * 10) * 1000 + 123), + nil, + # 02:00:09.987 + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1000 + 987), + ], + nil, + ] + target = build({ + type: :time32, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + [ + # 00:10:00.123456 + Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456), + nil, + # 02:00:09.987654 + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000 + 987_654), + ], + nil, + ] + target = build({ + type: :time64, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + [ + # 00:10:00.123456789 + Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789), + nil, + # 02:00:09.987654321 + Arrow::Time.new(unit, (60 * 60 * 2 + 9) * 1_000_000_000 + 987_654_321), + ], + nil, + ] + target = build({ + type: :time64, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + [ + BigDecimal("92.92"), + nil, + BigDecimal("29.29"), + ], + nil, + ] + target = build({ + type: :decimal128, + precision: 8, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + [ + BigDecimal("92.92"), + nil, + BigDecimal("29.29"), + ], + nil, + ] + target = build({ + type: :decimal256, + precision: 38, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_list + values = [ + [ + [ + true, + nil, + ], + nil, + [ + nil, + false, + ], + ], + nil, + ] + target = build({ + type: :list, + field: { + name: :sub_element, + type: :boolean, + }, + }, + values) + assert_equal(values, target.values) + end + + def test_struct + values = [ + [ + {"field" => true}, + nil, + {"field" => nil}, + ], + nil, + ] + target = build({ + type: :struct, + fields: [ + { + name: :field, + type: :boolean, + }, + ], + }, + values) + assert_equal(values, target.values) + end + + def test_map + values = [ + [ + {"key1" => true, "key2" => nil}, + nil, + ], + nil, + ] + target = build({ + type: :map, + key: :string, + item: :boolean, + }, + values) + assert_equal(values, target.values) + end + + def test_sparse + omit("Need to add support for SparseUnionArrayBuilder") + values = [ + [ + {"field1" => true}, + nil, + {"field2" => nil}, + ], + nil, + ] + target = build({ + type: :sparse_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dense + omit("Need to add support for DenseUnionArrayBuilder") + values = [ + [ + {"field1" => true}, + nil, + {"field2" => nil}, + ], + nil, + ] + target = build({ + type: :dense_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dictionary + omit("Need to add support for DictionaryArrayBuilder") + values = [ + [ + "Ruby", + nil, + "GLib", + ], + nil, + ] + dictionary = Arrow::StringArray.new(["GLib", "Ruby"]) + target = build({ + type: :dictionary, + index_data_type: :int8, + dictionary: dictionary, + ordered: true, + }, + values) + assert_equal(values, target.values) + end +end + +class ValuesArrayListArrayTest < Test::Unit::TestCase + include ValuesListArrayTests + + def build(type, values) + build_array(type, values) + end +end + +class ValuesChunkedArrayListArrayTest < Test::Unit::TestCase + include ValuesListArrayTests + + def build(type, values) + Arrow::ChunkedArray.new([build_array(type, values)]) + end +end diff --git a/src/arrow/ruby/red-arrow/test/values/test-map-array.rb b/src/arrow/ruby/red-arrow/test/values/test-map-array.rb new file mode 100644 index 000000000..14b5bf6c3 --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-map-array.rb @@ -0,0 +1,433 @@ +# 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. + +module ValuesMapArrayTests + def build_data_type(item_type) + Arrow::MapDataType.new( + key: :string, + item: item_type + ) + end + + def build_array(item_type, values) + Arrow::MapArray.new(build_data_type(item_type), values) + end + + def test_null + values = [ + {"key1" => nil}, + nil, + ] + target = build(:null, values) + assert_equal(values, target.values) + end + + def test_boolean + values = [ + {"key1" => false, "key2" => nil}, + nil, + ] + target = build(:boolean, values) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + {"key1" => (2 ** 7) - 1, "key2" => nil}, + nil, + ] + target = build(:int8, values) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + {"key1" => (2 ** 8) - 1, "key2" => nil}, + nil, + ] + target = build(:uint8, values) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + {"key1" => (2 ** 16) - 1, "key2" => nil}, + nil, + ] + target = build(:uint16, values) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + {"key1" => -(2 ** 31), "key2" => nil}, + nil, + ] + target = build(:int32, values) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + {"key1" => (2 ** 32) - 1, "key2" => nil}, + nil, + ] + target = build(:uint32, values) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + {"key1" => -(2 ** 63), "key2" => nil}, + nil, + ] + target = build(:int64, values) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + {"key1" => (2 ** 64) - 1, "key2" => nil}, + nil, + ] + target = build(:uint64, values) + assert_equal(values, target.values) + end + + def test_float + values = [ + {"key1" => -1.0, "key2" => nil}, + nil, + ] + target = build(:float, values) + assert_equal(values, target.values) + end + + def test_double + values = [ + {"key1" => -1.0, "key2" => nil}, + nil, + ] + target = build(:double, values) + assert_equal(values, target.values) + end + + def test_binary + values = [ + {"key1" => "\xff".b, "key2" => nil}, + nil, + ] + target = build(:binary, values) + assert_equal(values, target.values) + end + + def test_string + values = [ + {"key1" => "Ruby", "key2" => nil}, + nil, + ] + target = build(:string, values) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + {"key1" => Date.new(1960, 1, 1), "key2" => nil}, + nil, + ] + target = build(:date32, values) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + {"key1" => DateTime.new(1960, 1, 1, 2, 9, 30), "key2" => nil}, + nil, + ] + target = build(:date64, values) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + {"key1" => Time.parse("1960-01-01T02:09:30Z"), "key2" => nil}, + nil, + ] + target = build({ + type: :timestamp, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + {"key1" => Time.parse("1960-01-01T02:09:30.123Z"), "key2" => nil}, + nil, + ] + target = build({ + type: :timestamp, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + {"key1" => Time.parse("1960-01-01T02:09:30.123456Z"), "key2" => nil}, + nil, + ] + target = build({ + type: :timestamp, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + {"key1" => Time.parse("1960-01-01T02:09:30.123456789Z"), "key2" => nil}, + nil, + ] + target = build({ + type: :timestamp, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + # 00:10:00 + {"key1" => Arrow::Time.new(unit, 60 * 10), "key2" => nil}, + nil, + ] + target = build({ + type: :time32, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + # 00:10:00.123 + {"key1" => Arrow::Time.new(unit, (60 * 10) * 1000 + 123), "key2" => nil}, + nil, + ] + target = build({ + type: :time32, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + # 00:10:00.123456 + {"key1" => Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456), "key2" => nil}, + nil, + ] + target = build({ + type: :time64, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + # 00:10:00.123456789 + {"key1" => Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789), "key2" => nil}, + nil, + ] + target = build({ + type: :time64, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + {"key1" => BigDecimal("92.92"), "key2" => nil}, + nil, + ] + target = build({ + type: :decimal128, + precision: 8, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + {"key1" => BigDecimal("92.92"), "key2" => nil}, + nil, + ] + target = build({ + type: :decimal256, + precision: 38, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_list + values = [ + {"key1" => [true, nil, false], "key2" => nil}, + nil, + ] + target = build({ + type: :list, + field: { + name: :sub_element, + type: :boolean, + }, + }, + values) + assert_equal(values, target.values) + end + + def test_struct + values = [ + {"key1" => {"field" => true}, "key2" => nil, "key3" => {"field" => nil}}, + nil, + ] + target = build({ + type: :struct, + fields: [ + { + name: :field, + type: :boolean, + }, + ], + }, + values) + assert_equal(values, target.values) + end + + def test_map + values = [ + {"key1" => {"sub_key1" => true, "sub_key2" => nil}, "key2" => nil}, + nil, + ] + target = build({ + type: :map, + key: :string, + item: :boolean, + }, + values) + assert_equal(values, target.values) + end + + def test_sparse_union + omit("Need to add support for SparseUnionArrayBuilder") + values = [ + {"key1" => {"field1" => true}, "key2" => nil, "key3" => {"field2" => nil}}, + nil, + ] + target = build({ + type: :sparse_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dense_union + omit("Need to add support for DenseUnionArrayBuilder") + values = [ + {"key1" => {"field1" => true}, "key2" => nil, "key3" => {"field2" => nil}}, + nil, + ] + target = build({ + type: :dense_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dictionary + omit("Need to add support for DictionaryArrayBuilder") + values = [ + {"key1" => "Ruby", "key2" => nil, "key3" => "GLib"}, + nil, + ] + dictionary = Arrow::StringArray.new(["GLib", "Ruby"]) + target = build({ + type: :dictionary, + index_data_type: :int8, + dictionary: dictionary, + ordered: true, + }, + values) + assert_equal(values, target.values) + end +end + +class ValuesArrayMapArrayTest < Test::Unit::TestCase + include ValuesMapArrayTests + + def build(item_type, values) + build_array(item_type, values) + end +end + +class ValuesChunkedArrayMapArrayTest < Test::Unit::TestCase + include ValuesMapArrayTests + + def build(item_type, values) + Arrow::ChunkedArray.new([build_array(item_type, values)]) + end +end diff --git a/src/arrow/ruby/red-arrow/test/values/test-sparse-union-array.rb b/src/arrow/ruby/red-arrow/test/values/test-sparse-union-array.rb new file mode 100644 index 000000000..909d67e61 --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-sparse-union-array.rb @@ -0,0 +1,473 @@ +# 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. + +module ValuesSparseUnionArrayTests + def build_data_type(type, type_codes) + field_description = {} + if type.is_a?(Hash) + field_description = field_description.merge(type) + else + field_description[:type] = type + end + Arrow::SparseUnionDataType.new(fields: [ + field_description.merge(name: "0"), + field_description.merge(name: "1"), + ], + type_codes: type_codes) + end + + def build_array(type, values) + type_codes = [0, 1] + data_type = build_data_type(type, type_codes) + type_ids = [] + arrays = data_type.fields.collect do |field| + sub_schema = Arrow::Schema.new([field]) + sub_records = values.collect do |value| + [value.nil? ? nil : value[field.name]] + end + sub_record_batch = Arrow::RecordBatch.new(sub_schema, + sub_records) + sub_record_batch.columns[0].data + end + values.each do |value| + if value.key?("0") + type_ids << type_codes[0] + elsif value.key?("1") + type_ids << type_codes[1] + end + end + Arrow::SparseUnionArray.new(data_type, + Arrow::Int8Array.new(type_ids), + arrays) + end + + def test_null + values = [ + {"0" => nil}, + ] + target = build(:null, values) + assert_equal(values, target.values) + end + + def test_boolean + values = [ + {"0" => true}, + {"1" => nil}, + ] + target = build(:boolean, values) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + {"0" => -(2 ** 7)}, + {"1" => nil}, + ] + target = build(:int8, values) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + {"0" => (2 ** 8) - 1}, + {"1" => nil}, + ] + target = build(:uint8, values) + assert_equal(values, target.values) + end + + def test_int16 + values = [ + {"0" => -(2 ** 15)}, + {"1" => nil}, + ] + target = build(:int16, values) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + {"0" => (2 ** 16) - 1}, + {"1" => nil}, + ] + target = build(:uint16, values) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + {"0" => -(2 ** 31)}, + {"1" => nil}, + ] + target = build(:int32, values) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + {"0" => (2 ** 32) - 1}, + {"1" => nil}, + ] + target = build(:uint32, values) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + {"0" => -(2 ** 63)}, + {"1" => nil}, + ] + target = build(:int64, values) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + {"0" => (2 ** 64) - 1}, + {"1" => nil}, + ] + target = build(:uint64, values) + assert_equal(values, target.values) + end + + def test_float + values = [ + {"0" => -1.0}, + {"1" => nil}, + ] + target = build(:float, values) + assert_equal(values, target.values) + end + + def test_double + values = [ + {"0" => -1.0}, + {"1" => nil}, + ] + target = build(:double, values) + assert_equal(values, target.values) + end + + def test_binary + values = [ + {"0" => "\xff".b}, + {"1" => nil}, + ] + target = build(:binary, values) + assert_equal(values, target.values) + end + + def test_string + values = [ + {"0" => "Ruby"}, + {"1" => nil}, + ] + target = build(:string, values) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + {"0" => Date.new(1960, 1, 1)}, + {"1" => nil}, + ] + target = build(:date32, values) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + {"0" => DateTime.new(1960, 1, 1, 2, 9, 30)}, + {"1" => nil}, + ] + target = build(:date64, values) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + {"0" => Time.parse("1960-01-01T02:09:30Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123456Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + {"0" => Time.parse("1960-01-01T02:09:30.123456789Z")}, + {"1" => nil}, + ] + target = build({ + type: :timestamp, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + # 00:10:00 + {"0" => Arrow::Time.new(unit, 60 * 10)}, + {"1" => nil}, + ] + target = build({ + type: :time32, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + # 00:10:00.123 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1000 + 123)}, + {"1" => nil}, + ] + target = build({ + type: :time32, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + # 00:10:00.123456 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456)}, + {"1" => nil}, + ] + target = build({ + type: :time64, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + # 00:10:00.123456789 + {"0" => Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789)}, + {"1" => nil}, + ] + target = build({ + type: :time64, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + {"0" => BigDecimal("92.92")}, + {"1" => nil}, + ] + target = build({ + type: :decimal128, + precision: 8, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + {"0" => BigDecimal("92.92")}, + {"1" => nil}, + ] + target = build({ + type: :decimal256, + precision: 38, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_list + values = [ + {"0" => [true, nil, false]}, + {"1" => nil}, + ] + target = build({ + type: :list, + field: { + name: :sub_element, + type: :boolean, + }, + }, + values) + assert_equal(values, target.values) + end + + def test_struct + values = [ + {"0" => {"sub_field" => true}}, + {"1" => nil}, + {"0" => {"sub_field" => nil}}, + ] + target = build({ + type: :struct, + fields: [ + { + name: :sub_field, + type: :boolean, + }, + ], + }, + values) + assert_equal(values, target.values) + end + + def test_map + values = [ + {"0" => {"key1" => true, "key2" => nil}}, + {"1" => nil}, + ] + target = build({ + type: :map, + key: :string, + item: :boolean, + }, + values) + assert_equal(values, target.values) + end + + def test_sparse_union + omit("Need to add support for SparseUnionArrayBuilder") + values = [ + {"0" => {"field1" => true}}, + {"1" => nil}, + {"0" => {"field2" => nil}}, + ] + target = build({ + type: :sparse_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dense_union + omit("Need to add support for DenseUnionArrayBuilder") + values = [ + {"0" => {"field1" => true}}, + {"1" => nil}, + {"0" => {"field2" => nil}}, + ] + target = build({ + type: :dense_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dictionary + omit("Need to add support for DictionaryArrayBuilder") + values = [ + {"0" => "Ruby"}, + {"1" => nil}, + {"0" => "GLib"}, + ] + dictionary = Arrow::StringArray.new(["GLib", "Ruby"]) + target = build({ + type: :dictionary, + index_data_type: :int8, + dictionary: dictionary, + ordered: true, + }, + values) + assert_equal(values, target.values) + end +end + +class ValuesArraySparseUnionArrayTest < Test::Unit::TestCase + include ValuesSparseUnionArrayTests + + def build(type, values) + build_array(type, values) + end +end + +class ValuesChunkedArraySparseUnionArrayTest < Test::Unit::TestCase + include ValuesSparseUnionArrayTests + + def build(type, values) + Arrow::ChunkedArray.new([build_array(type, values)]) + end +end diff --git a/src/arrow/ruby/red-arrow/test/values/test-struct-array.rb b/src/arrow/ruby/red-arrow/test/values/test-struct-array.rb new file mode 100644 index 000000000..4e3396796 --- /dev/null +++ b/src/arrow/ruby/red-arrow/test/values/test-struct-array.rb @@ -0,0 +1,482 @@ +# 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. + +module ValuesStructArrayTests + def build_data_type(type) + field_description = { + name: :field, + } + if type.is_a?(Hash) + field_description = field_description.merge(type) + else + field_description[:type] = type + end + Arrow::StructDataType.new([field_description]) + end + + def build_array(type, values) + Arrow::StructArray.new(build_data_type(type), values) + end + + def test_null + values = [ + {"field" => nil}, + nil, + ] + target = build(:null, values) + assert_equal(values, target.values) + end + + def test_boolean + values = [ + {"field" => true}, + nil, + {"field" => nil}, + ] + target = build(:boolean, values) + assert_equal(values, target.values) + end + + def test_int8 + values = [ + {"field" => -(2 ** 7)}, + nil, + {"field" => nil}, + ] + target = build(:int8, values) + assert_equal(values, target.values) + end + + def test_uint8 + values = [ + {"field" => (2 ** 8) - 1}, + nil, + {"field" => nil}, + ] + target = build(:uint8, values) + assert_equal(values, target.values) + end + + def test_int16 + values = [ + {"field" => -(2 ** 15)}, + nil, + {"field" => nil}, + ] + target = build(:int16, values) + assert_equal(values, target.values) + end + + def test_uint16 + values = [ + {"field" => (2 ** 16) - 1}, + nil, + {"field" => nil}, + ] + target = build(:uint16, values) + assert_equal(values, target.values) + end + + def test_int32 + values = [ + {"field" => -(2 ** 31)}, + nil, + {"field" => nil}, + ] + target = build(:int32, values) + assert_equal(values, target.values) + end + + def test_uint32 + values = [ + {"field" => (2 ** 32) - 1}, + nil, + {"field" => nil}, + ] + target = build(:uint32, values) + assert_equal(values, target.values) + end + + def test_int64 + values = [ + {"field" => -(2 ** 63)}, + nil, + {"field" => nil}, + ] + target = build(:int64, values) + assert_equal(values, target.values) + end + + def test_uint64 + values = [ + {"field" => (2 ** 64) - 1}, + nil, + {"field" => nil}, + ] + target = build(:uint64, values) + assert_equal(values, target.values) + end + + def test_float + values = [ + {"field" => -1.0}, + nil, + {"field" => nil}, + ] + target = build(:float, values) + assert_equal(values, target.values) + end + + def test_double + values = [ + {"field" => -1.0}, + nil, + {"field" => nil}, + ] + target = build(:double, values) + assert_equal(values, target.values) + end + + def test_binary + values = [ + {"field" => "\xff".b}, + nil, + {"field" => nil}, + ] + target = build(:binary, values) + assert_equal(values, target.values) + end + + def test_string + values = [ + {"field" => "Ruby"}, + nil, + {"field" => nil}, + ] + target = build(:string, values) + assert_equal(values, target.values) + end + + def test_date32 + values = [ + {"field" => Date.new(1960, 1, 1)}, + nil, + {"field" => nil}, + ] + target = build(:date32, values) + assert_equal(values, target.values) + end + + def test_date64 + values = [ + {"field" => DateTime.new(1960, 1, 1, 2, 9, 30)}, + nil, + {"field" => nil}, + ] + target = build(:date64, values) + assert_equal(values, target.values) + end + + def test_timestamp_second + values = [ + {"field" => Time.parse("1960-01-01T02:09:30Z")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :timestamp, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_milli + values = [ + {"field" => Time.parse("1960-01-01T02:09:30.123Z")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :timestamp, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_micro + values = [ + {"field" => Time.parse("1960-01-01T02:09:30.123456Z")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :timestamp, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_timestamp_nano + values = [ + {"field" => Time.parse("1960-01-01T02:09:30.123456789Z")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :timestamp, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_second + unit = Arrow::TimeUnit::SECOND + values = [ + # 00:10:00 + {"field" => Arrow::Time.new(unit, 60 * 10)}, + nil, + {"field" => nil}, + ] + target = build({ + type: :time32, + unit: :second, + }, + values) + assert_equal(values, target.values) + end + + def test_time32_milli + unit = Arrow::TimeUnit::MILLI + values = [ + # 00:10:00.123 + {"field" => Arrow::Time.new(unit, (60 * 10) * 1000 + 123)}, + nil, + {"field" => nil}, + ] + target = build({ + type: :time32, + unit: :milli, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_micro + unit = Arrow::TimeUnit::MICRO + values = [ + # 00:10:00.123456 + {"field" => Arrow::Time.new(unit, (60 * 10) * 1_000_000 + 123_456)}, + nil, + {"field" => nil}, + ] + target = build({ + type: :time64, + unit: :micro, + }, + values) + assert_equal(values, target.values) + end + + def test_time64_nano + unit = Arrow::TimeUnit::NANO + values = [ + # 00:10:00.123456789 + {"field" => Arrow::Time.new(unit, (60 * 10) * 1_000_000_000 + 123_456_789)}, + nil, + {"field" => nil}, + ] + target = build({ + type: :time64, + unit: :nano, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal128 + values = [ + {"field" => BigDecimal("92.92")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :decimal128, + precision: 8, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_decimal256 + values = [ + {"field" => BigDecimal("92.92")}, + nil, + {"field" => nil}, + ] + target = build({ + type: :decimal256, + precision: 38, + scale: 2, + }, + values) + assert_equal(values, target.values) + end + + def test_list + values = [ + {"field" => [true, nil, false]}, + nil, + {"field" => nil}, + ] + target = build({ + type: :list, + field: { + name: :sub_element, + type: :boolean, + }, + }, + values) + assert_equal(values, target.values) + end + + def test_struct + values = [ + {"field" => {"sub_field" => true}}, + nil, + {"field" => nil}, + {"field" => {"sub_field" => nil}}, + ] + target = build({ + type: :struct, + fields: [ + { + name: :sub_field, + type: :boolean, + }, + ], + }, + values) + assert_equal(values, target.values) + end + + def test_map + values = [ + {"field" => {"key1" => true, "key2" => nil}}, + nil, + {"field" => nil}, + ] + target = build({ + type: :map, + key: :string, + item: :boolean, + }, + values) + assert_equal(values, target.values) + end + + def test_sparse_union + omit("Need to add support for SparseUnionArrayBuilder") + values = [ + {"field" => {"field1" => true}}, + nil, + {"field" => nil}, + {"field" => {"field2" => nil}}, + ] + target = build({ + type: :sparse_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dense_union + omit("Need to add support for DenseUnionArrayBuilder") + values = [ + {"field" => {"field1" => true}}, + nil, + {"field" => nil}, + {"field" => {"field2" => nil}}, + ] + target = build({ + type: :dense_union, + fields: [ + { + name: :field1, + type: :boolean, + }, + { + name: :field2, + type: :uint8, + }, + ], + type_codes: [0, 1], + }, + values) + assert_equal(values, target.values) + end + + def test_dictionary + omit("Need to add support for DictionaryArrayBuilder") + values = [ + {"field" => "Ruby"}, + nil, + {"field" => nil}, + {"field" => "GLib"}, + ] + dictionary = Arrow::StringArray.new(["GLib", "Ruby"]) + target = build({ + type: :dictionary, + index_data_type: :int8, + dictionary: dictionary, + ordered: true, + }, + values) + assert_equal(values, target.values) + end +end + +class ValuesArrayStructArrayTest < Test::Unit::TestCase + include ValuesStructArrayTests + + def build(type, values) + build_array(type, values) + end +end + +class ValuesChunkedArrayStructArrayTest < Test::Unit::TestCase + include ValuesStructArrayTests + + def build(type, values) + Arrow::ChunkedArray.new([build_array(type, values)]) + end +end -- cgit v1.2.3