diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/arrow/c_glib/test | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/arrow/c_glib/test')
232 files changed, 17752 insertions, 0 deletions
diff --git a/src/arrow/c_glib/test/dataset/test-file-format.rb b/src/arrow/c_glib/test/dataset/test-file-format.rb new file mode 100644 index 000000000..76ffede94 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-file-format.rb @@ -0,0 +1,34 @@ +# 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 TestDatasetFileFormat < Test::Unit::TestCase + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + end + + def test_csv + assert_equal("csv", ArrowDataset::CSVFileFormat.new.type_name) + end + + def test_ipc + assert_equal("ipc", ArrowDataset::IPCFileFormat.new.type_name) + end + + def test_parquet + assert_equal("parquet", ArrowDataset::ParquetFileFormat.new.type_name) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-file-system-dataset-factory.rb b/src/arrow/c_glib/test/dataset/test-file-system-dataset-factory.rb new file mode 100644 index 000000000..bca9e7241 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-file-system-dataset-factory.rb @@ -0,0 +1,73 @@ +# 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 TestDatasetFileSystemDatasetFactory < Test::Unit::TestCase + include Helper::Buildable + include Helper::Writable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + Dir.mktmpdir do |tmpdir| + @dir = tmpdir + @format = ArrowDataset::IPCFileFormat.new + @path1 = File.join(@dir, "table1.arrow") + @table1 = build_table(visible: [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true, false, true]), + ], + point: [ + build_int32_array([1, 2, 3]), + build_int32_array([-1, -2, -3, -4]), + ]) + write_table(@table1, @path1) + @path2 = File.join(@dir, "table2.arrow") + @table2 = build_table(visible: [ + build_boolean_array([false, true]), + build_boolean_array([true]), + ], + point: [ + build_int32_array([10]), + build_int32_array([-10, -20]), + ]) + write_table(@table2, @path2) + yield + end + end + + def test_file_system + factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + factory.file_system = Arrow::LocalFileSystem.new + factory.add_path(File.expand_path(@path1)) + dataset = factory.finish + assert_equal(@table1, dataset.to_table) + end + + def test_file_system_uri + factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + factory.file_system_uri = build_file_uri(@path1) + dataset = factory.finish + assert_equal(@table1, dataset.to_table) + end + + def test_directory + factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + factory.file_system_uri = build_file_uri(@dir) + dataset = factory.finish + assert_equal(@table1.concatenate([@table2]), + dataset.to_table) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-file-system-dataset.rb b/src/arrow/c_glib/test/dataset/test-file-system-dataset.rb new file mode 100644 index 000000000..1aef38fcc --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-file-system-dataset.rb @@ -0,0 +1,89 @@ +# 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 TestDatasetFileSystemDataset < Test::Unit::TestCase + include Helper::Buildable + include Helper::Readable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + Dir.mktmpdir do |tmpdir| + @dir = tmpdir + @format = ArrowDataset::IPCFileFormat.new + @factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + @file_system = Arrow::LocalFileSystem.new + @factory.file_system = @file_system + partitioning_schema = build_schema(label: Arrow::StringDataType.new) + @partitioning = + ArrowDataset::DirectoryPartitioning.new(partitioning_schema) + @factory.partitioning = @partitioning + yield + end + end + + def test_type_name + dataset = @factory.finish + assert_equal("filesystem", dataset.type_name) + end + + def test_format + dataset = @factory.finish + assert_equal(@format, dataset.format) + end + + def test_file_system + dataset = @factory.finish + assert_equal(@file_system, dataset.file_system) + end + + def test_partitioning + dataset = @factory.finish + assert_equal(@partitioning, dataset.partitioning) + end + + def test_read_write + table = build_table(label: build_string_array(["a", "a", "b", "c"]), + count: build_int32_array([1, 10, 2, 3])) + table_reader = Arrow::TableBatchReader.new(table) + scanner_builder = ArrowDataset::ScannerBuilder.new(table_reader) + scanner_builder.use_async = true + scanner = scanner_builder.finish + options = ArrowDataset::FileSystemDatasetWriteOptions.new + options.file_write_options = @format.default_write_options + options.file_system = Arrow::LocalFileSystem.new + options.base_dir = @dir + options.base_name_template = "{i}.arrow" + options.partitioning = @partitioning + ArrowDataset::FileSystemDataset.write_scanner(scanner, options) + Find.find(@dir) do |path| + @factory.add_path(path) if File.file?(path) + end + @factory.partition_base_dir = @dir + dataset = @factory.finish + assert_equal(build_table(count: [ + build_int32_array([1, 10]), + build_int32_array([2]), + build_int32_array([3]), + ], + label: [ + build_string_array(["a", "a"]), + build_string_array(["b"]), + build_string_array(["c"]), + ]), + dataset.to_table) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-file-writer.rb b/src/arrow/c_glib/test/dataset/test-file-writer.rb new file mode 100644 index 000000000..5b25d6044 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-file-writer.rb @@ -0,0 +1,65 @@ +# 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 TestDatasetFileWriter < Test::Unit::TestCase + include Helper::Buildable + include Helper::Readable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + Dir.mktmpdir do |tmpdir| + @dir = tmpdir + @format = ArrowDataset::IPCFileFormat.new + @file_system = Arrow::LocalFileSystem.new + @path = File.join(@dir, "data.arrow") + @output = @file_system.open_output_stream(@path) + @schema = build_schema(visible: Arrow::BooleanDataType.new, + point: Arrow::UInt8DataType.new) + @writer = @format.open_writer(@output, + @file_system, + @path, + @schema, + @format.default_write_options) + yield + end + end + + def test_write_record_batch + record_batch = build_record_batch( + visible: build_boolean_array([true, false, true]), + point: build_uint8_array([1, 2, 3])) + @writer.write_record_batch(record_batch) + @writer.finish + @output.close + read_table(@path) do |written_table| + assert_equal(Arrow::Table.new(record_batch.schema, + [record_batch]), + written_table) + end + end + + def test_write_record_batch_reader + table = build_table(visible: build_boolean_array([true, false, true]), + point: build_uint8_array([1, 2, 3])) + @writer.write_record_batch_reader(Arrow::TableBatchReader.new(table)) + @writer.finish + @output.close + read_table(@path) do |written_table| + assert_equal(table, written_table) + end + end +end diff --git a/src/arrow/c_glib/test/dataset/test-partitioning-options.rb b/src/arrow/c_glib/test/dataset/test-partitioning-options.rb new file mode 100644 index 000000000..9ff585aa7 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-partitioning-options.rb @@ -0,0 +1,46 @@ +# 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 TestDatasetPartitioningOptions < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + @options = ArrowDataset::PartitioningOptions.new + end + + def test_infer_dictionary + assert_false(@options.infer_dictionary?) + @options.infer_dictionary = true + assert_true(@options.infer_dictionary?) + end + + def test_schema + assert_nil(@options.schema) + schema = build_schema(year: Arrow::UInt16DataType.new) + @options.schema = schema + assert_equal(schema, @options.schema) + end + + def test_segment_encoding + assert_equal(ArrowDataset::SegmentEncoding::NONE, + @options.segment_encoding) + @options.segment_encoding = :uri + assert_equal(ArrowDataset::SegmentEncoding::URI, + @options.segment_encoding) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-partitioning.rb b/src/arrow/c_glib/test/dataset/test-partitioning.rb new file mode 100644 index 000000000..2b33b1eaa --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-partitioning.rb @@ -0,0 +1,34 @@ +# 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 TestDatasetPartitioning < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + end + + def test_default + assert_equal("default", ArrowDataset::Partitioning.new.type_name) + end + + def test_directory + schema = build_schema(year: Arrow::UInt16DataType.new) + partitioning = ArrowDataset::DirectoryPartitioning.new(schema) + assert_equal("directory", partitioning.type_name) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-scanner-builder.rb b/src/arrow/c_glib/test/dataset/test-scanner-builder.rb new file mode 100644 index 000000000..5674db4c3 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-scanner-builder.rb @@ -0,0 +1,75 @@ +# 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 TestDatasetScannerBuilder < Test::Unit::TestCase + include Helper::Buildable + include Helper::Writable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + Dir.mktmpdir do |tmpdir| + path = File.join(tmpdir, "table.arrow") + @table = build_table(visible: [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true, false, true]), + ], + point: [ + build_int32_array([1, 2, 3]), + build_int32_array([-1, -2, -3, -4]), + ]) + @format = ArrowDataset::IPCFileFormat.new + write_table(@table, path) + factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + factory.file_system_uri = build_file_uri(path) + @dataset = factory.finish + @builder = @dataset.begin_scan + yield + end + end + + def test_new_record_batch_reader + reader = Arrow::TableBatchReader.new(@table) + builder = ArrowDataset::ScannerBuilder.new(reader) + scanner = builder.finish + assert_equal(@table, scanner.to_table) + end + + def test_filter + visible = Arrow::FieldExpression.new("visible") + true_scalar = Arrow::BooleanScalar.new(true) + true_datum = Arrow::ScalarDatum.new(true_scalar) + true_literal = Arrow::LiteralExpression.new(true_datum) + filter = Arrow::CallExpression.new("equal", [visible, true_literal]) + @builder.filter = filter + scanner = @builder.finish + assert_equal(build_table(visible: [ + build_boolean_array([true, true]), + build_boolean_array([true, true]), + ], + point: [ + build_int32_array([1, 3]), + build_int32_array([-2, -4]), + ]), + scanner.to_table) + end + + def test_use_async + @builder.use_async = true + scanner = @builder.finish + assert_equal(@table, scanner.to_table) + end +end diff --git a/src/arrow/c_glib/test/dataset/test-scanner.rb b/src/arrow/c_glib/test/dataset/test-scanner.rb new file mode 100644 index 000000000..f7702d490 --- /dev/null +++ b/src/arrow/c_glib/test/dataset/test-scanner.rb @@ -0,0 +1,48 @@ +# 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 TestDatasetScanner < Test::Unit::TestCase + include Helper::Buildable + include Helper::Writable + + def setup + omit("Arrow Dataset is required") unless defined?(ArrowDataset) + Dir.mktmpdir do |tmpdir| + path = File.join(tmpdir, "table.arrow") + @table = build_table(visible: [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true, false, true]), + ], + point: [ + build_int32_array([1, 2, 3]), + build_int32_array([-1, -2, -3, -4]), + ]) + @format = ArrowDataset::IPCFileFormat.new + write_table(@table, path) + factory = ArrowDataset::FileSystemDatasetFactory.new(@format) + factory.file_system_uri = build_file_uri(path) + @dataset = factory.finish + builder = @dataset.begin_scan + @scanner = builder.finish + yield + end + end + + def test_to_table + assert_equal(@table, @scanner.to_table) + end +end diff --git a/src/arrow/c_glib/test/file-system-tests.rb b/src/arrow/c_glib/test/file-system-tests.rb new file mode 100644 index 000000000..3c9db8266 --- /dev/null +++ b/src/arrow/c_glib/test/file-system-tests.rb @@ -0,0 +1,383 @@ +# 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 FileSystemTests + private def all_entries + selector = Arrow::FileSelector.new + selector.base_dir = "" + selector.recursive = true + infos = @fs.get_file_infos_selector(selector) + infos.map {|info| [info.path, info.type.nick.to_sym]}.to_h + end + + private def mkpath(path) + @fs.create_dir(path, true) + end + + private def create_file(path, content=nil) + stream = @fs.open_output_stream(path) + stream.write(content) if content + stream.close + end + + private def read_file(path) + stream = @fs.open_input_stream(path) + size = @fs.get_file_info(path).size + bytes = stream.read_bytes(size) + stream.close + bytes.to_s + end + + private def file?(path) + info = @fs.get_file_info(path) + info.file? + rescue Arrow::Error::Io + false + end + + private def directory?(path) + info = @fs.get_file_info(path) + info.dir? + rescue Arrow::Error::Io + false + end + + def test_empty + assert { all_entries.empty? } + end + + def test_create_dir + @fs.create_dir("AB/CD/EF", true) # recursive + @fs.create_dir("AB/GH", false) # non-recursive + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "AB/CD/EF" => :dir, + "AB/GH" => :dir + }, + all_entries) + end + + def test_create_dir_with_nonexistent_parent + assert_raise(Arrow::Error::Io) do + @fs.create_dir("AB/GH/IJ", false) # non-recursive, parent doesn't exist + end + assert_equal({}, + all_entries) + end + + def test_create_dir_under_file + create_file("empty_file") + assert_raise(Arrow::Error::Io) do + @fs.create_dir(File.join("empty_file", "XY"), true) + end + assert_equal({"empty_file" => :file}, + all_entries) + end + + def test_delete_dir + mkpath("AB/CD/EF") + mkpath("AB/GH/IJ") + create_file("AB/abc") + create_file("AB/CD/def") + create_file("AB/CD/EF/ghi") + + @fs.delete_dir("AB/CD") + @fs.delete_dir("AB/GH/IJ") + + assert_equal({ + "AB" => :dir, + "AB/GH" => :dir, + "AB/abc" => :file + }, + all_entries) + end + + def test_delete_dir_contents + mkpath("AB/CD/EF") + mkpath("AB/GH/IJ") + create_file("AB/abc") + create_file("AB/CD/def") + create_file("AB/CD/EF/ghi") + + @fs.delete_dir_contents("AB/CD") + @fs.delete_dir_contents("AB/GH/IJ") + + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "AB/GH" => :dir, + "AB/GH/IJ" => :dir, + "AB/abc" => :file + }, + all_entries) + end + + def test_delete_file + mkpath("AB") + create_file("AB/def") + assert { file?("AB/def") } + + @fs.delete_file("AB/def") + assert { not file?("AB/def") } + end + + def test_delete_files + mkpath("AB") + create_file("abc") + { + def: 123, + ghi: 456, + jkl: 789, + mno: 789 + }.each do |name, content| + create_file(File.join("AB", name.to_s), content.to_s) + end + + assert_equal({ + "AB" => :dir, + "AB/def" => :file, + "AB/ghi" => :file, + "AB/jkl" => :file, + "AB/mno" => :file, + "abc" => :file + }, + all_entries) + + @fs.delete_files(["abc", "AB/def"]) + assert_equal({ + "AB" => :dir, + "AB/ghi" => :file, + "AB/jkl" => :file, + "AB/mno" => :file + }, + all_entries) + end + + def test_move_file + mkpath("AB/CD") + mkpath("EF") + create_file("abc") + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "EF" => :dir, + "abc" => :file + }, + all_entries) + + @fs.move("abc", "AB/CD/ghi") + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "EF" => :dir, + "AB/CD/ghi" => :file + }, + all_entries) + end + + def move_dir_is_supported? + true + end + + def test_move_dir + omit("move_dir is not allowed") unless move_dir_is_supported? + + mkpath("AB/CD") + mkpath("EF") + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "EF" => :dir + }, + all_entries) + + @fs.move("AB", "GH") + assert_equal({ + "EF" => :dir, + "GH" => :dir, + "GH/CD" => :dir + }, + all_entries) + end + + def test_copy_file + mkpath("AB/CD") + mkpath("EF") + create_file("AB/abc", "data") + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "EF" => :dir, + "AB/abc" => :file + }, + all_entries) + + @fs.copy_file("AB/abc", "def") + assert_equal({ + "AB" => :dir, + "AB/CD" => :dir, + "EF" => :dir, + "AB/abc" => :file, + "def" => :file + }, + all_entries) + assert_equal("data", + read_file("def")) + end + + def test_get_file_info + mkpath("AB/CD") + create_file("AB/CD/ghi", "some data") + + info = @fs.get_file_info("AB") + assert_equal(Arrow::FileType::DIR, + info.type) + assert_equal("AB", + info.base_name) + assert_equal(-1, + info.size) + assert do + info.mtime > 0 + end + + info = @fs.get_file_info("AB/CD/ghi") + assert_equal(Arrow::FileType::FILE, + info.type) + assert_equal("ghi", + info.base_name) + assert_equal(9, + info.size) + assert do + info.mtime > 0 + end + end + + def test_get_file_infos_paths + mkpath("AB/CD") + create_file("AB/CD/ghi", "some data") + + infos = @fs.get_file_infos_paths(["AB", "AB/CD/ghi"]) + assert_equal({ + "AB" => -1, + "AB/CD/ghi" => 9 + }, + infos.map {|info| [info.path, info.size]}.to_h) + end + + def test_get_file_infos_selector + mkpath("AB/CD") + create_file("abc", "data") + create_file("AB/def", "some data") + create_file("AB/CD/ghi", "some other data") + + selector = Arrow::FileSelector.new + infos = @fs.get_file_infos_selector(selector) + assert_equal({ + "AB" => -1, + "abc" => 4 + }, + infos.map {|info| [info.path, info.size]}.to_h) + + selector.base_dir = "AB" + infos = @fs.get_file_infos_selector(selector) + assert_equal({ + "AB/CD" => -1, + "AB/def" => 9 + }, + infos.map {|info| [info.path, info.size]}.to_h) + end + + def test_get_file_infos_selector_with_recursion + mkpath("AB/CD") + create_file("abc", "data") + create_file("AB/def", "some data") + create_file("AB/CD/ghi", "some other data") + + selector = Arrow::FileSelector.new + selector.recursive = true + infos = @fs.get_file_infos_selector(selector) + assert_equal({ + "AB" => -1, + "AB/CD" => -1, + "AB/CD/ghi" => 15, + "AB/def" => 9, + "abc" => 4 + }, + infos.map {|info| [info.path, info.size]}.to_h) + end + + def test_open_output_stream + assert { not file?("abc") } + stream = @fs.open_output_stream("abc") + assert_equal(0, stream.tell) + stream.write("some ") + stream.write("data") + stream.close + assert { file?("abc") } + assert_equal("some data", + read_file("abc")) + + stream = @fs.open_output_stream("abc") + assert_equal(0, stream.tell) + stream.write("other data") + stream.close + assert { file?("abc") } + assert_equal("other data", + read_file("abc")) + end + + def test_open_append_stream + assert { not file?("abc") } + stream = @fs.open_append_stream("abc") + assert_equal(0, stream.tell) + stream.write("some ") + stream.close + assert { file?("abc") } + assert_equal("some ", + read_file("abc")) + + stream = @fs.open_append_stream("abc") + assert_equal(5, stream.tell) + stream.write("data") + stream.close + assert { file?("abc") } + assert_equal("some data", + read_file("abc")) + end + + def test_open_input_stream + mkpath("AB") + create_file("AB/abc", "some data") + + stream = @fs.open_input_stream("AB/abc") + bytes = stream.read_bytes(4) + assert_equal("some", + bytes.to_s) + stream.close + end + + def test_open_input_file + create_file("ab", "some data") + + stream = @fs.open_input_file("ab") + bytes = stream.read_at_bytes(5, 4) + assert_equal("data", + bytes.to_s) + stream.close + end +end diff --git a/src/arrow/c_glib/test/fixture/TestOrcFile.test1.orc b/src/arrow/c_glib/test/fixture/TestOrcFile.test1.orc Binary files differnew file mode 100644 index 000000000..4fb0beff8 --- /dev/null +++ b/src/arrow/c_glib/test/fixture/TestOrcFile.test1.orc diff --git a/src/arrow/c_glib/test/flight/test-client.rb b/src/arrow/c_glib/test/flight/test-client.rb new file mode 100644 index 000000000..f6660a4ca --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-client.rb @@ -0,0 +1,64 @@ +# 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 TestFlightClient < Test::Unit::TestCase + include Helper::Omittable + + def setup + @server = nil + omit("Arrow Flight is required") unless defined?(ArrowFlight) + omit("Unstable on Windows") if Gem.win_platform? + require_gi_bindings(3, 4, 7) + @server = Helper::FlightServer.new + host = "127.0.0.1" + location = ArrowFlight::Location.new("grpc://#{host}:0") + options = ArrowFlight::ServerOptions.new(location) + @server.listen(options) + @location = ArrowFlight::Location.new("grpc://#{host}:#{@server.port}") + end + + def teardown + return if @server.nil? + @server.shutdown + end + + def test_list_flights + client = ArrowFlight::Client.new(@location) + generator = Helper::FlightInfoGenerator.new + assert_equal([generator.page_view], + client.list_flights) + end + + sub_test_case("#do_get") do + def test_success + client = ArrowFlight::Client.new(@location) + info = client.list_flights.first + endpoint = info.endpoints.first + generator = Helper::FlightInfoGenerator.new + reader = client.do_get(endpoint.ticket) + assert_equal(generator.page_view_table, + reader.read_all) + end + + def test_error + client = ArrowFlight::Client.new(@location) + assert_raise(Arrow::Error::Invalid) do + client.do_get(ArrowFlight::Ticket.new("invalid")) + end + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-command-descriptor.rb b/src/arrow/c_glib/test/flight/test-command-descriptor.rb new file mode 100644 index 000000000..316973287 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-command-descriptor.rb @@ -0,0 +1,52 @@ +# 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 TestFlightCommandDescriptor < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_to_s + descriptor = ArrowFlight::CommandDescriptor.new("command") + assert_equal("FlightDescriptor<cmd = 'command'>", + descriptor.to_s) + end + + def test_command + command = "command" + descriptor = ArrowFlight::CommandDescriptor.new(command) + assert_equal(command, descriptor.command) + end + + sub_test_case("#==") do + def test_true + descriptor1 = ArrowFlight::CommandDescriptor.new("command") + descriptor2 = ArrowFlight::CommandDescriptor.new("command") + assert do + descriptor1 == descriptor2 + end + end + + def test_false + descriptor1 = ArrowFlight::CommandDescriptor.new("command1") + descriptor2 = ArrowFlight::CommandDescriptor.new("command2") + assert do + not (descriptor1 == descriptor2) + end + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-criteria.rb b/src/arrow/c_glib/test/flight/test-criteria.rb new file mode 100644 index 000000000..d5f60a895 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-criteria.rb @@ -0,0 +1,29 @@ +# 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 TestFlightCriteria < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_expression + expression = "expression" + criteria = ArrowFlight::Criteria.new(expression) + assert_equal(expression, + criteria.expression.to_s) + end +end diff --git a/src/arrow/c_glib/test/flight/test-endpoint.rb b/src/arrow/c_glib/test/flight/test-endpoint.rb new file mode 100644 index 000000000..06cddf001 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-endpoint.rb @@ -0,0 +1,67 @@ +# 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 TestFlightEndpoint < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_ticket + ticket = ArrowFlight::Ticket.new("data") + locations = [ + ArrowFlight::Location.new("grpc://127.0.0.1:2929"), + ArrowFlight::Location.new("grpc+tcp://127.0.0.1:12929"), + ] + endpoint = ArrowFlight::Endpoint.new(ticket, locations) + assert_equal(ticket, + endpoint.ticket) + end + + def test_locations + ticket = ArrowFlight::Ticket.new("data") + locations = [ + ArrowFlight::Location.new("grpc://127.0.0.1:2929"), + ArrowFlight::Location.new("grpc+tcp://127.0.0.1:12929"), + ] + endpoint = ArrowFlight::Endpoint.new(ticket, locations) + assert_equal(locations, + endpoint.locations) + end + + sub_test_case("#==") do + def test_true + ticket = ArrowFlight::Ticket.new("data") + location = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + endpoint1 = ArrowFlight::Endpoint.new(ticket, [location]) + endpoint2 = ArrowFlight::Endpoint.new(ticket, [location]) + assert do + endpoint1 == endpoint2 + end + end + + def test_false + ticket = ArrowFlight::Ticket.new("data") + location1 = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + location2 = ArrowFlight::Location.new("grpc://127.0.0.1:1129") + endpoint1 = ArrowFlight::Endpoint.new(ticket, [location1]) + endpoint2 = ArrowFlight::Endpoint.new(ticket, [location2]) + assert do + not (endpoint1 == endpoint2) + end + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-info.rb b/src/arrow/c_glib/test/flight/test-info.rb new file mode 100644 index 000000000..5bf0fbfad --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-info.rb @@ -0,0 +1,78 @@ +# 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 TestFlightInfo < Test::Unit::TestCase + include Helper::Writable + + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + @generator = Helper::FlightInfoGenerator.new + end + + sub_test_case("#get_schema") do + def test_with_options + info = @generator.page_view + table = @generator.page_view_table + options = Arrow::ReadOptions.new + assert_equal(table.schema, + info.get_schema(options)) + end + + def test_without_options + info = @generator.page_view + table = @generator.page_view_table + assert_equal(table.schema, + info.get_schema) + end + end + + def test_descriptor + info = @generator.page_view + assert_equal(@generator.page_view_descriptor, + info.descriptor) + end + + def test_endpoints + info = @generator.page_view + assert_equal(@generator.page_view_endpoints, + info.endpoints) + end + + def test_total_records + info = @generator.page_view + table = @generator.page_view_table + assert_equal(table.n_rows, + info.total_records) + end + + def test_total_bytes + info = @generator.page_view + table = @generator.page_view_table + output = Arrow::ResizableBuffer.new(0) + write_table(table, output, type: :stream) + assert_equal(output.size, + info.total_bytes) + end + + def test_equal + info1 = @generator.page_view + info2 = @generator.page_view + assert do + info1 == info2 + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-location.rb b/src/arrow/c_glib/test/flight/test-location.rb new file mode 100644 index 000000000..5b1679322 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-location.rb @@ -0,0 +1,40 @@ +# 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 TestFlightLocation < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_to_s + location = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + assert_equal("grpc://127.0.0.1:2929", location.to_s) + end + + def test_scheme + location = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + assert_equal("grpc", location.scheme) + end + + def test_equal + location1 = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + location2 = ArrowFlight::Location.new("grpc://127.0.0.1:2929") + assert do + location1 == location2 + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-path-descriptor.rb b/src/arrow/c_glib/test/flight/test-path-descriptor.rb new file mode 100644 index 000000000..441fc7bb0 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-path-descriptor.rb @@ -0,0 +1,52 @@ +# 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 TestFlightPathDescriptor < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_to_s + descriptor = ArrowFlight::PathDescriptor.new(["a", "b", "c"]) + assert_equal("FlightDescriptor<path = 'a/b/c'>", + descriptor.to_s) + end + + def test_paths + paths = ["a", "b", "c"] + descriptor = ArrowFlight::PathDescriptor.new(paths) + assert_equal(paths, descriptor.paths) + end + + sub_test_case("#==") do + def test_true + descriptor1 = ArrowFlight::PathDescriptor.new(["a", "b", "c"]) + descriptor2 = ArrowFlight::PathDescriptor.new(["a", "b", "c"]) + assert do + descriptor1 == descriptor2 + end + end + + def test_false + descriptor1 = ArrowFlight::PathDescriptor.new(["a", "b", "c"]) + descriptor2 = ArrowFlight::PathDescriptor.new(["A", "B", "C"]) + assert do + not (descriptor1 == descriptor2) + end + end + end +end diff --git a/src/arrow/c_glib/test/flight/test-server-options.rb b/src/arrow/c_glib/test/flight/test-server-options.rb new file mode 100644 index 000000000..93a90297e --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-server-options.rb @@ -0,0 +1,28 @@ +# 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 TestFlightServerOptions < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_location + location = ArrowFlight::Location.new("grpc://127.0.0.1:0") + options = ArrowFlight::ServerOptions.new(location) + assert_equal(location, options.location) + end +end diff --git a/src/arrow/c_glib/test/flight/test-stream-reader.rb b/src/arrow/c_glib/test/flight/test-stream-reader.rb new file mode 100644 index 000000000..f2e6229b0 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-stream-reader.rb @@ -0,0 +1,69 @@ +# 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 TestFlightStreamReader < Test::Unit::TestCase + include Helper::Omittable + + def setup + @server = nil + omit("Arrow Flight is required") unless defined?(ArrowFlight) + omit("Unstable on Windows") if Gem.win_platform? + require_gi_bindings(3, 4, 5) + @server = Helper::FlightServer.new + host = "127.0.0.1" + location = ArrowFlight::Location.new("grpc://#{host}:0") + options = ArrowFlight::ServerOptions.new(location) + @server.listen(options) + location = ArrowFlight::Location.new("grpc://#{host}:#{@server.port}") + client = ArrowFlight::Client.new(location) + @generator = Helper::FlightInfoGenerator.new + @reader = client.do_get(@generator.page_view_ticket) + end + + def teardown + return if @server.nil? + @server.shutdown + end + + def test_read_next + chunks = [] + loop do + chunk = @reader.read_next + break if chunk.nil? + chunks << chunk + end + chunks_content = chunks.collect do |chunk| + [ + chunk.data, + chunk.metadata&.data&.to_s, + ] + end + table_batch_reader = Arrow::TableBatchReader.new(@generator.page_view_table) + assert_equal([ + [ + table_batch_reader.read_next, + nil, + ], + ], + chunks_content) + end + + def test_read_all + assert_equal(@generator.page_view_table, + @reader.read_all) + end +end diff --git a/src/arrow/c_glib/test/flight/test-ticket.rb b/src/arrow/c_glib/test/flight/test-ticket.rb new file mode 100644 index 000000000..976089762 --- /dev/null +++ b/src/arrow/c_glib/test/flight/test-ticket.rb @@ -0,0 +1,47 @@ +# 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 TestFlightTicket < Test::Unit::TestCase + def setup + omit("Arrow Flight is required") unless defined?(ArrowFlight) + end + + def test_data + data = "data" + ticket = ArrowFlight::Ticket.new(data) + assert_equal(data, + ticket.data.to_s) + end + + sub_test_case("#==") do + def test_true + ticket1 = ArrowFlight::Ticket.new("data") + ticket2 = ArrowFlight::Ticket.new("data") + assert do + ticket1 == ticket2 + end + end + + def test_false + ticket1 = ArrowFlight::Ticket.new("data1") + ticket2 = ArrowFlight::Ticket.new("data2") + assert do + not (ticket1 == ticket2) + end + end + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-binary-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-binary-literal-node.rb new file mode 100644 index 000000000..fddf74830 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-binary-literal-node.rb @@ -0,0 +1,47 @@ +# 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 TestGandivaBinaryLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = "\x00\x01\x02\x03\x04" + end + + sub_test_case(".new") do + def test_string + node = Gandiva::BinaryLiteralNode.new(@value) + assert_equal(@value, node.value.to_s) + end + + def test_bytes + bytes_value = GLib::Bytes.new(@value) + node = Gandiva::BinaryLiteralNode.new(bytes_value) + assert_equal(@value, node.value.to_s) + end + end + + sub_test_case("instance methods") do + def setup + super + @node = Gandiva::BinaryLiteralNode.new(@value) + end + + def test_return_type + assert_equal(Arrow::BinaryDataType.new, @node.return_type) + end + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-boolean-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-boolean-literal-node.rb new file mode 100644 index 000000000..6e18a7621 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-boolean-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaBooleanLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = true + @node = Gandiva::BooleanLiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value?) + end + + def test_return_type + assert_equal(Arrow::BooleanDataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-boolean-node.rb b/src/arrow/c_glib/test/gandiva/test-boolean-node.rb new file mode 100644 index 000000000..a14685e4e --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-boolean-node.rb @@ -0,0 +1,38 @@ +# 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 TestGandivaBooleanNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + @field1_node = Gandiva::FieldNode.new(field1) + @field2_node = Gandiva::FieldNode.new(field2) + end + + def test_and + and_node = Gandiva::AndNode.new([@field1_node, @field2_node]) + assert_equal([@field1_node, @field2_node], + and_node.children) + end + + def test_or + or_node = Gandiva::OrNode.new([@field1_node, @field2_node]) + assert_equal([@field1_node, @field2_node], + or_node.children) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-condition.rb b/src/arrow/c_glib/test/gandiva/test-condition.rb new file mode 100644 index 000000000..51fb9f1b1 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-condition.rb @@ -0,0 +1,35 @@ +# 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 TestGandivaCondition < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + field1_node = Gandiva::FieldNode.new(field1) + field2_node = Gandiva::FieldNode.new(field2) + function_node = Gandiva::FunctionNode.new("equal", + [field1_node, field2_node], + Arrow::BooleanDataType.new) + @condition = Gandiva::Condition.new(function_node) + end + + def test_to_s + assert_equal("bool equal((int32) field1, (int32) field2)", + @condition.to_s) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-double-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-double-literal-node.rb new file mode 100644 index 000000000..27cc3aea2 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-double-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaDoubleLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 1.5 + @node = Gandiva::DoubleLiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::DoubleDataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-expression.rb b/src/arrow/c_glib/test/gandiva/test-expression.rb new file mode 100644 index 000000000..2e27d6e67 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-expression.rb @@ -0,0 +1,46 @@ +# 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 TestGandivaExpression < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + augend = Arrow::Field.new("augend", Arrow::Int32DataType.new) + addend = Arrow::Field.new("addend", Arrow::Int32DataType.new) + augend_node = Gandiva::FieldNode.new(augend) + addend_node = Gandiva::FieldNode.new(addend) + @function_node = Gandiva::FunctionNode.new("add", + [augend_node, addend_node], + Arrow::Int32DataType.new) + @sum = Arrow::Field.new("sum", Arrow::Int32DataType.new) + @expression = Gandiva::Expression.new(@function_node, @sum) + end + + def test_readers + assert_equal([ + @function_node, + @sum + ], + [ + @expression.root_node, + @expression.result_field + ]) + end + + def test_to_s + assert_equal("int32 add((int32) augend, (int32) addend)", @expression.to_s) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-field-node.rb b/src/arrow/c_glib/test/gandiva/test-field-node.rb new file mode 100644 index 000000000..905088128 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-field-node.rb @@ -0,0 +1,37 @@ +# 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 TestGandivaFieldNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @field = Arrow::Field.new("valid", Arrow::BooleanDataType.new) + @node = Gandiva::FieldNode.new(@field) + end + + def test_field + assert_equal(@field, @node.field) + end + + def test_return_type + assert_equal(@field.data_type, @node.return_type) + end + + def test_to_s + assert_equal("(bool) valid", + @node.to_s) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-filter.rb b/src/arrow/c_glib/test/gandiva/test-filter.rb new file mode 100644 index 000000000..3da777431 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-filter.rb @@ -0,0 +1,51 @@ +# 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 TestGandivaFilter < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + schema = Arrow::Schema.new([field1, field2]) + field_node1 = Gandiva::FieldNode.new(field1) + field_node2 = Gandiva::FieldNode.new(field2) + equal_function_node = + Gandiva::FunctionNode.new("equal", + [field_node1, field_node2], + Arrow::BooleanDataType.new) + condition = Gandiva::Condition.new(equal_function_node) + @filter = Gandiva::Filter.new(schema, condition) + + input_arrays = [ + build_int32_array([1, 2, 3, 4]), + build_int32_array([11, 2, 15, 4]), + ] + @record_batch = Arrow::RecordBatch.new(schema, + input_arrays[0].length, + input_arrays) + end + + def test_evaluate + selection_vector = Gandiva::UInt16SelectionVector.new(@record_batch.n_rows) + @filter.evaluate(@record_batch, selection_vector) + assert_equal(build_uint16_array([1, 3]), + selection_vector.to_array) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-float-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-float-literal-node.rb new file mode 100644 index 000000000..4a49eb374 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-float-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaFloatLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 1.5 + @node = Gandiva::FloatLiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::FloatDataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-function-node.rb b/src/arrow/c_glib/test/gandiva/test-function-node.rb new file mode 100644 index 000000000..cb4fe0a65 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-function-node.rb @@ -0,0 +1,43 @@ +# 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 TestGandivaFunctionNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + end + + def test_readers + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + field1_node = Gandiva::FieldNode.new(field1) + field2_node = Gandiva::FieldNode.new(field2) + return_type = Arrow::Int64DataType.new + function_node = Gandiva::FunctionNode.new("add", + [field1_node, field2_node], + return_type) + assert_equal([ + "add", + [field1_node, field2_node], + return_type, + ], + [ + function_node.name, + function_node.parameters, + function_node.return_type, + ]) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-function-registry.rb b/src/arrow/c_glib/test/gandiva/test-function-registry.rb new file mode 100644 index 000000000..25bac6673 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-function-registry.rb @@ -0,0 +1,45 @@ +# 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 TestGandivaFunctionRegistry < Test::Unit::TestCase + include Helper::DataType + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @registry = Gandiva::FunctionRegistry.new + end + + sub_test_case("lookup") do + def test_found + native_function = @registry.native_functions[0] + assert_equal(native_function, + @registry.lookup(native_function.signatures[0])) + end + + def test_not_found + signature = Gandiva::FunctionSignature.new("nonexistent", + [], + boolean_data_type) + assert_nil(@registry.lookup(signature)) + end + end + + def test_native_functions + assert_equal([Gandiva::NativeFunction], + @registry.native_functions.collect(&:class).uniq) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-function-signature.rb b/src/arrow/c_glib/test/gandiva/test-function-signature.rb new file mode 100644 index 000000000..ada3bbc8b --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-function-signature.rb @@ -0,0 +1,101 @@ +# 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 TestGandivaFunctionSignature < Test::Unit::TestCase + include Helper::DataType + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + parameter_types = [string_data_type, string_data_type, int32_data_type] + @to_date = Gandiva::FunctionSignature.new("to_date", + parameter_types, + date64_data_type) + end + + def test_new + signature = Gandiva::FunctionSignature.new("add", + [ + int32_data_type, + int32_data_type, + ], + int32_data_type) + assert_equal("int32 add(int32, int32)", + signature.to_s) + end + + sub_test_case("equal") do + def test_true + add_int32_1 = Gandiva::FunctionSignature.new("add", + [ + int32_data_type, + int32_data_type, + ], + int32_data_type) + add_int32_2 = Gandiva::FunctionSignature.new("add", + [ + int32_data_type, + int32_data_type, + ], + int32_data_type) + assert do + add_int32_1 == add_int32_2 + end + end + + def test_false + add_int32 = Gandiva::FunctionSignature.new("add", + [ + int32_data_type, + int32_data_type, + ], + int32_data_type) + add_int16 = Gandiva::FunctionSignature.new("add", + [ + int16_data_type, + int16_data_type, + ], + int16_data_type) + assert do + add_int32 != add_int16 + end + end + end + + def test_to_string + assert_equal("date64[ms] to_date(string, string, int32)", + @to_date.to_s) + end + + def test_get_return_type + assert_equal(date64_data_type, + @to_date.return_type) + end + + def test_get_base_name + assert_equal("to_date", + @to_date.base_name) + end + + def test_get_param_types + assert_equal([ + string_data_type, + string_data_type, + int32_data_type, + ], + @to_date.param_types) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-if-node.rb b/src/arrow/c_glib/test/gandiva/test-if-node.rb new file mode 100644 index 000000000..b00359590 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-if-node.rb @@ -0,0 +1,49 @@ +# 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 TestGandivaIfNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + @then_node = Gandiva::FieldNode.new(field1) + @else_node = Gandiva::FieldNode.new(field2) + @return_type = Arrow::Int32DataType.new + @condition_node = Gandiva::FunctionNode.new("greater_than", + [@then_node, @else_node], + @return_type) + @if_node = Gandiva::IfNode.new(@condition_node, + @then_node, + @else_node, + @return_type) + end + + def test_readers + assert_equal([ + @condition_node, + @then_node, + @else_node, + @return_type + ], + [ + @if_node.condition_node, + @if_node.then_node, + @if_node.else_node, + @if_node.return_type + ]) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-int16-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-int16-literal-node.rb new file mode 100644 index 000000000..f8e6b2684 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-int16-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaInt16LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = -(2 ** 15) + @node = Gandiva::Int16LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::Int16DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-int32-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-int32-literal-node.rb new file mode 100644 index 000000000..3d1bf588c --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-int32-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaInt32LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = -(2 ** 31) + @node = Gandiva::Int32LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::Int32DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-int64-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-int64-literal-node.rb new file mode 100644 index 000000000..b2ca3bf63 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-int64-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaInt64LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = -(2 ** 63) + @node = Gandiva::Int64LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::Int64DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-int8-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-int8-literal-node.rb new file mode 100644 index 000000000..8d917bd1b --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-int8-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaInt8LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = -(2 ** 7) + @node = Gandiva::Int8LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::Int8DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-native-function.rb b/src/arrow/c_glib/test/gandiva/test-native-function.rb new file mode 100644 index 000000000..7888f96b6 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-native-function.rb @@ -0,0 +1,132 @@ +# 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 TestGandivaNativeFunction < Test::Unit::TestCase + include Helper::DataType + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @registry = Gandiva::FunctionRegistry.new + @not = lookup("not", [boolean_data_type], boolean_data_type) + @isnull = lookup("isnull", [int8_data_type], boolean_data_type) + end + + def lookup(name, param_types, return_type) + signature = Gandiva::FunctionSignature.new(name, + param_types, + return_type) + @registry.lookup(signature) + end + + def test_signatures + assert_equal([Gandiva::FunctionSignature], + @not.signatures.collect(&:class).uniq) + end + + sub_test_case("equal") do + def test_true + assert do + @not == @registry.lookup(@not.signatures[0]) + end + end + + def test_false + assert do + @not != @isnull + end + end + end + + def test_to_string + modulo = lookup("modulo", + [int64_data_type, int64_data_type], + int64_data_type) + assert_equal(modulo.signatures.collect(&:to_s).join(", "), + modulo.to_s) + end + + sub_test_case("get_result_nullbale_type") do + def test_if_null + assert_equal(Gandiva::ResultNullableType::IF_NULL, + @not.result_nullable_type) + end + + def test_never + assert_equal(Gandiva::ResultNullableType::NEVER, + @isnull.result_nullable_type) + end + + def test_internal + to_date = lookup("to_date", + [string_data_type, string_data_type, int32_data_type], + date64_data_type) + assert_equal(Gandiva::ResultNullableType::INTERNAL, + to_date.result_nullable_type) + end + end + + sub_test_case("need_context") do + def test_need + assert do + not @not.need_context + end + end + + def test_not_need + upper = lookup("upper", + [string_data_type], + string_data_type) + assert do + upper.need_context + end + end + end + + sub_test_case("need_function_holder") do + def test_need + like = lookup("like", + [string_data_type, string_data_type], + boolean_data_type) + assert do + like.need_function_holder + end + end + + def test_not_need + assert do + not @not.need_function_holder + end + end + end + + sub_test_case("can_return_errors") do + def test_can + divide = lookup("divide", + [int8_data_type, int8_data_type], + int8_data_type) + assert do + divide.can_return_errors? + end + end + + def test_not_can + assert do + not @not.can_return_errors? + end + end + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-null-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-null-literal-node.rb new file mode 100644 index 000000000..ae14f3c15 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-null-literal-node.rb @@ -0,0 +1,38 @@ +# 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 TestGandivaNullLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + end + + def test_invalid_type + return_type = Arrow::NullDataType.new + message = + "[gandiva][null-literal-node][new] " + + "failed to create: <#{return_type}>" + assert_raise(Arrow::Error::Invalid.new(message)) do + Gandiva::NullLiteralNode.new(return_type) + end + end + + def test_return_type + return_type = Arrow::BooleanDataType.new + literal_node = Gandiva::NullLiteralNode.new(return_type) + assert_equal(return_type, literal_node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-projector.rb b/src/arrow/c_glib/test/gandiva/test-projector.rb new file mode 100644 index 000000000..308e1c3a5 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-projector.rb @@ -0,0 +1,63 @@ +# 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 TestGandivaProjector < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + @schema = Arrow::Schema.new([field1, field2]) + @field_node1 = Gandiva::FieldNode.new(field1) + @field_node2 = Gandiva::FieldNode.new(field2) + add_function_node = + Gandiva::FunctionNode.new("add", + [@field_node1, @field_node2], + Arrow::Int32DataType.new) + subtract_function_node = + Gandiva::FunctionNode.new("subtract", + [@field_node1, @field_node2], + Arrow::Int32DataType.new) + add_result = Arrow::Field.new("add_result", Arrow::Int32DataType.new) + add_expression = Gandiva::Expression.new(add_function_node, add_result) + subtract_result = Arrow::Field.new("subtract_result", + Arrow::Int32DataType.new) + subtract_expression = Gandiva::Expression.new(subtract_function_node, + subtract_result) + @projector = Gandiva::Projector.new(@schema, + [add_expression, subtract_expression]) + + input_arrays = [ + build_int32_array([1, 2, 3, 4]), + build_int32_array([11, 13, 15, 17]), + ] + @record_batch = Arrow::RecordBatch.new(@schema, + input_arrays[0].length, + input_arrays) + end + + def test_evaluate + outputs = @projector.evaluate(@record_batch) + assert_equal([ + [12, 15, 18, 21], + [-10, -11, -12, -13], + ], + outputs.collect(&:values)) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-selectable-projector.rb b/src/arrow/c_glib/test/gandiva/test-selectable-projector.rb new file mode 100644 index 000000000..47b0059a2 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-selectable-projector.rb @@ -0,0 +1,74 @@ +# 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 TestGandivaSelectableProjector < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + + field1 = Arrow::Field.new("field1", Arrow::Int32DataType.new) + field2 = Arrow::Field.new("field2", Arrow::Int32DataType.new) + @schema = Arrow::Schema.new([field1, field2]) + + input_arrays = [ + build_int32_array([1, 2, 3, 4]), + build_int32_array([11, 13, 15, 17]), + ] + @record_batch = Arrow::RecordBatch.new(@schema, + input_arrays[0].length, + input_arrays) + + @field_node1 = Gandiva::FieldNode.new(field1) + @field_node2 = Gandiva::FieldNode.new(field2) + add_function_node = + Gandiva::FunctionNode.new("add", + [@field_node1, @field_node2], + Arrow::Int32DataType.new) + subtract_function_node = + Gandiva::FunctionNode.new("subtract", + [@field_node1, @field_node2], + Arrow::Int32DataType.new) + add_result = Arrow::Field.new("add_result", Arrow::Int32DataType.new) + add_expression = Gandiva::Expression.new(add_function_node, add_result) + subtract_result = Arrow::Field.new("subtract_result", + Arrow::Int32DataType.new) + subtract_expression = Gandiva::Expression.new(subtract_function_node, + subtract_result) + @selection_vector = Gandiva::UInt16SelectionVector.new(@record_batch.n_rows) + @projector = + Gandiva::SelectableProjector.new(@schema, + [add_expression, subtract_expression], + @selection_vector.mode) + end + + def test_evaluate + two_node = Gandiva::Int32LiteralNode.new(2) + condition_node = Gandiva::FunctionNode.new("greater_than", + [@field_node1, two_node], + Arrow::BooleanDataType.new) + condition = Gandiva::Condition.new(condition_node) + filter = Gandiva::Filter.new(@schema, condition) + filter.evaluate(@record_batch, @selection_vector) + outputs = @projector.evaluate(@record_batch, @selection_vector) + assert_equal([ + [18, 21], + [-12, -13], + ], + outputs.collect(&:values)) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-selection-vector.rb b/src/arrow/c_glib/test/gandiva/test-selection-vector.rb new file mode 100644 index 000000000..ca5042c28 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-selection-vector.rb @@ -0,0 +1,42 @@ +# 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 TestGandivaSelectionVector < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + end + + def test_uint16 + selection_vector = Gandiva::UInt16SelectionVector.new(10) + assert_equal(build_uint16_array([]), + selection_vector.to_array) + end + + def test_uint32 + selection_vector = Gandiva::UInt32SelectionVector.new(10) + assert_equal(build_uint32_array([]), + selection_vector.to_array) + end + + def test_uint64 + selection_vector = Gandiva::UInt64SelectionVector.new(10) + assert_equal(build_uint64_array([]), + selection_vector.to_array) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-string-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-string-literal-node.rb new file mode 100644 index 000000000..8a397ab4d --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-string-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaStringLiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = "Hello" + @node = Gandiva::StringLiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::StringDataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-uint16-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-uint16-literal-node.rb new file mode 100644 index 000000000..971da3888 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-uint16-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaUInt16LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 2 ** 16 - 1 + @node = Gandiva::UInt16LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::UInt16DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-uint32-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-uint32-literal-node.rb new file mode 100644 index 000000000..8fcab7fef --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-uint32-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaUInt32LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 2 ** 32 - 1 + @node = Gandiva::UInt32LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::UInt32DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-uint64-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-uint64-literal-node.rb new file mode 100644 index 000000000..d5afddcd7 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-uint64-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaUInt64LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 3 + @node = Gandiva::UInt64LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::UInt64DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/gandiva/test-uint8-literal-node.rb b/src/arrow/c_glib/test/gandiva/test-uint8-literal-node.rb new file mode 100644 index 000000000..8ce91d599 --- /dev/null +++ b/src/arrow/c_glib/test/gandiva/test-uint8-literal-node.rb @@ -0,0 +1,32 @@ +# 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 TestGandivaUInt8LiteralNode < Test::Unit::TestCase + def setup + omit("Gandiva is required") unless defined?(::Gandiva) + @value = 2 ** 8 - 1 + @node = Gandiva::UInt8LiteralNode.new(@value) + end + + def test_value + assert_equal(@value, @node.value) + end + + def test_return_type + assert_equal(Arrow::UInt8DataType.new, @node.return_type) + end +end diff --git a/src/arrow/c_glib/test/helper/buildable.rb b/src/arrow/c_glib/test/helper/buildable.rb new file mode 100644 index 000000000..3a1240cfa --- /dev/null +++ b/src/arrow/c_glib/test/helper/buildable.rb @@ -0,0 +1,263 @@ +# 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 Helper + module Buildable + def build_schema(fields) + fields = fields.collect do |name, data_type| + Arrow::Field.new(name, data_type) + end + Arrow::Schema.new(fields) + end + + def build_null_array(values) + build_array(Arrow::NullArrayBuilder.new, values) + end + + def build_boolean_array(values) + build_array(Arrow::BooleanArrayBuilder.new, values) + end + + def build_int_array(values) + build_array(Arrow::IntArrayBuilder.new, values) + end + + def build_uint_array(values) + build_array(Arrow::UIntArrayBuilder.new, values) + end + + def build_int8_array(values) + build_array(Arrow::Int8ArrayBuilder.new, values) + end + + def build_uint8_array(values) + build_array(Arrow::UInt8ArrayBuilder.new, values) + end + + def build_int16_array(values) + build_array(Arrow::Int16ArrayBuilder.new, values) + end + + def build_uint16_array(values) + build_array(Arrow::UInt16ArrayBuilder.new, values) + end + + def build_int32_array(values) + build_array(Arrow::Int32ArrayBuilder.new, values) + end + + def build_uint32_array(values) + build_array(Arrow::UInt32ArrayBuilder.new, values) + end + + def build_int64_array(values) + build_array(Arrow::Int64ArrayBuilder.new, values) + end + + def build_uint64_array(values) + build_array(Arrow::UInt64ArrayBuilder.new, values) + end + + def build_float_array(values) + build_array(Arrow::FloatArrayBuilder.new, values) + end + + def build_double_array(values) + build_array(Arrow::DoubleArrayBuilder.new, values) + end + + def build_date32_array(values) + build_array(Arrow::Date32ArrayBuilder.new, values) + end + + def build_date64_array(values) + build_array(Arrow::Date64ArrayBuilder.new, values) + end + + def build_timestamp_array(unit, values) + data_type = Arrow::TimestampDataType.new(unit) + build_array(Arrow::TimestampArrayBuilder.new(data_type), + values) + end + + def build_time32_array(unit, values) + build_array(Arrow::Time32ArrayBuilder.new(Arrow::Time32DataType.new(unit)), + values) + end + + def build_time64_array(unit, values) + build_array(Arrow::Time64ArrayBuilder.new(Arrow::Time64DataType.new(unit)), + values) + end + + def build_binary_array(values) + build_array(Arrow::BinaryArrayBuilder.new, values) + end + + def build_large_binary_array(values) + build_array(Arrow::LargeBinaryArrayBuilder.new, values) + end + + def build_fixed_size_binary_array(data_type, values) + build_array(Arrow::FixedSizeBinaryArrayBuilder.new(data_type), + values) + end + + def build_string_array(values) + build_array(Arrow::StringArrayBuilder.new, values) + end + + def build_large_string_array(values) + build_array(Arrow::LargeStringArrayBuilder.new, values) + end + + def build_decimal128_array(value_data_type, values) + values = values.collect do |value| + case value + when String + Arrow::Decimal128.new(value) + else + value + end + end + build_array(Arrow::Decimal128ArrayBuilder.new(value_data_type), + values) + end + + def build_list_array(value_data_type, values_list, field_name: "value") + value_field = Arrow::Field.new(field_name, value_data_type) + data_type = Arrow::ListDataType.new(value_field) + builder = Arrow::ListArrayBuilder.new(data_type) + values_list.each do |values| + append_to_builder(builder, values) + end + builder.finish + end + + def build_large_list_array(value_data_type, values_list, field_name: "value") + value_field = Arrow::Field.new(field_name, value_data_type) + data_type = Arrow::LargeListDataType.new(value_field) + builder = Arrow::LargeListArrayBuilder.new(data_type) + values_list.each do |values| + append_to_builder(builder, values) + end + builder.finish + end + + def build_map_array(key_data_type, item_data_type, maps) + data_type = Arrow::MapDataType.new(key_data_type, item_data_type) + builder = Arrow::MapArrayBuilder.new(data_type) + maps.each do |map| + append_to_builder(builder, map) + end + builder.finish + end + + def build_struct_array(fields, structs) + data_type = Arrow::StructDataType.new(fields) + builder = Arrow::StructArrayBuilder.new(data_type) + structs.each do |struct| + append_to_builder(builder, struct) + end + builder.finish + end + + def append_to_builder(builder, value) + if value.nil? + builder.append_null + else + data_type = builder.value_data_type + case data_type + when Arrow::MapDataType + builder.append_value + key_builder = builder.key_builder + item_builder = builder.item_builder + value.each do |k, v| + append_to_builder(key_builder, k) + append_to_builder(item_builder, v) + end + when Arrow::ListDataType, Arrow::LargeListDataType + builder.append_value + value_builder = builder.value_builder + value.each do |v| + append_to_builder(value_builder, v) + end + when Arrow::StructDataType + builder.append_value + value.each do |name, v| + field_index = data_type.get_field_index(name) + field_builder = builder.get_field_builder(field_index) + append_to_builder(field_builder, v) + end + else + builder.append_value(value) + end + end + end + + def build_table(columns) + fields = [] + chunked_arrays = [] + columns.each do |name, data| + case data + when Arrow::Array + chunked_array = Arrow::ChunkedArray.new([data]) + when Array + chunked_array = Arrow::ChunkedArray.new(data) + else + chunked_array = data + end + fields << Arrow::Field.new(name, chunked_array.value_data_type) + chunked_arrays << chunked_array + end + schema = Arrow::Schema.new(fields) + Arrow::Table.new(schema, chunked_arrays) + end + + def build_record_batch(columns) + n_rows = columns.collect {|_, array| array.length}.min || 0 + fields = columns.collect do |name, array| + Arrow::Field.new(name, array.value_data_type) + end + schema = Arrow::Schema.new(fields) + Arrow::RecordBatch.new(schema, n_rows, columns.values) + end + + def build_file_uri(path) + absolute_path = File.expand_path(path) + if absolute_path.start_with?("/") + "file://#{absolute_path}" + else + "file:///#{absolute_path}" + end + end + + private + def build_array(builder, values) + values.each do |value| + if value.nil? + builder.append_null + elsif builder.respond_to?(:append_string) + builder.append_string(value) + else + builder.append_value(value) + end + end + builder.finish + end + end +end diff --git a/src/arrow/c_glib/test/helper/data-type.rb b/src/arrow/c_glib/test/helper/data-type.rb new file mode 100644 index 000000000..bbe6866f5 --- /dev/null +++ b/src/arrow/c_glib/test/helper/data-type.rb @@ -0,0 +1,79 @@ +# 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 +# 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 Helper + module DataType + def boolean_data_type + Arrow::BooleanDataType.new + end + + def int8_data_type + Arrow::Int8DataType.new + end + + def int16_data_type + Arrow::Int16DataType.new + end + + def int32_data_type + Arrow::Int32DataType.new + end + + def int64_data_type + Arrow::Int64DataType.new + end + + def uint8_data_type + Arrow::UInt8DataType.new + end + + def uint16_data_type + Arrow::UInt16DataType.new + end + + def uint32_data_type + Arrow::UInt32DataType.new + end + + def uint64_data_type + Arrow::UInt64DataType.new + end + + def string_data_type + Arrow::StringDataType.new + end + + def date64_data_type + Arrow::Date64DataType.new + end + end +end diff --git a/src/arrow/c_glib/test/helper/fixture.rb b/src/arrow/c_glib/test/helper/fixture.rb new file mode 100644 index 000000000..f07afd0e4 --- /dev/null +++ b/src/arrow/c_glib/test/helper/fixture.rb @@ -0,0 +1,24 @@ +# 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 Helper + module Fixture + def fixture_path(*components) + File.join(__dir__, "..", "fixture", *components) + end + end +end diff --git a/src/arrow/c_glib/test/helper/flight-info-generator.rb b/src/arrow/c_glib/test/helper/flight-info-generator.rb new file mode 100644 index 000000000..c57530879 --- /dev/null +++ b/src/arrow/c_glib/test/helper/flight-info-generator.rb @@ -0,0 +1,64 @@ +# 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. + +require_relative "buildable" +require_relative "data-type" +require_relative "writable" + +module Helper + class FlightInfoGenerator + include Buildable + include DataType + include Writable + + def page_view_table + build_table("count" => build_uint64_array([1, 2, 3]), + "private" => build_boolean_array([true, false, true])) + end + + def page_view_descriptor + ArrowFlight::PathDescriptor.new(["page-view"]) + end + + def page_view_ticket + ArrowFlight::Ticket.new("page-view") + end + + def page_view_endpoints + locations = [ + ArrowFlight::Location.new("grpc+tcp://127.0.0.1:10000"), + ArrowFlight::Location.new("grpc+tcp://127.0.0.1:10001"), + ] + [ + ArrowFlight::Endpoint.new(page_view_ticket, locations), + ] + end + + def page_view + table = page_view_table + descriptor = page_view_descriptor + endpoints = page_view_endpoints + output = Arrow::ResizableBuffer.new(0) + write_table(table, output, type: :stream) + ArrowFlight::Info.new(table.schema, + descriptor, + endpoints, + table.n_rows, + output.size) + end + end +end diff --git a/src/arrow/c_glib/test/helper/flight-server.rb b/src/arrow/c_glib/test/helper/flight-server.rb new file mode 100644 index 000000000..89fd13b42 --- /dev/null +++ b/src/arrow/c_glib/test/helper/flight-server.rb @@ -0,0 +1,40 @@ +# 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. + +require_relative "flight-info-generator" + +module Helper + class FlightServer < ArrowFlight::Server + type_register + + private + def virtual_do_list_flights(context, criteria) + generator = FlightInfoGenerator.new + [generator.page_view] + end + + def virtual_do_do_get(context, ticket) + generator = FlightInfoGenerator.new + unless ticket == generator.page_view_ticket + raise Arrow::Error::Invalid.new("invalid ticket") + end + table = generator.page_view_table + reader = Arrow::TableBatchReader.new(table) + ArrowFlight::RecordBatchStream.new(reader) + end + end +end diff --git a/src/arrow/c_glib/test/helper/omittable.rb b/src/arrow/c_glib/test/helper/omittable.rb new file mode 100644 index 000000000..a1c0334b6 --- /dev/null +++ b/src/arrow/c_glib/test/helper/omittable.rb @@ -0,0 +1,36 @@ +# 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 Helper + module Omittable + def require_gi_bindings(major, minor, micro) + return if GLib.check_binding_version?(major, minor, micro) + message = + "Require gobject-introspection #{major}.#{minor}.#{micro} or later: " + + GLib::BINDING_VERSION.join(".") + omit(message) + end + + def require_gi(major, minor, micro) + return if GObjectIntrospection::Version.or_later?(major, minor, micro) + message = + "Require GObject Introspection #{major}.#{minor}.#{micro} or later: " + + GObjectIntrospection::Version::STRING + omit(message) + end + end +end diff --git a/src/arrow/c_glib/test/helper/plasma-store.rb b/src/arrow/c_glib/test/helper/plasma-store.rb new file mode 100644 index 000000000..dcf1f47ae --- /dev/null +++ b/src/arrow/c_glib/test/helper/plasma-store.rb @@ -0,0 +1,57 @@ +# 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 Helper + class PlasmaStore + def initialize(options={}) + @path = `pkg-config --variable=plasma_store_server plasma`.chomp + @memory_size = options[:memory_size] || 1024 * 1024 + @socket_file = Tempfile.new(["plasma-store", ".sock"]) + @socket_file.close + @pid = nil + FileUtils.rm_f(socket_path) + end + + def socket_path + @socket_file.path + end + + def start + @pid = spawn(@path, + "-m", @memory_size.to_s, + "-s", socket_path) + until File.exist?(socket_path) + if Process.waitpid(@pid, Process::WNOHANG) + raise "Failed to run plasma-store-server: #{@path}" + end + end + end + + def stop + return if @pid.nil? + Process.kill(:TERM, @pid) + timeout = 1 + limit = Time.now + timeout + while Time.now < limit + return if Process.waitpid(@pid, Process::WNOHANG) + sleep(0.1) + end + Process.kill(:KILL, @pid) + Process.waitpid(@pid) + end + end +end diff --git a/src/arrow/c_glib/test/helper/readable.rb b/src/arrow/c_glib/test/helper/readable.rb new file mode 100644 index 000000000..81bf0795c --- /dev/null +++ b/src/arrow/c_glib/test/helper/readable.rb @@ -0,0 +1,47 @@ +# 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 Helper + module Readable + def read_table(input, type: :file) + if input.is_a?(Arrow::Buffer) + input_stream = Arrow::BufferIntputStream.new(input) + else + input_stream = Arrow::FileInputStream.new(input) + end + begin + if type == :file + reader = Arrow::RecordBatchFileReader.new(input_stream) + record_batches = [] + reader.n_record_batches.times do |i| + record_batches << reader.read_record_batch(i) + end + yield(Arrow::Table.new(record_batches[0].schema, record_batches)) + else + reader = Arrow::RecordBatchStreamReader.new(input_stream) + begin + yield(reader.read_all) + ensure + reader.close + end + end + ensure + input_stream.close + end + end + end +end diff --git a/src/arrow/c_glib/test/helper/writable.rb b/src/arrow/c_glib/test/helper/writable.rb new file mode 100644 index 000000000..1c8db756c --- /dev/null +++ b/src/arrow/c_glib/test/helper/writable.rb @@ -0,0 +1,43 @@ +# 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 Helper + module Writable + def write_table(table, output, type: :file) + if output.is_a?(Arrow::Buffer) + output_stream = Arrow::BufferOutputStream.new(output) + else + output_stream = Arrow::FileOutputStream.new(output, false) + end + begin + if type == :file + writer_class = Arrow::RecordBatchFileWriter + else + writer_class = Arrow::RecordBatchStreamWriter + end + writer = writer_class.new(output_stream, table.schema) + begin + writer.write_table(table) + ensure + writer.close + end + ensure + output_stream.close + end + end + end +end diff --git a/src/arrow/c_glib/test/parquet/test-arrow-file-reader.rb b/src/arrow/c_glib/test/parquet/test-arrow-file-reader.rb new file mode 100644 index 000000000..45eb33596 --- /dev/null +++ b/src/arrow/c_glib/test/parquet/test-arrow-file-reader.rb @@ -0,0 +1,69 @@ +# 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 TestParquetArrowFileReader < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Parquet is required") unless defined?(::Parquet) + @file = Tempfile.open(["data", ".parquet"]) + @a_array = build_string_array(["foo", "bar"]) + @b_array = build_int32_array([123, 456]) + @table = build_table("a" => @a_array, + "b" => @b_array) + writer = Parquet::ArrowFileWriter.new(@table.schema, @file.path) + chunk_size = 1 + writer.write_table(@table, chunk_size) + writer.close + @reader = Parquet::ArrowFileReader.new(@file.path) + end + + def test_schema + assert_equal(<<-SCHEMA.chomp, @reader.schema.to_s) +a: string +b: int32 + SCHEMA + end + + sub_test_case("#read_row_group") do + test("with column indices") do + assert_equal(build_table("b" => @b_array.slice(0, 1)), + @reader.read_row_group(0, [-1])) + end + + test("without column indices") do + assert_equal(build_table("a" => @a_array.slice(1, 1), + "b" => @b_array.slice(1, 1)), + @reader.read_row_group(1)) + end + end + + def test_read_column + assert_equal([ + Arrow::ChunkedArray.new([@a_array]), + Arrow::ChunkedArray.new([@b_array]), + ], + [ + @reader.read_column_data(0), + @reader.read_column_data(-1), + ]) + end + + def test_n_rows + assert_equal(2, @reader.n_rows) + end +end diff --git a/src/arrow/c_glib/test/parquet/test-arrow-file-writer.rb b/src/arrow/c_glib/test/parquet/test-arrow-file-writer.rb new file mode 100644 index 000000000..855527444 --- /dev/null +++ b/src/arrow/c_glib/test/parquet/test-arrow-file-writer.rb @@ -0,0 +1,46 @@ +# 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 TestParquetArrowFileWriter < Test::Unit::TestCase + include Helper::Buildable + + def setup + omit("Parquet is required") unless defined?(::Parquet) + @file = Tempfile.open(["data", ".parquet"]) + end + + def test_write + enabled_values = [true, nil, false, true] + table = build_table("enabled" => build_boolean_array(enabled_values)) + chunk_size = 2 + + writer = Parquet::ArrowFileWriter.new(table.schema, @file.path) + writer.write_table(table, chunk_size) + writer.close + + reader = Parquet::ArrowFileReader.new(@file.path) + reader.use_threads = true + assert_equal([ + enabled_values.length / chunk_size, + true, + ], + [ + reader.n_row_groups, + table.equal_metadata(reader.read_table, false), + ]) + end +end diff --git a/src/arrow/c_glib/test/parquet/test-writer-properties.rb b/src/arrow/c_glib/test/parquet/test-writer-properties.rb new file mode 100644 index 000000000..1203a220b --- /dev/null +++ b/src/arrow/c_glib/test/parquet/test-writer-properties.rb @@ -0,0 +1,103 @@ +# 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 TestParquetWriterProperties < Test::Unit::TestCase + def setup + omit("Parquet is required") unless defined?(::Parquet) + @properties = Parquet::WriterProperties.new + end + + def test_compression + @properties.set_compression(:gzip) + assert_equal(Arrow::CompressionType::GZIP, + @properties.get_compression_path("not-specified")) + end + + def test_compression_with_path + @properties.set_compression(:gzip, "column") + assert_equal([ + Arrow::CompressionType::GZIP, + Arrow::CompressionType::UNCOMPRESSED, + ], + [ + @properties.get_compression_path("column"), + @properties.get_compression_path("not-specified"), + ]) + end + + def test_enable_dictionary + @properties.enable_dictionary + assert_equal(true, + @properties.dictionary_enabled?("not-specified")) + end + + def test_enable_dictionary_with_path + @properties.disable_dictionary + @properties.enable_dictionary("column") + assert_equal([ + true, + false, + ], + [ + @properties.dictionary_enabled?("column"), + @properties.dictionary_enabled?("not-specified"), + ]) + end + + def test_disable_dictionary + @properties.disable_dictionary + assert_equal(false, + @properties.dictionary_enabled?("not-specified")) + end + + def test_disable_dictionary_with_path + @properties.enable_dictionary + @properties.disable_dictionary("column") + assert_equal([ + false, + true, + ], + [ + @properties.dictionary_enabled?("column"), + @properties.dictionary_enabled?("not-specified"), + ]) + end + + def test_dictionary_page_size_limit + @properties.dictionary_page_size_limit = 4096 + assert_equal(4096, + @properties.dictionary_page_size_limit) + end + + def test_batch_size + @properties.batch_size = 100 + assert_equal(100, + @properties.batch_size) + end + + def test_data_page_size + @properties.data_page_size = 128 + assert_equal(128, + @properties.data_page_size) + end + + def test_max_row_group_length + @properties.max_row_group_length = 1024 + assert_equal(1024, + @properties.max_row_group_length) + end +end diff --git a/src/arrow/c_glib/test/plasma/test-plasma-client-options.rb b/src/arrow/c_glib/test/plasma/test-plasma-client-options.rb new file mode 100644 index 000000000..abe6fd3ce --- /dev/null +++ b/src/arrow/c_glib/test/plasma/test-plasma-client-options.rb @@ -0,0 +1,31 @@ +# 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 TestPlasmaClientOptions < Test::Unit::TestCase + include Helper::Omittable + + def setup + omit("Plasma is required") unless defined?(::Plasma) + @options = Plasma::ClientOptions.new + end + + test("n_retries") do + assert_equal(-1, @options.n_retries) + @options.n_retries = 10 + assert_equal(10, @options.n_retries) + end +end diff --git a/src/arrow/c_glib/test/plasma/test-plasma-client.rb b/src/arrow/c_glib/test/plasma/test-plasma-client.rb new file mode 100644 index 000000000..4ff39372b --- /dev/null +++ b/src/arrow/c_glib/test/plasma/test-plasma-client.rb @@ -0,0 +1,94 @@ +# 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 TestPlasmaClient < Test::Unit::TestCase + include Helper::Omittable + + def setup + @store = nil + omit("Plasma is required") unless defined?(::Plasma) + require_gi_bindings(3, 3, 9) + @store = Helper::PlasmaStore.new + @store.start + @options = Plasma::ClientOptions.new + @client = Plasma::Client.new(@store.socket_path, @options) + @id = Plasma::ObjectID.new("Hello") + @data = "World" + @options = Plasma::ClientCreateOptions.new + end + + def teardown + @store.stop if @store + end + + sub_test_case("#create") do + def setup + super + + @metadata = "Metadata" + end + + test("no options") do + object = @client.create(@id, @data.bytesize) + object.data.set_data(0, @data) + object.seal + + object = @client.refer_object(@id, -1) + assert_equal(@data, object.data.data.to_s) + end + + test("options: metadata") do + @options.set_metadata(@metadata) + object = @client.create(@id, 1, @options) + object.seal + + object = @client.refer_object(@id, -1) + assert_equal(@metadata, object.metadata.data.to_s) + end + + test("options: GPU device") do + omit("Arrow CUDA is required") unless defined?(::ArrowCUDA) + + gpu_device = 0 + + @options.gpu_device = gpu_device + @options.metadata = @metadata + object = @client.create(@id, @data.bytesize, @options) + object.data.copy_from_host(@data) + object.seal + + object = @client.refer_object(@id, -1) + assert_equal([ + gpu_device, + @data, + @metadata, + ], + [ + object.gpu_device, + object.data.copy_to_host(0, @data.bytesize).to_s, + object.metadata.copy_to_host(0, @metadata.bytesize).to_s, + ]) + end + end + + test("#disconnect") do + @client.disconnect + assert_raise(Arrow::Error::Io) do + @client.create(@id, @data.bytesize, @options) + end + end +end diff --git a/src/arrow/c_glib/test/plasma/test-plasma-created-object.rb b/src/arrow/c_glib/test/plasma/test-plasma-created-object.rb new file mode 100644 index 000000000..8d036cda8 --- /dev/null +++ b/src/arrow/c_glib/test/plasma/test-plasma-created-object.rb @@ -0,0 +1,59 @@ +# 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 TestPlasmaCreatedObject < Test::Unit::TestCase + include Helper::Omittable + + def setup + @store = nil + omit("Plasma is required") unless defined?(::Plasma) + require_gi_bindings(3, 3, 9) + @store = Helper::PlasmaStore.new + @store.start + @client = Plasma::Client.new(@store.socket_path, nil) + + @id = Plasma::ObjectID.new("Hello") + @data = "World" + @metadata = "Metadata" + @options = Plasma::ClientCreateOptions.new + @options.metadata = @metadata + @object = @client.create(@id, @data.bytesize, @options) + end + + def teardown + @store.stop if @store + end + + test("#seal") do + @object.data.set_data(0, @data) + @object.seal + + object = @client.refer_object(@id, -1) + assert_equal(@data, object.data.data.to_s) + end + + test("#abort") do + @object.data.set_data(0, @data) + assert_raise(Arrow::Error::AlreadyExists) do + @client.create(@id, @data.bytesize, @options) + end + @object.abort + + object = @client.create(@id, @data.bytesize, @options) + object.abort + end +end diff --git a/src/arrow/c_glib/test/plasma/test-plasma-referred-object.rb b/src/arrow/c_glib/test/plasma/test-plasma-referred-object.rb new file mode 100644 index 000000000..ef4c72aee --- /dev/null +++ b/src/arrow/c_glib/test/plasma/test-plasma-referred-object.rb @@ -0,0 +1,54 @@ +# 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 TestPlasmaReferredObject < Test::Unit::TestCase + include Helper::Omittable + + def setup + @store = nil + omit("Plasma is required") unless defined?(::Plasma) + require_gi_bindings(3, 3, 9) + @store = Helper::PlasmaStore.new + @store.start + @client = Plasma::Client.new(@store.socket_path, nil) + + @id = Plasma::ObjectID.new("Hello") + @data = "World" + @metadata = "Metadata" + @options = Plasma::ClientCreateOptions.new + @options.metadata = @metadata + object = @client.create(@id, @data.bytesize, @options) + object.data.set_data(0, @data) + object.seal + @object = @client.refer_object(@id, -1) + end + + def teardown + @store.stop if @store + end + + test("#release") do + @object.release + + message = "[plasma][referred-object][release]: " + message << "Can't process released object: <#{@id.to_hex}>" + error = Arrow::Error::Invalid.new(message) + assert_raise(error) do + @object.release + end + end +end diff --git a/src/arrow/c_glib/test/run-test.rb b/src/arrow/c_glib/test/run-test.rb new file mode 100755 index 000000000..621c78c39 --- /dev/null +++ b/src/arrow/c_glib/test/run-test.rb @@ -0,0 +1,104 @@ +#!/usr/bin/env ruby +# +# 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. + +require "pathname" +require "test-unit" + +(ENV["ARROW_DLL_PATH"] || "").split(File::PATH_SEPARATOR).each do |path| + RubyInstaller::Runtime.add_dll_directory(path) +end + +base_dir = Pathname(__dir__).parent +test_dir = base_dir + "test" + +require "gi" + +Gio = GI.load("Gio") +Arrow = GI.load("Arrow") +module Arrow + class Buffer + alias_method :initialize_raw, :initialize + def initialize(data) + initialize_raw(data) + @data = data + end + end + + class BooleanScalar + alias_method :value, :value? + end +end + +begin + ArrowCUDA = GI.load("ArrowCUDA") +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +begin + ArrowDataset = GI.load("ArrowDataset") +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +begin + class ArrowFlightLoader < GI::Loader + def should_unlock_gvl?(info, klass) + true + end + end + flight_module = Module.new + ArrowFlightLoader.load("ArrowFlight", flight_module) + ArrowFlight = flight_module + GObjectIntrospection::Loader.start_callback_dispatch_thread +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +begin + Gandiva = GI.load("Gandiva") +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +begin + Parquet = GI.load("Parquet") +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +begin + Plasma = GI.load("Plasma") +rescue GObjectIntrospection::RepositoryError::TypelibNotFound +end + +require "fileutils" +require "find" +require "rbconfig" +require "stringio" +require "tempfile" +require "zlib" +require_relative "helper/buildable" +require_relative "helper/data-type" +require_relative "helper/fixture" +if defined?(ArrowFlight) + require_relative "helper/flight-info-generator" + require_relative "helper/flight-server" +end +require_relative "helper/omittable" +require_relative "helper/plasma-store" +require_relative "helper/readable" +require_relative "helper/writable" + +exit(Test::Unit::AutoRunner.run(true, test_dir.to_s)) diff --git a/src/arrow/c_glib/test/run-test.sh b/src/arrow/c_glib/test/run-test.sh new file mode 100755 index 000000000..c3565d272 --- /dev/null +++ b/src/arrow/c_glib/test/run-test.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env sh +# +# 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. + +test_dir="$(cd $(dirname $0); pwd)" +build_dir="$(cd .; pwd)" + +modules="arrow-glib arrow-cuda-glib arrow-dataset-glib arrow-flight-glib gandiva-glib parquet-glib plasma-glib" + +for module in ${modules}; do + module_build_dir="${build_dir}/${module}" + libtool_dir="${module_build_dir}/.libs" + if [ -d "${libtool_dir}" ]; then + LD_LIBRARY_PATH="${libtool_dir}:${LD_LIBRARY_PATH}" + else + if [ -d "${module_build_dir}" ]; then + LD_LIBRARY_PATH="${module_build_dir}:${LD_LIBRARY_PATH}" + fi + fi +done +export LD_LIBRARY_PATH + +if [ "${BUILD}" != "no" ]; then + if [ -f "Makefile" ]; then + make -j8 > /dev/null || exit $? + elif [ -f "build.ninja" ]; then + ninja || exit $? + fi +fi + +for module in ${modules}; do + MODULE_TYPELIB_DIR_VAR_NAME="$(echo ${module} | tr a-z- A-Z_)_TYPELIB_DIR" + module_typelib_dir=$(eval "echo \${${MODULE_TYPELIB_DIR_VAR_NAME}}") + if [ -z "${module_typelib_dir}" ]; then + module_typelib_dir="${build_dir}/${module}" + fi + + if [ -d "${module_typelib_dir}" ]; then + GI_TYPELIB_PATH="${module_typelib_dir}:${GI_TYPELIB_PATH}" + fi +done +export GI_TYPELIB_PATH + +${GDB} ruby ${test_dir}/run-test.rb "$@" diff --git a/src/arrow/c_glib/test/test-array-builder.rb b/src/arrow/c_glib/test/test-array-builder.rb new file mode 100644 index 000000000..6bece6367 --- /dev/null +++ b/src/arrow/c_glib/test/test-array-builder.rb @@ -0,0 +1,1944 @@ +# 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 ArrayBuilderAppendValueBytesTests + def test_append + builder = create_builder + value = "\x00\xff" + builder.append_value_bytes(GLib::Bytes.new(value)) + assert_equal(build_array([value]), + builder.finish) + end +end + +module ArrayBuilderAppendValuesTests + def test_empty + builder = create_builder + builder.append_values([]) + assert_equal(build_array([]), + builder.finish) + end + + def test_values_only + builder = create_builder + builder.append_values(sample_values) + assert_equal(build_array(sample_values), + builder.finish) + end + + def test_with_is_valids + builder = create_builder + builder.append_values(sample_values, [true, true, false]) + actual_sample_values = sample_values.dup + actual_sample_values[2] = nil + assert_equal(build_array(actual_sample_values), + builder.finish) + end + + def test_with_large_is_valids + builder = create_builder + n = 10000 + large_sample_values = sample_values * n + large_is_valids = [true, true, false] * n + builder.append_values(large_sample_values, large_is_valids) + actual_sample_values = sample_values.dup + actual_sample_values[2] = nil + actual_large_sample_values = actual_sample_values * n + assert_equal(build_array(actual_large_sample_values), + builder.finish) + end + + def test_mismatch_length + builder = create_builder + message = "[#{builder_class_name}][append-values]: " + + "values length and is_valids length must be equal: <3> != <2>" + assert_raise(Arrow::Error::Invalid.new(message)) do + builder.append_values(sample_values, [true, true]) + end + end +end + +module ArrayBuilderAppendValuesWithNullTests + def test_values_only + builder = create_builder + builder.append_values(sample_values_with_null) + assert_equal(build_array(sample_values_with_null), + builder.finish) + end + + def test_large_values_only + builder = create_builder + n = 10000 + large_sample_values_with_null = sample_values_with_null * n + builder.append_values(large_sample_values_with_null) + assert_equal(build_array(large_sample_values_with_null), + builder.finish) + end + + def test_with_is_valids + builder = create_builder + builder.append_values(sample_values_with_null, [true, true, false]) + actual_sample_values = sample_values_with_null.dup + actual_sample_values[2] = nil + assert_equal(build_array(actual_sample_values), + builder.finish) + end + + def test_with_large_is_valids + builder = create_builder + n = 10000 + large_sample_values = sample_values_with_null * n + large_is_valids = [true, true, false] * n + builder.append_values(large_sample_values, large_is_valids) + actual_sample_values = sample_values_with_null.dup + actual_sample_values[2] = nil + actual_large_sample_values = actual_sample_values * n + assert_equal(build_array(actual_large_sample_values), + builder.finish) + end +end + +module ArrayBuilderAppendValuesPackedTests + def test_empty + builder = create_builder + builder.append_values_packed("") + assert_equal(build_array([]), + builder.finish) + end + + def test_values_only + builder = create_builder + builder.append_values_packed(pack_values(sample_values)) + assert_equal(build_array(sample_values), + builder.finish) + end + + def test_with_is_valids + builder = create_builder + builder.append_values_packed(pack_values(sample_values), + [true, true, false]) + sample_values_with_null = sample_values + sample_values_with_null[2] = nil + assert_equal(build_array(sample_values_with_null), + builder.finish) + end + + def test_with_large_is_valids + builder = create_builder + n = 10000 + large_sample_values = sample_values * n + large_is_valids = [true, true, false] * n + builder.append_values_packed(pack_values(large_sample_values), + large_is_valids) + sample_values_with_null = sample_values + sample_values_with_null[2] = nil + large_sample_values_with_null = sample_values_with_null * n + assert_equal(build_array(large_sample_values_with_null), + builder.finish) + end + + def test_mismatch_length + builder = create_builder + message = "[fixed-size-binary-array-builder][append-values-packed]: " + + "the number of values and is_valids length must be equal: <3> != <2>" + assert_raise(Arrow::Error::Invalid.new(message)) do + builder.append_values_packed(pack_values(sample_values), + [true, true]) + end + end +end + +module ArrayBuilderAppendStringsTests + def test_empty + builder = create_builder + builder.append_strings([]) + assert_equal(build_array([]), + builder.finish) + end + + def test_strings_only + builder = create_builder + builder.append_strings(sample_values) + assert_equal(build_array(sample_values), + builder.finish) + end + + def test_with_is_valids + builder = create_builder + builder.append_strings(sample_values, [true, true, false]) + sample_values_with_null = sample_values + sample_values_with_null[2] = nil + assert_equal(build_array(sample_values_with_null), + builder.finish) + end + + def test_with_large_is_valids + builder = create_builder + n = 10000 + large_sample_values = sample_values * n + large_is_valids = [true, true, false] * n + builder.append_strings(large_sample_values, large_is_valids) + sample_values_with_null = sample_values + sample_values_with_null[2] = nil + large_sample_values_with_null = sample_values_with_null * n + assert_equal(build_array(large_sample_values_with_null), + builder.finish) + end + + def test_mismatch_length + builder = create_builder + message = "[#{builder_class_name}][append-strings]: " + + "values length and is_valids length must be equal: <3> != <2>" + assert_raise(Arrow::Error::Invalid.new(message)) do + builder.append_strings(sample_values, [true, true]) + end + end +end + +module ArrayBuilderAppendNullsTests + def test_zero + builder = create_builder + builder.append_nulls(0) + assert_equal(build_array([]), + builder.finish) + end + + def test_positive + builder = create_builder + builder.append_nulls(3) + assert_equal(build_array([nil, nil, nil]), + builder.finish) + end + + def test_negative + builder = create_builder + message = "[array-builder][append-nulls]: " + + "the number of nulls must be 0 or larger: <-1>" + assert_raise(Arrow::Error::Invalid.new(message)) do + builder.append_nulls(-1) + end + end +end + +module ArrayBuilderAppendEmptyValueTests + def test_append + builder = create_builder + builder.append_empty_value + assert_equal(build_array([empty_value]), + builder.finish) + end +end + +module ArrayBuilderAppendEmptyValuesTests + def test_zero + builder = create_builder + builder.append_empty_values(0) + assert_equal(build_array([]), + builder.finish) + end + + def test_positive + builder = create_builder + builder.append_empty_values(3) + assert_equal(build_array([empty_value] * 3), + builder.finish) + end + + def test_negative + builder = create_builder + message = "[array-builder][append-empty-values]: " + + "the number of empty values must be 0 or larger: <-1>" + assert_raise(Arrow::Error::Invalid.new(message)) do + builder.append_empty_values(-1) + end + end +end + +module ArrayBuilderValueTypeTests + def test_value_data_type + assert_equal(value_data_type, + build_array(sample_values).value_data_type) + end + + def test_value_type + assert_equal(value_data_type.id, + build_array(sample_values).value_type) + end +end + +module ArrayBuilderCapacityControlTests + def test_resize + builder = create_builder + before_capacity = builder.capacity + builder.resize(before_capacity + 100) + after_capacity = builder.capacity + + assert do + after_capacity >= before_capacity + 100 + end + end + + def test_reserve + builder = create_builder + before_capacity = builder.capacity + builder.reserve(100) + after_capacity = builder.capacity + + assert do + after_capacity >= before_capacity + 100 + end + end +end + +module ArrayBuilderLengthTests + def test_length + builder = create_builder + sample_values_with_null = sample_values + sample_values_with_null[2, 0] = nil + lengths = [builder.length] + sample_values_with_null.each do |value| + if value.nil? + builder.append_null + else + builder.append_value(value) + end + lengths << builder.length + end + expected_lengths = [*0 ... (sample_values_with_null.length+1)] + assert_equal(expected_lengths, + lengths) + end +end + +module ArrayBuilderNNullsTests + def test_n_nulls + builder = create_builder + sample_values_with_null = sample_values + sample_values_with_null[2, 0] = nil + null_counts = [builder.n_nulls] + sample_values_with_null.each do |value| + if value.nil? + builder.append_null + else + builder.append_value(value) + end + null_counts << builder.n_nulls + end + expected_null_counts = [0, 0, 0] + [1] * (sample_values_with_null.length - 2) + assert_equal(expected_null_counts, + null_counts) + end +end + +class TestArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def setup + require_gi_bindings(3, 1, 9) + end + + def build_array(values) + super(create_builder, values) + end + + sub_test_case("NullArrayBuilder") do + def create_builder + Arrow::NullArrayBuilder.new + end + + def value_data_type + Arrow::NullDataType.new + end + + def builder_class_name + "null-array-builder" + end + + def sample_values + [nil, nil, nil] + end + + def empty_value + nil + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + test("#append_null") do + builder = create_builder + builder.append_null + assert_equal(build_array([nil]), + builder.finish) + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + test("#length") do + builder = create_builder + before = builder.length + builder.append_null + after = builder.length + assert_equal(1, + after - before) + end + + test("#n_nulls") do + builder = create_builder + before = builder.length + builder.append_null + after = builder.length + assert_equal(1, + after - before) + end + end + + sub_test_case("BooleanArrayBuilder") do + def create_builder + Arrow::BooleanArrayBuilder.new + end + + def value_data_type + Arrow::BooleanDataType.new + end + + def builder_class_name + "boolean-array-builder" + end + + def sample_values + [true, false, true] + end + + def empty_value + false + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("IntArrayBuilder") do + def create_builder + Arrow::IntArrayBuilder.new + end + + def value_data_type + Arrow::Int8DataType.new + end + + def builder_class_name + "int-array-builder" + end + + def sample_values + [1, -2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("UIntArrayBuilder") do + def create_builder + Arrow::UIntArrayBuilder.new + end + + def value_data_type + Arrow::UInt8DataType.new + end + + def builder_class_name + "uint-array-builder" + end + + def sample_values + [1, 2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Int8ArrayBuilder") do + def create_builder + Arrow::Int8ArrayBuilder.new + end + + def value_data_type + Arrow::Int8DataType.new + end + + def builder_class_name + "int8-array-builder" + end + + def sample_values + [1, -2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("UInt8ArrayBuilder") do + def create_builder + Arrow::UInt8ArrayBuilder.new + end + + def value_data_type + Arrow::UInt8DataType.new + end + + def builder_class_name + "uint8-array-builder" + end + + def sample_values + [1, 2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Int16ArrayBuilder") do + def create_builder + Arrow::Int16ArrayBuilder.new + end + + def value_data_type + Arrow::Int16DataType.new + end + + def builder_class_name + "int16-array-builder" + end + + def sample_values + [1, -2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("UInt16ArrayBuilder") do + def create_builder + Arrow::UInt16ArrayBuilder.new + end + + def value_data_type + Arrow::UInt16DataType.new + end + + def builder_class_name + "uint16-array-builder" + end + + def sample_values + [1, 2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Int32ArrayBuilder") do + def create_builder + Arrow::Int32ArrayBuilder.new + end + + def value_data_type + Arrow::Int32DataType.new + end + + def builder_class_name + "int32-array-builder" + end + + def sample_values + [1, -2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("UInt32ArrayBuilder") do + def create_builder + Arrow::UInt32ArrayBuilder.new + end + + def value_data_type + Arrow::UInt32DataType.new + end + + def builder_class_name + "uint32-array-builder" + end + + def sample_values + [1, 2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Int64ArrayBuilder") do + def create_builder + Arrow::Int64ArrayBuilder.new + end + + def value_data_type + Arrow::Int64DataType.new + end + + def builder_class_name + "int64-array-builder" + end + + def sample_values + [1, -2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("UInt64ArrayBuilder") do + def create_builder + Arrow::UInt64ArrayBuilder.new + end + + def value_data_type + Arrow::UInt64DataType.new + end + + def builder_class_name + "uint64-array-builder" + end + + def sample_values + [1, 2, 3] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("FloatArrayBuilder") do + def create_builder + Arrow::FloatArrayBuilder.new + end + + def value_data_type + Arrow::FloatDataType.new + end + + def builder_class_name + "float-array-builder" + end + + def sample_values + [1.1, -2.2, 3.3] + end + + def empty_value + 0.0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("DoubleArrayBuilder") do + def create_builder + Arrow::DoubleArrayBuilder.new + end + + def value_data_type + Arrow::DoubleDataType.new + end + + def builder_class_name + "double-array-builder" + end + + def sample_values + [1.1, -2.2, 3.3] + end + + def empty_value + 0.0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Date32ArrayBuilder") do + def create_builder + Arrow::Date32ArrayBuilder.new + end + + def value_data_type + Arrow::Date32DataType.new + end + + def builder_class_name + "date32-array-builder" + end + + def sample_values + [ + 0, # epoch + 17406, # 2017-08-28 + 17427, # 2017-09-18 + ] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Date64ArrayBuilder") do + def create_builder + Arrow::Date64ArrayBuilder.new + end + + def value_data_type + Arrow::Date64DataType.new + end + + def builder_class_name + "date64-array-builder" + end + + def sample_values + [ + -315619200, # 1960-01-01T00:00:00Z + 0, # epoch + 1503878400000, # 2017-08-28T00:00:00Z + ] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("TimestampArrayBuilder") do + def create_builder + data_type = Arrow::TimestampDataType.new(:milli) + Arrow::TimestampArrayBuilder.new(data_type) + end + + def value_data_type + Arrow::TimestampDataType.new(:milli) + end + + def builder_class_name + "timestamp-array-builder" + end + + def sample_values + [ + 0, # epoch + 1504953190854, # 2017-09-09T10:33:10.854Z + 1505660812942, # 2017-09-17T15:06:52.942Z + ] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Time32ArrayBuilder") do + def create_builder + data_type = Arrow::Time32DataType.new(:second) + Arrow::Time32ArrayBuilder.new(data_type) + end + + def value_data_type + Arrow::Time32DataType.new(:second) + end + + def builder_class_name + "time32-array-builder" + end + + def sample_values + [ + 0, # midnight + 60 * 10, # 00:10:00 + 60 * 60 * 2 + 30, # 02:00:30 + ] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Time64ArrayBuilder") do + def create_builder + data_type = Arrow::Time64DataType.new(:micro) + Arrow::Time64ArrayBuilder.new(data_type) + end + + def value_data_type + Arrow::Time64DataType.new(:micro) + end + + def builder_class_name + "time64-array-builder" + end + + def sample_values + [ + 0, # midnight + 60 * 10 * 1000 * 1000, # 00:10:00.000000 + (60 * 60 * 2 + 30) * 1000 * 1000, # 02:00:30.000000 + ] + end + + def empty_value + 0 + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("BinaryArrayBuilder") do + def create_builder + Arrow::BinaryArrayBuilder.new + end + + def value_data_type + Arrow::BinaryDataType.new + end + + def builder_class_name + "binary-array-builder" + end + + def sample_values + [ + "\x00\x01", + "\xfe\xff", + "", + ] + end + + def empty_value + "" + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_value_bytes") do + include ArrayBuilderAppendValueBytesTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + + def setup + require_gi_bindings(3, 4, 1) + end + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("LargeBinaryArrayBuilder") do + def create_builder + Arrow::LargeBinaryArrayBuilder.new + end + + def value_data_type + Arrow::LargeBinaryDataType.new + end + + def builder_class_name + "large-binary-array-builder" + end + + def sample_values + [ + "\x00\x01", + "\xfe\xff", + "", + ] + end + + def empty_value + "" + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_value_bytes") do + include ArrayBuilderAppendValueBytesTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + + def setup + require_gi_bindings(3, 4, 1) + end + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("StringArrayBuilder") do + def create_builder + Arrow::StringArrayBuilder.new + end + + def value_data_type + Arrow::StringDataType.new + end + + def builder_class_name + "string-array-builder" + end + + def sample_values + [ + "hello", + "world!!", + "", + ] + end + + def empty_value + "" + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + + def setup + require_gi_bindings(3, 4, 1) + end + + def builder_class_name + "binary-array-builder" + end + end + + sub_test_case("#append_strings") do + include ArrayBuilderAppendStringsTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("LargeStringArrayBuilder") do + def create_builder + Arrow::LargeStringArrayBuilder.new + end + + def value_data_type + Arrow::LargeStringDataType.new + end + + def builder_class_name + "large-string-array-builder" + end + + def sample_values + [ + "hello", + "world!!", + "", + ] + end + + def empty_value + "" + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + + def setup + require_gi_bindings(3, 4, 1) + end + + def builder_class_name + "large-binary-array-builder" + end + end + + sub_test_case("#append_strings") do + include ArrayBuilderAppendStringsTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("FixedSizeBinaryArrayBuilder") do + def create_builder + Arrow::FixedSizeBinaryArrayBuilder.new(value_data_type) + end + + def value_data_type + Arrow::FixedSizeBinaryDataType.new(4) + end + + def builder_class_name + "fixed-size-binary-array-builder" + end + + def sample_values + [ + "0123", + "abcd", + "\x0\x0\x0\x0".b, + ] + end + + def sample_values_with_null + [ + "0123", + nil, + "\x0\x0\x0\x0".b, + ] + end + + def pack_values(values) + values.join("") + end + + def empty_value + "\x0\x0\x0\x0" + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_value") do + test("nil") do + builder = create_builder + builder.append_value(nil) + assert_equal(build_array([nil]), + builder.finish) + end + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + include ArrayBuilderAppendValuesWithNullTests + end + + sub_test_case("#append_values_packed") do + include ArrayBuilderAppendValuesPackedTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Decimal128ArrayBuilder") do + def create_builder + Arrow::Decimal128ArrayBuilder.new(value_data_type) + end + + def value_data_type + Arrow::Decimal128DataType.new(8, 2) + end + + def builder_class_name + "decimal128-array-builder" + end + + def sample_values + [ + Arrow::Decimal128.new("23423445"), + Arrow::Decimal128.new("00012345"), + Arrow::Decimal128.new("00000000"), + ] + end + + def sample_values_with_null + [ + Arrow::Decimal128.new("23423445"), + nil, + Arrow::Decimal128.new("00000000"), + ] + end + + def pack_values(values) + values.collect(&:to_bytes).collect(&:to_s).join("") + end + + def empty_value + Arrow::Decimal128.new("0") + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_value") do + test("nil") do + builder = create_builder + builder.append_value(nil) + assert_equal(build_array([nil]), + builder.finish) + end + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + include ArrayBuilderAppendValuesWithNullTests + end + + sub_test_case("#append_values_packed") do + include ArrayBuilderAppendValuesPackedTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end + + sub_test_case("Decimal256ArrayBuilder") do + def create_builder + Arrow::Decimal256ArrayBuilder.new(value_data_type) + end + + def value_data_type + Arrow::Decimal256DataType.new(38, 2) + end + + def builder_class_name + "decimal256-array-builder" + end + + def sample_values + [ + Arrow::Decimal256.new("23423445"), + Arrow::Decimal256.new("00012345"), + Arrow::Decimal256.new("00000000"), + ] + end + + def sample_values_with_null + [ + Arrow::Decimal256.new("23423445"), + nil, + Arrow::Decimal256.new("00000000"), + ] + end + + def pack_values(values) + values.collect(&:to_bytes).collect(&:to_s).join("") + end + + def empty_value + Arrow::Decimal256.new("0") + end + + sub_test_case("value type") do + include ArrayBuilderValueTypeTests + end + + sub_test_case("#append_value") do + test("nil") do + builder = create_builder + builder.append_value(nil) + assert_equal(build_array([nil]), + builder.finish) + end + end + + sub_test_case("#append_values") do + include ArrayBuilderAppendValuesTests + include ArrayBuilderAppendValuesWithNullTests + end + + sub_test_case("#append_values_packed") do + include ArrayBuilderAppendValuesPackedTests + end + + sub_test_case("#append_nulls") do + include ArrayBuilderAppendNullsTests + end + + sub_test_case("#append_empty_value") do + include ArrayBuilderAppendEmptyValueTests + end + + sub_test_case("#append_empty_values") do + include ArrayBuilderAppendEmptyValuesTests + end + + sub_test_case("capacity control") do + include ArrayBuilderCapacityControlTests + end + + sub_test_case("#length") do + include ArrayBuilderLengthTests + end + + sub_test_case("#n_nulls") do + include ArrayBuilderNNullsTests + end + end +end diff --git a/src/arrow/c_glib/test/test-array-datum.rb b/src/arrow/c_glib/test/test-array-datum.rb new file mode 100644 index 000000000..623e5589c --- /dev/null +++ b/src/arrow/c_glib/test/test-array-datum.rb @@ -0,0 +1,70 @@ +# 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 TestArrayDatum < Test::Unit::TestCase + include Helper::Buildable + + def setup + @array = build_boolean_array([true, false]) + @datum = Arrow::ArrayDatum.new(@array) + end + + def test_array? + assert do + @datum.array? + end + end + + def test_array_like? + assert do + @datum.array_like? + end + end + + def test_scalar? + assert do + not @datum.scalar? + end + end + + def test_value? + assert do + @datum.value? + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::ArrayDatum.new(@array), + Arrow::ArrayDatum.new(@array)) + end + + def test_false + table = build_table("visible" => @array) + assert_not_equal(@datum, + Arrow::TableDatum.new(table)) + end + end + + def test_to_string + assert_equal("Array", @datum.to_s) + end + + def test_value + assert_equal(@array, @datum.value) + end +end diff --git a/src/arrow/c_glib/test/test-array-sort-options.rb b/src/arrow/c_glib/test/test-array-sort-options.rb new file mode 100644 index 000000000..afb6a7025 --- /dev/null +++ b/src/arrow/c_glib/test/test-array-sort-options.rb @@ -0,0 +1,31 @@ +# 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 TestArraySortOptions < Test::Unit::TestCase + include Helper::Buildable + + def test_new + options = Arrow::ArraySortOptions.new(:ascending) + assert_equal(Arrow::SortOrder::ASCENDING, + options.order) + end + + def test_equal + assert_equal(Arrow::ArraySortOptions.new(:descending), + Arrow::ArraySortOptions.new(:descending)) + end +end diff --git a/src/arrow/c_glib/test/test-array.rb b/src/arrow/c_glib/test/test-array.rb new file mode 100644 index 000000000..c03aecf17 --- /dev/null +++ b/src/arrow/c_glib/test/test-array.rb @@ -0,0 +1,188 @@ +# 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 TestArray < Test::Unit::TestCase + include Helper::Buildable + + def test_equal + assert_equal(build_boolean_array([true, false]), + build_boolean_array([true, false])) + end + + def test_equal_approx + array1 = build_double_array([1.1, 2.2 + Float::EPSILON * 10]) + array2 = build_double_array([1.1, 2.2]) + assert do + array1.equal_approx(array2) + end + end + + def test_equal_range + array1 = build_int32_array([1, 2, 3, 4, 5]) + array2 = build_int32_array([-2, -1, 0, 1, 2, 3, 4, 999]) + assert do + array1.equal_range(1, array2, 4, 3) + end + end + + def test_is_null + builder = Arrow::BooleanArrayBuilder.new + builder.append_null + builder.append_value(true) + array = builder.finish + assert_equal([true, false], + array.length.times.collect {|i| array.null?(i)}) + end + + def test_is_valid + builder = Arrow::BooleanArrayBuilder.new + builder.append_null + builder.append_value(true) + array = builder.finish + assert_equal([false, true], + array.length.times.collect {|i| array.valid?(i)}) + end + + def test_length + builder = Arrow::BooleanArrayBuilder.new + builder.append_value(true) + array = builder.finish + assert_equal(1, array.length) + end + + def test_n_nulls + builder = Arrow::BooleanArrayBuilder.new + builder.append_null + builder.append_null + array = builder.finish + assert_equal(2, array.n_nulls) + end + + def test_null_bitmap + builder = Arrow::BooleanArrayBuilder.new + builder.append_null + builder.append_value(true) + builder.append_value(false) + builder.append_null + builder.append_value(false) + array = builder.finish + assert_equal(0b10110, array.null_bitmap.data.to_s.unpack("c*")[0]) + end + + def test_value_data_type + builder = Arrow::BooleanArrayBuilder.new + array = builder.finish + assert_equal(Arrow::BooleanDataType.new, array.value_data_type) + end + + def test_value_type + builder = Arrow::BooleanArrayBuilder.new + array = builder.finish + assert_equal(Arrow::Type::BOOLEAN, array.value_type) + end + + def test_slice + builder = Arrow::BooleanArrayBuilder.new + builder.append_value(true) + builder.append_value(false) + builder.append_value(true) + array = builder.finish + sub_array = array.slice(1, 2) + assert_equal([false, true], + sub_array.length.times.collect {|i| sub_array.get_value(i)}) + end + + def test_to_s + assert_equal(<<-CONTENT.chomp, build_boolean_array([true, false, true]).to_s) +[ + true, + false, + true +] + CONTENT + end + + sub_test_case("#view") do + def test_valid + assert_equal(build_float_array([0.0, 1.5, -2.5, nil]), + build_int32_array([0, 1069547520, -1071644672, nil]).view(Arrow::FloatDataType.new)) + end + + def test_invalid + message = "[array][view]: Invalid: " + + "Can't view array of type int16 as int8: incompatible layouts" + error = assert_raise(Arrow::Error::Invalid) do + build_int16_array([0, -1, 3]).view(Arrow::Int8DataType.new) + end + assert_equal(message, error.message.lines.first.chomp) + end + end + + sub_test_case("#diff_unified") do + def test_no_diff + array = build_string_array(["Start", "Shutdown", "Reboot"]) + other_array = build_string_array(["Start", "Shutdown", "Reboot"]) + assert_nil(array.diff_unified(other_array)) + end + + def test_diff + array = build_string_array(["Start", "Shutdown", "Reboot"]) + other_array = build_string_array(["Start", "Shutdonw", "Reboot"]) + assert_equal(<<-STRING.chomp, array.diff_unified(other_array)) + +@@ -1, +1 @@ +-"Shutdown" ++"Shutdonw" + + STRING + end + + def test_different_type + array = build_string_array(["Start", "Shutdown", "Reboot"]) + other_array = build_int8_array([2, 3, 6, 10]) + assert_equal("# Array types differed: string vs int8\n", + array.diff_unified(other_array)) + end + end + + sub_test_case("#concatenate") do + def test_no_other_arrays + assert_equal(build_int32_array([1, 2, 3]), + build_int32_array([1, 2, 3]).concatenate([])) + end + + def test_multiple_other_arrays + a = build_int32_array([1, 2, 3]) + b = build_int32_array([4]) + c = build_int32_array([5, 6]) + assert_equal(build_int32_array([1, 2, 3, 4, 5, 6]), + a.concatenate([b, c])) + end + + def test_mixed_type + int32_array = build_int32_array([1, 2, 3]) + uint32_array = build_uint32_array([4]) + message = + "[array][concatenate]: Invalid: " + + "arrays to be concatenated must be identically typed, " + + "but int32 and uint32 were encountered." + assert_raise(Arrow::Error::Invalid.new(message)) do + int32_array.concatenate([uint32_array]) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-binary-array.rb b/src/arrow/c_glib/test/test-binary-array.rb new file mode 100644 index 000000000..0dcaf4eef --- /dev/null +++ b/src/arrow/c_glib/test/test-binary-array.rb @@ -0,0 +1,61 @@ +# 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 TestBinaryArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + value_offsets = Arrow::Buffer.new([0, 2, 5, 5].pack("l*")) + data = Arrow::Buffer.new("\x00\x01\x02\x03\x04") + assert_equal(build_binary_array(["\x00\x01", "\x02\x03\x04", nil]), + Arrow::BinaryArray.new(3, + value_offsets, + data, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + data = "\x00\x01\x02" + builder = Arrow::BinaryArrayBuilder.new + builder.append_value(data) + array = builder.finish + assert_equal(data, array.get_value(0).to_s) + end + + def test_buffer + data1 = "\x00\x01\x02" + data2 = "\x03\x04\x05" + builder = Arrow::BinaryArrayBuilder.new + builder.append_value(data1) + builder.append_value(data2) + array = builder.finish + assert_equal(data1 + data2, array.buffer.data.to_s) + end + + def test_offsets_buffer + data1 = "\x00\x01" + data2 = "\x02\x03\x04" + builder = Arrow::BinaryArrayBuilder.new + builder.append_value(data1) + builder.append_value(data2) + array = builder.finish + byte_per_offset = 4 + assert_equal([0, 2, 5].pack("l*"), + array.offsets_buffer.data.to_s[0, byte_per_offset * 3]) + end +end diff --git a/src/arrow/c_glib/test/test-binary-data-type.rb b/src/arrow/c_glib/test/test-binary-data-type.rb new file mode 100644 index 000000000..90fec1d74 --- /dev/null +++ b/src/arrow/c_glib/test/test-binary-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestBinaryDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::BinaryDataType.new + assert_equal(Arrow::Type::BINARY, data_type.id) + end + + def test_name + data_type = Arrow::BinaryDataType.new + assert_equal("binary", data_type.name) + end + + def test_to_s + data_type = Arrow::BinaryDataType.new + assert_equal("binary", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-binary-scalar.rb b/src/arrow/c_glib/test/test-binary-scalar.rb new file mode 100644 index 000000000..4efc50da0 --- /dev/null +++ b/src/arrow/c_glib/test/test-binary-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestBinaryScalar < Test::Unit::TestCase + def setup + @buffer = Arrow::Buffer.new("\x03\x01\x02") + @scalar = Arrow::BinaryScalar.new(@buffer) + end + + def test_data_type + assert_equal(Arrow::BinaryDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::BinaryScalar.new(@buffer), + @scalar) + end + + def test_to_s + assert_equal("\x03\x01\x02", @scalar.to_s) + end + + def test_value + assert_equal(@buffer, + @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-boolean-array.rb b/src/arrow/c_glib/test/test-boolean-array.rb new file mode 100644 index 000000000..3605d613d --- /dev/null +++ b/src/arrow/c_glib/test/test-boolean-array.rb @@ -0,0 +1,90 @@ +# 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 TestBooleanArray < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_boolean_array([true, false, nil]), + Arrow::BooleanArray.new(3, + Arrow::Buffer.new([0b001].pack("C*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_export + require_gi_bindings(3, 4, 8) + array = build_boolean_array([true, false, nil]) + success, c_abi_array, c_abi_schema = array.export + data_type = Arrow::DataType.import(c_abi_schema) + assert_equal([success, array], + [true, Arrow::Array.import(c_abi_array, data_type)]) + end + + def test_buffer + builder = Arrow::BooleanArrayBuilder.new + builder.append_value(true) + builder.append_value(false) + builder.append_value(true) + array = builder.finish + assert_equal([0b101].pack("C*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::BooleanArrayBuilder.new + builder.append_value(true) + array = builder.finish + assert_equal(true, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 3, 1) + builder = Arrow::BooleanArrayBuilder.new + builder.append_value(true) + builder.append_value(false) + builder.append_value(true) + array = builder.finish + assert_equal([true, false, true], array.values) + end + + def test_invert + assert_equal(build_boolean_array([true, nil, false]), + build_boolean_array([false, nil, true]).invert) + end + + def test_and + left = build_boolean_array([true, false, nil, true]) + right = build_boolean_array([true, nil, true, false]) + assert_equal(build_boolean_array([true, nil, nil, false]), + left.and(right)) + end + + def test_or + left = build_boolean_array([true, false, nil, false]) + right = build_boolean_array([false, nil, true, false]) + assert_equal(build_boolean_array([true, nil, nil, false]), + left.or(right)) + end + + def test_xor + left = build_boolean_array([true, false, nil, true]) + right = build_boolean_array([false, nil, true, true]) + assert_equal(build_boolean_array([true, nil, nil, false]), + left.xor(right)) + end +end diff --git a/src/arrow/c_glib/test/test-boolean-data-type.rb b/src/arrow/c_glib/test/test-boolean-data-type.rb new file mode 100644 index 000000000..e9a18bee0 --- /dev/null +++ b/src/arrow/c_glib/test/test-boolean-data-type.rb @@ -0,0 +1,43 @@ +# 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 TestBooleanDataType < Test::Unit::TestCase + include Helper::Omittable + + def test_export + require_gi_bindings(3, 4, 8) + data_type = Arrow::BooleanDataType.new + c_abi_schema = data_type.export + assert_equal(data_type, + Arrow::DataType.import(c_abi_schema)) + end + + def test_type + data_type = Arrow::BooleanDataType.new + assert_equal(Arrow::Type::BOOLEAN, data_type.id) + end + + def test_name + data_type = Arrow::BooleanDataType.new + assert_equal("bool", data_type.name) + end + + def test_to_s + data_type = Arrow::BooleanDataType.new + assert_equal("bool", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-boolean-scalar.rb b/src/arrow/c_glib/test/test-boolean-scalar.rb new file mode 100644 index 000000000..f8913d6a7 --- /dev/null +++ b/src/arrow/c_glib/test/test-boolean-scalar.rb @@ -0,0 +1,52 @@ +# 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 TestBooleanScalar < Test::Unit::TestCase + def setup + @scalar = Arrow::BooleanScalar.new(true) + end + + def test_parse + assert_equal(@scalar, + Arrow::Scalar.parse(Arrow::BooleanDataType.new, + "true")) + end + + def test_data_type + assert_equal(Arrow::BooleanDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::BooleanScalar.new(true), + @scalar) + end + + def test_to_s + assert_equal("true", @scalar.to_s) + end + + def test_value + assert_equal(true, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-buffer-input-stream.rb b/src/arrow/c_glib/test/test-buffer-input-stream.rb new file mode 100644 index 000000000..e31ea38db --- /dev/null +++ b/src/arrow/c_glib/test/test-buffer-input-stream.rb @@ -0,0 +1,111 @@ +# 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 TestBufferInputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_read + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + read_buffer = buffer_input_stream.read(5) + assert_equal("Hello", read_buffer.data.to_s) + end + + def test_read_bytes + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + read_bytes = buffer_input_stream.read_bytes(5) + assert_equal("Hello", read_bytes.to_s) + end + + def test_read_at + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + read_buffer = buffer_input_stream.read_at(6, 3) + assert_equal("Wor", read_buffer.data.to_s) + end + + def test_read_at_bytes + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + read_bytes = buffer_input_stream.read_at_bytes(6, 3) + assert_equal("Wor", read_bytes.to_s) + end + + def test_advance + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + buffer_input_stream.advance(6) + read_buffer = buffer_input_stream.read(5) + assert_equal("World", read_buffer.data.to_s) + end + + def test_align + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + buffer_input_stream.advance(3) + buffer_input_stream.align(8) + read_buffer = buffer_input_stream.read(3) + assert_equal("rld", read_buffer.data.to_s) + end + + def test_peek + buffer = Arrow::Buffer.new("Hello World") + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + peeked_data = buffer_input_stream.peek(5) + assert_equal(buffer_input_stream.read(5).data.to_s, + peeked_data.to_s) + end + + def test_gio_input_stream + # U+3042 HIRAGANA LETTER A + data = "\u3042" + convert_encoding = "cp932" + buffer = Arrow::Buffer.new(data) + buffer_input_stream = Arrow::BufferInputStream.new(buffer) + converter = Gio::CharsetConverter.new(convert_encoding, "UTF-8") + convert_input_stream = + Gio::ConverterInputStream.new(buffer_input_stream, converter) + gio_input_stream = Arrow::GIOInputStream.new(convert_input_stream) + raw_read_data = gio_input_stream.read(10).data.to_s + assert_equal(data.encode(convert_encoding), + raw_read_data.dup.force_encoding(convert_encoding)) + end + + def test_read_record_batch + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + columns = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + record_batch = Arrow::RecordBatch.new(schema, 1, columns) + + buffer = Arrow::ResizableBuffer.new(0) + output_stream = Arrow::BufferOutputStream.new(buffer) + output_stream.write_record_batch(record_batch) + output_stream.close + + input_stream = Arrow::BufferInputStream.new(buffer) + options = Arrow::ReadOptions.new + assert_equal(record_batch, + input_stream.read_record_batch(schema, options)) + end +end diff --git a/src/arrow/c_glib/test/test-buffer-output-stream.rb b/src/arrow/c_glib/test/test-buffer-output-stream.rb new file mode 100644 index 000000000..9866762ff --- /dev/null +++ b/src/arrow/c_glib/test/test-buffer-output-stream.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 TestBufferOutputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_new + buffer = Arrow::ResizableBuffer.new(0) + output_stream = Arrow::BufferOutputStream.new(buffer) + output_stream.write("Hello") + output_stream.close + assert_equal("Hello", buffer.data.to_s) + end + + def test_align + buffer = Arrow::ResizableBuffer.new(0) + output_stream = Arrow::BufferOutputStream.new(buffer) + output_stream.write("Hello") + output_stream.align(8) + output_stream.close + assert_equal("Hello\x00\x00\x00", buffer.data.to_s) + end + + def test_write_record_batch + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + columns = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + record_batch = Arrow::RecordBatch.new(schema, 1, columns) + + buffer = Arrow::ResizableBuffer.new(0) + options = Arrow::WriteOptions.new + output_stream = Arrow::BufferOutputStream.new(buffer) + output_stream.write_record_batch(record_batch, options) + output_stream.close + + input_stream = Arrow::BufferInputStream.new(buffer) + assert_equal(record_batch, + input_stream.read_record_batch(schema)) + end +end diff --git a/src/arrow/c_glib/test/test-buffer.rb b/src/arrow/c_glib/test/test-buffer.rb new file mode 100644 index 000000000..0617eac04 --- /dev/null +++ b/src/arrow/c_glib/test/test-buffer.rb @@ -0,0 +1,102 @@ +# 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 TestBuffer < Test::Unit::TestCase + include Helper::Omittable + + def setup + @data = "Hello" + @buffer = Arrow::Buffer.new(@data) + end + + def test_new_bytes + bytes_data = GLib::Bytes.new(@data) + buffer = Arrow::Buffer.new(bytes_data) + assert_equal([ + bytes_data.pointer, + @data, + ], + [ + buffer.data.pointer, + buffer.data.to_s, + ]) + end + + def test_new_bytes_slice + bytes_data = GLib::Bytes.new(@data) + buffer = Arrow::Buffer.new(bytes_data) + sliced_buffer = buffer.slice(1, 3) + assert_equal([ + bytes_data.pointer + 1, + @data[1, 3], + ], + [ + sliced_buffer.data.pointer, + sliced_buffer.data.to_s, + ]) + end + + def test_equal + assert_equal(@buffer, + Arrow::Buffer.new(@data.dup)) + end + + def test_equal_n_bytes + buffer1 = Arrow::Buffer.new("Hello!") + buffer2 = Arrow::Buffer.new("Hello World!") + assert do + buffer1.equal_n_bytes(buffer2, 5) + end + end + + def test_mutable? + assert do + not @buffer.mutable? + end + end + + def test_capacity + assert_equal(@data.bytesize, @buffer.capacity) + end + + def test_data + assert_equal(@data, @buffer.data.to_s) + end + + def test_mutable_data + require_gi_bindings(3, 1, 2) + assert_nil(@buffer.mutable_data) + end + + def test_size + assert_equal(@data.bytesize, @buffer.size) + end + + def test_parent + assert_nil(@buffer.parent) + end + + def test_copy + copied_buffer = @buffer.copy(1, 3) + assert_equal(@data[1, 3], copied_buffer.data.to_s) + end + + def test_slice + sliced_buffer = @buffer.slice(1, 3) + assert_equal(@data[1, 3], sliced_buffer.data.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-call-expression.rb b/src/arrow/c_glib/test/test-call-expression.rb new file mode 100644 index 000000000..a8ba2f4cb --- /dev/null +++ b/src/arrow/c_glib/test/test-call-expression.rb @@ -0,0 +1,42 @@ +# 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 TestCallExpression < Test::Unit::TestCase + def setup + @arguments = [ + Arrow::FieldExpression.new("augend"), + Arrow::FieldExpression.new("addend"), + ] + @expression = Arrow::CallExpression.new("add", @arguments) + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::CallExpression.new("now", []), + Arrow::CallExpression.new("now", [])) + end + + def test_false + assert_not_equal(Arrow::CallExpression.new("a", []), + Arrow::CallExpression.new("b", [])) + end + end + + def test_to_string + assert_equal("add(augend, addend)", @expression.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-cast.rb b/src/arrow/c_glib/test/test-cast.rb new file mode 100644 index 000000000..528a0e8c1 --- /dev/null +++ b/src/arrow/c_glib/test/test-cast.rb @@ -0,0 +1,145 @@ +# 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 TestCast < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_safe + data = [-1, 2, nil] + assert_equal(build_int32_array(data), + build_int8_array(data).cast(Arrow::Int32DataType.new)) + end + + sub_test_case("allow-int-overflow") do + def test_default + assert_raise(Arrow::Error::Invalid) do + build_int32_array([128]).cast(Arrow::Int8DataType.new) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_int_overflow = true + assert_equal(build_int8_array([-128]), + build_int32_array([128]).cast(Arrow::Int8DataType.new, + options)) + end + end + + sub_test_case("allow-time-truncate") do + def test_default + after_epoch_in_milli = 1504953190854 # 2017-09-09T10:33:10.854Z + second_timestamp_data_type = Arrow::TimestampDataType.new(:second) + milli_array = build_timestamp_array(:milli, [after_epoch_in_milli]) + assert_raise(Arrow::Error::Invalid) do + milli_array.cast(second_timestamp_data_type) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_time_truncate = true + after_epoch_in_milli = 1504953190854 # 2017-09-09T10:33:10.854Z + second_array = build_timestamp_array(:second, + [after_epoch_in_milli / 1000]) + milli_array = build_timestamp_array(:milli, [after_epoch_in_milli]) + second_timestamp_data_type = Arrow::TimestampDataType.new(:second) + assert_equal(second_array, + milli_array.cast(second_timestamp_data_type, options)) + end + end + + sub_test_case("allow-time-overflow") do + def test_default + after_epoch_in_second = 95617584000 # 5000-01-01T00:00:00Z + nano_timestamp_data_type = Arrow::TimestampDataType.new(:nano) + second_array = build_timestamp_array(:second, [after_epoch_in_second]) + assert_raise(Arrow::Error::Invalid) do + second_array.cast(nano_timestamp_data_type) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_time_overflow = true + after_epoch_in_second = 95617584000 # 5000-01-01T00:00:00Z + second_array = build_timestamp_array(:second, + [after_epoch_in_second]) + after_epoch_in_nano_overflowed = + (after_epoch_in_second * 1000 * 1000 * 1000) % (2 ** 64) + nano_array = build_timestamp_array(:nano, + [after_epoch_in_nano_overflowed]) + nano_timestamp_data_type = Arrow::TimestampDataType.new(:nano) + assert_equal(nano_array, + second_array.cast(nano_timestamp_data_type, options)) + end + end + + sub_test_case("allow-decimal-truncate") do + def test_default + decimal128_data_type = Arrow::Decimal128DataType.new(8, 2) + decimal128_array = build_decimal128_array(decimal128_data_type, + ["23423445"]) + assert_raise(Arrow::Error::Invalid) do + decimal128_array.cast(Arrow::Int64DataType.new) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_decimal_truncate = true + decimal128_data_type = Arrow::Decimal128DataType.new(8, 2) + decimal128_array = build_decimal128_array(decimal128_data_type, + ["23423445"]) + assert_equal(build_int64_array([234234]), + decimal128_array.cast(Arrow::Int64DataType.new, options)) + end + end + + sub_test_case("allow-float-truncate") do + def test_default + assert_raise(Arrow::Error::Invalid) do + build_float_array([1.1]).cast(Arrow::Int8DataType.new) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_float_truncate = true + int8_data_type = Arrow::Int8DataType.new + assert_equal(build_int8_array([1]), + build_float_array([1.1]).cast(int8_data_type, options)) + end + end + + sub_test_case("allow-invalid-utf8") do + def test_default + assert_raise(Arrow::Error::Invalid) do + build_binary_array(["\xff"]).cast(Arrow::StringDataType.new) + end + end + + def test_true + options = Arrow::CastOptions.new + options.allow_invalid_utf8 = true + string_data_type = Arrow::StringDataType.new + assert_equal(build_string_array(["\xff"]), + build_binary_array(["\xff"]).cast(string_data_type, options)) + end + end +end diff --git a/src/arrow/c_glib/test/test-chunked-array-datum.rb b/src/arrow/c_glib/test/test-chunked-array-datum.rb new file mode 100644 index 000000000..763173153 --- /dev/null +++ b/src/arrow/c_glib/test/test-chunked-array-datum.rb @@ -0,0 +1,58 @@ +# 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 TestChunkedArrayDatum < Test::Unit::TestCase + include Helper::Buildable + + def setup + @array = build_boolean_array([true, false]) + @chunked_array = Arrow::ChunkedArray.new([@array]) + @datum = Arrow::ChunkedArrayDatum.new(@chunked_array) + end + + def test_array? + assert do + not @datum.array? + end + end + + def test_array_like? + assert do + @datum.array_like? + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::ChunkedArrayDatum.new(@chunked_array), + Arrow::ChunkedArrayDatum.new(@chunked_array)) + end + + def test_false + assert_not_equal(@datum, + Arrow::ArrayDatum.new(@array)) + end + end + + def test_to_string + assert_equal("ChunkedArray", @datum.to_s) + end + + def test_value + assert_equal(@chunked_array, @datum.value) + end +end diff --git a/src/arrow/c_glib/test/test-chunked-array.rb b/src/arrow/c_glib/test/test-chunked-array.rb new file mode 100644 index 000000000..8f912ac84 --- /dev/null +++ b/src/arrow/c_glib/test/test-chunked-array.rb @@ -0,0 +1,141 @@ +# 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 TestChunkedArray < Test::Unit::TestCase + include Helper::Buildable + + def test_equal + chunks1 = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + chunks2 = [ + build_boolean_array([true]), + build_boolean_array([false, true]), + ] + assert_equal(Arrow::ChunkedArray.new(chunks1), + Arrow::ChunkedArray.new(chunks2)) + end + + def test_value_data_type + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + assert_equal(Arrow::BooleanDataType.new, + Arrow::ChunkedArray.new(chunks).value_data_type) + end + + def test_value_type + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + assert_equal(Arrow::Type::BOOLEAN, + Arrow::ChunkedArray.new(chunks).value_type) + end + + def test_n_rows + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(3, chunked_array.n_rows) + end + + def test_n_nulls + chunks = [ + build_boolean_array([true, nil, false]), + build_boolean_array([nil, nil, true]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(3, chunked_array.n_nulls) + end + + + def test_n_chunks + chunks = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(2, chunked_array.n_chunks) + end + + def test_chunk + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([false]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(2, chunked_array.get_chunk(0).length) + end + + def test_chunks + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([false]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal([2, 1], + chunked_array.chunks.collect(&:length)) + end + + def test_slice + chunks1 = [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true]), + ] + chunks2 = [ + build_boolean_array([false, true]), + build_boolean_array([false]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks1) + sub_chunked_array = chunked_array.slice(1, 3) + assert_equal(chunks2, sub_chunked_array.chunks) + end + + def test_to_s + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(<<-PRETTY_PRINT.chomp, chunked_array.to_s) +[ + [ + true, + false + ], + [ + true + ] +] + PRETTY_PRINT + end + + def test_combine + chunks = [ + build_boolean_array([true]), + build_boolean_array([false, nil]), + ] + chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(build_boolean_array([true, false, nil]), + chunked_array.combine) + end +end diff --git a/src/arrow/c_glib/test/test-codec.rb b/src/arrow/c_glib/test/test-codec.rb new file mode 100644 index 000000000..a32ec4dc7 --- /dev/null +++ b/src/arrow/c_glib/test/test-codec.rb @@ -0,0 +1,33 @@ +# 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 TestCodec < Test::Unit::TestCase + def test_name + codec = Arrow::Codec.new(:gzip) + assert_equal("gzip", codec.name) + end + + def test_compression_type + codec = Arrow::Codec.new(:gzip) + assert_equal(Arrow::CompressionType::GZIP, codec.compression_type) + end + + def test_compression_level + codec = Arrow::Codec.new(:gzip) + assert_equal(9, codec.compression_level) + end +end diff --git a/src/arrow/c_glib/test/test-compressed-input-stream.rb b/src/arrow/c_glib/test/test-compressed-input-stream.rb new file mode 100644 index 000000000..71f230a50 --- /dev/null +++ b/src/arrow/c_glib/test/test-compressed-input-stream.rb @@ -0,0 +1,45 @@ +# 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 TestCompressedInputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_read + data = "Hello" + + output = StringIO.new + Zlib::GzipWriter.wrap(output) do |gz| + gz.write(data) + end + + codec = Arrow::Codec.new(:gzip) + buffer = Arrow::Buffer.new(output.string) + raw_input = Arrow::BufferInputStream.new(buffer) + input = Arrow::CompressedInputStream.new(codec, raw_input) + assert_equal(data, input.read(data.bytesize).data.to_s) + input.close + raw_input.close + end + + def test_raw + buffer = Arrow::Buffer.new("Hello") + raw_input = Arrow::BufferInputStream.new(buffer) + codec = Arrow::Codec.new(:gzip) + input = Arrow::CompressedInputStream.new(codec, raw_input) + assert_equal(raw_input, input.raw) + end +end diff --git a/src/arrow/c_glib/test/test-compressed-output-stream.rb b/src/arrow/c_glib/test/test-compressed-output-stream.rb new file mode 100644 index 000000000..eb54a45d3 --- /dev/null +++ b/src/arrow/c_glib/test/test-compressed-output-stream.rb @@ -0,0 +1,43 @@ +# 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 TestCompressedOutputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_write + data = "Hello" + buffer = Arrow::ResizableBuffer.new(8) + raw_output = Arrow::BufferOutputStream.new(buffer) + codec = Arrow::Codec.new(:gzip) + output = Arrow::CompressedOutputStream.new(codec, raw_output) + output.write(data) + output.close + + input = StringIO.new(buffer.data.to_s) + Zlib::GzipReader.wrap(input) do |gz| + assert_equal(data, gz.read) + end + end + + def test_raw + buffer = Arrow::ResizableBuffer.new(8) + raw_output = Arrow::BufferOutputStream.new(buffer) + codec = Arrow::Codec.new(:gzip) + output = Arrow::CompressedOutputStream.new(codec, raw_output) + assert_equal(raw_output, output.raw) + end +end diff --git a/src/arrow/c_glib/test/test-count-values.rb b/src/arrow/c_glib/test/test-count-values.rb new file mode 100644 index 000000000..46c36cf47 --- /dev/null +++ b/src/arrow/c_glib/test/test-count-values.rb @@ -0,0 +1,51 @@ +# 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 TestCountValues < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_int32 + array = build_int32_array([1, 3, 1, -1, -3, -1]) + fields = [ + Arrow::Field.new("values", Arrow::Int32DataType.new), + Arrow::Field.new("counts", Arrow::Int64DataType.new), + ] + structs = [ + {"values" => 1, "counts" => 2}, + {"values" => 3, "counts" => 1}, + {"values" => -1, "counts" => 2}, + {"values" => -3, "counts" => 1}, + ] + assert_equal(build_struct_array(fields, structs), + array.count_values) + end + + def test_string + array = build_string_array(["Ruby", "Python", "Ruby"]) + fields = [ + Arrow::Field.new("values", Arrow::StringDataType.new), + Arrow::Field.new("counts", Arrow::Int64DataType.new), + ] + structs = [ + {"values" => "Ruby", "counts" => 2}, + {"values" => "Python", "counts" => 1}, + ] + assert_equal(build_struct_array(fields, structs), + array.count_values) + end +end diff --git a/src/arrow/c_glib/test/test-count.rb b/src/arrow/c_glib/test/test-count.rb new file mode 100644 index 000000000..6e9421914 --- /dev/null +++ b/src/arrow/c_glib/test/test-count.rb @@ -0,0 +1,43 @@ +# 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 TestCount < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case("mode") do + def test_default + assert_equal(2, build_int32_array([1, nil, 3]).count) + + options = Arrow::CountOptions.new + options.mode = Arrow::CountMode::ONLY_VALID + assert_equal(2, build_int32_array([1, nil, 3]).count(options)) + end + + def test_nulls + options = Arrow::CountOptions.new + options.mode = Arrow::CountMode::ONLY_NULL + assert_equal(1, build_int32_array([1, nil, 3]).count(options)) + end + + def test_all + options = Arrow::CountOptions.new + options.mode = Arrow::CountMode::ALL + assert_equal(3, build_int32_array([1, nil, 3]).count(options)) + end + end +end diff --git a/src/arrow/c_glib/test/test-csv-reader.rb b/src/arrow/c_glib/test/test-csv-reader.rb new file mode 100644 index 000000000..018f062ac --- /dev/null +++ b/src/arrow/c_glib/test/test-csv-reader.rb @@ -0,0 +1,241 @@ +# 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 TestCSVReader < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case("#read") do + def open_input(csv) + buffer = Arrow::Buffer.new(csv) + Arrow::BufferInputStream.new(buffer) + end + + def test_default + table = Arrow::CSVReader.new(open_input(<<-CSV)) +message,count +"Start",2 +"Shutdown",9 + CSV + columns = { + "message" => build_string_array(["Start", "Shutdown"]), + "count" => build_int64_array([2, 9]), + } + assert_equal(build_table(columns), + table.read) + end + + sub_test_case("options") do + def test_add_column_type + options = Arrow::CSVReadOptions.new + options.add_column_type("count", Arrow::UInt8DataType.new) + options.add_column_type("valid", Arrow::BooleanDataType.new) + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +count,valid +2,1 +9,0 + CSV + columns = { + "count" => build_uint8_array([2, 9]), + "valid" => build_boolean_array([true, false]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_add_schema + options = Arrow::CSVReadOptions.new + fields = [ + Arrow::Field.new("count", Arrow::UInt8DataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + options.add_schema(schema) + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +count,valid +2,1 +9,0 + CSV + columns = { + "count" => build_uint8_array([2, 9]), + "valid" => build_boolean_array([true, false]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_column_types + require_gi_bindings(3, 3, 1) + options = Arrow::CSVReadOptions.new + options.add_column_type("count", Arrow::UInt8DataType.new) + options.add_column_type("valid", Arrow::BooleanDataType.new) + assert_equal({ + "count" => Arrow::UInt8DataType.new, + "valid" => Arrow::BooleanDataType.new, + }, + options.column_types) + end + + def test_null_values + options = Arrow::CSVReadOptions.new + null_values = ["2", "5"] + options.null_values = null_values + assert_equal(null_values, options.null_values) + + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +message,count +"Start",2 +"Shutdown",9 +"Restart",5 + CSV + columns = { + "message" => build_string_array(["Start", "Shutdown", "Restart"]), + "count" => build_int64_array([nil, 9, nil]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_add_null_value + options = Arrow::CSVReadOptions.new + null_values = ["2", "5"] + options.null_values = null_values + options.add_null_value("9") + assert_equal(null_values + ["9"], options.null_values) + end + + def test_boolean_values + options = Arrow::CSVReadOptions.new + true_values = ["Start", "Restart"] + options.true_values = true_values + assert_equal(true_values, options.true_values) + + false_values = ["Shutdown"] + options.false_values = false_values + assert_equal(false_values, options.false_values) + + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +message,count +"Start",2 +"Shutdown",9 +"Restart",5 + CSV + columns = { + "message" => build_boolean_array([true, false, true]), + "count" => build_int64_array([2, 9, 5]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_add_true_value + options = Arrow::CSVReadOptions.new + true_values = ["Start", "Restart"] + options.true_values = true_values + options.add_true_value("Shutdown") + assert_equal(true_values + ["Shutdown"], options.true_values) + end + + def test_add_false_value + options = Arrow::CSVReadOptions.new + false_values = ["Start", "Restart"] + options.false_values = false_values + options.add_false_value("Shutdown") + assert_equal(false_values + ["Shutdown"], options.false_values) + end + + def test_allow_null_strings + options = Arrow::CSVReadOptions.new + options.null_values = ["Start", "Restart"] + options.allow_null_strings = true + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +message,count +"Start",2 +"Shutdown",9 +"Restart",5 + CSV + columns = { + "message" => build_string_array([nil, "Shutdown", nil]), + "count" => build_int64_array([2, 9, 5]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_n_skip_rows + options = Arrow::CSVReadOptions.new + options.n_skip_rows = 1 + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +message1,message2 +"Start1","Start2" +"Shutdown1","Shutdown2" +"Reboot1","Reboot2" + CSV + columns = { + "Start1" => build_string_array(["Shutdown1", "Reboot1"]), + "Start2" => build_string_array(["Shutdown2", "Reboot2"]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_column_names + options = Arrow::CSVReadOptions.new + column_names = ["message", "count"] + options.column_names = column_names + assert_equal(column_names, options.column_names) + + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +"Start",2 +"Shutdown",9 +"Reboot",5 + CSV + columns = { + "message" => build_string_array(["Start", "Shutdown", "Reboot"]), + "count" => build_int64_array([2, 9, 5]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_add_column_name + options = Arrow::CSVReadOptions.new + column_names = ["message", "count"] + options.column_names = column_names + options.add_column_name("score") + assert_equal(column_names + ["score"], options.column_names) + end + + def test_generate_column_names + options = Arrow::CSVReadOptions.new + options.generate_column_names = true + + table = Arrow::CSVReader.new(open_input(<<-CSV), options) +"Start",2 +"Shutdown",9 +"Reboot",5 + CSV + columns = { + "f0" => build_string_array(["Start", "Shutdown", "Reboot"]), + "f1" => build_int64_array([2, 9, 5]), + } + assert_equal(build_table(columns), + table.read) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-cuda.rb b/src/arrow/c_glib/test/test-cuda.rb new file mode 100644 index 000000000..c9b349293 --- /dev/null +++ b/src/arrow/c_glib/test/test-cuda.rb @@ -0,0 +1,159 @@ +# 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 TestCUDA < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def setup + omit("Arrow CUDA is required") unless defined?(::ArrowCUDA) + @manager = ArrowCUDA::DeviceManager.new + omit("At least one GPU is required") if @manager.n_devices.zero? + @context = @manager.get_context(0) + end + + sub_test_case("Context") do + def test_allocated_size + allocated_size_before = @context.allocated_size + size = 128 + buffer = ArrowCUDA::Buffer.new(@context, size) + assert_equal(size, + @context.allocated_size - allocated_size_before) + end + end + + sub_test_case("Buffer") do + def setup + super + @buffer = ArrowCUDA::Buffer.new(@context, 128) + end + + def test_copy + @buffer.copy_from_host("Hello World") + assert_equal("llo W", @buffer.copy_to_host(2, 5).to_s) + end + + def test_export + require_gi_bindings(3, 3, 9) + @buffer.copy_from_host("Hello World") + handle = @buffer.export + serialized_handle = handle.serialize.data + Tempfile.open("arrow-cuda-export") do |output| + pid = spawn(RbConfig.ruby, "-e", <<-SCRIPT) +require "gi" + +Gio = GI.load("Gio") +Arrow = GI.load("Arrow") +ArrowCUDA = GI.load("ArrowCUDA") + +manager = ArrowCUDA::DeviceManager.new +context = manager.get_context(0) +serialized_handle = #{serialized_handle.to_s.dump} +handle = ArrowCUDA::IPCMemoryHandle.new(serialized_handle) +buffer = ArrowCUDA::Buffer.new(context, handle) +File.open(#{output.path.dump}, "w") do |output| + output.print(buffer.copy_to_host(0, 6).to_s) +end + SCRIPT + Process.waitpid(pid) + assert_equal("Hello ", output.read) + end + end + + def test_context + assert_equal(@context.allocated_size, + @buffer.context.allocated_size) + end + + sub_test_case("#read_record_batch") do + def setup + super + @field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + @schema = Arrow::Schema.new([@field]) + @columns = [ + build_boolean_array([true]), + ] + @cpu_record_batch = Arrow::RecordBatch.new(@schema, 1, @columns) + + @buffer = ArrowCUDA::Buffer.new(@context, @cpu_record_batch) + end + + def test_default + gpu_record_batch = @buffer.read_record_batch(@schema) + assert_equal(@cpu_record_batch.n_rows, + gpu_record_batch.n_rows) + end + + def test_options + options = Arrow::ReadOptions.new + gpu_record_batch = @buffer.read_record_batch(@schema, options) + assert_equal(@cpu_record_batch.n_rows, + gpu_record_batch.n_rows) + end + end + end + + sub_test_case("HostBuffer") do + def test_new + buffer = ArrowCUDA::HostBuffer.new(0, 128) + assert_equal(128, buffer.size) + end + end + + sub_test_case("BufferInputStream") do + def test_new + buffer = ArrowCUDA::Buffer.new(@context, 128) + buffer.copy_from_host("Hello World") + stream = ArrowCUDA::BufferInputStream.new(buffer) + begin + assert_equal("Hello Worl", stream.read(5).copy_to_host(0, 10).to_s) + ensure + stream.close + end + end + end + + sub_test_case("BufferOutputStream") do + def setup + super + @buffer = ArrowCUDA::Buffer.new(@context, 128) + @buffer.copy_from_host("\x00" * @buffer.size) + @stream = ArrowCUDA::BufferOutputStream.new(@buffer) + end + + def cleanup + super + @stream.close + end + + def test_new + @stream.write("Hello World") + assert_equal("Hello World", @buffer.copy_to_host(0, 11).to_s) + end + + def test_buffer + assert_equal(0, @stream.buffer_size) + @stream.buffer_size = 5 + assert_equal(5, @stream.buffer_size) + @stream.write("Hell") + assert_equal(4, @stream.buffered_size) + assert_equal("\x00" * 5, @buffer.copy_to_host(0, 5).to_s) + @stream.write("o") + assert_equal("Hello", @buffer.copy_to_host(0, 5).to_s) + end + end +end diff --git a/src/arrow/c_glib/test/test-date32-array.rb b/src/arrow/c_glib/test/test-date32-array.rb new file mode 100644 index 000000000..09ef78650 --- /dev/null +++ b/src/arrow/c_glib/test/test-date32-array.rb @@ -0,0 +1,65 @@ +# 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 + include Helper::Buildable + include Helper::Omittable + + def test_new + after_epoch = 17406 # 2017-08-28 + raw_data = [0, after_epoch] + assert_equal(build_date32_array([*raw_data, nil]), + Arrow::Date32Array.new(3, + Arrow::Buffer.new(raw_data.pack("l*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + before_epoch = -3653 # 1960-01-01 + after_epoch = 17406 # 2017-08-28 + + builder = Arrow::Date32ArrayBuilder.new + builder.append_value(0) + builder.append_value(after_epoch) + builder.append_value(before_epoch) + array = builder.finish + assert_equal([0, after_epoch, before_epoch].pack("l*"), + array.buffer.data.to_s) + end + + def test_value + after_epoch = 17406 # 2017-08-28 + + builder = Arrow::Date32ArrayBuilder.new + builder.append_value(after_epoch) + array = builder.finish + assert_equal(after_epoch, array.get_value(0)) + end + + def test_values + before_epoch = -3653 # 1960-01-01 + after_epoch = 17406 # 2017-08-28 + + builder = Arrow::Date32ArrayBuilder.new + builder.append_value(0) + builder.append_value(after_epoch) + builder.append_value(before_epoch) + array = builder.finish + assert_equal([0, after_epoch, before_epoch], array.values) + end +end diff --git a/src/arrow/c_glib/test/test-date32-data-type.rb b/src/arrow/c_glib/test/test-date32-data-type.rb new file mode 100644 index 000000000..e3c80e106 --- /dev/null +++ b/src/arrow/c_glib/test/test-date32-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestDate32DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Date32DataType.new + assert_equal(Arrow::Type::DATE32, data_type.id) + end + + def test_name + data_type = Arrow::Date32DataType.new + assert_equal("date32", data_type.name) + end + + def test_to_s + data_type = Arrow::Date32DataType.new + assert_equal("date32[day]", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-date32-scalar.rb b/src/arrow/c_glib/test/test-date32-scalar.rb new file mode 100644 index 000000000..ae41ebf72 --- /dev/null +++ b/src/arrow/c_glib/test/test-date32-scalar.rb @@ -0,0 +1,47 @@ +# 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 TestDate32Scalar < Test::Unit::TestCase + def setup + @value = 17406 # 2017-08-28 + @scalar = Arrow::Date32Scalar.new(@value) + end + + def test_data_type + assert_equal(Arrow::Date32DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Date32Scalar.new(@value), + @scalar) + end + + def test_to_s + assert_equal("2017-08-28", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-date64-array.rb b/src/arrow/c_glib/test/test-date64-array.rb new file mode 100644 index 000000000..4d9f18919 --- /dev/null +++ b/src/arrow/c_glib/test/test-date64-array.rb @@ -0,0 +1,65 @@ +# 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 + include Helper::Buildable + include Helper::Omittable + + def test_new + after_epoch = 1503878400000 # 2017-08-28T00:00:00Z + raw_data = [0, after_epoch] + assert_equal(build_date64_array([*raw_data, nil]), + Arrow::Date64Array.new(3, + Arrow::Buffer.new(raw_data.pack("q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + before_epoch = -315619200 # 1960-01-01T00:00:00Z + after_epoch = 1503878400000 # 2017-08-28T00:00:00Z + + builder = Arrow::Date64ArrayBuilder.new + builder.append_value(0) + builder.append_value(after_epoch) + builder.append_value(before_epoch) + array = builder.finish + assert_equal([0, after_epoch, before_epoch].pack("q*"), + array.buffer.data.to_s) + end + + def test_value + after_epoch = 1503878400000 # 2017-08-28T00:00:00Z + + builder = Arrow::Date64ArrayBuilder.new + builder.append_value(after_epoch) + array = builder.finish + assert_equal(after_epoch, array.get_value(0)) + end + + def test_values + before_epoch = -315619200 # 1960-01-01T00:00:00Z + after_epoch = 1503878400000 # 2017-08-28T00:00:00Z + + builder = Arrow::Date64ArrayBuilder.new + builder.append_value(0) + builder.append_value(after_epoch) + builder.append_value(before_epoch) + array = builder.finish + assert_equal([0, after_epoch, before_epoch], array.values) + end +end diff --git a/src/arrow/c_glib/test/test-date64-data-type.rb b/src/arrow/c_glib/test/test-date64-data-type.rb new file mode 100644 index 000000000..5a5ccbac5 --- /dev/null +++ b/src/arrow/c_glib/test/test-date64-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestDate64DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Date64DataType.new + assert_equal(Arrow::Type::DATE64, data_type.id) + end + + def test_name + data_type = Arrow::Date64DataType.new + assert_equal("date64", data_type.name) + end + + def test_to_s + data_type = Arrow::Date64DataType.new + assert_equal("date64[ms]", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-date64-scalar.rb b/src/arrow/c_glib/test/test-date64-scalar.rb new file mode 100644 index 000000000..ce39d3c2d --- /dev/null +++ b/src/arrow/c_glib/test/test-date64-scalar.rb @@ -0,0 +1,47 @@ +# 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 TestDate64Scalar < Test::Unit::TestCase + def setup + @value = 1503878400000 # 2017-08-28T00:00:00Z + @scalar = Arrow::Date64Scalar.new(@value) + end + + def test_data_type + assert_equal(Arrow::Date64DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Date64Scalar.new(@value), + @scalar) + end + + def test_to_s + assert_equal("2017-08-28", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-decimal128-array.rb b/src/arrow/c_glib/test/test-decimal128-array.rb new file mode 100644 index 000000000..132ceb778 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal128-array.rb @@ -0,0 +1,37 @@ +# 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 TestDecimal128Array < Test::Unit::TestCase + def test_format_value + data_type = Arrow::Decimal128DataType.new(8, 2) + builder = Arrow::Decimal128ArrayBuilder.new(data_type) + decimal = Arrow::Decimal128.new("23423445") + builder.append_value(decimal) + array = builder.finish + assert_equal("234234.45", array.format_value(0)) + end + + def test_value + data_type = Arrow::Decimal128DataType.new(8, 2) + builder = Arrow::Decimal128ArrayBuilder.new(data_type) + decimal = Arrow::Decimal128.new("23423445") + builder.append_value(decimal) + array = builder.finish + assert_equal("234234.45", + array.get_value(0).to_string_scale(array.value_data_type.scale)) + end +end diff --git a/src/arrow/c_glib/test/test-decimal128-data-type.rb b/src/arrow/c_glib/test/test-decimal128-data-type.rb new file mode 100644 index 000000000..b27e1cad1 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal128-data-type.rb @@ -0,0 +1,43 @@ +# 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 TestDecimal128DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Decimal128DataType.new(2, 0) + assert_equal(Arrow::Type::DECIMAL128, data_type.id) + end + + def test_name + data_type = Arrow::Decimal128DataType.new(2, 0) + assert_equal("decimal128", data_type.name) + end + + def test_to_s + data_type = Arrow::Decimal128DataType.new(2, 0) + assert_equal("decimal128(2, 0)", data_type.to_s) + end + + def test_precision + data_type = Arrow::Decimal128DataType.new(8, 2) + assert_equal(8, data_type.precision) + end + + def test_scale + data_type = Arrow::Decimal128DataType.new(8, 2) + assert_equal(2, data_type.scale) + end +end diff --git a/src/arrow/c_glib/test/test-decimal128-scalar.rb b/src/arrow/c_glib/test/test-decimal128-scalar.rb new file mode 100644 index 000000000..380623a67 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal128-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestDecimal128Scalar < Test::Unit::TestCase + def setup + @data_type = Arrow::Decimal128DataType.new(8, 2) + @value = Arrow::Decimal128.new("23423445") + @scalar = Arrow::Decimal128Scalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Decimal128Scalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("234234.45", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-decimal128.rb b/src/arrow/c_glib/test/test-decimal128.rb new file mode 100644 index 000000000..8f14cfbe5 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal128.rb @@ -0,0 +1,233 @@ +# 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 TestDecimal128 < Test::Unit::TestCase + include Helper::Omittable + + def test_copy + decimal = Arrow::Decimal128.new("234.23445") + assert_equal(decimal, decimal.copy) + end + + def test_to_string_scale + integer_data = 23423445 + string_data = "234.23445" + decimal = Arrow::Decimal128.new(integer_data) + assert_equal(string_data, decimal.to_string_scale(5)) + end + + def test_to_string + string_data = "99999999999999999999999999999999999999" + decimal = Arrow::Decimal128.new(string_data) + assert_equal(string_data, decimal.to_s) + end + + def test_to_bytes + decimal = Arrow::Decimal128.new("12.3") + assert_equal([123, 0].pack("q*"), + decimal.to_bytes.to_s) + end + + def test_abs + absolute_value = "23049223942343532412" + negative_value = "-23049223942343532412" + decimal = Arrow::Decimal128.new(negative_value) + decimal.abs + assert_equal(absolute_value, decimal.to_s) + end + + def test_negate + positive_value = "23049223942343532412" + negative_value = "-23049223942343532412" + decimal = Arrow::Decimal128.new(positive_value) + decimal.negate + assert_equal(negative_value, decimal.to_s) + decimal.negate + assert_equal(positive_value, decimal.to_s) + end + + def test_to_integer + integer_data = 999999999999999999 + decimal = Arrow::Decimal128.new(integer_data) + assert_equal(integer_data, decimal.to_i) + end + + def test_plus + integer_data1 = 23423445 + integer_data2 = 5443 + decimal1 = Arrow::Decimal128.new(integer_data1) + decimal2 = Arrow::Decimal128.new(integer_data2) + decimal3 = decimal1.plus(decimal2) + assert_equal(integer_data1 + integer_data2, decimal3.to_i) + end + + def test_minus + integer_data1 = 23423445 + integer_data2 = 5443 + decimal1 = Arrow::Decimal128.new(integer_data1) + decimal2 = Arrow::Decimal128.new(integer_data2) + decimal3 = decimal1.minus(decimal2) + assert_equal(integer_data1 - integer_data2, decimal3.to_i) + end + + def test_multiply + integer_data1 = 23423445 + integer_data2 = 5443 + decimal1 = Arrow::Decimal128.new(integer_data1) + decimal2 = Arrow::Decimal128.new(integer_data2) + decimal3 = decimal1.multiply(decimal2) + assert_equal(integer_data1 * integer_data2, decimal3.to_i) + end + + def test_divide + require_gi_bindings(3, 3, 0) + integer_data1 = 23423445 + integer_data2 = -5443 + decimal1 = Arrow::Decimal128.new(integer_data1) + decimal2 = Arrow::Decimal128.new(integer_data2) + result, remainder = decimal1.divide(decimal2) + assert_equal([ + integer_data1.quo(integer_data2).truncate, + integer_data1.remainder(integer_data2), + ], + [result.to_i, remainder.to_i]) + end + + def test_divide_zero + require_gi_bindings(3, 3, 0) + decimal1 = Arrow::Decimal128.new(23423445) + decimal2 = Arrow::Decimal128.new(0) + message = + "[decimal128][divide]: Invalid: Division by 0 in Decimal128" + assert_raise(Arrow::Error::Invalid.new(message)) do + decimal1.divide(decimal2) + end + end + + def test_equal + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(10) + other_decimal2 = Arrow::Decimal128.new(11) + assert_equal([ + true, + false, + ], + [ + decimal == other_decimal1, + decimal == other_decimal2, + ]) + end + + def test_not_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(10) + other_decimal2 = Arrow::Decimal128.new(11) + assert_equal([ + false, + true, + ], + [ + decimal != other_decimal1, + decimal != other_decimal2, + ]) + end + + def test_less_than + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(11) + other_decimal2 = Arrow::Decimal128.new(9) + assert_equal([ + true, + false, + false + ], + [ + decimal < other_decimal1, + decimal < other_decimal2, + decimal < decimal, + ]) + end + + def test_less_than_or_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(11) + other_decimal2 = Arrow::Decimal128.new(9) + assert_equal([ + true, + false, + true + ], + [ + decimal <= other_decimal1, + decimal <= other_decimal2, + decimal <= decimal + ]) + end + + def test_greater_than + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(11) + other_decimal2 = Arrow::Decimal128.new(9) + assert_equal([ + false, + true, + false + ], + [ + decimal > other_decimal1, + decimal > other_decimal2, + decimal > decimal + ]) + end + + def test_greater_than_or_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal128.new(10) + other_decimal1 = Arrow::Decimal128.new(11) + other_decimal2 = Arrow::Decimal128.new(9) + assert_equal([ + false, + true, + true + ], + [ + decimal >= other_decimal1, + decimal >= other_decimal2, + decimal >= decimal + ]) + end + + def test_rescale + decimal = Arrow::Decimal128.new(10) + assert_equal(Arrow::Decimal128.new(1000), + decimal.rescale(1, 3)) + end + + def test_rescale_fail + decimal = Arrow::Decimal128.new(10) + message = + "[decimal128][rescale]: Invalid: " + + "Rescaling Decimal128 value would cause data loss" + assert_raise(Arrow::Error::Invalid.new(message)) do + decimal.rescale(1, -1) + end + end +end diff --git a/src/arrow/c_glib/test/test-decimal256-array.rb b/src/arrow/c_glib/test/test-decimal256-array.rb new file mode 100644 index 000000000..766f1a71a --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal256-array.rb @@ -0,0 +1,37 @@ +# 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 TestDecimal256Array < Test::Unit::TestCase + def test_format_value + data_type = Arrow::Decimal256DataType.new(8, 2) + builder = Arrow::Decimal256ArrayBuilder.new(data_type) + decimal = Arrow::Decimal256.new("23423445") + builder.append_value(decimal) + array = builder.finish + assert_equal("234234.45", array.format_value(0)) + end + + def test_value + data_type = Arrow::Decimal256DataType.new(8, 2) + builder = Arrow::Decimal256ArrayBuilder.new(data_type) + decimal = Arrow::Decimal256.new("23423445") + builder.append_value(decimal) + array = builder.finish + assert_equal("234234.45", + array.get_value(0).to_string_scale(array.value_data_type.scale)) + end +end diff --git a/src/arrow/c_glib/test/test-decimal256-data-type.rb b/src/arrow/c_glib/test/test-decimal256-data-type.rb new file mode 100644 index 000000000..596c3dab9 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal256-data-type.rb @@ -0,0 +1,43 @@ +# 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 TestDecimal256DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Decimal256DataType.new(2, 0) + assert_equal(Arrow::Type::DECIMAL256, data_type.id) + end + + def test_name + data_type = Arrow::Decimal256DataType.new(2, 0) + assert_equal("decimal256", data_type.name) + end + + def test_to_s + data_type = Arrow::Decimal256DataType.new(2, 0) + assert_equal("decimal256(2, 0)", data_type.to_s) + end + + def test_precision + data_type = Arrow::Decimal256DataType.new(8, 2) + assert_equal(8, data_type.precision) + end + + def test_scale + data_type = Arrow::Decimal256DataType.new(8, 2) + assert_equal(2, data_type.scale) + end +end diff --git a/src/arrow/c_glib/test/test-decimal256-scalar.rb b/src/arrow/c_glib/test/test-decimal256-scalar.rb new file mode 100644 index 000000000..2c419940d --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal256-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestDecimal256Scalar < Test::Unit::TestCase + def setup + @data_type = Arrow::Decimal256DataType.new(8, 2) + @value = Arrow::Decimal256.new("23423445") + @scalar = Arrow::Decimal256Scalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Decimal256Scalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("234234.45", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-decimal256.rb b/src/arrow/c_glib/test/test-decimal256.rb new file mode 100644 index 000000000..d422aef33 --- /dev/null +++ b/src/arrow/c_glib/test/test-decimal256.rb @@ -0,0 +1,220 @@ +# 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 TestDecimal256 < Test::Unit::TestCase + include Helper::Omittable + + def test_copy + decimal = Arrow::Decimal256.new("234.23445") + assert_equal(decimal, decimal.copy) + end + + def test_to_string_scale + integer_data = 23423445 + string_data = "234.23445" + decimal = Arrow::Decimal256.new(integer_data) + assert_equal(string_data, decimal.to_string_scale(5)) + end + + def test_to_string + string_data = "99999999999999999999999999999999999999" + decimal = Arrow::Decimal256.new(string_data) + assert_equal(string_data, decimal.to_s) + end + + def test_to_bytes + decimal = Arrow::Decimal256.new("12.3") + assert_equal([123, 0, 0, 0].pack("q*"), + decimal.to_bytes.to_s) + end + + def test_abs + absolute_value = "23049223942343532412" + negative_value = "-23049223942343532412" + decimal = Arrow::Decimal256.new(negative_value) + decimal.abs + assert_equal(absolute_value, decimal.to_s) + end + + def test_negate + positive_value = "23049223942343532412" + negative_value = "-23049223942343532412" + decimal = Arrow::Decimal256.new(positive_value) + decimal.negate + assert_equal(negative_value, decimal.to_s) + decimal.negate + assert_equal(positive_value, decimal.to_s) + end + + def test_plus + integer_data1 = 23423445 + integer_data2 = 5443 + decimal1 = Arrow::Decimal256.new(integer_data1) + decimal2 = Arrow::Decimal256.new(integer_data2) + decimal3 = decimal1.plus(decimal2) + assert_equal((integer_data1 + integer_data2).to_s, + decimal3.to_s) + end + + def test_multiply + integer_data1 = 23423445 + integer_data2 = 5443 + decimal1 = Arrow::Decimal256.new(integer_data1) + decimal2 = Arrow::Decimal256.new(integer_data2) + decimal3 = decimal1.multiply(decimal2) + assert_equal((integer_data1 * integer_data2).to_s, + decimal3.to_s) + end + + def test_divide + require_gi_bindings(3, 3, 0) + integer_data1 = 23423445 + integer_data2 = -5443 + decimal1 = Arrow::Decimal256.new(integer_data1) + decimal2 = Arrow::Decimal256.new(integer_data2) + result, remainder = decimal1.divide(decimal2) + assert_equal([ + integer_data1.quo(integer_data2).truncate.to_s, + integer_data1.remainder(integer_data2).to_s, + ], + [result.to_s, remainder.to_s]) + end + + def test_divide_zero + require_gi_bindings(3, 3, 0) + decimal1 = Arrow::Decimal256.new(23423445) + decimal2 = Arrow::Decimal256.new(0) + message = + "[decimal256][divide]: Invalid: Division by 0 in Decimal256" + assert_raise(Arrow::Error::Invalid.new(message)) do + decimal1.divide(decimal2) + end + end + + def test_equal + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(10) + other_decimal2 = Arrow::Decimal256.new(11) + assert_equal([ + true, + false, + ], + [ + decimal == other_decimal1, + decimal == other_decimal2, + ]) + end + + def test_not_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(10) + other_decimal2 = Arrow::Decimal256.new(11) + assert_equal([ + false, + true, + ], + [ + decimal != other_decimal1, + decimal != other_decimal2, + ]) + end + + def test_less_than + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(11) + other_decimal2 = Arrow::Decimal256.new(9) + assert_equal([ + true, + false, + false + ], + [ + decimal < other_decimal1, + decimal < other_decimal2, + decimal < decimal, + ]) + end + + def test_less_than_or_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(11) + other_decimal2 = Arrow::Decimal256.new(9) + assert_equal([ + true, + false, + true + ], + [ + decimal <= other_decimal1, + decimal <= other_decimal2, + decimal <= decimal + ]) + end + + def test_greater_than + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(11) + other_decimal2 = Arrow::Decimal256.new(9) + assert_equal([ + false, + true, + false + ], + [ + decimal > other_decimal1, + decimal > other_decimal2, + decimal > decimal + ]) + end + + def test_greater_than_or_equal + require_gi_bindings(3, 3, 1) + decimal = Arrow::Decimal256.new(10) + other_decimal1 = Arrow::Decimal256.new(11) + other_decimal2 = Arrow::Decimal256.new(9) + assert_equal([ + false, + true, + true + ], + [ + decimal >= other_decimal1, + decimal >= other_decimal2, + decimal >= decimal + ]) + end + + def test_rescale + decimal = Arrow::Decimal256.new(10) + assert_equal(Arrow::Decimal256.new(1000), + decimal.rescale(1, 3)) + end + + def test_rescale_fail + decimal = Arrow::Decimal256.new(10) + message = + "[decimal256][rescale]: Invalid: " + + "Rescaling Decimal256 value would cause data loss" + assert_raise(Arrow::Error::Invalid.new(message)) do + decimal.rescale(1, -1) + end + end +end diff --git a/src/arrow/c_glib/test/test-dense-union-array.rb b/src/arrow/c_glib/test/test-dense-union-array.rb new file mode 100644 index 000000000..ecd17d5a0 --- /dev/null +++ b/src/arrow/c_glib/test/test-dense-union-array.rb @@ -0,0 +1,88 @@ +# 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 TestDenseUnionArray < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case(".new") do + sub_test_case("default") do + def setup + type_ids = build_int8_array([0, 1, 0, 1, 1]) + value_offsets = build_int32_array([0, 0, 1, 1, 2]) + fields = [ + build_int16_array([1, nil]), + build_string_array(["a", "b", "c"]), + ] + @array = Arrow::DenseUnionArray.new(type_ids, value_offsets, fields) + end + + def test_value_data_type + fields = [ + Arrow::Field.new("0", Arrow::Int16DataType.new), + Arrow::Field.new("1", Arrow::StringDataType.new), + ] + assert_equal(Arrow::DenseUnionDataType.new(fields, [0, 1]), + @array.value_data_type) + end + + def test_field + assert_equal([ + build_int16_array([1, nil]), + build_string_array(["a", "b", "c"]), + ], + [ + @array.get_field(0), + @array.get_field(1), + ]) + end + end + + sub_test_case("DataType") do + def setup + data_type_fields = [ + Arrow::Field.new("number", Arrow::Int16DataType.new), + Arrow::Field.new("text", Arrow::StringDataType.new), + ] + type_codes = [11, 13] + @data_type = Arrow::DenseUnionDataType.new(data_type_fields, type_codes) + type_ids = build_int8_array([11, 13, 11, 13, 13]) + value_offsets = build_int32_array([0, 0, 1, 1, 2]) + fields = [ + build_int16_array([1, nil]), + build_string_array(["a", "b", "c"]) + ] + @array = Arrow::DenseUnionArray.new(@data_type, type_ids, value_offsets, fields) + end + + def test_value_data_type + assert_equal(@data_type, + @array.value_data_type) + end + + def test_field + assert_equal([ + build_int16_array([1, nil]), + build_string_array(["a", "b", "c"]), + ], + [ + @array.get_field(0), + @array.get_field(1), + ]) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-dense-union-data-type.rb b/src/arrow/c_glib/test/test-dense-union-data-type.rb new file mode 100644 index 000000000..71b045e06 --- /dev/null +++ b/src/arrow/c_glib/test/test-dense-union-data-type.rb @@ -0,0 +1,64 @@ +# 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 TestDenseUnionDataType < Test::Unit::TestCase + def setup + @number_field_data_type = Arrow::Int32DataType.new + @text_field_data_type = Arrow::StringDataType.new + @field_data_types = [ + @number_field_data_type, + @text_field_data_type, + ] + @number_field = Arrow::Field.new("number", @number_field_data_type) + @text_field = Arrow::Field.new("text", @text_field_data_type) + @fields = [ + @number_field, + @text_field, + ] + @data_type = Arrow::DenseUnionDataType.new(@fields, [2, 9]) + end + + def test_type + assert_equal(Arrow::Type::DENSE_UNION, @data_type.id) + end + + def test_name + assert_equal("dense_union", @data_type.name) + end + + def test_to_s + assert_equal("dense_union<number: int32=2, text: string=9>", + @data_type.to_s) + end + + def test_fields + assert_equal(@fields.zip(@field_data_types), + @data_type.fields.collect {|field| [field, field.data_type]}) + end + + def test_get_field + field = @data_type.get_field(0) + assert_equal([ + @fields[0], + @field_data_types[0], + ], + [ + field, + field.data_type, + ]) + end +end diff --git a/src/arrow/c_glib/test/test-dense-union-scalar.rb b/src/arrow/c_glib/test/test-dense-union-scalar.rb new file mode 100644 index 000000000..4a3e5c0de --- /dev/null +++ b/src/arrow/c_glib/test/test-dense-union-scalar.rb @@ -0,0 +1,58 @@ +# 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 TestDenseUnionScalar < Test::Unit::TestCase + def setup + fields = [ + Arrow::Field.new("number", Arrow::Int8DataType.new), + Arrow::Field.new("text", Arrow::StringDataType.new), + ] + @data_type = Arrow::DenseUnionDataType.new(fields, [2, 9]) + @type_code = 2 + @value = Arrow::Int8Scalar.new(-29) + @scalar = Arrow::DenseUnionScalar.new(@data_type, @type_code, @value) + end + + def test_type_code + assert_equal(@type_code, + @scalar.type_code) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::DenseUnionScalar.new(@data_type, @type_code, @value), + @scalar) + end + + def test_to_s + assert_equal("union{number: int8 = -29}", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-dictionary-array-builder.rb b/src/arrow/c_glib/test/test-dictionary-array-builder.rb new file mode 100644 index 000000000..4531e44f4 --- /dev/null +++ b/src/arrow/c_glib/test/test-dictionary-array-builder.rb @@ -0,0 +1,395 @@ +# 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 TestDictinaryArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + + def setup + @values = [ + *%w(foo bar foo), + nil, + *%w(foo baz bar baz baz) + ] + end + + sub_test_case("BinaryDictionaryArrayBuilder") do + sub_test_case("constructed from empty") do + def setup + super + + @dictionary = %w(foo bar baz) + @dictionary_array = build_binary_array(@dictionary) + @indices = @values.map {|x| x ? @dictionary.index(x) : nil } + @indices_array = build_int8_array(@indices) + @data_type = Arrow::DictionaryDataType.new(@indices_array.value_data_type, + @dictionary_array.value_data_type, + false) + @expected_array = Arrow::DictionaryArray.new(@data_type, + @indices_array, + @dictionary_array) + @builder = Arrow::BinaryDictionaryArrayBuilder.new + @values.each do |value| + if value + @builder.append_value_bytes(value) + else + @builder.append_null + end + end + end + + test("append_value") do + dictionary_array = build_binary_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 3]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_value("qux") + assert_equal(expected_array, @builder.finish) + end + + test("append_value_bytes") do + dictionary_array = build_binary_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 3]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_value_bytes("qux") + assert_equal(expected_array, @builder.finish) + end + + test("append_array") do + dictionary_array = build_binary_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 3, 0, nil, 2]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_array(build_binary_array(["qux", "foo", nil, "baz"])) + assert_equal(expected_array, @builder.finish) + end + + test("append_indices") do + @builder.insert_memo_values(build_binary_array(["qux"])) + dictionary_array = build_binary_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 1, 2, nil, 3, 0, 1, 2, 1, 3, 0]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_indices([1, 2, 1, 3, 0], + [true, true, false, true, true]) + @builder.append_indices([1, 2, 1, 3, 0]) + assert_equal(expected_array, @builder.finish) + end + + test("append_nulls") do + dictionary_array = build_binary_array([]) + indices_array = build_int8_array([nil, nil, nil]) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + builder = Arrow::BinaryDictionaryArrayBuilder.new + builder.append_nulls(3) + assert_equal(expected_array, + builder.finish) + end + + test("append_empty_values") do + dictionary_array = build_binary_array(["hello"]) + indices_array = build_int8_array([0, 0, 0, 0]) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + builder = Arrow::BinaryDictionaryArrayBuilder.new + builder.append_value("hello") + builder.append_empty_value + builder.append_empty_values(2) + assert_equal(expected_array, + builder.finish) + end + + test("dictionary_length") do + assert_equal(@dictionary.length, @builder.dictionary_length) + end + + test("finish") do + assert_equal(@expected_array, + @builder.finish) + end + + test("finish_delta") do + assert_equal([ + true, + @indices_array, + @dictionary_array, + ], + @builder.finish_delta) + end + + test("reset") do + expected_array = Arrow::DictionaryArray.new(@data_type, + build_int8_array([]), + @dictionary_array) + @builder.reset + assert_equal({ + dictionary_length: @dictionary.length, + array: expected_array, + }, + { + dictionary_length: @builder.dictionary_length, + array: @builder.finish, + }) + end + + test("reset_full") do + expected_array = Arrow::DictionaryArray.new(@data_type, + build_int8_array([]), + build_binary_array([])) + @builder.reset_full + assert_equal({ + dictionary_length: 0, + array: expected_array, + }, + { + dictionary_length: @builder.dictionary_length, + array: @builder.finish, + }) + end + end + + sub_test_case("constructed with memo values") do + def setup + super + + @dictionary = %w(qux foo bar baz) + dictionary_array = build_binary_array(@dictionary) + indices = @values.map {|x| x ? @dictionary.index(x) : nil } + indices_array = build_int8_array(indices) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + @expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + + @builder = Arrow::BinaryDictionaryArrayBuilder.new + @builder.insert_memo_values(dictionary_array) + @values.each do |value| + if value + @builder.append_value_bytes(value) + else + @builder.append_null + end + end + end + + test("dictionary_length") do + assert_equal(@dictionary.length, @builder.dictionary_length) + end + + test("finish") do + assert_equal(@expected_array, @builder.finish) + end + end + end + + sub_test_case("StringDictionaryArrayBuilder") do + sub_test_case("constructed from empty") do + def setup + super + + @dictionary = %w(foo bar baz) + @dictionary_array = build_string_array(@dictionary) + @indices = @values.map {|x| x ? @dictionary.index(x) : nil } + @indices_array = build_int8_array(@indices) + @data_type = Arrow::DictionaryDataType.new(@indices_array.value_data_type, + @dictionary_array.value_data_type, + false) + @expected_array = Arrow::DictionaryArray.new(@data_type, + @indices_array, + @dictionary_array) + @builder = Arrow::StringDictionaryArrayBuilder.new + @values.each do |value| + if value + @builder.append_string(value) + else + @builder.append_null + end + end + end + + test("append_string") do + dictionary_array = build_string_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 3]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_string("qux") + assert_equal(expected_array, @builder.finish) + end + + test("append_array") do + dictionary_array = build_string_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 3, 0, nil, 2]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_array(build_string_array(["qux", "foo", nil, "baz"])) + assert_equal(expected_array, @builder.finish) + end + + test("append_indices") do + @builder.insert_memo_values(build_string_array(["qux"])) + dictionary_array = build_string_array([*@dictionary, "qux"]) + indices_array = build_int8_array([*@indices, 1, 2, nil, 3, 0, 1, 2, 1, 3, 0]) + expected_array = Arrow::DictionaryArray.new(@data_type, + indices_array, + dictionary_array) + + @builder.append_indices([1, 2, 1, 3, 0], + [true, true, false, true, true]) + @builder.append_indices([1, 2, 1, 3, 0]) + assert_equal(expected_array, @builder.finish) + end + + test("append_nulls") do + dictionary_array = build_string_array([]) + indices_array = build_int8_array([nil, nil, nil]) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + builder = Arrow::StringDictionaryArrayBuilder.new + builder.append_nulls(3) + assert_equal(expected_array, + builder.finish) + end + + test("append_empty_values") do + dictionary_array = build_string_array(["hello"]) + indices_array = build_int8_array([0, 0, 0, 0]) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + builder = Arrow::StringDictionaryArrayBuilder.new + builder.append_string("hello") + builder.append_empty_value + builder.append_empty_values(2) + assert_equal(expected_array, + builder.finish) + end + + test("dictionary_length") do + assert_equal(@dictionary.length, @builder.dictionary_length) + end + + test("finish") do + assert_equal(@expected_array, + @builder.finish) + end + + test("finish_delta") do + assert_equal([ + true, + @indices_array, + @dictionary_array, + ], + @builder.finish_delta) + end + + test("reset") do + expected_array = Arrow::DictionaryArray.new(@data_type, + build_int8_array([]), + @dictionary_array) + @builder.reset + assert_equal({ + dictionary_length: @dictionary.length, + array: expected_array, + }, + { + dictionary_length: @builder.dictionary_length, + array: @builder.finish, + }) + end + + test("reset_full") do + expected_array = Arrow::DictionaryArray.new(@data_type, + build_int8_array([]), + build_string_array([])) + @builder.reset_full + assert_equal({ + dictionary_length: 0, + array: expected_array, + }, + { + dictionary_length: @builder.dictionary_length, + array: @builder.finish, + }) + end + end + + sub_test_case("constructed with memo values") do + def setup + super + + @dictionary = %w(qux foo bar baz) + dictionary_array = build_string_array(@dictionary) + indices = @values.map {|x| x ? @dictionary.index(x) : nil } + indices_array = build_int8_array(indices) + data_type = Arrow::DictionaryDataType.new(indices_array.value_data_type, + dictionary_array.value_data_type, + false) + @expected_array = Arrow::DictionaryArray.new(data_type, + indices_array, + dictionary_array) + + @builder = Arrow::StringDictionaryArrayBuilder.new + @builder.insert_memo_values(dictionary_array) + @values.each do |value| + if value + @builder.append_string(value) + else + @builder.append_null + end + end + end + + test("dictionary_length") do + assert_equal(@dictionary.length, @builder.dictionary_length) + end + + test("finish") do + assert_equal(@expected_array, @builder.finish) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-dictionary-array.rb b/src/arrow/c_glib/test/test-dictionary-array.rb new file mode 100644 index 000000000..0f5157869 --- /dev/null +++ b/src/arrow/c_glib/test/test-dictionary-array.rb @@ -0,0 +1,78 @@ +# 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 TestDictionaryArray < Test::Unit::TestCase + include Helper::Buildable + + def setup + @index_data_type = Arrow::Int32DataType.new + @dictionary = build_string_array(["C", "C++", "Ruby"]) + @ordered = false + @data_type = Arrow::DictionaryDataType.new(@index_data_type, + @dictionary.value_data_type, + @ordered) + end + + sub_test_case(".new") do + def test_new + indices = build_int32_array([0, 2, 2, 1, 0]) + dictionary_array = Arrow::DictionaryArray.new(@data_type, + indices, + @dictionary) + assert_equal(<<-STRING.chomp, dictionary_array.to_s) + +-- dictionary: + [ + "C", + "C++", + "Ruby" + ] +-- indices: + [ + 0, + 2, + 2, + 1, + 0 + ] + STRING + end + end + + sub_test_case("instance methods") do + def setup + super + @indices = build_int32_array([0, 2, 2, 1, 0]) + @dictionary_array = Arrow::DictionaryArray.new(@data_type, + @indices, + @dictionary) + end + + def test_indices + assert_equal(@indices, @dictionary_array.indices) + end + + def test_dictionary + assert_equal(@dictionary, @dictionary_array.dictionary) + end + + def test_dictionary_data_type + assert_equal(@data_type, + @dictionary_array.dictionary_data_type) + end + end +end diff --git a/src/arrow/c_glib/test/test-dictionary-data-type.rb b/src/arrow/c_glib/test/test-dictionary-data-type.rb new file mode 100644 index 000000000..cdc42edcd --- /dev/null +++ b/src/arrow/c_glib/test/test-dictionary-data-type.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 TestDictionaryDataType < Test::Unit::TestCase + include Helper::Buildable + + def setup + @index_data_type = Arrow::Int32DataType.new + @value_data_type = Arrow::StringDataType.new + @ordered = true + @data_type = Arrow::DictionaryDataType.new(@index_data_type, + @value_data_type, + @ordered) + end + + def test_type + assert_equal(Arrow::Type::DICTIONARY, @data_type.id) + end + + def test_name + assert_equal("dictionary", @data_type.name) + end + + def test_to_s + assert_equal("dictionary<values=string, indices=int32, ordered=1>", + @data_type.to_s) + end + + def test_bit_width + assert_equal(32, @data_type.bit_width) + end + + def test_index_data_type + assert_equal(@index_data_type, @data_type.index_data_type) + end + + def test_value_data_type + assert_equal(@value_data_type, @data_type.value_data_type) + end + + def test_ordered? + assert do + @data_type.ordered? + end + end +end diff --git a/src/arrow/c_glib/test/test-dictionary-encode.rb b/src/arrow/c_glib/test/test-dictionary-encode.rb new file mode 100644 index 000000000..100494c9e --- /dev/null +++ b/src/arrow/c_glib/test/test-dictionary-encode.rb @@ -0,0 +1,62 @@ +# 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 TestDictionaryEncode < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_int32 + array = build_int32_array([1, 3, 1, -1, -3, -1]) + assert_equal(<<-STRING.chomp, array.dictionary_encode.to_s) + +-- dictionary: + [ + 1, + 3, + -1, + -3 + ] +-- indices: + [ + 0, + 1, + 0, + 2, + 3, + 2 + ] + STRING + end + + def test_string + array = build_string_array(["Ruby", "Python", "Ruby"]) + assert_equal(<<-STRING.chomp, array.dictionary_encode.to_s) + +-- dictionary: + [ + "Ruby", + "Python" + ] +-- indices: + [ + 0, + 1, + 0 + ] + STRING + end +end diff --git a/src/arrow/c_glib/test/test-double-array.rb b/src/arrow/c_glib/test/test-double-array.rb new file mode 100644 index 000000000..4f30b59a0 --- /dev/null +++ b/src/arrow/c_glib/test/test-double-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 TestDoubleArray < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_double_array([-1.1, 2.2, nil]), + Arrow::DoubleArray.new(3, + Arrow::Buffer.new([-1.1, 2.2].pack("d*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::DoubleArrayBuilder.new + builder.append_value(-1.1) + builder.append_value(2.2) + builder.append_value(-4.4) + array = builder.finish + assert_equal([-1.1, 2.2, -4.4].pack("d*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::DoubleArrayBuilder.new + builder.append_value(1.5) + array = builder.finish + assert_in_delta(1.5, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::DoubleArrayBuilder.new + builder.append_value(1.5) + builder.append_value(3) + builder.append_value(4.5) + array = builder.finish + assert_equal([1.5, 3.0, 4.5], array.values) + end + + def test_sum + array = build_float_array([1.5, 3.0, nil]) + assert_in_delta(4.5, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-double-data-type.rb b/src/arrow/c_glib/test/test-double-data-type.rb new file mode 100644 index 000000000..5a56ebeac --- /dev/null +++ b/src/arrow/c_glib/test/test-double-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestDoubleDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::DoubleDataType.new + assert_equal(Arrow::Type::DOUBLE, data_type.id) + end + + def test_name + data_type = Arrow::DoubleDataType.new + assert_equal("double", data_type.name) + end + + def test_to_s + data_type = Arrow::DoubleDataType.new + assert_equal("double", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-double-scalar.rb b/src/arrow/c_glib/test/test-double-scalar.rb new file mode 100644 index 000000000..eea673b41 --- /dev/null +++ b/src/arrow/c_glib/test/test-double-scalar.rb @@ -0,0 +1,49 @@ +# 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 TestDoubleScalar < Test::Unit::TestCase + def setup + @scalar = Arrow::DoubleScalar.new(1.1) + end + + def test_data_type + assert_equal(Arrow::DoubleDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + options = Arrow::EqualOptions.new + options.approx = true + assert do + @scalar.equal_options(Arrow::DoubleScalar.new(1.1), options) + end + end + + def test_to_s + assert_equal("1.1", @scalar.to_s) + end + + def test_value + assert_in_delta(1.1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-equal-options.rb b/src/arrow/c_glib/test/test-equal-options.rb new file mode 100644 index 000000000..4ea1979a7 --- /dev/null +++ b/src/arrow/c_glib/test/test-equal-options.rb @@ -0,0 +1,96 @@ +# 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 TestEqualOptions < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case("approx") do + def setup + @options = Arrow::EqualOptions.new + end + + def test_accessor + assert do + not @options.approx? + end + @options.approx = true + assert do + @options.approx? + end + end + + def test_compare + array1 = build_float_array([0.01]) + array2 = build_float_array([0.010001]) + @options.approx = true + assert do + array1.equal_options(array2, @options) + end + end + end + + sub_test_case("nans-equal") do + def setup + @options = Arrow::EqualOptions.new + end + + def test_accessor + assert do + not @options.nans_equal? + end + @options.nans_equal = true + assert do + @options.nans_equal? + end + end + + def test_compare + array1 = build_float_array([0.1, Float::NAN, 0.2]) + array2 = build_float_array([0.1, Float::NAN, 0.2]) + @options.nans_equal = true + assert do + array1.equal_options(array2, @options) + end + end + end + + sub_test_case("absolute-tolerance") do + def setup + @options = Arrow::EqualOptions.new + end + + def test_accessor + assert do + @options.absolute_tolerance < 0.001 + end + @options.absolute_tolerance = 0.001 + assert do + @options.absolute_tolerance >= 0.001 + end + end + + def test_compare + array1 = build_float_array([0.01]) + array2 = build_float_array([0.0109]) + @options.approx = true + @options.absolute_tolerance = 0.001 + assert do + array1.equal_options(array2, @options) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-execute-plan.rb b/src/arrow/c_glib/test/test-execute-plan.rb new file mode 100644 index 000000000..d698e1e31 --- /dev/null +++ b/src/arrow/c_glib/test/test-execute-plan.rb @@ -0,0 +1,53 @@ +# 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 TestExecutePlan < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def setup + @record_batch = + build_record_batch(number: build_int8_array([1, 2, 3, 4, 5]), + string: build_string_array(["a", "b", "a", "b", "a"])) + @plan = Arrow::ExecutePlan.new + @source_node_options = Arrow::SourceNodeOptions.new(@record_batch) + @source_node = @plan.build_source_node(@source_node_options) + aggregations = [ + Arrow::Aggregation.new("hash_sum", nil, "number", "sum(number)"), + Arrow::Aggregation.new("hash_count", nil, "number", "count(number)"), + ] + @aggregate_node_options = + Arrow::AggregateNodeOptions.new(aggregations, ["string"]) + @aggregate_node = @plan.build_aggregate_node(@source_node, + @aggregate_node_options) + @sink_node_options = Arrow::SinkNodeOptions.new + @sink_node = @plan.build_sink_node(@aggregate_node, + @sink_node_options) + end + + def test_start + @plan.validate + @plan.start + @plan.wait + reader = @sink_node_options.get_reader(@aggregate_node.output_schema) + assert_equal(build_table("sum(number)" => build_int64_array([9, 6]), + "count(number)" => build_int64_array([3, 2]), + "string" => build_string_array(["a", "b"])), + reader.read_all) + @plan.stop + end +end diff --git a/src/arrow/c_glib/test/test-extension-data-type.rb b/src/arrow/c_glib/test/test-extension-data-type.rb new file mode 100644 index 000000000..59c6395e9 --- /dev/null +++ b/src/arrow/c_glib/test/test-extension-data-type.rb @@ -0,0 +1,105 @@ +# 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 TestExtensionDataType < Test::Unit::TestCase + class UUIDArray < Arrow::ExtensionArray + type_register + end + + class UUIDDataType < Arrow::ExtensionDataType + type_register + + def initialize + super(storage_data_type: Arrow::FixedSizeBinaryDataType.new(16)) + end + + # TODO + # def get_extension_name_impl + # "uuid" + # end + + # TODO + # def get_array_gtype_impl + # UUIDArray.gtype + # end + end + + include Helper::Buildable + + def test_type + data_type = UUIDDataType.new + assert_equal(Arrow::Type::EXTENSION, data_type.id) + end + + def test_name + data_type = UUIDDataType.new + assert_equal("extension", data_type.name) + end + + def test_to_s + omit("gobject-introspection gem doesn't support implementing methods for GLib object yet") + data_type = UUIDDataType.new + assert_equal("extension<uuid>", data_type.to_s) + end + + def test_storage_data_type + data_type = UUIDDataType.new + assert_equal(Arrow::FixedSizeBinaryDataType.new(16), + data_type.storage_data_type) + end + + def test_extension_name + omit("gobject-introspection gem doesn't support implementing methods for GLib object yet") + data_type = UUIDDataType.new + assert_equal("uuid", data_type.extension_name) + end + + def test_wrap_array + omit("gobject-introspection gem doesn't support implementing methods for GLib object yet") + data_type = UUIDDataType.new + storage = build_fixed_size_binary_array(data_type.storage_data_type, + ["a" * 16, nil, "c" * 16]) + extension_array = data_type.wrap_array(storage) + assert_equal([ + UUIDArray, + storage, + ], + [ + extension_array.class, + extension_array.storage, + ]) + end + + def test_wrap_chunked_array + omit("gobject-introspection gem doesn't support implementing methods for GLib object yet") + data_type = UUIDDataType.new + storage1 = build_fixed_size_binary_array(data_type.storage_data_type, + ["a" * 16, nil]) + storage2 = build_fixed_size_binary_array(data_type.storage_data_type, + ["c" * 16]) + chunkd_array = Arrow::ChunkedArray.new([storage1, storage2]) + extension_chunked_array = data_type.wrap_chunked_array(chunked_array) + assert_equal([ + data_type, + [UUIDArray] * chunked_array.size, + ], + [ + extension_chunked_array.get_value_data_type, + extension_chunked_array.chunks.collect(&:class), + ]) + end +end diff --git a/src/arrow/c_glib/test/test-feather-file-reader.rb b/src/arrow/c_glib/test/test-feather-file-reader.rb new file mode 100644 index 000000000..a9380bb1a --- /dev/null +++ b/src/arrow/c_glib/test/test-feather-file-reader.rb @@ -0,0 +1,71 @@ +# 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 TestFeatherFileReader < Test::Unit::TestCase + include Helper::Buildable + + def setup_file(table) + tempfile = Tempfile.open("arrow-feather-file-reader") + output = Arrow::FileOutputStream.new(tempfile.path, false) + begin + table.write_as_feather(output) + ensure + output.close + end + + input = Arrow::MemoryMappedInputStream.new(tempfile.path) + begin + reader = Arrow::FeatherFileReader.new(input) + yield(reader) + ensure + input.close + end + end + + test("#read") do + table = build_table("message" => build_string_array(["Login"]), + "is_critical" => build_boolean_array([true])) + setup_file(table) do |reader| + assert do + reader.version >= 2 + end + assert_equal(table, reader.read) + end + end + + test("#read_indices") do + table = build_table("message" => build_string_array(["Login"]), + "is_critical" => build_boolean_array([true]), + "host" => build_string_array(["www"])) + setup_file(table) do |reader| + assert_equal(build_table("message" => build_string_array(["Login"]), + "host" => build_string_array(["www"])), + reader.read_indices([2, 0])) + end + end + + test("#read_names") do + table = build_table("message" => build_string_array(["Login"]), + "is_critical" => build_boolean_array([true]), + "host" => build_string_array(["www"])) + setup_file(table) do |reader| + assert_equal(build_table("message" => build_string_array(["Login"]), + "host" => build_string_array(["www"])), + reader.read_names(["message", "host"])) + end + end +end diff --git a/src/arrow/c_glib/test/test-field-expression.rb b/src/arrow/c_glib/test/test-field-expression.rb new file mode 100644 index 000000000..cdcfab71e --- /dev/null +++ b/src/arrow/c_glib/test/test-field-expression.rb @@ -0,0 +1,49 @@ +# 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 TestFieldExpression < Test::Unit::TestCase + def setup + @expression = Arrow::FieldExpression.new("visible") + end + + sub_test_case("#initialize") do + def test_invalid_dot_path + message = + "[field-expression][new]: Invalid: " + + "Dot path '.[' contained an unterminated index" + assert_raise(Arrow::Error::Invalid.new(message)) do + Arrow::FieldExpression.new(".[") + end + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::FieldExpression.new("visible"), + Arrow::FieldExpression.new(".visible")) + end + + def test_false + assert_not_equal(@expression, + Arrow::FieldExpression.new("equal")) + end + end + + def test_to_string + assert_equal("visible", @expression.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-field.rb b/src/arrow/c_glib/test/test-field.rb new file mode 100644 index 000000000..fa341de26 --- /dev/null +++ b/src/arrow/c_glib/test/test-field.rb @@ -0,0 +1,125 @@ +# 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 TestField < Test::Unit::TestCase + include Helper::Omittable + + def setup + @data_type = Arrow::BooleanDataType.new + @field = Arrow::Field.new("enabled", @data_type) + @field_with_metadata = @field.with_metadata("key1" => "value1", + "key2" => "value2") + end + + def test_export + require_gi_bindings(3, 4, 8) + c_abi_schema = @field.export + assert_equal(@field, + Arrow::Field.import(c_abi_schema)) + end + + def test_equal + assert_equal(Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("enabled", Arrow::BooleanDataType.new)) + end + + def test_name + assert_equal("enabled", @field.name) + end + + def test_data_type + assert_equal(@data_type.to_s, + @field.data_type.to_s) + end + + def test_nullable? + assert do + @field.nullable? + end + end + + def test_to_s + assert_equal("enabled: bool", @field_with_metadata.to_s) + end + + sub_test_case("#to_string_metadata") do + def test_true + assert_equal(<<-FIELD.chomp, @field_with_metadata.to_string_metadata(true)) +enabled: bool +-- metadata -- +key1: value1 +key2: value2 + FIELD + end + + def test_false + assert_equal(<<-FIELD.chomp, @field_with_metadata.to_string_metadata(false)) +enabled: bool + FIELD + end + end + + sub_test_case("#has_metadata?") do + def test_existent + assert do + @field_with_metadata.has_metadata? + end + end + + def test_nonexistent + assert do + not @field.has_metadata? + end + end + end + + sub_test_case("#metadata") do + def test_existent + assert_equal({ + "key1" => "value1", + "key2" => "value2", + }, + @field_with_metadata.metadata) + end + + def test_nonexistent + assert_nil(@field.metadata) + end + end + + def test_with_metadata + field = @field_with_metadata.with_metadata("key3" => "value3") + assert_equal({"key3" => "value3"}, + field.metadata) + end + + def test_with_merged_metadata + field = @field_with_metadata.with_merged_metadata("key1" => "new-value1", + "key3" => "value3") + assert_equal({ + "key1" => "new-value1", + "key2" => "value2", + "key3" => "value3", + }, + field.metadata) + end + + def test_remove_metadata + field = @field_with_metadata.remove_metadata + assert_nil(field.metadata) + end +end diff --git a/src/arrow/c_glib/test/test-file-info.rb b/src/arrow/c_glib/test/test-file-info.rb new file mode 100644 index 000000000..e6a3a0d62 --- /dev/null +++ b/src/arrow/c_glib/test/test-file-info.rb @@ -0,0 +1,170 @@ +# 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 TestFileInfo < Test::Unit::TestCase + def setup + @file_info = Arrow::FileInfo.new + end + + sub_test_case("#type") do + test("default") do + assert_equal(Arrow::FileType::UNKNOWN, + @file_info.type) + end + end + + test("#type=") do + @file_info.type = :dir + assert_equal(Arrow::FileType::DIR, + @file_info.type) + end + + sub_test_case("#path") do + test("default") do + assert_equal("", @file_info.path) + end + end + + test("#path=") do + @file_info.path = "/a/b/c.d" + assert_equal("/a/b/c.d", + @file_info.path) + end + + sub_test_case("#base_name") do + test("default") do + assert_equal("", @file_info.base_name) + end + + test("with directory") do + @file_info.path = "/a/b/c.d" + assert_equal("c.d", @file_info.base_name) + end + end + + sub_test_case("#dir_name") do + test("default") do + assert_equal("", @file_info.dir_name) + end + + test("with directory") do + @file_info.path = "/a/b/c.d" + assert_equal("/a/b", @file_info.dir_name) + end + end + + sub_test_case("#extension") do + test("default") do + assert_equal("", @file_info.extension) + end + + test("exist") do + @file_info.path = "/a/b/c.d" + assert_equal("d", @file_info.extension) + end + end + + sub_test_case("#size") do + test("default") do + assert_equal(-1, @file_info.size) + end + end + + sub_test_case("#mtime") do + test("default") do + assert_equal(-1, @file_info.mtime) + end + end + + sub_test_case("#==") do + def setup + super + @other_file_info = Arrow::FileInfo.new + end + + test("all the properties are the same") do + assert do + @file_info == @other_file_info + end + end + + test("the different type") do + @other_file_info.type = Arrow::FileType::FILE + assert do + @file_info != @other_file_info + end + end + + test("the different path") do + @other_file_info.path = "/a/b/c" + assert do + @file_info != @other_file_info + end + end + + test("the different size") do + @other_file_info.size = 42 + assert do + @file_info != @other_file_info + end + end + + test("the different mtime") do + @other_file_info.mtime = Time.now.to_i + assert do + @file_info != @other_file_info + end + end + end + + sub_test_case("#file?") do + test("true") do + @file_info.type = :file + assert do + @file_info.file? + end + end + + test("false") do + @file_info.type = :dir + assert do + not @file_info.file? + end + end + end + + sub_test_case("#dir?") do + test("true") do + @file_info.type = :dir + assert do + @file_info.dir? + end + end + + test("false") do + @file_info.type = :file + assert do + not @file_info.dir? + end + end + end + + test("#to_s") do + assert_equal("FileInfo(FileType::Unknown, )", + @file_info.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-file-input-stream.rb b/src/arrow/c_glib/test/test-file-input-stream.rb new file mode 100644 index 000000000..2b43f97f5 --- /dev/null +++ b/src/arrow/c_glib/test/test-file-input-stream.rb @@ -0,0 +1,102 @@ +# 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 TestFileInputStream < Test::Unit::TestCase + def setup + @data = "Hello World" + @tempfile = Tempfile.open("arrow-file-input-stream") + @tempfile.write(@data) + @tempfile.close + end + + def test_new + input = Arrow::FileInputStream.new(@tempfile.path) + begin + buffer = input.read(5) + assert_equal("Hello", buffer.data.to_s) + ensure + input.close + end + end + + def test_close + input = Arrow::FileInputStream.new(@tempfile.path) + assert do + not input.closed? + end + input.close + assert do + input.closed? + end + end + + def test_size + input = Arrow::FileInputStream.new(@tempfile.path) + begin + assert_equal(@data.bytesize, input.size) + ensure + input.close + end + end + + def test_read + input = Arrow::FileInputStream.new(@tempfile.path) + begin + buffer = input.read(5) + assert_equal("Hello", buffer.data.to_s) + ensure + input.close + end + end + + def test_read_at + input = Arrow::FileInputStream.new(@tempfile.path) + begin + buffer = input.read_at(6, 5) + assert_equal("World", buffer.data.to_s) + ensure + input.close + end + end + + def test_mode + input = Arrow::FileInputStream.new(@tempfile.path) + begin + assert_equal(Arrow::FileMode::READ, input.mode) + ensure + input.close + end + end + + def test_file_descriptor + @tempfile.open + begin + fd = @tempfile.fileno + input = Arrow::FileInputStream.new(fd) + begin + assert_equal(fd, input.file_descriptor) + ensure + input.close + end + ensure + begin + @tempfile.close + rescue + end + end + end +end diff --git a/src/arrow/c_glib/test/test-file-output-stream.rb b/src/arrow/c_glib/test/test-file-output-stream.rb new file mode 100644 index 000000000..237781ac0 --- /dev/null +++ b/src/arrow/c_glib/test/test-file-output-stream.rb @@ -0,0 +1,38 @@ +# 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 TestFileOutputStream < Test::Unit::TestCase + sub_test_case(".new") do + def test_create + tempfile = Tempfile.open("arrow-io-file-output-stream") + tempfile.write("Hello") + tempfile.close + file = Arrow::FileOutputStream.new(tempfile.path, false) + file.close + assert_equal("", File.read(tempfile.path)) + end + + def test_append + tempfile = Tempfile.open("arrow-io-file-output-stream") + tempfile.write("Hello") + tempfile.close + file = Arrow::FileOutputStream.new(tempfile.path, true) + file.close + assert_equal("Hello", File.read(tempfile.path)) + end + end +end diff --git a/src/arrow/c_glib/test/test-file-selector.rb b/src/arrow/c_glib/test/test-file-selector.rb new file mode 100644 index 000000000..23a4ea93b --- /dev/null +++ b/src/arrow/c_glib/test/test-file-selector.rb @@ -0,0 +1,82 @@ +# 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 TestFileSelector < Test::Unit::TestCase + def setup + @file_selector = Arrow::FileSelector.new + end + + sub_test_case("#base_dir") do + test("default") do + assert do + "" == @file_selector.base_dir + end + end + end + + test("#base_dir=") do + @file_selector.base_dir = "/a/b" + assert do + "/a/b" == @file_selector.base_dir + end + end + + sub_test_case("#allow_not_found?") do + test("default") do + assert do + not @file_selector.allow_not_found? + end + end + end + + test("#allow_not_found=") do + @file_selector.allow_not_found = true + assert do + @file_selector.allow_not_found? + end + end + + sub_test_case("#recursive?") do + test("default") do + assert do + false == @file_selector.recursive? + end + end + end + + test("#recursive=") do + @file_selector.recursive = true + assert do + true == @file_selector.recursive? + end + end + + sub_test_case("#max_recursion") do + test("default") do + assert do + (1<<31) - 1 == @file_selector.max_recursion + end + end + end + + test("#max_recursion=") do + @file_selector.max_recursion = 42 + assert do + 42 == @file_selector.max_recursion + end + end +end diff --git a/src/arrow/c_glib/test/test-file-writer.rb b/src/arrow/c_glib/test/test-file-writer.rb new file mode 100644 index 000000000..5f9c3c4e1 --- /dev/null +++ b/src/arrow/c_glib/test/test-file-writer.rb @@ -0,0 +1,85 @@ +# 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 TestFileWriter < Test::Unit::TestCase + include Helper::Buildable + + def test_write_record_batch + data = [true] + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + + tempfile = Tempfile.open("arrow-ipc-file-writer") + output = Arrow::FileOutputStream.new(tempfile.path, false) + begin + file_writer = Arrow::RecordBatchFileWriter.new(output, schema) + begin + record_batch = Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]) + file_writer.write_record_batch(record_batch) + ensure + file_writer.close + end + ensure + output.close + end + + input = Arrow::MemoryMappedInputStream.new(tempfile.path) + begin + file_reader = Arrow::RecordBatchFileReader.new(input) + assert_equal([field.name], + file_reader.schema.fields.collect(&:name)) + assert_equal(Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]), + file_reader.read_record_batch(0)) + ensure + input.close + end + end + + def test_write_table + tempfile = Tempfile.open("arrow-ipc-file-writer") + output = Arrow::FileOutputStream.new(tempfile.path, false) + + array = build_boolean_array([true, false, true]) + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + + begin + file_writer = Arrow::RecordBatchFileWriter.new(output, schema) + begin + table = Arrow::Table.new(schema, [array]) + file_writer.write_table(table) + ensure + file_writer.close + end + ensure + output.close + end + + input = Arrow::MemoryMappedInputStream.new(tempfile.path) + begin + file_reader = Arrow::RecordBatchFileReader.new(input) + assert_equal(Arrow::RecordBatch.new(schema, array.length, [array]), + file_reader.read_record_batch(0)) + ensure + input.close + end + end +end diff --git a/src/arrow/c_glib/test/test-filter.rb b/src/arrow/c_glib/test/test-filter.rb new file mode 100644 index 000000000..5ed035917 --- /dev/null +++ b/src/arrow/c_glib/test/test-filter.rb @@ -0,0 +1,247 @@ +# 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 TestFilter < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case("FilterOptions") do + def test_default_null_selection_behavior + assert_equal(Arrow::FilterNullSelectionBehavior::DROP, + Arrow::FilterOptions.new.null_selection_behavior) + end + end + + sub_test_case("Array") do + def setup + @filter = build_boolean_array([false, true, true, nil]) + end + + def test_filter + assert_equal(build_int16_array([1, 0]), + build_int16_array([0, 1, 0, 2]).filter(@filter)) + end + + def test_filter_emit_null + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(build_int16_array([1, 0, nil]), + build_int16_array([0, 1, 0, 2]).filter(@filter, options)) + end + + def test_invalid_array_length + filter = build_boolean_array([false, true, true, false]) + assert_raise(Arrow::Error::Invalid) do + build_int16_array([0, 1, 0]).filter(filter) + end + end + end + + sub_test_case("Table") do + def setup + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(fields) + arrays = [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true, true]), + ] + @table = Arrow::Table.new(@schema, arrays) + end + + def test_filter + filter = build_boolean_array([false, true, nil]) + arrays = [ + build_boolean_array([false]), + build_boolean_array([true]), + ] + filtered_table = Arrow::Table.new(@schema, arrays) + assert_equal(filtered_table, + @table.filter(filter)) + end + + def test_filter_emit_null + filter = build_boolean_array([false, true, nil]) + arrays = [ + build_boolean_array([false, nil]), + build_boolean_array([true, nil]), + ] + filtered_table = Arrow::Table.new(@schema, arrays) + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(filtered_table, + @table.filter(filter, options)) + end + + def test_filter_chunked_array + chunks = [ + build_boolean_array([false]), + build_boolean_array([true, nil]), + ] + filter = Arrow::ChunkedArray.new(chunks) + arrays = [ + build_boolean_array([false]), + build_boolean_array([true]), + ] + filtered_table = Arrow::Table.new(@schema, arrays) + assert_equal(filtered_table, + @table.filter_chunked_array(filter)) + end + + def test_filter_chunked_array_emit_null + chunks = [ + build_boolean_array([false]), + build_boolean_array([true, nil]), + ] + filter = Arrow::ChunkedArray.new(chunks) + arrays = [ + build_boolean_array([false, nil]), + build_boolean_array([true, nil]), + ] + filtered_table = Arrow::Table.new(@schema, arrays) + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(filtered_table, + @table.filter_chunked_array(filter, options)) + end + + def test_invalid_array_length + filter = build_boolean_array([false, true, true, false]) + assert_raise(Arrow::Error::Invalid) do + @table.filter(filter) + end + end + end + + sub_test_case("ChunkedArray") do + def setup + chunks = [ + build_boolean_array([true, false]), + build_boolean_array([true]), + ] + @chunked_array = Arrow::ChunkedArray.new(chunks) + end + + def test_filter + filter = build_boolean_array([false, true, nil]) + chunks = [ + build_boolean_array([false]), + ] + filtered_chunked_array = Arrow::ChunkedArray.new(chunks) + assert_equal(filtered_chunked_array, + @chunked_array.filter(filter)) + end + + def test_filter_emit_null + filter = build_boolean_array([false, true, nil]) + chunks = [ + build_boolean_array([false]), + build_boolean_array([nil]), + ] + filtered_chunked_array = Arrow::ChunkedArray.new(chunks) + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(filtered_chunked_array, + @chunked_array.filter(filter, options)) + end + + def test_filter_chunked_array + chunks = [ + build_boolean_array([false]), + build_boolean_array([true, nil]), + ] + filter = Arrow::ChunkedArray.new(chunks) + filtered_chunks = [ + build_boolean_array([false]), + ] + filtered_chunked_array = Arrow::ChunkedArray.new(filtered_chunks) + assert_equal(filtered_chunked_array, + @chunked_array.filter_chunked_array(filter)) + end + + def test_filter_chunked_array_emit_null + chunks = [ + build_boolean_array([false]), + build_boolean_array([true, nil]), + ] + filter = Arrow::ChunkedArray.new(chunks) + filtered_chunks = [ + build_boolean_array([false]), + build_boolean_array([nil]), + ] + filtered_chunked_array = Arrow::ChunkedArray.new(filtered_chunks) + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(filtered_chunked_array, + @chunked_array.filter_chunked_array(filter, options)) + end + + def test_invalid_array_length + filter = build_boolean_array([false, true, true, false]) + assert_raise(Arrow::Error::Invalid) do + @chunked_array.filter(filter) + end + end + end + + sub_test_case("RecordBatch") do + def setup + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(fields) + columns = [ + build_boolean_array([true, false, true]), + build_boolean_array([false, true, false]), + ] + @record_batch = Arrow::RecordBatch.new(@schema, 3, columns) + end + + def test_filter + filter = build_boolean_array([false, true, nil]) + columns = [ + build_boolean_array([false]), + build_boolean_array([true]), + ] + filtered_record_batch = Arrow::RecordBatch.new(@schema, 1, columns) + assert_equal(filtered_record_batch, + @record_batch.filter(filter)) + end + + def test_filter_emit_null + filter = build_boolean_array([false, true, nil]) + columns = [ + build_boolean_array([false, nil]), + build_boolean_array([true, nil]), + ] + filtered_record_batch = Arrow::RecordBatch.new(@schema, 2, columns) + options = Arrow::FilterOptions.new + options.null_selection_behavior = :emit_null + assert_equal(filtered_record_batch, + @record_batch.filter(filter, options)) + end + + def test_invalid_array_length + filter = build_boolean_array([false, true, true, false]) + assert_raise(Arrow::Error::Invalid) do + @record_batch.filter(filter) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-fixed-size-binary-array.rb b/src/arrow/c_glib/test/test-fixed-size-binary-array.rb new file mode 100644 index 000000000..29189e78a --- /dev/null +++ b/src/arrow/c_glib/test/test-fixed-size-binary-array.rb @@ -0,0 +1,59 @@ +# 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 TestFixedSizeBinaryrray < Test::Unit::TestCase + include Helper::Buildable + + def setup + @data_type = Arrow::FixedSizeBinaryDataType.new(4) + end + + def test_new + args = [ + @data_type, + 3, + Arrow::Buffer.new("0123abcd0000"), + Arrow::Buffer.new([0b011].pack("C*")), + -1, + ] + assert_equal(build_fixed_size_binary_array(@data_type, + ["0123", "abcd", nil]), + Arrow::FixedSizeBinaryArray.new(*args)) + end + + def test_buffer + array = build_fixed_size_binary_array(@data_type, + ["0123", "abcd", "0000"]) + assert_equal("0123abcd0000", array.buffer.data.to_s) + end + + def test_byte_width + array = build_fixed_size_binary_array(@data_type, ["0123"]) + assert_equal(@data_type.byte_width, array.byte_width) + end + + def test_value + array = build_fixed_size_binary_array(@data_type, ["0123"]) + assert_equal("0123", array.get_value(0).to_s) + end + + def test_values_bytes + array = build_fixed_size_binary_array(@data_type, + ["0123", "abcd", "0000"]) + assert_equal("0123abcd0000", array.values_bytes.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-fixed-size-binary-data-type.rb b/src/arrow/c_glib/test/test-fixed-size-binary-data-type.rb new file mode 100644 index 000000000..b2dfa9df6 --- /dev/null +++ b/src/arrow/c_glib/test/test-fixed-size-binary-data-type.rb @@ -0,0 +1,43 @@ +# 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 TestFixedSizeBinaryDataType < Test::Unit::TestCase + def setup + @byte_width = 10 + @data_type = Arrow::FixedSizeBinaryDataType.new(@byte_width) + end + + def test_type + assert_equal(Arrow::Type::FIXED_SIZE_BINARY, @data_type.id) + end + + def test_name + assert_equal("fixed_size_binary", @data_type.name) + end + + def test_to_s + assert_equal("fixed_size_binary[10]", @data_type.to_s) + end + + def test_byte_width + assert_equal(@byte_width, @data_type.byte_width) + end + + def test_bit_width + assert_equal(@byte_width * 8, @data_type.bit_width) + end +end diff --git a/src/arrow/c_glib/test/test-fixed-size-binary-scalar.rb b/src/arrow/c_glib/test/test-fixed-size-binary-scalar.rb new file mode 100644 index 000000000..1a6f07035 --- /dev/null +++ b/src/arrow/c_glib/test/test-fixed-size-binary-scalar.rb @@ -0,0 +1,49 @@ +# 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 TestFixedSizeBinaryScalar < Test::Unit::TestCase + def setup + @data_type = Arrow::FixedSizeBinaryDataType.new(3) + @buffer = Arrow::Buffer.new("\x03\x01\x02") + @scalar = Arrow::FixedSizeBinaryScalar.new(@data_type, @buffer) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::FixedSizeBinaryScalar.new(@data_type, @buffer), + @scalar) + end + + def test_to_s + assert_equal("\x03\x01\x02", @scalar.to_s) + end + + def test_value + assert_equal(@buffer, + @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-float-array.rb b/src/arrow/c_glib/test/test-float-array.rb new file mode 100644 index 000000000..a3e774384 --- /dev/null +++ b/src/arrow/c_glib/test/test-float-array.rb @@ -0,0 +1,67 @@ +# 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 TestFloatArray < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_float_array([-1.1, 2.2, nil]), + Arrow::FloatArray.new(3, + Arrow::Buffer.new([-1.1, 2.2].pack("f*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::FloatArrayBuilder.new + builder.append_value(-1.1) + builder.append_value(2.2) + builder.append_value(-4.4) + array = builder.finish + assert_equal([-1.1, 2.2, -4.4].pack("f*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::FloatArrayBuilder.new + builder.append_value(1.5) + array = builder.finish + assert_in_delta(1.5, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::FloatArrayBuilder.new + builder.append_value(1.5) + builder.append_value(3) + builder.append_value(4.5) + array = builder.finish + assert_equal([1.5, 3.0, 4.5], array.values) + end + + sub_test_case("#sum") do + def test_with_nil + array = build_float_array([1.5, 3.0, nil]) + assert_in_delta(4.5, array.sum) + end + + def test_empty + array = build_float_array([]) + assert_in_delta(0.0, array.sum) + end + end +end diff --git a/src/arrow/c_glib/test/test-float-data-type.rb b/src/arrow/c_glib/test/test-float-data-type.rb new file mode 100644 index 000000000..f70a60b53 --- /dev/null +++ b/src/arrow/c_glib/test/test-float-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestFloatDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::FloatDataType.new + assert_equal(Arrow::Type::FLOAT, data_type.id) + end + + def test_name + data_type = Arrow::FloatDataType.new + assert_equal("float", data_type.name) + end + + def test_to_s + data_type = Arrow::FloatDataType.new + assert_equal("float", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-float-scalar.rb b/src/arrow/c_glib/test/test-float-scalar.rb new file mode 100644 index 000000000..1b830408c --- /dev/null +++ b/src/arrow/c_glib/test/test-float-scalar.rb @@ -0,0 +1,49 @@ +# 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 TestFloatScalar < Test::Unit::TestCase + def setup + @scalar = Arrow::FloatScalar.new(1.1) + end + + def test_data_type + assert_equal(Arrow::FloatDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + options = Arrow::EqualOptions.new + options.approx = true + assert do + @scalar.equal_options(Arrow::FloatScalar.new(1.1), options) + end + end + + def test_to_s + assert_equal("1.1", @scalar.to_s) + end + + def test_value + assert_in_delta(1.1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-function-doc.rb b/src/arrow/c_glib/test/test-function-doc.rb new file mode 100644 index 000000000..7e624a5ab --- /dev/null +++ b/src/arrow/c_glib/test/test-function-doc.rb @@ -0,0 +1,43 @@ +# 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 TestFunctionDoc < Test::Unit::TestCase + def setup + @doc = Arrow::Function.find("or").doc + end + + def test_summary + assert_equal("Logical 'or' boolean values", + @doc.summary) + end + + def test_description + assert_equal(<<-DESCRIPTION.chomp, @doc.description) +When a null is encountered in either input, a null is output. +For a different null behavior, see function "or_kleene". + DESCRIPTION + end + + def test_arg_names + assert_equal(["x", "y"], @doc.arg_names) + end + + def test_options_class_name + doc = Arrow::Function.find("cast").doc + assert_equal("CastOptions", doc.options_class_name) + end +end diff --git a/src/arrow/c_glib/test/test-function.rb b/src/arrow/c_glib/test/test-function.rb new file mode 100644 index 000000000..390bed5cc --- /dev/null +++ b/src/arrow/c_glib/test/test-function.rb @@ -0,0 +1,83 @@ +# 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 TestFunction < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case("#execute") do + def test_array + or_function = Arrow::Function.find("or") + args = [ + Arrow::ArrayDatum.new(build_boolean_array([true, false, false])), + Arrow::ArrayDatum.new(build_boolean_array([true, false, true])), + ] + assert_equal(build_boolean_array([true, false, true]), + or_function.execute(args).value) + end + + def test_chunked_array + or_function = Arrow::Function.find("or") + chunked_arrays = [ + Arrow::ChunkedArray.new([ + build_boolean_array([true]), + build_boolean_array([false, false]), + ]), + Arrow::ChunkedArray.new([ + build_boolean_array([true, false]), + build_boolean_array([true]), + ]), + ] + args = chunked_arrays.collect do |chunked_array| + Arrow::ChunkedArrayDatum.new(chunked_array) + end + expected_array = build_boolean_array([true, false, true]) + expected = Arrow::ChunkedArray.new([expected_array]) + assert_equal(expected, + or_function.execute(args).value) + end + + def test_input_scalar + add_function = Arrow::Function.find("add") + args = [ + Arrow::ArrayDatum.new(build_int8_array([1, 2, 3])), + Arrow::ScalarDatum.new(Arrow::Int8Scalar.new(5)), + ] + assert_equal(build_int8_array([6, 7, 8]), + add_function.execute(args).value) + end + + def test_output_scalar + sum_function = Arrow::Function.find("sum") + args = [ + Arrow::ArrayDatum.new(build_int8_array([1, 2, 3])), + ] + assert_equal(Arrow::Int64Scalar.new(6), + sum_function.execute(args).value) + end + + def test_options + cast_function = Arrow::Function.find("cast") + args = [ + Arrow::ArrayDatum.new(build_string_array(["1", "2", "-3"])), + ] + options = Arrow::CastOptions.new + options.to_data_type = Arrow::Int8DataType.new + assert_equal(build_int8_array([1, 2, -3]), + cast_function.execute(args, options).value) + end + end +end diff --git a/src/arrow/c_glib/test/test-gio-input-stream.rb b/src/arrow/c_glib/test/test-gio-input-stream.rb new file mode 100644 index 000000000..8cc109987 --- /dev/null +++ b/src/arrow/c_glib/test/test-gio-input-stream.rb @@ -0,0 +1,72 @@ +# 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 TestGIOInputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_reader_backend + data = [true] + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + + tempfile = Tempfile.open("arrow-gio-input-stream") + output = Arrow::FileOutputStream.new(tempfile.path, false) + begin + file_writer = Arrow::RecordBatchFileWriter.new(output, schema) + begin + record_batch = Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]) + file_writer.write_record_batch(record_batch) + ensure + file_writer.close + end + ensure + output.close + end + + file = Gio::File.new_for_path(tempfile.path) + input_stream = file.read + input = Arrow::GIOInputStream.new(input_stream) + begin + file_reader = Arrow::RecordBatchFileReader.new(input) + assert_equal([field.name], + file_reader.schema.fields.collect(&:name)) + assert_equal(Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]), + file_reader.read_record_batch(0)) + ensure + input.close + end + end + + def test_getter + input_stream = Gio::MemoryInputStream.new("Hello") + input = Arrow::GIOInputStream.new(input_stream) + assert_equal(input_stream, input.raw) + end + + def test_peek + input_stream = Gio::MemoryInputStream.new("Hello World") + buffered_input_stream = Gio::BufferedInputStream.new(input_stream) + input = Arrow::GIOInputStream.new(buffered_input_stream) + assert_equal("He", input.peek(2).to_s) + assert_equal("Hel", input.read_bytes(3).to_s) + assert_equal("lo ", input.peek(3).to_s) + end +end diff --git a/src/arrow/c_glib/test/test-gio-output-stream.rb b/src/arrow/c_glib/test/test-gio-output-stream.rb new file mode 100644 index 000000000..36756cb00 --- /dev/null +++ b/src/arrow/c_glib/test/test-gio-output-stream.rb @@ -0,0 +1,79 @@ +# 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 TestGIOOutputStream < Test::Unit::TestCase + include Helper::Buildable + + def test_writer_backend + data = [true] + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + + tempfile = Tempfile.open("arrow-gio-output-stream") + file = Gio::File.new_for_path(tempfile.path) + output_stream = file.append_to(:none) + output = Arrow::GIOOutputStream.new(output_stream) + begin + file_writer = Arrow::RecordBatchFileWriter.new(output, schema) + begin + record_batch = Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]) + file_writer.write_record_batch(record_batch) + ensure + file_writer.close + end + ensure + output.close + end + + input = Arrow::MemoryMappedInputStream.new(tempfile.path) + begin + file_reader = Arrow::RecordBatchFileReader.new(input) + assert_equal([field.name], + file_reader.schema.fields.collect(&:name)) + assert_equal(Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]), + file_reader.read_record_batch(0)) + ensure + input.close + end + end + + def test_getter + output_stream = Gio::MemoryOutputStream.new + output = Arrow::GIOOutputStream.new(output_stream) + assert_equal(output_stream, output.raw) + end + + def test_tell + unless Gio.const_defined?(:UnixOutputStream) + omit("Need Gio::UnixOutputStream") + end + tempfile = Tempfile.open("arrow-gio-output-stream") + begin + output_stream = Gio::UnixOutputStream.new(tempfile.to_i, false) + output = Arrow::GIOOutputStream.new(output_stream) + assert_equal(0, output.tell) + output.write("Hello") + assert_equal(5, output.tell) + ensure + tempfile.close! + end + end +end diff --git a/src/arrow/c_glib/test/test-int-array-builder.rb b/src/arrow/c_glib/test/test-int-array-builder.rb new file mode 100644 index 000000000..e1a6c3b21 --- /dev/null +++ b/src/arrow/c_glib/test/test-int-array-builder.rb @@ -0,0 +1,59 @@ +# 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 TestIntArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + + def test_int8 + values = [-1, 2] + assert_equal(build_int_array([*values, nil]), + Arrow::Int8Array.new(3, + Arrow::Buffer.new(values.pack("c*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_int16 + border_value = (2 ** (8 - 1)) + values = [-1, border_value] + assert_equal(build_int_array([*values, nil]), + Arrow::Int16Array.new(3, + Arrow::Buffer.new(values.pack("s*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_int32 + border_value = (2 ** (16 - 1)) + values = [-1, border_value] + assert_equal(build_int_array([*values, nil]), + Arrow::Int32Array.new(3, + Arrow::Buffer.new(values.pack("l*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_int64 + border_value = (2 ** (32 - 1)) + values = [-1, border_value] + assert_equal(build_int_array([*values, nil]), + Arrow::Int64Array.new(3, + Arrow::Buffer.new(values.pack("q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end +end diff --git a/src/arrow/c_glib/test/test-int16-array.rb b/src/arrow/c_glib/test/test-int16-array.rb new file mode 100644 index 000000000..8c159e910 --- /dev/null +++ b/src/arrow/c_glib/test/test-int16-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 TestInt16Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_int16_array([-1, 2, nil]), + Arrow::Int16Array.new(3, + Arrow::Buffer.new([-1, 2].pack("s*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::Int16ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4].pack("s*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::Int16ArrayBuilder.new + builder.append_value(-1) + array = builder.finish + assert_equal(-1, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::Int16ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4], array.values) + end + + def test_sum + array = build_int16_array([2, -4, nil]) + assert_equal(-2, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-int16-data-type.rb b/src/arrow/c_glib/test/test-int16-data-type.rb new file mode 100644 index 000000000..1b3d51f69 --- /dev/null +++ b/src/arrow/c_glib/test/test-int16-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestInt16DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Int16DataType.new + assert_equal(Arrow::Type::INT16, data_type.id) + end + + def test_name + data_type = Arrow::Int16DataType.new + assert_equal("int16", data_type.name) + end + + def test_to_s + data_type = Arrow::Int16DataType.new + assert_equal("int16", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-int16-scalar.rb b/src/arrow/c_glib/test/test-int16-scalar.rb new file mode 100644 index 000000000..1a7927140 --- /dev/null +++ b/src/arrow/c_glib/test/test-int16-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestInt16Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::Int16Scalar.new(-(2 ** 15)) + end + + def test_data_type + assert_equal(Arrow::Int16DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Int16Scalar.new(-(2 ** 15)), + @scalar) + end + + def test_to_s + assert_equal((-(2 ** 15)).to_s, @scalar.to_s) + end + + def test_value + assert_equal(-(2 ** 15), @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-int32-array.rb b/src/arrow/c_glib/test/test-int32-array.rb new file mode 100644 index 000000000..9dff0e97b --- /dev/null +++ b/src/arrow/c_glib/test/test-int32-array.rb @@ -0,0 +1,58 @@ +# 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 TestInt32Array < Test::Unit::TestCase + include Helper::Buildable + + def test_new + assert_equal(build_int32_array([-1, 2, nil]), + Arrow::Int32Array.new(3, + Arrow::Buffer.new([-1, 2].pack("l*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::Int32ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4].pack("l*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::Int32ArrayBuilder.new + builder.append_value(-1) + array = builder.finish + assert_equal(-1, array.get_value(0)) + end + + def test_values + builder = Arrow::Int32ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4], array.values) + end + + def test_sum + array = build_int32_array([2, -4, nil]) + assert_equal(-2, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-int32-data-type.rb b/src/arrow/c_glib/test/test-int32-data-type.rb new file mode 100644 index 000000000..2afb51742 --- /dev/null +++ b/src/arrow/c_glib/test/test-int32-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestInt32DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Int32DataType.new + assert_equal(Arrow::Type::INT32, data_type.id) + end + + def test_name + data_type = Arrow::Int32DataType.new + assert_equal("int32", data_type.name) + end + + def test_to_s + data_type = Arrow::Int32DataType.new + assert_equal("int32", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-int32-scalar.rb b/src/arrow/c_glib/test/test-int32-scalar.rb new file mode 100644 index 000000000..eba554845 --- /dev/null +++ b/src/arrow/c_glib/test/test-int32-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestInt32Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::Int32Scalar.new(-(2 ** 31)) + end + + def test_data_type + assert_equal(Arrow::Int32DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Int32Scalar.new(-(2 ** 31)), + @scalar) + end + + def test_to_s + assert_equal((-(2 ** 31)).to_s, @scalar.to_s) + end + + def test_value + assert_equal(-(2 ** 31), @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-int64-array.rb b/src/arrow/c_glib/test/test-int64-array.rb new file mode 100644 index 000000000..f6327c7c2 --- /dev/null +++ b/src/arrow/c_glib/test/test-int64-array.rb @@ -0,0 +1,58 @@ +# 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 TestInt64Array < Test::Unit::TestCase + include Helper::Buildable + + def test_new + assert_equal(build_int64_array([-1, 2, nil]), + Arrow::Int64Array.new(3, + Arrow::Buffer.new([-1, 2].pack("q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::Int64ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4].pack("q*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::Int64ArrayBuilder.new + builder.append_value(-1) + array = builder.finish + assert_equal(-1, array.get_value(0)) + end + + def test_values + builder = Arrow::Int64ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4], array.values) + end + + def test_sum + array = build_int64_array([2, -4, nil]) + assert_equal(-2, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-int64-data-type.rb b/src/arrow/c_glib/test/test-int64-data-type.rb new file mode 100644 index 000000000..39764df5c --- /dev/null +++ b/src/arrow/c_glib/test/test-int64-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestInt64DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Int64DataType.new + assert_equal(Arrow::Type::INT64, data_type.id) + end + + def test_name + data_type = Arrow::Int64DataType.new + assert_equal("int64", data_type.name) + end + + def test_to_s + data_type = Arrow::Int64DataType.new + assert_equal("int64", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-int64-scalar.rb b/src/arrow/c_glib/test/test-int64-scalar.rb new file mode 100644 index 000000000..bfa7b4529 --- /dev/null +++ b/src/arrow/c_glib/test/test-int64-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestInt64Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::Int64Scalar.new(-(2 ** 63)) + end + + def test_data_type + assert_equal(Arrow::Int64DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Int64Scalar.new(-(2 ** 63)), + @scalar) + end + + def test_to_s + assert_equal((-(2 ** 63)).to_s, @scalar.to_s) + end + + def test_value + assert_equal(-(2 ** 63), @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-int8-array.rb b/src/arrow/c_glib/test/test-int8-array.rb new file mode 100644 index 000000000..21740305b --- /dev/null +++ b/src/arrow/c_glib/test/test-int8-array.rb @@ -0,0 +1,65 @@ +# 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 TestInt8Array < Test::Unit::TestCase + include Helper::Buildable + + def test_new + assert_equal(build_int8_array([-1, 2, nil]), + Arrow::Int8Array.new(3, + Arrow::Buffer.new([-1, 2].pack("c*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::Int8ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4].pack("c*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::Int8ArrayBuilder.new + builder.append_value(-1) + array = builder.finish + assert_equal(-1, array.get_value(0)) + end + + def test_values + builder = Arrow::Int8ArrayBuilder.new + builder.append_value(-1) + builder.append_value(2) + builder.append_value(-4) + array = builder.finish + assert_equal([-1, 2, -4], array.values) + end + + sub_test_case("#sum") do + def test_with_null + array = build_int8_array([2, -4, nil]) + assert_equal(-2, array.sum) + end + + def test_empty + array = build_int8_array([]) + assert_equal(0, array.sum) + end + end +end diff --git a/src/arrow/c_glib/test/test-int8-data-type.rb b/src/arrow/c_glib/test/test-int8-data-type.rb new file mode 100644 index 000000000..53391a34d --- /dev/null +++ b/src/arrow/c_glib/test/test-int8-data-type.rb @@ -0,0 +1,40 @@ +# 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 TestInt8DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Int8DataType.new + assert_equal(Arrow::Type::INT8, data_type.id) + end + + def test_signed? + data_type = Arrow::Int8DataType.new + assert do + data_type.signed? + end + end + + def test_name + data_type = Arrow::Int8DataType.new + assert_equal("int8", data_type.name) + end + + def test_to_s + data_type = Arrow::Int8DataType.new + assert_equal("int8", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-int8-scalar.rb b/src/arrow/c_glib/test/test-int8-scalar.rb new file mode 100644 index 000000000..214c59073 --- /dev/null +++ b/src/arrow/c_glib/test/test-int8-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestInt8Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::Int8Scalar.new(-128) + end + + def test_data_type + assert_equal(Arrow::Int8DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Int8Scalar.new(-128), + @scalar) + end + + def test_to_s + assert_equal("-128", @scalar.to_s) + end + + def test_value + assert_equal(-128, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-is-in.rb b/src/arrow/c_glib/test/test-is-in.rb new file mode 100644 index 000000000..590b5e379 --- /dev/null +++ b/src/arrow/c_glib/test/test-is-in.rb @@ -0,0 +1,120 @@ +# 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 TestIsIn < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case("Array") do + def test_no_null + left = build_int16_array([1, 0, 1, 2]) + right = build_int16_array([2, 0]) + assert_equal(build_boolean_array([false, true, false, true]), + left.is_in(right)) + end + + def test_null_in_left + left = build_int16_array([1, 0, nil, 2]) + right = build_int16_array([2, 0, 3]) + assert_equal(build_boolean_array([false, true, false, true]), + left.is_in(right)) + end + + def test_null_in_right + left = build_int16_array([1, 0, 1, 2]) + right = build_int16_array([2, 0, nil]) + assert_equal(build_boolean_array([false, true, false, true]), + left.is_in(right)) + end + + def test_null_in_both + left = build_int16_array([1, 0, nil, 2]) + right = build_int16_array([2, 0, nil]) + assert_equal(build_boolean_array([false, true, true, true]), + left.is_in(right)) + end + + def test_options + left = build_int16_array([1, 0, nil, 2]) + right = build_int16_array([2, 0, nil]) + is_in = Arrow::Function.find("is_in") + options = Arrow::SetLookupOptions.new(Arrow::ArrayDatum.new(right)) + assert_equal(build_boolean_array([false, true, true, true]), + is_in.execute([Arrow::ArrayDatum.new(left)], + options).value) + end + end + + sub_test_case("ChunkedArray") do + def test_no_null + left = build_int16_array([1, 0, 1, 2]) + chunks = [ + build_int16_array([1, 4]), + build_int16_array([3, 0]) + ] + right = Arrow::ChunkedArray.new(chunks) + assert_equal(build_boolean_array([true, true, true, false]), + left.is_in_chunked_array(right)) + end + + def test_null_in_left + left = build_int16_array([1, 0, nil, 2]) + chunks = [ + build_int16_array([2, 0]), + build_int16_array([3, 4]) + ] + right = Arrow::ChunkedArray.new(chunks) + assert_equal(build_boolean_array([false, true, false, true]), + left.is_in_chunked_array(right)) + end + + def test_null_in_right + left = build_int16_array([1, 0, 1, 2]) + chunks = [ + build_int16_array([2, 0]), + build_int16_array([3, nil]) + ] + right = Arrow::ChunkedArray.new(chunks) + assert_equal(build_boolean_array([false, true, false, true]), + left.is_in_chunked_array(right)) + end + + def test_null_in_both + left = build_int16_array([1, 0, nil, 2]) + chunks = [ + build_int16_array([2, 0]), + build_int16_array([3, nil]) + ] + right = Arrow::ChunkedArray.new(chunks) + assert_equal(build_boolean_array([false, true, true, true]), + left.is_in_chunked_array(right)) + end + + def test_options + left = build_int16_array([1, 0, nil, 2]) + chunks = [ + build_int16_array([2, 0]), + build_int16_array([3, nil]) + ] + right = Arrow::ChunkedArray.new(chunks) + is_in = Arrow::Function.find("is_in") + options = Arrow::SetLookupOptions.new(Arrow::ChunkedArrayDatum.new(right)) + assert_equal(build_boolean_array([false, true, true, true]), + is_in.execute([Arrow::ArrayDatum.new(left)], + options).value) + end + end +end diff --git a/src/arrow/c_glib/test/test-json-reader.rb b/src/arrow/c_glib/test/test-json-reader.rb new file mode 100644 index 000000000..df028a451 --- /dev/null +++ b/src/arrow/c_glib/test/test-json-reader.rb @@ -0,0 +1,90 @@ +# 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 TestJSONReader < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case("#read") do + def open_input(json) + buffer = Arrow::Buffer.new(json) + Arrow::BufferInputStream.new(buffer) + end + + def test_default + table = Arrow::JSONReader.new(open_input(<<-JSON)) +{ "message": "Hello", "count": 3.5, "valid": false } +{ "message": "World", "count": 3.25, "valid": true } + JSON + columns = { + "message" => build_string_array(["Hello", "World"]), + "count" => build_double_array([3.5, 3.25]), + "valid" => build_boolean_array([false, true]), + } + assert_equal(build_table(columns), + table.read) + end + + sub_test_case("unexpected-field-behavior") do + def setup + @options = Arrow::JSONReadOptions.new + field = Arrow::Field.new("message", Arrow::StringDataType.new) + schema = Arrow::Schema.new([field]) + @options.schema = schema + end + + def test_ignore + @options.unexpected_field_behavior = :ignore + table = Arrow::JSONReader.new(open_input(<<-JSON), @options) +{ "message": "Hello", "count": 3.5, "valid": false } +{ "message": "World", "count": 3.25, "valid": true } + JSON + columns = { + "message" => build_string_array(["Hello", "World"]), + } + assert_equal(build_table(columns), + table.read) + end + + def test_error + @options.unexpected_field_behavior = :error + table = Arrow::JSONReader.new(open_input(<<-JSON), @options) +{ "message": "Hello", "count": 3.5, "valid": false } +{ "message": "World", "count": 3.25, "valid": true } + JSON + assert_raise(Arrow::Error::Invalid) do + table.read + end + end + + def test_infer_type + @options.unexpected_field_behavior = :infer_type + table = Arrow::JSONReader.new(open_input(<<-JSON), @options) +{ "message": "Hello", "count": 3.5, "valid": false } +{ "message": "World", "count": 3.25, "valid": true } + JSON + columns = { + "message" => build_string_array(["Hello", "World"]), + "count" => build_double_array([3.5, 3.25]), + "valid" => build_boolean_array([false, true]), + } + assert_equal(build_table(columns), + table.read) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-large-binary-array.rb b/src/arrow/c_glib/test/test-large-binary-array.rb new file mode 100644 index 000000000..019727fdc --- /dev/null +++ b/src/arrow/c_glib/test/test-large-binary-array.rb @@ -0,0 +1,61 @@ +# 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 TestLargeBinaryArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + value_offsets = Arrow::Buffer.new([0, 2, 5, 5].pack("q*")) + data = Arrow::Buffer.new("\x00\x01\x02\x03\x04") + assert_equal(build_large_binary_array(["\x00\x01", "\x02\x03\x04", nil]), + Arrow::LargeBinaryArray.new(3, + value_offsets, + data, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + data = "\x00\x01\x02" + builder = Arrow::LargeBinaryArrayBuilder.new + builder.append_value(data) + array = builder.finish + assert_equal(data, array.get_value(0).to_s) + end + + def test_buffer + data1 = "\x00\x01\x02" + data2 = "\x03\x04\x05" + builder = Arrow::LargeBinaryArrayBuilder.new + builder.append_value(data1) + builder.append_value(data2) + array = builder.finish + assert_equal(data1 + data2, array.buffer.data.to_s) + end + + def test_offsets_buffer + data1 = "\x00\x01" + data2 = "\x02\x03\x04" + builder = Arrow::LargeBinaryArrayBuilder.new + builder.append_value(data1) + builder.append_value(data2) + array = builder.finish + byte_per_offset = 8 + assert_equal([0, 2, 5].pack("q*"), + array.offsets_buffer.data.to_s[0, byte_per_offset * 3]) + end +end diff --git a/src/arrow/c_glib/test/test-large-binary-data-type.rb b/src/arrow/c_glib/test/test-large-binary-data-type.rb new file mode 100644 index 000000000..63983b7af --- /dev/null +++ b/src/arrow/c_glib/test/test-large-binary-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestLargeBinaryDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::LargeBinaryDataType.new + assert_equal(Arrow::Type::LARGE_BINARY, data_type.id) + end + + def test_name + data_type = Arrow::LargeBinaryDataType.new + assert_equal("large_binary", data_type.name) + end + + def test_to_s + data_type = Arrow::LargeBinaryDataType.new + assert_equal("large_binary", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-large-binary-scalar.rb b/src/arrow/c_glib/test/test-large-binary-scalar.rb new file mode 100644 index 000000000..a6bc4addb --- /dev/null +++ b/src/arrow/c_glib/test/test-large-binary-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestLargeBinaryScalar < Test::Unit::TestCase + def setup + @buffer = Arrow::Buffer.new("\x03\x01\x02") + @scalar = Arrow::LargeBinaryScalar.new(@buffer) + end + + def test_data_type + assert_equal(Arrow::LargeBinaryDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::LargeBinaryScalar.new(@buffer), + @scalar) + end + + def test_to_s + assert_equal("...", @scalar.to_s) + end + + def test_value + assert_equal(@buffer, + @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-large-list-array.rb b/src/arrow/c_glib/test/test-large-list-array.rb new file mode 100644 index 000000000..2f7efab5a --- /dev/null +++ b/src/arrow/c_glib/test/test-large-list-array.rb @@ -0,0 +1,98 @@ +# 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 TestLargeListArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + field = Arrow::Field.new("value", Arrow::Int64DataType.new) + data_type = Arrow::LargeListDataType.new(field) + value_offsets = Arrow::Buffer.new([0, 2, 5, 5].pack("q*")) + data = Arrow::Buffer.new([1, 2, 3, 4, 5].pack("q*")) + nulls = Arrow::Buffer.new([0b11111].pack("C*")) + values = Arrow::Int64Array.new(5, data, nulls, 0) + assert_equal(build_large_list_array(Arrow::Int64DataType.new, + [[1, 2], [3, 4, 5], nil]), + Arrow::LargeListArray.new(data_type, + 3, + value_offsets, + values, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + array = build_large_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + value = array.get_value(1) + assert_equal([-1, 0, 1], + value.length.times.collect {|i| value.get_value(i)}) + end + + def test_value_type + field = Arrow::Field.new("value", Arrow::Int64DataType.new) + data_type = Arrow::LargeListDataType.new(field) + builder = Arrow::LargeListArrayBuilder.new(data_type) + array = builder.finish + assert_equal(Arrow::Int64DataType.new, array.value_type) + end + + + def test_values + array = build_large_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + values = array.values + assert_equal([-29, 29, -1, 0, 1], + values.length.times.collect {|i| values.get_value(i)}) + end + + def test_value_offset + array = build_large_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([0, 2], + array.length.times.collect {|i| array.get_value_offset(i)}) + end + + def test_value_length + array = build_large_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([2, 3], + array.length.times.collect {|i| array.get_value_length(i)}) + end + + def test_value_offsets + array = build_large_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([0, 2, 5], + array.value_offsets) + end +end diff --git a/src/arrow/c_glib/test/test-large-list-data-type.rb b/src/arrow/c_glib/test/test-large-list-data-type.rb new file mode 100644 index 000000000..f06fed0a6 --- /dev/null +++ b/src/arrow/c_glib/test/test-large-list-data-type.rb @@ -0,0 +1,48 @@ +# 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 TestLargeListDataType < Test::Unit::TestCase + def setup + @field_data_type = Arrow::BooleanDataType.new + @field = Arrow::Field.new("enabled", @field_data_type) + @data_type = Arrow::LargeListDataType.new(@field) + end + + def test_type + assert_equal(Arrow::Type::LARGE_LIST, @data_type.id) + end + + def test_name + assert_equal("large_list", @data_type.name) + end + + + def test_to_s + assert_equal("large_list<enabled: bool>", @data_type.to_s) + end + + def test_field + assert_equal([ + @field, + @field_data_type, + ], + [ + @data_type.field, + @data_type.field.data_type, + ]) + end +end diff --git a/src/arrow/c_glib/test/test-large-string-array.rb b/src/arrow/c_glib/test/test-large-string-array.rb new file mode 100644 index 000000000..d77f9fee7 --- /dev/null +++ b/src/arrow/c_glib/test/test-large-string-array.rb @@ -0,0 +1,46 @@ +# 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 TestLargeStringArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + value_offsets = Arrow::Buffer.new([0, 5, 11, 11].pack("q*")) + data = Arrow::Buffer.new("HelloWorld!") + assert_equal(build_large_string_array(["Hello", "World!", nil]), + Arrow::LargeStringArray.new(3, + value_offsets, + data, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + builder = Arrow::LargeStringArrayBuilder.new + builder.append_string("Hello World") + array = builder.finish + assert_equal("Hello World", array.get_string(0)) + end + + def test_buffer + builder = Arrow::LargeStringArrayBuilder.new + builder.append_string("Hello") + builder.append_string("World") + array = builder.finish + assert_equal("HelloWorld", array.buffer.data.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-large-string-data-type.rb b/src/arrow/c_glib/test/test-large-string-data-type.rb new file mode 100644 index 000000000..731e27403 --- /dev/null +++ b/src/arrow/c_glib/test/test-large-string-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestLargeStringDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::LargeStringDataType.new + assert_equal(Arrow::Type::LARGE_STRING, data_type.id) + end + + def test_name + data_type = Arrow::LargeStringDataType.new + assert_equal("large_utf8", data_type.name) + end + + def test_to_s + data_type = Arrow::LargeStringDataType.new + assert_equal("large_string", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-large-string-scalar.rb b/src/arrow/c_glib/test/test-large-string-scalar.rb new file mode 100644 index 000000000..13e28f647 --- /dev/null +++ b/src/arrow/c_glib/test/test-large-string-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestLargeStringScalar < Test::Unit::TestCase + def setup + @buffer = Arrow::Buffer.new("Hello") + @scalar = Arrow::LargeStringScalar.new(@buffer) + end + + def test_data_type + assert_equal(Arrow::LargeStringDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::LargeStringScalar.new(@buffer), + @scalar) + end + + def test_to_s + assert_equal("...", @scalar.to_s) + end + + def test_value + assert_equal(@buffer, + @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-list-array.rb b/src/arrow/c_glib/test/test-list-array.rb new file mode 100644 index 000000000..f94b28dd1 --- /dev/null +++ b/src/arrow/c_glib/test/test-list-array.rb @@ -0,0 +1,97 @@ +# 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 TestListArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + field = Arrow::Field.new("value", Arrow::Int8DataType.new) + data_type = Arrow::ListDataType.new(field) + value_offsets = Arrow::Buffer.new([0, 2, 5, 5].pack("l*")) + data = Arrow::Buffer.new([1, 2, 3, 4, 5].pack("c*")) + nulls = Arrow::Buffer.new([0b11111].pack("C*")) + values = Arrow::Int8Array.new(5, data, nulls, 0) + assert_equal(build_list_array(Arrow::Int8DataType.new, + [[1, 2], [3, 4, 5], nil]), + Arrow::ListArray.new(data_type, + 3, + value_offsets, + values, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + array = build_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + value = array.get_value(1) + assert_equal([-1, 0, 1], + value.length.times.collect {|i| value.get_value(i)}) + end + + def test_value_type + field = Arrow::Field.new("value", Arrow::Int8DataType.new) + data_type = Arrow::ListDataType.new(field) + builder = Arrow::ListArrayBuilder.new(data_type) + array = builder.finish + assert_equal(Arrow::Int8DataType.new, array.value_type) + end + + def test_values + array = build_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + values = array.values + assert_equal([-29, 29, -1, 0, 1], + values.length.times.collect {|i| values.get_value(i)}) + end + + def test_value_offset + array = build_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([0, 2], + array.length.times.collect {|i| array.get_value_offset(i)}) + end + + def test_value_length + array = build_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([2, 3], + array.length.times.collect {|i| array.get_value_length(i)}) + end + + def test_value_offsets + array = build_list_array(Arrow::Int8DataType.new, + [ + [-29, 29], + [-1, 0, 1], + ]) + assert_equal([0, 2, 5], + array.value_offsets) + end +end diff --git a/src/arrow/c_glib/test/test-list-data-type.rb b/src/arrow/c_glib/test/test-list-data-type.rb new file mode 100644 index 000000000..f9731d2fe --- /dev/null +++ b/src/arrow/c_glib/test/test-list-data-type.rb @@ -0,0 +1,48 @@ +# 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 TestListDataType < Test::Unit::TestCase + def setup + @field_data_type = Arrow::BooleanDataType.new + @field = Arrow::Field.new("enabled", @field_data_type) + @data_type = Arrow::ListDataType.new(@field) + end + + def test_type + assert_equal(Arrow::Type::LIST, @data_type.id) + end + + def test_name + assert_equal("list", @data_type.name) + end + + + def test_to_s + assert_equal("list<enabled: bool>", @data_type.to_s) + end + + def test_field + assert_equal([ + @field, + @field_data_type, + ], + [ + @data_type.field, + @data_type.field.data_type, + ]) + end +end diff --git a/src/arrow/c_glib/test/test-list-scalar.rb b/src/arrow/c_glib/test/test-list-scalar.rb new file mode 100644 index 000000000..3fda3f25b --- /dev/null +++ b/src/arrow/c_glib/test/test-list-scalar.rb @@ -0,0 +1,50 @@ +# 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 TestListScalar < Test::Unit::TestCase + include Helper::Buildable + + def setup + @value = build_list_array(Arrow::Int8DataType.new, + [[1, 2, 3]]) + @scalar = Arrow::ListScalar.new(@value) + end + + def test_data_type + assert_equal(@value.value_data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::ListScalar.new(@value), + @scalar) + end + + def test_to_s + assert_equal("...", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-literal-expression.rb b/src/arrow/c_glib/test/test-literal-expression.rb new file mode 100644 index 000000000..7a8796fb4 --- /dev/null +++ b/src/arrow/c_glib/test/test-literal-expression.rb @@ -0,0 +1,40 @@ +# 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 TestLiteralExpression < Test::Unit::TestCase + def setup + @scalar = Arrow::BooleanScalar.new(true) + @datum = Arrow::ScalarDatum.new(@scalar) + @expression = Arrow::LiteralExpression.new(@datum) + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::LiteralExpression.new(@datum), + Arrow::LiteralExpression.new(@datum)) + end + + def test_false + assert_not_equal(@expression, + Arrow::FieldExpression.new("visible")) + end + end + + def test_to_string + assert_equal("true", @expression.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-local-file-system.rb b/src/arrow/c_glib/test/test-local-file-system.rb new file mode 100644 index 000000000..884f2bdf5 --- /dev/null +++ b/src/arrow/c_glib/test/test-local-file-system.rb @@ -0,0 +1,57 @@ +# 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. + +require_relative "file-system-tests" + +class TestLocalFileSystem < Test::Unit::TestCase + def setup + Dir.mktmpdir do |tmpdir| + local_fs = Arrow::LocalFileSystem.new(build_options) + @fs = Arrow::SubTreeFileSystem.new(tmpdir, local_fs) + yield + end + end + + def build_options + Arrow::LocalFileSystemOptions.new + end + + sub_test_case("default") do + include FileSystemTests + + def test_type_name + assert_equal([ + "local", + "subtree", + ], + [ + @fs.base_file_system.type_name, + @fs.type_name, + ]) + end + end + + sub_test_case("mmap") do + include FileSystemTests + + def build_options + options = Arrow::LocalFileSystemOptions.new + options.use_mmap = true + options + end + end +end diff --git a/src/arrow/c_glib/test/test-map-array-builder.rb b/src/arrow/c_glib/test/test-map-array-builder.rb new file mode 100644 index 000000000..ce5023ae4 --- /dev/null +++ b/src/arrow/c_glib/test/test-map-array-builder.rb @@ -0,0 +1,143 @@ +# 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 TestMapArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + + def setup + offsets = build_int32_array([0, 2, 5]) + keys = build_string_array(["a", "b", "c", "d", "e"]) + items = build_int16_array([0, 1, 2, 3, 4]) + @map_array = Arrow::MapArray.new(offsets, + keys, + items) + key_type = Arrow::StringDataType.new + item_type = Arrow::Int16DataType.new + data_type = Arrow::MapDataType.new(key_type, item_type) + @builder = Arrow::MapArrayBuilder.new(data_type) + end + + def test_append_value + key_builder = @builder.key_builder + item_builder = @builder.item_builder + + @builder.append_value + key_builder.append_string("a") + key_builder.append_string("b") + item_builder.append_value(0) + item_builder.append_value(1) + + @builder.append_value + key_builder.append_string("c") + key_builder.append_string("d") + key_builder.append_string("e") + item_builder.append_value(2) + item_builder.append_value(3) + item_builder.append_value(4) + + array = @builder.finish + assert_equal([ + @map_array.get_value(0), + @map_array.get_value(1) + ], + [ + array.get_value(0), + array.get_value(1) + ]) + end + + def test_append_values + key_builder = @builder.key_builder + item_builder = @builder.item_builder + @builder.append_values([0, 2, 5]) + key_builder.append_strings(["a", "b", "c", "d", "e"]) + item_builder.append_values([0, 1, 2, 3, 4]) + + array = @builder.finish + assert_equal([ + @map_array.get_value(0), + @map_array.get_value(1) + ], + [ + array.get_value(0), + array.get_value(1) + ]) + end + + def test_append_structs + value_builder = @builder.value_builder + + @builder.append_value + value_builder.append_value + value_builder.get_field_builder(0).append_string("a") + value_builder.get_field_builder(0).append_string("b") + value_builder.get_field_builder(1).append_value(0) + value_builder.get_field_builder(1).append_value(1) + + @builder.append_value + value_builder.append_value + value_builder.get_field_builder(0).append_string("c") + value_builder.get_field_builder(0).append_string("d") + value_builder.get_field_builder(0).append_string("e") + value_builder.get_field_builder(1).append_value(2) + value_builder.get_field_builder(1).append_value(3) + value_builder.get_field_builder(1).append_value(4) + + array = @builder.finish + assert_equal([ + @map_array.get_value(0), + @map_array.get_value(1) + ], + [ + array.get_value(0), + array.get_value(1) + ]) + end + + def test_append_null + @builder.append_null + @builder.append_null + array = @builder.finish + assert_equal(2, array.n_nulls) + end + + def test_append_nulls + @builder.append_nulls(2) + array = @builder.finish + assert_equal(2, array.n_nulls) + end + + def test_append_empty_value + offsets = build_int32_array([0, 0]) + keys = build_string_array([]) + items = build_int16_array([]) + expected_array = Arrow::MapArray.new(offsets, keys, items) + @builder.append_empty_value + assert_equal(expected_array, + @builder.finish) + end + + def test_append_empty_values + offsets = build_int32_array([0, 0, 0, 0]) + keys = build_string_array([]) + items = build_int16_array([]) + expected_array = Arrow::MapArray.new(offsets, keys, items) + @builder.append_empty_values(3) + assert_equal(expected_array, + @builder.finish) + end +end diff --git a/src/arrow/c_glib/test/test-map-array.rb b/src/arrow/c_glib/test/test-map-array.rb new file mode 100644 index 000000000..d70746c84 --- /dev/null +++ b/src/arrow/c_glib/test/test-map-array.rb @@ -0,0 +1,39 @@ +# 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 TestMapArray < Test::Unit::TestCase + include Helper::Buildable + + def setup + offsets = build_int32_array([0, 2, 5]) + @keys = build_string_array(["a", "b", "c", "d", "e"]) + @items = build_int16_array([0, 1, 2, 3, 4]) + @map_array = Arrow::MapArray.new(offsets, + @keys, + @items) + end + + def test_keys + assert_equal(@keys, + @map_array.keys) + end + + def test_items + assert_equal(@items, + @map_array.items) + end +end diff --git a/src/arrow/c_glib/test/test-map-data-type.rb b/src/arrow/c_glib/test/test-map-data-type.rb new file mode 100644 index 000000000..c537c43d8 --- /dev/null +++ b/src/arrow/c_glib/test/test-map-data-type.rb @@ -0,0 +1,44 @@ +# 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 TestMapDataType < Test::Unit::TestCase + def setup + @key_type = Arrow::StringDataType.new + @item_type = Arrow::Int16DataType.new + @data_type = Arrow::MapDataType.new(@key_type, @item_type) + end + + def test_type + assert_equal(Arrow::Type::MAP, @data_type.id) + end + + def test_name + assert_equal("map", @data_type.name) + end + + def test_to_s + assert_equal("map<string, int16>", @data_type.to_s) + end + + def test_key + assert_equal(@key_type, @data_type.key_type) + end + + def test_item + assert_equal(@item_type, @data_type.item_type) + end +end diff --git a/src/arrow/c_glib/test/test-map-scalar.rb b/src/arrow/c_glib/test/test-map-scalar.rb new file mode 100644 index 000000000..9c6eb69e0 --- /dev/null +++ b/src/arrow/c_glib/test/test-map-scalar.rb @@ -0,0 +1,65 @@ +# 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 TestMapScalar < Test::Unit::TestCase + include Helper::Buildable + + def setup + @value = build_struct_array([ + Arrow::Field.new("key", + Arrow::StringDataType.new, + false), + Arrow::Field.new("value", + Arrow::Int8DataType.new), + ], + [ + { + "key" => "hello", + "value" => 1, + }, + { + "key" => "world", + "value" => 2, + }, + ]) + @scalar = Arrow::MapScalar.new(@value) + end + + def test_data_type + assert_equal(@value.value_data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::MapScalar.new(@value), + @scalar) + end + + def test_to_s + assert_equal("...", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-memory-mapped-input-stream.rb b/src/arrow/c_glib/test/test-memory-mapped-input-stream.rb new file mode 100644 index 000000000..7c5f933b4 --- /dev/null +++ b/src/arrow/c_glib/test/test-memory-mapped-input-stream.rb @@ -0,0 +1,84 @@ +# 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 TestMemoryMappedInputStream < Test::Unit::TestCase + def setup + @data = "Hello World" + @tempfile = Tempfile.open("arrow-memory-mapped-input-stream") + @tempfile.write(@data) + @tempfile.close + end + + def test_new + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + begin + buffer = input.read(5) + assert_equal("Hello", buffer.data.to_s) + ensure + input.close + end + end + + def test_close + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + assert do + not input.closed? + end + input.close + assert do + input.closed? + end + end + + def test_size + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + begin + assert_equal(@data.bytesize, input.size) + ensure + input.close + end + end + + def test_read + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + begin + buffer = input.read(5) + assert_equal("Hello", buffer.data.to_s) + ensure + input.close + end + end + + def test_read_at + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + begin + buffer = input.read_at(6, 5) + assert_equal("World", buffer.data.to_s) + ensure + input.close + end + end + + def test_mode + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + begin + assert_equal(Arrow::FileMode::READWRITE, input.mode) + ensure + input.close + end + end +end diff --git a/src/arrow/c_glib/test/test-mock-file-system.rb b/src/arrow/c_glib/test/test-mock-file-system.rb new file mode 100644 index 000000000..c6148d699 --- /dev/null +++ b/src/arrow/c_glib/test/test-mock-file-system.rb @@ -0,0 +1,30 @@ +# 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. + +require_relative "file-system-tests" + +class TestMockFileSystem < Test::Unit::TestCase + include FileSystemTests + + def setup + @fs = Arrow::FileSystem.create("mock://") + end + + def test_type_name + assert_equal("mock", @fs.type_name) + end +end diff --git a/src/arrow/c_glib/test/test-mutable-buffer.rb b/src/arrow/c_glib/test/test-mutable-buffer.rb new file mode 100644 index 000000000..ccfd6e222 --- /dev/null +++ b/src/arrow/c_glib/test/test-mutable-buffer.rb @@ -0,0 +1,74 @@ +# 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 TestMutableBuffer < Test::Unit::TestCase + def setup + @data = "Hello" + @buffer = Arrow::MutableBuffer.new(@data) + end + + def test_new_bytes + bytes_data = GLib::Bytes.new(@data) + buffer = Arrow::MutableBuffer.new(bytes_data) + if GLib.check_binding_version?(3, 2, 2) + assert_equal(bytes_data.pointer, buffer.mutable_data.pointer) + else + assert_equal(@data, buffer.mutable_data.to_s) + end + end + + def test_mutable? + assert do + @buffer.mutable? + end + end + + def test_mutable_data + assert_equal(@data, @buffer.mutable_data.to_s) + end + + def test_slice + sliced_buffer = @buffer.slice(1, 3) + assert_equal(@data[1, 3], sliced_buffer.data.to_s) + end + + sub_test_case("#set_data") do + test("offset") do + @buffer.set_data(1, "EL") + assert_equal("HELlo", @buffer.data.to_s) + end + + test("replace") do + @buffer.set_data(0, "World") + assert_equal("World", @buffer.data.to_s) + end + + test("offset: too large") do + message = "[mutable-buffer][set-data]: Data is too large: <(5 + 1) > (5)>" + assert_raise(Arrow::Error::Invalid.new(message)) do + @buffer.set_data(5, "X") + end + end + + test("data too large") do + message = "[mutable-buffer][set-data]: Data is too large: <(0 + 6) > (5)>" + assert_raise(Arrow::Error::Invalid.new(message)) do + @buffer.set_data(0, @data + "!") + end + end + end +end diff --git a/src/arrow/c_glib/test/test-null-array.rb b/src/arrow/c_glib/test/test-null-array.rb new file mode 100644 index 000000000..6aa8c037c --- /dev/null +++ b/src/arrow/c_glib/test/test-null-array.rb @@ -0,0 +1,33 @@ +# 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 TestNullArray < Test::Unit::TestCase + def test_length + array = Arrow::NullArray.new(3) + assert_equal(3, array.length) + end + + def test_n_nulls + array = Arrow::NullArray.new(3) + assert_equal(3, array.n_nulls) + end + + def test_slice + array = Arrow::NullArray.new(3) + assert_equal(2, array.slice(1, 2).length) + end +end diff --git a/src/arrow/c_glib/test/test-null-data-type.rb b/src/arrow/c_glib/test/test-null-data-type.rb new file mode 100644 index 000000000..8440b9900 --- /dev/null +++ b/src/arrow/c_glib/test/test-null-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestNullDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::NullDataType.new + assert_equal(Arrow::Type::NA, data_type.id) + end + + def test_name + data_type = Arrow::NullDataType.new + assert_equal("null", data_type.name) + end + + def test_to_s + data_type = Arrow::NullDataType.new + assert_equal("null", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-null-scalar.rb b/src/arrow/c_glib/test/test-null-scalar.rb new file mode 100644 index 000000000..07b887040 --- /dev/null +++ b/src/arrow/c_glib/test/test-null-scalar.rb @@ -0,0 +1,42 @@ +# 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 TestNullScalar < Test::Unit::TestCase + def setup + @scalar = Arrow::NullScalar.new + end + + def test_data_type + assert_equal(Arrow::NullDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + not @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::NullScalar.new, + @scalar) + end + + def test_to_s + assert_equal("null", @scalar.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-numeric-array.rb b/src/arrow/c_glib/test/test-numeric-array.rb new file mode 100644 index 000000000..f90007c03 --- /dev/null +++ b/src/arrow/c_glib/test/test-numeric-array.rb @@ -0,0 +1,26 @@ +# 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 TestNumericArray < Test::Unit::TestCase + include Helper::Buildable + + def test_mean + array = build_double_array([1.1, 2.2, nil]) + assert_in_delta(array.values.inject(&:+) / 2, + array.mean) + end +end diff --git a/src/arrow/c_glib/test/test-orc-file-reader.rb b/src/arrow/c_glib/test/test-orc-file-reader.rb new file mode 100644 index 000000000..38900cf12 --- /dev/null +++ b/src/arrow/c_glib/test/test-orc-file-reader.rb @@ -0,0 +1,218 @@ +# 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 TestORCFileReader < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + include Helper::Fixture + + def setup + omit("Require Apache Arrow ORC") unless Arrow.const_defined?(:ORCFileReader) + path = fixture_path("TestOrcFile.test1.orc") + input = Arrow::MemoryMappedInputStream.new(path) + @reader = Arrow::ORCFileReader.new(input) + end + + def test_read_type + assert_equal(<<-SCHEMA.chomp, @reader.read_type.to_s) +boolean1: bool +byte1: int8 +short1: int16 +int1: int32 +long1: int64 +float1: float +double1: double +bytes1: binary +string1: string +middle: struct<list: list<item: struct<int1: int32, string1: string>>> +list: list<item: struct<int1: int32, string1: string>> +map: map<string, struct<int1: int32, string1: string>> + SCHEMA + end + + def test_field_indices + require_gi_bindings(3, 2, 6) + assert_nil(@reader.field_indices) + @reader.field_indices = [1, 3] + assert_equal([1, 3], @reader.field_indices) + end + + def item_fields + [ + Arrow::Field.new("int1", Arrow::Int32DataType.new), + Arrow::Field.new("string1", Arrow::StringDataType.new), + ] + end + + def item_data_type + Arrow::StructDataType.new(item_fields) + end + + def build_items_array(items_array) + build_list_array(item_data_type, items_array, field_name: "item") + end + + def items_data_type + Arrow::ListDataType.new(Arrow::Field.new("item", item_data_type)) + end + + def middle_fields + [ + Arrow::Field.new("list", items_data_type), + ] + end + + def build_middle_array(middles) + build_struct_array(middle_fields, middles) + end + + def middle_array + build_middle_array([ + { + "list" => [ + { + "int1" => 1, + "string1" => "bye", + }, + { + "int1" => 2, + "string1" => "sigh", + }, + ], + }, + { + "list" => [ + { + "int1" => 1, + "string1" => "bye", + }, + { + "int1" => 2, + "string1" => "sigh", + }, + ], + }, + ]) + end + + def list_array + build_items_array([ + [ + { + "int1" => 3, + "string1" => "good", + }, + { + "int1" => 4, + "string1" => "bad", + }, + ], + [ + { + "int1" => 100000000, + "string1" => "cat", + }, + { + "int1" => -100000, + "string1" => "in", + }, + { + "int1" => 1234, + "string1" => "hat", + }, + ] + ]) + end + + def map_array + build_map_array(Arrow::StringDataType.new, + item_data_type, + [ + {}, + { + "chani" => { + "int1" => 5, + "string1" => "chani", + }, + "mauddib" => { + "int1" => 1, + "string1" => "mauddib", + }, + }, + ]) + end + + def all_columns + { + "boolean1" => build_boolean_array([false, true]), + "byte1" => build_int8_array([1, 100]), + "short1" => build_int16_array([1024, 2048]), + "int1" => build_int32_array([65536, 65536]), + "long1" => build_int64_array([ + 9223372036854775807, + 9223372036854775807, + ]), + "float1" => build_float_array([1.0, 2.0]), + "double1" => build_double_array([-15.0, -5.0]), + "bytes1" => build_binary_array(["\x00\x01\x02\x03\x04", ""]), + "string1" => build_string_array(["hi", "bye"]), + "middle" => middle_array, + "list" => list_array, + "map" => map_array, + } + end + + sub_test_case("#read_stripes") do + test("all") do + assert_equal(build_table(all_columns), + @reader.read_stripes) + end + + test("select fields") do + require_gi_bindings(3, 2, 6) + @reader.field_indices = [1, 3] + assert_equal(build_table("boolean1" => build_boolean_array([false, true]), + "short1" => build_int16_array([1024, 2048])), + @reader.read_stripes) + end + end + + sub_test_case("#read_stripe") do + test("all") do + assert_equal(build_record_batch(all_columns), + @reader.read_stripe(0)) + end + + test("select fields") do + require_gi_bindings(3, 2, 6) + @reader.field_indices = [1, 3] + boolean1 = build_boolean_array([false, true]) + short1 = build_int16_array([1024, 2048]) + assert_equal(build_record_batch("boolean1" => boolean1, + "short1" => short1), + @reader.read_stripe(0)) + end + end + + def test_n_stripes + assert_equal(1, @reader.n_stripes) + end + + def test_n_rows + assert_equal(2, @reader.n_rows) + end +end diff --git a/src/arrow/c_glib/test/test-read-options.rb b/src/arrow/c_glib/test/test-read-options.rb new file mode 100644 index 000000000..5b2a69cd3 --- /dev/null +++ b/src/arrow/c_glib/test/test-read-options.rb @@ -0,0 +1,61 @@ +# 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 TestReadOptions < Test::Unit::TestCase + def setup + @options = Arrow::ReadOptions.new + end + + sub_test_case("max-recursion-depth") do + def test_default + assert_equal(64, @options.max_recursion_depth) + end + + def test_accessor + @options.max_recursion_depth = 29 + assert_equal(29, @options.max_recursion_depth) + end + end + + sub_test_case("use-threads") do + def test_default + assert do + @options.use_threads? + end + end + + def test_accessor + @options.use_threads = false + assert do + not @options.use_threads? + end + end + end + + sub_test_case("#included_fields") do + def test_default + assert_equal([], @options.included_fields) + end + + def test_accessor + @options.included_fields = [1, 2, 3] + assert_equal([1, 2, 3], @options.included_fields) + @options.included_fields = [] + assert_equal([], @options.included_fields) + end + end +end diff --git a/src/arrow/c_glib/test/test-record-batch-builder.rb b/src/arrow/c_glib/test/test-record-batch-builder.rb new file mode 100644 index 000000000..ce8efdffd --- /dev/null +++ b/src/arrow/c_glib/test/test-record-batch-builder.rb @@ -0,0 +1,86 @@ +# 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 TestRecordBatchBuilder < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def setup + @fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("point", Arrow::Int32DataType.new), + ] + @schema = Arrow::Schema.new(@fields) + @builder = Arrow::RecordBatchBuilder.new(@schema) + end + + def test_initial_capacity + @builder.initial_capacity = 128 + assert_equal(128, @builder.initial_capacity) + end + + def test_schema + assert_equal(@schema, @builder.schema) + end + + def test_n_columns + assert_equal(@fields.size, @builder.n_columns) + end + + sub_test_case("#get_column_builder") do + def test_valid + assert_equal(Arrow::BooleanArrayBuilder, + @builder.get_column_builder(0).class) + end + + def test_negative + assert_equal(Arrow::Int32ArrayBuilder, + @builder.get_column_builder(-1).class) + end + + def test_too_negative + assert_nil(@builder.get_column_builder(-@fields.size - 1)) + end + + def test_too_large + assert_nil(@builder.get_column_builder(@fields.size)) + end + end + + def test_flush + require_gi_bindings(3, 3, 1) + arrays = { + "visible" => build_boolean_array([true, false, true]), + "point" => build_int32_array([1, -1, 0]), + } + arrays.each_with_index do |(_, array), i| + @builder.get_column_builder(i).append_values(array.values, []) + end + assert_equal(build_record_batch(arrays), + @builder.flush) + + arrays = { + "visible" => build_boolean_array([false, true]), + "point" => build_int32_array([10, -10]), + } + arrays.each_with_index do |(_, array), i| + @builder.get_column_builder(i).append_values(array.values, []) + end + assert_equal(build_record_batch(arrays), + @builder.flush) + end +end diff --git a/src/arrow/c_glib/test/test-record-batch-datum.rb b/src/arrow/c_glib/test/test-record-batch-datum.rb new file mode 100644 index 000000000..33eb793ba --- /dev/null +++ b/src/arrow/c_glib/test/test-record-batch-datum.rb @@ -0,0 +1,58 @@ +# 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 TestRecordBatchDatum < Test::Unit::TestCase + include Helper::Buildable + + def setup + @array = build_boolean_array([true, false]) + @record_batch = build_record_batch("visible" => @array) + @datum = Arrow::RecordBatchDatum.new(@record_batch) + end + + def test_array? + assert do + not @datum.array? + end + end + + def test_array_like? + assert do + not @datum.array_like? + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::RecordBatchDatum.new(@record_batch), + Arrow::RecordBatchDatum.new(@record_batch)) + end + + def test_false + assert_not_equal(@datum, + Arrow::ArrayDatum.new(@array)) + end + end + + def test_to_string + assert_equal("RecordBatch", @datum.to_s) + end + + def test_value + assert_equal(@record_batch, @datum.value) + end +end diff --git a/src/arrow/c_glib/test/test-record-batch-iterator.rb b/src/arrow/c_glib/test/test-record-batch-iterator.rb new file mode 100644 index 000000000..daedde759 --- /dev/null +++ b/src/arrow/c_glib/test/test-record-batch-iterator.rb @@ -0,0 +1,51 @@ +# 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 TestRecordBatchIterator <Test::Unit::TestCase + include Helper::Buildable + + def setup + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("point", Arrow::Int32DataType.new), + ] + schema = Arrow::Schema.new(fields) + @record_batches = [ + [ + build_boolean_array([true, false, true]), + build_int32_array([1, 2, 3]), + ], + [ + build_boolean_array([false, true, false, true]), + build_int32_array([-1, -2, -3, -4]), + ] + ].collect do |columns| + Arrow::RecordBatch.new(schema, columns[0].length, columns) + end + @iterator = Arrow::RecordBatchIterator.new(@record_batches) + end + + def test_next + assert_equal(@record_batches[0], @iterator.next) + assert_equal(@record_batches[1], @iterator.next) + assert_equal(nil, @iterator.next) + end + + def test_to_list + assert_equal(@record_batches, @iterator.to_list) + end +end diff --git a/src/arrow/c_glib/test/test-record-batch-reader.rb b/src/arrow/c_glib/test/test-record-batch-reader.rb new file mode 100644 index 000000000..489482141 --- /dev/null +++ b/src/arrow/c_glib/test/test-record-batch-reader.rb @@ -0,0 +1,64 @@ +# 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 TestRecordBatchReader <Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def setup + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("point", Arrow::Int32DataType.new), + ] + @schema = Arrow::Schema.new(fields) + @record_batches = [ + [ + build_boolean_array([true, false, true]), + build_int32_array([1, 2, 3]), + ], + [ + build_boolean_array([false, true, false, true]), + build_int32_array([-1, -2, -3, -4]), + ] + ].collect do |columns| + Arrow::RecordBatch.new(@schema, columns[0].length, columns) + end + @reader = Arrow::RecordBatchReader.new(@record_batches, @schema) + end + + def test_export + require_gi_bindings(3, 4, 8) + c_abi_array_stream = @reader.export + assert_equal(Arrow::Table.new(@schema, @record_batches), + Arrow::RecordBatchReader.import(c_abi_array_stream).read_all) + end + + def test_schema + assert_equal(@schema, @reader.schema) + end + + def test_read_next + assert_equal(@record_batches[0], @reader.read_next) + assert_equal(@record_batches[1], @reader.read_next) + assert_nil(@reader.read_next) + end + + def test_read_all + assert_equal(Arrow::Table.new(@schema, @record_batches), + @reader.read_all) + end +end diff --git a/src/arrow/c_glib/test/test-record-batch.rb b/src/arrow/c_glib/test/test-record-batch.rb new file mode 100644 index 000000000..bbdbf82d0 --- /dev/null +++ b/src/arrow/c_glib/test/test-record-batch.rb @@ -0,0 +1,193 @@ +# 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 TestRecordBatch < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case(".new") do + def test_valid + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + columns = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + record_batch = Arrow::RecordBatch.new(schema, 1, columns) + assert_equal(1, record_batch.n_rows) + end + + def test_no_columns + fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + message = "[record-batch][new]: " + + "Invalid: Number of columns did not match schema" + assert_raise(Arrow::Error::Invalid.new(message)) do + Arrow::RecordBatch.new(schema, 0, []) + end + end + end + + sub_test_case("instance methods") do + def setup + @visible_field = Arrow::Field.new("visible", Arrow::BooleanDataType.new) + @visible_values = [true, false, true, false, true] + @valid_field = Arrow::Field.new("valid", Arrow::BooleanDataType.new) + @valid_values = [false, true, false, true, false] + + fields = [ + @visible_field, + @valid_field, + ] + schema = Arrow::Schema.new(fields) + columns = [ + build_boolean_array(@visible_values), + build_boolean_array(@valid_values), + ] + @record_batch = Arrow::RecordBatch.new(schema, + @visible_values.size, + columns) + end + + def test_export + require_gi_bindings(3, 4, 8) + success, c_abi_array, c_abi_schema = @record_batch.export + schema = Arrow::Schema.import(c_abi_schema) + assert_equal([success, @record_batch], + [true, Arrow::RecordBatch.import(c_abi_array, schema)]) + end + + sub_test_case("#equal") do + def setup + require_gi_bindings(3, 4, 2) + + @fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(@fields) + @columns = [ + build_boolean_array([true, false, true, false, true]), + build_boolean_array([false, true, false, true, false]), + ] + @record_batch = Arrow::RecordBatch.new(@schema, 5, @columns) + end + + def test_equal + other_record_batch = Arrow::RecordBatch.new(@schema, 5, @columns) + assert_equal(@record_batch, other_record_batch) + end + + def test_equal_metadata + schema_with_meta = @schema.with_metadata("key" => "value") + other_record_batch = Arrow::RecordBatch.new(schema_with_meta, 5, @columns) + + assert @record_batch.equal_metadata(other_record_batch, false) + assert do + not @record_batch.equal_metadata(other_record_batch, true) + end + end + end + + def test_schema + assert_equal(["visible", "valid"], + @record_batch.schema.fields.collect(&:name)) + end + + sub_test_case("#column_data") do + def test_positive + assert_equal(build_boolean_array(@valid_values), + @record_batch.get_column_data(1)) + end + + def test_negative + assert_equal(build_boolean_array(@visible_values), + @record_batch.get_column_data(-2)) + end + + def test_positive_out_of_index + assert_nil(@record_batch.get_column_data(2)) + end + + def test_negative_out_of_index + assert_nil(@record_batch.get_column_data(-3)) + end + end + + def test_n_columns + assert_equal(2, @record_batch.n_columns) + end + + def test_n_rows + assert_equal(5, @record_batch.n_rows) + end + + def test_slice + sub_record_batch = @record_batch.slice(3, 2) + sub_visible_values = sub_record_batch.n_rows.times.collect do |i| + sub_record_batch.get_column_data(0).get_value(i) + end + assert_equal([false, true], + sub_visible_values) + end + + def test_to_s + assert_equal(<<-PRETTY_PRINT, @record_batch.to_s) +visible: [ + true, + false, + true, + false, + true + ] +valid: [ + false, + true, + false, + true, + false + ] + PRETTY_PRINT + end + + def test_add_column + field = Arrow::Field.new("added", Arrow::BooleanDataType.new) + column = build_boolean_array([false, false, true, true, true]) + new_record_batch = @record_batch.add_column(1, field, column) + assert_equal(["visible", "added", "valid"], + new_record_batch.schema.fields.collect(&:name)) + end + + def test_remove_column + new_record_batch = @record_batch.remove_column(0) + assert_equal(["valid"], + new_record_batch.schema.fields.collect(&:name)) + end + + def test_serialize + buffer = @record_batch.serialize + input_stream = Arrow::BufferInputStream.new(buffer) + assert_equal(@record_batch, + input_stream.read_record_batch(@record_batch.schema)) + end + end +end diff --git a/src/arrow/c_glib/test/test-resizable-buffer.rb b/src/arrow/c_glib/test/test-resizable-buffer.rb new file mode 100644 index 000000000..84d95dec5 --- /dev/null +++ b/src/arrow/c_glib/test/test-resizable-buffer.rb @@ -0,0 +1,32 @@ +# 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 TestResizableBuffer < Test::Unit::TestCase + def setup + @buffer = Arrow::ResizableBuffer.new(0) + end + + def test_resize + @buffer.resize(1) + assert_equal(1, @buffer.size) + end + + def test_reserve + @buffer.reserve(1) + assert_equal(64, @buffer.capacity) + end +end diff --git a/src/arrow/c_glib/test/test-scalar-aggregate-options.rb b/src/arrow/c_glib/test/test-scalar-aggregate-options.rb new file mode 100644 index 000000000..a794b5324 --- /dev/null +++ b/src/arrow/c_glib/test/test-scalar-aggregate-options.rb @@ -0,0 +1,48 @@ +# 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 TestScalarAggregateOptions < Test::Unit::TestCase + def setup + @options = Arrow::ScalarAggregateOptions.new + end + + sub_test_case("skip_nulls") do + def test_default + assert do + @options.skip_nulls? + end + end + + def test_accessor + @options.skip_nulls = false + assert do + not @options.skip_nulls? + end + end + end + + sub_test_case("min_count") do + def test_default + assert_equal(1, @options.min_count) + end + + def test_accessor + @options.min_count = 0 + assert_equal(0, @options.min_count) + end + end +end diff --git a/src/arrow/c_glib/test/test-scalar-datum.rb b/src/arrow/c_glib/test/test-scalar-datum.rb new file mode 100644 index 000000000..17e5d6b06 --- /dev/null +++ b/src/arrow/c_glib/test/test-scalar-datum.rb @@ -0,0 +1,69 @@ +# 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 TestScalarDatum < Test::Unit::TestCase + include Helper::Buildable + + def setup + @scalar = Arrow::BooleanScalar.new(true) + @datum = Arrow::ScalarDatum.new(@scalar) + end + + def test_array? + assert do + not @datum.array? + end + end + + def test_array_like? + assert do + not @datum.array_like? + end + end + + def test_scalar? + assert do + @datum.scalar? + end + end + + def test_value? + assert do + @datum.value? + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::ScalarDatum.new(@scalar), + Arrow::ScalarDatum.new(@scalar)) + end + + def test_false + assert_not_equal(@datum, + Arrow::ArrayDatum.new(build_boolean_array([true, false]))) + end + end + + def test_to_string + assert_equal("Scalar", @datum.to_s) + end + + def test_value + assert_equal(@scalar, @datum.value) + end +end diff --git a/src/arrow/c_glib/test/test-schema.rb b/src/arrow/c_glib/test/test-schema.rb new file mode 100644 index 000000000..e90dd897a --- /dev/null +++ b/src/arrow/c_glib/test/test-schema.rb @@ -0,0 +1,214 @@ +# 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 TestSchema < Test::Unit::TestCase + include Helper::Omittable + + def test_export + require_gi_bindings(3, 4, 8) + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + c_abi_schema = schema.export + assert_equal(schema, + Arrow::Schema.import(c_abi_schema)) + end + + def test_equal + fields1 = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + fields2 = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + assert_equal(Arrow::Schema.new(fields1), + Arrow::Schema.new(fields2)) + end + + def test_field + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + assert_equal("enabled", schema.get_field(0).name) + end + + sub_test_case("#get_field_by_name") do + def test_found + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + assert_equal("enabled", schema.get_field_by_name("enabled").name) + end + + def test_not_found + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + assert_nil(schema.get_field_by_name("nonexistent")) + end + end + + sub_test_case("#get_field_index") do + def test_found + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + assert_equal(0, schema.get_field_index("enabled")) + end + + def test_not_found + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + assert_equal(-1, schema.get_field_index("nonexistent")) + end + end + + def test_n_fields + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + assert_equal(2, schema.n_fields) + end + + def test_fields + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + assert_equal(["enabled", "required"], + schema.fields.collect(&:name)) + end + + def test_to_s + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + assert_equal(<<-SCHEMA.chomp, schema.to_s) +enabled: bool +required: bool + SCHEMA + end + + sub_test_case("#to_string_metadata") do + def setup + require_gi_bindings(3, 4, 2) + + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + @schema = schema.with_metadata("key" => "value") + end + + def test_true + assert_equal(<<-SCHEMA.chomp, @schema.to_string_metadata(true)) +enabled: bool +required: bool +-- metadata -- +key: value + SCHEMA + end + + def test_false + assert_equal(<<-SCHEMA.chomp, @schema.to_string_metadata(false)) +enabled: bool +required: bool + SCHEMA + end + end + + def test_add_field + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new) + ] + schema = Arrow::Schema.new(fields) + new_field = Arrow::Field.new("new", Arrow::BooleanDataType.new) + new_schema = schema.add_field(1, new_field) + assert_equal(<<-SCHEMA.chomp, new_schema.to_s) +enabled: bool +new: bool +required: bool + SCHEMA + end + + def test_remove_field + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new) + ] + schema = Arrow::Schema.new(fields) + new_schema = schema.remove_field(0) + assert_equal(<<-SCHEMA.chomp, new_schema.to_s) +required: bool + SCHEMA + end + + def test_replace_field + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new) + ] + schema = Arrow::Schema.new(fields) + new_field = Arrow::Field.new("new", Arrow::BooleanDataType.new) + new_schema = schema.replace_field(1, new_field) + assert_equal(<<-SCHEMA.chomp, new_schema.to_s) +enabled: bool +new: bool + SCHEMA + end + + def test_has_metadata + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + schema = Arrow::Schema.new(fields) + assert do + not schema.has_metadata? + end + schema_with_metadata = schema.with_metadata("key" => "value") + assert do + schema_with_metadata.has_metadata? + end + end + + sub_test_case("#metadata") do + def setup + require_gi_bindings(3, 4, 2) + + fields = [ + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + Arrow::Field.new("required", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(fields) + end + + def test_existent + schema_with_metadata = @schema.with_metadata("key" => "value") + assert_equal({"key" => "value"}, + schema_with_metadata.metadata) + end + + def test_nonexistent + assert_nil(@schema.metadata) + end + end +end diff --git a/src/arrow/c_glib/test/test-set-lookup-options.rb b/src/arrow/c_glib/test/test-set-lookup-options.rb new file mode 100644 index 000000000..779bacef6 --- /dev/null +++ b/src/arrow/c_glib/test/test-set-lookup-options.rb @@ -0,0 +1,43 @@ +# 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 TestSetLookupOptions < Test::Unit::TestCase + include Helper::Buildable + + def test_new + value_set = Arrow::ArrayDatum.new(build_int8_array([1, 2, 3])) + options = Arrow::SetLookupOptions.new(value_set) + assert_equal(value_set, options.value_set) + end + + sub_test_case("instance methods") do + def setup + value_set = Arrow::ArrayDatum.new(build_int8_array([1, 2, 3])) + @options = Arrow::SetLookupOptions.new(value_set) + end + + def test_skip_nulls + assert do + not @options.skip_nulls? + end + @options.skip_nulls = true + assert do + @options.skip_nulls? + end + end + end +end diff --git a/src/arrow/c_glib/test/test-slow-file-system.rb b/src/arrow/c_glib/test/test-slow-file-system.rb new file mode 100644 index 000000000..0e36dca48 --- /dev/null +++ b/src/arrow/c_glib/test/test-slow-file-system.rb @@ -0,0 +1,43 @@ +# 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. + +require_relative "file-system-tests" + +class TestSlowFileSystem < Test::Unit::TestCase + def setup + Dir.mktmpdir do |tmpdir| + options = Arrow::LocalFileSystemOptions.new + local_fs = Arrow::LocalFileSystem.new(options) + subtree_fs = Arrow::SubTreeFileSystem.new(tmpdir, local_fs) + @fs = Arrow::SlowFileSystem.new(subtree_fs, 0.001) + yield + end + end + + include FileSystemTests + + def test_type_name + assert_equal([ + "slow", + "subtree", + ], + [ + @fs.type_name, + @fs.base_file_system.type_name, + ]) + end +end diff --git a/src/arrow/c_glib/test/test-sort-indices.rb b/src/arrow/c_glib/test/test-sort-indices.rb new file mode 100644 index 000000000..a8c4f40c5 --- /dev/null +++ b/src/arrow/c_glib/test/test-sort-indices.rb @@ -0,0 +1,69 @@ +# 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 TestSortIndices < Test::Unit::TestCase + include Helper::Buildable + + def test_array + array = build_int16_array([nil, 1, 0, nil, 4, 3]) + assert_equal(build_uint64_array([2, 1, 5, 4, 0, 3]), + array.sort_indices(:ascending)) + end + + def test_chunked_array + arrays = [ + build_int16_array([1]), + build_int16_array([0, 4, -3]), + ] + chunked_array = Arrow::ChunkedArray.new(arrays) + assert_equal(build_uint64_array([3, 1, 0, 2]), + chunked_array.sort_indices(:ascending)) + end + + def test_record_batch + columns = { + column1: build_int16_array([ 1, 0, 4, 4, -3, 1]), + column2: build_string_array(["a", "a", "b", "c", "d", "a"]), + } + record_batch = build_record_batch(columns) + sort_keys = [ + Arrow::SortKey.new("column1", :ascending), + Arrow::SortKey.new("column2", :descending), + ] + options = Arrow::SortOptions.new(sort_keys) + assert_equal(build_uint64_array([4, 1, 0, 5, 3, 2]), + record_batch.sort_indices(options)) + end + + def test_table + raw_array1 = [ 1, 0, 4, 4, -3, 1] + raw_array2 = ["a", "a", "b", "c", "d", "a"] + columns = { + column1: Arrow::ChunkedArray.new([build_int16_array(raw_array1[0...1]), + build_int16_array(raw_array1[1...3]), + build_int16_array(raw_array1[3..-1])]), + column2: Arrow::ChunkedArray.new([build_string_array(raw_array2[0...2]), + build_string_array(raw_array2[2..-1])]), + } + table = build_table(columns) + options = Arrow::SortOptions.new + options.add_sort_key(Arrow::SortKey.new("column1", :ascending)) + options.add_sort_key(Arrow::SortKey.new("column2", :descending)) + assert_equal(build_uint64_array([4, 1, 0, 5, 3, 2]), + table.sort_indices(options)) + end +end diff --git a/src/arrow/c_glib/test/test-sort-options.rb b/src/arrow/c_glib/test/test-sort-options.rb new file mode 100644 index 000000000..e57645b1c --- /dev/null +++ b/src/arrow/c_glib/test/test-sort-options.rb @@ -0,0 +1,59 @@ +# 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 TestSortOptions < Test::Unit::TestCase + include Helper::Buildable + + def test_new + sort_keys = [ + Arrow::SortKey.new("column1", :ascending), + Arrow::SortKey.new("column2", :descending), + ] + options = Arrow::SortOptions.new(sort_keys) + assert_equal(sort_keys, options.sort_keys) + end + + def test_add_sort_key + options = Arrow::SortOptions.new + options.add_sort_key(Arrow::SortKey.new("column1", :ascending)) + options.add_sort_key(Arrow::SortKey.new("column2", :descending)) + assert_equal([ + Arrow::SortKey.new("column1", :ascending), + Arrow::SortKey.new("column2", :descending), + ], + options.sort_keys) + end + + def test_set_sort_keys + options = Arrow::SortOptions.new([Arrow::SortKey.new("column3", :ascending)]) + sort_keys = [ + Arrow::SortKey.new("column1", :ascending), + Arrow::SortKey.new("column2", :descending), + ] + options.sort_keys = sort_keys + assert_equal(sort_keys, options.sort_keys) + end + + def test_equal + sort_keys = [ + Arrow::SortKey.new("column1", :ascending), + Arrow::SortKey.new("column2", :descending), + ] + assert_equal(Arrow::SortOptions.new(sort_keys), + Arrow::SortOptions.new(sort_keys)) + end +end diff --git a/src/arrow/c_glib/test/test-source-node.rb b/src/arrow/c_glib/test/test-source-node.rb new file mode 100644 index 000000000..e04c9b72b --- /dev/null +++ b/src/arrow/c_glib/test/test-source-node.rb @@ -0,0 +1,67 @@ +# 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 TestSourceNode < Test::Unit::TestCase + include Helper::Buildable + + def execute_plan(options) + plan = Arrow::ExecutePlan.new + source_node = plan.build_source_node(options) + sink_node_options = Arrow::SinkNodeOptions.new + sink_node = plan.build_sink_node(source_node, + sink_node_options) + plan.validate + plan.start + plan.wait + reader = sink_node_options.get_reader(source_node.output_schema) + table = reader.read_all + plan.stop + table + end + + def test_record_batch_reader + numbers = build_int8_array([1, 2, 3, 4, 5]) + strings = build_string_array(["a", "b", "a", "b", "a"]) + record_batch = build_record_batch(number: numbers, + string: strings) + reader = Arrow::RecordBatchReader.new([record_batch]) + options = Arrow::SourceNodeOptions.new(reader) + assert_equal(build_table(number: numbers, + string: strings), + execute_plan(options)) + end + + def test_record_batch + numbers = build_int8_array([1, 2, 3, 4, 5]) + strings = build_string_array(["a", "b", "a", "b", "a"]) + record_batch = build_record_batch(number: numbers, + string: strings) + options = Arrow::SourceNodeOptions.new(record_batch) + assert_equal(build_table(number: numbers, + string: strings), + execute_plan(options)) + end + + def test_table + numbers = build_int8_array([1, 2, 3, 4, 5]) + strings = build_string_array(["a", "b", "a", "b", "a"]) + table = build_table(number: numbers, + string: strings) + options = Arrow::SourceNodeOptions.new(table) + assert_equal(table, execute_plan(options)) + end +end diff --git a/src/arrow/c_glib/test/test-sparse-union-array.rb b/src/arrow/c_glib/test/test-sparse-union-array.rb new file mode 100644 index 000000000..1132ccb45 --- /dev/null +++ b/src/arrow/c_glib/test/test-sparse-union-array.rb @@ -0,0 +1,86 @@ +# 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 TestSparseUnionArray < Test::Unit::TestCase + include Helper::Buildable + + sub_test_case(".new") do + sub_test_case("default") do + def setup + type_ids = build_int8_array([0, 1, 0, 1, 0]) + fields = [ + build_int16_array([1, nil, nil, nil, 5]), + build_string_array([nil, "b", nil, "d", nil]), + ] + @array = Arrow::SparseUnionArray.new(type_ids, fields) + end + + def test_value_data_type + fields = [ + Arrow::Field.new("0", Arrow::Int16DataType.new), + Arrow::Field.new("1", Arrow::StringDataType.new), + ] + assert_equal(Arrow::SparseUnionDataType.new(fields, [0, 1]), + @array.value_data_type) + end + + def test_field + assert_equal([ + build_int16_array([1, nil, nil, nil, 5]), + build_string_array([nil, "b", nil, "d", nil]), + ], + [ + @array.get_field(0), + @array.get_field(1), + ]) + end + end + + sub_test_case("DataType") do + def setup + data_type_fields = [ + Arrow::Field.new("number", Arrow::Int16DataType.new), + Arrow::Field.new("text", Arrow::StringDataType.new), + ] + type_codes = [11, 13] + @data_type = Arrow::SparseUnionDataType.new(data_type_fields, type_codes) + type_ids = build_int8_array([11, 13, 11, 13, 11]) + fields = [ + build_int16_array([1, nil, nil, nil, 5]), + build_string_array([nil, "b", nil, "d", nil]), + ] + @array = Arrow::SparseUnionArray.new(@data_type, type_ids, fields) + end + + def test_value_data_type + assert_equal(@data_type, + @array.value_data_type) + end + + def test_field + assert_equal([ + build_int16_array([1, nil, nil, nil, 5]), + build_string_array([nil, "b", nil, "d", nil]), + ], + [ + @array.get_field(0), + @array.get_field(1), + ]) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-sparse-union-data-type.rb b/src/arrow/c_glib/test/test-sparse-union-data-type.rb new file mode 100644 index 000000000..bf65ae0a4 --- /dev/null +++ b/src/arrow/c_glib/test/test-sparse-union-data-type.rb @@ -0,0 +1,64 @@ +# 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 TestSparseUnionDataType < Test::Unit::TestCase + def setup + @number_field_data_type = Arrow::Int32DataType.new + @text_field_data_type = Arrow::StringDataType.new + @field_data_types = [ + @number_field_data_type, + @text_field_data_type, + ] + @number_field = Arrow::Field.new("number", @number_field_data_type) + @text_field = Arrow::Field.new("text", @text_field_data_type) + @fields = [ + @number_field, + @text_field, + ] + @data_type = Arrow::SparseUnionDataType.new(@fields, [2, 9]) + end + + def test_type + assert_equal(Arrow::Type::SPARSE_UNION, @data_type.id) + end + + def test_name + assert_equal("sparse_union", @data_type.name) + end + + def test_to_s + assert_equal("sparse_union<number: int32=2, text: string=9>", + @data_type.to_s) + end + + def test_fields + assert_equal(@fields.zip(@field_data_types), + @data_type.fields.collect {|field| [field, field.data_type]}) + end + + def test_get_field + field = @data_type.get_field(0) + assert_equal([ + @fields[0], + @field_data_types[0], + ], + [ + field, + field.data_type, + ]) + end +end diff --git a/src/arrow/c_glib/test/test-sparse-union-scalar.rb b/src/arrow/c_glib/test/test-sparse-union-scalar.rb new file mode 100644 index 000000000..a7f1b0695 --- /dev/null +++ b/src/arrow/c_glib/test/test-sparse-union-scalar.rb @@ -0,0 +1,58 @@ +# 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 TestSparseUnionScalar < Test::Unit::TestCase + def setup + fields = [ + Arrow::Field.new("number", Arrow::Int8DataType.new), + Arrow::Field.new("text", Arrow::StringDataType.new), + ] + @data_type = Arrow::SparseUnionDataType.new(fields, [2, 9]) + @type_code = 2 + @value = Arrow::Int8Scalar.new(-29) + @scalar = Arrow::SparseUnionScalar.new(@data_type, @type_code, @value) + end + + def test_type_code + assert_equal(@type_code, + @scalar.type_code) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::SparseUnionScalar.new(@data_type, @type_code, @value), + @scalar) + end + + def test_to_s + assert_equal("union{number: int8 = -29}", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-stream-writer.rb b/src/arrow/c_glib/test/test-stream-writer.rb new file mode 100644 index 000000000..32754e208 --- /dev/null +++ b/src/arrow/c_glib/test/test-stream-writer.rb @@ -0,0 +1,57 @@ +# 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 TestStreamWriter < Test::Unit::TestCase + include Helper::Buildable + + def test_write_record_batch + data = [true] + field = Arrow::Field.new("enabled", Arrow::BooleanDataType.new) + schema = Arrow::Schema.new([field]) + + tempfile = Tempfile.open("arrow-ipc-stream-writer") + output = Arrow::FileOutputStream.new(tempfile.path, false) + begin + stream_writer = Arrow::RecordBatchStreamWriter.new(output, schema) + begin + columns = [ + build_boolean_array(data), + ] + record_batch = Arrow::RecordBatch.new(schema, data.size, columns) + stream_writer.write_record_batch(record_batch) + ensure + stream_writer.close + end + ensure + output.close + end + + input = Arrow::MemoryMappedInputStream.new(tempfile.path) + begin + stream_reader = Arrow::RecordBatchStreamReader.new(input) + assert_equal([field.name], + stream_reader.schema.fields.collect(&:name)) + assert_equal(Arrow::RecordBatch.new(schema, + data.size, + [build_boolean_array(data)]), + stream_reader.read_next) + assert_nil(stream_reader.read_next) + ensure + input.close + end + end +end diff --git a/src/arrow/c_glib/test/test-string-array.rb b/src/arrow/c_glib/test/test-string-array.rb new file mode 100644 index 000000000..09a43d197 --- /dev/null +++ b/src/arrow/c_glib/test/test-string-array.rb @@ -0,0 +1,46 @@ +# 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 TestStringArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + value_offsets = Arrow::Buffer.new([0, 5, 11, 11].pack("l*")) + data = Arrow::Buffer.new("HelloWorld!") + assert_equal(build_string_array(["Hello", "World!", nil]), + Arrow::StringArray.new(3, + value_offsets, + data, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_value + builder = Arrow::StringArrayBuilder.new + builder.append_string("Hello") + array = builder.finish + assert_equal("Hello", array.get_string(0)) + end + + def test_buffer + builder = Arrow::StringArrayBuilder.new + builder.append_string("Hello") + builder.append_string("World") + array = builder.finish + assert_equal("HelloWorld", array.buffer.data.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-string-data-type.rb b/src/arrow/c_glib/test/test-string-data-type.rb new file mode 100644 index 000000000..87613d289 --- /dev/null +++ b/src/arrow/c_glib/test/test-string-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestStringDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::StringDataType.new + assert_equal(Arrow::Type::STRING, data_type.id) + end + + def test_name + data_type = Arrow::StringDataType.new + assert_equal("utf8", data_type.name) + end + + def test_to_s + data_type = Arrow::StringDataType.new + assert_equal("string", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-string-scalar.rb b/src/arrow/c_glib/test/test-string-scalar.rb new file mode 100644 index 000000000..3b9499ef9 --- /dev/null +++ b/src/arrow/c_glib/test/test-string-scalar.rb @@ -0,0 +1,55 @@ +# 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 TestStringScalar < Test::Unit::TestCase + def setup + @buffer = Arrow::Buffer.new("Hello") + @scalar = Arrow::StringScalar.new(@buffer) + end + + def test_data_type + assert_equal(Arrow::StringDataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::StringScalar.new(@buffer), + @scalar) + end + + def test_to_s + assert_equal("Hello", @scalar.to_s) + end + + def test_value + assert_equal(@buffer, + @scalar.value) + end + + def test_cast + buffer = Arrow::Buffer.new("-10") + scalar = Arrow::StringScalar.new(buffer) + assert_equal(Arrow::Int8Scalar.new(-10), + scalar.cast(Arrow::Int8DataType.new)) + end +end diff --git a/src/arrow/c_glib/test/test-struct-array.rb b/src/arrow/c_glib/test/test-struct-array.rb new file mode 100644 index 000000000..af7e299d8 --- /dev/null +++ b/src/arrow/c_glib/test/test-struct-array.rb @@ -0,0 +1,88 @@ +# 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 TestStructArray < Test::Unit::TestCase + include Helper::Buildable + + def test_new + fields = [ + Arrow::Field.new("score", Arrow::Int8DataType.new), + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + structs = [ + { + "score" => -29, + "enabled" => true, + }, + { + "score" => 2, + "enabled" => false, + }, + nil, + ] + struct_array1 = build_struct_array(fields, structs) + + data_type = Arrow::StructDataType.new(fields) + nulls = Arrow::Buffer.new([0b11].pack("C*")) + children = [ + Arrow::Int8Array.new(2, Arrow::Buffer.new([-29, 2].pack("C*")), nulls, 0), + Arrow::BooleanArray.new(2, Arrow::Buffer.new([0b01].pack("C*")), nulls, 0), + ] + assert_equal(struct_array1, + Arrow::StructArray.new(data_type, + 3, + children, + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_flatten + fields = [ + Arrow::Field.new("score", Arrow::Int8DataType.new), + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + data_type = Arrow::StructDataType.new(fields) + builder = Arrow::StructArrayBuilder.new(data_type) + + builder.append_value + builder.get_field_builder(0).append_value(-29) + builder.get_field_builder(1).append_value(true) + + builder.append_value + builder.field_builders[0].append_value(2) + builder.field_builders[1].append_value(false) + + array = builder.finish + values = array.length.times.collect do |i| + if i.zero? + [ + array.get_field(0).get_value(i), + array.get_field(1).get_value(i), + ] + else + array.flatten.collect do |field| + field.get_value(i) + end + end + end + assert_equal([ + [-29, true], + [2, false], + ], + values) + end +end diff --git a/src/arrow/c_glib/test/test-struct-data-type.rb b/src/arrow/c_glib/test/test-struct-data-type.rb new file mode 100644 index 000000000..15b43768c --- /dev/null +++ b/src/arrow/c_glib/test/test-struct-data-type.rb @@ -0,0 +1,115 @@ +# 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 TestStructDataType < Test::Unit::TestCase + def setup + @enabled_field_data_type = Arrow::BooleanDataType.new + @message_field_data_type = Arrow::StringDataType.new + @field_data_types = [ + @enabled_field_data_type, + @message_field_data_type, + ] + @enabled_field = Arrow::Field.new("enabled", @enabled_field_data_type) + @message_field = Arrow::Field.new("message", @message_field_data_type) + @fields = [@enabled_field, @message_field] + @data_type = Arrow::StructDataType.new(@fields) + end + + def test_type + assert_equal(Arrow::Type::STRUCT, @data_type.id) + end + + def test_name + assert_equal("struct", @data_type.name) + end + + def test_to_s + assert_equal("struct<enabled: bool, message: string>", + @data_type.to_s) + end + + def test_n_fields + assert_equal(2, @data_type.n_fields) + end + + def test_fields + assert_equal(@fields.zip(@field_data_types), + @data_type.fields.collect {|field| [field, field.data_type]}) + end + + sub_test_case("#get_field") do + def test_found + assert_equal(@fields[1], @data_type.get_field(1)) + end + + def test_negative + assert_equal(@fields[-1], @data_type.get_field(-1)) + end + + def test_over + assert_equal(nil, @data_type.get_field(2)) + end + + def test_data_type + field = @data_type.get_field(0) + assert_equal([ + @fields[0], + @field_data_types[0], + ], + [ + field, + field.data_type, + ]) + end + end + + sub_test_case("#get_field_by_name") do + def test_found + assert_equal(@enabled_field, + @data_type.get_field_by_name("enabled")) + end + + def test_not_found + assert_equal(nil, + @data_type.get_field_by_name("nonexistent")) + end + + def test_data_type + field = @data_type.get_field_by_name("enabled") + assert_equal([ + @enabled_field, + @enabled_field_data_type, + ], + [ + field, + field.data_type, + ]) + end + end + + sub_test_case("#get_field_index") do + def test_found + assert_equal(@fields.index(@enabled_field), + @data_type.get_field_index("enabled")) + end + + def test_not_found + assert_equal(-1, + @data_type.get_field_index("nonexistent")) + end + end +end diff --git a/src/arrow/c_glib/test/test-struct-scalar.rb b/src/arrow/c_glib/test/test-struct-scalar.rb new file mode 100644 index 000000000..9774943ba --- /dev/null +++ b/src/arrow/c_glib/test/test-struct-scalar.rb @@ -0,0 +1,55 @@ +# 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 TestStructScalar < Test::Unit::TestCase + def setup + fields = [ + Arrow::Field.new("score", Arrow::Int8DataType.new), + Arrow::Field.new("enabled", Arrow::BooleanDataType.new), + ] + @data_type = Arrow::StructDataType.new(fields) + @value = [ + Arrow::Int8Scalar.new(-29), + Arrow::BooleanScalar.new(true), + ] + @scalar = Arrow::StructScalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::StructScalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("{score:int8 = -29, enabled:bool = true}", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-table-batch-reader.rb b/src/arrow/c_glib/test/test-table-batch-reader.rb new file mode 100644 index 000000000..b161c8a55 --- /dev/null +++ b/src/arrow/c_glib/test/test-table-batch-reader.rb @@ -0,0 +1,42 @@ +# 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 TestTableBatchReader < Test::Unit::TestCase + include Helper::Buildable + + def test_empty + table = build_table("visible" => build_boolean_array([])) + reader = Arrow::TableBatchReader.new(table) + assert_nil(reader.read_next) + end + + def test_have_record + array = build_boolean_array([true]) + table = build_table("visible" => array) + reader = Arrow::TableBatchReader.new(table) + assert_equal(build_record_batch("visible" => array), + reader.read_next) + assert_nil(reader.read_next) + end + + def test_schema + array = build_boolean_array([]) + table = build_table("visible" => array) + reader = Arrow::TableBatchReader.new(table) + assert_equal(table.schema, reader.schema) + end +end diff --git a/src/arrow/c_glib/test/test-table-concatenate-options.rb b/src/arrow/c_glib/test/test-table-concatenate-options.rb new file mode 100644 index 000000000..055d6e270 --- /dev/null +++ b/src/arrow/c_glib/test/test-table-concatenate-options.rb @@ -0,0 +1,42 @@ +# 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 TestTableConcatenateOptions < Test::Unit::TestCase + def setup + @options = Arrow::TableConcatenateOptions.new + end + + def test_unify_schemas + assert do + not @options.unify_schemas? + end + @options.unify_schemas = true + assert do + @options.unify_schemas? + end + end + + def test_promote_nullability + assert do + @options.promote_nullability? + end + @options.promote_nullability = false + assert do + not @options.promote_nullability? + end + end +end diff --git a/src/arrow/c_glib/test/test-table-datum.rb b/src/arrow/c_glib/test/test-table-datum.rb new file mode 100644 index 000000000..7ff3997e8 --- /dev/null +++ b/src/arrow/c_glib/test/test-table-datum.rb @@ -0,0 +1,58 @@ +# 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 TestTableDatum < Test::Unit::TestCase + include Helper::Buildable + + def setup + @array = build_boolean_array([true, false]) + @table = build_table("visible" => @array) + @datum = Arrow::TableDatum.new(@table) + end + + def test_array? + assert do + not @datum.array? + end + end + + def test_array_like? + assert do + not @datum.array_like? + end + end + + sub_test_case("==") do + def test_true + assert_equal(Arrow::TableDatum.new(@table), + Arrow::TableDatum.new(@table)) + end + + def test_false + assert_not_equal(@datum, + Arrow::ArrayDatum.new(@array)) + end + end + + def test_to_string + assert_equal("Table", @datum.to_s) + end + + def test_value + assert_equal(@table, @datum.value) + end +end diff --git a/src/arrow/c_glib/test/test-table.rb b/src/arrow/c_glib/test/test-table.rb new file mode 100644 index 000000000..615a90c2f --- /dev/null +++ b/src/arrow/c_glib/test/test-table.rb @@ -0,0 +1,290 @@ +# 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 TestTable < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case(".new") do + def setup + @fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(@fields) + end + + def dump_table(table) + table.n_columns.times.collect do |i| + field = table.schema.get_field(i) + chunked_array = table.get_column_data(i) + values = [] + chunked_array.chunks.each do |chunk| + chunk.length.times do |j| + values << chunk.get_value(j) + end + end + [ + field.name, + values, + ] + end + end + + def test_arrays + require_gi_bindings(3, 3, 1) + arrays = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + table = Arrow::Table.new(@schema, arrays) + assert_equal([ + ["visible", [true]], + ["valid", [false]], + ], + dump_table(table)) + end + + def test_chunked_arrays + require_gi_bindings(3, 3, 1) + arrays = [ + Arrow::ChunkedArray.new([build_boolean_array([true]), + build_boolean_array([false])]), + Arrow::ChunkedArray.new([build_boolean_array([false]), + build_boolean_array([true])]), + ] + table = Arrow::Table.new(@schema, arrays) + assert_equal([ + ["visible", [true, false]], + ["valid", [false, true]], + ], + dump_table(table)) + end + + def test_record_batches + require_gi_bindings(3, 3, 1) + record_batches = [ + build_record_batch({ + "visible" => build_boolean_array([true]), + "valid" => build_boolean_array([false]) + }), + build_record_batch({ + "visible" => build_boolean_array([false]), + "valid" => build_boolean_array([true]) + }), + ] + table = Arrow::Table.new(@schema, record_batches) + + assert_equal([ + ["visible", [true, false]], + ["valid", [false, true]], + ], + dump_table(table)) + end + end + + sub_test_case("instance methods") do + def setup + @fields = [ + Arrow::Field.new("visible", Arrow::BooleanDataType.new), + Arrow::Field.new("valid", Arrow::BooleanDataType.new), + ] + @schema = Arrow::Schema.new(@fields) + @columns = [ + build_boolean_array([true]), + build_boolean_array([false]), + ] + @table = Arrow::Table.new(@schema, @columns) + end + + def test_equal + other_table = Arrow::Table.new(@schema, @columns) + assert_equal(@table, other_table) + end + + def test_equal_metadata + other_table = Arrow::Table.new(@schema, @columns) + assert do + @table.equal_metadata(other_table, true) + end + end + + def test_schema + assert_equal(["visible", "valid"], + @table.schema.fields.collect(&:name)) + end + + def test_column_data + assert_equal([ + Arrow::ChunkedArray.new([build_boolean_array([true])]), + Arrow::ChunkedArray.new([build_boolean_array([false])]), + ], + [ + @table.get_column_data(0), + @table.get_column_data(-1), + ]) + end + + def test_n_columns + assert_equal(2, @table.n_columns) + end + + def test_n_rows + assert_equal(1, @table.n_rows) + end + + def test_add_column + field = Arrow::Field.new("added", Arrow::BooleanDataType.new) + chunked_array = Arrow::ChunkedArray.new([build_boolean_array([true])]) + new_table = @table.add_column(1, field, chunked_array) + assert_equal(["visible", "added", "valid"], + new_table.schema.fields.collect(&:name)) + end + + def test_remove_column + new_table = @table.remove_column(0) + assert_equal(["valid"], + new_table.schema.fields.collect(&:name)) + end + + def test_replace_column + field = Arrow::Field.new("added", Arrow::BooleanDataType.new) + chunked_array = Arrow::ChunkedArray.new([build_boolean_array([true])]) + new_table = @table.replace_column(0, field, chunked_array) + assert_equal(["added", "valid"], + new_table.schema.fields.collect(&:name)) + end + + def test_to_s + table = build_table("valid" => build_boolean_array([true, false, true])) + assert_equal(<<-TABLE, table.to_s) +valid: bool +---- +valid: + [ + [ + true, + false, + true + ] + ] + TABLE + end + + sub_test_case("#concatenate") do + def test_without_options + table = build_table("visible" => + build_boolean_array([true, false, true, false])) + table1 = build_table("visible" => build_boolean_array([true])) + table2 = build_table("visible" => build_boolean_array([false, true])) + table3 = build_table("visible" => build_boolean_array([false])) + assert_equal(table, table1.concatenate([table2, table3])) + end + + def test_with_options + options = Arrow::TableConcatenateOptions.new + options.unify_schemas = true + table = build_table("a" => build_int32_array([1, nil, 3]), + "b" => build_int32_array([10, nil, 30]), + "c" => build_int32_array([nil, 200, nil])) + table1 = build_table("a" => build_int32_array([1]), + "b" => build_int32_array([10])) + table2 = build_table("c" => build_int32_array([200])) + table3 = build_table("a" => build_int32_array([3]), + "b" => build_int32_array([30])) + assert_equal(table, table1.concatenate([table2, table3], options)) + end + end + + sub_test_case("#slice") do + test("offset: positive") do + visibles = [true, false, true] + table = build_table("visible" => build_boolean_array(visibles)) + assert_equal(build_table("visible" => build_boolean_array([false, true])), + table.slice(1, 2)) + end + + test("offset: negative") do + visibles = [true, false, true] + table = build_table("visible" => build_boolean_array(visibles)) + assert_equal(build_table("visible" => build_boolean_array([false, true])), + table.slice(-2, 2)) + end + end + + def test_combine_chunks + table = build_table( + "visible" => Arrow::ChunkedArray::new([build_boolean_array([true, false, true]), + build_boolean_array([false, true]), + build_boolean_array([false])]) + ) + combined_table = table.combine_chunks + all_values = combined_table.n_columns.times.collect do |i| + column = combined_table.get_column_data(i) + column.n_chunks.times.collect do |j| + column.get_chunk(j).values + end + end + assert_equal([[[true, false, true, false, true, false]]], + all_values) + end + + sub_test_case("#write_as_feather") do + def setup + super + @tempfile = Tempfile.open("arrow-table-write-as-feather") + begin + yield + ensure + @tempfile.close! + end + end + + def read_feather + input = Arrow::MemoryMappedInputStream.new(@tempfile.path) + reader = Arrow::FeatherFileReader.new(input) + begin + yield(reader.read) + ensure + input.close + end + end + + test("default") do + output = Arrow::FileOutputStream.new(@tempfile.path, false) + @table.write_as_feather(output) + output.close + + read_feather do |read_table| + assert_equal(@table, read_table) + end + end + + test("compression") do + output = Arrow::FileOutputStream.new(@tempfile.path, false) + properties = Arrow::FeatherWriteProperties.new + properties.compression = :zstd + @table.write_as_feather(output, properties) + output.close + + read_feather do |read_table| + assert_equal(@table, read_table) + end + end + end + end +end diff --git a/src/arrow/c_glib/test/test-take.rb b/src/arrow/c_glib/test/test-take.rb new file mode 100644 index 000000000..f97c7ad73 --- /dev/null +++ b/src/arrow/c_glib/test/test-take.rb @@ -0,0 +1,214 @@ +# 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 TestTake < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + sub_test_case("Array") do + def test_no_null + indices = build_int16_array([1, 0, 2]) + assert_equal(build_int16_array([0, 1, 2]), + build_int16_array([1, 0 ,2]).take(indices)) + end + + def test_null + indices = build_int16_array([2, nil, 0]) + assert_equal(build_int16_array([2, nil, 1]), + build_int16_array([1, 0, 2]).take(indices)) + end + + def test_out_of_index + indices = build_int16_array([1, 2, 3]) + assert_raise(Arrow::Error::Index) do + build_int16_array([0, 1, 2]).take(indices) + end + end + + def test_chunked_array + taken_chunks = [ + build_int16_array([0, 1]), + build_int16_array([2]) + ] + taken_chunked_array = Arrow::ChunkedArray.new(taken_chunks) + indices_chunks = [ + build_int16_array([1, 0]), + build_int16_array([2]) + ] + indices = Arrow::ChunkedArray.new(indices_chunks) + assert_equal(taken_chunked_array, + build_int16_array([1, 0, 2]).take_chunked_array(indices)) + end + end + + sub_test_case("Table") do + def setup + fields = [ + Arrow::Field.new("field1", Arrow::Int16DataType.new), + Arrow::Field.new("field2", Arrow::Int16DataType.new) + ] + @schema = Arrow::Schema.new(fields) + arrays = [ + build_int16_array([0, 1, 2]), + build_int16_array([3, 5, 4]) + ] + @table = Arrow::Table.new(@schema, arrays) + end + + def test_no_null + arrays = [ + build_int16_array([1, 0, 2]), + build_int16_array([5, 3, 4]) + ] + taken_table = Arrow::Table.new(@schema, arrays) + indices = build_int16_array([1, 0, 2]) + assert_equal(taken_table, + @table.take(indices)) + end + + def test_null + arrays = [ + build_int16_array([2, nil, 0]), + build_int16_array([4, nil, 3]) + ] + taken_table = Arrow::Table.new(@schema, arrays) + indices = build_int16_array([2, nil, 0]) + assert_equal(taken_table, + @table.take(indices)) + end + + def test_out_of_index + indices = build_int16_array([1, 2, 3]) + assert_raise(Arrow::Error::Index) do + @table.take(indices) + end + end + + def test_chunked_array + arrays = [ + build_int16_array([1, 0, 2]), + build_int16_array([5, 3, 4]) + ] + taken_table = Arrow::Table.new(@schema, arrays) + chunks = [ + build_int16_array([1, 0]), + build_int16_array([2]) + ] + indices = Arrow::ChunkedArray.new(chunks) + assert_equal(taken_table, + @table.take_chunked_array(indices)) + end + end + + sub_test_case("ChunkedArray") do + def setup + chunks = [ + build_int16_array([1, 0]), + build_int16_array([2]), + ] + @chunked_array = Arrow::ChunkedArray.new(chunks) + end + + def test_no_null + chunks = [ + build_int16_array([0, 1]), + build_int16_array([2]) + ] + taken_chunked_array = Arrow::ChunkedArray.new(chunks) + indices = build_int16_array([1, 0, 2]) + assert_equal(taken_chunked_array, + @chunked_array.take(indices)) + end + + def test_null + chunks = [ + build_int16_array([2, nil]), + build_int16_array([1]) + ] + taken_chunked_array = Arrow::ChunkedArray.new(chunks) + indices = build_int16_array([2, nil, 0]) + assert_equal(taken_chunked_array, + @chunked_array.take(indices)) + end + + def test_out_of_index + indices = build_int16_array([1, 2, 3]) + assert_raise(Arrow::Error::Index) do + @chunked_array.take(indices) + end + end + + def test_chunked_array + taken_chunks = [ + build_int16_array([0, 1]), + build_int16_array([2]) + ] + taken_chunked_array = Arrow::ChunkedArray.new(taken_chunks) + indices_chunks = [ + build_int16_array([1, 0]), + build_int16_array([2]) + ] + indices = Arrow::ChunkedArray.new(indices_chunks) + assert_equal(taken_chunked_array, + @chunked_array.take_chunked_array(indices)) + end + end + + sub_test_case("RecordBatch") do + def setup + fields = [ + Arrow::Field.new("field1", Arrow::Int16DataType.new), + Arrow::Field.new("field2", Arrow::Int16DataType.new) + ] + @schema = Arrow::Schema.new(fields) + columns = [ + build_int16_array([1, 0, 2]), + build_int16_array([3, 5, 4]) + ] + @record_batch = Arrow::RecordBatch.new(@schema, 3, columns) + end + + def test_no_null + columns = [ + build_int16_array([0, 1, 2]), + build_int16_array([5, 3, 4]) + ] + taken_record_batch = Arrow::RecordBatch.new(@schema, 3, columns) + indices = build_int16_array([1, 0, 2]) + assert_equal(taken_record_batch, + @record_batch.take(indices)) + end + + def test_null + columns = [ + build_int16_array([2, nil, 1]), + build_int16_array([4, nil, 3]) + ] + taken_record_batch = Arrow::RecordBatch.new(@schema, 3, columns) + indices = build_int16_array([2, nil, 0]) + assert_equal(taken_record_batch, + @record_batch.take(indices)) + end + + def test_out_of_index + indices = build_int16_array([1, 2, 3]) + assert_raise(Arrow::Error::Index) do + @record_batch.take(indices) + end + end + end +end diff --git a/src/arrow/c_glib/test/test-tensor.rb b/src/arrow/c_glib/test/test-tensor.rb new file mode 100644 index 000000000..31f2556c4 --- /dev/null +++ b/src/arrow/c_glib/test/test-tensor.rb @@ -0,0 +1,125 @@ +# 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 TestTensor < Test::Unit::TestCase + include Helper::Omittable + + def setup + @raw_data = [ + 1, 2, + 3, 4, + + 5, 6, + 7, 8, + + 9, 10, + 11, 12, + ] + data = Arrow::Buffer.new(@raw_data.pack("c*")) + @shape = [3, 2, 2] + strides = [] + names = ["a", "b", "c"] + @tensor = Arrow::Tensor.new(Arrow::Int8DataType.new, + data, + @shape, + strides, + names) + end + + def test_equal + data = Arrow::Buffer.new(@raw_data.pack("c*")) + strides = [] + names = ["a", "b", "c"] + other_tensor = Arrow::Tensor.new(Arrow::Int8DataType.new, + data, + @shape, + strides, + names) + assert_equal(@tensor, + other_tensor) + end + + def test_value_data_type + assert_equal(Arrow::Int8DataType, @tensor.value_data_type.class) + end + + def test_value_type + assert_equal(Arrow::Type::INT8, @tensor.value_type) + end + + def test_buffer + assert_equal(@raw_data, @tensor.buffer.data.to_s.unpack("c*")) + end + + def test_shape + require_gi_bindings(3, 3, 1) + assert_equal(@shape, @tensor.shape) + end + + def test_strides + require_gi_bindings(3, 3, 1) + assert_equal([4, 2, 1], @tensor.strides) + end + + def test_n_dimensions + assert_equal(@shape.size, @tensor.n_dimensions) + end + + def test_dimension_name + dimension_names = @tensor.n_dimensions.times.collect do |i| + @tensor.get_dimension_name(i) + end + assert_equal(["a", "b", "c"], + dimension_names) + end + + def test_size + assert_equal(@raw_data.size, @tensor.size) + end + + def test_mutable? + assert do + not @tensor.mutable? + end + end + + def test_contiguous? + assert do + @tensor.contiguous? + end + end + + def test_row_major? + assert do + @tensor.row_major? + end + end + + def test_column_major? + assert do + not @tensor.column_major? + end + end + + def test_io + buffer = Arrow::ResizableBuffer.new(0) + output = Arrow::BufferOutputStream.new(buffer) + output.write_tensor(@tensor) + input = Arrow::BufferInputStream.new(buffer) + assert_equal(@tensor, input.read_tensor) + end +end diff --git a/src/arrow/c_glib/test/test-time-data-type.rb b/src/arrow/c_glib/test/test-time-data-type.rb new file mode 100644 index 000000000..a7b1503fd --- /dev/null +++ b/src/arrow/c_glib/test/test-time-data-type.rb @@ -0,0 +1,24 @@ +# 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 TestTimeDataType < Test::Unit::TestCase + def test_type + assert do + Arrow::TimeDataType.gtype.abstract? + end + end +end diff --git a/src/arrow/c_glib/test/test-time32-array.rb b/src/arrow/c_glib/test/test-time32-array.rb new file mode 100644 index 000000000..b648c69f8 --- /dev/null +++ b/src/arrow/c_glib/test/test-time32-array.rb @@ -0,0 +1,69 @@ +# 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 TestTime32Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + midnight = 0 + after_midnight = 60 * 10 # 00:10:00 + raw_data = [midnight, after_midnight] + data_type = Arrow::Time32DataType.new(:second) + assert_equal(build_time32_array(:second, [*raw_data, nil]), + Arrow::Time32Array.new(data_type, + 3, + Arrow::Buffer.new(raw_data.pack("l*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + midnight = 0 + after_midnight = 60 * 10 # 00:10:00 + raw_data = [midnight, after_midnight] + array = build_time32_array(:second, raw_data) + assert_equal(raw_data.pack("l*"), + array.buffer.data.to_s) + end + + def test_value + after_midnight = 60 * 10 # 00:10:00 + array = build_time32_array(:second, [after_midnight]) + assert_equal(after_midnight, array.get_value(0)) + end + + def test_values + midnight = 0 + after_midnight = 60 * 10 # 00:10:00 + raw_data = [midnight, after_midnight] + array = build_time32_array(:second, raw_data) + assert_equal(raw_data, array.values) + end + + sub_test_case("unit") do + def test_second + array = build_time32_array(:second, []) + assert_equal(Arrow::TimeUnit::SECOND, array.value_data_type.unit) + end + + def test_milli + array = build_time32_array(:milli, []) + assert_equal(Arrow::TimeUnit::MILLI, array.value_data_type.unit) + end + end +end diff --git a/src/arrow/c_glib/test/test-time32-data-type.rb b/src/arrow/c_glib/test/test-time32-data-type.rb new file mode 100644 index 000000000..6ecf327fe --- /dev/null +++ b/src/arrow/c_glib/test/test-time32-data-type.rb @@ -0,0 +1,56 @@ +# 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 TestTime32DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Time32DataType.new(:second) + assert_equal(Arrow::Type::TIME32, data_type.id) + end + + def test_name + data_type = Arrow::Time32DataType.new(:second) + assert_equal("time32", data_type.name) + end + + def test_invalid_unit + message = + "[time32-data-type][new] time unit must be second or milli: <micro>" + assert_raise(Arrow::Error::Invalid.new(message)) do + Arrow::Time32DataType.new(:micro) + end + end + + sub_test_case("second") do + def setup + @data_type = Arrow::Time32DataType.new(:second) + end + + def test_to_s + assert_equal("time32[s]", @data_type.to_s) + end + end + + sub_test_case("milli") do + def setup + @data_type = Arrow::Time32DataType.new(:milli) + end + + def test_to_s + assert_equal("time32[ms]", @data_type.to_s) + end + end +end diff --git a/src/arrow/c_glib/test/test-time32-scalar.rb b/src/arrow/c_glib/test/test-time32-scalar.rb new file mode 100644 index 000000000..94c0a7592 --- /dev/null +++ b/src/arrow/c_glib/test/test-time32-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestTime32Scalar < Test::Unit::TestCase + def setup + @data_type = Arrow::Time32DataType.new(:second) + @value = 60 * 10 # 00:10:00 + @scalar = Arrow::Time32Scalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Time32Scalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("00:10:00", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-time64-array.rb b/src/arrow/c_glib/test/test-time64-array.rb new file mode 100644 index 000000000..775d3153a --- /dev/null +++ b/src/arrow/c_glib/test/test-time64-array.rb @@ -0,0 +1,57 @@ +# 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 TestTime64Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + midnight = 0 + after_midnight = 60 * 10 * 1000 * 1000 # 00:10:00.000000 + raw_data = [midnight, after_midnight] + data_type = Arrow::Time64DataType.new(:micro) + assert_equal(build_time64_array(:micro, [*raw_data, nil]), + Arrow::Time64Array.new(data_type, + 3, + Arrow::Buffer.new(raw_data.pack("q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + midnight = 0 + after_midnight = 60 * 10 * 1000 * 1000 # 00:10:00.000000 + raw_data = [midnight, after_midnight] + array = build_time64_array(:micro, raw_data) + assert_equal(raw_data.pack("q*"), + array.buffer.data.to_s) + end + + def test_value + after_midnight = 60 * 10 * 1000 * 1000 # 00:10:00.000000 + array = build_time64_array(:micro, [after_midnight]) + assert_equal(after_midnight, array.get_value(0)) + end + + def test_values + midnight = 0 + after_midnight = 60 * 10 * 1000 * 1000 # 00:10:00.000000 + raw_data = [midnight, after_midnight] + array = build_time64_array(:micro, raw_data) + assert_equal(raw_data, array.values) + end +end diff --git a/src/arrow/c_glib/test/test-time64-data-type.rb b/src/arrow/c_glib/test/test-time64-data-type.rb new file mode 100644 index 000000000..812e9220b --- /dev/null +++ b/src/arrow/c_glib/test/test-time64-data-type.rb @@ -0,0 +1,56 @@ +# 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 TestTime64DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::Time64DataType.new(:micro) + assert_equal(Arrow::Type::TIME64, data_type.id) + end + + def test_name + data_type = Arrow::Time64DataType.new(:micro) + assert_equal("time64", data_type.name) + end + + def test_invalid_unit + message = + "[time64-data-type][new] time unit must be micro or nano: <second>" + assert_raise(Arrow::Error::Invalid.new(message)) do + Arrow::Time64DataType.new(:second) + end + end + + sub_test_case("micro") do + def setup + @data_type = Arrow::Time64DataType.new(:micro) + end + + def test_to_s + assert_equal("time64[us]", @data_type.to_s) + end + end + + sub_test_case("nano") do + def setup + @data_type = Arrow::Time64DataType.new(:nano) + end + + def test_to_s + assert_equal("time64[ns]", @data_type.to_s) + end + end +end diff --git a/src/arrow/c_glib/test/test-time64-scalar.rb b/src/arrow/c_glib/test/test-time64-scalar.rb new file mode 100644 index 000000000..fb2843ca6 --- /dev/null +++ b/src/arrow/c_glib/test/test-time64-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestTime64Scalar < Test::Unit::TestCase + def setup + @data_type = Arrow::Time64DataType.new(:micro) + @value = 60 * 10 * 1000 * 1000 # 00:10:00.000000 + @scalar = Arrow::Time64Scalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::Time64Scalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("00:10:00.000000", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-timestamp-array.rb b/src/arrow/c_glib/test/test-timestamp-array.rb new file mode 100644 index 000000000..793402323 --- /dev/null +++ b/src/arrow/c_glib/test/test-timestamp-array.rb @@ -0,0 +1,57 @@ +# 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 TestTimestampArray < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + epoch = 0 + after_epoch = 1504953190854 # 2017-09-09T10:33:10.854Z + raw_data = [epoch, after_epoch] + data_type = Arrow::TimestampDataType.new(:milli) + assert_equal(build_timestamp_array(:milli, [*raw_data, nil]), + Arrow::TimestampArray.new(data_type, + 3, + Arrow::Buffer.new(raw_data.pack("q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + epoch = 0 + after_epoch = 1504953190854 # 2017-09-09T10:33:10.854Z + raw_data = [epoch, after_epoch] + array = build_timestamp_array(:milli, raw_data) + assert_equal(raw_data.pack("q*"), + array.buffer.data.to_s) + end + + def test_value + after_epoch = 1504953190854 # 2017-09-09T10:33:10.854Z + array = build_timestamp_array(:milli, [after_epoch]) + assert_equal(after_epoch, array.get_value(0)) + end + + def test_values + epoch = 0 + after_epoch = 1504953190854 # 2017-09-09T10:33:10.854Z + raw_data = [epoch, after_epoch] + array = build_timestamp_array(:milli, raw_data) + assert_equal(raw_data, array.values) + end +end diff --git a/src/arrow/c_glib/test/test-timestamp-data-type.rb b/src/arrow/c_glib/test/test-timestamp-data-type.rb new file mode 100644 index 000000000..dac3a9bc6 --- /dev/null +++ b/src/arrow/c_glib/test/test-timestamp-data-type.rb @@ -0,0 +1,84 @@ +# 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 TestTimestampDataType < Test::Unit::TestCase + def test_type + data_type = Arrow::TimestampDataType.new(:micro) + assert_equal(Arrow::Type::TIMESTAMP, data_type.id) + end + + def test_name + data_type = Arrow::TimestampDataType.new(:micro) + assert_equal("timestamp", data_type.name) + end + + sub_test_case("second") do + def setup + @data_type = Arrow::TimestampDataType.new(:second) + end + + def test_to_s + assert_equal("timestamp[s]", @data_type.to_s) + end + + def test_unit + assert_equal(Arrow::TimeUnit::SECOND, @data_type.unit) + end + end + + sub_test_case("millisecond") do + def setup + @data_type = Arrow::TimestampDataType.new(:milli) + end + + def test_to_s + assert_equal("timestamp[ms]", @data_type.to_s) + end + + def test_unit + assert_equal(Arrow::TimeUnit::MILLI, @data_type.unit) + end + end + + sub_test_case("micro") do + def setup + @data_type = Arrow::TimestampDataType.new(:micro) + end + + def test_to_s + assert_equal("timestamp[us]", @data_type.to_s) + end + + def test_unit + assert_equal(Arrow::TimeUnit::MICRO, @data_type.unit) + end + end + + sub_test_case("nano") do + def setup + @data_type = Arrow::TimestampDataType.new(:nano) + end + + def test_to_s + assert_equal("timestamp[ns]", @data_type.to_s) + end + + def test_unit + assert_equal(Arrow::TimeUnit::NANO, @data_type.unit) + end + end +end diff --git a/src/arrow/c_glib/test/test-timestamp-scalar.rb b/src/arrow/c_glib/test/test-timestamp-scalar.rb new file mode 100644 index 000000000..9aa676b5d --- /dev/null +++ b/src/arrow/c_glib/test/test-timestamp-scalar.rb @@ -0,0 +1,48 @@ +# 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 TestTimestampScalar < Test::Unit::TestCase + def setup + @data_type = Arrow::TimestampDataType.new(:milli) + @value = 1504953190854 # 2017-09-09T10:33:10.854Z + @scalar = Arrow::TimestampScalar.new(@data_type, @value) + end + + def test_data_type + assert_equal(@data_type, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::TimestampScalar.new(@data_type, @value), + @scalar) + end + + def test_to_s + assert_equal("2017-09-09 10:33:10.854", @scalar.to_s) + end + + def test_value + assert_equal(@value, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-uint-array-builder.rb b/src/arrow/c_glib/test/test-uint-array-builder.rb new file mode 100644 index 000000000..89621189b --- /dev/null +++ b/src/arrow/c_glib/test/test-uint-array-builder.rb @@ -0,0 +1,59 @@ +# 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 TestUIntArrayBuilder < Test::Unit::TestCase + include Helper::Buildable + + def test_uint8 + values = [0, 2] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt8Array.new(3, + Arrow::Buffer.new(values.pack("C*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint16 + border_value = 2 ** 8 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt16Array.new(3, + Arrow::Buffer.new(values.pack("S*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint32 + border_value = 2 ** 16 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt32Array.new(3, + Arrow::Buffer.new(values.pack("L*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_uint64 + border_value = 2 ** 32 + values = [0, border_value] + assert_equal(build_uint_array([*values, nil]), + Arrow::UInt64Array.new(3, + Arrow::Buffer.new(values.pack("Q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end +end diff --git a/src/arrow/c_glib/test/test-uint16-array.rb b/src/arrow/c_glib/test/test-uint16-array.rb new file mode 100644 index 000000000..a02f8338a --- /dev/null +++ b/src/arrow/c_glib/test/test-uint16-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 TestUInt16Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_uint16_array([1, 2, nil]), + Arrow::UInt16Array.new(3, + Arrow::Buffer.new([1, 2].pack("S*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::UInt16ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4].pack("S*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::UInt16ArrayBuilder.new + builder.append_value(1) + array = builder.finish + assert_equal(1, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::UInt16ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4], array.values) + end + + def test_sum + array = build_uint8_array([2, 4, nil]) + assert_equal(6, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-uint16-data-type.rb b/src/arrow/c_glib/test/test-uint16-data-type.rb new file mode 100644 index 000000000..e91489e08 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint16-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestUInt16DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::UInt16DataType.new + assert_equal(Arrow::Type::UINT16, data_type.id) + end + + def test_name + data_type = Arrow::UInt16DataType.new + assert_equal("uint16", data_type.name) + end + + def test_to_s + data_type = Arrow::UInt16DataType.new + assert_equal("uint16", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-uint16-scalar.rb b/src/arrow/c_glib/test/test-uint16-scalar.rb new file mode 100644 index 000000000..000d620b3 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint16-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestUInt16Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::UInt16Scalar.new((2 ** 16) - 1) + end + + def test_data_type + assert_equal(Arrow::UInt16DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::UInt16Scalar.new((2 ** 16) - 1), + @scalar) + end + + def test_to_s + assert_equal(((2 ** 16) - 1).to_s, @scalar.to_s) + end + + def test_value + assert_equal((2 ** 16) - 1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-uint32-array.rb b/src/arrow/c_glib/test/test-uint32-array.rb new file mode 100644 index 000000000..04eb60b37 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint32-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 TestUInt32Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_uint32_array([1, 2, nil]), + Arrow::UInt32Array.new(3, + Arrow::Buffer.new([1, 2].pack("L*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::UInt32ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4].pack("L*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::UInt32ArrayBuilder.new + builder.append_value(1) + array = builder.finish + assert_equal(1, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::UInt32ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4], array.values) + end + + def test_sum + array = build_uint32_array([2, 4, nil]) + assert_equal(6, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-uint32-data-type.rb b/src/arrow/c_glib/test/test-uint32-data-type.rb new file mode 100644 index 000000000..dbe8c34a0 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint32-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestUInt32DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::UInt32DataType.new + assert_equal(Arrow::Type::UINT32, data_type.id) + end + + def test_name + data_type = Arrow::UInt32DataType.new + assert_equal("uint32", data_type.name) + end + + def test_to_s + data_type = Arrow::UInt32DataType.new + assert_equal("uint32", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-uint32-scalar.rb b/src/arrow/c_glib/test/test-uint32-scalar.rb new file mode 100644 index 000000000..c41f99330 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint32-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestUInt32Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::UInt32Scalar.new((2 ** 32) - 1) + end + + def test_data_type + assert_equal(Arrow::UInt32DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::UInt32Scalar.new((2 ** 32) - 1), + @scalar) + end + + def test_to_s + assert_equal(((2 ** 32) - 1).to_s, @scalar.to_s) + end + + def test_value + assert_equal((2 ** 32) - 1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-uint64-array.rb b/src/arrow/c_glib/test/test-uint64-array.rb new file mode 100644 index 000000000..f34e6813e --- /dev/null +++ b/src/arrow/c_glib/test/test-uint64-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 TestUInt64Array < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_new + assert_equal(build_uint64_array([1, 2, nil]), + Arrow::UInt64Array.new(3, + Arrow::Buffer.new([1, 2].pack("Q*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::UInt64ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4].pack("Q*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::UInt64ArrayBuilder.new + builder.append_value(1) + array = builder.finish + assert_equal(1, array.get_value(0)) + end + + def test_values + require_gi_bindings(3, 1, 7) + builder = Arrow::UInt64ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4], array.values) + end + + def test_sum + array = build_uint64_array([2, 4, nil]) + assert_equal(6, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-uint64-data-type.rb b/src/arrow/c_glib/test/test-uint64-data-type.rb new file mode 100644 index 000000000..bd53b48bc --- /dev/null +++ b/src/arrow/c_glib/test/test-uint64-data-type.rb @@ -0,0 +1,33 @@ +# 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 TestUInt64DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::UInt64DataType.new + assert_equal(Arrow::Type::UINT64, data_type.id) + end + + def test_name + data_type = Arrow::UInt64DataType.new + assert_equal("uint64", data_type.name) + end + + def test_to_s + data_type = Arrow::UInt64DataType.new + assert_equal("uint64", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-uint64-scalar.rb b/src/arrow/c_glib/test/test-uint64-scalar.rb new file mode 100644 index 000000000..19c12461c --- /dev/null +++ b/src/arrow/c_glib/test/test-uint64-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestUInt64Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::UInt64Scalar.new((2 ** 64) - 1) + end + + def test_data_type + assert_equal(Arrow::UInt64DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::UInt64Scalar.new((2 ** 64) - 1), + @scalar) + end + + def test_to_s + assert_equal(((2 ** 64) - 1).to_s, @scalar.to_s) + end + + def test_value + assert_equal((2 ** 64) - 1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-uint8-array.rb b/src/arrow/c_glib/test/test-uint8-array.rb new file mode 100644 index 000000000..e22b1b12c --- /dev/null +++ b/src/arrow/c_glib/test/test-uint8-array.rb @@ -0,0 +1,58 @@ +# 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 TestUInt8Array < Test::Unit::TestCase + include Helper::Buildable + + def test_new + assert_equal(build_uint8_array([1, 2, nil]), + Arrow::UInt8Array.new(3, + Arrow::Buffer.new([1, 2].pack("C*")), + Arrow::Buffer.new([0b011].pack("C*")), + -1)) + end + + def test_buffer + builder = Arrow::UInt8ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4].pack("C*"), array.buffer.data.to_s) + end + + def test_value + builder = Arrow::UInt8ArrayBuilder.new + builder.append_value(1) + array = builder.finish + assert_equal(1, array.get_value(0)) + end + + def test_values + builder = Arrow::UInt8ArrayBuilder.new + builder.append_value(1) + builder.append_value(2) + builder.append_value(4) + array = builder.finish + assert_equal([1, 2, 4], array.values) + end + + def test_sum + array = build_uint8_array([2, 4, nil]) + assert_equal(6, array.sum) + end +end diff --git a/src/arrow/c_glib/test/test-uint8-data-type.rb b/src/arrow/c_glib/test/test-uint8-data-type.rb new file mode 100644 index 000000000..cf8f89dbd --- /dev/null +++ b/src/arrow/c_glib/test/test-uint8-data-type.rb @@ -0,0 +1,40 @@ +# 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 TestUInt8DataType < Test::Unit::TestCase + def test_type + data_type = Arrow::UInt8DataType.new + assert_equal(Arrow::Type::UINT8, data_type.id) + end + + def test_signed? + data_type = Arrow::UInt8DataType.new + assert do + not data_type.signed? + end + end + + def test_name + data_type = Arrow::UInt8DataType.new + assert_equal("uint8", data_type.name) + end + + def test_to_s + data_type = Arrow::UInt8DataType.new + assert_equal("uint8", data_type.to_s) + end +end diff --git a/src/arrow/c_glib/test/test-uint8-scalar.rb b/src/arrow/c_glib/test/test-uint8-scalar.rb new file mode 100644 index 000000000..54bc1c954 --- /dev/null +++ b/src/arrow/c_glib/test/test-uint8-scalar.rb @@ -0,0 +1,46 @@ +# 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 TestUInt8Scalar < Test::Unit::TestCase + def setup + @scalar = Arrow::UInt8Scalar.new((2 ** 8) - 1) + end + + def test_data_type + assert_equal(Arrow::UInt8DataType.new, + @scalar.data_type) + end + + def test_valid? + assert do + @scalar.valid? + end + end + + def test_equal + assert_equal(Arrow::UInt8Scalar.new((2 ** 8) - 1), + @scalar) + end + + def test_to_s + assert_equal(((2 ** 8) - 1).to_s, @scalar.to_s) + end + + def test_value + assert_equal((2 ** 8) - 1, @scalar.value) + end +end diff --git a/src/arrow/c_glib/test/test-unique.rb b/src/arrow/c_glib/test/test-unique.rb new file mode 100644 index 000000000..b94ff462b --- /dev/null +++ b/src/arrow/c_glib/test/test-unique.rb @@ -0,0 +1,31 @@ +# 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 TestUnique < Test::Unit::TestCase + include Helper::Buildable + include Helper::Omittable + + def test_int32 + assert_equal(build_int32_array([1, 3, -1, -3]), + build_int32_array([1, 3, 1, -1, -3, -1]).unique) + end + + def test_string + assert_equal(build_string_array(["Ruby", "Python"]), + build_string_array(["Ruby", "Python", "Ruby"]).unique) + end +end diff --git a/src/arrow/c_glib/test/test-variance-options.rb b/src/arrow/c_glib/test/test-variance-options.rb new file mode 100644 index 000000000..64bdf670b --- /dev/null +++ b/src/arrow/c_glib/test/test-variance-options.rb @@ -0,0 +1,46 @@ +# 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 TestVarianceOptions < Test::Unit::TestCase + include Helper::Buildable + + def setup + @options = Arrow::VarianceOptions.new + end + + def test_ddof + assert_equal(0, @options.ddof) + @options.ddof = 1 + assert_equal(1, @options.ddof) + end + + def test_skip_nulls + assert do + @options.skip_nulls? + end + @options.skip_nulls = false + assert do + not @options.skip_nulls? + end + end + + def test_min_count + assert_equal(0, @options.min_count) + @options.min_count = 1 + assert_equal(1, @options.min_count) + end +end diff --git a/src/arrow/c_glib/test/test-write-options.rb b/src/arrow/c_glib/test/test-write-options.rb new file mode 100644 index 000000000..c528ce673 --- /dev/null +++ b/src/arrow/c_glib/test/test-write-options.rb @@ -0,0 +1,102 @@ +# 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 TestWriteOptions < Test::Unit::TestCase + def setup + @options = Arrow::WriteOptions.new + end + + sub_test_case("allow-64bit") do + def test_default + assert do + not @options.allow_64bit? + end + end + + def test_accessor + @options.allow_64bit = true + assert do + @options.allow_64bit? + end + end + end + + sub_test_case("max-recursion-depth") do + def test_default + assert_equal(64, @options.max_recursion_depth) + end + + def test_accessor + @options.max_recursion_depth = 29 + assert_equal(29, @options.max_recursion_depth) + end + end + + + sub_test_case("alignment") do + def test_default + assert_equal(8, @options.alignment) + end + + def test_accessor + @options.alignment = 64 + assert_equal(64, @options.alignment) + end + end + + sub_test_case("write-legacy-ipc-format") do + def test_default + assert do + not @options.write_legacy_ipc_format? + end + end + + def test_accessor + @options.write_legacy_ipc_format = true + assert do + @options.write_legacy_ipc_format? + end + end + end + + sub_test_case("codec") do + def test_default + assert_nil(@options.codec) + end + + def test_accessor + @options.codec = Arrow::Codec.new(:zstd) + assert_equal("zstd", + @options.codec.name) + end + end + + sub_test_case("use-threads") do + def test_default + assert do + @options.use_threads? + end + end + + def test_accessor + @options.use_threads = false + assert do + not @options.use_threads? + end + end + end +end |